Change naming convention, merge texture/shaders

Reduce the number of files in project by merging texture and shaders into
assets/renderer file. It's better to keep code local to where it is rather than
splitting them into "self-contained modules". It becomes easier to understand
reading top-to-bottom and there's is less jumping around which can increase the
difficulty of comprehending the code.

Convert names to use more of a <namespace>_<object><Action> format.
This commit is contained in:
Doyle Thai 2016-11-27 21:16:12 +11:00
parent 2fb2e6db5b
commit 6df92e7aed
27 changed files with 422 additions and 485 deletions

View File

@ -130,10 +130,8 @@
<ClCompile Include="src\MemoryArena.c" />
<ClCompile Include="src\Platform.c" />
<ClCompile Include="src\Renderer.c" />
<ClCompile Include="src\Shader.c" />
<ClCompile Include="src\String.c" />
<ClCompile Include="src\UserInterface.c" />
<ClCompile Include="src\Texture.c" />
<ClCompile Include="src\Ui.c" />
</ItemGroup>
<ItemGroup>
<None Include="data\shaders\default.frag.glsl" />
@ -155,10 +153,8 @@
<ClInclude Include="src\include\Dengine\Math.h" />
<ClInclude Include="src\include\Dengine\OpenGL.h" />
<ClInclude Include="src\include\Dengine\Renderer.h" />
<ClInclude Include="src\include\Dengine\Shader.h" />
<ClInclude Include="src\include\Dengine\String.h" />
<ClInclude Include="src\include\Dengine\Texture.h" />
<ClInclude Include="src\include\Dengine\UserInterface.h" />
<ClInclude Include="src\include\Dengine\Ui.h" />
<ClInclude Include="src\include\Dengine\WorldTraveller.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -15,12 +15,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Texture.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Shader.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Renderer.c">
<Filter>Source Files</Filter>
</ClCompile>
@ -42,9 +36,6 @@
<ClCompile Include="src\dengine.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\UserInterface.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\String.c">
<Filter>Source Files</Filter>
</ClCompile>
@ -57,6 +48,9 @@
<ClCompile Include="src\Entity.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Ui.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="data\shaders\default.vert.glsl" />
@ -68,15 +62,9 @@
<ClInclude Include="src\include\Dengine\Common.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\Shader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\OpenGL.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\Texture.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\AssetManager.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -107,9 +95,6 @@
<ClInclude Include="src\include\Dengine\WorldTraveller.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\UserInterface.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\String.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -119,5 +104,8 @@
<ClInclude Include="src\include\Dengine\Entity.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\Ui.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -22,6 +22,83 @@
#include "Dengine/OpenGL.h"
#include "Dengine/Platform.h"
enum BytesPerPixel
{
bytesPerPixel_Greyscale = 1,
bytesPerPixel_GreyscaleAlpha = 2,
bytesPerPixel_RGB = 3,
bytesPerPixel_RGBA = 4,
};
INTERNAL GLint getGLFormat(i32 bytesPerPixel, b32 srgb)
{
switch (bytesPerPixel)
{
case bytesPerPixel_Greyscale:
return GL_LUMINANCE;
case bytesPerPixel_GreyscaleAlpha:
return GL_LUMINANCE_ALPHA;
case bytesPerPixel_RGB:
return (srgb ? GL_SRGB : GL_RGB);
case bytesPerPixel_RGBA:
return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
default:
// TODO(doyle): Invalid
// std::cout << "getGLFormat() invalid bytesPerPixel: "
// << bytesPerPixel << std::endl;
return GL_LUMINANCE;
}
}
Texture textureGen(const GLuint width, const GLuint height,
const GLint bytesPerPixel, const u8 *const image)
{
// TODO(doyle): Let us set the parameters gl params as well
GL_CHECK_ERROR();
Texture tex = {0};
tex.width = width;
tex.height = height;
tex.internalFormat = GL_RGBA;
tex.wrapS = GL_REPEAT;
tex.wrapT = GL_REPEAT;
tex.filterMinification = GL_NEAREST;
tex.filterMagnification = GL_NEAREST;
glGenTextures(1, &tex.id);
GL_CHECK_ERROR();
glBindTexture(GL_TEXTURE_2D, tex.id);
GL_CHECK_ERROR();
/* Load image into texture */
// TODO(doyle) Figure out the gl format
tex.imageFormat = getGLFormat(bytesPerPixel, FALSE);
ASSERT(tex.imageFormat == GL_RGBA);
GL_CHECK_ERROR();
glTexImage2D(GL_TEXTURE_2D, 0, tex.internalFormat, tex.width, tex.height, 0,
tex.imageFormat, GL_UNSIGNED_BYTE, image);
GL_CHECK_ERROR();
// TODO(doyle): Not needed for sprites? glGenerateMipmap(GL_TEXTURE_2D);
/* Set parameter of currently bound texture */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, tex.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tex.wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
tex.filterMinification);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
tex.filterMagnification);
GL_CHECK_ERROR();
/* Unbind and clean up */
glBindTexture(GL_TEXTURE_2D, 0);
GL_CHECK_ERROR();
return tex;
}
void asset_init(AssetManager *assetManager, MemoryArena_ *arena)
{
i32 texAtlasEntries = 8;
@ -41,8 +118,8 @@ void asset_init(AssetManager *assetManager, MemoryArena_ *arena)
/* Create empty 1x1 4bpp black texture */
u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0);
Texture *tex = asset_getFreeTexSlot(assetManager, arena, "nullTex");
*tex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap));
Texture *tex = asset_texGetFreeSlot(assetManager, arena, "nullTex");
*tex = textureGen(1, 1, 4, CAST(u8 *)(&bitmap));
i32 audioEntries = 32;
assetManager->audio.size = audioEntries;
@ -128,7 +205,7 @@ INTERNAL SubTexture *getFreeAtlasSubTexSlot(TexAtlas *const atlas,
}
}
const SubTexture asset_getAtlasSubTex(TexAtlas *const atlas, const char *const key)
const SubTexture asset_atlasGetSubTex(TexAtlas *const atlas, const char *const key)
{
HashTableEntry *entry = getEntryFromHash(&atlas->subTex, key);
@ -140,11 +217,11 @@ const SubTexture asset_getAtlasSubTex(TexAtlas *const atlas, const char *const k
return result;
}
DEBUG_LOG("asset_getAtlasSubTex() failed: Sub texture does not exist");
DEBUG_LOG("asset_atlasGetSubTex() failed: Sub texture does not exist");
return result;
}
Texture *asset_getTex(AssetManager *const assetManager, const char *const key)
Texture *asset_texGet(AssetManager *const assetManager, const char *const key)
{
HashTableEntry *entry = getEntryFromHash(&assetManager->textures, key);
@ -154,7 +231,7 @@ Texture *asset_getTex(AssetManager *const assetManager, const char *const key)
return result;
}
TexAtlas *asset_getFreeTexAtlasSlot(AssetManager *const assetManager,
TexAtlas *asset_atlasGetFreeSlot(AssetManager *const assetManager,
MemoryArena_ *arena, const char *const key,
i32 numSubTex)
{
@ -191,7 +268,7 @@ TexAtlas *asset_getFreeTexAtlasSlot(AssetManager *const assetManager,
}
}
TexAtlas *asset_getTexAtlas(AssetManager *const assetManager,
TexAtlas *asset_atlasGet(AssetManager *const assetManager,
const char *const key)
{
@ -204,7 +281,7 @@ TexAtlas *asset_getTexAtlas(AssetManager *const assetManager,
}
Texture *asset_getFreeTexSlot(AssetManager *const assetManager,
Texture *asset_texGetFreeSlot(AssetManager *const assetManager,
MemoryArena_ *const arena, const char *const key)
{
@ -223,7 +300,7 @@ Texture *asset_getFreeTexSlot(AssetManager *const assetManager,
}
}
Texture *asset_loadTextureImage(AssetManager *assetManager, MemoryArena_ *arena,
Texture *asset_texLoadImage(AssetManager *assetManager, MemoryArena_ *arena,
const char *const path, const char *const key)
{
/* Open the texture image */
@ -236,7 +313,7 @@ Texture *asset_loadTextureImage(AssetManager *assetManager, MemoryArena_ *arena,
if (imgWidth != imgHeight)
{
printf(
"asset_loadTextureImage() warning: Sprite sheet is not square: "
"asset_texLoadImage() warning: Sprite sheet is not square: "
"%dx%dpx\n", imgWidth, imgHeight);
}
#endif
@ -247,8 +324,8 @@ Texture *asset_loadTextureImage(AssetManager *assetManager, MemoryArena_ *arena,
return NULL;
}
Texture *result = asset_getFreeTexSlot(assetManager, arena, key);
*result = texture_gen(CAST(GLuint)(imgWidth), CAST(GLuint)(imgHeight),
Texture *result = asset_texGetFreeSlot(assetManager, arena, key);
*result = textureGen(CAST(GLuint)(imgWidth), CAST(GLuint)(imgHeight),
CAST(GLint)(bytesPerPixel), image);
GL_CHECK_ERROR();
@ -280,7 +357,7 @@ INTERNAL Animation *getFreeAnimationSlot(AssetManager *const assetManager,
}
}
void asset_addAnimation(AssetManager *const assetManager,
void asset_animAdd(AssetManager *const assetManager,
MemoryArena_ *const arena, const char *const animName,
TexAtlas *const atlas,
char **const subTextureNames,
@ -304,7 +381,7 @@ void asset_addAnimation(AssetManager *const assetManager,
}
Animation *asset_getAnim(AssetManager *const assetManager,
Animation *asset_animGet(AssetManager *const assetManager,
const char *const key)
{
HashTableEntry *entry = getEntryFromHash(&assetManager->anims, key);
@ -617,7 +694,7 @@ INTERNAL void parseXmlTreeToGame(AssetManager *assetManager, MemoryArena_ *arena
*/
char *imageName = atlasXmlNode->attribute.value;
i32 numSubTex = 1024;
atlas = asset_getFreeTexAtlasSlot(assetManager, arena,
atlas = asset_atlasGetFreeSlot(assetManager, arena,
imageName, numSubTex);
if (!atlas)
@ -643,7 +720,7 @@ INTERNAL void parseXmlTreeToGame(AssetManager *assetManager, MemoryArena_ *arena
common_strncat(imagePath, dataDir, dataDirLen);
common_strncat(imagePath, imageName, imageNameLen);
Texture *tex = asset_loadTextureImage(assetManager, arena,
Texture *tex = asset_texLoadImage(assetManager, arena,
imagePath, imageName);
if (!tex)
@ -839,7 +916,7 @@ INTERNAL void freeXmlData(MemoryArena_ *const arena, XmlToken *tokens,
* Everything else
*********************************
*/
const i32 asset_loadXmlFile(AssetManager *const assetManager,
const i32 asset_xmlLoad(AssetManager *const assetManager,
MemoryArena_ *const arena,
const PlatformFileRead *const fileRead)
{
@ -868,7 +945,7 @@ const i32 asset_loadXmlFile(AssetManager *const assetManager,
return result;
}
AudioVorbis *const asset_getVorbis(AssetManager *const assetManager,
AudioVorbis *const asset_vorbisGet(AssetManager *const assetManager,
const char *const key)
{
@ -880,7 +957,7 @@ AudioVorbis *const asset_getVorbis(AssetManager *const assetManager,
return result;
}
const i32 asset_loadVorbis(AssetManager *assetManager, MemoryArena_ *arena,
const i32 asset_vorbisLoad(AssetManager *assetManager, MemoryArena_ *arena,
const char *const path, const char *const key)
{
HashTableEntry *entry = getFreeHashSlot(&assetManager->audio, arena, key);
@ -974,7 +1051,7 @@ INTERNAL u32 shaderLoadProgram(const GLuint vertexShader,
return result;
}
u32 asset_getShader(AssetManager *assetManager, const enum ShaderList type)
u32 asset_shaderGet(AssetManager *assetManager, const enum ShaderList type)
{
if (type < shaderlist_count) return assetManager->shaders[type];
@ -984,7 +1061,7 @@ u32 asset_getShader(AssetManager *assetManager, const enum ShaderList type)
return -1;
}
const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
const i32 asset_shaderLoad(AssetManager *assetManager, MemoryArena_ *arena,
const char *const vertexPath,
const char *const fragmentPath,
const enum ShaderList type)
@ -1017,13 +1094,13 @@ INTERNAL FontPack *getMatchingFontPack(AssetManager *assetManager,
return result;
}
Font *asset_getFontCreateSizeOnDemand(AssetManager *assetManager,
Font *asset_fontGetOrCreateOnDemand(AssetManager *assetManager,
MemoryArena_ *persistentArena,
MemoryArena_ *transientArena, char *name,
i32 size)
{
Font *result = asset_getFont(assetManager, name, size);
Font *result = asset_fontGet(assetManager, name, size);
if (result == NULL)
{
@ -1042,10 +1119,10 @@ Font *asset_getFontCreateSizeOnDemand(AssetManager *assetManager,
if (result == NULL)
{
asset_loadTTFont(assetManager, persistentArena, transientArena,
asset_fontLoadTTF(assetManager, persistentArena, transientArena,
pack->filePath, name, size);
result = asset_getFont(assetManager, name, size);
result = asset_fontGet(assetManager, name, size);
}
}
else
@ -1057,7 +1134,7 @@ Font *asset_getFontCreateSizeOnDemand(AssetManager *assetManager,
return result;
}
Font *asset_getFont(AssetManager *assetManager, char *name, i32 size)
Font *asset_fontGet(AssetManager *assetManager, char *name, i32 size)
{
Font *result = NULL;
FontPack *pack = getMatchingFontPack(assetManager, name);
@ -1084,7 +1161,7 @@ typedef struct GlyphBitmap
i32 codepoint;
} GlyphBitmap;
const i32 asset_loadTTFont(AssetManager *assetManager,
const i32 asset_fontLoadTTF(AssetManager *assetManager,
MemoryArena_ *persistentArena,
MemoryArena_ *transientArena, char *filePath,
char *name, i32 targetFontHeight)
@ -1134,7 +1211,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager,
return 0;
}
TempMemory tempRegion = memory_begin_temporary_region(transientArena);
TempMemory tempRegion = memory_beginTempRegion(transientArena);
PlatformFileRead fontFileRead = {0};
i32 result =
@ -1230,7 +1307,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager,
if ((largestGlyphDimension.h - CAST(i32)targetFontHeight) >= 50)
{
printf(
"asset_loadTTFont() warning: The loaded font file has a glyph "
"asset_fontLoadTTF() warning: The loaded font file has a glyph "
"considerably larger than our target .. font packing is "
"unoptimal\n");
}
@ -1254,7 +1331,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager,
if ((glyphsPerRow * glyphsPerCol) <= numGlyphs)
{
printf(
"asset_loadTTFont() warning: The target font height creates a "
"asset_fontLoadTTF() warning: The target font height creates a "
"glyph sheet that exceeds the available space!");
ASSERT(INVALID_CODE_PATH);
@ -1280,7 +1357,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager,
char charToEncode = CAST(char)codepointRange.x;
i32 numSubTex = numGlyphs;
TexAtlas *fontAtlas = asset_getFreeTexAtlasSlot(
TexAtlas *fontAtlas = asset_atlasGetFreeSlot(
assetManager, persistentArena, "font", numSubTex);
/*
@ -1363,8 +1440,8 @@ const i32 asset_loadTTFont(AssetManager *assetManager,
* Generate and store font bitmap to assets
*******************************************
*/
Texture *tex = asset_getFreeTexSlot(assetManager, persistentArena, "font");
*tex = texture_gen(MAX_TEXTURE_SIZE, MAX_TEXTURE_SIZE, 4,
Texture *tex = asset_texGetFreeSlot(assetManager, persistentArena, "font");
*tex = textureGen(MAX_TEXTURE_SIZE, MAX_TEXTURE_SIZE, 4,
CAST(u8 *) fontBitmap);
#ifdef WT_RENDER_FONT_FILE
@ -1390,11 +1467,11 @@ const i32 asset_loadTTFont(AssetManager *assetManager,
sizeof(u32);
}
memory_end_temporary_region(tempRegion);
memory_endTempRegion(tempRegion);
return 0;
}
const v2 asset_stringDimInPixels(const Font *const font,
const v2 asset_fontStringDimInPixels(const Font *const font,
const char *const string)
{
v2 stringDim = V2(0, 0);

View File

@ -8,55 +8,55 @@ INTERNAL void loadGameAssets(GameState *state)
{ // Init font assets
i32 result =
asset_loadTTFont(assetManager, arena, &state->transientArena,
asset_fontLoadTTF(assetManager, arena, &state->transientArena,
"C:/Windows/Fonts/Arialbd.ttf", "Arial", 15);
}
{ // Init shaders assets
asset_loadShaderFiles(
asset_shaderLoad(
assetManager, arena, "data/shaders/default_tex.vert.glsl",
"data/shaders/default_tex.frag.glsl", shaderlist_default);
asset_loadShaderFiles(
asset_shaderLoad(
assetManager, arena, "data/shaders/default_no_tex.vert.glsl",
"data/shaders/default_no_tex.frag.glsl", shaderlist_default_no_tex);
}
{ // Init audio assets
i32 result = asset_loadVorbis(assetManager, arena,
i32 result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/bang_large.ogg",
"bang_large");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/bang_medium.ogg",
"bang_medium");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/bang_small.ogg",
"bang_small");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/beat1.ogg", "beat1");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/beat2.ogg", "beat2");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/extra_ship.ogg",
"extra_ship");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/fire.ogg", "fire");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/saucer_big.ogg",
"saucer_big");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/saucer_small.ogg",
"saucer_small");
ASSERT(!result);
result = asset_loadVorbis(assetManager, arena,
result = asset_vorbisLoad(assetManager, arena,
"data/audio/Asteroids/thrust.ogg", "thrust");
ASSERT(!result);
}
@ -348,19 +348,19 @@ INTERNAL void addAsteroidWithSpec(World *world, enum AsteroidSize asteroidSize,
// generated
// to float back into game space
v2 newP = V2i(randX, randY);
if (math_rect_contains_p(topLeftQuadrant, newP))
if (math_rectContainsP(topLeftQuadrant, newP))
{
newP.y += midpoint.y;
}
else if (math_rect_contains_p(botLeftQuadrant, newP))
else if (math_rectContainsP(botLeftQuadrant, newP))
{
newP.x -= midpoint.x;
}
else if (math_rect_contains_p(topRightQuadrant, newP))
else if (math_rectContainsP(topRightQuadrant, newP))
{
newP.y -= midpoint.y;
}
else if (math_rect_contains_p(botRightQuadrant, newP))
else if (math_rectContainsP(botRightQuadrant, newP))
{
newP.x += midpoint.x;
}
@ -505,7 +505,7 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
{ // Init ship entity
Entity *ship = &world->entityList[world->entityIndex++];
ship->id = world->entityIdCounter++;
ship->pos = math_rect_get_centre(world->camera);
ship->pos = math_rectGetCentre(world->camera);
ship->size = V2(25.0f, 50.0f);
ship->hitbox = ship->size;
ship->offset = v2_scale(ship->size, -0.5f);
@ -596,10 +596,10 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
if (audioRenderer)
{
AudioVorbis *fire =
asset_getVorbis(&state->assetManager, "fire");
asset_vorbisGet(&state->assetManager, "fire");
// TODO(doyle): Atm transient arena is not used, this is
// just to fill out the arguments
audio_playVorbis(&state->transientArena,
audio_vorbisPlay(&state->transientArena,
&state->audioManager, audioRenderer, fire,
1);
}
@ -711,7 +711,7 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
}
else if (entity->type == entitytype_bullet)
{
if (!math_rect_contains_p(world->camera, entity->pos))
if (!math_rectContainsP(world->camera, entity->pos))
{
deleteEntity(world, i--);
continue;
@ -882,8 +882,8 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
}
AudioVorbis *explode =
asset_getVorbis(&state->assetManager, sound);
audio_playVorbis(&state->transientArena,
asset_vorbisGet(&state->assetManager, sound);
audio_vorbisPlay(&state->transientArena,
&state->audioManager, audioRenderer,
explode, 1);
}
@ -916,31 +916,31 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
World *world = &state->world;
InputBuffer *inputBuffer = &state->input;
Font *arial15 = asset_getFontCreateSizeOnDemand(
Font *arial15 = asset_fontGetOrCreateOnDemand(
assetManager, &state->persistentArena, transientArena, "Arial", 15);
Font *arial25 = asset_getFontCreateSizeOnDemand(
Font *arial25 = asset_fontGetOrCreateOnDemand(
assetManager, &state->persistentArena, transientArena, "Arial", 40);
f32 margin = 20.0f;
f32 padding = 20.0f;
v2 titleP = V2(margin, renderer->size.h - 100 + margin);
renderer_staticString(renderer, transientArena, arial25, "Asteroids",
renderer_stringFixed(renderer, transientArena, arial25, "Asteroids",
titleP, V2(0, 0), 0, V4(1, 0, 0, 1), 0);
userInterface_beginState(uiState);
ui_beginState(uiState);
Rect buttonRect = {0};
buttonRect.min = V2(margin, margin);
buttonRect.max = V2(margin + 100, margin + 40);
buttonRect = math_rect_shift(buttonRect, V2(0, titleP.y - 100));
if (userInterface_button(uiState, transientArena, assetManager, renderer,
buttonRect = math_rectShift(buttonRect, V2(0, titleP.y - 100));
if (ui_button(uiState, transientArena, assetManager, renderer,
arial15, *inputBuffer, 1, buttonRect,
"Start Game"))
{
state->appState = appstate_game;
}
userInterface_endState(uiState, inputBuffer);
ui_endState(uiState, inputBuffer);
}
void asteroid_gameUpdateAndRender(GameState *state, Memory *memory,
@ -963,14 +963,14 @@ void asteroid_gameUpdateAndRender(GameState *state, Memory *memory,
renderer_init(&state->renderer, &state->assetManager,
&state->persistentArena, windowSize);
Font *arial15 = asset_getFont(&state->assetManager, "Arial", 15);
Font *arial15 = asset_fontGet(&state->assetManager, "Arial", 15);
debug_init(&state->persistentArena, windowSize, *arial15);
state->appState = appstate_start_menu;
state->init = TRUE;
}
platform_processInputBuffer(&state->input, dt);
platform_inputBufferProcess(&state->input, dt);
switch (state->appState)
{

View File

@ -129,7 +129,7 @@ INTERNAL i32 rendererAcquire(MemoryArena_ *arena, AudioManager *audioManager,
"rendererAcquire(): Renderer has not been released before "
"acquiring, force release by stopping stream");
#endif
audio_stopVorbis(arena, audioManager, audioRenderer);
audio_vorbisStop(arena, audioManager, audioRenderer);
}
// TODO(doyle): Super bad linear O(n) search for every audio-enabled entity
@ -239,14 +239,14 @@ INTERNAL i32 initRendererForPlayback(MemoryArena_ *arena,
ASSERT(audioManager && audioRenderer && vorbis);
if (numPlays != AUDIO_REPEAT_INFINITE && numPlays <= 0)
{
DEBUG_LOG("audio_streamPlayVorbis() warning: Number of plays is less than 0");
DEBUG_LOG("audio_vorbisStream() warning: Number of plays is less than 0");
}
#endif
i32 result = rendererAcquire(arena, audioManager, audioRenderer);
if (result)
{
DEBUG_LOG("audio_streamPlayVorbis() failed: Could not acquire renderer");
DEBUG_LOG("audio_vorbisStream() failed: Could not acquire renderer");
return result;
}
@ -258,7 +258,7 @@ INTERNAL i32 initRendererForPlayback(MemoryArena_ *arena,
{
#ifdef DENGINE_DEBUG
DEBUG_LOG(
"audio_streamPlayVorbis() warning: Unexpected channel format");
"audio_vorbisStream() warning: Unexpected channel format");
#endif
}
@ -268,7 +268,7 @@ INTERNAL i32 initRendererForPlayback(MemoryArena_ *arena,
}
#include <stdlib.h>
const i32 audio_playVorbis(MemoryArena_ *arena, AudioManager *audioManager,
const i32 audio_vorbisPlay(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer, AudioVorbis *vorbis,
i32 numPlays)
{
@ -296,7 +296,7 @@ const i32 audio_playVorbis(MemoryArena_ *arena, AudioManager *audioManager,
return result;
}
const i32 audio_streamPlayVorbis(MemoryArena_ *arena, AudioManager *audioManager,
const i32 audio_vorbisStream(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer,
AudioVorbis *vorbis, i32 numPlays)
{
@ -326,7 +326,7 @@ const i32 audio_streamPlayVorbis(MemoryArena_ *arena, AudioManager *audioManager
return result;
}
const i32 audio_stopVorbis(MemoryArena_ *arena, AudioManager *audioManager,
const i32 audio_vorbisStop(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
@ -354,7 +354,7 @@ const i32 audio_stopVorbis(MemoryArena_ *arena, AudioManager *audioManager,
return result;
}
const i32 audio_pauseVorbis(AudioManager *audioManager,
const i32 audio_vorbisPause(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
@ -373,7 +373,7 @@ const i32 audio_pauseVorbis(AudioManager *audioManager,
return result;
}
const i32 audio_resumeVorbis(AudioManager *audioManager,
const i32 audio_vorbisResume(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
@ -387,7 +387,7 @@ const i32 audio_resumeVorbis(AudioManager *audioManager,
else
{
#ifdef DENGINE_DEBUG
DEBUG_LOG("audio_resumeVorbis(): Tried to resume invalid source")
DEBUG_LOG("audio_vorbisResume(): Tried to resume invalid source")
#endif
result = -1;
}

View File

@ -223,7 +223,7 @@ INTERNAL void updateAndRenderDebugStack(Renderer *renderer, MemoryArena_ *arena,
{
f32 rotate = 0;
v4 color = V4(1, 1, 1, 1);
renderer_staticString(
renderer_stringFixed(
renderer, arena, &GLOBAL_debug.font, GLOBAL_debug.debugStrings[i],
GLOBAL_debug.currStringP, V2(0, 0), rotate, color, 0);
GLOBAL_debug.currStringP.y -= (0.9f * GLOBAL_debug.stringLineGap);
@ -255,7 +255,7 @@ INTERNAL void renderConsole(Renderer *renderer, MemoryArena_ *arena)
{
f32 rotate = 0;
v4 color = V4(1.0f, 1.0f, 1.0f, 1.0f);
renderer_staticString(renderer, arena, &GLOBAL_debug.font,
renderer_stringFixed(renderer, arena, &GLOBAL_debug.font,
GLOBAL_debug.console[i], consoleStrP,
V2(0, 0), rotate, color, 0);
consoleStrP.y -= (0.9f * GLOBAL_debug.stringLineGap);

View File

@ -4,17 +4,17 @@
#include "Dengine/Math.h"
#include "Dengine/MemoryArena.h"
SubTexture entity_getActiveSubTexture(Entity *const entity)
SubTexture entity_subTexGetCurr(Entity *const entity)
{
EntityAnim *entityAnim = &entity->animList[entity->animListIndex];
Animation *anim = entityAnim->anim;
char *frameName = anim->frameList[entityAnim->currFrame];
SubTexture result = asset_getAtlasSubTex(anim->atlas, frameName);
SubTexture result = asset_atlasGetSubTex(anim->atlas, frameName);
return result;
}
void entity_setActiveAnim(Entity *const entity, const char *const animName)
void entity_animSet(Entity *const entity, const char *const animName)
{
/* Reset current anim data */
EntityAnim *currEntityAnim = &entity->animList[entity->animListIndex];
@ -44,7 +44,7 @@ void entity_setActiveAnim(Entity *const entity, const char *const animName)
DEBUG_LOG("Entity does not have access to desired anim");
}
void entity_updateAnim(Entity *const entity, const f32 dt)
void entity_animUpdate(Entity *const entity, const f32 dt)
{
if (!entity->tex)
return;
@ -66,11 +66,11 @@ void entity_updateAnim(Entity *const entity, const f32 dt)
}
char *frameName = anim->frameList[currEntityAnim->currFrame];
SubTexture texRect = asset_getAtlasSubTex(anim->atlas, frameName);
SubTexture texRect = asset_atlasGetSubTex(anim->atlas, frameName);
entity->size = v2_scale(texRect.rect.max, entity->scale);
}
void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
void entity_animAdd(AssetManager *const assetManager, Entity *const entity,
const char *const animName)
{
i32 freeAnimIndex = 0;
@ -79,7 +79,7 @@ void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
EntityAnim *entityAnim = &entity->animList[i];
if (!entityAnim->anim)
{
entityAnim->anim = asset_getAnim(assetManager, animName);
entityAnim->anim = asset_animGet(assetManager, animName);
entityAnim->currFrame = 0;
entityAnim->currDuration = entityAnim->anim->frameDuration;
return;

View File

@ -8,7 +8,7 @@ void memory_arenaInit(MemoryArena_ *arena, void *base, size_t size)
arena->tempMemoryCount = 0;
}
TempMemory memory_begin_temporary_region(MemoryArena_ *arena)
TempMemory memory_beginTempRegion(MemoryArena_ *arena)
{
TempMemory result = {0};
result.arena = arena;
@ -19,7 +19,7 @@ TempMemory memory_begin_temporary_region(MemoryArena_ *arena)
return result;
}
void memory_end_temporary_region(TempMemory tempMemory)
void memory_endTempRegion(TempMemory tempMemory)
{
MemoryArena_ *arena = tempMemory.arena;
ASSERT(arena->used > tempMemory.used)

View File

@ -96,7 +96,7 @@ i32 platform_readFileToBuffer(MemoryArena_ *arena, const char *const filePath,
return 0;
}
void platform_processInputBuffer(InputBuffer *inputBuffer, f32 dt)
void platform_inputBufferProcess(InputBuffer *inputBuffer, f32 dt)
{
KeyState *keyBuffer = inputBuffer->keys;
for (enum KeyCode code = 0; code < keycode_count; code++)

View File

@ -5,8 +5,32 @@
#include "Dengine/Entity.h"
#include "Dengine/MemoryArena.h"
#include "Dengine/OpenGL.h"
#include "Dengine/Shader.h"
#include "Dengine/Texture.h"
void shaderUniformSet1i(u32 shaderId, const GLchar *name,
const GLuint data)
{
GLint uniformLoc = glGetUniformLocation(shaderId, name);
glUniform1i(uniformLoc, data);
}
void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name,
mat4 data)
{
GLint uniformLoc = glGetUniformLocation(shaderId, name);
GL_CHECK_ERROR();
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]);
GL_CHECK_ERROR();
}
void shaderUniformSetVec4f(u32 shaderId, const GLchar *name,
v4 data)
{
GLint uniformLoc = glGetUniformLocation(shaderId, name);
glUniform4f(uniformLoc, data.e[0], data.e[1], data.e[2], data.e[3]);
}
void shaderUse(u32 shaderId) { glUseProgram(shaderId); }
void renderer_init(Renderer *renderer, AssetManager *assetManager,
MemoryArena_ *persistentArena, v2 windowSize)
@ -21,9 +45,9 @@ void renderer_init(Renderer *renderer, AssetManager *assetManager,
mat4_ortho(0.0f, renderer->size.w, 0.0f, renderer->size.h, 0.0f, 1.0f);
for (i32 i = 0; i < shaderlist_count; i++)
{
renderer->shaderList[i] = asset_getShader(assetManager, i);
shader_use(renderer->shaderList[i]);
shader_uniformSetMat4fv(renderer->shaderList[i], "projection",
renderer->shaderList[i] = asset_shaderGet(assetManager, i);
shaderUse(renderer->shaderList[i]);
shaderUniformSetMat4fv(renderer->shaderList[i], "projection",
projection);
GL_CHECK_ERROR();
}
@ -431,18 +455,18 @@ INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
{
renderer->activeShaderId =
renderer->shaderList[shaderlist_default_no_tex];
shader_use(renderer->activeShaderId);
shaderUse(renderer->activeShaderId);
}
else
{
renderer->activeShaderId = renderer->shaderList[shaderlist_default];
shader_use(renderer->activeShaderId);
shaderUse(renderer->activeShaderId);
Texture *tex = group->tex;
if (tex)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id);
shader_uniformSet1i(renderer->activeShaderId, "tex", 0);
shaderUniformSet1i(renderer->activeShaderId, "tex", 0);
GL_CHECK_ERROR();
}
}
@ -452,7 +476,7 @@ INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
#endif
/* Set color modulation value */
shader_uniformSetVec4f(renderer->activeShaderId, "spriteColor",
shaderUniformSetVec4f(renderer->activeShaderId, "spriteColor",
group->color);
glBindVertexArray(renderer->vao[group->mode]);
@ -469,7 +493,7 @@ INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
RenderTex renderer_createNullRenderTex(AssetManager *const assetManager)
{
Texture *emptyTex = asset_getTex(assetManager, "nullTex");
Texture *emptyTex = asset_texGet(assetManager, "nullTex");
RenderTex result = {emptyTex, V4(0, 1, 1, 0)};
return result;
}
@ -606,8 +630,8 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
v2_add(pos, V2((CAST(f32) font->maxSize.w * CAST(f32) strLen),
CAST(f32) font->maxSize.h));
v2 leftAlignedP = pos;
if (math_rect_contains_p(camera, leftAlignedP) ||
math_rect_contains_p(camera, rightAlignedP))
if (math_rectContainsP(camera, leftAlignedP) ||
math_rectContainsP(camera, rightAlignedP))
{
i32 vertexIndex = 0;
i32 numVertexPerQuad = 4;
@ -630,7 +654,7 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
/* Get texture out */
SubTexture subTexture =
asset_getAtlasSubTex(font->atlas, &CAST(char)codepoint);
asset_atlasGetSubTex(font->atlas, &CAST(char)codepoint);
v4 charTexRect = {0};
charTexRect.vec2[0] = subTexture.rect.min;
@ -667,7 +691,7 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
{
Animation *anim = entityAnim->anim;
char *frameName = anim->frameList[entityAnim->currFrame];
SubTexture subTex = asset_getAtlasSubTex(anim->atlas, frameName);
SubTexture subTex = asset_atlasGetSubTex(anim->atlas, frameName);
texRect.vec2[0] = subTex.rect.min;
texRect.vec2[1] = v2_add(subTex.rect.min, subTex.rect.max);

View File

@ -1,27 +0,0 @@
#include "Dengine/Shader.h"
void shader_uniformSet1i(u32 shaderId, const GLchar *name,
const GLuint data)
{
GLint uniformLoc = glGetUniformLocation(shaderId, name);
glUniform1i(uniformLoc, data);
}
void shader_uniformSetMat4fv(u32 shaderId, const GLchar *name,
mat4 data)
{
GLint uniformLoc = glGetUniformLocation(shaderId, name);
GL_CHECK_ERROR();
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]);
GL_CHECK_ERROR();
}
void shader_uniformSetVec4f(u32 shaderId, const GLchar *name,
v4 data)
{
GLint uniformLoc = glGetUniformLocation(shaderId, name);
glUniform4f(uniformLoc, data.e[0], data.e[1], data.e[2], data.e[3]);
}
void shader_use(u32 shaderId) { glUseProgram(shaderId); }

View File

@ -1,78 +0,0 @@
#include "Dengine/Texture.h"
enum BytesPerPixel
{
bytesPerPixel_Greyscale = 1,
bytesPerPixel_GreyscaleAlpha = 2,
bytesPerPixel_RGB = 3,
bytesPerPixel_RGBA = 4,
};
INTERNAL GLint getGLFormat(i32 bytesPerPixel, b32 srgb)
{
switch (bytesPerPixel)
{
case bytesPerPixel_Greyscale:
return GL_LUMINANCE;
case bytesPerPixel_GreyscaleAlpha:
return GL_LUMINANCE_ALPHA;
case bytesPerPixel_RGB:
return (srgb ? GL_SRGB : GL_RGB);
case bytesPerPixel_RGBA:
return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
default:
// TODO(doyle): Invalid
// std::cout << "getGLFormat() invalid bytesPerPixel: "
// << bytesPerPixel << std::endl;
return GL_LUMINANCE;
}
}
Texture texture_gen(const GLuint width, const GLuint height,
const GLint bytesPerPixel, const u8 *const image)
{
// TODO(doyle): Let us set the parameters gl params as well
GL_CHECK_ERROR();
Texture tex = {0};
tex.width = width;
tex.height = height;
tex.internalFormat = GL_RGBA;
tex.wrapS = GL_REPEAT;
tex.wrapT = GL_REPEAT;
tex.filterMinification = GL_NEAREST;
tex.filterMagnification = GL_NEAREST;
glGenTextures(1, &tex.id);
GL_CHECK_ERROR();
glBindTexture(GL_TEXTURE_2D, tex.id);
GL_CHECK_ERROR();
/* Load image into texture */
// TODO(doyle) Figure out the gl format
tex.imageFormat = getGLFormat(bytesPerPixel, FALSE);
ASSERT(tex.imageFormat == GL_RGBA);
GL_CHECK_ERROR();
glTexImage2D(GL_TEXTURE_2D, 0, tex.internalFormat, tex.width, tex.height, 0,
tex.imageFormat, GL_UNSIGNED_BYTE, image);
GL_CHECK_ERROR();
// TODO(doyle): Not needed for sprites? glGenerateMipmap(GL_TEXTURE_2D);
/* Set parameter of currently bound texture */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, tex.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tex.wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
tex.filterMinification);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
tex.filterMagnification);
GL_CHECK_ERROR();
/* Unbind and clean up */
glBindTexture(GL_TEXTURE_2D, 0);
GL_CHECK_ERROR();
return tex;
}

View File

@ -1,16 +1,13 @@
#include "Dengine/UserInterface.h"
#include "Dengine/Ui.h"
#include "Dengine/AssetManager.h"
#include "Dengine/Assets.h"
#include "Dengine/Asteroid.h"
#include "Dengine/Debug.h"
#include "Dengine/Renderer.h"
void userInterface_beginState(UiState *state)
{
state->hotItem = 0;
}
void ui_beginState(UiState *state) { state->hotItem = 0; }
void userInterface_endState(UiState *state, InputBuffer *input)
void ui_endState(UiState *state, InputBuffer *input)
{
if (!common_isSet(input->keys[keycode_mouseLeft].flags,
keystateflag_ended_down))
@ -28,13 +25,12 @@ void userInterface_endState(UiState *state, InputBuffer *input)
state->keyChar = keycode_null;
}
i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager,
Renderer *const renderer, Font *const font,
const InputBuffer input, const i32 id, const Rect rect,
const char *const label)
i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer,
Font *const font, const InputBuffer input, const i32 id,
const Rect rect, const char *const label)
{
if (math_rect_contains_p(rect, input.mouseP))
if (math_rectContainsP(rect, input.mouseP))
{
uiState->hotItem = id;
if (uiState->activeItem == 0)
@ -45,7 +41,6 @@ i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
uiState->activeItem = id;
}
}
}
#if 0
@ -61,7 +56,7 @@ i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
if (uiState->activeItem == id)
{
buttonOffset = V2(1, 1);
buttonColor = V4(0.8f, 0.8f, 0.8f, 1);
buttonColor = V4(0.8f, 0.8f, 0.8f, 1);
}
else
{
@ -71,27 +66,25 @@ i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
}
/* If no widget has keyboard focus, take it */
if (uiState->kbdItem == 0)
uiState->kbdItem = id;
if (uiState->kbdItem == 0) uiState->kbdItem = id;
v2 rectSize = math_rect_get_size(rect);
v2 rectSize = math_rectGetSize(rect);
/* If we have keyboard focus, show it */
if (uiState->kbdItem == id)
{
// Draw outline
renderer_staticRect(renderer,
v2_add(V2(-2, -2), v2_add(buttonOffset, rect.min)),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL,
buttonColor, renderflag_no_texture);
renderer_rectFixed(renderer,
v2_add(V2(-2, -2), v2_add(buttonOffset, rect.min)),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL,
buttonColor, renderflag_no_texture);
}
renderer_staticRect(renderer, v2_add(buttonOffset, rect.min),
rectSize, V2(0, 0), 0, NULL,
buttonColor, renderflag_no_texture);
renderer_rectFixed(renderer, v2_add(buttonOffset, rect.min), rectSize,
V2(0, 0), 0, NULL, buttonColor, renderflag_no_texture);
if (label)
{
v2 labelDim = asset_stringDimInPixels(font, label);
v2 labelDim = asset_fontStringDimInPixels(font, label);
v2 labelPos = rect.min;
// Initially position the label to half the width of the button
@ -104,14 +97,14 @@ i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
if (labelDim.h < rectSize.h)
{
labelPos.y += (rectSize.h * 0.5f);
labelPos.y -= (CAST(f32)labelDim.h * 0.5f);
labelPos.y -= (CAST(f32) labelDim.h * 0.5f);
}
// TODO(doyle): We're using odd colors to overcome z-sorting by forcing
// button text into another render group
labelPos = v2_add(labelPos, buttonOffset);
renderer_staticString(renderer, arena, font, label, labelPos, V2(0, 0),
0, V4(0.9f, 0.9f, 0.9f, 0.9f), 0);
renderer_stringFixed(renderer, arena, font, label, labelPos, V2(0, 0),
0, V4(0.9f, 0.9f, 0.9f, 0.9f), 0);
}
// After renderering before click check, see if we need to process keys
@ -149,17 +142,16 @@ i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
return 0;
}
i32 userInterface_scrollbar(UiState *const uiState,
AssetManager *const assetManager,
Renderer *const renderer, const InputBuffer input,
const i32 id, const Rect scrollBarRect,
i32 *const value, const i32 maxValue)
i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
Renderer *const renderer, const InputBuffer input,
const i32 id, const Rect scrollBarRect, i32 *const value,
const i32 maxValue)
{
#ifdef DENGINE_DEBUG
ASSERT(*value <= maxValue);
#endif
if (math_rect_contains_p(scrollBarRect, input.mouseP))
if (math_rectContainsP(scrollBarRect, input.mouseP))
{
uiState->hotItem = id;
if (uiState->activeItem == 0)
@ -175,39 +167,38 @@ i32 userInterface_scrollbar(UiState *const uiState,
RenderTex renderTex = renderer_createNullRenderTex(assetManager);
/* If no widget has keyboard focus, take it */
if (uiState->kbdItem == 0)
uiState->kbdItem = id;
if (uiState->kbdItem == 0) uiState->kbdItem = id;
v2 rectSize = math_rect_get_size(scrollBarRect);
v2 rectSize = math_rectGetSize(scrollBarRect);
/* If we have keyboard focus, show it */
if (uiState->kbdItem == id)
{
// Draw outline
renderer_staticRect(renderer, v2_add(V2(-2, -2), scrollBarRect.min),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0,
&renderTex, V4(1, 0, 0, 1), 0);
renderer_rectFixed(renderer, v2_add(V2(-2, -2), scrollBarRect.min),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, &renderTex,
V4(1, 0, 0, 1), 0);
}
// Render scroll bar background
renderer_staticRect(renderer, scrollBarRect.min, rectSize,
V2(0, 0), 0, &renderTex, V4(0.75f, 0.5f, 0.5f, 1), 0);
renderer_rectFixed(renderer, scrollBarRect.min, rectSize, V2(0, 0), 0,
&renderTex, V4(0.75f, 0.5f, 0.5f, 1), 0);
// Render scroll bar slider
v2 sliderSize = V2(16, 16);
v4 sliderColor = V4(0, 0, 0, 1);
v2 sliderSize = V2(16, 16);
v4 sliderColor = V4(0, 0, 0, 1);
f32 sliderPercentageOffset = (CAST(f32) *value / CAST(f32) maxValue);
f32 sliderPercentageOffset = (CAST(f32) * value / CAST(f32) maxValue);
f32 sliderYOffsetToBar =
(rectSize.h - sliderSize.h) * sliderPercentageOffset;
v2 sliderPos = v2_add(scrollBarRect.min, V2(0, sliderYOffsetToBar));
v2 sliderPos = v2_add(scrollBarRect.min, V2(0, sliderYOffsetToBar));
if (uiState->hotItem == id || uiState->activeItem == id)
sliderColor = V4(1.0f, 0, 0, 1);
else
sliderColor = V4(0.0f, 1.0f, 0, 1);
renderer_staticRect(renderer, sliderPos, sliderSize, V2(0, 0), 0, &renderTex,
sliderColor, 0);
renderer_rectFixed(renderer, sliderPos, sliderSize, V2(0, 0), 0, &renderTex,
sliderColor, 0);
if (uiState->kbdItem == id)
{
@ -222,7 +213,8 @@ i32 userInterface_scrollbar(UiState *const uiState,
uiState->keyEntered = keycode_null;
break;
case keycode_up:
// TODO(doyle): Fix input for this to work, i.e. proper rate limited input poll
// TODO(doyle): Fix input for this to work, i.e. proper rate limited
// input poll
if (*value < maxValue)
{
(*value)++;
@ -251,10 +243,9 @@ i32 userInterface_scrollbar(UiState *const uiState,
else if (mouseYRelToRect > rectSize.h)
mouseYRelToRect = rectSize.h;
f32 newSliderPercentOffset =
(CAST(f32) mouseYRelToRect / rectSize.h);
f32 newSliderPercentOffset = (CAST(f32) mouseYRelToRect / rectSize.h);
i32 newValue = CAST(i32)(newSliderPercentOffset * CAST(f32)maxValue);
i32 newValue = CAST(i32)(newSliderPercentOffset * CAST(f32) maxValue);
if (newValue != *value)
{
*value = newValue;
@ -265,16 +256,15 @@ i32 userInterface_scrollbar(UiState *const uiState,
return 0;
}
i32 userInterface_textField(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager,
Renderer *const renderer, Font *const font,
InputBuffer input, const i32 id, const Rect rect,
char *const string)
i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer,
Font *const font, InputBuffer input, const i32 id,
const Rect rect, char *const string)
{
i32 strLen = common_strlen(string);
i32 strLen = common_strlen(string);
b32 changed = FALSE;
if (math_rect_contains_p(rect, input.mouseP))
if (math_rectContainsP(rect, input.mouseP))
{
uiState->hotItem = id;
if (uiState->activeItem == 0)
@ -288,37 +278,36 @@ i32 userInterface_textField(UiState *const uiState, MemoryArena_ *const arena,
}
/* If no widget has keyboard focus, take it */
if (uiState->kbdItem == 0)
uiState->kbdItem = id;
if (uiState->kbdItem == 0) uiState->kbdItem = id;
v2 rectSize = math_rect_get_size(rect);
v2 rectSize = math_rectGetSize(rect);
/* If we have keyboard focus, show it */
if (uiState->kbdItem == id)
{
// Draw outline
renderer_staticRect(renderer, v2_add(V2(-2, -2), rect.min),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0,
NULL, V4(1.0f, 0, 0, 1), 0);
renderer_rectFixed(renderer, v2_add(V2(-2, -2), rect.min),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL,
V4(1.0f, 0, 0, 1), 0);
}
// Render text field
renderer_staticRect(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.75f, 0.5f, 0.5f, 1), 0);
renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.75f, 0.5f, 0.5f, 1), 0);
if (uiState->activeItem == id || uiState->hotItem == id)
{
renderer_staticRect(renderer, rect.min, rectSize, V2(0, 0), 0,
NULL, V4(0.75f, 0.75f, 0.0f, 1), 0);
renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.75f, 0.75f, 0.0f, 1), 0);
}
else
{
renderer_staticRect(renderer, rect.min, rectSize, V2(0, 0), 0,
NULL, V4(0.5f, 0.5f, 0.5f, 1), 0);
renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.5f, 0.5f, 0.5f, 1), 0);
}
v2 strPos = rect.min;
renderer_staticString(renderer, arena, font, string, strPos, V2(0, 0), 0,
V4(0, 0, 0, 1), 0);
renderer_stringFixed(renderer, arena, font, string, strPos, V2(0, 0), 0,
V4(0, 0, 0, 1), 0);
if (uiState->kbdItem == id)
{
@ -336,7 +325,7 @@ i32 userInterface_textField(UiState *const uiState, MemoryArena_ *const arena,
if (strLen > 0)
{
string[--strLen] = 0;
changed = TRUE;
changed = TRUE;
}
break;
default:
@ -347,8 +336,8 @@ i32 userInterface_textField(UiState *const uiState, MemoryArena_ *const arena,
uiState->keyChar <= keycode_tilda && strLen < 30)
{
string[strLen++] = uiState->keyChar + ' ';
string[strLen] = 0;
changed = TRUE;
string[strLen] = 0;
changed = TRUE;
}
}

View File

@ -1,10 +1,10 @@
#include "Dengine/AssetManager.h"
#include "Dengine/Asteroid.h"
#include "Dengine/Common.h"
#include "Dengine/Debug.h"
#include "Dengine/Math.h"
#include "Dengine/OpenGL.h"
#include "Dengine/MemoryArena.h"
#include "Dengine/Asteroid.h"
#include "Dengine/OpenGL.h"
INTERNAL inline void processKey(KeyState *state, int action)
{

View File

@ -2,8 +2,6 @@
#define DENGINE_ASSET_MANAGER_H
#include "Dengine/Assets.h"
#include "Dengine/Shader.h"
#include "Dengine/Texture.h"
/* Forward declaration */
typedef struct MemoryArena MemoryArena_;
@ -33,15 +31,15 @@ void asset_init(AssetManager *assetManager, MemoryArena_ *arena);
* Texture Operations
*********************************
*/
const SubTexture asset_getAtlasSubTex(TexAtlas *const atlas,
const SubTexture asset_atlasGetSubTex(TexAtlas *const atlas,
const char *const key);
Texture *asset_getTex(AssetManager *const assetManager, const char *const key);
TexAtlas *asset_getFreeTexAtlasSlot(AssetManager *const assetManager,
Texture *asset_texGet(AssetManager *const assetManager, const char *const key);
TexAtlas *asset_atlasGetFreeSlot(AssetManager *const assetManager,
MemoryArena_ *arena, const char *const key,
i32 numSubTex);
TexAtlas *asset_getTexAtlas(AssetManager *const assetManager,
TexAtlas *asset_atlasGet(AssetManager *const assetManager,
const char *const key);
Texture *asset_getFreeTexSlot(AssetManager *const assetManager,
Texture *asset_texGetFreeSlot(AssetManager *const assetManager,
MemoryArena_ *const arena, const char *const key);
Texture *asset_loadTextureImage(AssetManager *assetManager, MemoryArena_ *arena,
const char *const path, const char *const key);
@ -51,11 +49,11 @@ Texture *asset_loadTextureImage(AssetManager *assetManager, MemoryArena_ *arena,
* Animation Asset Managing
*********************************
*/
void asset_addAnimation(AssetManager *const assetManager,
void asset_animAdd(AssetManager *const assetManager,
MemoryArena_ *const arena, const char *const animName,
TexAtlas *const atlas, char **const subTextureNames,
const i32 numSubTextures, const f32 frameDuration);
Animation *asset_getAnim(AssetManager *const assetManager,
Animation *asset_animGet(AssetManager *const assetManager,
const char *const key);
/*
@ -63,9 +61,9 @@ Animation *asset_getAnim(AssetManager *const assetManager,
* Audio
*********************************
*/
AudioVorbis *const asset_getVorbis(AssetManager *const assetManager,
AudioVorbis *const asset_vorbisGet(AssetManager *const assetManager,
const char *const key);
const i32 asset_loadVorbis(AssetManager *assetManager, MemoryArena_ *arena,
const i32 asset_vorbisLoad(AssetManager *assetManager, MemoryArena_ *arena,
const char *const path, const char *const key);
/*
@ -73,28 +71,28 @@ const i32 asset_loadVorbis(AssetManager *assetManager, MemoryArena_ *arena,
* Everything else
*********************************
*/
const i32 asset_loadXmlFile(AssetManager *const assetManager,
const i32 asset_xmlLoad(AssetManager *const assetManager,
MemoryArena_ *const arena,
const PlatformFileRead *const fileRead);
u32 asset_getShader(AssetManager *assetManager, const enum ShaderList type);
const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
u32 asset_shaderGet(AssetManager *assetManager, const enum ShaderList type);
const i32 asset_shaderLoad(AssetManager *assetManager, MemoryArena_ *arena,
const char *const vertexPath,
const char *const fragmentPath,
const enum ShaderList type);
Font *asset_getFontCreateSizeOnDemand(AssetManager *assetManager,
Font *asset_fontGetOrCreateOnDemand(AssetManager *assetManager,
MemoryArena_ *persistentArena,
MemoryArena_ *transientArena, char *name,
i32 size);
Font *asset_getFont(AssetManager *assetManager, char *name,
Font *asset_fontGet(AssetManager *assetManager, char *name,
i32 size);
const i32 asset_loadTTFont(AssetManager *assetManager,
const i32 asset_fontLoadTTF(AssetManager *assetManager,
MemoryArena_ *persistentArena,
MemoryArena_ *transientArena, char *filePath,
char *name, i32 targetFontHeight);
const v2 asset_stringDimInPixels(const Font *const font,
const v2 asset_fontStringDimInPixels(const Font *const font,
const char *const string);
void asset_unitTest(MemoryArena_ *arena);

View File

@ -123,6 +123,34 @@ typedef struct Animation
f32 frameDuration;
} Animation;
#define TARGET_TEXTURE_SIZE 1024
#define TARGET_BYTES_PER_PIXEL 4
// TODO(doyle): Look into merging into assets.h file ..
typedef struct Texture
{
// Holds the ID of the texture object, used for all texture operations to
// reference to this particlar texture
u32 id;
// Texture image dimensions
u32 width;
u32 height;
// Texture Format
u32 internalFormat; // Format of texture object
u32 imageFormat; // Format of loaded image
// Texture configuration
u32 wrapS; // Wrapping mode on S axis
u32 wrapT; // Wrapping mode on T axis
// Filtering mode if texture pixels < screen pixels
u32 filterMinification;
// Filtering mode if texture pixels > screen pixels
u32 filterMagnification;
} Texture;
/*
*********************************
* Font

View File

@ -8,7 +8,7 @@
#include "Dengine/MemoryArena.h"
#include "Dengine/Platform.h"
#include "Dengine/Renderer.h"
#include "Dengine/UserInterface.h"
#include "Dengine/Ui.h"
enum AppState
{

View File

@ -49,17 +49,17 @@ typedef struct AudioRenderer
const i32 audio_init(AudioManager *audioManager);
const i32 audio_playVorbis(MemoryArena_ *arena, AudioManager *audioManager,
const i32 audio_vorbisPlay(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer, AudioVorbis *vorbis,
i32 numPlays);
const i32 audio_streamPlayVorbis(MemoryArena_ *arena, AudioManager *audioManager,
const i32 audio_vorbisStream(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer,
AudioVorbis *vorbis, i32 numPlays);
const i32 audio_stopVorbis(MemoryArena_ *arena, AudioManager *audioManager,
const i32 audio_vorbisStop(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer);
const i32 audio_pauseVorbis(AudioManager *audioManager,
const i32 audio_vorbisPause(AudioManager *audioManager,
AudioRenderer *audioRenderer);
const i32 audio_resumeVorbis(AudioManager *audioManager,
const i32 audio_vorbisResume(AudioManager *audioManager,
AudioRenderer *audioRenderer);
const i32 audio_updateAndPlay(MemoryArena_ *arena, AudioManager *audioManager,
AudioRenderer *audioRenderer);

View File

@ -110,10 +110,10 @@ typedef struct Entity
i32 animListIndex;
} 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,
SubTexture entity_subTexGetCurr(Entity *const entity);
void entity_animSet(Entity *const entity, const char *const animName);
void entity_animUpdate(Entity *const entity, const f32 dt);
void entity_animAdd(AssetManager *const assetManager, Entity *const entity,
const char *const animName);
v2 *entity_generateUpdatedVertexList(MemoryArena_ *transientArena,

View File

@ -331,7 +331,7 @@ INTERNAL inline mat4 mat4_mul(const mat4 a, const mat4 b)
}
INTERNAL inline v4 mat4_mul_v4(const mat4 a, const v4 b)
INTERNAL inline v4 mat4_mulV4(const mat4 a, const v4 b)
{
v4 result = {0};
@ -347,7 +347,7 @@ INTERNAL inline v4 mat4_mul_v4(const mat4 a, const v4 b)
return result;
}
INTERNAL inline b32 math_rect_contains_p(Rect rect, v2 p)
INTERNAL inline b32 math_rectContainsP(Rect rect, v2 p)
{
b32 outsideOfRectX = FALSE;
if (p.x < rect.min.x || p.x > rect.max.w)
@ -362,7 +362,7 @@ INTERNAL inline b32 math_rect_contains_p(Rect rect, v2 p)
}
INTERNAL inline Rect math_rect_create(v2 origin, v2 size)
INTERNAL inline Rect math_rectCreate(v2 origin, v2 size)
{
Rect result = {0};
result.min = origin;
@ -371,7 +371,7 @@ INTERNAL inline Rect math_rect_create(v2 origin, v2 size)
return result;
}
INTERNAL inline v2 math_rect_get_size(Rect rect)
INTERNAL inline v2 math_rectGetSize(Rect rect)
{
f32 width = ABS(rect.max.x - rect.min.x);
f32 height = ABS(rect.max.y - rect.min.y);
@ -381,7 +381,7 @@ INTERNAL inline v2 math_rect_get_size(Rect rect)
return result;
}
INTERNAL inline v2 math_rect_get_centre(Rect rect)
INTERNAL inline v2 math_rectGetCentre(Rect rect)
{
f32 sumX = rect.min.x + rect.max.x;
f32 sumY = rect.min.y + rect.max.y;
@ -390,7 +390,7 @@ INTERNAL inline v2 math_rect_get_centre(Rect rect)
return result;
}
INTERNAL inline Rect math_rect_shift(Rect rect, v2 shift)
INTERNAL inline Rect math_rectShift(Rect rect, v2 shift)
{
Rect result = {0};
result.min = v2_add(rect.min, shift);

View File

@ -29,8 +29,8 @@ typedef struct TempMemory
MemoryIndex used;
} TempMemory;
TempMemory memory_begin_temporary_region(MemoryArena_ *arena);
void memory_end_temporary_region(TempMemory tempMemory);
TempMemory memory_beginTempRegion(MemoryArena_ *arena);
void memory_endTempRegion(TempMemory tempMemory);
#define MEMORY_PUSH_STRUCT(arena, type) (type *)memory_pushBytes(arena, sizeof(type))
#define MEMORY_PUSH_ARRAY(arena, count, type) (type *)memory_pushBytes(arena, (count)*sizeof(type))

View File

@ -184,7 +184,7 @@ enum ReadKeyType
readkeytype_repeat,
readkeytype_count,
};
void platform_processInputBuffer(InputBuffer *inputBuffer, f32 dt);
void platform_inputBufferProcess(InputBuffer *inputBuffer, f32 dt);
b32 platform_queryKey(KeyState *key, enum ReadKeyType readType,
f32 delayInterval);
#endif

View File

@ -100,7 +100,7 @@ void renderer_polygon(Renderer *const renderer, Rect camera, v2 *polygonPoints,
i32 numPoints, v2 pivotPoint, Radians rotate,
RenderTex *renderTex, v4 color, RenderFlags flags);
inline void renderer_staticRect(Renderer *const renderer, v2 pos, v2 size,
inline void renderer_rectFixed(Renderer *const renderer, v2 pos, v2 size,
v2 pivotPoint, Radians rotate,
RenderTex *renderTex, v4 color,
RenderFlags flags)
@ -115,7 +115,7 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
v2 pivotPoint, Radians rotate, v4 color,
RenderFlags flags);
inline void renderer_staticString(Renderer *const renderer, MemoryArena_ *arena,
inline void renderer_stringFixed(Renderer *const renderer, MemoryArena_ *arena,
Font *const font, const char *const string,
v2 pos, v2 pivotPoint, Radians rotate,
v4 color, RenderFlags flags)

View File

@ -1,16 +0,0 @@
#ifndef DENGINE_SHADER_H
#define DENGINE_SHADER_H
#include "Dengine/Math.h"
#include "Dengine/OpenGL.h"
void shader_uniformSet1i(u32 shaderId, const GLchar *name,
const GLuint data);
void shader_uniformSetMat4fv(u32 shaderId, const GLchar *name,
mat4 data);
void shader_uniformSetVec4f(u32 shaderId, const GLchar *name,
v4 data);
void shader_use(u32 shaderId);
#endif

View File

@ -1,39 +0,0 @@
#ifndef DENGINE_TEXTURE_H
#define DENGINE_TEXTURE_H
#include "Dengine/Common.h"
#include "Dengine/OpenGL.h"
#define TARGET_TEXTURE_SIZE 1024
#define TARGET_BYTES_PER_PIXEL 4
// TODO(doyle): Look into merging into assets.h file ..
typedef struct Texture
{
// Holds the ID of the texture object, used for all texture operations to
// reference to this particlar texture
GLuint id;
// Texture image dimensions
GLuint width;
GLuint height;
// Texture Format
GLuint internalFormat; // Format of texture object
GLuint imageFormat; // Format of loaded image
// Texture configuration
GLuint wrapS; // Wrapping mode on S axis
GLuint wrapT; // Wrapping mode on T axis
// Filtering mode if texture pixels < screen pixels
GLuint filterMinification;
// Filtering mode if texture pixels > screen pixels
GLuint filterMagnification;
} Texture;
// Generates texture from image data
Texture texture_gen(const GLuint width, const GLuint height,
const GLint bytesPerPixel, const u8 *const image);
#endif

78
src/include/Dengine/Ui.h Normal file
View File

@ -0,0 +1,78 @@
#ifndef DENGINE_USER_INTERFACE_H
#define DENGINE_USER_INTERFACE_H
#include "Dengine/Common.h"
#include "Dengine/Math.h"
#include "Dengine/Platform.h"
/* Forward Declaration */
typedef struct AssetManager AssetManager;
typedef struct Font Font;
typedef struct MemoryArena MemoryArena_;
typedef struct Renderer Renderer;
typedef struct GameState GameState;
enum UiType
{
uitype_button,
uitype_scrollbar,
uitype_textField,
uitype_string,
uitype_count,
};
typedef struct UiItem
{
i32 id;
char label[64];
enum UiType type;
Rect rect;
// TODO(doyle): ECS this? Not all elements need
i32 value;
i32 maxValue;
char string[80];
} UiItem;
typedef struct UiState
{
i32 uniqueId;
UiItem uiList[128];
i32 numItems;
i32 hotItem;
i32 activeItem;
i32 lastWidget;
i32 kbdItem;
enum KeyCode keyEntered;
enum KeyCode keyMod;
enum KeyCode keyChar;
} UiState;
inline i32 ui_generateId(UiState *const uiState)
{
i32 result = uiState->uniqueId++;
return result;
}
void ui_beginState(UiState *state);
void ui_endState(UiState *state, InputBuffer *input);
i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer,
Font *const font, const InputBuffer input, const i32 id,
const Rect rect, const char *const label);
i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer,
Font *const font, InputBuffer input, const i32 id,
const Rect rect, char *const string);
i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
Renderer *const renderer, const InputBuffer input,
const i32 id, const Rect scrollBarRect, i32 *const value,
const i32 maxValue);
#endif

View File

@ -1,81 +0,0 @@
#ifndef DENGINE_USER_INTERFACE_H
#define DENGINE_USER_INTERFACE_H
#include "Dengine/Common.h"
#include "Dengine/Math.h"
#include "Dengine/Platform.h"
/* Forward Declaration */
typedef struct AssetManager AssetManager;
typedef struct Font Font;
typedef struct MemoryArena MemoryArena_;
typedef struct Renderer Renderer;
typedef struct GameState GameState;
enum UiType
{
uitype_button,
uitype_scrollbar,
uitype_textField,
uitype_string,
uitype_count,
};
typedef struct UiItem
{
i32 id;
char label[64];
enum UiType type;
Rect rect;
// TODO(doyle): ECS this? Not all elements need
i32 value;
i32 maxValue;
char string[80];
} UiItem;
typedef struct UiState
{
i32 uniqueId;
UiItem uiList[128];
i32 numItems;
i32 hotItem;
i32 activeItem;
i32 lastWidget;
i32 kbdItem;
enum KeyCode keyEntered;
enum KeyCode keyMod;
enum KeyCode keyChar;
} UiState;
inline i32 userInterface_generateId(UiState *const uiState)
{
i32 result = uiState->uniqueId++;
return result;
}
void userInterface_beginState(UiState *state);
void userInterface_endState(UiState *state, InputBuffer *input);
i32 userInterface_button(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager,
Renderer *const renderer, Font *const font,
const InputBuffer input, const i32 id, const Rect rect,
const char *const label);
i32 userInterface_textField(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager,
Renderer *const renderer, Font *const font,
InputBuffer input, const i32 id, const Rect rect,
char *const string);
i32 userInterface_scrollbar(UiState *const uiState,
AssetManager *const assetManager,
Renderer *const renderer, const InputBuffer input,
const i32 id, const Rect scrollBarRect,
i32 *const value, const i32 maxValue);
#endif