Build world model in engine

This commit is contained in:
Doyle Thai 2016-06-17 00:14:58 +10:00
parent b75009d03e
commit 5cebf9ad0d
19 changed files with 294 additions and 177 deletions

View File

@ -1,5 +1,5 @@
{ {
ColumnLimit: 80, ColumnLimit: 100,
TabWidth: 4, TabWidth: 4,
IndentWidth: 4, # 1 tab IndentWidth: 4, # 1 tab
UseTab: ForIndentation, UseTab: ForIndentation,

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
# Custom # Custom
*.swp *.swp
*.opendb *.opendb
data/textures/SrcAssets/
# User-specific files # User-specific files
*.suo *.suo

View File

@ -122,9 +122,10 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="src\AssetManager.cpp" /> <ClCompile Include="src\AssetManager.cpp" />
<ClCompile Include="src\dengine.cpp" /> <ClCompile Include="src\dengine.cpp" />
<ClCompile Include="src\Game.cpp" /> <ClCompile Include="src\Entity.cpp" />
<ClCompile Include="src\Renderer.cpp" /> <ClCompile Include="src\Renderer.cpp" />
<ClCompile Include="src\Shader.cpp" /> <ClCompile Include="src\Shader.cpp" />
<ClCompile Include="src\WorldTraveller.cpp" />
<ClCompile Include="src\Texture.cpp" /> <ClCompile Include="src\Texture.cpp" />
<ClCompile Include="src\Tutorial.cpp" /> <ClCompile Include="src\Tutorial.cpp" />
</ItemGroup> </ItemGroup>
@ -138,10 +139,12 @@
<ClInclude Include="src\include\Dengine\AssetManager.h" /> <ClInclude Include="src\include\Dengine\AssetManager.h" />
<ClInclude Include="src\include\Dengine\Common.h" /> <ClInclude Include="src\include\Dengine\Common.h" />
<ClInclude Include="src\include\Breakout\Game.h" /> <ClInclude Include="src\include\Breakout\Game.h" />
<ClInclude Include="src\include\Dengine\Entity.h" />
<ClInclude Include="src\include\Dengine\OpenGL.h" /> <ClInclude Include="src\include\Dengine\OpenGL.h" />
<ClInclude Include="src\include\Dengine\Renderer.h" /> <ClInclude Include="src\include\Dengine\Renderer.h" />
<ClInclude Include="src\include\Dengine\Shader.h" /> <ClInclude Include="src\include\Dengine\Shader.h" />
<ClInclude Include="src\include\Dengine\Texture.h" /> <ClInclude Include="src\include\Dengine\Texture.h" />
<ClInclude Include="src\include\WorldTraveller\WorldTraveller.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -27,15 +27,18 @@
<ClCompile Include="src\AssetManager.cpp"> <ClCompile Include="src\AssetManager.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Game.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Tutorial.cpp"> <ClCompile Include="src\Tutorial.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Renderer.cpp"> <ClCompile Include="src\Renderer.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\WorldTraveller.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="data\shaders\default.vert.glsl" /> <None Include="data\shaders\default.vert.glsl" />
@ -65,5 +68,11 @@
<ClInclude Include="src\include\Dengine\Renderer.h"> <ClInclude Include="src\include\Dengine\Renderer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\WorldTraveller\WorldTraveller.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\Entity.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -21,24 +21,22 @@ Texture *AssetManager::getTexture(const std::string name)
return nullptr; return nullptr;
} }
const i32 AssetManager::loadTextureImage(const std::string path, const i32 AssetManager::loadTextureImage(const std::string path, const std::string name)
const std::string name)
{ {
/* Open the texture image */ /* Open the texture image */
i32 imgWidth, imgHeight, bytesPerPixel; i32 imgWidth, imgHeight, bytesPerPixel;
stbi_set_flip_vertically_on_load(TRUE); stbi_set_flip_vertically_on_load(TRUE);
u8 *image = u8 *image = stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0);
stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0);
if (!image) if (!image)
{ {
std::cerr << "stdbi_load() failed: " << stbi_failure_reason() std::cerr << "stdbi_load() failed: " << stbi_failure_reason() << std::endl;
<< std::endl;
return -1; return -1;
} }
Texture tex; Texture tex;
tex.generate(imgWidth, imgHeight, image); tex.generate(static_cast<GLuint>(imgWidth), static_cast<GLuint>(imgHeight),
static_cast<GLint>(bytesPerPixel), image);
stbi_image_free(image); stbi_image_free(image);
textures[name] = tex; textures[name] = tex;
@ -99,13 +97,11 @@ INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype)
} }
const i32 AssetManager::loadShaderFiles(const std::string vertexPath, const i32 AssetManager::loadShaderFiles(const std::string vertexPath,
const std::string fragmentPath, const std::string fragmentPath, const std::string name)
const std::string name)
{ {
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER); GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
GLuint fragmentShader = GLuint fragmentShader = createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
Shader shader; Shader shader;
i32 result = shader.loadProgram(vertexShader, fragmentShader); i32 result = shader.loadProgram(vertexShader, fragmentShader);

40
src/Entity.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <Dengine/Entity.h>
#include <Dengine/AssetManager.h>
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()
{
}
}

