Dawn/src/game/poker/ui/pokerplayerui.c

153 lines
4.3 KiB
C

/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "pokerplayerui.h"
void pokerPlayerUiInit(pokerplayerui_t *ui) {
gridchild_t *child;
labelInit(&ui->label);
frameBufferInit(&ui->frame,
POKER_PLAYER_UI_IMAGE_RESOLUTION, POKER_PLAYER_UI_IMAGE_RESOLUTION
);
quadInit(&ui->quad, 0,
0, 0, 0, 1,
POKER_PLAYER_UI_IMAGE_SIZE, POKER_PLAYER_UI_IMAGE_SIZE, 1, 0
);
// Grid
gridInit(&ui->grid);
gridAddBreakpoint(&ui->grid, -1, 2, 2, POKER_PLAYER_UI_PADDING, 0);
// Player UI Image
child = gridAddChild(&ui->grid);
gridChildAddBreakpoint(child, 1,0, 1,2);
// Chips
child = gridAddChild(&ui->grid);
gridChildAddBreakpoint(child, 0,0, 1,1);
// Title.
child = gridAddChild(&ui->grid);
gridChildAddBreakpoint(child, 0,1, 1,1);
}
void pokerPlayerUiUpdate(
pokerplayerui_t *ui, pokergame_t *game, shader_t *shader, int32_t playerIndex,
engine_t *engine
) {
camera_t camera;
uint8_t seat;
pokerplayer_t *player;
float x, y, z;
player = game->poker.players + playerIndex;
// Bind the frame buffer
frameBufferUse(&ui->frame, true);
// Set up the camera perspective
cameraPerspective(&camera, 45,
(float)POKER_PLAYER_UI_IMAGE_SIZE / (float)POKER_PLAYER_UI_IMAGE_SIZE,
0.03f, 10.0f
);
// Locate the XYZ position of the camera to look at the player
seat = pokerGameSeatFromIndex(playerIndex);
x = POKER_WORLD_SEAT_POSITION_X(seat);
y = POKER_PLAYER_UI_IMAGE_Y;
z = POKER_WORLD_SEAT_POSITION_Z(seat);
// Actually look at the player, we need to get close.
cameraLookAt(&camera,
x * POKER_PLAYER_UI_IMAGE_DIST, y, z * POKER_PLAYER_UI_IMAGE_DIST,
x, y, z
);
// Render the VN character
shaderUse(shader);
shaderUseCamera(shader, &camera);
shaderUsePosition(shader, 0,0,0, 0,0,0);
vnCharacterRender(game->scene.characters + playerIndex, shader);
// Unbind the frame buffer.
frameBufferUnbind(&engine->render, false);
}
void pokerPlayerUiRender(
pokerplayerui_t *ui, pokergame_t *game, shader_t *shader, font_t *font,
engine_t *engine,
int32_t playerIndex, float x, float y
) {
pokerplayer_t *player;
char buffer[32];
float scale;
align_t align;
float gx, gy, gw, gh, sCol, sRow;
scale = fontGetScale(FONT_SIZE_DEFAULT);
player = game->poker.players + playerIndex;
// Resize the grid
align = alignmentGet(
ALIGN_POS_END | ALIGN_SIZE_ORIGINAL, ALIGN_POS_START | ALIGN_SIZE_ORIGINAL,
engine->render.width, engine->render.height,
POKER_PLAYER_UI_IMAGE_SIZE*2, POKER_PLAYER_UI_IMAGE_SIZE, -1, -1
);
gridSetSize(
&ui->grid, engine->render.width, engine->render.height,
align.width, align.height,
align.x + x, align.y + y
);
// Render face
gridGetChildSize(&ui->grid, ui->grid.breakpointCurrent, ui->grid.children+0,
&sCol, &sRow, &gx, &gy, &gw, &gh
);
shaderUseTexture(shader, &ui->frame.texture);
shaderUsePosition(shader, gx, gy, 0, 0,0,0);
primitiveDraw(&ui->quad, 0, -1);
// Render chips
sprintf(buffer, "$%i", player->chips);
ui->label.maxWidth = -1;
labelSetText(&ui->label, font, buffer);
align = gridAlignChild(
&ui->grid, ui->grid.breakpointCurrent, ui->grid.children + 1, &sCol, &sRow,
ALIGN_POS_END | ALIGN_SIZE_ORIGINAL, ALIGN_POS_CENTER | ALIGN_SIZE_ORIGINAL,
ui->label.info.width, ui->label.info.height
);
labelRender(&ui->label, shader, align.x, align.y);
// Render state
if(player->state & POKER_PLAYER_STATE_OUT) {
sprintf(buffer, "Out");
} else if(player->state & POKER_PLAYER_STATE_FOLDED) {
sprintf(buffer, "Folded");
} else if(player->state & POKER_PLAYER_STATE_SHOWING) {
sprintf(buffer, "Showing");
} else if(game->poker.bet.better == playerIndex) {
sprintf(buffer, "Thinking");
} else {
sprintf(buffer, "Whatever");
}
labelSetText(&ui->label, font, buffer);
align = gridAlignChild(
&ui->grid, ui->grid.breakpointCurrent, ui->grid.children + 2, &sCol, &sRow,
ALIGN_POS_END | ALIGN_SIZE_ORIGINAL, ALIGN_POS_CENTER | ALIGN_SIZE_ORIGINAL,
ui->label.info.width, ui->label.info.height
);
labelRender(&ui->label, shader, align.x, align.y);
}
void pokerPlayerUiDispose(pokerplayerui_t *ui) {
primitiveDispose(&ui->quad);
frameBufferDispose(&ui->frame);
labelDispose(&ui->label);
}