diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index c2e90d7..8b21673 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -8,111 +8,111 @@ namespace Dengine { - AssetManager::AssetManager(){} - AssetManager::~AssetManager(){} +AssetManager::AssetManager() {} +AssetManager::~AssetManager() {} - Texture *AssetManager::getTexture(std::string name) - { - // NOTE(doyle): Since we're using a map, the count of an object can - // only be 1 or 0 - if (mTextures.count(name) == 1) - return &mTextures[name]; - - return nullptr; - } - - i32 AssetManager::loadTextureImage(std::string path, std::string name) - { - /* Open the texture image */ - i32 imgWidth, imgHeight, bytesPerPixel; - stbi_set_flip_vertically_on_load(TRUE); - u8 *image = - stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0); - - if (!image) - { - std::cerr << "stdbi_load() failed: " << stbi_failure_reason() - << std::endl; - return -1; - } - - Texture tex; - tex.generate(imgWidth, imgHeight, image); - stbi_image_free(image); - - mTextures[name] = tex; - return 0; - } - - Shader *AssetManager::getShader(std::string name) - { - if (mShaders.count(name) == 1) - return &mShaders[name]; - - return nullptr; - } - - INTERNAL std::string stringFromFile(const std::string &filename) - { - std::ifstream file; - file.open(filename.c_str(), std::ios::in | std::ios::binary); - - std::string output; - std::string line; - - if (!file.is_open()) - { - std::runtime_error(std::string("Failed to open file: ") + filename); - } - else - { - while (file.good()) - { - std::getline(file, line); - output.append(line + "\n"); - } - } - file.close(); - return output; - } - - INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype) - { - std::string shaderSource = stringFromFile(path); - const GLchar *source = shaderSource.c_str(); - - GLuint result = glCreateShader(shadertype); - glShaderSource(result, 1, &source, NULL); - glCompileShader(result); - - GLint success; - GLchar infoLog[512]; - glGetShaderiv(result, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(result, 512, NULL, infoLog); - std::cout << "glCompileShader failed: " << infoLog << std::endl; - } - - return result; - } - - i32 AssetManager::loadShaderFiles(std::string vertexPath, - std::string fragmentPath, std::string name) - { - - GLuint vertexShader = - createShaderFromPath(vertexPath, GL_VERTEX_SHADER); - GLuint fragmentShader = - createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER); - - Shader shader; - i32 result = shader.loadProgram(vertexShader, fragmentShader); - if (result) - return result; - - mShaders[name] = shader; - return 0; - } +const Texture *AssetManager::getTexture(const std::string name) +{ + // NOTE(doyle): Since we're using a map, the count of an object can + // only be 1 or 0 + if (mTextures.count(name) == 1) + return &mTextures[name]; + return nullptr; +} + +const i32 AssetManager::loadTextureImage(const std::string path, + const std::string name) +{ + /* Open the texture image */ + i32 imgWidth, imgHeight, bytesPerPixel; + stbi_set_flip_vertically_on_load(TRUE); + u8 *image = + stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0); + + if (!image) + { + std::cerr << "stdbi_load() failed: " << stbi_failure_reason() + << std::endl; + return -1; + } + + Texture tex; + tex.generate(imgWidth, imgHeight, image); + stbi_image_free(image); + + mTextures[name] = tex; + return 0; +} + +const Shader *AssetManager::getShader(const std::string name) +{ + if (mShaders.count(name) == 1) + return &mShaders[name]; + + return nullptr; +} + +INTERNAL std::string stringFromFile(const std::string &filename) +{ + std::ifstream file; + file.open(filename.c_str(), std::ios::in | std::ios::binary); + + std::string output; + std::string line; + + if (!file.is_open()) + { + std::runtime_error(std::string("Failed to open file: ") + filename); + } + else + { + while (file.good()) + { + std::getline(file, line); + output.append(line + "\n"); + } + } + file.close(); + return output; +} + +INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype) +{ + std::string shaderSource = stringFromFile(path); + const GLchar *source = shaderSource.c_str(); + + GLuint result = glCreateShader(shadertype); + glShaderSource(result, 1, &source, NULL); + glCompileShader(result); + + GLint success; + GLchar infoLog[512]; + glGetShaderiv(result, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(result, 512, NULL, infoLog); + std::cout << "glCompileShader failed: " << infoLog << std::endl; + } + + return result; +} + +const i32 AssetManager::loadShaderFiles(const std::string vertexPath, + const std::string fragmentPath, + const std::string name) +{ + + GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER); + GLuint fragmentShader = + createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER); + + Shader shader; + i32 result = shader.loadProgram(vertexShader, fragmentShader); + if (result) + return result; + + mShaders[name] = shader; + return 0; +} } diff --git a/src/Game.cpp b/src/Game.cpp index 4d0fd46..2b15542 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -38,20 +38,23 @@ void Game::init(Dengine::AssetManager *assetManager) } + result = assetManager->loadTextureImage("data/textures/plain_terrain.png", + "plain_terrain"); + this->shader = assetManager->getShader("sprite"); /* Init player */ - Dengine::Texture *wallTex = assetManager->getTexture("wall"); + const Dengine::Texture *tex = assetManager->getTexture("plain_terrain"); this->player = Dengine::Sprite(); - this->player.loadSprite(wallTex, glm::vec2(0, 0)); + this->player.loadSprite(tex, glm::vec2(0, 0)); /* Init game state */ this->state = GAME_ACTIVE; } -void Game::processInput(f32 dt) {} -void Game::update(f32 dt) {} +void Game::processInput(const f32 dt) {} +void Game::update(const f32 dt) {} void Game::render() { shader->use(); diff --git a/src/Shader.cpp b/src/Shader.cpp index 462bfd0..7591442 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -36,5 +36,5 @@ i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader) } -void Shader::use() { glUseProgram(this->program); } +void Shader::use() const { glUseProgram(this->program); } } diff --git a/src/Sprite.cpp b/src/Sprite.cpp index b53c935..2533343 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -12,7 +12,7 @@ tex(nullptr) Sprite::~Sprite() {} -b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos) +const b32 Sprite::loadSprite(const Texture *tex, const glm::vec2 pos) { if (!tex) return -1; @@ -55,7 +55,7 @@ b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos) return 0; } -void Sprite::render(Shader *shader) +void Sprite::render(const Shader *shader) const { // NOTE(doyle): Associate the VAO with this sprites VBO glBindVertexArray(this->vao); diff --git a/src/Texture.cpp b/src/Texture.cpp index 4536c57..1ca2c2a 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -3,17 +3,17 @@ namespace Dengine { Texture::Texture() -: mId(0) -, mWidth(0) -, mHeight(0) -, mInternalFormat(GL_RGB) -, mImageFormat(GL_RGB) -, mWrapS(GL_REPEAT) -, mWrapT(GL_REPEAT) -, mFilterMinification(GL_LINEAR) -, mFilterMagnification(GL_LINEAR) +: id(0) +, width(0) +, height(0) +, internalFormat(GL_RGB) +, imageFormat(GL_RGB) +, wrapS(GL_REPEAT) +, wrapT(GL_REPEAT) +, filterMinification(GL_LINEAR) +, filterMagnification(GL_LINEAR) { - glGenTextures(1, &mId); + glGenTextures(1, &this->id); } enum BytesPerPixel @@ -44,29 +44,32 @@ INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb) } } -void Texture::generate(GLuint width, GLuint height, u8 *image) +void Texture::generate(const GLuint width, const GLuint height, + const u8 *const image) { // TODO(doyle): Let us set the parameters gl params as well - mWidth = width; - mHeight = height; + this->width = width; + this->height = height; - glBindTexture(GL_TEXTURE_2D, mId); + glBindTexture(GL_TEXTURE_2D, this->id); /* Load image into texture */ // TODO(doyle) Figure out the gl format - glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, mWidth, mHeight, 0, - mImageFormat, GL_UNSIGNED_BYTE, image); + glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, this->width, + this->height, 0, this->imageFormat, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); /* Set parameter of currently bound texture */ - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mFilterMinification); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mFilterMagnification); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->wrapS); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->wrapT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + this->filterMinification); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, + this->filterMagnification); /* Unbind and clean up */ glBindTexture(GL_TEXTURE_2D, 0); } -void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, mId); } +void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, this->id); } } diff --git a/src/include/Breakout/Game.h b/src/include/Breakout/Game.h index 21cb8b7..a3d352c 100644 --- a/src/include/Breakout/Game.h +++ b/src/include/Breakout/Game.h @@ -29,13 +29,13 @@ public: Game(i32 width, i32 height); ~Game(); - void init(Dengine::AssetManager *assetManager); + void init(Dengine::AssetManager *const assetManager); - void processInput(f32 dt); - void update(f32 dt); + void processInput(const f32 dt); + void update(const f32 dt); void render(); private: - Dengine::Shader *shader; + const Dengine::Shader *shader; Dengine::Sprite player; }; } diff --git a/src/include/Dengine/AssetManager.h b/src/include/Dengine/AssetManager.h index c76b123..d14ac31 100644 --- a/src/include/Dengine/AssetManager.h +++ b/src/include/Dengine/AssetManager.h @@ -19,13 +19,14 @@ public: ~AssetManager(); /* Texture */ - Texture *getTexture(std::string name); - i32 loadTextureImage(std::string path, std::string name); + const Texture *getTexture(const std::string name); + const i32 loadTextureImage(const std::string path, const std::string name); /* Shaders */ - Shader *getShader(std::string name); - i32 loadShaderFiles(std::string vertexPath, std::string fragmentPath, - std::string name); + const Shader *getShader(const std::string name); + const i32 loadShaderFiles(const std::string vertexPath, + const std::string fragmentPath, + const std::string name); private: std::map mTextures; diff --git a/src/include/Dengine/Shader.h b/src/include/Dengine/Shader.h index 0e310e3..b8d4eb2 100644 --- a/src/include/Dengine/Shader.h +++ b/src/include/Dengine/Shader.h @@ -17,7 +17,7 @@ public: i32 loadProgram(GLuint vertexShader, GLuint fragmentShader); - void use(); + void use() const; }; } diff --git a/src/include/Dengine/Sprite.h b/src/include/Dengine/Sprite.h index d635571..d06a5fc 100644 --- a/src/include/Dengine/Sprite.h +++ b/src/include/Dengine/Sprite.h @@ -13,12 +13,12 @@ public: Sprite(); ~Sprite(); - b32 loadSprite(Texture *tex, glm::vec2 pos); + const b32 loadSprite(const Texture *tex, const glm::vec2 pos); - void render(Shader *shader); + void render(const Shader *shader) const; private: glm::vec2 pos; - Texture *tex; + const Texture *tex; GLuint vbo; GLuint vao; }; diff --git a/src/include/Dengine/Texture.h b/src/include/Dengine/Texture.h index c6cbb3d..0867174 100644 --- a/src/include/Dengine/Texture.h +++ b/src/include/Dengine/Texture.h @@ -9,35 +9,42 @@ namespace Dengine class Texture { public: - // Holds the ID of the texture object, used for all texture operations to - // reference to this particlar texture - GLuint mId; - - // Texture image dimensions - GLuint mWidth; - GLuint mHeight; - - // Texture Format - GLuint mInternalFormat; // Format of texture object - GLuint mImageFormat; // Format of loaded image - - // Texture configuration - GLuint mWrapS; // Wrapping mode on S axis - GLuint mWrapT; // Wrapping mode on T axis - - // Filtering mode if texture pixels < screen pixels - GLuint mFilterMinification; - // Filtering mode if texture pixels > screen pixels - GLuint mFilterMagnification; - // Constructor (sets default texture modes) Texture(); // Generates texture from image data - void generate(GLuint width, GLuint height, unsigned char *data); + void generate(const GLuint width, const GLuint height, + const u8 *const image); // Binds the texture as the current active GL_TEXTURE_2D texture object void bind() const; + + inline const GLuint getWidth() const { return this->width; } + inline const GLuint getHeight() const { return this->height; } + inline const GLuint getID() const { return this->id; } + +private: + // Holds the ID of the texture object, used for all texture operations to + // reference to this particlar texture + GLuint id; + + // Texture image dimensions + GLuint width; + GLuint height; + + // Texture Format + GLuint internalFormat; // Format of texture object + GLuint imageFormat; // Format of loaded image + + // Texture configuration + GLuint wrapS; // Wrapping mode on S axis + GLuint wrapT; // Wrapping mode on T axis + + // Filtering mode if texture pixels < screen pixels + GLuint filterMinification; + // Filtering mode if texture pixels > screen pixels + GLuint filterMagnification; + }; } #endif