summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/entity.h14
-rw-r--r--src/graphics/animation.cpp122
-rw-r--r--src/graphics/animation.h44
-rw-r--r--src/graphics/window.h8
-rw-r--r--src/main.cpp39
-rw-r--r--src/vec.h7
6 files changed, 234 insertions, 0 deletions
diff --git a/src/entity.h b/src/entity.h
new file mode 100644
index 0000000..3875c22
--- /dev/null
+++ b/src/entity.h
@@ -0,0 +1,14 @@
+#ifndef ENTITY_H
+#define ENTITY_H
+
+#include "vec.h"
+
+class Entity{
+
+public:
+ Vec2 position;
+ Vec2 size;
+ Vec2 speed;
+};
+
+#endif // ENTITY_H
diff --git a/src/graphics/animation.cpp b/src/graphics/animation.cpp
new file mode 100644
index 0000000..f326c22
--- /dev/null
+++ b/src/graphics/animation.cpp
@@ -0,0 +1,122 @@
+#include "animation.h"
+
+#include <cstdio>
+
+namespace Graphics{
+
+ // Basic animation, loops through the pictures
+ Animation::Animation(){}
+
+ Animation::Animation(const char* filename, int count, int switchTime) :
+ i_ (0),
+ count_ (count),
+ 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);
+ }
+ 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);
+ }
+
+ bool Animation::ticked(int deltaTime){
+ lastTime_ += deltaTime;
+ if ( switchTime_ > lastTime_ ){
+ return false;
+ }
+ lastTime_ = 0;
+ return true;
+ }
+
+ sf::Sprite& Animation::next(int deltaTime){
+ if ( !ticked(deltaTime) ){
+ return sprite_;
+ }
+ if (i_ < count_ -1 ){
+ i_ += 1;
+ } else {
+ i_ = 0;
+ }
+ rect_.left = i_ * w_;
+
+ sprite_.setTexture(texture_); // This shouldn't be needed...!
+ sprite_.setTextureRect(rect_);
+
+ return sprite_;
+ }
+
+ Animation::~Animation(){}
+
+ // ONESHOT ANIMATION
+ OneShotAnimation::OneShotAnimation(){}
+
+ OneShotAnimation::OneShotAnimation(const char* filename, int count, int switchTime):
+ Animation(filename, count, switchTime) {
+ }
+
+ bool OneShotAnimation::finished(){
+ return i_ == count_ - 1;
+ }
+
+ sf::Sprite& OneShotAnimation::next(int deltaTime){
+ if ( !ticked(deltaTime) ){
+ return sprite_;
+ }
+ if ( finished() ){
+ return sprite_;
+ }
+ if (i_ < count_ -1 ){
+ i_ += 1;
+ }
+ rect_.left = i_ * w_;
+
+ sprite_.setTexture(texture_); // This shouldn't be needed...!
+ sprite_.setTextureRect(rect_);
+
+ return sprite_;
+ }
+
+ // BOUNCING ANIMATION
+ BouncingAnimation::BouncingAnimation(){}
+
+ BouncingAnimation::BouncingAnimation(const char* filename, int count, int switchTime):
+ Animation(filename, count, switchTime),
+ up_ (true) {
+ }
+
+ sf::Sprite& BouncingAnimation::next(int deltaTime){
+ if( !ticked(deltaTime) ) {
+ return sprite_;
+ }
+ // Rewrite this and eliminate the boolean (use the count as
+ // positive/negative)
+ if ( up_ ) {
+ if( i_ == count_ - 1){
+ up_ = false;
+ } else {
+ i_++;
+ }
+ } else {
+ i_--;
+ if( i_ == 0 ){
+ up_ = true;
+ }
+ }
+ rect_.left = i_ * w_;
+
+ sprite_.setTexture(texture_); // This shouldn't be needed...!
+ sprite_.setTextureRect(rect_);
+
+ return sprite_;
+ }
+
+ BouncingAnimation::~BouncingAnimation(){}
+}
diff --git a/src/graphics/animation.h b/src/graphics/animation.h
new file mode 100644
index 0000000..87b4427
--- /dev/null
+++ b/src/graphics/animation.h
@@ -0,0 +1,44 @@
+#ifndef GRAPHICS_ANIMATION_H
+#define GRAPHICS_ANIMATION_H
+
+#include <SFML/Graphics.hpp>
+
+namespace Graphics{
+
+ class Animation{
+ protected:
+ int i_;
+ int count_;
+ 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);
+ };
+
+ class OneShotAnimation : Animation {
+ public:
+ OneShotAnimation();
+ OneShotAnimation(const char* filename, int count, int switchTime);
+ bool finished();
+ sf::Sprite& next(int deltaTime);
+ };
+
+ class BouncingAnimation : Animation{
+ private:
+ bool up_;
+ public:
+ BouncingAnimation();
+ BouncingAnimation(const char* filename, int count, int switchTime);
+ ~BouncingAnimation();
+ sf::Sprite& next(int deltaTime);
+ };
+}
+#endif // GRAPHICS_ANIMATION_H
diff --git a/src/graphics/window.h b/src/graphics/window.h
new file mode 100644
index 0000000..91e15c8
--- /dev/null
+++ b/src/graphics/window.h
@@ -0,0 +1,8 @@
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include<SFML/Graphics.hpp>
+
+class Window ()
+
+#endif //WINDOW_H
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..509f691
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,39 @@
+#include<SFML/Graphics.hpp>
+#include<cstdio>
+
+#include "graphics/animation.h"
+#include "entity.h"
+
+class Unit: public Entity{
+ public:
+ Graphics::Animation animation;
+
+ Unit(){
+ animation = Graphics::Animation("angle1.png", 6, 60);
+ };
+ ~Unit(){};
+};
+
+int main()
+{
+ sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Demo Game");
+
+ sf::Event event;
+ sf::Clock clock;
+ Unit unit;
+
+ renderWindow.setFramerateLimit(60);
+
+ while (renderWindow.isOpen()){
+ int dt = clock.getElapsedTime().asMilliseconds();
+ clock.restart();
+ while (renderWindow.pollEvent(event)){
+ if (event.type == sf::Event::EventType::Closed)
+ renderWindow.close();
+ }
+
+ renderWindow.clear();
+ renderWindow.draw(unit.animation.next(dt));
+ renderWindow.display();
+ }
+}
diff --git a/src/vec.h b/src/vec.h
new file mode 100644
index 0000000..a983256
--- /dev/null
+++ b/src/vec.h
@@ -0,0 +1,7 @@
+#ifndef VEC_H
+#define VEC_H
+struct Vec2{
+ int x;
+ int y;
+};
+#endif // VEC_H