View File

@ -1,66 +0,0 @@
#include <Breakout\Game.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
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<GLfloat>(this->width), 0.0f,
static_cast<GLfloat>(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);
}
}

View File

@ -1,8 +1,8 @@
#include <Dengine/Renderer.h>
#include <Dengine/OpenGL.h> #include <Dengine/OpenGL.h>
#include <Dengine/Renderer.h>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
namespace Dengine namespace Dengine
{ {
@ -12,20 +12,15 @@ Renderer::Renderer(Shader *shader)
this->initRenderData(); this->initRenderData();
} }
Renderer::~Renderer() Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
{ void Renderer::drawEntity(Entity *entity, GLfloat rotate, glm::vec3 color)
glDeleteVertexArrays(1, &this->quadVAO);
}
void Renderer::drawSprite(const Texture *texture, glm::vec2 position,
glm::vec2 size, GLfloat rotate, glm::vec3 color)
{ {
this->shader->use(); 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)); 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 // 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; glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix;
@ -35,7 +30,7 @@ void Renderer::drawSprite(const Texture *texture, glm::vec2 position,
// this->shader->uniformSetVec3f("spriteColor", color); // this->shader->uniformSetVec3f("spriteColor", color);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
texture->bind(); entity->tex->bind();
this->shader->uniformSet1i("tex", 0); this->shader->uniformSet1i("tex", 0);
glBindVertexArray(this->quadVAO); glBindVertexArray(this->quadVAO);
@ -70,11 +65,10 @@ void Renderer::initRenderData()
/* Configure VAO */ /* Configure VAO */
const GLuint numVertexElements = 4; const GLuint numVertexElements = 4;
const GLuint vertexSize = sizeof(glm::vec4); const GLuint vertexSize = sizeof(glm::vec4);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize, glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize, (GLvoid *)0);
(GLvoid *)0);
/* Unbind */ /* Unbind */
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -14,8 +14,7 @@ Shader::Shader()
Shader::~Shader() {} Shader::~Shader() {}
const i32 Shader::loadProgram(const GLuint vertexShader, const i32 Shader::loadProgram(const GLuint vertexShader, const GLuint fragmentShader)
const GLuint fragmentShader)
{ {
this->id = glCreateProgram(); this->id = glCreateProgram();
glAttachShader(this->id, vertexShader); glAttachShader(this->id, vertexShader);
@ -36,7 +35,6 @@ const i32 Shader::loadProgram(const GLuint vertexShader,
} }
return 0; return 0;
} }
void Shader::uniformSet1i(const GLchar *name, const GLuint data) void Shader::uniformSet1i(const GLchar *name, const GLuint data)

View File

