Built basic UI Grid.

This commit is contained in:
2021-09-07 09:07:53 -07:00
parent 6c10429dfb
commit 6c3bf3eeb0
13 changed files with 315 additions and 49 deletions

View File

@ -20,7 +20,7 @@ set(SETTING_GAME_POKER 1)
set(SETTING_GAME_DAWN 2) set(SETTING_GAME_DAWN 2)
set(SETTING_GAME_SANDBOX 3) set(SETTING_GAME_SANDBOX 3)
set(SETTING_GAME SETTING_GAME_POKER) set(SETTING_GAME SETTING_GAME_SANDBOX)
set(SETTING_GAME_NAME "DawnGame") set(SETTING_GAME_NAME "DawnGame")
################################## Targets ##################################### ################################## Targets #####################################

View File

@ -12,6 +12,7 @@
#define SHADER_UNI_PROJ "u_Proj" #define SHADER_UNI_PROJ "u_Proj"
#define SHADER_UNI_TEXT "u_Text" #define SHADER_UNI_TEXT "u_Text"
#define SHADER_UNI_MODL "u_Modl" #define SHADER_UNI_MODL "u_Modl"
#define SHADER_UNI_COLR "u_Colr"
/** Representation of a shader uniform */ /** Representation of a shader uniform */
typedef GLuint shaderuniform_t; typedef GLuint shaderuniform_t;
@ -41,4 +42,7 @@ typedef struct {
/** Uniform for the current model world position */ /** Uniform for the current model world position */
shaderuniform_t uniModl; shaderuniform_t uniModl;
/** Uniform for the color multiplier */
shaderuniform_t uniColr;
} shader_t; } shader_t;

View File

@ -25,3 +25,10 @@ typedef struct {
/** RGBA Color values */ /** RGBA Color values */
uint8_t r, g, b, a; uint8_t r, g, b, a;
} pixel_t; } pixel_t;
#define PIXEL_COLOR_WHITE ((pixel_t){ .r = 255, .g = 255, .b = 255, .a = 255 })
#define PIXEL_COLOR_RED ((pixel_t){ .r = 255, .g = 0, .b = 0, .a = 255 })
#define PIXEL_COLOR_GREEN ((pixel_t){ .r = 0, .g = 255, .b = 0, .a = 255 })
#define PIXEL_COLOR_BLUE ((pixel_t){ .r = 0, .g = 0, .b = 255, .a = 255 })
#define PIXEL_COLOR_BLACK ((pixel_t){ .r = 0, .g = 0, .b = 0, .a = 255 })
#define PIXEL_COLOR_TRANSPARENT ((pixel_t){ .r = 0, .g = 0, .b = 0, .a = 0 })

View File

@ -17,6 +17,7 @@
#define POKER_PLAYER_UI_IMAGE_DIST 0.8f #define POKER_PLAYER_UI_IMAGE_DIST 0.8f
#define POKER_PLAYER_UI_IMAGE_Y 0.1f #define POKER_PLAYER_UI_IMAGE_Y 0.1f
#define POKER_PLAYER_UI_PADDING 8 #define POKER_PLAYER_UI_PADDING 8
#define POKER_PLAYER_UI_CHIPS_ANIMATION_SPEED 0.5f
typedef struct { typedef struct {
label_t label; label_t label;

View File

@ -12,13 +12,20 @@
#include "../../display/font.h" #include "../../display/font.h"
#include "../../display/primitive.h" #include "../../display/primitive.h"
#include "../../display/renderlist.h" #include "../../display/renderlist.h"
#include "../../display/texture.h"
#define GRID_BRUH_COUNT 1
typedef struct {
float x, y, width, height;
} gridbruh_t;
typedef struct { typedef struct {
camera_t camera; camera_t camera;
renderlist_t list;
shader_t shader; primitive_t primitive;
texture_t texture; texture_t texture;
shader_t shader;
primitive_t cube;
gridbruh_t items[GRID_BRUH_COUNT];
} sandboxscene_t; } sandboxscene_t;

View File

@ -76,12 +76,14 @@ void shaderInit(shader_t *shader,
shader->uniView = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_VIEW); shader->uniView = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_VIEW);
shader->uniText = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_TEXT); shader->uniText = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_TEXT);
shader->uniModl = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_MODL); shader->uniModl = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_MODL);
shader->uniColr = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_COLR);
// Bind the shader // Bind the shader
shaderUse(shader); shaderUse(shader);
// Reset position // Reset position
shaderUsePosition(shader, 0, 0, 0, 0, 0, 0); shaderUsePosition(shader, 0, 0, 0, 0, 0, 0);
shaderUseColor(shader, PIXEL_COLOR_WHITE);
} }
void shaderDispose(shader_t *shader) { void shaderDispose(shader_t *shader) {
@ -144,4 +146,13 @@ void shaderUsePositionAndScale(shader_t *shader,
matrixScale(&matrix, scaleX, scaleY, scaleZ); matrixScale(&matrix, scaleX, scaleY, scaleZ);
shaderUseMatrix(shader, shader->uniModl, &matrix); shaderUseMatrix(shader, shader->uniModl, &matrix);
}
void shaderUseColor(shader_t *shader, pixel_t color) {
glUniform4f(shader->uniColr,
(float)color.r / 255.0f,
(float)color.g / 255.0f,
(float)color.b / 255.0f,
(float)color.a / 255.0f
);
} }

View File

@ -91,4 +91,7 @@ void shaderUsePositionAndScale(shader_t *shader,
float x, float y, float z, float x, float y, float z,
float pitch, float yaw, float roll, float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ float scaleX, float scaleY, float scaleZ
); );
void shaderUseColor(shader_t *shader, pixel_t color);

