Migrate game code into game.c

Separate the game code from the engine code
This commit is contained in:
Doyle Thai 2016-06-08 02:02:58 +10:00
parent 6f7f9fa50a
commit a01c597340
4 changed files with 70 additions and 45 deletions

View File

@ -5,9 +5,56 @@ namespace Breakout
Game::Game(i32 width, i32 height) {}
Game::~Game() {}
void Game::init() {}
void Game::init(Dengine::AssetManager *assetManager)
{
/* Initialise assets */
i32 result = 0;
result = assetManager->loadShaderFiles("data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl",
"sprite");
if (result)
{
// TODO(doyle): Do something
}
result = assetManager->loadTextureImage("data/textures/container.jpg",
"container");
if (result)
{
}
result = assetManager->loadTextureImage("data/textures/wall.jpg",
"wall");
if (result)
{
}
result = assetManager->loadTextureImage("data/textures/awesomeface.png",
"awesomeface");
if (result)
{
}
this->shader = assetManager->getShader("sprite");
/* Init player */
Dengine::Texture *wallTex = assetManager->getTexture("wall");
this->player = Dengine::Sprite();
this->player.loadSprite(wallTex, glm::vec2(0, 0));
/* Init game state */
this->state = GAME_ACTIVE;
}
void Game::processInput(f32 dt) {}
void Game::update(f32 dt) {}
void Game::render() {}
void Game::render()
{
shader->use();
this->player.render(shader);
}
}

View File

@ -28,19 +28,22 @@ b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos)
{-0.5f, -0.5f, 0.0f, 0.0f}, // Bottom left
};
/* Create and bind buffers */
/* Create buffers */
glGenBuffers(1, &this->vbo);
glGenVertexArrays(1, &this->vao);
/* Bind buffers */
glBindVertexArray(this->vao);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
/* Configure VBO */
glBufferData(GL_ARRAY_BUFFER, sizeof(spriteVertex), spriteVertex,
GL_DYNAMIC_DRAW);
GL_STATIC_DRAW);
/* Configure VAO */
i32 numElementsInVertex = 4;
i32 vertexSize = sizeof(glm::vec4);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, numElementsInVertex, GL_FLOAT, GL_FALSE,
vertexSize, (GLvoid *)(0));
@ -63,7 +66,8 @@ void Sprite::render(Shader *shader)
glUniform1i(glGetUniformLocation(shader->program, "tex"), 0);
/* Render */
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
i32 numVerticesToDraw = 4;
glDrawArrays(GL_TRIANGLE_STRIP, 0, numVerticesToDraw);
// Unbind
glBindVertexArray(0);

View File

@ -129,48 +129,19 @@ int main()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK);
Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y);
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
game.init();
/* Initialise shaders */
Dengine::AssetManager assetManager;
i32 result = 0;
Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y);
game.init(&assetManager);
result = assetManager.loadShaderFiles("data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl",
"sprite");
if (result) return result;
/* Load a texture */
result = assetManager.loadTextureImage("data/textures/container.jpg",
"container");
if (result) return result;
result = assetManager.loadTextureImage("data/textures/wall.jpg",
"wall");
if (result) return result;
result = assetManager.loadTextureImage("data/textures/awesomeface.png",
"awesomeface");
if (result) return result;
Dengine::Texture *containerTex = assetManager.getTexture("container");
Dengine::Texture *wallTex = assetManager.getTexture("wall");
Dengine::Texture *awesomeTex = assetManager.getTexture("awesomeface");
Dengine::Shader *shader = assetManager.getShader("sprite");
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
f32 deltaTime = 0.0f; // Time between current frame and last frame
f32 lastFrame = 0.0f; // Time of last frame
Dengine::Sprite sprite = Dengine::Sprite();
sprite.loadSprite(wallTex, glm::vec2(0, 0));
/* Main game loop */
while (!glfwWindowShouldClose(window))
{
f32 currentFrame = (f32)glfwGetTime();
f32 currentFrame = static_cast<f32>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
@ -184,11 +155,8 @@ int main()
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader->use();
game.render();
sprite.render(shader);
/* Swap the buffers */
glfwSwapBuffers(window);
}

View File

@ -1,8 +1,11 @@
#ifndef BREAKOUT_GAME_H
#define BREAKOUT_GAME_H
#include <Dengine\OpenGL.h>
#include <Dengine\Common.h>
#include <Dengine/OpenGL.h>
#include <Dengine/Common.h>
#include <Dengine/Sprite.h>
#include <Dengine/Shader.h>
#include <Dengine/AssetManager.h>
namespace Breakout
{
@ -19,18 +22,21 @@ enum GameState
class Game
{
public:
GameState mState;
GameState state;
GLboolean keys[NUM_KEYS];
i32 width, height;
Game(i32 width, i32 height);
~Game();
void init();
void init(Dengine::AssetManager *assetManager);
void processInput(f32 dt);
void update(f32 dt);
void render();
private:
Dengine::Shader *shader;
Dengine::Sprite player;
};
}