aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-04-06 16:14:38 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-04-06 16:14:38 +0100
commit0e355cacba945cab5e50739b20e4709557768a4f (patch)
treec617db42f036765676f2317a9fd35b225255b5c1
parent8986a9cd869c3f0fe38f9610110bfd9e2d20dd74 (diff)
downloadArider-0e355cacba945cab5e50739b20e4709557768a4f.tar.gz
Arider-0e355cacba945cab5e50739b20e4709557768a4f.zip
Added animations but have to improve class
-rw-r--r--CMakeLists.txt9
-rw-r--r--config/ariderconfig.hpp.in (renamed from config/arider_config.hpp.in)0
-rw-r--r--include/animation.hpp41
-rw-r--r--include/ariderconfig.hpp (renamed from include/arider_config.hpp)0
-rw-r--r--include/character.hpp40
-rw-r--r--include/game.hpp9
-rw-r--r--include/player.hpp23
-rw-r--r--src/animation.cpp32
-rw-r--r--src/character.cpp19
-rw-r--r--src/game.cpp110
-rw-r--r--src/main.cpp2
-rw-r--r--src/player.cpp35
12 files changed, 270 insertions, 50 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index eb97744..e421dc6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,14 +23,17 @@ set(YAGE_LIBRARIES YAGE)
# setting up configuration header
configure_file (
- ${CMAKE_SOURCE_DIR}/config/arider_config.hpp.in
- ${CMAKE_SOURCE_DIR}/include/arider_config.hpp
+ ${CMAKE_SOURCE_DIR}/config/ariderconfig.hpp.in
+ ${CMAKE_SOURCE_DIR}/include/ariderconfig.hpp
)
# adding my executable
add_executable(${PROJECT_NAME}
+ ${CMAKE_SOURCE_DIR}/src/animation.cpp
+ ${CMAKE_SOURCE_DIR}/src/character.cpp
${CMAKE_SOURCE_DIR}/src/game.cpp
- ${CMAKE_SOURCE_DIR}/src/main.cpp)
+ ${CMAKE_SOURCE_DIR}/src/main.cpp
+ ${CMAKE_SOURCE_DIR}/src/player.cpp)
# adding sdl2 library
include(FindPkgConfig)
diff --git a/config/arider_config.hpp.in b/config/ariderconfig.hpp.in
index 5674160..5674160 100644
--- a/config/arider_config.hpp.in
+++ b/config/ariderconfig.hpp.in
diff --git a/include/animation.hpp b/include/animation.hpp
new file mode 100644
index 0000000..122dc91
--- /dev/null
+++ b/include/animation.hpp
@@ -0,0 +1,41 @@
+#ifndef ANIMATION_HPP
+#define ANIMATION_HPP
+
+#include <YAGE/resourcemanager.hpp>
+#include <YAGE/texture.hpp>
+
+#include <glm/glm.hpp>
+
+#include <iostream>
+#include <unordered_map>
+#include <vector>
+
+class Animation
+{
+private:
+ std::unordered_map<std::string, std::vector<yage::Texture>> frame_animations_;
+ std::string current_animation_;
+ int current_index_=0;
+public:
+ Animation() {}
+
+ void pushFrame(const std::string &animation_name, const std::string &texture_path);
+ yage::Texture currentFrame() const;
+ void start(const std::string &animation_name);
+ void nextFrame();
+
+ template<typename First, typename Second, typename ...Rest>
+ void initializeAnimation(const std::string &animation_name, First &&first, Second &&second, Rest &&...rest)
+ {
+ frame_animations_[animation_name].push_back(yage::ResourceManager::getTexture(std::forward<First>(first)));
+ initializeAnimation(std::forward<Second>(second), std::forward<Rest>(rest)...);
+ }
+
+ template<typename Last>
+ void initializeAnimation(const std::string &animation_name, Last &&last)
+ {
+ frame_animations_[animation_name].push_back(yage::ResourceManager::getTexture(std::forward<Last>(last)));
+ }
+};
+
+#endif
diff --git a/include/arider_config.hpp b/include/ariderconfig.hpp
index f71fe78..f71fe78 100644
--- a/include/arider_config.hpp
+++ b/include/ariderconfig.hpp
diff --git a/include/character.hpp b/include/character.hpp
new file mode 100644
index 0000000..4f45017
--- /dev/null
+++ b/include/character.hpp
@@ -0,0 +1,40 @@
+#ifndef CHARACTER_HPP
+#define CHARACTER_HPP
+
+#include "animation.hpp"
+
+#include <glm/glm.hpp>
+
+#include <YAGE/spritebatch.hpp>
+#include <YAGE/texture.hpp>
+
+#include <string>
+#include <vector>
+
+class Character
+{
+public:
+ Animation animation_;
+protected:
+ glm::vec4 uv_;
+ glm::vec2 position_;
+ glm::vec2 size_;
+ glm::vec2 speed_;
+
+public:
+ Character() {}
+ virtual ~Character() {}
+
+ virtual void moveUp()=0;
+ virtual void moveRight()=0;
+ virtual void moveDown()=0;
+ virtual void moveLeft()=0;
+ virtual void idle()=0;
+
+ void create(const glm::vec2 &position=glm::vec2(0.f, 0.f),
+ const glm::vec2 &size=glm::vec2(50.f, 50.f),
+ const glm::vec2 &speed=glm::vec2(1.f, 1.f));
+ void renderSprite(yage::SpriteBatch &sprite_batch) const;
+};
+
+#endif
diff --git a/include/game.hpp b/include/game.hpp
index 5ebf69e..d70332d 100644
--- a/include/game.hpp
+++ b/include/game.hpp
@@ -1,9 +1,11 @@
#ifndef GAME_HPP
#define GAME_HPP
+#include "player.hpp"
+
#include <YAGE/camera2d.hpp>
#include <YAGE/glslprogram.hpp>
-#include <YAGE/gltexture.hpp>
+#include <YAGE/texture.hpp>
#include <YAGE/inputmanager.hpp>
#include <YAGE/spritebatch.hpp>
#include <YAGE/window.hpp>
@@ -27,7 +29,7 @@ private:
// initializer game state
GameState game_state_=GameState::PLAY;
// set timer to 0
- float time_=0;
+ int time_=0;
// window
yage::Window window_;
// camera
@@ -38,6 +40,8 @@ private:
yage::SpriteBatch sprite_batch_;
// input manager
yage::InputManager input_manager_;
+ // player
+ Player player_;
// member functions
public:
@@ -51,6 +55,7 @@ private:
void processInput();
void gameLoop();
void drawGame();
+ void renderSprites();
float calculateFps();
};
diff --git a/include/player.hpp b/include/player.hpp
new file mode 100644
index 0000000..3569049
--- /dev/null
+++ b/include/player.hpp
@@ -0,0 +1,23 @@
+#ifndef PLAYER_HPP
+#define PLAYER_HPP
+
+#include "character.hpp"
+
+#include <YAGE/resourcemanager.hpp>
+#include <YAGE/texture.hpp>
+
+#include <glm/glm.hpp>
+
+class Player : public Character
+{
+public:
+ Player();
+
+ void moveUp();
+ void moveRight();
+ void moveDown();
+ void moveLeft();
+ void idle();
+};
+
+#endif
diff --git a/src/animation.cpp b/src/animation.cpp
new file mode 100644
index 0000000..d9bafbb
--- /dev/null
+++ b/src/animation.cpp
@@ -0,0 +1,32 @@
+#include "animation.hpp"
+
+#include <iostream>
+
+void Animation::pushFrame(const std::string &animation_name, const std::string &texture_path)
+{
+ frame_animations_[animation_name].push_back(yage::ResourceManager::getTexture(texture_path));
+}
+
+yage::Texture Animation::currentFrame() const
+{
+ return frame_animations_.find(current_animation_)->second[current_index_];
+}
+
+void Animation::start(const std::string &animation_name)
+{
+ if(current_animation_!=animation_name)
+ {
+ current_animation_=animation_name;
+ }
+}
+
+void Animation::nextFrame()
+{
+ if(current_index_+1<(int)frame_animations_.find(current_animation_)->second.size())
+ ++current_index_;
+ else
+ current_index_=0;
+ std::cout<<"id: "<<frame_animations_.find(current_animation_)->second[current_index_].id<<'\n';
+ std::cout<<"index: "<<current_index_<<'\n';
+ std::cout<<"size: "<<(int)frame_animations_.find(current_animation_)->second.size()<<'\n';
+}
diff --git a/src/character.cpp b/src/character.cpp
new file mode 100644
index 0000000..ef76289
--- /dev/null
+++ b/src/character.cpp
@@ -0,0 +1,19 @@
+#include "character.hpp"
+
+#include <YAGE/resourcemanager.hpp>
+#include <YAGE/vertex.hpp>
+
+void Character::create(const glm::vec2 &position/*=glm::vec2(0.f, 0.f)*/,
+ const glm::vec2 &size/*=glm::vec2(50.f, 50.f)*/,
+ const glm::vec2 &speed/*=glm::vec2(1.f, 1.f)*/)
+{
+ uv_=glm::vec4(0.f, 0.f, 1.f, 1.f);
+ position_=position;
+ size_=size;
+ speed_=speed;
+}
+
+void Character::renderSprite(yage::SpriteBatch &sprite_batch) const
+{
+ sprite_batch.draw(glm::vec4(position_.x, position_.y, size_.x, size_.y), uv_, animation_.currentFrame().id, yage::Color(255, 255, 255, 255), 0.f);
+}
diff --git a/src/game.cpp b/src/game.cpp
index 232ca95..851579d 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1,10 +1,11 @@
+#include "animation.hpp"
#include "game.hpp"
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <SDL2/SDL.h>
-#include <YAGE/gltexture.hpp>
+#include <YAGE/texture.hpp>
#include <YAGE/resourcemanager.hpp>
#include <YAGE/vertex.hpp>
@@ -14,10 +15,7 @@
Game::Game(int screen_width/*=1280*/, int screen_height/*=720*/) :
screen_width_(screen_width),
screen_height_(screen_height),
- window_(),
- camera_(screen_width_, screen_height_),
- program_(),
- sprite_batch_()
+ camera_(screen_width_, screen_height_)
{}
Game::~Game()
@@ -27,7 +25,7 @@ Game::~Game()
void Game::run()
{
- initSystems();
+ initSystems();
sprite_batch_.init();
gameLoop();
}
@@ -36,10 +34,22 @@ void Game::initSystems()
{
if(SDL_Init(SDL_INIT_VIDEO))
throw std::runtime_error("SDL_Init failed");
-
window_.create("Arider", screen_width_, screen_height_, yage::WindowFlags::SHOWN);
-
initShaders();
+
+ player_.create(glm::vec2(0.f, 0.f), glm::vec2(66, 92), glm::vec2(5.f, 5.f));
+ player_.animation_.pushFrame("idle", "res/textures/Player/p3_front.png");
+ for(int i=1; i<=11; ++i)
+ {
+ if(i<10)
+ {
+ player_.animation_.pushFrame("move", "res/textures/Player/p3_walk/PNG/p3_walk0"+std::to_string(i)+".png");
+ }
+ else
+ {
+ player_.animation_.pushFrame("move", "res/textures/Player/p3_walk/PNG/p3_walk"+std::to_string(i)+".png");
+ }
+ }
}
void Game::initShaders()
@@ -55,18 +65,12 @@ void Game::processInput()
{
SDL_Event event;
- static const float CAMERA_SPEED = 5.f;
- // static const float SCALE_SPEED = 0.1f;
-
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
- game_state_ = GameState::EXIT;
- break;
- case SDL_MOUSEMOTION:
- // when mouse moves
+ game_state_=GameState::EXIT;
break;
case SDL_KEYDOWN:
input_manager_.keyPressed(event.key.keysym.sym);
@@ -76,24 +80,41 @@ void Game::processInput()
}
}
+ player_.animation_.start("idle");
+ player_.idle();
+
if(input_manager_.isKeyPressed(SDLK_w))
- camera_.move(glm::vec2(0, CAMERA_SPEED));
- if(input_manager_.isKeyPressed(SDLK_d))
- camera_.move(glm::vec2(CAMERA_SPEED, 0));
+ {
+ player_.animation_.start("idle");
+ player_.moveUp();
+ }
if(input_manager_.isKeyPressed(SDLK_s))
- camera_.move(glm::vec2(0, -CAMERA_SPEED));
+ {
+ player_.animation_.start("idle");
+ player_.moveDown();
+ }
+ if(input_manager_.isKeyPressed(SDLK_d))
+ {
+ player_.animation_.start("move");
+ player_.moveRight();
+ }
if(input_manager_.isKeyPressed(SDLK_a))
- camera_.move(glm::vec2(-CAMERA_SPEED, 0));
+ {
+ player_.animation_.start("move");
+ player_.moveLeft();
+ }
+
+ if(time_%3==0)
+ player_.animation_.nextFrame();
}
void Game::gameLoop()
{
- while(game_state_ != GameState::EXIT)
+ while(game_state_!=GameState::EXIT)
{
processInput();
drawGame();
- time_ += 0.05;
- // std::cout<<calculateFps()<<'\n';
+ time_+=1;
}
}
@@ -102,32 +123,18 @@ void Game::drawGame()
window_.clearBuffer();
program_.use();
- camera_.update();
-
- glActiveTexture(GL_TEXTURE0);
- GLint texture_location = program_.getUniformLocation("texture_sampler");
+ camera_.update(program_);
+
+ // activate texture 0
+ glActiveTexture(GL_TEXTURE0);
+ // bind it to the sampler
+ GLint texture_location=program_.getUniformLocation("texture_sampler");
glUniform1i(texture_location, 0);
// GLint time_location = program_.getUniformLocation("time");
// glUniform1f(time_location, time_);
-
- GLint matrix_location = program_.getUniformLocation("P");
- glUniformMatrix4fv(matrix_location, 1, GL_FALSE, &(camera_.getCameraMatrix()[0][0]));
- // draw the sprite batches
- sprite_batch_.begin();
-
- glm::vec4 rect(200.f, 200.f, 66.f, 92.f);
- glm::vec4 uv(0.f, 0.f, 1.f, 1.f);
- yage::GlTexture texture=yage::ResourceManager::getTexture("res/textures/Player/p1_front.png");
- yage::Color color;
- color.r=255;
- color.g=255;
- color.b=255;
- color.a=255;
- sprite_batch_.draw(rect, uv, texture.id, color, 0.f);
- sprite_batch_.end();
- sprite_batch_.render();
+ renderSprites();
glBindTexture(GL_TEXTURE_2D, 0);
program_.unuse();
@@ -136,6 +143,21 @@ void Game::drawGame()
window_.swapBuffer();
}
+void Game::renderSprites()
+{
+ // draw the sprite batches
+ sprite_batch_.begin();
+
+ // drawing the player
+ player_.renderSprite(sprite_batch_);
+
+ // drawing the background
+ sprite_batch_.draw(glm::vec4(0.f, 0.f, 2560, 2560), glm::vec4(0.f, 0.f, 10.f, 10.f), yage::ResourceManager::getTexture("res/textures/bg_castle.png").id, yage::Color(255, 255, 255, 255), -1.f);
+
+ sprite_batch_.end();
+ sprite_batch_.render();
+}
+
float Game::calculateFps()
{
static const int NUM_SAMPLES = 100;
diff --git a/src/main.cpp b/src/main.cpp
index ab057c3..b6cde15 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,4 +1,4 @@
-#include "arider_config.hpp"
+#include "ariderconfig.hpp"
#include "game.hpp"
#include <GL/glew.h>
diff --git a/src/player.cpp b/src/player.cpp
new file mode 100644
index 0000000..f0bc35c
--- /dev/null
+++ b/src/player.cpp
@@ -0,0 +1,35 @@
+#include "player.hpp"
+
+#include <glm/glm.hpp>
+
+Player::Player()
+{}
+
+void Player::moveUp()
+{
+ uv_=glm::vec4(0.f, 0.f, 1.f, 1.f);
+ position_+=glm::vec2(0.f, speed_.y);
+}
+
+void Player::moveRight()
+{
+ uv_=glm::vec4(0.f, 0.f, 1.f, 1.f);
+ position_+=glm::vec2(speed_.x, 0.f);
+}
+
+void Player::moveDown()
+{
+ uv_=glm::vec4(0.f, 0.f, 1.f, 1.f);
+ position_+=glm::vec2(0.f, -speed_.y);
+}
+
+void Player::moveLeft()
+{
+ uv_=glm::vec4(1.f, 0.f, -1.f, 1.f);
+ position_+=glm::vec2(-speed_.x, 0.f);
+}
+
+void Player::idle()
+{
+ uv_=glm::vec4(1.f, 0.f, -1.f, 1.f);
+}