Remove int v2 type, change V2i to cast i32 to f32

Mixing and matching V2 int and float types in the code creates too much
necessary work when an integer implementation has to interact with float
implementation. Let V2i create the cast for us and use floats for all
vector operations since they are mostly mathematic.
This commit is contained in:
Doyle Thai 2016-07-17 23:45:59 +10:00
parent 2745a8e25a
commit 7971b10b74
7 changed files with 44 additions and 59 deletions

View File

@ -156,7 +156,7 @@ const i32 asset_loadShaderFiles(AssetManager *assetManager,
/* Individual glyph bitmap generated from STB used for creating a font sheet */
typedef struct GlyphBitmap
{
v2i dimensions;
v2 dimensions;
u32 *pixels;
i32 codepoint;
} GlyphBitmap;
@ -173,11 +173,11 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
/* Initialise Assetmanager Font */
Font *font = &assetManager->font;
font->codepointRange = V2i(32, 127);
v2i codepointRange = font->codepointRange;
const i32 numGlyphs = codepointRange.y - codepointRange.x;
v2 codepointRange = font->codepointRange;
const i32 numGlyphs = CAST(i32)(codepointRange.y - codepointRange.x);
GlyphBitmap *glyphBitmaps = PLATFORM_MEM_ALLOC(numGlyphs, GlyphBitmap);
v2i largestGlyphDimension = V2i(0, 0);
v2 largestGlyphDimension = V2(0, 0);
const f32 targetFontHeight = 15.0f;
f32 scaleY = stbtt_ScaleForPixelHeight(&fontInfo, targetFontHeight);
@ -195,8 +195,8 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
/* Use STB_TrueType to generate a series of bitmap characters */
i32 glyphIndex = 0;
for (i32 codepoint = codepointRange.x; codepoint < codepointRange.y;
codepoint++)
for (i32 codepoint = CAST(i32) codepointRange.x;
codepoint < CAST(i32) codepointRange.y; codepoint++)
{
// NOTE(doyle): ScaleX if not specified, is then calculated based on the
// ScaleY component
@ -241,10 +241,10 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
glyphBitmaps[glyphIndex].codepoint = codepoint;
glyphBitmaps[glyphIndex++].pixels = colorBitmap;
if (height > largestGlyphDimension.h)
largestGlyphDimension.h = height;
if (width > largestGlyphDimension.w)
largestGlyphDimension.w = width;
if (height > CAST(f32)largestGlyphDimension.h)
largestGlyphDimension.h = CAST(f32)height;
if (width > CAST(f32)largestGlyphDimension.w)
largestGlyphDimension.w = CAST(f32)width;
#ifdef DENGINE_DEBUG
if ((largestGlyphDimension.h - CAST(i32)targetFontHeight) >= 50)
@ -267,10 +267,10 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
* all pixels for the glyphs, then move onto the next set of glyphs.
*/
font->maxSize = largestGlyphDimension;
i32 glyphsPerRow = (MAX_TEXTURE_SIZE / font->maxSize.w);
i32 glyphsPerRow = (MAX_TEXTURE_SIZE / CAST(i32)font->maxSize.w);
#ifdef DENGINE_DEBUG
i32 glyphsPerCol = MAX_TEXTURE_SIZE / font->maxSize.h;
i32 glyphsPerCol = MAX_TEXTURE_SIZE / CAST(i32)font->maxSize.h;
if ((glyphsPerRow * glyphsPerCol) <= numGlyphs)
{
printf(
@ -330,14 +330,14 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
}
/* Copy over exactly one row of pixels */
i32 numPixelsToPad = font->maxSize.w;
i32 numPixelsToPad = CAST(i32)font->maxSize.w;
if (verticalPixelsBlitted < activeGlyph.dimensions.h)
{
const i32 srcPitch =
activeGlyph.dimensions.w * verticalPixelsBlitted;
CAST(i32) activeGlyph.dimensions.w * verticalPixelsBlitted;
const u32 *src = activeGlyph.pixels + srcPitch;
const i32 numPixelsToCopy = activeGlyph.dimensions.w;
const i32 numPixelsToCopy = CAST(i32)activeGlyph.dimensions.w;
for (i32 count = 0; count < numPixelsToCopy; count++)
*destRow++ = *src++;
@ -348,7 +348,8 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
* (NULL/mixes up rows), instead just advance the final bitmap
* pointer by the remaining distance
*/
numPixelsToPad = font->maxSize.w - activeGlyph.dimensions.w;
numPixelsToPad =
CAST(i32)(font->maxSize.w - activeGlyph.dimensions.w);
}
destRow += numPixelsToPad;
}
@ -388,8 +389,9 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
for (i32 i = 0; i < numGlyphs; i++)
{
i32 glyphBitmapSizeInBytes = glyphBitmaps[i].dimensions.w *
glyphBitmaps[i].dimensions.h * sizeof(u32);
i32 glyphBitmapSizeInBytes = CAST(i32) glyphBitmaps[i].dimensions.w *
CAST(i32) glyphBitmaps[i].dimensions.h *
sizeof(u32);
PLATFORM_MEM_FREE(glyphBitmaps[i].pixels, glyphBitmapSizeInBytes);
}

View File

@ -184,7 +184,7 @@ void renderer_string(Renderer *const renderer, v4 cameraBounds,
// NOTE(doyle): Atlas packs fonts tightly, so offset the codepoint
// to its actual atlas index, i.e. we skip the first 31 glyphs
i32 codepoint = string[i];
i32 relativeIndex = codepoint - font->codepointRange.x;
i32 relativeIndex = CAST(i32)(codepoint - font->codepointRange.x);
CharMetrics charMetric = font->charMetrics[relativeIndex];
pos.y = baseline - (scale * charMetric.offset.y);

View File

@ -76,11 +76,11 @@ INTERNAL void addAnim(Entity *entity, enum EntityAnimId animId, v4 *rects,
entity->anim[animId] = result;
}
INTERNAL void rendererInit(GameState *state, v2i windowSize)
INTERNAL void rendererInit(GameState *state, v2 windowSize)
{
AssetManager *assetManager = &state->assetManager;
Renderer *renderer = &state->renderer;
renderer->size = V2(CAST(f32) windowSize.x, CAST(f32) windowSize.y);
renderer->size = windowSize;
// NOTE(doyle): Value to map a screen coordinate to NDC coordinate
renderer->vertexNdcFactor =
V2(1.0f / renderer->size.w, 1.0f / renderer->size.h);
@ -116,7 +116,7 @@ INTERNAL void rendererInit(GameState *state, v2i windowSize)
}
void worldTraveller_gameInit(GameState *state, v2i windowSize)
void worldTraveller_gameInit(GameState *state, v2 windowSize)
{
AssetManager *assetManager = &state->assetManager;
/* Initialise assets */
@ -171,8 +171,8 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
/* Init world */
const i32 targetWorldWidth = 500 * METERS_TO_PIXEL;
const i32 targetWorldHeight = 15 * METERS_TO_PIXEL;
v2i worldDimensionInTiles = V2i(targetWorldWidth / state->tileSize,
targetWorldHeight / state->tileSize);
v2 worldDimensionInTiles = V2i(targetWorldWidth / state->tileSize,
targetWorldHeight / state->tileSize);
for (i32 i = 0; i < ARRAY_COUNT(state->world); i++)
{
@ -181,10 +181,8 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
world->entities = PLATFORM_MEM_ALLOC(world->maxEntities, Entity);
world->texType = texlist_terrain;
v2 worldDimensionInTilesf = V2(CAST(f32) worldDimensionInTiles.x,
CAST(f32) worldDimensionInTiles.y);
world->bounds =
math_getRect(V2(0, 0), v2_scale(worldDimensionInTilesf,
math_getRect(V2(0, 0), v2_scale(worldDimensionInTiles,
CAST(f32) state->tileSize));
TexAtlas *const atlas =
@ -267,7 +265,7 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
duration);
/* Add hero battle tackle animation */
duration = 0.09f;
duration = 0.15f;
numRects = 3;
v4 *heroTackleRects = PLATFORM_MEM_ALLOC(numRects, v4);
heroTackleRects[0] = heroAtlas->texRect[herorects_castA];
@ -678,7 +676,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
if (debugString)
{
v2 strPos = v2_add(entity->pos, entity->size);
i32 indexOfLowerAInMetrics = 'a' - font->codepointRange.x;
i32 indexOfLowerAInMetrics = 'a' - CAST(i32)font->codepointRange.x;
strPos.y += font->charMetrics[indexOfLowerAInMetrics].offset.y;
renderer_string(&state->renderer, cameraBounds, font, debugString,

View File

@ -37,10 +37,11 @@ int main()
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
v2i windowSize = V2i(1600, 900);
i32 windowWidth = 1600;
i32 windowHeight = 900;
GLFWwindow *window =
glfwCreateWindow(windowSize.x, windowSize.y, "Dengine", NULL, NULL);
glfwCreateWindow(windowWidth, windowHeight, "Dengine", NULL, NULL);
if (!window)
{
@ -62,9 +63,9 @@ int main()
// regardless of success. Catch it once by calling glGetError
glGetError();
v2i frameBufferSize = V2i(0, 0);
glfwGetFramebufferSize(window, &frameBufferSize.w, &frameBufferSize.h);
glViewport(0, 0, frameBufferSize.x, frameBufferSize.y);
i32 frameBufferWidth, frameBufferHeight;
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
@ -82,7 +83,8 @@ int main()
#endif
GameState worldTraveller = {0};
worldTraveller_gameInit(&worldTraveller, frameBufferSize);
worldTraveller_gameInit(&worldTraveller,
V2i(frameBufferWidth, frameBufferHeight));
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));

View File

@ -60,8 +60,8 @@ typedef struct CharMetrics
i32 leftSideBearing;
// TODO(doyle): Utilise kerning
i32 *kerning;
v2i offset;
v2i trueSize;
v2 offset;
v2 trueSize;
} CharMetrics;
typedef struct Font
@ -72,8 +72,8 @@ typedef struct Font
FontMetrics metrics;
CharMetrics *charMetrics;
v2i codepointRange;
v2i maxSize;
v2 codepointRange;
v2 maxSize;
} Font;
#endif

View File

@ -11,13 +11,6 @@
#define SQRT(x) (sqrtf(x))
/* VECTORS */
typedef union v2i
{
struct { i32 x, y; };
struct { i32 w, h; };
i32 e[2];
} v2i;
typedef union v2
{
struct { f32 x, y; };
@ -39,9 +32,9 @@ typedef union v4
f32 e[4];
} v4;
INTERNAL inline v2i V2i(const i32 x, const i32 y)
INTERNAL inline v2 V2i(const i32 x, const i32 y)
{
v2i result = {x, y};
v2 result = {CAST(f32)x, CAST(f32)y};
return result;
}
INTERNAL inline v2 V2(const f32 x, const f32 y)
@ -127,16 +120,6 @@ DEFINE_VECTOR_FLOAT_MATH(2);
DEFINE_VECTOR_FLOAT_MATH(3);
DEFINE_VECTOR_FLOAT_MATH(4);
#define DEFINE_VECTOR_INT_MATH(num) \
INTERNAL inline v##num##i v##num##i_add(const v##num##i a, const v##num##i b) \
{ \
v##num##i result; \
for (i32 i = 0; i < ##num; i++) { result.e[i] = a.e[i] + b.e[i]; } \
return result; \
} \
DEFINE_VECTOR_INT_MATH(2);
INTERNAL inline f32 v2_magnitude(const v2 a, const v2 b)
{
f32 x = b.x - a.x;

View File

@ -40,6 +40,6 @@ typedef struct GameState
AssetManager assetManager;
} GameState;
void worldTraveller_gameInit(GameState *state, v2i windowSize);
void worldTraveller_gameInit(GameState *state, v2 windowSize);
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt);
#endif