Apply const correctness to project

This commit is contained in:
Doyle Thai 2016-06-08 17:29:16 +10:00
parent a01c597340
commit 60fcecab0e
10 changed files with 182 additions and 168 deletions

View File

@ -8,111 +8,111 @@
namespace Dengine namespace Dengine
{ {
AssetManager::AssetManager(){} AssetManager::AssetManager() {}
AssetManager::~AssetManager(){} AssetManager::~AssetManager() {}
Texture *AssetManager::getTexture(std::string name) const Texture *AssetManager::getTexture(const std::string name)
{ {
// NOTE(doyle): Since we're using a map, the count of an object can // NOTE(doyle): Since we're using a map, the count of an object can
// only be 1 or 0 // only be 1 or 0
if (mTextures.count(name) == 1) if (mTextures.count(name) == 1)
return &mTextures[name]; 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;
}
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;
}
} }

View File

@ -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"); this->shader = assetManager->getShader("sprite");
/* Init player */ /* Init player */
Dengine::Texture *wallTex = assetManager->getTexture("wall"); const Dengine::Texture *tex = assetManager->getTexture("plain_terrain");
this->player = Dengine::Sprite(); this->player = Dengine::Sprite();
this->player.loadSprite(wallTex, glm::vec2(0, 0)); this->player.loadSprite(tex, glm::vec2(0, 0));
/* Init game state */ /* Init game state */
this->state = GAME_ACTIVE; this->state = GAME_ACTIVE;
} }
void Game::processInput(f32 dt) {} void Game::processInput(const f32 dt) {}
void Game::update(f32 dt) {} void Game::update(const f32 dt) {}
void Game::render() void Game::render()
{ {
shader->use(); shader->use();

View File

@ -36,5 +36,5 @@ i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader)
} }
void Shader::use() { glUseProgram(this->program); } void Shader::use() const { glUseProgram(this->program); }
} }

View File

@ -12,7 +12,7 @@ tex(nullptr)
Sprite::~Sprite() {} 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; if (!tex) return -1;
@ -55,7 +55,7 @@ b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos)
return 0; return 0;
} }
void Sprite::render(Shader *shader) void Sprite::render(const Shader *shader) const
{ {
// NOTE(doyle): Associate the VAO with this sprites VBO // NOTE(doyle): Associate the VAO with this sprites VBO
glBindVertexArray(this->vao); glBindVertexArray(this->vao);

View File

@ -3,17 +3,17 @@
namespace Dengine namespace Dengine
{ {
Texture::Texture() Texture::Texture()
: mId(0) : id(0)
, mWidth(0) , width(0)
, mHeight(0) , height(0)
, mInternalFormat(GL_RGB) , internalFormat(GL_RGB)
, mImageFormat(GL_RGB) , imageFormat(GL_RGB)
, mWrapS(GL_REPEAT) , wrapS(GL_REPEAT)
, mWrapT(GL_REPEAT) , wrapT(GL_REPEAT)
, mFilterMinification(GL_LINEAR) , filterMinification(GL_LINEAR)
, mFilterMagnification(GL_LINEAR) , filterMagnification(GL_LINEAR)
{ {
glGenTextures(1, &mId); glGenTextures(1, &this->id);
} }
enum BytesPerPixel 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 // TODO(doyle): Let us set the parameters gl params as well
mWidth = width; this->width = width;
mHeight = height; this->height = height;
glBindTexture(GL_TEXTURE_2D, mId); glBindTexture(GL_TEXTURE_2D, this->id);
/* Load image into texture */ /* Load image into texture */
// TODO(doyle) Figure out the gl format // TODO(doyle) Figure out the gl format
glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, mWidth, mHeight, 0, glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, this->width,
mImageFormat, GL_UNSIGNED_BYTE, image); this->height, 0, this->imageFormat, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
/* Set parameter of currently bound texture */ /* Set parameter of currently bound texture */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mFilterMinification); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mFilterMagnification); this->filterMinification);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
this->filterMagnification);
/* Unbind and clean up */ /* Unbind and clean up */
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, mId); } void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, this->id); }
} }

View File

@ -29,13 +29,13 @@ public:
Game(i32 width, i32 height); Game(i32 width, i32 height);
~Game(); ~Game();
void init(Dengine::AssetManager *assetManager); void init(Dengine::AssetManager *const assetManager);
void processInput(f32 dt); void processInput(const f32 dt);
void update(f32 dt); void update(const f32 dt);
void render(); void render();
private: private:
Dengine::Shader *shader; const Dengine::Shader *shader;
Dengine::Sprite player; Dengine::Sprite player;
}; };
} }

View File

@ -19,13 +19,14 @@ public:
~AssetManager(); ~AssetManager();
/* Texture */ /* Texture */
Texture *getTexture(std::string name); const Texture *getTexture(const std::string name);
i32 loadTextureImage(std::string path, std::string name); const i32 loadTextureImage(const std::string path, const std::string name);
/* Shaders */ /* Shaders */
Shader *getShader(std::string name); const Shader *getShader(const std::string name);
i32 loadShaderFiles(std::string vertexPath, std::string fragmentPath, const i32 loadShaderFiles(const std::string vertexPath,
std::string name); const std::string fragmentPath,
const std::string name);
private: private:
std::map<std::string, Texture> mTextures; std::map<std::string, Texture> mTextures;

View File

@ -17,7 +17,7 @@ public:
i32 loadProgram(GLuint vertexShader, GLuint fragmentShader); i32 loadProgram(GLuint vertexShader, GLuint fragmentShader);
void use(); void use() const;
}; };
} }

View File

@ -13,12 +13,12 @@ public:
Sprite(); Sprite();
~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: private:
glm::vec2 pos; glm::vec2 pos;
Texture *tex; const Texture *tex;
GLuint vbo; GLuint vbo;
GLuint vao; GLuint vao;
}; };

View File

@ -9,35 +9,42 @@ namespace Dengine
class Texture class Texture
{ {
public: 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) // Constructor (sets default texture modes)
Texture(); Texture();
// Generates texture from image data // 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 // Binds the texture as the current active GL_TEXTURE_2D texture object
void bind() const; 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 #endif