aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-05-19 20:08:45 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-05-19 20:08:45 +0100
commit5cf7ed8339d773fc149fea8a5cb6c57f40e881ca (patch)
treeb4afd9869906801c7244b983f3fa12155a2a55c7 /src
parent46f18e72aeced4275ee92fe189db8aa3eeca0818 (diff)
downloadArider-5cf7ed8339d773fc149fea8a5cb6c57f40e881ca.tar.gz
Arider-5cf7ed8339d773fc149fea8a5cb6c57f40e881ca.zip
Improving yage and added gravity
Diffstat (limited to 'src')
-rw-r--r--src/animation.cpp22
-rw-r--r--src/character.cpp1
-rw-r--r--src/game.cpp215
-rw-r--r--src/particle.cpp20
4 files changed, 148 insertions, 110 deletions
diff --git a/src/animation.cpp b/src/animation.cpp
index 928c500..9b68451 100644
--- a/src/animation.cpp
+++ b/src/animation.cpp
@@ -7,27 +7,27 @@ Animation::Animation() : current_animation_(AnimationState::IDLE)
void Animation::pushFrame(AnimationState state, const std::string &texture_path)
{
- frame_animations_[state].push_back(yage::ResourceManager::getTexture(texture_path));
+ frame_animations_[state].push_back(yage::ResourceManager::getTexture(texture_path));
}
yage::Texture Animation::currentFrame() const
{
- int current_index=current_index_;
- if(current_index_>(int)frame_animations_.find(current_animation_)->second.size()-1)
- current_index=0;
- return frame_animations_.find(current_animation_)->second[current_index];
+ int current_index=current_index_;
+ if(current_index_>(int)frame_animations_.find(current_animation_)->second.size()-1)
+ current_index=0;
+ return frame_animations_.find(current_animation_)->second[current_index];
}
void Animation::start(AnimationState state)
{
- if(current_animation_!=state)
- current_animation_=state;
+ if(current_animation_!=state)
+ current_animation_=state;
}
void Animation::nextFrame()
{
- if(current_index_+1<(int)frame_animations_.find(current_animation_)->second.size())
- ++current_index_;
- else
- current_index_=0;
+ if(current_index_+1<(int)frame_animations_.find(current_animation_)->second.size())
+ ++current_index_;
+ else
+ current_index_=0;
}
diff --git a/src/character.cpp b/src/character.cpp
index ef76289..2fb1d7b 100644
--- a/src/character.cpp
+++ b/src/character.cpp
@@ -1,6 +1,5 @@
#include "character.hpp"
-#include <YAGE/resourcemanager.hpp>
#include <YAGE/vertex.hpp>
void Character::create(const glm::vec2 &position/*=glm::vec2(0.f, 0.f)*/,
diff --git a/src/game.cpp b/src/game.cpp
index 81f0fa6..ae78d17 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -14,161 +14,180 @@
#include <iostream>
Game::Game(int screen_width/*=1280*/, int screen_height/*=720*/) :
- screen_width_(screen_width),
- screen_height_(screen_height),
- camera_(screen_width_, screen_height_)
+ screen_width_(screen_width),
+ screen_height_(screen_height),
+ camera_(screen_width_, screen_height_)
{}
Game::~Game()
{
- SDL_Quit();
+ SDL_Quit();
}
void Game::run()
{
- initSystems();
- sprite_batch_.init();
- gameLoop();
+ initSystems();
+ sprite_batch_.init();
+ gameLoop();
}
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, 70.f), glm::vec2(66, 92), glm::vec2(5.f, 5.f));
- player_.animation_.pushFrame(AnimationState::IDLE, "res/textures/Player/p2_front.png");
- for(int i=1; i<=11; ++i)
- if(i<10)
- player_.animation_.pushFrame(AnimationState::MOVING, "res/textures/Player/p2_walk/PNG/p2_walk0"+std::to_string(i)+".png");
- else
- player_.animation_.pushFrame(AnimationState::MOVING, "res/textures/Player/p2_walk/PNG/p2_walk"+std::to_string(i)+".png");
- level_.loadLevel("res/levels/level1.txt");
+ 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, 70.f), glm::vec2(66, 92), glm::vec2(5.f, 5.f));
+ player_.animation_.pushFrame(AnimationState::IDLE, "res/textures/Player/p2_front.png");
+
+ for(int i=1; i<=11; ++i)
+ {
+ if(i<10)
+ player_.animation_.pushFrame(AnimationState::MOVING,std::string("res/textures/Player/")+
+ "p2_walk/PNG/p2_walk0"+std::to_string(i)+".png");
+ else
+ player_.animation_.pushFrame(AnimationState::MOVING, std::string("res/textures/")+
+ "Player/p2_walk/PNG/p2_walk"+std::to_string(i)+".png");
+ }
+
+ particle_.setTexture("res/textures/Player/p1_front.png");
+
+ level_.loadLevel("res/levels/level1.txt");
}
void Game::initShaders()
{
- program_.compileShaders("res/shaders/color_shading.vert", "res/shaders/color_shading.frag");
- program_.addAttribute("vertex_position");
- program_.addAttribute("vertex_color");
- program_.addAttribute("vertex_uv");
- program_.linkShaders();
+ program_.compileShaders("res/shaders/color_shading.vert", "res/shaders/color_shading.frag");
+ program_.addAttribute("vertex_position");
+ program_.addAttribute("vertex_color");
+ program_.addAttribute("vertex_uv");
+ program_.linkShaders();
}
void Game::processInput()
{
- SDL_Event event;
+ SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
+ while(SDL_PollEvent(&event))
{
- case SDL_QUIT:
- game_state_=GameState::EXIT;
- break;
- case SDL_KEYDOWN:
- input_manager_.keyPressed(event.key.keysym.sym);
- break;
- case SDL_KEYUP:
- input_manager_.keyReleased(event.key.keysym.sym);
+ switch(event.type)
+ {
+ case SDL_QUIT:
+ game_state_=GameState::EXIT;
+ break;
+ case SDL_KEYDOWN:
+ input_manager_.keyPressed(event.key.keysym.sym);
+ break;
+ case SDL_KEYUP:
+ input_manager_.keyReleased(event.key.keysym.sym);
+ }
}
- }
- player_.idle();
+ player_.idle();
- if(input_manager_.isKeyPressed(SDLK_w))
- player_.moveUp();
- if(input_manager_.isKeyPressed(SDLK_s))
- player_.moveDown();
- if(input_manager_.isKeyPressed(SDLK_d))
- player_.moveRight();
- if(input_manager_.isKeyPressed(SDLK_a))
- player_.moveLeft();
+ if(input_manager_.isKeyPressed(SDLK_w))
+ player_.moveUp();
+ if(input_manager_.isKeyPressed(SDLK_s))
+ player_.moveDown();
+ if(input_manager_.isKeyPressed(SDLK_d))
+ player_.moveRight();
+ if(input_manager_.isKeyPressed(SDLK_a))
+ player_.moveLeft();
- if(time_%3==0)
- player_.animation_.nextFrame();
+ if(time_%3==0)
+ player_.animation_.nextFrame();
}
void Game::gameLoop()
{
- while(game_state_!=GameState::EXIT)
- {
- processInput();
- drawGame();
- time_+=1;
- }
+ while(game_state_!=GameState::EXIT)
+ {
+ processInput();
+ drawGame();
+ time_+=1;
+ }
}
void Game::drawGame()
{
- window_.clearBuffer();
+ window_.clearBuffer();
- program_.use();
- camera_.update(program_);
+ program_.use();
+ 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);
+ // 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 time_location = program_.getUniformLocation("time");
+ // glUniform1f(time_location, time_);
- renderSprites();
+ renderSprites();
- glBindTexture(GL_TEXTURE_2D, 0);
- program_.unuse();
+ glBindTexture(GL_TEXTURE_2D, 0);
+ program_.unuse();
- // swap buffer and draw everything to the screen
- window_.swapBuffer();
+ // swap buffer and draw everything to the screen
+ window_.swapBuffer();
}
void Game::renderSprites()
{
- // draw the sprite batches
- sprite_batch_.begin();
+ static int x=0;
+ static unsigned start=SDL_GetTicks();
+ if((SDL_GetTicks()-start)/1000==1)
+ {
+ std::cout<<"FPS: "<<x<<'\n';
+ x=0;
+ start=SDL_GetTicks();
+ }
+ x++;
+
+ // draw the sprite batches
+ sprite_batch_.begin();
- level_.drawLevel(sprite_batch_);
+ level_.drawLevel(sprite_batch_);
- // drawing the player
- player_.renderSprite(sprite_batch_);
+ // drawing the player
+ player_.renderSprite(sprite_batch_);
+ particle_.renderSprite(sprite_batch_);
- // drawing the background
- sprite_batch_.draw(glm::vec4(0.f, 0.f, 2560.f, 2560.f), 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), -2.f);
+ // drawing the background
+ sprite_batch_.draw(glm::vec4(0.f, 0.f, 2560.f, 2560.f), 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), -2.f);
- sprite_batch_.end();
- sprite_batch_.render();
+ sprite_batch_.end();
+ sprite_batch_.render();
}
float Game::calculateFps()
{
- static const int NUM_SAMPLES = 100;
- static float frame_times[NUM_SAMPLES];
- static float prev_ticks = SDL_GetTicks();
- static unsigned current_frame = 0;
+ static const int NUM_SAMPLES = 100;
+ static float frame_times[NUM_SAMPLES];
+ static float prev_ticks = SDL_GetTicks();
+ static unsigned current_frame = 0;
- float current_ticks = SDL_GetTicks();
+ float current_ticks = SDL_GetTicks();
- frame_times[current_frame%NUM_SAMPLES] = current_ticks-prev_ticks;
- prev_ticks = current_ticks;
+ frame_times[current_frame%NUM_SAMPLES] = current_ticks-prev_ticks;
+ prev_ticks = current_ticks;
- int count = NUM_SAMPLES;
- if(current_frame<NUM_SAMPLES)
- count = current_frame;
+ int count = NUM_SAMPLES;
+ if(current_frame<NUM_SAMPLES)
+ count = current_frame;
- float frame_time_average = 0;
- for(int i = 0; i < count; ++i)
- frame_time_average += frame_times[i];
- frame_time_average /= count;
+ float frame_time_average = 0;
+ for(int i = 0; i < count; ++i)
+ frame_time_average += frame_times[i];
+ frame_time_average /= count;
- float fps = 60.f;
- if(frame_time_average>0)
- fps = 1000.f/frame_time_average;
+ float fps = 60.f;
+ if(frame_time_average>0)
+ fps = 1000.f/frame_time_average;
- ++current_frame;
+ ++current_frame;
- return fps;
+ return fps;
}
diff --git a/src/particle.cpp b/src/particle.cpp
new file mode 100644
index 0000000..f968f73
--- /dev/null
+++ b/src/particle.cpp
@@ -0,0 +1,20 @@
+#include "particle.hpp"
+
+#include <YAGE/resourcemanager.hpp>
+
+Particle::Particle() : particle_body_(glm::vec2(200.f/70.f, 600.f/70.f))
+{}
+
+Particle::~Particle()
+{}
+
+void Particle::setTexture(const std::string &texture_path)
+{
+ texture_=yage::ResourceManager::getTexture(texture_path);
+}
+
+void Particle::renderSprite(yage::SpriteBatch &sprite_batch)
+{
+ sprite_batch.draw(glm::vec4(70*particle_body_.xPosition(), 70*particle_body_.yPosition(), 50, 50), glm::vec4(0.f, 0.f, 1.f, 1.f), texture_.id, yage::Color(255, 255, 255, 255), 0.f);
+ particle_body_.update();
+}