diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-05-19 20:08:45 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-05-19 20:08:45 +0100 |
commit | 5cf7ed8339d773fc149fea8a5cb6c57f40e881ca (patch) | |
tree | b4afd9869906801c7244b983f3fa12155a2a55c7 | |
parent | 46f18e72aeced4275ee92fe189db8aa3eeca0818 (diff) | |
download | Arider-5cf7ed8339d773fc149fea8a5cb6c57f40e881ca.tar.gz Arider-5cf7ed8339d773fc149fea8a5cb6c57f40e881ca.zip |
Improving yage and added gravity
-rw-r--r-- | CMakeLists.txt | 32 | ||||
m--------- | YAGE | 0 | ||||
-rw-r--r-- | include/animation.hpp | 57 | ||||
-rw-r--r-- | include/character.hpp | 36 | ||||
-rw-r--r-- | include/game.hpp | 3 | ||||
-rw-r--r-- | include/particle.hpp | 25 | ||||
-rw-r--r-- | src/animation.cpp | 22 | ||||
-rw-r--r-- | src/character.cpp | 1 | ||||
-rw-r--r-- | src/game.cpp | 215 | ||||
-rw-r--r-- | src/particle.cpp | 20 |
10 files changed, 224 insertions, 187 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 50d7ed5..65248b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,15 +38,9 @@ add_executable(${PROJECT_NAME} ${ARIDER_SOURCE_DIR}/game.cpp ${ARIDER_SOURCE_DIR}/level.cpp ${ARIDER_SOURCE_DIR}/main.cpp + ${ARIDER_SOURCE_DIR}/particle.cpp ${ARIDER_SOURCE_DIR}/player.cpp) -add_executable(test001 - ${ARIDER_TEST_DIR}/test001.cpp) -add_executable(test002 - ${ARIDER_TEST_DIR}/test002.cpp) -add_executable(test003 - ${ARIDER_TEST_DIR}/test003.cpp) - # adding sdl2 library include(FindPkgConfig) find_package(OpenGL REQUIRED) @@ -68,27 +62,3 @@ target_link_libraries(${CMAKE_PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${SDL2_LIBRARIES}) - -target_link_libraries(test001 - ${YAGE_LIBRARIES} - ${OPENGL_LIBRARIES} - ${GLEW_LIBRARIES} - ) - -target_link_libraries(test002 - ${YAGE_LIBRARIES} - ${OPENGL_LIBRARIES} - ${GLEW_LIBRARIES} - ) - -target_link_libraries(test003 - ${YAGE_LIBRARIES} - ${OPENGL_LIBRARIES} - ${GLEW_LIBRARIES} - ) - -enable_testing() - -add_test(test1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test001) -add_test(test2 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test002) -add_test(test3 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test003) diff --git a/YAGE b/YAGE -Subproject e15b2c6dd405d008d9e892608c209fd980bcd8f +Subproject 3308d13ad04fac43e7a111b299ff9444aea4ab9 diff --git a/include/animation.hpp b/include/animation.hpp index 54eb71a..b4dfcc3 100644 --- a/include/animation.hpp +++ b/include/animation.hpp @@ -12,40 +12,41 @@ enum class AnimationState { - STILL, - IDLE, - MOVING, - JUMPING, - CROUCHING, - FALLING, + STILL, + IDLE, + MOVING, + JUMPING, + CROUCHING, + FALLING, }; class Animation { private: - std::unordered_map<AnimationState, std::vector<yage::Texture>> frame_animations_; - AnimationState current_animation_; - int current_index_=0; + std::unordered_map<AnimationState, std::vector<yage::Texture>> frame_animations_; + AnimationState current_animation_; + int current_index_=0; + public: - Animation(); - - void pushFrame(AnimationState state, const std::string &texture_path); - yage::Texture currentFrame() const; - void start(AnimationState state); - void nextFrame(); - - template<typename First, typename Second, typename ...Rest> - void initializeAnimation(AnimationState state, First &&first, Second &&second, Rest &&...rest) - { - frame_animations_[state].push_back(yage::ResourceManager::getTexture(std::forward<First>(first))); - initializeAnimation(std::forward<Second>(second), std::forward<Rest>(rest)...); - } - - template<typename Last> - void initializeAnimation(AnimationState state, Last &&last) - { - frame_animations_[state].push_back(yage::ResourceManager::getTexture(std::forward<Last>(last))); - } + Animation(); + + void pushFrame(AnimationState state, const std::string &texture_path); + yage::Texture currentFrame() const; + void start(AnimationState state); + void nextFrame(); + + template<typename First, typename Second, typename ...Rest> + void initializeAnimation(AnimationState state, First &&first, Second &&second, Rest &&...rest) + { + frame_animations_[state].push_back(yage::ResourceManager::getTexture(std::forward<First>(first))); + initializeAnimation(std::forward<Second>(second), std::forward<Rest>(rest)...); + } + + template<typename Last> + void initializeAnimation(AnimationState state, Last &&last) + { + frame_animations_[state].push_back(yage::ResourceManager::getTexture(std::forward<Last>(last))); + } }; #endif diff --git a/include/character.hpp b/include/character.hpp index 4f45017..8247d7f 100644 --- a/include/character.hpp +++ b/include/character.hpp @@ -14,27 +14,27 @@ class Character { public: - Animation animation_; + Animation animation_; protected: - glm::vec4 uv_; - glm::vec2 position_; - glm::vec2 size_; - glm::vec2 speed_; + 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; + 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 8f9f1fa..1146f5b 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -3,6 +3,7 @@ #include "level.hpp" #include "player.hpp" +#include "particle.hpp" #include <YAGE/camera2d.hpp> #include <YAGE/glslprogram.hpp> @@ -46,6 +47,8 @@ private: //level Level level_; + Particle particle_; + // member functions public: Game(int screen_width=1280, int screen_height=720); diff --git a/include/particle.hpp b/include/particle.hpp new file mode 100644 index 0000000..09651d2 --- /dev/null +++ b/include/particle.hpp @@ -0,0 +1,25 @@ +#ifndef ARIDER_PARTICLE_HPP +#define ARIDER_PARTICLE_HPP + +#include <glm/glm.hpp> + +#include <YAGE/Physics/particlebody.hpp> +#include <YAGE/texturecache.hpp> +#include <YAGE/spritebatch.hpp> + + +class Particle +{ +private: + yage::ParticleBody particle_body_; + yage::Texture texture_; + +public: + Particle(); + ~Particle(); + + void setTexture(const std::string &texture_path); + void renderSprite(yage::SpriteBatch &sprite_batch); +}; + +#endif 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(); +} |