diff --git a/.clang-format b/.clang-format
index a600b74..7c5883a 100644
--- a/.clang-format
+++ b/.clang-format
@@ -1,5 +1,5 @@
{
- ColumnLimit: 80,
+ ColumnLimit: 100,
TabWidth: 4,
IndentWidth: 4, # 1 tab
UseTab: ForIndentation,
diff --git a/.gitignore b/.gitignore
index d5d067f..44cea61 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
# Custom
*.swp
*.opendb
+data/textures/SrcAssets/
# User-specific files
*.suo
diff --git a/Dengine.vcxproj b/Dengine.vcxproj
index 30572ac..fd65531 100644
--- a/Dengine.vcxproj
+++ b/Dengine.vcxproj
@@ -122,9 +122,10 @@
-
+
+
@@ -138,10 +139,12 @@
+
+
diff --git a/Dengine.vcxproj.filters b/Dengine.vcxproj.filters
index aa74647..09f3b1d 100644
--- a/Dengine.vcxproj.filters
+++ b/Dengine.vcxproj.filters
@@ -27,15 +27,18 @@
Source Files
-
- Source Files
-
Source Files
Source Files
+
+ Source Files
+
+
+ Source Files
+
@@ -65,5 +68,11 @@
Header Files
+
+ Header Files
+
+
+ Header Files
+
\ No newline at end of file
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 4927dfe..1f510b2 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -21,24 +21,22 @@ Texture *AssetManager::getTexture(const std::string name)
return nullptr;
}
-const i32 AssetManager::loadTextureImage(const std::string path,
- const std::string name)
+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);
+ u8 *image = stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0);
if (!image)
{
- std::cerr << "stdbi_load() failed: " << stbi_failure_reason()
- << std::endl;
+ std::cerr << "stdbi_load() failed: " << stbi_failure_reason() << std::endl;
return -1;
}
Texture tex;
- tex.generate(imgWidth, imgHeight, image);
+ tex.generate(static_cast(imgWidth), static_cast(imgHeight),
+ static_cast(bytesPerPixel), image);
stbi_image_free(image);
textures[name] = tex;
@@ -99,13 +97,11 @@ INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype)
}
const i32 AssetManager::loadShaderFiles(const std::string vertexPath,
- const std::string fragmentPath,
- const std::string name)
+ const std::string fragmentPath, const std::string name)
{
- GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
- GLuint fragmentShader =
- createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
+ GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
+ GLuint fragmentShader = createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
Shader shader;
i32 result = shader.loadProgram(vertexShader, fragmentShader);
diff --git a/src/Entity.cpp b/src/Entity.cpp
new file mode 100644
index 0000000..2fec2a0
--- /dev/null
+++ b/src/Entity.cpp
@@ -0,0 +1,40 @@
+#include
+#include
+
+namespace Dengine
+{
+
+Entity::Entity()
+: pos(glm::vec2(0, 0))
+, size(glm::vec2(0, 0))
+, tex(nullptr)
+{
+}
+
+Entity::Entity(const glm::vec2 pos, const Texture *tex)
+{
+ this->pos = pos;
+ this->tex = tex;
+ this->size = glm::vec2(tex->getWidth(), tex->getHeight());
+}
+
+Entity::Entity(const glm::vec2 pos, glm::vec2 size, const Texture *tex)
+{
+ this->pos = pos;
+ this->tex = tex;
+ this->size = size;
+}
+
+Entity::Entity(const glm::vec2 pos, const std::string texName)
+{
+ Texture *tex = AssetManager::getTexture(texName);
+ this->pos = pos;
+ this->tex = tex;
+ this->size = glm::vec2(tex->getWidth(), tex->getHeight());
+
+}
+
+Entity::~Entity()
+{
+}
+}
diff --git a/src/Game.cpp b/src/Game.cpp
deleted file mode 100644
index 709672a..0000000
--- a/src/Game.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
-#include
-#include
-#include
-#include
-
-namespace Breakout
-{
-Game::Game(i32 width, i32 height)
-{
- this->width = width;
- this->height = height;
- glCheckError();
-}
-
-Game::~Game()
-{
- delete this->renderer;
-}
-
-void Game::init()
-{
- /* Initialise assets */
- Dengine::AssetManager::loadShaderFiles("data/shaders/sprite.vert.glsl",
- "data/shaders/sprite.frag.glsl",
- "sprite");
-
- Dengine::AssetManager::loadTextureImage("data/textures/container.jpg",
- "container");
- Dengine::AssetManager::loadTextureImage("data/textures/wall.jpg", "wall");
- Dengine::AssetManager::loadTextureImage("data/textures/awesomeface.png",
- "awesomeface");
- Dengine::AssetManager::loadTextureImage("data/textures/plain_terrain.jpg",
- "plain_terrain");
-
- this->shader = Dengine::AssetManager::getShader("sprite");
- this->shader->use();
-
- glm::mat4 projection =
- glm::ortho(0.0f, static_cast(this->width), 0.0f,
- static_cast(this->height), 0.0f, 1.0f);
- this->shader->uniformSetMat4fv("projection", projection);
-
- GLuint projectionLoc = glGetUniformLocation(this->shader->id, "projection");
- glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
-
- /* Init game state */
- this->state = GAME_ACTIVE;
- this->renderer = new Dengine::Renderer(this->shader);
-}
-
-void Game::processInput(const f32 dt) {}
-void Game::update(const f32 dt) {}
-void Game::render()
-{
- const Dengine::Texture *tex =
- Dengine::AssetManager::getTexture("plain_terrain");
- glm::vec2 pos = glm::vec2(0, 0);
- glm::vec2 size = glm::vec2(1280.0f, 720.0f);
- this->renderer->drawSprite(tex, pos, size);
-
- tex = Dengine::AssetManager::getTexture("container");
- pos = glm::vec2(200, 200);
- size = glm::vec2(250.0f, 250.0f);
- this->renderer->drawSprite(tex, pos, size);
-}
-}
diff --git a/src/Renderer.cpp b/src/Renderer.cpp
index c52bda7..33e6229 100644
--- a/src/Renderer.cpp
+++ b/src/Renderer.cpp
@@ -1,8 +1,8 @@
-#include
#include
+#include
-#include
#include
+#include
namespace Dengine
{
@@ -12,20 +12,15 @@ Renderer::Renderer(Shader *shader)
this->initRenderData();
}
-Renderer::~Renderer()
-{
- glDeleteVertexArrays(1, &this->quadVAO);
-}
-void Renderer::drawSprite(const Texture *texture, glm::vec2 position,
- glm::vec2 size, GLfloat rotate, glm::vec3 color)
+Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
+void Renderer::drawEntity(Entity *entity, GLfloat rotate, glm::vec3 color)
{
this->shader->use();
- glm::mat4 transMatrix = glm::translate(glm::vec3(position, 0.0f));
+ glm::mat4 transMatrix = glm::translate(glm::vec3(entity->pos, 0.0f));
glm::mat4 rotateMatrix = glm::rotate(rotate, glm::vec3(0.0f, 0.0f, 1.0f));
-
// NOTE(doyle): We draw everything as a unit square in OGL. Scale it to size
- glm::mat4 scaleMatrix = glm::scale(glm::vec3(size, 1.0f));
+ glm::mat4 scaleMatrix = glm::scale(glm::vec3(entity->size, 1.0f));
glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix;
@@ -35,7 +30,7 @@ void Renderer::drawSprite(const Texture *texture, glm::vec2 position,
// this->shader->uniformSetVec3f("spriteColor", color);
glActiveTexture(GL_TEXTURE0);
- texture->bind();
+ entity->tex->bind();
this->shader->uniformSet1i("tex", 0);
glBindVertexArray(this->quadVAO);
@@ -70,11 +65,10 @@ void Renderer::initRenderData()
/* Configure VAO */
const GLuint numVertexElements = 4;
- const GLuint vertexSize = sizeof(glm::vec4);
+ const GLuint vertexSize = sizeof(glm::vec4);
glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize,
- (GLvoid *)0);
+ glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize, (GLvoid *)0);
/* Unbind */
glBindBuffer(GL_ARRAY_BUFFER, 0);
diff --git a/src/Shader.cpp b/src/Shader.cpp
index 9a3b6c1..5a427bd 100644
--- a/src/Shader.cpp
+++ b/src/Shader.cpp
@@ -14,8 +14,7 @@ Shader::Shader()
Shader::~Shader() {}
-const i32 Shader::loadProgram(const GLuint vertexShader,
- const GLuint fragmentShader)
+const i32 Shader::loadProgram(const GLuint vertexShader, const GLuint fragmentShader)
{
this->id = glCreateProgram();
glAttachShader(this->id, vertexShader);
@@ -36,7 +35,6 @@ const i32 Shader::loadProgram(const GLuint vertexShader,
}
return 0;
-
}
void Shader::uniformSet1i(const GLchar *name, const GLuint data)
diff --git a/src/Texture.cpp b/src/Texture.cpp
index 1ca2c2a..e185981 100644
--- a/src/Texture.cpp
+++ b/src/Texture.cpp
@@ -6,7 +6,7 @@ Texture::Texture()
: id(0)
, width(0)
, height(0)
-, internalFormat(GL_RGB)
+, internalFormat(GL_RGBA)
, imageFormat(GL_RGB)
, wrapS(GL_REPEAT)
, wrapT(GL_REPEAT)
@@ -28,26 +28,26 @@ INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb)
{
switch (bytesPerPixel)
{
- case Greyscale:
- return GL_LUMINANCE;
- case GreyscaleAlpha:
- return GL_LUMINANCE_ALPHA;
- case RGB:
- return (srgb ? GL_SRGB : GL_RGB);
- case RGBA:
- return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
- default:
- // TODO(doyle): Invalid
- //std::cout << "getGLFormat() invalid bytesPerPixel: "
- // << bytesPerPixel << std::endl;
- return GL_LUMINANCE;
+ case Greyscale:
+ return GL_LUMINANCE;
+ case GreyscaleAlpha:
+ return GL_LUMINANCE_ALPHA;
+ case RGB:
+ return (srgb ? GL_SRGB : GL_RGB);
+ case RGBA:
+ return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
+ default:
+ // TODO(doyle): Invalid
+ // std::cout << "getGLFormat() invalid bytesPerPixel: "
+ // << bytesPerPixel << std::endl;
+ return GL_LUMINANCE;
}
}
-void Texture::generate(const GLuint width, const GLuint height,
+void Texture::generate(const GLuint width, const GLuint height, const GLint bytesPerPixel,
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
this->width = width;
this->height = height;
@@ -55,17 +55,17 @@ void Texture::generate(const GLuint width, const GLuint height,
/* Load image into texture */
// TODO(doyle) Figure out the gl format
- glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, this->width,
- this->height, 0, this->imageFormat, GL_UNSIGNED_BYTE, image);
+ this->imageFormat = getGLFormat(static_cast(bytesPerPixel), FALSE);
+
+ 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, 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);
+ 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);
diff --git a/src/WorldTraveller.cpp b/src/WorldTraveller.cpp
new file mode 100644
index 0000000..08a3be4
--- /dev/null
+++ b/src/WorldTraveller.cpp
@@ -0,0 +1,116 @@
+#include
+
+#include
+#include
+#include
+
+#include
+
+namespace WorldTraveller
+{
+
+// TODO(doyle): Entity list for each world
+// TODO(doyle): Jumping mechanics
+// TODO(doyle): Collision
+
+
+Game::Game(i32 width, i32 height)
+{
+ this->width = width;
+ this->height = height;
+
+ for (i32 i = 0; i < NUM_KEYS; i++)
+ this->keys[i] = FALSE;
+}
+
+Game::~Game() { delete this->renderer; }
+
+void Game::init()
+{
+ /* Initialise assets */
+ std::string texFolder = "data/textures/WorldTraveller/";
+ Dengine::AssetManager::loadShaderFiles("data/shaders/sprite.vert.glsl",
+ "data/shaders/sprite.frag.glsl", "sprite");
+
+ Dengine::AssetManager::loadTextureImage(texFolder + "hero.png", "hero");
+ Dengine::AssetManager::loadTextureImage(texFolder + "wall.png", "wall");
+ Dengine::AssetManager::loadTextureImage(texFolder + "hitMarker.png", "hitMarker");
+
+ this->shader = Dengine::AssetManager::getShader("sprite");
+ this->shader->use();
+
+ glm::mat4 projection = glm::ortho(0.0f, static_cast(this->width), 0.0f,
+ static_cast(this->height), 0.0f, 1.0f);
+ this->shader->uniformSetMat4fv("projection", projection);
+
+ GLuint projectionLoc = glGetUniformLocation(this->shader->id, "projection");
+ glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
+
+ /* Init game state */
+ this->state = state_active;
+ this->renderer = new Dengine::Renderer(this->shader);
+
+ /* Init hero */
+ this->hero = Dengine::Entity(glm::vec2(0, 0), "hero");
+
+ glm::vec2 screenCentre = glm::vec2(this->width / 2.0f, this->height / 2.0f);
+ glm::vec2 heroOffsetToCentre = glm::vec2(-((i32)hero.size.x / 2), -((i32)hero.size.y / 2));
+
+ glm::vec2 heroCentered = screenCentre + heroOffsetToCentre;
+ hero.pos = heroCentered;
+
+ srand(static_cast(glfwGetTime()));
+}
+
+void Game::update(const f32 dt)
+{
+ const f32 heroSpeed = static_cast((3.0f * METERS_TO_PIXEL) * dt);
+
+ if (this->keys[GLFW_KEY_SPACE])
+ {
+#if 0
+ Dengine::Entity hitMarker = Dengine::Entity(glm::vec2(0, 0), "hitMarker");
+ glm::vec2 hitMarkerP =
+ glm::vec2((hero.pos.x * 1.5f) + hitMarker.tex->getWidth(), hero.pos.y * 1.5f);
+ hitMarker.pos = hitMarkerP;
+ renderer->drawEntity(&hitMarker);
+#endif
+ }
+
+ if (this->keys[GLFW_KEY_RIGHT])
+ {
+ hero.pos.x += heroSpeed;
+ }
+ else if (this->keys[GLFW_KEY_LEFT])
+ {
+ hero.pos.x -= heroSpeed;
+ }
+}
+
+void Game::render()
+{
+
+ Dengine::Renderer *renderer = this->renderer;
+
+ Dengine::Entity wall = Dengine::Entity(glm::vec2(0, 0), "wall");
+ glm::vec2 maxTilesOnScreen =
+ glm::vec2((this->width / wall.size.x), (this->height / wall.size.y));
+
+ Dengine::Entity hitMarker = Dengine::Entity(glm::vec2(100, 100), "hitMarker");
+
+ for (i32 x = 0; x < maxTilesOnScreen.x; x++)
+ {
+ for (i32 y = 0; y < maxTilesOnScreen.y; y++)
+ {
+ if (x == 0 || x == maxTilesOnScreen.x - 1 || y == 0 || y == maxTilesOnScreen.y - 1)
+ {
+ wall.pos = glm::vec2(x * wall.tex->getWidth(), y * wall.tex->getHeight());
+
+ renderer->drawEntity(&wall);
+ }
+ }
+ }
+ renderer->drawEntity(&hero);
+ renderer->drawEntity(&hitMarker);
+}
+} // namespace Dengine
diff --git a/src/dengine.cpp b/src/dengine.cpp
index 3e2621a..045d0c4 100644
--- a/src/dengine.cpp
+++ b/src/dengine.cpp
@@ -1,33 +1,32 @@
#if 1
-#include
-#include
-#include
#include
+#include
+#include
#include
+#include
-#include
+#include
#include
#include
#include
-#include
#include
#include
+#include
#include
-void key_callback(GLFWwindow *window, int key, int scancode, int action,
- int mode)
+void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
{
- Breakout::Game *game =
- static_cast(glfwGetWindowUserPointer(window));
+ WorldTraveller::Game *game =
+ static_cast(glfwGetWindowUserPointer(window));
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
- if (key >= 0 && key < Breakout::NUM_KEYS)
+ if (key >= 0 && key < WorldTraveller::NUM_KEYS)
{
if (action == GLFW_PRESS)
game->keys[key] = TRUE;
@@ -36,13 +35,9 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action,
}
}
-void mouse_callback(GLFWwindow *window, double xPos, double yPos)
-{
-}
+void mouse_callback(GLFWwindow *window, double xPos, double yPos) {}
-void scroll_callback(GLFWwindow *window, double xOffset, double yOffset)
-{
-}
+void scroll_callback(GLFWwindow *window, double xOffset, double yOffset) {}
int main()
{
@@ -54,13 +49,11 @@ int main()
glm::ivec2 windowSize = glm::ivec2(1280, 720);
- GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y,
- "Breakout", nullptr, nullptr);
+ GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Breakout", nullptr, nullptr);
if (!window)
{
- std::cout << "glfwCreateWindow() failed: Failed to create window"
- << std::endl;
+ std::cout << "glfwCreateWindow() failed: Failed to create window" << std::endl;
glfwTerminate();
return -1;
}
@@ -71,8 +64,7 @@ int main()
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
- std::cout << "glewInit() failed: Failed to initialise GLEW"
- << std::endl;
+ std::cout << "glewInit() failed: Failed to initialise GLEW" << std::endl;
return -1;
}
// NOTE(doyle): glewInit() bug that sets the gl error flag after init
@@ -94,7 +86,7 @@ int main()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK);
- Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y);
+ WorldTraveller::Game game = WorldTraveller::Game(frameBufferSize.x, frameBufferSize.y);
game.init();
glfwSetWindowUserPointer(window, static_cast(&game));
@@ -106,13 +98,12 @@ int main()
while (!glfwWindowShouldClose(window))
{
f32 currentFrame = static_cast(glfwGetTime());
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
+ deltaTime = currentFrame - lastFrame;
+ lastFrame = currentFrame;
/* Check and call events */
glfwPollEvents();
- game.processInput(deltaTime);
game.update(deltaTime);
/* Rendering commands here*/
diff --git a/src/include/Dengine/AssetManager.h b/src/include/Dengine/AssetManager.h
index 4fc0a2e..ef99525 100644
--- a/src/include/Dengine/AssetManager.h
+++ b/src/include/Dengine/AssetManager.h
@@ -1,13 +1,13 @@
#ifndef DENGINE_ASSET_MANAGER_H
#define DENGINE_ASSET_MANAGER_H
-#include
-#include
-#include
+#include
+#include
+#include
+#include
#include