View File

@ -9,11 +9,9 @@
void pokerPlayerUiInit(pokerplayerui_t *ui) { void pokerPlayerUiInit(pokerplayerui_t *ui) {
labelInit(&ui->label); labelInit(&ui->label);
frameBufferInit(&ui->frame, frameBufferInit(&ui->frame,
POKER_PLAYER_UI_IMAGE_RESOLUTION, POKER_PLAYER_UI_IMAGE_RESOLUTION POKER_PLAYER_UI_IMAGE_RESOLUTION, POKER_PLAYER_UI_IMAGE_RESOLUTION
); );
quadInit(&ui->quad, 0, quadInit(&ui->quad, 0,
0, 0, 0, 1, 0, 0, 0, 1,
POKER_PLAYER_UI_IMAGE_SIZE, POKER_PLAYER_UI_IMAGE_SIZE, 1, 0 POKER_PLAYER_UI_IMAGE_SIZE, POKER_PLAYER_UI_IMAGE_SIZE, 1, 0
@ -24,11 +22,13 @@ void pokerPlayerUiUpdate(
pokerplayerui_t *ui, pokergame_t *game, shader_t *shader, int32_t playerIndex, pokerplayerui_t *ui, pokergame_t *game, shader_t *shader, int32_t playerIndex,
engine_t *engine engine_t *engine
) { ) {
camera_t camera; camera_t camera;
uint8_t seat; uint8_t seat;
pokerplayer_t *player;
float x, y, z; float x, y, z;
player = game->poker.players + playerIndex;
// Bind the frame buffer // Bind the frame buffer
frameBufferUse(&ui->frame, true); frameBufferUse(&ui->frame, true);

View File

@ -8,42 +8,53 @@
#include "pokerui.h" #include "pokerui.h"
void pokerUiInit(pokergame_t *pokerGame) { void pokerUiInit(pokergame_t *pokerGame) {
uint8_t i; uint8_t i, j;
j = 0;
for(i = 0; i < POKER_PLAYER_COUNT; i++) { for(i = 0; i < POKER_PLAYER_COUNT; i++) {
pokerPlayerUiInit(pokerGame->ui.player + i); if(i == POKER_PLAYER_HUMAN_INDEX) continue;
pokerPlayerUiInit(pokerGame->ui.player + j);
j++;
} }
} }
void pokerUiUpdate(pokergame_t *pokerGame, engine_t *engine) { void pokerUiUpdate(pokergame_t *pokerGame, engine_t *engine) {
uint8_t i; uint8_t i, j;
pokerplayerui_t *ui; pokerplayerui_t *ui;
j = 0;
for(i = 0; i < POKER_PLAYER_COUNT; i++) { for(i = 0; i < POKER_PLAYER_COUNT; i++) {
ui = pokerGame->ui.player + i; if(i == POKER_PLAYER_HUMAN_INDEX) continue;
ui = pokerGame->ui.player + j;
pokerPlayerUiUpdate(ui, pokerGame, &pokerGame->assets.shader, i, engine); pokerPlayerUiUpdate(ui, pokerGame, &pokerGame->assets.shader, i, engine);
j++;
} }
} }
void pokerUiRender(pokergame_t *pokerGame, engine_t *engine) { void pokerUiRender(pokergame_t *pokerGame, engine_t *engine) {
uint8_t i; uint8_t i, j;
pokerplayerui_t *ui; pokerplayerui_t *ui;
j = 0;
for(i = 0; i < POKER_PLAYER_COUNT; i++) { for(i = 0; i < POKER_PLAYER_COUNT; i++) {
ui = pokerGame->ui.player + i; if(i == POKER_PLAYER_HUMAN_INDEX) continue;
ui = pokerGame->ui.player + j;
pokerPlayerUiRender(ui, pokerGame, pokerPlayerUiRender(ui, pokerGame,
&pokerGame->assets.shader, &pokerGame->assets.font, i, &pokerGame->assets.shader, &pokerGame->assets.font, i,
engine->render.width, i * 75.0f engine->render.width, j * 75.0f
); );
j++;
} }
} }
void pokerUiDispose(pokergame_t *pokerGame) { void pokerUiDispose(pokergame_t *pokerGame) {
uint8_t i; uint8_t i, j;
j = 0;
for(i = 0; i < POKER_PLAYER_COUNT; i++) { for(i = 0; i < POKER_PLAYER_COUNT; i++) {
pokerPlayerUiDispose(pokerGame->ui.player + i); if(i == POKER_PLAYER_HUMAN_INDEX) continue;
pokerPlayerUiDispose(pokerGame->ui.player + j);
j++;
} }
} }

View File

@ -7,50 +7,75 @@
#include "sandboxscene.h" #include "sandboxscene.h"
void renderTest(renderlist_t *list, renderpass_t *pass, engine_t *engine, int32_t i) { grid_t grid;
sandboxscene_t *scene;
scene = (sandboxscene_t *)list->user;
shaderUsePosition(pass->shader, 0, 0, 0, engine->time.current, engine->time.current, 0); void gridTest(
shaderUseTexture(pass->shader, &scene->texture); void *user, int32_t i,
primitiveDraw(&scene->cube, 0, -1); float screenWidth, float screenHeight,
float x, float y,
float width, float height
) {
sandboxscene_t *game = (sandboxscene_t *)user;
game->items[i].x = x;
game->items[i].y = y;
game->items[i].width = width;
game->items[i].height = height;
} }
bool sandboxSceneInit(sandboxscene_t *game) { bool sandboxSceneInit(sandboxscene_t *game) {
int32_t passIndex; gridInit(&grid);
renderpass_t *pass; grid.user = (void *)game;
gridAddBreakpoint(&grid, 800, 2, 2, 8.0f, 8.0f);
gridAddBreakpoint(&grid, -1, 6, 6, 16.0f, 16.0f);
gridchild_t *child = gridAddChild(&grid);
child->onResize = &gridTest;
// Load assets
assetTextureLoad(&game->texture, "test_texture.png"); assetTextureLoad(&game->texture, "test_texture.png");
assetShaderLoad(&game->shader, "shaders/textured.vert", "shaders/textured.frag"); assetShaderLoad(&game->shader,
cubeInit(&game->cube, 1.0f, 1.0f, 1.0f); "shaders/textured.vert", "shaders/textured.frag"
);
// Initialize list quadInit(&game->primitive, 0,
renderListInit(&game->list, 1, 400, 400); 0,0,0,0,
game->list.user = game; 1,1,1,1
);
// Set up the pass
passIndex = renderPassAdd(&game->list);
pass = renderListGetPass(&game->list, passIndex);
pass->shader = &game->shader;
return true; return true;
} }
void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) { void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) {
dynarray_t items; cameraLookAt(&game->camera,
renderitem_t *item; 0, 0, 10,
dynArrayInit(&items, sizeof(renderitem_t), 1); 0, 0, 0
item = (renderitem_t *)dynArrayGet(&items, dynArrayAdd(&items)); );
item->onRender= &renderTest;
cameraLookAt(&game->camera, 3,3,3, 0,0,0); cameraOrtho(&game->camera,
cameraPerspective(&game->camera, 75, 16.0f/9.0f, 0.3f, 100.0f); 0, engine->render.width,
engine->render.height, 0,
0.01f, 1000.0f
);
shaderUse(&game->shader);
shaderUseCamera(&game->shader, &game->camera);
shaderUseTexture(&game->shader, &game->texture);
gridSetSize(&grid,
engine->render.width, engine->render.height,
engine->render.width, engine->render.height,
0, 0
);
renderListRenderPass(&game->list, engine, &game->camera, 0, &items);
renderListRender(&game->list, engine, &game->shader);
dynArrayDispose(&items); for(uint8_t i = 0; i < GRID_BRUH_COUNT; i++) {
gridbruh_t *b = game->items + i;
shaderUsePositionAndScale(&game->shader,
b->x, b->y, 0,
0, 0, 0,
b->width, b->height, 1
);
primitiveDraw(&game->primitive, 0, -1);
}
} }
void sandboxSceneDispose(sandboxscene_t *game) { void sandboxSceneDispose(sandboxscene_t *game) {

View File

@ -14,7 +14,9 @@
#include "../../display/primitives/quad.h" #include "../../display/primitives/quad.h"
#include "../../display/primitives/cube.h" #include "../../display/primitives/cube.h"
#include "../../display/renderlist.h" #include "../../display/renderlist.h"
#include "../../display/texture.h"
#include "../../file/asset.h" #include "../../file/asset.h"
#include "../../ui/grid.h"
/** /**
* Initialize the sandbox scene test game. * Initialize the sandbox scene test game.

118
src/ui/grid.c Normal file
View File

@ -0,0 +1,118 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "grid.h"
void gridInit(grid_t *grid) {
grid->breakpointCount = 0x00;
grid->childCount = 0x00;
grid->width = 0;
grid->height = 0;
grid->x = 0;
grid->y = 0;
}
uint8_t gridAddBreakpoint(
grid_t *grid, float width,
uint8_t rows, uint8_t columns,
float gutterX, float gutterY
) {
uint8_t i;
i = grid->breakpointCount;
grid->grids[i].rows = rows;
grid->grids[i].columns = columns;
grid->grids[i].gutterX = gutterX;
grid->grids[i].gutterY = gutterY;
grid->breakpoints[i].width = width;
grid->breakpointCount++;
return i;
}
gridchild_t * gridAddChild(grid_t *grid) {
gridchild_t *child = grid->children + grid->childCount;
child->defCount = 1;
child->defs[0].columns = 1;
child->defs[0].rows = 1;
child->defs[0].x = 0;
child->defs[0].y = 0;
grid->childCount++;
return child;
}
void gridSetSize(grid_t *grid,
float screenWidth, float screenHeight,
float width, float height,
float x, float y
) {
uint8_t i, breakpoint;
gridchild_t *item;
gridchilddef_t *itemdef;
griddef_t griddef;
float sizeCol, sizeRow, gx, gy, gw, gh;
// Need to resize?
if((
grid->width == width && grid->height == height &&
grid->x == x && grid->y == y
)) {
return;
}
// Update properties
grid->width = width;
grid->height = height;
grid->x = x;
grid->y = y;
// Determine breakpoint
breakpoint = 0xFF;
for(i = 0; i < grid->breakpointCount; i++) {
if(grid->breakpoints[i].width < screenWidth) continue;
breakpoint = i;
}
if(breakpoint == 0xFF) breakpoint = grid->breakpointCount - 1;
griddef = grid->grids[breakpoint];
// Determine the size of a single column
sizeCol = (width - (
griddef.gutterX * (griddef.columns - 1)
)) / griddef.columns;
sizeRow = (height - (
griddef.gutterY * (griddef.rows - 1)
)) / griddef.rows;
// Resize children
for(i = 0; i < grid->childCount; i++) {
// Get the item and the definition.
item = grid->children + i;
itemdef = item->defs + (
breakpoint >= item->defCount ? item->defCount-1 : breakpoint
);
// Get the local X/Y
gx = (sizeCol * itemdef->x) + (griddef.gutterX * itemdef->x);
gy = (sizeRow * itemdef->y) + (griddef.gutterY * itemdef->y);
// Get the width/height
gw = (
(itemdef->columns * sizeCol) +
(mathMax(itemdef->columns - 1, 0) * griddef.gutterX)
);
gh = (
(itemdef->rows * sizeRow) +
(mathMax(itemdef->rows - 1, 0) * griddef.gutterY)
);
item->onResize(
grid->user, i,
screenWidth, screenHeight,
x + gx,y + gy,
gw, gh
);
}
}

77
src/ui/grid.h Normal file
View File

@ -0,0 +1,77 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#define BREAKPOINT_COUNT 4
#define GRID_CHILD_COUNT 32
typedef struct {
float width;
} breakpoint_t;
typedef struct {
uint8_t rows;
uint8_t columns;
float gutterX;
float gutterY;
} griddef_t;
typedef void gridchildresizecallback_t(
void *user, int32_t i,
float screenWidth, float screenHeight,
float x, float y,
float width, float height
);
typedef struct {
uint8_t columns;
uint8_t rows;
uint8_t x;
uint8_t y;
} gridchilddef_t;
typedef struct {
gridchilddef_t defs[BREAKPOINT_COUNT];
uint8_t defCount;
gridchildresizecallback_t *onResize;
} gridchild_t;
typedef struct {
griddef_t grids[BREAKPOINT_COUNT];
breakpoint_t breakpoints[BREAKPOINT_COUNT];
gridchild_t children[GRID_CHILD_COUNT];
uint8_t childCount;
float width;
float height;
float x;
float y;
uint8_t breakpointCount;
void *user;
} grid_t;
void gridInit(grid_t *grid);
uint8_t gridAddBreakpoint(
grid_t *grid, float width,
uint8_t rows, uint8_t columns,
float gutterX, float gutterY
);
gridchild_t * gridAddChild(grid_t *grid);
void gridSetSize(grid_t *grid,
float screenWidth, float screenHeight,
float width, float height,
float x, float y
);