diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-04-04 12:19:13 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-04-04 12:19:13 +0100 |
commit | 2dd12e02ba00619921214ea196968c0860f355d1 (patch) | |
tree | 88130232b9c9e5968519b8b2ba57e0f02a92363f | |
parent | 6e2e4fbbe1db21c87ccd28f75f5cf77818bc19ea (diff) | |
download | Arider-2dd12e02ba00619921214ea196968c0860f355d1.tar.gz Arider-2dd12e02ba00619921214ea196968c0860f355d1.zip |
Fixed window class and redundancy
-rw-r--r-- | .dir-locals.el | 12 | ||||
-rw-r--r-- | CMakeLists.txt | 7 | ||||
-rw-r--r-- | include/game.hpp | 45 | ||||
-rw-r--r-- | res/shaders/color_shading.frag | 11 | ||||
-rw-r--r-- | res/shaders/color_shading.vert | 4 | ||||
-rw-r--r-- | src/game.cpp | 57 |
6 files changed, 86 insertions, 50 deletions
diff --git a/.dir-locals.el b/.dir-locals.el index b4e402d..223f1d6 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -1,9 +1,15 @@ ((nil . ((company-clang-arguments . ("-I../include/YAGE/" "-I../include/" - "-I../YAGE/include/")) + "-I../YAGE/include/" + "-I/usr/include/" + "-I/usr/include/SDL2/")) (company-c-headers-path-user . ("../include/YAGE/" "../include/" - "../YAGE/include/")) + "../YAGE/include/" + "/usr/include/" + "/usr/include/SDL2/")) (flycheck-clang-include-path . ("../include/YAGE/" "../include/" - "../YAGE/include/"))))) + "../YAGE/include/" + "/usr/include/" + "/usr/include/SDL2/"))))) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c6171c..eb97744 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,8 @@ set(ARIDER_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) -set(YAGE_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/yage/include/) -set(YAGE_LIBRARIES yage) +set(YAGE_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/YAGE/include/) +set(YAGE_LIBRARIES YAGE) # setting up configuration header configure_file ( @@ -39,12 +39,11 @@ find_package(GLEW REQUIRED) pkg_search_module(SDL2 REQUIRED sdl2) # add zedengine library -add_subdirectory(yage) +add_subdirectory(YAGE) # adding include directories include_directories(${ARIDER_INCLUDE_DIRS} ${YAGE_INCLUDE_DIRS} - ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${SLD2_INCLUDE_DIRS}) diff --git a/include/game.hpp b/include/game.hpp index 8011a40..ec3ac4a 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -3,9 +3,13 @@ #include <GL/glew.h> #include <SDL2/SDL.h> -#include <yage/glsl_program.hpp> -#include <yage/gl_texture.hpp> -#include <yage/sprite.hpp> +#include <glm/glm.hpp> + +#include <YAGE/glslprogram.hpp> +#include <YAGE/camera2d.hpp> +#include <YAGE/gltexture.hpp> +#include <YAGE/sprite.hpp> +#include <YAGE/window.hpp> #include <memory> #include <vector> @@ -18,28 +22,35 @@ enum class GameState class Game { +public: // member variables private: - SDL_Window *window_ = nullptr; - int width_ = 1280; - int height_ = 720; - GameState game_state_ = GameState::PLAY; - float time_ = 0; - - // Temporary program - GlslProgram program_; - std::vector<std::shared_ptr<Sprite>> sprites_; + // screen width and height initialized in constructor + int screen_width_; + int screen_height_; + // initializer game state + GameState game_state_=GameState::PLAY; + // set timer to 0 + float time_=0; + // window + yage::Window window_; + // camera + yage::Camera2D camera_; + // temporary program + yage::GlslProgram program_; + std::vector<std::shared_ptr<yage::Sprite>> sprites_; + +public: // member functions + Game(int screen_width=1280, int screen_height=720); + ~Game(); + void run(); +private: void initSystems(); void initShaders(); void processInput(); void gameLoop(); void drawGame(); float calculateFps(); -public: - Game(); - ~Game(); - - void run(); }; #endif diff --git a/res/shaders/color_shading.frag b/res/shaders/color_shading.frag index 5b70355..dc70c9c 100644 --- a/res/shaders/color_shading.frag +++ b/res/shaders/color_shading.frag @@ -6,7 +6,7 @@ in vec2 fragment_uv; out vec4 color; -uniform float time; +// uniform float time; uniform sampler2D texture_sampler; void main() @@ -14,8 +14,9 @@ void main() vec4 texture_color = texture(texture_sampler, fragment_uv); // color = texture_color * fragment_color; - color = texture_color * vec4(fragment_color.g*(cos(fragment_position.x*4.0+time)+1.0)/2.0, - fragment_color.r*(cos(fragment_position.y*8.0+time)+1.0)/2.0, - fragment_color.r*(sin(fragment_position.x*2.0+time)+1.0)/2.0, - 0.0); + // color = texture_color * vec4(fragment_color.g*(cos(fragment_position.x*4.0+time)+1.0)/2.0, + // fragment_color.r*(cos(fragment_position.y*8.0+time)+1.0)/2.0, + // fragment_color.r*(sin(fragment_position.x*2.0+time)+1.0)/2.0, + // 0.0); + color=texture_color; } diff --git a/res/shaders/color_shading.vert b/res/shaders/color_shading.vert index 98d0142..db336e5 100644 --- a/res/shaders/color_shading.vert +++ b/res/shaders/color_shading.vert @@ -8,9 +8,11 @@ out vec2 fragment_position; out vec4 fragment_color; out vec2 fragment_uv; +uniform mat4 P; + void main() { - gl_Position.xy = vertex_position; + gl_Position.xy = (P*vec4(vertex_position, 0.0, 1.0)).xy; gl_Position.z = 0.0; gl_Position.w = 1.0; diff --git a/src/game.cpp b/src/game.cpp index 288e616..ee4c05a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,24 +1,41 @@ #include "game.hpp" -#include <yage/sprite.hpp> +#include <YAGE/sprite.hpp> #include <stdexcept> #include <iostream> -Game::Game() +Game::Game(int screen_width/*=1280*/, int screen_height/*=720*/) : + screen_width_(screen_width), + screen_height_(screen_height), + camera_(screen_width_, screen_height_) {} Game::~Game() { - SDL_DestroyWindow(window_); SDL_Quit(); } +void Game::run() +{ + initSystems(); + sprites_.push_back(std::make_shared<yage::Sprite>()); + sprites_.back()->init(0.f, 0.f, 66.f, 92.f, "res/platformer_png/Player/p1_stand.png"); + sprites_.push_back(std::make_shared<yage::Sprite>()); + sprites_.back()->init(100.f, 0.f, 66.f, 92.f, "res/platformer_png/Player/p2_jump.png"); + sprites_.push_back(std::make_shared<yage::Sprite>()); + sprites_.back()->init(200.f, 0.f, 66.f, 92.f, "res/platformer_png/Player/p3_jump.png"); + + 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(); } @@ -35,12 +52,22 @@ void Game::processInput() { SDL_Event event; + static const float CAMERA_SPEED = 20.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 + break; + case SDL_KEYDOWN: + + break; } } } @@ -63,13 +90,17 @@ void Game::drawGame() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); program_.use(); + camera_.update(); glActiveTexture(GL_TEXTURE0); 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_); + + GLint matrix_location = program_.getUniformLocation("P"); + glUniformMatrix4fv(matrix_location, 1, GL_FALSE, &(camera_.getCameraMatrix()[0][0])); for(auto sprite : sprites_) sprite->draw(); @@ -78,21 +109,7 @@ void Game::drawGame() program_.unuse(); // swap buffer and draw everything to the screen - SDL_GL_SwapWindow(window_); -} - -void Game::run() -{ - initSystems(); - sprites_.push_back(std::make_shared<Sprite>()); - sprites_.back()->init(-1.f, -1.f, 1.f, 1.f, "res/platformer_png/Player/p1_stand.png"); - sprites_.push_back(std::make_shared<Sprite>()); - sprites_.back()->init(0.f, -1.f, 1.f, 1.f, "res/platformer_png/Player/p1_jump.png"); - sprites_.push_back(std::make_shared<Sprite>()); - sprites_.back()->init(0.f, 0.f, 1.f, 1.f, "res/platformer_png/Player/p1_hurt.png"); - sprites_.push_back(std::make_shared<Sprite>()); - sprites_.back()->init(-1.f, 0.f, 1.f, 1.f, "res/platformer_png/Player/p1_duck.png"); - gameLoop(); + window_.swapBuffer(); } float Game::calculateFps() |