diff --git a/include/dawn/dawn.h b/include/dawn/dawn.h index c7e006d2..3579828b 100644 --- a/include/dawn/dawn.h +++ b/include/dawn/dawn.h @@ -42,6 +42,7 @@ #include "game/poker/pokergame.h" #include "game/poker/pokerdiscussion.h" #include "game/poker/pokergameassets.h" +#include "game/poker/pokerui.h" // Player Input #include "input/input.h" @@ -57,6 +58,9 @@ #include "poker/poker.h" #include "poker/winner.h" +// User Interface Objects +#include "ui/label.h" + // Utility Objects #include "util/array.h" #include "util/list.h" diff --git a/include/dawn/display/gui/font.h b/include/dawn/display/gui/font.h index 56cb4125..8c36a413 100644 --- a/include/dawn/display/gui/font.h +++ b/include/dawn/display/gui/font.h @@ -24,8 +24,11 @@ /** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */ #define FONT_FILL_MODE 1 -/** Passed to STBTT for scaling the font nicely, essentially the font size. */ -#define FONT_TEXTURE_SIZE 32 +/** Passed to STBTT for scaling the font, essentially the font resolution */ +#define FONT_TEXTURE_SIZE 64 + +/** Default Font Size (on which all font scales are based) */ +#define FONT_SIZE_DEFAULT (FONT_TEXTURE_SIZE / 2) // Chars #define FONT_NEWLINE '\n' diff --git a/include/dawn/game/dawn/dawngame.h b/include/dawn/game/dawn/dawngame.h index 3480f19e..7318cbd3 100644 --- a/include/dawn/game/dawn/dawngame.h +++ b/include/dawn/game/dawn/dawngame.h @@ -9,5 +9,5 @@ #include "../../libs.h" typedef struct { - + int32_t INeedSomePropertyToStopCompilerComplaining; } dawngame_t; \ No newline at end of file diff --git a/include/dawn/game/poker/pokerui.h b/include/dawn/game/poker/pokerui.h new file mode 100644 index 00000000..ed2cf7d7 --- /dev/null +++ b/include/dawn/game/poker/pokerui.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../libs.h" +#include "../../ui/label.h" + +typedef struct { + label_t labelTest; +} pokerui_t; \ No newline at end of file diff --git a/include/dawn/settings.h b/include/dawn/settings.h index eaf09c3a..00dd2e4f 100644 --- a/include/dawn/settings.h +++ b/include/dawn/settings.h @@ -11,4 +11,4 @@ #define SETTING_GAME_DAWN 2 // Settings -#define SETTING_GAME SETTING_GAME_DAWN \ No newline at end of file +#define SETTING_GAME SETTING_GAME_POKER \ No newline at end of file diff --git a/include/dawn/ui/label.h b/include/dawn/ui/label.h new file mode 100644 index 00000000..7639b1b5 --- /dev/null +++ b/include/dawn/ui/label.h @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../libs.h" +#include "../display/primitive.h" +#include "../display/gui/font.h" + +/** Representation of a Label UI Element */ +typedef struct { + font_t *font; + float x, y, z; + float fontSize; + fonttextinfo_t info; + primitive_t primitive; +} label_t; \ No newline at end of file diff --git a/include/dawn/vn/vntextbox.h b/include/dawn/vn/vntextbox.h index 358de19a..ce67ba6e 100644 --- a/include/dawn/vn/vntextbox.h +++ b/include/dawn/vn/vntextbox.h @@ -13,6 +13,8 @@ /** Amount of characters scrolled, per second */ #define VN_TEXTBOX_SCROLL_SPEED 60 +#define VN_TEXTBOX_FONT_SIZE 16.0 + #define VN_TEXTBOX_STATE_CLOSED 0x01 typedef struct { diff --git a/src/display/gui/font.c b/src/display/gui/font.c index 1e236f8d..3da323c8 100644 --- a/src/display/gui/font.c +++ b/src/display/gui/font.c @@ -44,7 +44,11 @@ void fontDispose(font_t *font) { textureDispose(&font->texture); } -void fontTextBuffer(font_t *font,primitive_t *primitive,fonttextinfo_t *info) { +float fontGetScale(float fontSize) { + return fontSize / FONT_SIZE_DEFAULT; +} + +void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info){ stbtt_aligned_quad *quad; int32_t i; @@ -66,13 +70,19 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info) { } void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, - float maxWidth + float maxWidth, float fontSize ) { int32_t i, j, wordIndex; char c; - float x, y, wordX; + float x, y, wordX, scale; stbtt_aligned_quad *quad; + // Get the font scale + scale = fontGetScale(fontSize); + + // Adjust the max width to match the scale, and allow "no max width". + maxWidth = maxWidth == -1 ? 999999 : maxWidth * (1 / scale); + /** Which index in the original text var is the current word from */ int32_t wordStart = 0; @@ -98,7 +108,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, // When space, start of new word about to begin if(c == FONT_SPACE) { - x += FONT_SPACE_SIZE; + x += FONT_SPACE_SIZE * scale; // Did this space cause a newline? if(x > maxWidth) { @@ -106,7 +116,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, info->lines[info->lineCount].start = info->realLength; info->lines[info->lineCount].length = 0; - y += FONT_LINE_HEIGHT; + y += FONT_LINE_HEIGHT * scale; x = 0; } wordX = x; @@ -121,7 +131,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, info->lines[info->lineCount].length = 0; wordStart = info->realLength; - y += FONT_LINE_HEIGHT; + y += FONT_LINE_HEIGHT * scale; x = 0; continue; } @@ -139,8 +149,10 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, x = quad->x1 - wordX; for(j = wordStart; j <= info->realLength; j++) { quad = info->quads + j; - quad->x0 -= wordX, quad->x1 -= wordX; - quad->y0 += FONT_LINE_HEIGHT, quad->y1 += FONT_LINE_HEIGHT; + quad->x0 -= wordX; + quad->x1 -= wordX; + quad->y0 += FONT_LINE_HEIGHT; + quad->y1 += FONT_LINE_HEIGHT; } // Go back to the previous (still current) line and remove the chars @@ -163,6 +175,16 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, for(j = 0; j < info->realLength; j++) { quad = info->quads + j; + + // Scale the Quad + if(scale != 1.0) { + quad->x0 *= scale; + quad->x1 *= scale; + quad->y0 *= scale; + quad->y1 *= scale; + } + + // Update the dimensions. info->width = mathMax(info->width, quad->x1); info->height = mathMax(info->height, quad->y1); } diff --git a/src/display/gui/font.h b/src/display/gui/font.h index 65c8d950..954da3ae 100644 --- a/src/display/gui/font.h +++ b/src/display/gui/font.h @@ -45,6 +45,14 @@ void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info); */ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info); +/** + * Convert a Font's Size into a Font Scale. The scale is based on the font's + * default size for the given texture size (Refer to FONT_SIZE_DEFAULT). + * + * @param fontSize Font size to convert. + * @return The times scale that the font size is to the textured default. + */ +float fontGetScale(float fontSize); /** * Clamps text to a max width, inserting newlines where possible. @@ -52,10 +60,11 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info); * @param font Font to use * @param info Font text info to clamp into. * @param text Text to clamp. - * @param maxWidth Max width (in pixels) to clamp the text to. + * @param maxWidth Max width (pixels) to clamp the text to, -1 for no max width. + * @param fontSize Font Size of the text. */ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text, - float maxWidth + float maxWidth, float fontSize ); /** diff --git a/src/game/dawn/dawngame.c b/src/game/dawn/dawngame.c new file mode 100644 index 00000000..df09ceaf --- /dev/null +++ b/src/game/dawn/dawngame.c @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "dawngame.h" + +bool dawnGameInit(game_t *game) { + return true; +} + +void dawnGameUpdate(game_t *game) { + +} + +void dawnGameDispose(game_t *game) { + +} \ No newline at end of file diff --git a/src/game/poker/pokergame.c b/src/game/poker/pokergame.c index 46e55014..0b8a79c6 100644 --- a/src/game/poker/pokergame.c +++ b/src/game/poker/pokergame.c @@ -7,6 +7,8 @@ #include "pokergame.h" +label_t label; + bool pokerGameInit(game_t *game) { pokergame_t *pokerGame = &game->pokerGame; @@ -20,6 +22,11 @@ bool pokerGameInit(game_t *game) { vnSceneInit(&pokerGame->scene, &pokerGame->assets.font); pokerGameActionStartAdd(pokerGame); + labelInit(&label); + label.y = 32; + label.fontSize = 8.0; + labelSetText(&label, &pokerGame->assets.font, "Hello World"); + // pokerActionMatchAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); // pokerActionMatchAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); // pokerActionRoundAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); @@ -48,6 +55,7 @@ void pokerGameUpdate(game_t *game) { // Render the visual novel scene vnSceneRenderWorld(&pokerGame->scene, &game->engine, &pokerGame->assets.shader); vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader); + labelRender(&label, &pokerGame->assets.shader); } void pokerGameDispose(game_t *game) { diff --git a/src/game/poker/pokergame.h b/src/game/poker/pokergame.h index f9e6ca28..689861c6 100644 --- a/src/game/poker/pokergame.h +++ b/src/game/poker/pokergame.h @@ -13,6 +13,8 @@ #include "../../vn/vnscene.h" #include "actions/start.h" +#include "../../ui/label.h" + /** * Initializes the game state for the poker game. * diff --git a/src/game/poker/pokerui.c b/src/game/poker/pokerui.c new file mode 100644 index 00000000..4fd70e2d --- /dev/null +++ b/src/game/poker/pokerui.c @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "pokerui.h" + +void pokerUiInit(pokerui_t *ui) { + +} \ No newline at end of file diff --git a/src/game/poker/pokerui.h b/src/game/poker/pokerui.h new file mode 100644 index 00000000..a534ab77 --- /dev/null +++ b/src/game/poker/pokerui.h @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include +#include "../../ui/label.h" + +void pokerUiInit(pokerui_t *ui); \ No newline at end of file diff --git a/src/ui/label.c b/src/ui/label.c new file mode 100644 index 00000000..595d5974 --- /dev/null +++ b/src/ui/label.c @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "label.h" + +void labelInit(label_t *label) { + label->x = 0; + label->y = 0; + label->z = 0; + label->fontSize = FONT_SIZE_DEFAULT; + label->font = NULL; + label->primitive.verticeCount = 0; +} + +void labelSetText(label_t *label, font_t *font, char *text) { + if(label->primitive.verticeCount != 0) { + primitiveDispose(&label->primitive); + label->primitive.verticeCount = 0; + } + + label->font = font; + fontTextClamp(font, &label->info, text, -1, 16.0); + fontTextInit(font, &label->primitive, &label->info); +} + +void labelRender(label_t *label, shader_t *shader) { + if(label->primitive.verticeCount == 0) return; + shaderUsePosition(shader, label->x, label->y, label->z, 0, 0, 0); + shaderUseTexture(shader, &label->font->texture); + primitiveDraw(&label->primitive, 0, -1); +} + +void labelDispose(label_t *label) { + primitiveDispose(&label->primitive); +} \ No newline at end of file diff --git a/src/ui/label.h b/src/ui/label.h new file mode 100644 index 00000000..40f2ba1a --- /dev/null +++ b/src/ui/label.h @@ -0,0 +1,18 @@ +// Copyright (c) 2021 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include +#include "../display/shader.h" +#include "../display/primitive.h" +#include "../display/gui/font.h" + +void labelInit(label_t *label); + +void labelSetText(label_t *label, font_t *font, char *text); + +void labelRender(label_t *label, shader_t *shader); + +void labelDispose(label_t *label); \ No newline at end of file diff --git a/src/vn/gui/vntextbox.c b/src/vn/gui/vntextbox.c index 3756c319..b37b2cf6 100644 --- a/src/vn/gui/vntextbox.c +++ b/src/vn/gui/vntextbox.c @@ -33,7 +33,9 @@ void vnTextBoxRebuffer(vntextbox_t *box) { if(box->text == NULL) return; // Rebuffer the text. - fontTextClamp(box->font, &box->textInfo, box->text, box->widthMax); + fontTextClamp( + box->font, &box->textInfo, box->text, box->widthMax, VN_TEXTBOX_FONT_SIZE + ); fontTextInit(box->font, &box->primitive, &box->textInfo); // Test "Background"