diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-31 23:25:03 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-31 23:25:03 +0100 |
commit | eb8f03ac327b62bf1b8afa3fcd7dfd8d8ed86e05 (patch) | |
tree | 08c8c3580b84d2560e6c6dcbe10909c6d4b595a8 | |
parent | 299237fa42474ac05fe3e9da0b1bbee7c7ab9cde (diff) | |
download | Arider-eb8f03ac327b62bf1b8afa3fcd7dfd8d8ed86e05.tar.gz Arider-eb8f03ac327b62bf1b8afa3fcd7dfd8d8ed86e05.zip |
added comments and fixed naming convention
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | include/game.hpp | 8 | ||||
-rw-r--r-- | include/glsl_program.hpp | 4 | ||||
-rw-r--r-- | include/io_manager.hpp | 2 | ||||
-rw-r--r-- | include/pico_png.hpp | 2 | ||||
-rw-r--r-- | include/resource_manager.hpp | 18 | ||||
-rw-r--r-- | include/sprite.hpp | 7 | ||||
-rw-r--r-- | include/texture_cache.hpp | 20 | ||||
-rw-r--r-- | src/game.cpp | 20 | ||||
-rw-r--r-- | src/image_loader.cpp | 2 | ||||
-rw-r--r-- | src/io_manager.cpp | 2 | ||||
-rw-r--r-- | src/main.cpp (renamed from src/arider.cpp) | 0 | ||||
-rw-r--r-- | src/resource_manager.cpp | 8 | ||||
-rw-r--r-- | src/sprite.cpp | 5 | ||||
-rw-r--r-- | src/texture_cache.cpp | 22 |
16 files changed, 108 insertions, 19 deletions
@@ -14,3 +14,4 @@ Makefile # specific directories to ignore /debug_build/ +/G* diff --git a/CMakeLists.txt b/CMakeLists.txt index 84f6031..437a9ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,13 +25,15 @@ configure_file ( # adding my executable add_executable(${PROJECT_NAME} - src/arider.cpp + src/main.cpp src/game.cpp src/glsl_program.cpp src/image_loader.cpp src/io_manager.cpp src/pico_png.cpp - src/sprite.cpp) + src/sprite.cpp + src/resource_manager.cpp + src/texture_cache.cpp) # adding sdl2 library include(FindPkgConfig) diff --git a/include/game.hpp b/include/game.hpp index 3871b36..a5443a0 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -5,8 +5,11 @@ #include "gl_texture.hpp" #include "sprite.hpp" -#include <SDL2/SDL.h> #include <GL/glew.h> +#include <SDL2/SDL.h> + +#include <memory> +#include <vector> enum class GameState { @@ -25,8 +28,7 @@ private: // Temporary program GlslProgram program_; - GlTexture player_texture_; - Sprite sprite_; + std::vector<std::shared_ptr<Sprite>> sprites_; void initSystems(); void initShaders(); diff --git a/include/glsl_program.hpp b/include/glsl_program.hpp index 98a47e3..cef38a8 100644 --- a/include/glsl_program.hpp +++ b/include/glsl_program.hpp @@ -2,21 +2,25 @@ #define GLSL_PROGRAM_HPP #include <GL/glew.h> + #include <string> class GlslProgram { private: + // compiled shader program id GLuint program_id_ = 0; GLuint vertex_shader_id_ = 0; GLuint fragment_shader_id_ = 0; int attribute_index_ = 0; + // compiles one shader void compileShader(const GLuint &shader, const std::string &file_path); public: GlslProgram(); ~GlslProgram(); + // compiles vertex and fragment shader void compileShaders(const std::string &vertex_shader_path, const std::string &fragment_shader_path); void linkShaders(); void addAttribute(const std::string &attribute_name); diff --git a/include/io_manager.hpp b/include/io_manager.hpp index d23cd71..05d288b 100644 --- a/include/io_manager.hpp +++ b/include/io_manager.hpp @@ -4,7 +4,7 @@ #include <string> #include <vector> -class IOManager +class IoManager { public: static bool readFileToBuffer(const std::string &file_path, std::vector<unsigned char> &buffer); diff --git a/include/pico_png.hpp b/include/pico_png.hpp index e2d7d1a..ef12357 100644 --- a/include/pico_png.hpp +++ b/include/pico_png.hpp @@ -1,4 +1,4 @@ #include <vector> #include <cstdlib> -extern int decodePNG(std::vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32 = true); +extern int decodePNG(std::vector<unsigned char> &out_image, unsigned long &image_width, unsigned long &image_height, const unsigned char *in_png, size_t in_size, bool convert_to_rgba32 = true); diff --git a/include/resource_manager.hpp b/include/resource_manager.hpp new file mode 100644 index 0000000..155515a --- /dev/null +++ b/include/resource_manager.hpp @@ -0,0 +1,18 @@ +#ifndef RESOURCE_MANAGER_HPP +#define RESOURCE_MANAGER_HPP + +#include "gl_texture.hpp" +#include "texture_cache.hpp" + +#include <string> + +class ResourceManager +{ +private: + static TextureCache texture_cache_; +public: + static GlTexture getTexture(const std::string &texture_path); +}; + + +#endif diff --git a/include/sprite.hpp b/include/sprite.hpp index 65ea45b..9f765c7 100644 --- a/include/sprite.hpp +++ b/include/sprite.hpp @@ -1,8 +1,12 @@ #ifndef SPRITE_HPP #define SPRITE_HPP +#include "gl_texture.hpp" + #include <GL/glew.h> +#include <string> + class Sprite { private: @@ -11,11 +15,12 @@ private: float width_; float height_; GLuint vbo_id_ = 0; + GlTexture texture_; public: Sprite(); ~Sprite(); - void init(float x, float y, float width, float height); + void init(float x, float y, float width, float height, const std::string &texture_path); void draw(); }; diff --git a/include/texture_cache.hpp b/include/texture_cache.hpp new file mode 100644 index 0000000..44dba2f --- /dev/null +++ b/include/texture_cache.hpp @@ -0,0 +1,20 @@ +#ifndef TEXTURE_CACHE_HPP +#define TEXTURE_CACHE_HPP + +#include "gl_texture.hpp" + +#include <unordered_map> + +class TextureCache +{ +private: + std::unordered_map<std::string, GlTexture> texture_map_; +public: + TextureCache(); + ~TextureCache(); + + GlTexture getTexture(const std::string &texture_path); +}; + + +#endif diff --git a/src/game.cpp b/src/game.cpp index b1c7af8..4c2e122 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,5 +1,4 @@ #include "game.hpp" -#include "image_loader.hpp" #include "sprite.hpp" #include <stdexcept> @@ -18,6 +17,8 @@ void Game::initSystems() if(SDL_Init(SDL_INIT_VIDEO)) throw std::runtime_error("SDL_Init failed"); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + window_ = SDL_CreateWindow("Arider", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width_, height_, SDL_WINDOW_OPENGL); if(window_ == nullptr) @@ -31,7 +32,6 @@ void Game::initSystems() if(error != GLEW_OK) throw std::runtime_error("glewInit failed"); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); glClearColor(0.f, 0.f, 0.f, 1.f); initShaders(); @@ -78,7 +78,6 @@ void Game::drawGame() program_.use(); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, player_texture_.id); GLint texture_location = program_.getUniformLocation("sampler"); glUniform1i(texture_location, 0); @@ -86,7 +85,8 @@ void Game::drawGame() GLint time_location = program_.getUniformLocation("time"); glUniform1f(time_location, time_); - sprite_.draw(); + for(auto sprite : sprites_) + sprite->draw(); glBindTexture(GL_TEXTURE_2D, 0); program_.unuse(); @@ -98,9 +98,13 @@ void Game::drawGame() void Game::run() { initSystems(); - - sprite_.init(-1.f, -1.f, 2.f, 2.f); - player_texture_ = ImageLoader::loadPng("res/platformer_png/Player/p1_front.png"); - + 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(); } diff --git a/src/image_loader.cpp b/src/image_loader.cpp index 295e1a7..bb5485d 100644 --- a/src/image_loader.cpp +++ b/src/image_loader.cpp @@ -12,7 +12,7 @@ GlTexture ImageLoader::loadPng(const std::string &file_path) std::vector<unsigned char> out; unsigned long width, height; - if(!IOManager::readFileToBuffer(file_path, in)) + if(!IoManager::readFileToBuffer(file_path, in)) throw std::runtime_error("Failed to load '"+file_path+"' to buffer"); int error_code = decodePNG(out, width, height, &in[0], in.size()); diff --git a/src/io_manager.cpp b/src/io_manager.cpp index 799341a..106dd24 100644 --- a/src/io_manager.cpp +++ b/src/io_manager.cpp @@ -3,7 +3,7 @@ #include <fstream> #include <stdexcept> -bool IOManager::readFileToBuffer(const std::string &file_path, std::vector<unsigned char> &buffer) +bool IoManager::readFileToBuffer(const std::string &file_path, std::vector<unsigned char> &buffer) { std::ifstream file(file_path, std::ios::binary); if(!file.is_open()) diff --git a/src/arider.cpp b/src/main.cpp index f8e06e8..f8e06e8 100644 --- a/src/arider.cpp +++ b/src/main.cpp diff --git a/src/resource_manager.cpp b/src/resource_manager.cpp new file mode 100644 index 0000000..7bd8dd4 --- /dev/null +++ b/src/resource_manager.cpp @@ -0,0 +1,8 @@ +#include "resource_manager.hpp" + +TextureCache ResourceManager::texture_cache_; + +GlTexture ResourceManager::getTexture(const std::string &texture_path) +{ + return texture_cache_.getTexture(texture_path); +} diff --git a/src/sprite.cpp b/src/sprite.cpp index f037ec3..5821eb8 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -1,4 +1,5 @@ #include "sprite.hpp" +#include "resource_manager.hpp" #include "vertex.hpp" #include <cstddef> @@ -12,12 +13,13 @@ Sprite::~Sprite() glDeleteBuffers(1, &vbo_id_); } -void Sprite::init(float x, float y, float width, float height) +void Sprite::init(float x, float y, float width, float height, const std::string &texture_path) { x_ = x; y_ = y; width_ = width; height_ = height; + texture_ = ResourceManager::getTexture(texture_path); if(vbo_id_ == 0) glGenBuffers(1, &vbo_id_); @@ -55,6 +57,7 @@ void Sprite::init(float x, float y, float width, float height) void Sprite::draw() { + glBindTexture(GL_TEXTURE_2D, texture_.id); glBindBuffer(GL_ARRAY_BUFFER, vbo_id_); glEnableVertexAttribArray(0); diff --git a/src/texture_cache.cpp b/src/texture_cache.cpp new file mode 100644 index 0000000..c57538b --- /dev/null +++ b/src/texture_cache.cpp @@ -0,0 +1,22 @@ +#include "image_loader.hpp" +#include "texture_cache.hpp" + +TextureCache::TextureCache() +{} + +TextureCache::~TextureCache() +{} + +GlTexture TextureCache::getTexture(const std::string &texture_path) +{ + auto itr = texture_map_.find(texture_path); + + if(itr == texture_map_.end()) + { + GlTexture new_texture = ImageLoader::loadPng(texture_path); + texture_map_.insert(make_pair(texture_path, new_texture)); + return new_texture; + } + + return itr->second; +} |