@ -6,7 +6,7 @@ Texture::Texture()
: id(0) : id(0)
, width(0) , width(0)
, height(0) , height(0)
, internalFormat(GL_RGB) , internalFormat(GL_RGBA)
, imageFormat(GL_RGB) , imageFormat(GL_RGB)
, wrapS(GL_REPEAT) , wrapS(GL_REPEAT)
, wrapT(GL_REPEAT) , wrapT(GL_REPEAT)
@ -28,26 +28,26 @@ INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb)
{ {
switch (bytesPerPixel) switch (bytesPerPixel)
{ {
case Greyscale: case Greyscale:
return GL_LUMINANCE; return GL_LUMINANCE;
case GreyscaleAlpha: case GreyscaleAlpha:
return GL_LUMINANCE_ALPHA; return GL_LUMINANCE_ALPHA;
case RGB: case RGB:
return (srgb ? GL_SRGB : GL_RGB); return (srgb ? GL_SRGB : GL_RGB);
case RGBA: case RGBA:
return (srgb ? GL_SRGB_ALPHA : GL_RGBA); return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
default: default:
// TODO(doyle): Invalid // TODO(doyle): Invalid
//std::cout << "getGLFormat() invalid bytesPerPixel: " // std::cout << "getGLFormat() invalid bytesPerPixel: "
// << bytesPerPixel << std::endl; // << bytesPerPixel << std::endl;
return GL_LUMINANCE; 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) 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->width = width;
this->height = height; this->height = height;
@ -55,17 +55,17 @@ void Texture::generate(const GLuint width, const GLuint height,
/* 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, this->internalFormat, this->width, this->imageFormat = getGLFormat(static_cast<enum BytesPerPixel>(bytesPerPixel), FALSE);
this->height, 0, this->imageFormat, 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); glGenerateMipmap(GL_TEXTURE_2D);
/* Set parameter of currently bound texture */ /* Set parameter of currently bound texture */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->wrapS); 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_WRAP_T, this->wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, this->filterMinification);
this->filterMinification); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, this->filterMagnification);
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);

116
src/WorldTraveller.cpp Normal file
View File

