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:
parent
2745a8e25a
commit
7971b10b74
@ -156,7 +156,7 @@ const i32 asset_loadShaderFiles(AssetManager *assetManager,
|
|||||||
/* Individual glyph bitmap generated from STB used for creating a font sheet */
|
/* Individual glyph bitmap generated from STB used for creating a font sheet */
|
||||||
typedef struct GlyphBitmap
|
typedef struct GlyphBitmap
|
||||||
{
|
{
|
||||||
v2i dimensions;
|
v2 dimensions;
|
||||||
u32 *pixels;
|
u32 *pixels;
|
||||||
i32 codepoint;
|
i32 codepoint;
|
||||||
} GlyphBitmap;
|
} GlyphBitmap;
|
||||||
@ -173,11 +173,11 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
|
|||||||
/* Initialise Assetmanager Font */
|
/* Initialise Assetmanager Font */
|
||||||
Font *font = &assetManager->font;
|
Font *font = &assetManager->font;
|
||||||
font->codepointRange = V2i(32, 127);
|
font->codepointRange = V2i(32, 127);
|
||||||
v2i codepointRange = font->codepointRange;
|
v2 codepointRange = font->codepointRange;
|
||||||
const i32 numGlyphs = codepointRange.y - codepointRange.x;
|
const i32 numGlyphs = CAST(i32)(codepointRange.y - codepointRange.x);
|
||||||
|
|
||||||
GlyphBitmap *glyphBitmaps = PLATFORM_MEM_ALLOC(numGlyphs, GlyphBitmap);
|
GlyphBitmap *glyphBitmaps = PLATFORM_MEM_ALLOC(numGlyphs, GlyphBitmap);
|
||||||
v2i largestGlyphDimension = V2i(0, 0);
|
v2 largestGlyphDimension = V2(0, 0);
|
||||||
|
|
||||||
const f32 targetFontHeight = 15.0f;
|
const f32 targetFontHeight = 15.0f;
|
||||||
f32 scaleY = stbtt_ScaleForPixelHeight(&fontInfo, targetFontHeight);
|
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 */
|
/* Use STB_TrueType to generate a series of bitmap characters */
|
||||||
i32 glyphIndex = 0;
|
i32 glyphIndex = 0;
|
||||||
for (i32 codepoint = codepointRange.x; codepoint < codepointRange.y;
|
for (i32 codepoint = CAST(i32) codepointRange.x;
|
||||||
codepoint++)
|
codepoint < CAST(i32) codepointRange.y; codepoint++)
|
||||||
{
|
{
|
||||||
// NOTE(doyle): ScaleX if not specified, is then calculated based on the
|
// NOTE(doyle): ScaleX if not specified, is then calculated based on the
|
||||||
// ScaleY component
|
// ScaleY component
|
||||||
@ -241,10 +241,10 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
|
|||||||
glyphBitmaps[glyphIndex].codepoint = codepoint;
|
glyphBitmaps[glyphIndex].codepoint = codepoint;
|
||||||
glyphBitmaps[glyphIndex++].pixels = colorBitmap;
|
glyphBitmaps[glyphIndex++].pixels = colorBitmap;
|
||||||
|
|
||||||
if (height > largestGlyphDimension.h)
|
if (height > CAST(f32)largestGlyphDimension.h)
|
||||||
largestGlyphDimension.h = height;
|
largestGlyphDimension.h = CAST(f32)height;
|
||||||
if (width > largestGlyphDimension.w)
|
if (width > CAST(f32)largestGlyphDimension.w)
|
||||||
largestGlyphDimension.w = width;
|
largestGlyphDimension.w = CAST(f32)width;
|
||||||
|
|
||||||
#ifdef DENGINE_DEBUG
|
#ifdef DENGINE_DEBUG
|
||||||
if ((largestGlyphDimension.h - CAST(i32)targetFontHeight) >= 50)
|
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.
|
* all pixels for the glyphs, then move onto the next set of glyphs.
|
||||||
*/
|
*/
|
||||||
font->maxSize = largestGlyphDimension;
|
font->maxSize = largestGlyphDimension;
|
||||||
i32 glyphsPerRow = (MAX_TEXTURE_SIZE / font->maxSize.w);
|
i32 glyphsPerRow = (MAX_TEXTURE_SIZE / CAST(i32)font->maxSize.w);
|
||||||
|
|
||||||
#ifdef DENGINE_DEBUG
|
#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)
|
if ((glyphsPerRow * glyphsPerCol) <= numGlyphs)
|
||||||
{
|
{
|
||||||
printf(
|
printf(
|
||||||
@ -330,14 +330,14 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Copy over exactly one row of pixels */
|
/* Copy over exactly one row of pixels */
|
||||||
i32 numPixelsToPad = font->maxSize.w;
|
i32 numPixelsToPad = CAST(i32)font->maxSize.w;
|
||||||
if (verticalPixelsBlitted < activeGlyph.dimensions.h)
|
if (verticalPixelsBlitted < activeGlyph.dimensions.h)
|
||||||
{
|
{
|
||||||
const i32 srcPitch =
|
const i32 srcPitch =
|
||||||
activeGlyph.dimensions.w * verticalPixelsBlitted;
|
CAST(i32) activeGlyph.dimensions.w * verticalPixelsBlitted;
|
||||||
const u32 *src = activeGlyph.pixels + srcPitch;
|
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++)
|
for (i32 count = 0; count < numPixelsToCopy; count++)
|
||||||
*destRow++ = *src++;
|
*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
|
* (NULL/mixes up rows), instead just advance the final bitmap
|
||||||
* pointer by the remaining distance
|
* pointer by the remaining distance
|
||||||
*/
|
*/
|
||||||
numPixelsToPad = font->maxSize.w - activeGlyph.dimensions.w;
|
numPixelsToPad =
|
||||||
|
CAST(i32)(font->maxSize.w - activeGlyph.dimensions.w);
|
||||||
}
|
}
|
||||||
destRow += numPixelsToPad;
|
destRow += numPixelsToPad;
|
||||||
}
|
}
|
||||||
@ -388,8 +389,9 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
|
|||||||
|
|
||||||
for (i32 i = 0; i < numGlyphs; i++)
|
for (i32 i = 0; i < numGlyphs; i++)
|
||||||
{
|
{
|
||||||
i32 glyphBitmapSizeInBytes = glyphBitmaps[i].dimensions.w *
|
i32 glyphBitmapSizeInBytes = CAST(i32) glyphBitmaps[i].dimensions.w *
|
||||||
glyphBitmaps[i].dimensions.h * sizeof(u32);
|
CAST(i32) glyphBitmaps[i].dimensions.h *
|
||||||
|
sizeof(u32);
|
||||||
PLATFORM_MEM_FREE(glyphBitmaps[i].pixels, glyphBitmapSizeInBytes);
|
PLATFORM_MEM_FREE(glyphBitmaps[i].pixels, glyphBitmapSizeInBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ void renderer_string(Renderer *const renderer, v4 cameraBounds,
|
|||||||
// NOTE(doyle): Atlas packs fonts tightly, so offset the codepoint
|
// NOTE(doyle): Atlas packs fonts tightly, so offset the codepoint
|
||||||
// to its actual atlas index, i.e. we skip the first 31 glyphs
|
// to its actual atlas index, i.e. we skip the first 31 glyphs
|
||||||
i32 codepoint = string[i];
|
i32 codepoint = string[i];
|
||||||
i32 relativeIndex = codepoint - font->codepointRange.x;
|
i32 relativeIndex = CAST(i32)(codepoint - font->codepointRange.x);
|
||||||
CharMetrics charMetric = font->charMetrics[relativeIndex];
|
CharMetrics charMetric = font->charMetrics[relativeIndex];
|
||||||
pos.y = baseline - (scale * charMetric.offset.y);
|
pos.y = baseline - (scale * charMetric.offset.y);
|
||||||
|
|
||||||
|
@ -76,11 +76,11 @@ INTERNAL void addAnim(Entity *entity, enum EntityAnimId animId, v4 *rects,
|
|||||||
entity->anim[animId] = result;
|
entity->anim[animId] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void rendererInit(GameState *state, v2i windowSize)
|
INTERNAL void rendererInit(GameState *state, v2 windowSize)
|
||||||
{
|
{
|
||||||
AssetManager *assetManager = &state->assetManager;
|
AssetManager *assetManager = &state->assetManager;
|
||||||
Renderer *renderer = &state->renderer;
|
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
|
// NOTE(doyle): Value to map a screen coordinate to NDC coordinate
|
||||||
renderer->vertexNdcFactor =
|
renderer->vertexNdcFactor =
|
||||||
V2(1.0f / renderer->size.w, 1.0f / renderer->size.h);
|
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;
|
AssetManager *assetManager = &state->assetManager;
|
||||||
/* Initialise assets */
|
/* Initialise assets */
|
||||||
@ -171,7 +171,7 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
/* Init world */
|
/* Init world */
|
||||||
const i32 targetWorldWidth = 500 * METERS_TO_PIXEL;
|
const i32 targetWorldWidth = 500 * METERS_TO_PIXEL;
|
||||||
const i32 targetWorldHeight = 15 * METERS_TO_PIXEL;
|
const i32 targetWorldHeight = 15 * METERS_TO_PIXEL;
|
||||||
v2i worldDimensionInTiles = V2i(targetWorldWidth / state->tileSize,
|
v2 worldDimensionInTiles = V2i(targetWorldWidth / state->tileSize,
|
||||||
targetWorldHeight / state->tileSize);
|
targetWorldHeight / state->tileSize);
|
||||||
|
|
||||||
for (i32 i = 0; i < ARRAY_COUNT(state->world); i++)
|
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->entities = PLATFORM_MEM_ALLOC(world->maxEntities, Entity);
|
||||||
world->texType = texlist_terrain;
|
world->texType = texlist_terrain;
|
||||||
|
|
||||||
v2 worldDimensionInTilesf = V2(CAST(f32) worldDimensionInTiles.x,
|
|
||||||
CAST(f32) worldDimensionInTiles.y);
|
|
||||||
world->bounds =
|
world->bounds =
|
||||||
math_getRect(V2(0, 0), v2_scale(worldDimensionInTilesf,
|
math_getRect(V2(0, 0), v2_scale(worldDimensionInTiles,
|
||||||
CAST(f32) state->tileSize));
|
CAST(f32) state->tileSize));
|
||||||
|
|
||||||
TexAtlas *const atlas =
|
TexAtlas *const atlas =
|
||||||
@ -267,7 +265,7 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
duration);
|
duration);
|
||||||
|
|
||||||
/* Add hero battle tackle animation */
|
/* Add hero battle tackle animation */
|
||||||
duration = 0.09f;
|
duration = 0.15f;
|
||||||
numRects = 3;
|
numRects = 3;
|
||||||
v4 *heroTackleRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
v4 *heroTackleRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
||||||
heroTackleRects[0] = heroAtlas->texRect[herorects_castA];
|
heroTackleRects[0] = heroAtlas->texRect[herorects_castA];
|
||||||
@ -678,7 +676,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
|
|||||||
if (debugString)
|
if (debugString)
|
||||||
{
|
{
|
||||||
v2 strPos = v2_add(entity->pos, entity->size);
|
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;
|
strPos.y += font->charMetrics[indexOfLowerAInMetrics].offset.y;
|
||||||
|
|
||||||
renderer_string(&state->renderer, cameraBounds, font, debugString,
|
renderer_string(&state->renderer, cameraBounds, font, debugString,
|
||||||
|
@ -37,10 +37,11 @@ int main()
|
|||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||||
|
|
||||||
v2i windowSize = V2i(1600, 900);
|
i32 windowWidth = 1600;
|
||||||
|
i32 windowHeight = 900;
|
||||||
|
|
||||||
GLFWwindow *window =
|
GLFWwindow *window =
|
||||||
glfwCreateWindow(windowSize.x, windowSize.y, "Dengine", NULL, NULL);
|
glfwCreateWindow(windowWidth, windowHeight, "Dengine", NULL, NULL);
|
||||||
|
|
||||||
if (!window)
|
if (!window)
|
||||||
{
|
{
|
||||||
@ -62,9 +63,9 @@ int main()
|
|||||||
// regardless of success. Catch it once by calling glGetError
|
// regardless of success. Catch it once by calling glGetError
|
||||||
glGetError();
|
glGetError();
|
||||||
|
|
||||||
v2i frameBufferSize = V2i(0, 0);
|
i32 frameBufferWidth, frameBufferHeight;
|
||||||
glfwGetFramebufferSize(window, &frameBufferSize.w, &frameBufferSize.h);
|
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
|
||||||
glViewport(0, 0, frameBufferSize.x, frameBufferSize.y);
|
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
|
||||||
|
|
||||||
glfwSetKeyCallback(window, key_callback);
|
glfwSetKeyCallback(window, key_callback);
|
||||||
glfwSetCursorPosCallback(window, mouse_callback);
|
glfwSetCursorPosCallback(window, mouse_callback);
|
||||||
@ -82,7 +83,8 @@ int main()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
GameState worldTraveller = {0};
|
GameState worldTraveller = {0};
|
||||||
worldTraveller_gameInit(&worldTraveller, frameBufferSize);
|
worldTraveller_gameInit(&worldTraveller,
|
||||||
|
V2i(frameBufferWidth, frameBufferHeight));
|
||||||
|
|
||||||
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
|
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ typedef struct CharMetrics
|
|||||||
i32 leftSideBearing;
|
i32 leftSideBearing;
|
||||||
// TODO(doyle): Utilise kerning
|
// TODO(doyle): Utilise kerning
|
||||||
i32 *kerning;
|
i32 *kerning;
|
||||||
v2i offset;
|
v2 offset;
|
||||||
v2i trueSize;
|
v2 trueSize;
|
||||||
} CharMetrics;
|
} CharMetrics;
|
||||||
|
|
||||||
typedef struct Font
|
typedef struct Font
|
||||||
@ -72,8 +72,8 @@ typedef struct Font
|
|||||||
FontMetrics metrics;
|
FontMetrics metrics;
|
||||||
CharMetrics *charMetrics;
|
CharMetrics *charMetrics;
|
||||||
|
|
||||||
v2i codepointRange;
|
v2 codepointRange;
|
||||||
v2i maxSize;
|
v2 maxSize;
|
||||||
|
|
||||||
} Font;
|
} Font;
|
||||||
#endif
|
#endif
|
||||||
|
@ -11,13 +11,6 @@
|
|||||||
#define SQRT(x) (sqrtf(x))
|
#define SQRT(x) (sqrtf(x))
|
||||||
|
|
||||||
/* VECTORS */
|
/* VECTORS */
|
||||||
typedef union v2i
|
|
||||||
{
|
|
||||||
struct { i32 x, y; };
|
|
||||||
struct { i32 w, h; };
|
|
||||||
i32 e[2];
|
|
||||||
} v2i;
|
|
||||||
|
|
||||||
typedef union v2
|
typedef union v2
|
||||||
{
|
{
|
||||||
struct { f32 x, y; };
|
struct { f32 x, y; };
|
||||||
@ -39,9 +32,9 @@ typedef union v4
|
|||||||
f32 e[4];
|
f32 e[4];
|
||||||
} v4;
|
} 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;
|
return result;
|
||||||
}
|
}
|
||||||
INTERNAL inline v2 V2(const f32 x, const f32 y)
|
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(3);
|
||||||
DEFINE_VECTOR_FLOAT_MATH(4);
|
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)
|
INTERNAL inline f32 v2_magnitude(const v2 a, const v2 b)
|
||||||
{
|
{
|
||||||
f32 x = b.x - a.x;
|
f32 x = b.x - a.x;
|
||||||
|
@ -40,6 +40,6 @@ typedef struct GameState
|
|||||||
AssetManager assetManager;
|
AssetManager assetManager;
|
||||||
} GameState;
|
} GameState;
|
||||||
|
|
||||||
void worldTraveller_gameInit(GameState *state, v2i windowSize);
|
void worldTraveller_gameInit(GameState *state, v2 windowSize);
|
||||||
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt);
|
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user