Merge triangle rendering code to polygon code
This commit is contained in:
parent
68c0560b28
commit
a4e5025dd4
100
src/Asteroid.c
100
src/Asteroid.c
@ -164,82 +164,6 @@ INTERNAL b32 getKeyStatus(KeyState *key, enum ReadKeyType readType,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
typedef struct Basis
|
||||
{
|
||||
v2 basis;
|
||||
v2 pivotPoint;
|
||||
} Basis;
|
||||
|
||||
enum RectBaseline
|
||||
{
|
||||
rectbaseline_top,
|
||||
rectbaseline_topLeft,
|
||||
rectbaseline_topRight,
|
||||
rectbaseline_bottom,
|
||||
rectbaseline_bottomRight,
|
||||
rectbaseline_bottomLeft,
|
||||
rectbaseline_left,
|
||||
rectbaseline_right,
|
||||
rectbaseline_center,
|
||||
rectbaseline_count,
|
||||
|
||||
};
|
||||
|
||||
Basis getBasis(Entity *entity, enum RectBaseline baseline)
|
||||
{
|
||||
ASSERT(baseline < rectbaseline_count);
|
||||
|
||||
v2 basis = v2_sub(entity->pos, entity->offset);
|
||||
v2 pivotPoint = v2_scale(entity->size, 0.5f);
|
||||
v2 size = entity->size;
|
||||
switch (baseline)
|
||||
{
|
||||
case rectbaseline_top:
|
||||
basis.y += (size.h);
|
||||
basis.x += (size.w * 0.5f);
|
||||
break;
|
||||
case rectbaseline_topLeft:
|
||||
basis.y += (size.h);
|
||||
break;
|
||||
case rectbaseline_topRight:
|
||||
basis.y += (size.h);
|
||||
basis.x += (size.w);
|
||||
break;
|
||||
case rectbaseline_bottom:
|
||||
basis.x += (size.w * 0.5f);
|
||||
break;
|
||||
case rectbaseline_bottomRight:
|
||||
basis.x += (size.w);
|
||||
break;
|
||||
case rectbaseline_left:
|
||||
basis.y += (size.h * 0.5f);
|
||||
break;
|
||||
case rectbaseline_right:
|
||||
basis.x += (size.w);
|
||||
basis.y += (size.h * 0.5f);
|
||||
break;
|
||||
|
||||
case rectbaseline_bottomLeft:
|
||||
break;
|
||||
default:
|
||||
DEBUG_LOG(
|
||||
"getPosRelativeToRect() warning: baseline enum not recognised");
|
||||
break;
|
||||
}
|
||||
|
||||
Basis result = {0};
|
||||
result.basis = basis;
|
||||
result.pivotPoint = pivotPoint;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Basis getDefaultBasis(Entity *entity)
|
||||
{
|
||||
Basis result = getBasis(entity, rectbaseline_bottomLeft);
|
||||
return result;
|
||||
}
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
v2 *createAsteroidVertexList(MemoryArena_ *arena, i32 iterations,
|
||||
@ -323,7 +247,6 @@ void moveEntity(GameState *state, Entity *entity, v2 ddP, f32 dt, f32 ddPSpeed)
|
||||
Entity *checkEntity = &state->entityList[i];
|
||||
if (checkEntity->id == entity->id) continue;
|
||||
|
||||
|
||||
}
|
||||
|
||||
b32 moveValid = TRUE;
|
||||
@ -359,10 +282,25 @@ void asteroid_gameUpdateAndRender(GameState *state, Memory *memory,
|
||||
ship->size = V2(25.0f, 50.0f);
|
||||
ship->hitbox = ship->size;
|
||||
ship->offset = v2_scale(ship->size, 0.5f);
|
||||
|
||||
ship->numVertexPoints = 3;
|
||||
ship->vertexPoints = memory_pushBytes(
|
||||
&state->persistentArena, sizeof(v2) * ship->numVertexPoints);
|
||||
|
||||
Basis shipBasis = getDefaultBasis(ship);
|
||||
v2 triangleTopPoint = V2(shipBasis.pos.x + (ship->size.w * 0.5f),
|
||||
shipBasis.pos.y + ship->size.h);
|
||||
v2 triangleRightSide =
|
||||
V2(shipBasis.pos.x + ship->size.w, shipBasis.pos.y);
|
||||
|
||||
ship->vertexPoints[0] = shipBasis.pos;
|
||||
ship->vertexPoints[1] = triangleRightSide;
|
||||
ship->vertexPoints[2] = triangleTopPoint;
|
||||
|
||||
ship->scale = 1;
|
||||
ship->type = entitytype_ship;
|
||||
ship->direction = direction_null;
|
||||
ship->renderMode = rendermode_triangle;
|
||||
ship->renderMode = rendermode_polygon;
|
||||
ship->tex = NULL;
|
||||
ship->collides = TRUE;
|
||||
|
||||
@ -580,7 +518,7 @@ void asteroid_gameUpdateAndRender(GameState *state, Memory *memory,
|
||||
flags);
|
||||
|
||||
Basis entityBasis = getDefaultBasis(entity);
|
||||
renderer_rect(&state->renderer, state->camera, entityBasis.basis,
|
||||
renderer_rect(&state->renderer, state->camera, entityBasis.pos,
|
||||
V2(4, 4), entityBasis.pivotPoint,
|
||||
DEGREES_TO_RADIANS(entity->rotation), NULL,
|
||||
V4(1.0f, 0, 0, 1.0f), flags);
|
||||
@ -592,8 +530,8 @@ void asteroid_gameUpdateAndRender(GameState *state, Memory *memory,
|
||||
triangle.points[1] = V2(200, 100);
|
||||
triangle.points[2] = V2(100, 300);
|
||||
|
||||
LOCAL_PERSIST Radians rotation = 0.0f;
|
||||
rotation += DEGREES_TO_RADIANS(((60.0f) * dt));
|
||||
LOCAL_PERSIST Degrees rotation = 0.0f;
|
||||
rotation += (60.0f) * dt;
|
||||
|
||||
RenderFlags flags = renderflag_wireframe | renderflag_no_texture;
|
||||
renderer_triangle(&state->renderer, state->camera, triangle, V2(0, 0),
|
||||
|
50
src/Entity.c
50
src/Entity.c
@ -86,3 +86,53 @@ void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
|
||||
|
||||
DEBUG_LOG("No more free entity animation slots");
|
||||
}
|
||||
|
||||
Basis getBasis(Entity *entity, enum RectBaseline baseline)
|
||||
{
|
||||
ASSERT(baseline < rectbaseline_count);
|
||||
|
||||
v2 basis = v2_sub(entity->pos, entity->offset);
|
||||
v2 pivotPoint = v2_scale(entity->size, 0.5f);
|
||||
v2 size = entity->size;
|
||||
switch (baseline)
|
||||
{
|
||||
case rectbaseline_top:
|
||||
basis.y += (size.h);
|
||||
basis.x += (size.w * 0.5f);
|
||||
break;
|
||||
case rectbaseline_topLeft:
|
||||
basis.y += (size.h);
|
||||
break;
|
||||
case rectbaseline_topRight:
|
||||
basis.y += (size.h);
|
||||
basis.x += (size.w);
|
||||
break;
|
||||
case rectbaseline_bottom:
|
||||
basis.x += (size.w * 0.5f);
|
||||
break;
|
||||
case rectbaseline_bottomRight:
|
||||
basis.x += (size.w);
|
||||
break;
|
||||
case rectbaseline_left:
|
||||
basis.y += (size.h * 0.5f);
|
||||
break;
|
||||
case rectbaseline_right:
|
||||
basis.x += (size.w);
|
||||
basis.y += (size.h * 0.5f);
|
||||
break;
|
||||
|
||||
case rectbaseline_bottomLeft:
|
||||
break;
|
||||
default:
|
||||
DEBUG_LOG(
|
||||
"getPosRelativeToRect() warning: baseline enum not recognised");
|
||||
break;
|
||||
}
|
||||
|
||||
Basis result = {0};
|
||||
result.pos = basis;
|
||||
result.pivotPoint = pivotPoint;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -427,32 +427,33 @@ void renderer_polygon(Renderer *const renderer, Rect camera,
|
||||
Radians rotate, RenderTex *renderTex, v4 color,
|
||||
RenderFlags flags)
|
||||
{
|
||||
ASSERT(numPoints > 3);
|
||||
ASSERT(numPoints >= 3);
|
||||
|
||||
for (i32 i = 0; i < numPoints; i++)
|
||||
polygonPoints[i] = v2_sub(polygonPoints[i], camera.min);
|
||||
|
||||
// TODO(doyle): Do something with render texture
|
||||
RenderTex emptyRenderTex = {0};
|
||||
if (!renderTex) renderTex = &emptyRenderTex;
|
||||
|
||||
i32 numTrisInTriangulation = numPoints - 2;
|
||||
v2 triangulationBaseP = polygonPoints[0];
|
||||
i32 triangulationIndex = 0;
|
||||
|
||||
v2 triangulationBaseP = polygonPoints[0];
|
||||
Vertex triangulationBaseVertex = {0};
|
||||
triangulationBaseVertex.pos = triangulationBaseP;
|
||||
|
||||
triangulationBaseVertex.pos = triangulationBaseP;
|
||||
|
||||
i32 numTrisInTriangulation = numPoints - 2;
|
||||
i32 triangulationIndex = 0;
|
||||
beginVertexBatch(renderer);
|
||||
for (i32 i = 1; triangulationIndex < numTrisInTriangulation; i++)
|
||||
{
|
||||
ASSERT((i + 1) <= numPoints);
|
||||
ASSERT((i + 1) < numPoints);
|
||||
|
||||
RenderTriangle_ tri = {0};
|
||||
tri.vertex[0].pos = triangulationBaseP;
|
||||
tri.vertex[1].pos = polygonPoints[i + 1];
|
||||
tri.vertex[2].pos = polygonPoints[i];
|
||||
tri.vertex[1].pos = polygonPoints[i];
|
||||
tri.vertex[2].pos = polygonPoints[i + 1];
|
||||
|
||||
applyRotationToVertexes(triangulationBaseP, pivotPoint, rotate,
|
||||
tri.vertex, 3);
|
||||
addVertexToRenderGroup_(renderer, renderTex->tex, color, tri.vertex,
|
||||
ARRAY_COUNT(tri.vertex), rendermode_polygon,
|
||||
flags);
|
||||
@ -462,27 +463,13 @@ void renderer_polygon(Renderer *const renderer, Rect camera,
|
||||
}
|
||||
|
||||
void renderer_triangle(Renderer *const renderer, Rect camera,
|
||||
TrianglePoints triangle, v2 pivotPoint, Radians rotate,
|
||||
TrianglePoints triangle, v2 pivotPoint, Degrees rotate,
|
||||
RenderTex *renderTex, v4 color, RenderFlags flags)
|
||||
{
|
||||
TrianglePoints triangleInCamSpace = {0};
|
||||
ASSERT(ARRAY_COUNT(triangle.points) ==
|
||||
ARRAY_COUNT(triangleInCamSpace.points));
|
||||
|
||||
for (i32 i = 0; i < ARRAY_COUNT(triangleInCamSpace.points); i++)
|
||||
triangleInCamSpace.points[i] = v2_sub(triangle.points[i], camera.min);
|
||||
|
||||
RenderTex emptyRenderTex = {0};
|
||||
if (!renderTex) renderTex = &emptyRenderTex;
|
||||
|
||||
RenderTriangle_ renderTriangle = createRenderTriangle(
|
||||
renderer, triangleInCamSpace, pivotPoint, rotate, *renderTex);
|
||||
|
||||
beginVertexBatch(renderer);
|
||||
addVertexToRenderGroup_(
|
||||
renderer, renderTex->tex, color, renderTriangle.vertex,
|
||||
ARRAY_COUNT(renderTriangle.vertex), rendermode_triangle, flags);
|
||||
endVertexBatch(renderer);
|
||||
Radians totalRotation = DEGREES_TO_RADIANS(rotate);
|
||||
renderer_polygon(renderer, camera, triangle.points,
|
||||
ARRAY_COUNT(triangle.points), pivotPoint, totalRotation,
|
||||
renderTex, color, flags);
|
||||
}
|
||||
|
||||
void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
|
||||
@ -588,25 +575,22 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
|
||||
}
|
||||
else if (entity->renderMode == rendermode_triangle)
|
||||
{
|
||||
TrianglePoints triangle = {0};
|
||||
|
||||
v2 entityPWithOffset = v2_sub(entity->pos, entity->offset);
|
||||
v2 triangleTopPoint = V2(entityPWithOffset.x + (entity->size.w * 0.5f),
|
||||
entityPWithOffset.y + entity->size.h);
|
||||
|
||||
Basis entityBasis = getDefaultBasis(entity);
|
||||
v2 triangleTopPoint = V2(entityBasis.pos.x + (entity->size.w * 0.5f),
|
||||
entityBasis.pos.y + entity->size.h);
|
||||
v2 triangleRightSide =
|
||||
V2(entityPWithOffset.x + entity->size.w, entityPWithOffset.y);
|
||||
V2(entityBasis.pos.x + entity->size.w, entityBasis.pos.y);
|
||||
|
||||
triangle.points[0] = entityPWithOffset;
|
||||
triangle.points[1] = triangleRightSide;
|
||||
triangle.points[2] = triangleTopPoint;
|
||||
v2 entityPolygonPoints[] = {entityBasis.pos, triangleRightSide,
|
||||
triangleTopPoint};
|
||||
|
||||
renderer_triangle(renderer, camera, triangle, pivotPoint, totalRotation,
|
||||
&renderTex, color, flags);
|
||||
renderer_polygon(renderer, camera, entityPolygonPoints,
|
||||
ARRAY_COUNT(entityPolygonPoints), pivotPoint,
|
||||
totalRotation, &renderTex, color, flags);
|
||||
}
|
||||
else if (entity->renderMode == rendermode_polygon)
|
||||
{
|
||||
ASSERT(entity->numVertexPoints > 3);
|
||||
ASSERT(entity->numVertexPoints >= 3);
|
||||
ASSERT(entity->vertexPoints);
|
||||
|
||||
v2 *offsetVertexPoints = memory_pushBytes(
|
||||
|
@ -22,6 +22,27 @@ enum Direction
|
||||
direction_num,
|
||||
};
|
||||
|
||||
typedef struct Basis
|
||||
{
|
||||
v2 pos;
|
||||
v2 pivotPoint;
|
||||
} Basis;
|
||||
|
||||
enum RectBaseline
|
||||
{
|
||||
rectbaseline_top,
|
||||
rectbaseline_topLeft,
|
||||
rectbaseline_topRight,
|
||||
rectbaseline_bottom,
|
||||
rectbaseline_bottomRight,
|
||||
rectbaseline_bottomLeft,
|
||||
rectbaseline_left,
|
||||
rectbaseline_right,
|
||||
rectbaseline_center,
|
||||
rectbaseline_count,
|
||||
|
||||
};
|
||||
|
||||
enum EntityType
|
||||
{
|
||||
entitytype_invalid,
|
||||
@ -54,8 +75,8 @@ typedef struct Entity
|
||||
v2 offset;
|
||||
|
||||
enum RenderMode renderMode;
|
||||
v2 *vertexPoints;
|
||||
i32 numVertexPoints;
|
||||
v2 *vertexPoints;
|
||||
|
||||
f32 scale;
|
||||
Degrees rotation;
|
||||
@ -84,4 +105,18 @@ typedef struct Entity
|
||||
i32 numAudioRenderers;
|
||||
} Entity;
|
||||
|
||||
SubTexture entity_getActiveSubTexture(Entity *const entity);
|
||||
void entity_setActiveAnim(Entity *const entity, const char *const animName);
|
||||
void entity_updateAnim(Entity *const entity, const f32 dt);
|
||||
void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
|
||||
const char *const animName);
|
||||
|
||||
Basis getBasis(Entity *entity, enum RectBaseline baseline);
|
||||
inline Basis getDefaultBasis(Entity *entity)
|
||||
{
|
||||
Basis result = getBasis(entity, rectbaseline_bottomLeft);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user