aboutsummaryrefslogtreecommitdiffstats
path: root/src
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 /src
parent8986a9cd869c3f0fe38f9610110bfd9e2d20dd74 (diff)
downloadArider-0e355cacba945cab5e50739b20e4709557768a4f.tar.gz
Arider-0e355cacba945cab5e50739b20e4709557768a4f.zip
Added animations but have to improve class
Diffstat (limited to 'src')
-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
5 files changed, 153 insertions, 45 deletions
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);
+}