@ -0,0 +1,116 @@
#include <WorldTraveller/WorldTraveller.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <cstdlib>
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<GLfloat>(this->width), 0.0f,
static_cast<GLfloat>(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<u32>(glfwGetTime()));
}
void Game::update(const f32 dt)
{
const f32 heroSpeed = static_cast<f32>((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

View File

@ -1,33 +1,32 @@
#if 1 #if 1
#include <Dengine/OpenGL.h>
#include <Dengine/Common.h>
#include <Dengine/Shader.h>
#include <Dengine/AssetManager.h> #include <Dengine/AssetManager.h>
#include <Dengine/Common.h>
#include <Dengine/OpenGL.h>
#include <Dengine/Renderer.h> #include <Dengine/Renderer.h>
#include <Dengine/Shader.h>
#include <Breakout/Game.h> #include <WorldTraveller/WorldTraveller.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <iostream>
#include <string> #include <string>
void key_callback(GLFWwindow *window, int key, int scancode, int action, void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
int mode)
{ {
Breakout::Game *game = WorldTraveller::Game *game =
static_cast<Breakout::Game *>(glfwGetWindowUserPointer(window)); static_cast<WorldTraveller::Game *>(glfwGetWindowUserPointer(window));
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{ {
glfwSetWindowShouldClose(window, GL_TRUE); glfwSetWindowShouldClose(window, GL_TRUE);
} }
if (key >= 0 && key < Breakout::NUM_KEYS) if (key >= 0 && key < WorldTraveller::NUM_KEYS)
{ {
if (action == GLFW_PRESS) if (action == GLFW_PRESS)
game->keys[key] = TRUE; 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() int main()
{ {
@ -54,13 +49,11 @@ int main()
glm::ivec2 windowSize = glm::ivec2(1280, 720); glm::ivec2 windowSize = glm::ivec2(1280, 720);
GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Breakout", nullptr, nullptr);
"Breakout", nullptr, nullptr);
if (!window) if (!window)
{ {
std::cout << "glfwCreateWindow() failed: Failed to create window" std::cout << "glfwCreateWindow() failed: Failed to create window" << std::endl;
<< std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
@ -71,8 +64,7 @@ int main()
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) if (glewInit() != GLEW_OK)
{ {
std::cout << "glewInit() failed: Failed to initialise GLEW" std::cout << "glewInit() failed: Failed to initialise GLEW" << std::endl;
<< std::endl;
return -1; return -1;
} }
// NOTE(doyle): glewInit() bug that sets the gl error flag after init // 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); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK); glCullFace(GL_BACK);
Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y); WorldTraveller::Game game = WorldTraveller::Game(frameBufferSize.x, frameBufferSize.y);
game.init(); game.init();
glfwSetWindowUserPointer(window, static_cast<void *>(&game)); glfwSetWindowUserPointer(window, static_cast<void *>(&game));
@ -106,13 +98,12 @@ int main()
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
f32 currentFrame = static_cast<f32>(glfwGetTime()); f32 currentFrame = static_cast<f32>(glfwGetTime());
deltaTime = currentFrame - lastFrame; deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
/* Check and call events */ /* Check and call events */
glfwPollEvents(); glfwPollEvents();
game.processInput(deltaTime);
game.update(deltaTime); game.update(deltaTime);
/* Rendering commands here*/ /* Rendering commands here*/

View File

@ -1,13 +1,13 @@
#ifndef DENGINE_ASSET_MANAGER_H #ifndef DENGINE_ASSET_MANAGER_H
#define DENGINE_ASSET_MANAGER_H #define DENGINE_ASSET_MANAGER_H
#include <Dengine\Common.h> #include <Dengine/Common.h>
#include <Dengine\Texture.h> #include <Dengine/Shader.h>
#include <Dengine\Shader.h> #include <Dengine/Texture.h>
#include <iostream>
#include <map> #include <map>
#include <string> #include <string>
#include <iostream>
namespace Dengine namespace Dengine
{ {
@ -23,9 +23,8 @@ public:
/* Shaders */ /* Shaders */
static Shader *getShader(const std::string name); static Shader *getShader(const std::string name);
static const i32 loadShaderFiles(const std::string vertexPath, static const i32 loadShaderFiles(const std::string vertexPath, const std::string fragmentPath,
const std::string fragmentPath, const std::string name);
const std::string name);
}; };
} }
#endif #endif

View File

@ -0,0 +1,25 @@
#ifndef DENGINE_ENTITY_H
#define DENGINE_ENTITY_H
#include <Dengine/Common.h>
#include <Dengine/Texture.h>
#include <glm/glm.hpp>
namespace Dengine
{
class Entity
{
public:
Entity();
Entity(const glm::vec2 pos, const Texture *tex);
Entity(const glm::vec2 pos, const glm::vec2 size, const Texture *tex);
Entity(const glm::vec2 pos, const std::string texName);
~Entity();
glm::vec2 pos;
glm::vec2 size;
const Texture *tex;
};
}
#endif

View File

@ -5,8 +5,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <string>
#include <iostream> #include <iostream>
#include <string>
inline GLenum glCheckError_(const char *file, int line) inline GLenum glCheckError_(const char *file, int line)
{ {

View File

@ -2,8 +2,10 @@
#define DENGINE_RENDERER_H #define DENGINE_RENDERER_H
#include <Dengine/Common.h> #include <Dengine/Common.h>
#include <Dengine/Texture.h> #include <Dengine/Entity.h>
#include <Dengine/Shader.h> #include <Dengine/Shader.h>
#include <Dengine/Texture.h>
#include <GLM/glm.hpp> #include <GLM/glm.hpp>
namespace Dengine namespace Dengine
@ -14,9 +16,7 @@ public:
Renderer(Shader *shader); Renderer(Shader *shader);
~Renderer(); ~Renderer();
void drawSprite(const Texture *texture, glm::vec2 position, void drawEntity(Entity *entity, GLfloat rotate = 0.0f, glm::vec3 color = glm::vec3(1.0f));
glm::vec2 size = glm::vec2(10, 10), GLfloat rotate = 0.0f,
glm::vec3 color = glm::vec3(1.0f));
private: private:
Shader *shader; Shader *shader;

View File

@ -18,8 +18,7 @@ public:
Shader(); Shader();
~Shader(); ~Shader();
const i32 loadProgram(const GLuint vertexShader, const i32 loadProgram(const GLuint vertexShader, const GLuint fragmentShader);
const GLuint fragmentShader);
void uniformSet1i(const GLchar *name, const GLuint data); void uniformSet1i(const GLchar *name, const GLuint data);
void uniformSetMat4fv(const GLchar *name, const glm::mat4 data); void uniformSetMat4fv(const GLchar *name, const glm::mat4 data);

View File

@ -1,8 +1,8 @@
#ifndef DENGINE_TEXTURE_H #ifndef DENGINE_TEXTURE_H
#define DENGINE_TEXTURE_H #define DENGINE_TEXTURE_H
#include <Dengine/OpenGL.h>
#include <Dengine/Common.h> #include <Dengine/Common.h>
#include <Dengine/OpenGL.h>
namespace Dengine namespace Dengine
{ {
@ -13,7 +13,7 @@ public:
Texture(); Texture();
// Generates texture from image data // Generates texture from image data
void generate(const GLuint width, const GLuint height, void generate(const GLuint width, const GLuint height, const GLint bytesPerPixel,
const u8 *const image); 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
@ -37,14 +37,13 @@ private:
GLuint imageFormat; // Format of loaded image GLuint imageFormat; // Format of loaded image
// Texture configuration // Texture configuration
GLuint wrapS; // Wrapping mode on S axis GLuint wrapS; // Wrapping mode on S axis
GLuint wrapT; // Wrapping mode on T axis GLuint wrapT; // Wrapping mode on T axis
// Filtering mode if texture pixels < screen pixels // Filtering mode if texture pixels < screen pixels
GLuint filterMinification; GLuint filterMinification;
// Filtering mode if texture pixels > screen pixels // Filtering mode if texture pixels > screen pixels
GLuint filterMagnification; GLuint filterMagnification;
}; };
} }
#endif #endif

View File

@ -1,27 +1,40 @@
#ifndef BREAKOUT_GAME_H #ifndef WORLDTRAVELLER_GAME_H
#define BREAKOUT_GAME_H #define WORLDTRAVELLER_GAME_H
#include <Dengine/OpenGL.h> #include <Dengine/OpenGL.h>
#include <Dengine/Common.h> #include <Dengine/Common.h>
#include <Dengine/Renderer.h> #include <Dengine/Renderer.h>
#include <Dengine/Shader.h> #include <Dengine/Shader.h>
#include <Dengine/AssetManager.h> #include <Dengine/AssetManager.h>
#include <Dengine/Entity.h>
namespace Breakout namespace WorldTraveller
{ {
GLOBAL_VAR const i32 NUM_KEYS = 1024; GLOBAL_VAR const i32 NUM_KEYS = 1024;
GLOBAL_VAR const i32 METERS_TO_PIXEL = 100;
enum GameState enum Cardinal
{ {
GAME_ACTIVE, cardinal_north = 0,
GAME_MENU, cardinal_west = 1,
GAME_WIN cardinal_south = 2,
cardinal_east = 3,
cardinal_num,
cardinal_null
};
enum State
{
state_active,
state_menu,
state_win
}; };
class Game class Game
{ {
public: public:
GameState state; State state;
GLboolean keys[NUM_KEYS]; GLboolean keys[NUM_KEYS];
i32 width, height; i32 width, height;
@ -30,12 +43,12 @@ public:
void init(); void init();
void processInput(const f32 dt);
void update(const f32 dt); void update(const f32 dt);
void render(); void render();
private: private:
Dengine::Shader *shader; Dengine::Shader *shader;
Dengine::Renderer *renderer; Dengine::Renderer *renderer;
Dengine::Entity hero;
}; };
} }