summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/entity.h3
-rw-r--r--src/graphics/animation.cpp73
-rw-r--r--src/graphics/animation.h18
-rw-r--r--src/main.cpp65
4 files changed, 112 insertions, 47 deletions
diff --git a/src/entity.h b/src/entity.h
index 3875c22..a1681d1 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -6,9 +6,6 @@
class Entity{
public:
- Vec2 position;
- Vec2 size;
- Vec2 speed;
};
#endif // ENTITY_H
diff --git a/src/graphics/animation.cpp b/src/graphics/animation.cpp
index f326c22..b93387e 100644
--- a/src/graphics/animation.cpp
+++ b/src/graphics/animation.cpp
@@ -7,24 +7,32 @@ namespace Graphics{
// Basic animation, loops through the pictures
Animation::Animation(){}
- Animation::Animation(const char* filename, int count, int switchTime) :
+ Animation::Animation(const sf::Texture& texture, int count, int switchTime,
+ int height = 0, int width = 0, int row = 0) :
i_ (0),
count_ (count),
+ row_ (row),
lastTime_ (0),
switchTime_ (switchTime){
- texture_.loadFromFile(filename);
-
sf::Vector2u size = texture_.getSize();
- h_ = size.y;
- if (size.x % count != 0){
- printf("Animation: size not divisible by %d\n", count);
+ if ( height == 0 ){
+ h_ = size.y;
+ } else {
+ h_ = height;
+ }
+
+ if ( width == 0 ){
+ if (size.x % count != 0){
+ printf("Animation: size not divisible by %d\n", count);
+ }
+ w_ = size.x / count_;
+ } else {
+ w_ = width;
}
- w_ = size.x / count_;
- rect_ = sf::IntRect(0,0,w_,h_); // set to first picture in
- // animation
- sprite_ = sf::Sprite(texture_, rect_);
- sprite_.setScale(8,8);
+
+ rect_ = sf::IntRect(0,row_ * h_,w_,h_); // set to first picture in
+ // animation
}
bool Animation::ticked(int deltaTime){
@@ -36,9 +44,9 @@ namespace Graphics{
return true;
}
- sf::Sprite& Animation::next(int deltaTime){
+ sf::IntRect& Animation::next(int deltaTime){
if ( !ticked(deltaTime) ){
- return sprite_;
+ return rect_;
}
if (i_ < count_ -1 ){
i_ += 1;
@@ -47,10 +55,13 @@ namespace Graphics{
}
rect_.left = i_ * w_;
- sprite_.setTexture(texture_); // This shouldn't be needed...!
- sprite_.setTextureRect(rect_);
+ return rect_;
+ }
- return sprite_;
+ void Animation::reset(){
+ i_ = 0;
+ rect_.left = i_ * w_;
+ rect_.top = row_ * h_;
}
Animation::~Animation(){}
@@ -58,43 +69,42 @@ namespace Graphics{
// ONESHOT ANIMATION
OneShotAnimation::OneShotAnimation(){}
- OneShotAnimation::OneShotAnimation(const char* filename, int count, int switchTime):
- Animation(filename, count, switchTime) {
+ OneShotAnimation::OneShotAnimation(const sf::Texture& texture, int count,
+ int switchTime, int height = 0, int width = 0, int row = 0):
+ Animation(texture, count, switchTime, height, width, row) {
}
bool OneShotAnimation::finished(){
return i_ == count_ - 1;
}
- sf::Sprite& OneShotAnimation::next(int deltaTime){
+ sf::IntRect& OneShotAnimation::next(int deltaTime){
if ( !ticked(deltaTime) ){
- return sprite_;
+ return rect_;
}
if ( finished() ){
- return sprite_;
+ return rect_;
}
if (i_ < count_ -1 ){
i_ += 1;
}
rect_.left = i_ * w_;
- sprite_.setTexture(texture_); // This shouldn't be needed...!
- sprite_.setTextureRect(rect_);
-
- return sprite_;
+ return rect_;
}
// BOUNCING ANIMATION
BouncingAnimation::BouncingAnimation(){}
- BouncingAnimation::BouncingAnimation(const char* filename, int count, int switchTime):
- Animation(filename, count, switchTime),
+ BouncingAnimation::BouncingAnimation(const sf::Texture& texture, int count,
+ int switchTime, int height = 0, int width = 0, int row = 0):
+ Animation(texture, count, switchTime, height, width, row),
up_ (true) {
}
- sf::Sprite& BouncingAnimation::next(int deltaTime){
+ sf::IntRect& BouncingAnimation::next(int deltaTime){
if( !ticked(deltaTime) ) {
- return sprite_;
+ return rect_;
}
// Rewrite this and eliminate the boolean (use the count as
// positive/negative)
@@ -112,10 +122,7 @@ namespace Graphics{
}
rect_.left = i_ * w_;
- sprite_.setTexture(texture_); // This shouldn't be needed...!
- sprite_.setTextureRect(rect_);
-
- return sprite_;
+ return rect_;
}
BouncingAnimation::~BouncingAnimation(){}
diff --git a/src/graphics/animation.h b/src/graphics/animation.h
index 87b4427..9f5dfa5 100644
--- a/src/graphics/animation.h
+++ b/src/graphics/animation.h
@@ -9,26 +9,29 @@ namespace Graphics{
protected:
int i_;
int count_;
+ int row_;
int lastTime_;
int switchTime_;
unsigned int w_, h_;
sf::IntRect rect_;
sf::Texture texture_;
- sf::Sprite sprite_;
bool ticked(int deltaTime);
public:
Animation();
~Animation();
- Animation(const char* filename, int count, int switchTime);
- sf::Sprite& next(int deltaTime);
+ Animation(const sf::Texture& texture, int count, int switchTime,
+ int height, int width, int row);
+ sf::IntRect& next(int deltaTime);
+ void reset();
};
class OneShotAnimation : Animation {
public:
OneShotAnimation();
- OneShotAnimation(const char* filename, int count, int switchTime);
+ OneShotAnimation(const sf::Texture& texture, int count, int
+ switchTime, int height, int width, int row);
bool finished();
- sf::Sprite& next(int deltaTime);
+ sf::IntRect& next(int deltaTime);
};
class BouncingAnimation : Animation{
@@ -36,9 +39,10 @@ namespace Graphics{
bool up_;
public:
BouncingAnimation();
- BouncingAnimation(const char* filename, int count, int switchTime);
+ BouncingAnimation(const sf::Texture& texture, int count, int
+ switchTime, int height, int width, int row);
~BouncingAnimation();
- sf::Sprite& next(int deltaTime);
+ sf::IntRect& next(int deltaTime);
};
}
#endif // GRAPHICS_ANIMATION_H
diff --git a/src/main.cpp b/src/main.cpp
index 509f691..38d1e06 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,17 +1,63 @@
#include<SFML/Graphics.hpp>
+#include<vector>
#include<cstdio>
+#include<cmath>
#include "graphics/animation.h"
#include "entity.h"
-class Unit: public Entity{
+class Unit: Entity{
+ private:
+ sf::Texture spritesheet_;
public:
- Graphics::Animation animation;
+
+ enum class Anim : int{
+ standing = 0,
+ walking = 1,
+ shooting = 2,
+ complaining = 3,
+ dying = 4,
+ };
+ std::vector<int> animation_frame_count { 6, 8, 6, 1, 14 };
+ Graphics::Animation walking_animation;
+ Graphics::Animation standing_animation;
+ Graphics::Animation shooting_animation;
+ Graphics::Animation complaining_animation;
+ Graphics::Animation dying_animation;
+ Graphics::Animation& current_animation = standing_animation;
+ sf::Sprite sprite;
+
+ sf::Vector2f position;
+ sf::Vector2f size;
+ sf::Vector2f speed;
+
Unit(){
- animation = Graphics::Animation("angle1.png", 6, 60);
+ spritesheet_.loadFromFile("angle1_all.png");
+ standing_animation = Graphics::Animation(spritesheet_, 6, 60, 44, 48, 0);
+ walking_animation = Graphics::Animation(spritesheet_, 8, 60, 44, 48, 1);
+ shooting_animation = Graphics::Animation(spritesheet_, 6, 60, 44, 48, 2);
+ complaining_animation = Graphics::Animation(spritesheet_, 1, 60, 44, 48, 3);
+ dying_animation = Graphics::Animation(spritesheet_, 14, 60, 44, 48, 4);
+
+ position = sf::Vector2f{0,0};
+ speed = sf::Vector2f{0,0};
};
+ void walk(float vx, float vy){
+ speed.y = vy;
+ speed.x = vx;
+ current_animation.reset();
+ current_animation = walking_animation;
+ walking_animation.reset();
+ }
~Unit(){};
+ void update(int dt){
+ position.x += round(speed.x * dt);
+ sprite.setTexture(spritesheet_);
+ sprite.setScale(8,8);
+ sprite.setTextureRect(current_animation.next(dt));
+ sprite.setPosition(position.x, position.y);
+ }
};
int main()
@@ -24,16 +70,27 @@ int main()
renderWindow.setFramerateLimit(60);
+ long elapsed_time = 0;
+ bool once = true;
+
while (renderWindow.isOpen()){
int dt = clock.getElapsedTime().asMilliseconds();
+ elapsed_time += dt;
clock.restart();
while (renderWindow.pollEvent(event)){
if (event.type == sf::Event::EventType::Closed)
renderWindow.close();
}
+ if(elapsed_time > 1000 && once){
+ unit.walk(0.3, 0);
+ once = false;
+ }
+
+ unit.update(dt);
+
renderWindow.clear();
- renderWindow.draw(unit.animation.next(dt));
+ renderWindow.draw(unit.sprite);
renderWindow.display();
}
}