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
{
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;
}
}

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");
/* 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();

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() {}
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);

View File

@ -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); }
}

View File

@ -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;
};
}

View File

@ -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<std::string, Texture> mTextures;

View File

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

View File

@ -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;
};

View File

@ -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