Port project over to C, some C++ left
This commit is contained in:
parent
bcb847c18c
commit
3c51010e77
@ -1,5 +1,5 @@
|
||||
{
|
||||
ColumnLimit: 100,
|
||||
ColumnLimit: 80,
|
||||
TabWidth: 4,
|
||||
IndentWidth: 4, # 1 tab
|
||||
UseTab: ForIndentation,
|
||||
|
@ -122,7 +122,6 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\AssetManager.cpp" />
|
||||
<ClCompile Include="src\dengine.cpp" />
|
||||
<ClCompile Include="src\Entity.cpp" />
|
||||
<ClCompile Include="src\Renderer.cpp" />
|
||||
<ClCompile Include="src\Shader.cpp" />
|
||||
<ClCompile Include="src\WorldTraveller.cpp" />
|
||||
|
@ -36,9 +36,6 @@
|
||||
<ClCompile Include="src\WorldTraveller.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Entity.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="data\shaders\default.vert.glsl" />
|
||||
|
@ -1,17 +1,14 @@
|
||||
#include <Dengine\AssetManager.h>
|
||||
#include <Dengine/AssetManager.h>
|
||||
|
||||
#define STBI_FAILURE_USERMSG
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <STB/stb_image.h>
|
||||
#include <fstream>
|
||||
|
||||
namespace Dengine
|
||||
{
|
||||
std::map<std::string, Texture> textures;
|
||||
std::map<std::string, Shader> shaders;
|
||||
|
||||
std::map<std::string, Texture> AssetManager::textures;
|
||||
std::map<std::string, Shader> AssetManager::shaders;
|
||||
|
||||
Texture *AssetManager::getTexture(const std::string name)
|
||||
Texture *asset_getTexture(const std::string name)
|
||||
{
|
||||
// NOTE(doyle): Since we're using a map, the count of an object can
|
||||
// only be 1 or 0
|
||||
@ -21,29 +18,31 @@ Texture *AssetManager::getTexture(const std::string name)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const i32 AssetManager::loadTextureImage(const std::string path, const std::string name)
|
||||
const i32 asset_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(static_cast<GLuint>(imgWidth), static_cast<GLuint>(imgHeight),
|
||||
static_cast<GLint>(bytesPerPixel), image);
|
||||
Texture tex = genTexture(CAST(GLuint)(imgWidth), CAST(GLuint)(imgHeight),
|
||||
CAST(GLint)(bytesPerPixel), image);
|
||||
glCheckError();
|
||||
stbi_image_free(image);
|
||||
|
||||
textures[name] = tex;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Shader *AssetManager::getShader(const std::string name)
|
||||
Shader *asset_getShader(const std::string name)
|
||||
{
|
||||
if (shaders.count(name) == 1)
|
||||
return &shaders[name];
|
||||
@ -96,19 +95,19 @@ INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype)
|
||||
return result;
|
||||
}
|
||||
|
||||
const i32 AssetManager::loadShaderFiles(const std::string vertexPath,
|
||||
const std::string fragmentPath, const std::string name)
|
||||
const i32 asset_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);
|
||||
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
|
||||
GLuint fragmentShader =
|
||||
createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
|
||||
|
||||
Shader shader;
|
||||
i32 result = shader.loadProgram(vertexShader, fragmentShader);
|
||||
i32 result = shader_loadProgram(&shader, vertexShader, fragmentShader);
|
||||
if (result)
|
||||
return result;
|
||||
|
||||
shaders[name] = shader;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,44 +0,0 @@
|
||||
#include <Dengine/Entity.h>
|
||||
#include <Dengine/AssetManager.h>
|
||||
|
||||
namespace Dengine
|
||||
{
|
||||
|
||||
Entity::Entity()
|
||||
: pos(glm::vec2(0, 0))
|
||||
, dPos(glm::vec2(0, 0))
|
||||
, size(glm::vec2(0, 0))
|
||||
, tex(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
Entity::Entity(const glm::vec2 pos, const Texture *tex)
|
||||
{
|
||||
this->pos = pos;
|
||||
this->dPos = glm::vec2(0, 0);
|
||||
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->dPos = glm::vec2(0, 0);
|
||||
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->dPos = glm::vec2(0, 0);
|
||||
this->tex = tex;
|
||||
this->size = glm::vec2(tex->getWidth(), tex->getHeight());
|
||||
|
||||
}
|
||||
|
||||
Entity::~Entity()
|
||||
{
|
||||
}
|
||||
}
|
@ -4,74 +4,35 @@
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtx/transform.hpp>
|
||||
|
||||
namespace Dengine
|
||||
void renderer_entity(Renderer *renderer, Entity *entity, GLfloat rotate,
|
||||
glm::vec3 color)
|
||||
{
|
||||
Renderer::Renderer(Shader *shader)
|
||||
{
|
||||
this->shader = shader;
|
||||
this->initRenderData();
|
||||
}
|
||||
|
||||
Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
|
||||
void Renderer::drawEntity(Entity *entity, GLfloat rotate, glm::vec3 color)
|
||||
{
|
||||
this->shader->use();
|
||||
shader_use(renderer->shader);
|
||||
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));
|
||||
glCheckError();
|
||||
|
||||
// NOTE(doyle): We draw everything as a unit square in OGL. Scale it to size
|
||||
glm::mat4 scaleMatrix = glm::scale(glm::vec3(entity->size, 1.0f));
|
||||
|
||||
glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix;
|
||||
|
||||
this->shader->uniformSetMat4fv("model", model);
|
||||
shader_uniformSetMat4fv(renderer->shader, "model", model);
|
||||
glCheckError();
|
||||
|
||||
// TODO(doyle): Unimplemented
|
||||
// this->shader->uniformSetVec3f("spriteColor", color);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
entity->tex->bind();
|
||||
this->shader->uniformSet1i("tex", 0);
|
||||
glCheckError();
|
||||
glBindTexture(GL_TEXTURE_2D, entity->tex->id);
|
||||
glCheckError();
|
||||
shader_uniformSet1i(renderer->shader, "tex", 0);
|
||||
glCheckError();
|
||||
|
||||
glBindVertexArray(this->quadVAO);
|
||||
glBindVertexArray(renderer->quadVAO);
|
||||
glCheckError();
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
glCheckError();
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Renderer::initRenderData()
|
||||
{
|
||||
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using
|
||||
// vertices v0, v1, v2, then v2, v1, v3 (note the order)
|
||||
glm::vec4 vertices[] = {
|
||||
// x y s t
|
||||
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left
|
||||
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left
|
||||
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right
|
||||
{1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right
|
||||
};
|
||||
|
||||
GLuint VBO;
|
||||
|
||||
/* Create buffers */
|
||||
glGenVertexArrays(1, &this->quadVAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
/* Bind buffers */
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBindVertexArray(this->quadVAO);
|
||||
|
||||
/* Configure VBO */
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
/* Configure VAO */
|
||||
const GLuint numVertexElements = 4;
|
||||
const GLuint vertexSize = sizeof(glm::vec4);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize, (GLvoid *)0);
|
||||
|
||||
/* Unbind */
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
glCheckError();
|
||||
}
|
||||
|
@ -1,55 +1,46 @@
|
||||
#include <Dengine\Shader.h>
|
||||
#include <Dengine/Shader.h>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace Dengine
|
||||
const i32 shader_loadProgram(Shader *const shader, const GLuint vertexShader,
|
||||
const GLuint fragmentShader)
|
||||
{
|
||||
Shader::Shader()
|
||||
: id(0)
|
||||
{
|
||||
}
|
||||
|
||||
Shader::~Shader() {}
|
||||
|
||||
const i32 Shader::loadProgram(const GLuint vertexShader, const GLuint fragmentShader)
|
||||
{
|
||||
this->id = glCreateProgram();
|
||||
glAttachShader(this->id, vertexShader);
|
||||
glAttachShader(this->id, fragmentShader);
|
||||
glLinkProgram(this->id);
|
||||
shader->id = glCreateProgram();
|
||||
glAttachShader(shader->id, vertexShader);
|
||||
glAttachShader(shader->id, fragmentShader);
|
||||
glLinkProgram(shader->id);
|
||||
|
||||
glDeleteShader(fragmentShader);
|
||||
glDeleteShader(vertexShader);
|
||||
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
glGetProgramiv(this->id, GL_LINK_STATUS, &success);
|
||||
glGetProgramiv(shader->id, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(this->id, 512, NULL, infoLog);
|
||||
std::cout << "glLinkProgram failed: " << infoLog << std::endl;
|
||||
glGetProgramInfoLog(shader->id, 512, NULL, infoLog);
|
||||
printf("glLinkProgram failed: %s\n", infoLog);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Shader::uniformSet1i(const GLchar *name, const GLuint data)
|
||||
void shader_uniformSet1i(Shader *const shader, const GLchar *name,
|
||||
const GLuint data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(this->id, name);
|
||||
GLint uniformLoc = glGetUniformLocation(shader->id, name);
|
||||
glUniform1i(uniformLoc, data);
|
||||
}
|
||||
|
||||
void Shader::uniformSetMat4fv(const GLchar *name, const glm::mat4 data)
|
||||
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
|
||||
const glm::mat4 data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(this->id, name);
|
||||
glCheckError();
|
||||
GLint uniformLoc = glGetUniformLocation(shader->id, name);
|
||||
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, glm::value_ptr(data));
|
||||
glCheckError();
|
||||
}
|
||||
|
||||
void Shader::use() const { glUseProgram(this->id); }
|
||||
}
|
||||
void shader_use(const Shader *const shader) { glUseProgram(shader->id); }
|
||||
|
@ -1,21 +1,5 @@
|
||||
#include <Dengine\Texture.h>
|
||||
|
||||
namespace Dengine
|
||||
{
|
||||
Texture::Texture()
|
||||
: id(0)
|
||||
, width(0)
|
||||
, height(0)
|
||||
, internalFormat(GL_RGBA)
|
||||
, imageFormat(GL_RGB)
|
||||
, wrapS(GL_REPEAT)
|
||||
, wrapT(GL_REPEAT)
|
||||
, filterMinification(GL_LINEAR)
|
||||
, filterMagnification(GL_LINEAR)
|
||||
{
|
||||
glGenTextures(1, &this->id);
|
||||
}
|
||||
|
||||
enum BytesPerPixel
|
||||
{
|
||||
Greyscale = 1,
|
||||
@ -24,7 +8,7 @@ enum BytesPerPixel
|
||||
RGBA = 4,
|
||||
};
|
||||
|
||||
INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb)
|
||||
INTERNAL GLint getGLFormat(i32 bytesPerPixel, b32 srgb)
|
||||
{
|
||||
switch (bytesPerPixel)
|
||||
{
|
||||
@ -44,32 +28,54 @@ INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb)
|
||||
}
|
||||
}
|
||||
|
||||
void Texture::generate(const GLuint width, const GLuint height, const GLint bytesPerPixel,
|
||||
const u8 *const image)
|
||||
Texture genTexture(const GLuint width, const GLuint height,
|
||||
const GLint bytesPerPixel, const u8 *const image)
|
||||
{
|
||||
// TODO(doyle): Let us set the parameters gl params as well
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
glCheckError();
|
||||
Texture tex = {};
|
||||
tex.width = width;
|
||||
tex.height = height;
|
||||
tex.internalFormat = GL_RGBA;
|
||||
tex.wrapS = GL_REPEAT;
|
||||
tex.wrapT = GL_REPEAT;
|
||||
tex.filterMinification = GL_LINEAR;
|
||||
tex.filterMagnification = GL_LINEAR;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, this->id);
|
||||
glGenTextures(1, &tex.id);
|
||||
glCheckError();
|
||||
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, tex.id);
|
||||
glCheckError();
|
||||
|
||||
/* Load image into texture */
|
||||
// TODO(doyle) Figure out the gl format
|
||||
this->imageFormat = getGLFormat(static_cast<enum BytesPerPixel>(bytesPerPixel), FALSE);
|
||||
tex.imageFormat = getGLFormat(bytesPerPixel, FALSE);
|
||||
glCheckError();
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, this->width, this->height, 0,
|
||||
this->imageFormat, GL_UNSIGNED_BYTE, image);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, tex.internalFormat, tex.width, tex.height, 0,
|
||||
tex.imageFormat, GL_UNSIGNED_BYTE, image);
|
||||
glCheckError();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
// TODO(doyle): Don't forget about thsi!
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 3, 3, 55, 55, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
image);
|
||||
glCheckError();
|
||||
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_WRAP_S, tex.wrapS);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tex.wrapT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
tex.filterMinification);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
||||
tex.filterMagnification);
|
||||
glCheckError();
|
||||
|
||||
/* Unbind and clean up */
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
glCheckError();
|
||||
|
||||
void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, this->id); }
|
||||
return tex;
|
||||
}
|
||||
|
@ -4,65 +4,85 @@
|
||||
#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()
|
||||
void worldTraveller_gameInit(GameState *state)
|
||||
{
|
||||
/* 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");
|
||||
asset_loadTextureImage("data/textures/WorldTraveller/elisa-spritesheet1.png",
|
||||
"hero");
|
||||
glCheckError();
|
||||
|
||||
this->shader = Dengine::AssetManager::getShader("sprite");
|
||||
this->shader->use();
|
||||
state->state = state_active;
|
||||
Renderer *renderer = &state->renderer;
|
||||
asset_loadShaderFiles("data/shaders/sprite.vert.glsl",
|
||||
"data/shaders/sprite.frag.glsl", "sprite");
|
||||
glCheckError();
|
||||
|
||||
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);
|
||||
renderer->shader = asset_getShader("sprite");
|
||||
shader_use(renderer->shader);
|
||||
glm::mat4 projection = glm::ortho(0.0f, CAST(GLfloat) state->width, 0.0f,
|
||||
CAST(GLfloat) state->height, 0.0f, 1.0f);
|
||||
shader_uniformSetMat4fv(renderer->shader, "projection", projection);
|
||||
glCheckError();
|
||||
|
||||
GLuint projectionLoc = glGetUniformLocation(this->shader->id, "projection");
|
||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
/* Init renderer */
|
||||
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using
|
||||
// vertices v0, v1, v2, then v2, v1, v3 (note the order)
|
||||
glm::vec4 vertices[] = {
|
||||
// x y s t
|
||||
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left
|
||||
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left
|
||||
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right
|
||||
{1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right
|
||||
};
|
||||
|
||||
/* Init game state */
|
||||
this->state = state_active;
|
||||
this->renderer = new Dengine::Renderer(this->shader);
|
||||
GLuint VBO;
|
||||
/* Create buffers */
|
||||
glGenVertexArrays(1, &renderer->quadVAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glCheckError();
|
||||
|
||||
/* Bind buffers */
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBindVertexArray(renderer->quadVAO);
|
||||
|
||||
/* Configure VBO */
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glCheckError();
|
||||
|
||||
/* Configure VAO */
|
||||
const GLuint numVertexElements = 4;
|
||||
const GLuint vertexSize = sizeof(glm::vec4);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize,
|
||||
(GLvoid *)0);
|
||||
glCheckError();
|
||||
|
||||
/* Unbind */
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glCheckError();
|
||||
/* 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));
|
||||
Entity *hero = &state->hero;
|
||||
hero->tex = asset_getTexture("hero");
|
||||
hero->size = glm::vec2(100, 100);
|
||||
|
||||
glm::vec2 screenCentre =
|
||||
glm::vec2(state->width / 2.0f, state->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;
|
||||
hero->pos = heroCentered;
|
||||
|
||||
srand(static_cast<u32>(glfwGetTime()));
|
||||
srand(CAST(u32)(glfwGetTime()));
|
||||
glCheckError();
|
||||
}
|
||||
|
||||
void Game::update(const f32 dt)
|
||||
INTERNAL void parseInput(GameState *state, const f32 dt)
|
||||
{
|
||||
/*
|
||||
Equations of Motion
|
||||
@ -78,24 +98,24 @@ void Game::update(const f32 dt)
|
||||
|
||||
glm::vec2 ddPos = glm::vec2(0, 0);
|
||||
|
||||
if (this->keys[GLFW_KEY_SPACE])
|
||||
if (state->keys[GLFW_KEY_SPACE])
|
||||
{
|
||||
}
|
||||
|
||||
if (this->keys[GLFW_KEY_RIGHT])
|
||||
if (state->keys[GLFW_KEY_RIGHT])
|
||||
{
|
||||
ddPos.x = 1.0f;
|
||||
}
|
||||
if (this->keys[GLFW_KEY_LEFT])
|
||||
if (state->keys[GLFW_KEY_LEFT])
|
||||
{
|
||||
ddPos.x = -1.0f;
|
||||
}
|
||||
|
||||
if (this->keys[GLFW_KEY_UP])
|
||||
if (state->keys[GLFW_KEY_UP])
|
||||
{
|
||||
ddPos.y = 1.0f;
|
||||
}
|
||||
if (this->keys[GLFW_KEY_DOWN])
|
||||
if (state->keys[GLFW_KEY_DOWN])
|
||||
{
|
||||
ddPos.y = -1.0f;
|
||||
}
|
||||
@ -108,42 +128,33 @@ void Game::update(const f32 dt)
|
||||
ddPos *= 0.70710678118f;
|
||||
}
|
||||
|
||||
const f32 heroSpeed = static_cast<f32>((22.0f * METERS_TO_PIXEL)); // m/s^2
|
||||
f32 heroSpeed = CAST(f32)(22.0f * METERS_TO_PIXEL); // m/s^2
|
||||
if (state->keys[GLFW_KEY_LEFT_SHIFT])
|
||||
{
|
||||
heroSpeed = CAST(f32)(22.0f * 5.0f * METERS_TO_PIXEL);
|
||||
}
|
||||
ddPos *= heroSpeed;
|
||||
|
||||
// TODO(doyle): Counteracting force on player's acceleration is arbitrary
|
||||
ddPos += -(5.5f * hero.dPos);
|
||||
Entity *hero = &state->hero;
|
||||
ddPos += -(5.5f * hero->dPos);
|
||||
|
||||
glm::vec2 newHeroP = (0.5f * ddPos) * Dengine::Math::squared(dt) + (hero.dPos * dt) + hero.pos;
|
||||
glm::vec2 newHeroP =
|
||||
(0.5f * ddPos) * squared(dt) + (hero->dPos * dt) + hero->pos;
|
||||
|
||||
hero.dPos = (ddPos * dt) + hero.dPos;
|
||||
hero.pos = newHeroP;
|
||||
hero->dPos = (ddPos * dt) + hero->dPos;
|
||||
hero->pos = newHeroP;
|
||||
}
|
||||
|
||||
void Game::render()
|
||||
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
|
||||
{
|
||||
/* Update */
|
||||
parseInput(state, dt);
|
||||
glCheckError();
|
||||
|
||||
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);
|
||||
/* Render */
|
||||
renderer_entity(&state->renderer, &state->hero);
|
||||
// TODO(doyle): Clean up lines
|
||||
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
|
||||
}
|
||||
} // namespace Dengine
|
||||
|
||||
|
@ -11,23 +11,18 @@
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
|
||||
{
|
||||
WorldTraveller::Game *game =
|
||||
static_cast<WorldTraveller::Game *>(glfwGetWindowUserPointer(window));
|
||||
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
|
||||
|
||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||
{
|
||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||
}
|
||||
|
||||
if (key >= 0 && key < WorldTraveller::NUM_KEYS)
|
||||
if (key >= 0 && key < NUM_KEYS)
|
||||
{
|
||||
if (action == GLFW_PRESS)
|
||||
game->keys[key] = TRUE;
|
||||
@ -50,11 +45,11 @@ int main()
|
||||
|
||||
glm::ivec2 windowSize = glm::ivec2(1280, 720);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Dengine", nullptr, nullptr);
|
||||
GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Dengine", NULL, NULL);
|
||||
|
||||
if (!window)
|
||||
{
|
||||
std::cout << "glfwCreateWindow() failed: Failed to create window" << std::endl;
|
||||
printf("glfwCreateWindow() failed: Failed to create window\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
@ -65,7 +60,7 @@ int main()
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK)
|
||||
{
|
||||
std::cout << "glewInit() failed: Failed to initialise GLEW" << std::endl;
|
||||
printf("glewInit() failed: Failed to initialise GLEW\n");
|
||||
return -1;
|
||||
}
|
||||
// NOTE(doyle): glewInit() bug that sets the gl error flag after init
|
||||
@ -87,12 +82,16 @@ int main()
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glCullFace(GL_BACK);
|
||||
|
||||
WorldTraveller::Game game = WorldTraveller::Game(frameBufferSize.x, frameBufferSize.y);
|
||||
game.init();
|
||||
GameState worldTraveller = {};
|
||||
worldTraveller.state = state_active;
|
||||
worldTraveller.width = frameBufferSize.x;
|
||||
worldTraveller.height = frameBufferSize.y;
|
||||
|
||||
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
|
||||
worldTraveller_gameInit(&worldTraveller);
|
||||
|
||||
f32 startTime = static_cast<f32>(glfwGetTime());
|
||||
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
|
||||
|
||||
f32 startTime = CAST(f32)(glfwGetTime());
|
||||
f32 secondsElapsed = 0.0f; // Time between current frame and last frame
|
||||
|
||||
#if 0
|
||||
@ -109,23 +108,20 @@ int main()
|
||||
/* Main game loop */
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
|
||||
/* Check and call events */
|
||||
glfwPollEvents();
|
||||
|
||||
game.update(secondsElapsed);
|
||||
|
||||
/* Rendering commands here*/
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
game.render();
|
||||
worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed);
|
||||
glCheckError();
|
||||
|
||||
/* Swap the buffers */
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
f32 endTime = static_cast<f32>(glfwGetTime());
|
||||
f32 endTime = (f32)glfwGetTime();
|
||||
secondsElapsed = endTime - startTime;
|
||||
|
||||
#if 0
|
||||
@ -144,9 +140,12 @@ int main()
|
||||
{
|
||||
f32 msPerFrame = secondsElapsed * 1000.0f;
|
||||
f32 framesPerSecond = 1.0f / secondsElapsed;
|
||||
std::stringstream ss;
|
||||
ss << "Dengine | " << msPerFrame << " ms/f | " << framesPerSecond << " fps";
|
||||
glfwSetWindowTitle(window, ss.str().c_str());
|
||||
|
||||
char textBuffer[256];
|
||||
snprintf(textBuffer, ARRAY_COUNT(textBuffer), "Dengine | %f ms/f | %f fps", msPerFrame,
|
||||
framesPerSecond);
|
||||
|
||||
glfwSetWindowTitle(window, textBuffer);
|
||||
titleUpdateFrequencyInSeconds = 0.5f;
|
||||
}
|
||||
|
||||
|
@ -9,22 +9,30 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace Dengine
|
||||
enum Assets
|
||||
{
|
||||
class AssetManager
|
||||
{
|
||||
public:
|
||||
static std::map<std::string, Texture> textures;
|
||||
static std::map<std::string, Shader> shaders;
|
||||
|
||||
/* Texture */
|
||||
static Texture *getTexture(const std::string name);
|
||||
static const i32 loadTextureImage(const std::string path, const std::string name);
|
||||
|
||||
/* Shaders */
|
||||
static Shader *getShader(const std::string name);
|
||||
static const i32 loadShaderFiles(const std::string vertexPath, const std::string fragmentPath,
|
||||
const std::string name);
|
||||
asset_vertShader,
|
||||
asset_fragShader,
|
||||
asset_hero,
|
||||
};
|
||||
}
|
||||
|
||||
struct AssetManager
|
||||
{
|
||||
Textures textures[256];
|
||||
Shaders shaders[256];
|
||||
};
|
||||
extern std::map<std::string, Texture> textures;
|
||||
extern std::map<std::string, Shader> shaders;
|
||||
|
||||
/* Texture */
|
||||
Texture *asset_getTexture(const std::string name);
|
||||
const i32 asset_loadTextureImage(const std::string path,
|
||||
const std::string name);
|
||||
|
||||
/* Shaders */
|
||||
Shader *asset_getShader(const std::string name);
|
||||
const i32 asset_loadShaderFiles(const std::string vertexPath,
|
||||
const std::string fragmentPath,
|
||||
const std::string name);
|
||||
|
||||
#endif
|
||||
|
@ -1,7 +1,8 @@
|
||||
#ifndef DENGINE_COMMON_H
|
||||
#define DENGINE_COMMON_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint32_t u32;
|
||||
@ -20,4 +21,7 @@ typedef double f64;
|
||||
#define INTERNAL static
|
||||
#define LOCAL_PERSIST static
|
||||
|
||||
#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
|
||||
#define CAST(type) (type)
|
||||
|
||||
#endif
|
||||
|
@ -6,23 +6,12 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Dengine
|
||||
struct Entity
|
||||
{
|
||||
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; // Position
|
||||
glm::vec2 dPos; // Velocity
|
||||
|
||||
glm::vec2 size;
|
||||
const Texture *tex;
|
||||
Texture *tex;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -3,17 +3,10 @@
|
||||
|
||||
#include <Dengine/Common.h>
|
||||
|
||||
namespace Dengine
|
||||
inline f32 squared(const f32 a)
|
||||
{
|
||||
class Math
|
||||
{
|
||||
public:
|
||||
static inline f32 squared(const f32 a)
|
||||
{
|
||||
f32 result = a * a;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
f32 result = a * a;
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -5,40 +5,38 @@
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
inline GLenum glCheckError_(const char *file, int line)
|
||||
{
|
||||
GLenum errorCode;
|
||||
while ((errorCode = glGetError()) != GL_NO_ERROR)
|
||||
{
|
||||
std::string error;
|
||||
switch (errorCode)
|
||||
{
|
||||
case GL_INVALID_ENUM:
|
||||
error = "INVALID_ENUM";
|
||||
printf("INVALID_ENUM | ");
|
||||
break;
|
||||
case GL_INVALID_VALUE:
|
||||
error = "INVALID_VALUE";
|
||||
printf("INVALID_VALUE | ");
|
||||
break;
|
||||
case GL_INVALID_OPERATION:
|
||||
error = "INVALID_OPERATION";
|
||||
printf("INVALID_OPERATION | ");
|
||||
break;
|
||||
case GL_STACK_OVERFLOW:
|
||||
error = "STACK_OVERFLOW";
|
||||
printf("STACK_OVERFLOW | ");
|
||||
break;
|
||||
case GL_STACK_UNDERFLOW:
|
||||
error = "STACK_UNDERFLOW";
|
||||
printf("STACK_UNDERFLOW | ");
|
||||
break;
|
||||
case GL_OUT_OF_MEMORY:
|
||||
error = "OUT_OF_MEMORY";
|
||||
printf("OUT_OF_MEMORY | ");
|
||||
break;
|
||||
case GL_INVALID_FRAMEBUFFER_OPERATION:
|
||||
error = "INVALID_FRAMEBUFFER_OPERATION";
|
||||
printf("INVALID_FRAMEBUFFER_OPERATION | ");
|
||||
break;
|
||||
}
|
||||
std::cout << error << " | " << file << " (" << line << ")" << std::endl;
|
||||
printf("%s (%d)\n", file, line);
|
||||
}
|
||||
return errorCode;
|
||||
}
|
||||
|
@ -8,22 +8,13 @@
|
||||
|
||||
#include <GLM/glm.hpp>
|
||||
|
||||
namespace Dengine
|
||||
struct Renderer
|
||||
{
|
||||
class Renderer
|
||||
{
|
||||
public:
|
||||
Renderer(Shader *shader);
|
||||
~Renderer();
|
||||
|
||||
void drawEntity(Entity *entity, GLfloat rotate = 0.0f, glm::vec3 color = glm::vec3(1.0f));
|
||||
|
||||
private:
|
||||
Shader *shader;
|
||||
GLuint quadVAO;
|
||||
|
||||
void initRenderData();
|
||||
};
|
||||
}
|
||||
|
||||
void renderer_entity(Renderer *renderer, Entity *entity, GLfloat rotate = 0.0f,
|
||||
glm::vec3 color = glm::vec3(1.0f));
|
||||
|
||||
#endif
|
||||
|
@ -6,25 +6,20 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Dengine
|
||||
struct Shader
|
||||
{
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
GLuint id;
|
||||
|
||||
Shader();
|
||||
~Shader();
|
||||
|
||||
const i32 loadProgram(const GLuint vertexShader, const GLuint fragmentShader);
|
||||
|
||||
void uniformSet1i(const GLchar *name, const GLuint data);
|
||||
void uniformSetMat4fv(const GLchar *name, const glm::mat4 data);
|
||||
|
||||
void use() const;
|
||||
};
|
||||
}
|
||||
|
||||
const i32 shader_loadProgram(Shader *const shader,
|
||||
const GLuint vertexShader,
|
||||
const GLuint fragmentShader);
|
||||
|
||||
void shader_uniformSet1i(Shader *const shader, const GLchar *name,
|
||||
const GLuint data);
|
||||
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
|
||||
const glm::mat4 data);
|
||||
|
||||
void shader_use(const Shader *const shader);
|
||||
|
||||
#endif
|
||||
|
@ -4,26 +4,8 @@
|
||||
#include <Dengine/Common.h>
|
||||
#include <Dengine/OpenGL.h>
|
||||
|
||||
namespace Dengine
|
||||
struct Texture
|
||||
{
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
// Constructor (sets default texture modes)
|
||||
Texture();
|
||||
|
||||
// Generates texture from image data
|
||||
void generate(const GLuint width, const GLuint height, const GLint bytesPerPixel,
|
||||
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;
|
||||
@ -45,5 +27,9 @@ private:
|
||||
// Filtering mode if texture pixels > screen pixels
|
||||
GLuint filterMagnification;
|
||||
};
|
||||
}
|
||||
|
||||
// Generates texture from image data
|
||||
Texture genTexture(const GLuint width, const GLuint height, const GLint bytesPerPixel,
|
||||
const u8 *const image);
|
||||
|
||||
#endif
|
||||
|
@ -9,21 +9,8 @@
|
||||
#include <Dengine/Renderer.h>
|
||||
#include <Dengine/Shader.h>
|
||||
|
||||
namespace WorldTraveller
|
||||
{
|
||||
GLOBAL_VAR const i32 NUM_KEYS = 1024;
|
||||
GLOBAL_VAR const f32 METERS_TO_PIXEL = 100;
|
||||
|
||||
enum Cardinal
|
||||
{
|
||||
cardinal_north = 0,
|
||||
cardinal_west = 1,
|
||||
cardinal_south = 2,
|
||||
cardinal_east = 3,
|
||||
cardinal_num,
|
||||
cardinal_null
|
||||
|
||||
};
|
||||
#define NUM_KEYS 1024
|
||||
#define METERS_TO_PIXEL 100
|
||||
|
||||
enum State
|
||||
{
|
||||
@ -32,25 +19,16 @@ enum State
|
||||
state_win
|
||||
};
|
||||
|
||||
class Game
|
||||
struct GameState
|
||||
{
|
||||
public:
|
||||
State state;
|
||||
GLboolean keys[NUM_KEYS];
|
||||
i32 width, height;
|
||||
|
||||
Game(i32 width, i32 height);
|
||||
~Game();
|
||||
|
||||
void init();
|
||||
|
||||
void update(const f32 dt);
|
||||
void render();
|
||||
private:
|
||||
Dengine::Shader *shader;
|
||||
Dengine::Renderer *renderer;
|
||||
Dengine::Entity hero;
|
||||
Renderer renderer;
|
||||
Entity hero;
|
||||
};
|
||||
}
|
||||
|
||||
void worldTraveller_gameInit(GameState *state);
|
||||
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt);
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user