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 <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)
{
// 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[] = {
// x y s t
{0.0f, 1.0f, texNDC.x, texNDC.y}, // Top left
@ -26,19 +32,28 @@ void worldTraveller_gameInit(GameState *state)
/* Initialise assets */
asset_loadTextureImage(
"data/textures/WorldTraveller/TerraSprite1024.png", texlist_hero);
glCheckError();
asset_loadShaderFiles("data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl", shaderlist_sprite);
glCheckError();
state->state = state_active;
state->heroLastDirection = direction_east;
state->state = state_active;
/* Init hero */
Entity *hero = &state->hero;
hero->tex = asset_getTexture(texlist_hero);
hero->size = V2(58.0f, 98.0f);
hero->pos = V2(0.0f, 0.0f);
SpriteAnim heroAnim = {NULL, 1, 0, 1.0f, 1.0f};
// TODO(doyle): Get rid of
heroAnim.rect = (v4 *)calloc(1, sizeof(v4));
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;
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);
}
f32 ndcFactor = sheetSize.w;
v2 heroStartPixel = V2(746.0f, 1018.0f);
v2 heroSizeOnSheet = V2(58.0f, 98.0f);
v4 heroRect = V4(heroStartPixel.x,
heroStartPixel.y,
heroStartPixel.x + heroSizeOnSheet.w,
heroStartPixel.y - heroSizeOnSheet.h);
/* Create a NPC */
SpriteAnim npcAnim = {NULL, 2, 0, 0.3f, 0.3f};
// TODO(doyle): Get rid of
npcAnim.rect = (v4 *)calloc(2, sizeof(v4));
npcAnim.rect[0] = V4(944.0f, 918.0f, 1010.0f, 816.0f);
npcAnim.rect[1] = V4(944.0f, 812.0f, 1010.0f, 710.0f);
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 */
Renderer *renderer = &state->renderer;
@ -70,16 +86,6 @@ void worldTraveller_gameInit(GameState *state)
shader_uniformSetMat4fv(renderer->shader, "projection", projection);
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 */
glGenVertexArrays(1, &renderer->vao);
glGenBuffers(1, &renderer->vbo);
@ -89,14 +95,9 @@ void worldTraveller_gameInit(GameState *state)
glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo);
glBindVertexArray(renderer->vao);
/* Configure VBO */
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glCheckError();
/* Configure VAO */
const GLuint numVertexElements = 4;
const GLuint vertexSize = sizeof(v4);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize,
(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
*/
Entity *hero = &state->entityList[state->heroIndex];
v2 ddPos = V2(0, 0);
if (state->keys[GLFW_KEY_SPACE])
@ -131,12 +133,12 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
if (state->keys[GLFW_KEY_RIGHT])
{
ddPos.x = 1.0f;
state->heroLastDirection = direction_east;
hero->direction = direction_east;
}
if (state->keys[GLFW_KEY_LEFT])
{
ddPos.x = -1.0f;
state->heroLastDirection = direction_west;
hero->direction = direction_west;
}
if (state->keys[GLFW_KEY_UP])
@ -164,7 +166,6 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
ddPos = v2_scale(ddPos, heroSpeed);
// TODO(doyle): Counteracting force on player's acceleration is arbitrary
Entity *hero = &state->hero;
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);
glCheckError();
Entity *hero = &state->hero;
Texture *heroSheet = hero->tex;
f32 ndcFactor = CAST(f32)heroSheet->width;
// NOTE(doyle): Factor to normalise sprite sheet rect coords to -1, 1
Entity *hero = &state->entityList[state->heroIndex];
f32 ndcFactor = 1.0f / CAST(f32) hero->tex->width;
v2 heroStartPixel = V2(746.0f, 1018.0f); // direction == east
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)
ASSERT(state->freeEntityIndex < ARRAY_COUNT(state->entityList));
for (i32 i = 0; i < state->freeEntityIndex; i++)
{
// NOTE(doyle): Flip the x coordinates to flip the tex
v4 copyNDC = heroTexNDC;
heroTexNDC.x = copyNDC.z;
heroTexNDC.z = copyNDC.x;
}
Entity *entity = &state->entityList[i];
SpriteAnim *anim = &entity->anim;
updateBufferObject(state->renderer.vbo, heroTexNDC);
renderer_entity(&state->renderer, hero, 0.0f, V3(0, 0, 0));
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)
v4 currFrameRect = anim->rect[anim->currRectIndex];
anim->currDuration -= dt;
if (anim->currDuration <= 0.0f)
{
npcCurrRect = npcRects[1];
npcLastRect = 1;
anim->currRectIndex++;
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);
updateBufferObject(state->renderer.vbo, npcTexNDC);
renderer_entity(&state->renderer, &npcEnt, 0.0f, V3(0, 0, 0));
v4 texNDC = v4_scale(currFrameRect, ndcFactor);
if (entity->direction == direction_east)
{
// 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
// 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 CAST(type) (type)
#define ASSERT(expr) if(!(expr)) { *(int *)0 = 0; }
#endif

View File

@ -4,12 +4,34 @@
#include <Dengine/Texture.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
{
v2 pos; // Position
v2 dPos; // Velocity
v2 size;
enum Direction direction;
Texture *tex;
SpriteAnim anim;
} Entity;
#endif

View File

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