Add game entity list rendering, create animations

This commit is contained in:
Doyle Thai 2016-06-23 18:40:00 +10:00
parent eda06bcfe9
commit 7a2dc3da41
4 changed files with 89 additions and 92 deletions

View File

@ -2,9 +2,15 @@
#include <Dengine/Math.h> #include <Dengine/Math.h>
#include <WorldTraveller/WorldTraveller.h> #include <WorldTraveller/WorldTraveller.h>
//TODO(doyle): This is temporary! Maybe abstract into our platform layer, or
//choose to load assets outside of WorldTraveller!
#include <stdlib.h>
void updateBufferObject(GLuint vbo, v4 texNDC) void updateBufferObject(GLuint vbo, v4 texNDC)
{ {
// TODO(doyle): We assume that vbo and vao are assigned // TODO(doyle): We assume that vbo and vao are assigned
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using
// vertices v0, v1, v2, then v2, v1, v3 (note the order)
v4 vertices[] = { v4 vertices[] = {
// x y s t // x y s t
{0.0f, 1.0f, texNDC.x, texNDC.y}, // Top left {0.0f, 1.0f, texNDC.x, texNDC.y}, // Top left
@ -26,19 +32,28 @@ void worldTraveller_gameInit(GameState *state)
/* Initialise assets */ /* Initialise assets */
asset_loadTextureImage( asset_loadTextureImage(
"data/textures/WorldTraveller/TerraSprite1024.png", texlist_hero); "data/textures/WorldTraveller/TerraSprite1024.png", texlist_hero);
glCheckError();
asset_loadShaderFiles("data/shaders/sprite.vert.glsl", asset_loadShaderFiles("data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl", shaderlist_sprite); "data/shaders/sprite.frag.glsl", shaderlist_sprite);
glCheckError(); glCheckError();
state->state = state_active; state->state = state_active;
state->heroLastDirection = direction_east;
/* Init hero */ /* Init hero */
Entity *hero = &state->hero; SpriteAnim heroAnim = {NULL, 1, 0, 1.0f, 1.0f};
hero->tex = asset_getTexture(texlist_hero); // TODO(doyle): Get rid of
hero->size = V2(58.0f, 98.0f); heroAnim.rect = (v4 *)calloc(1, sizeof(v4));
hero->pos = V2(0.0f, 0.0f); heroAnim.rect[0] = V4(746.0f, 1018.0f, 804.0f, 920.0f);
Entity heroEnt = {V2(0.0f, 0.0f),
V2(0.0f, 0.0f),
V2(58.0f, 98.0f),
direction_east,
asset_getTexture(texlist_hero),
heroAnim};
state->heroIndex = state->freeEntityIndex;
state->entityList[state->freeEntityIndex++] = heroEnt;
Entity *hero = &state->entityList[state->heroIndex];
Texture *heroSheet = hero->tex; Texture *heroSheet = hero->tex;
v2 sheetSize = V2(CAST(f32)heroSheet->width, CAST(f32)heroSheet->height); v2 sheetSize = V2(CAST(f32)heroSheet->width, CAST(f32)heroSheet->height);
@ -50,15 +65,16 @@ void worldTraveller_gameInit(GameState *state)
CAST(i32) sheetSize.w, CAST(i32) sheetSize.h); CAST(i32) sheetSize.w, CAST(i32) sheetSize.h);
} }
f32 ndcFactor = sheetSize.w; /* Create a NPC */
v2 heroStartPixel = V2(746.0f, 1018.0f); SpriteAnim npcAnim = {NULL, 2, 0, 0.3f, 0.3f};
v2 heroSizeOnSheet = V2(58.0f, 98.0f); // TODO(doyle): Get rid of
v4 heroRect = V4(heroStartPixel.x, npcAnim.rect = (v4 *)calloc(2, sizeof(v4));
heroStartPixel.y, npcAnim.rect[0] = V4(944.0f, 918.0f, 1010.0f, 816.0f);
heroStartPixel.x + heroSizeOnSheet.w, npcAnim.rect[1] = V4(944.0f, 812.0f, 1010.0f, 710.0f);
heroStartPixel.y - heroSizeOnSheet.h);
v4 heroTexNDC = v4_scale(heroRect, 1.0f/ndcFactor); Entity npcEnt = {V2(300.0f, 300.0f), V2(0.0f, 0.0f), hero->size,
direction_null, hero->tex, npcAnim};
state->entityList[state->freeEntityIndex++] = npcEnt;
/* Init renderer */ /* Init renderer */
Renderer *renderer = &state->renderer; Renderer *renderer = &state->renderer;
@ -70,16 +86,6 @@ void worldTraveller_gameInit(GameState *state)
shader_uniformSetMat4fv(renderer->shader, "projection", projection); shader_uniformSetMat4fv(renderer->shader, "projection", projection);
glCheckError(); glCheckError();
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using
// vertices v0, v1, v2, then v2, v1, v3 (note the order)
v4 vertices[] = {
// x y s t
{0.0f, 1.0f, heroTexNDC.x, heroTexNDC.y}, // Top left
{0.0f, 0.0f, heroTexNDC.x, heroTexNDC.w}, // Bottom left
{1.0f, 1.0f, heroTexNDC.z, heroTexNDC.y}, // Top right
{1.0f, 0.0f, heroTexNDC.z, heroTexNDC.w}, // Bottom right
};
/* Create buffers */ /* Create buffers */
glGenVertexArrays(1, &renderer->vao); glGenVertexArrays(1, &renderer->vao);
glGenBuffers(1, &renderer->vbo); glGenBuffers(1, &renderer->vbo);
@ -89,14 +95,9 @@ void worldTraveller_gameInit(GameState *state)
glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo); glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo);
glBindVertexArray(renderer->vao); glBindVertexArray(renderer->vao);
/* Configure VBO */
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glCheckError();
/* Configure VAO */ /* Configure VAO */
const GLuint numVertexElements = 4; const GLuint numVertexElements = 4;
const GLuint vertexSize = sizeof(v4); const GLuint vertexSize = sizeof(v4);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize, glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize,
(GLvoid *)0); (GLvoid *)0);
@ -122,6 +123,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
f (t) = (a/2)*t^2 + v*t + p, where p is a constant, old position f (t) = (a/2)*t^2 + v*t + p, where p is a constant, old position
*/ */
Entity *hero = &state->entityList[state->heroIndex];
v2 ddPos = V2(0, 0); v2 ddPos = V2(0, 0);
if (state->keys[GLFW_KEY_SPACE]) if (state->keys[GLFW_KEY_SPACE])
@ -131,12 +133,12 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
if (state->keys[GLFW_KEY_RIGHT]) if (state->keys[GLFW_KEY_RIGHT])
{ {
ddPos.x = 1.0f; ddPos.x = 1.0f;
state->heroLastDirection = direction_east; hero->direction = direction_east;
} }
if (state->keys[GLFW_KEY_LEFT]) if (state->keys[GLFW_KEY_LEFT])
{ {
ddPos.x = -1.0f; ddPos.x = -1.0f;
state->heroLastDirection = direction_west; hero->direction = direction_west;
} }
if (state->keys[GLFW_KEY_UP]) if (state->keys[GLFW_KEY_UP])
@ -164,7 +166,6 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
ddPos = v2_scale(ddPos, heroSpeed); ddPos = v2_scale(ddPos, heroSpeed);
// TODO(doyle): Counteracting force on player's acceleration is arbitrary // TODO(doyle): Counteracting force on player's acceleration is arbitrary
Entity *hero = &state->hero;
ddPos = v2_sub(ddPos, v2_scale(hero->dPos, 5.5f)); ddPos = v2_sub(ddPos, v2_scale(hero->dPos, 5.5f));
/* /*
@ -188,60 +189,38 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
parseInput(state, dt); parseInput(state, dt);
glCheckError(); glCheckError();
Entity *hero = &state->hero; // NOTE(doyle): Factor to normalise sprite sheet rect coords to -1, 1
Texture *heroSheet = hero->tex; Entity *hero = &state->entityList[state->heroIndex];
f32 ndcFactor = CAST(f32)heroSheet->width; f32 ndcFactor = 1.0f / CAST(f32) hero->tex->width;
v2 heroStartPixel = V2(746.0f, 1018.0f); // direction == east ASSERT(state->freeEntityIndex < ARRAY_COUNT(state->entityList));
for (i32 i = 0; i < state->freeEntityIndex; i++)
v2 heroSizeOnSheet = V2(58.0f, 98.0f);
v4 heroRect = V4(heroStartPixel.x,
heroStartPixel.y,
heroStartPixel.x + heroSizeOnSheet.w,
heroStartPixel.y - heroSizeOnSheet.h);
v4 heroTexNDC = v4_scale(heroRect, 1.0f / ndcFactor);
if (state->heroLastDirection == direction_east)
{ {
// NOTE(doyle): Flip the x coordinates to flip the tex Entity *entity = &state->entityList[i];
v4 copyNDC = heroTexNDC; SpriteAnim *anim = &entity->anim;
heroTexNDC.x = copyNDC.z;
heroTexNDC.z = copyNDC.x;
}
updateBufferObject(state->renderer.vbo, heroTexNDC); v4 currFrameRect = anim->rect[anim->currRectIndex];
renderer_entity(&state->renderer, hero, 0.0f, V3(0, 0, 0)); anim->currDuration -= dt;
if (anim->currDuration <= 0.0f)
Entity npcEnt = {V2(300.0f, 300.0f), V2(0.0f, 0.0f), hero->size, hero->tex};
v4 npcRects[2] = {
{944.0f, 918.0f, 1010.0f, 816.0f},
{944.0f, 812.0f, 1010.0f, 710.0f},
};
LOCAL_PERSIST f32 frameDurationInSec = 0.0f;
LOCAL_PERSIST i32 npcLastRect = 0;
frameDurationInSec -= dt;
v4 npcCurrRect = npcRects[npcLastRect];
if (frameDurationInSec <= 0.0f)
{
if (npcLastRect == 0)
{ {
npcCurrRect = npcRects[1]; anim->currRectIndex++;
npcLastRect = 1; anim->currRectIndex = anim->currRectIndex % anim->numRects;
currFrameRect = anim->rect[anim->currRectIndex];
anim->currDuration = anim->duration;
} }
else
{
npcCurrRect = npcRects[0];
npcLastRect = 0;
}
frameDurationInSec = 0.3f;
}
v4 npcTexNDC = v4_scale(npcCurrRect, 1.0f / ndcFactor); v4 texNDC = v4_scale(currFrameRect, ndcFactor);
updateBufferObject(state->renderer.vbo, npcTexNDC); if (entity->direction == direction_east)
renderer_entity(&state->renderer, &npcEnt, 0.0f, V3(0, 0, 0)); {
// NOTE(doyle): Flip the x coordinates to flip the tex
v4 tmpNDC = texNDC;
texNDC.x = tmpNDC.z;
texNDC.z = tmpNDC.x;
}
updateBufferObject(state->renderer.vbo, texNDC);
renderer_entity(&state->renderer, entity, 0.0f, V3(0, 0, 0));
}
// TODO(doyle): Clean up lines // TODO(doyle): Clean up lines
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }

View File

@ -22,5 +22,7 @@ typedef double f64;
#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) #define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
#define CAST(type) (type) #define CAST(type) (type)
#define ASSERT(expr) if(!(expr)) { *(int *)0 = 0; }
#endif #endif

View File

@ -4,12 +4,34 @@
#include <Dengine/Texture.h> #include <Dengine/Texture.h>
#include <Dengine/Math.h> #include <Dengine/Math.h>
enum Direction
{
direction_north,
direction_west,
direction_south,
direction_east,
direction_num,
direction_null,
};
typedef struct SpriteAnim
{
v4 *rect;
i32 numRects;
i32 currRectIndex;
f32 duration;
f32 currDuration;
} SpriteAnim;
typedef struct Entity typedef struct Entity
{ {
v2 pos; // Position v2 pos; // Position
v2 dPos; // Velocity v2 dPos; // Velocity
v2 size; v2 size;
enum Direction direction;
Texture *tex; Texture *tex;
SpriteAnim anim;
} Entity; } Entity;
#endif #endif

View File

@ -15,15 +15,6 @@ enum State
state_win state_win
}; };
enum Direction
{
direction_north,
direction_west,
direction_south,
direction_east,
direction_num,
};
typedef struct GameState typedef struct GameState
{ {
enum State state; enum State state;
@ -31,8 +22,11 @@ typedef struct GameState
i32 width, height; i32 width, height;
Renderer renderer; Renderer renderer;
Entity hero; i32 heroIndex;
enum Direction heroLastDirection;
// TODO(doyle): Make size of list dynamic
Entity entityList[256];
i32 freeEntityIndex;
} GameState; } GameState;
void worldTraveller_gameInit(GameState *state); void worldTraveller_gameInit(GameState *state);