Refactor struct part 2.

This commit is contained in:
2021-05-20 22:35:50 -07:00
parent 9bcf6223b6
commit 06facdbba6
18 changed files with 61 additions and 214 deletions

View File

@ -7,7 +7,7 @@
#include "pokerchip.h" #include "pokerchip.h"
primitive_t * pokerChipCreate() { void pokerChipInit(primitive_t *primitive) {
vertice_t vertices[POKER_CHIP_VERTICE_COUNT] = { vertice_t vertices[POKER_CHIP_VERTICE_COUNT] = {
{ .x = 0, .y = 0.015, .z = -0.1, .u = 0.351562, .v = 0.28125 }, { .x = 0, .y = 0.015, .z = -0.1, .u = 0.351562, .v = 0.28125 },
{ .x = 0.0309017, .y = -0.015, .z = -0.09510570000000002, .u = 0.273438, .v = 0.21093799999999996 }, { .x = 0.0309017, .y = -0.015, .z = -0.09510570000000002, .u = 0.273438, .v = 0.21093799999999996 },
@ -330,12 +330,7 @@ primitive_t * pokerChipCreate() {
48 48
}; };
primitive_t *primitive = primitiveCreate( primitiveInit(primitive, POKER_CHIP_VERTICE_COUNT, POKER_CHIP_INDICE_COUNT);
POKER_CHIP_VERTICE_COUNT,
POKER_CHIP_INDICE_COUNT
);
primitiveBufferVertices(primitive, 0, POKER_CHIP_VERTICE_COUNT, vertices); primitiveBufferVertices(primitive, 0, POKER_CHIP_VERTICE_COUNT, vertices);
primitiveBufferIndices(primitive, 0, POKER_CHIP_INDICE_COUNT, indices); primitiveBufferIndices(primitive, 0, POKER_CHIP_INDICE_COUNT, indices);
return primitive;
} }

View File

@ -17,6 +17,6 @@
/** /**
* Generated Model Poker Chip * Generated Model Poker Chip
* Generated at Wed, 19 May 2021 14:20:06 GMT * Generated at Wed, 19 May 2021 14:20:06 GMT
* @returns Poker Chip as a primitive. * @param primitive Poker Chip primitive to init.
*/ */
primitive_t * pokerChipCreate(); void pokerChipInit(primitive_t *primitive);

View File

@ -7,7 +7,7 @@
#include "pokertable.h" #include "pokertable.h"
primitive_t * pokerTableCreate() { void pokerTableCreate(primitive_t *primitive) {
vertice_t vertices[POKER_TABLE_VERTICE_COUNT] = { vertice_t vertices[POKER_TABLE_VERTICE_COUNT] = {
{ .x = 0.12437210000000001, .y = 0, .z = -0.30026090000000005, .u = 0.836914, .v = 0.265625 }, { .x = 0.12437210000000001, .y = 0, .z = -0.30026090000000005, .u = 0.836914, .v = 0.265625 },
{ .x = 0.29349590000000003, .y = 0.0176777, .z = -0.12157000000000001, .u = 0.853516, .v = 0.06445299999999998 }, { .x = 0.29349590000000003, .y = 0.0176777, .z = -0.12157000000000001, .u = 0.853516, .v = 0.06445299999999998 },
@ -686,12 +686,7 @@ primitive_t * pokerTableCreate() {
182 182
}; };
primitive_t *primitive = primitiveCreate( primitiveInit(primitive, POKER_TABLE_VERTICE_COUNT, POKER_TABLE_INDICE_COUNT);
POKER_TABLE_VERTICE_COUNT,
POKER_TABLE_INDICE_COUNT
);
primitiveBufferVertices(primitive, 0, POKER_TABLE_VERTICE_COUNT, vertices); primitiveBufferVertices(primitive, 0, POKER_TABLE_VERTICE_COUNT, vertices);
primitiveBufferIndices(primitive, 0, POKER_TABLE_INDICE_COUNT, indices); primitiveBufferIndices(primitive, 0, POKER_TABLE_INDICE_COUNT, indices);
return primitive;
} }

View File

@ -17,6 +17,6 @@
/** /**
* Generated Model Poker Table * Generated Model Poker Table
* Generated at Sun, 09 May 2021 21:15:45 GMT * Generated at Sun, 09 May 2021 21:15:45 GMT
* @returns Poker Table as a primitive. * @param primitive Poker Table primitive to init.
*/ */
primitive_t * pokerTableCreate(); void pokerTableInit(primitive_t *primitive);

View File

@ -1,81 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "position.h"
positiondebug_t positionDebugCreate(
primitive_t *debugPrimitive,
texture_t *fontTexture, tileset_t *fontTileset
) {
positiondebug_t debug = {
.primitive = debugPrimitive,
.textTexture = fontTexture,
.textTileset = fontTileset,
.x = 0, .y = 0, .z = 0,
.pitch = 0, .yaw = 0, .roll = 0,
.scaleX = 1, .scaleY = 1, .scaleZ = 1
};
debug.textBatch = spriteBatchCreate(POSITION_DEBUG_FONT_CHARS_MAX);
return debug;
}
void positionDebugRender(shader_t *shader, positiondebug_t *debug) {
float speed = 0.5;
// Update values.
if(inputIsDown(INPUT_DEBUG_FINE)) speed *= 0.1;
if(inputIsDown(INPUT_DEBUG_UP)) debug->z -= TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_DOWN)) debug->z += TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_LEFT)) debug->x -= TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_RIGHT)) debug->x += TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_LOWER)) debug->y -= TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_RAISE)) debug->y += TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_MINUS)) {
debug->scaleX -= TIME_STATE.delta * speed;
debug->scaleY -= TIME_STATE.delta * speed;
debug->scaleZ -= TIME_STATE.delta * speed;
}
if(inputIsDown(INPUT_DEBUG_PLUS)) {
debug->scaleX += TIME_STATE.delta * speed;
debug->scaleY += TIME_STATE.delta * speed;
debug->scaleZ += TIME_STATE.delta * speed;
}
// Render object
shaderUsePositionAndScale(shader,
debug->x, debug->y, debug->z,
mathDeg2Rad(debug->pitch),mathDeg2Rad(debug->yaw),mathDeg2Rad(debug->roll),
debug->scaleX, debug->scaleY, debug->scaleZ
);
primitiveDraw(debug->primitive, 0, -1);
// Render debug text
char text[POSITION_DEBUG_FONT_CHARS_MAX];
sprintf(text, "%.2f, %.2f, %.2f\n%.2f, %.2f, %.2f \n%.2f, %.2f, %.2f",
debug->x, debug->y, debug->z,
debug->pitch, debug->yaw, debug->roll,
debug->scaleX, debug->scaleY, debug->scaleZ
);
shaderUseTexture(shader, debug->textTexture);
shaderUsePosition(shader,
0, 1, 0,
mathDeg2Rad(-90), mathDeg2Rad(0), 0
);
spriteBatchFlush(debug->textBatch);
fontSpriteBatchBuffer(debug->textBatch, debug->textTileset, text,
FONT_CENTER_X, FONT_CENTER_Y, 0,
-1, 0.1
);
spriteBatchDraw(debug->textBatch, 0, -1);
}
void positionDebugDispose(positiondebug_t *debug) {
spriteBatchDispose(debug->textBatch);
}

View File

@ -1,45 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../primitive.h"
#include "../texture.h"
#include "../tileset.h"
#include "../gui/font.h"
#include "../spritebatch.h"
#include "../shader.h"
#include "../../input/input.h"
/**
* Creates a position debug tool.
*
* @param debugPrimitive Primitive to debug
* @param fontTexture Font Texture to use for debugging.
* @param fontTileset Tileset for for font to use for debugging.
* @return Pointer to debug renderer.
*/
positiondebug_t positionDebugCreate(
primitive_t *debugPrimitive,
texture_t *fontTexture, tileset_t *fontTileset
);
/**
* Render a position debug tool.
*
* @param shader Shader to use.
* @param debug Debug tool to render.
*/
void positionDebugRender(shader_t *shader, positiondebug_t *debug);
/**
* Dispose a previously created position debug tool.
*
* @param debug Debug tool to dispose.
*/
void positionDebugDispose(positiondebug_t *debug);

View File

@ -63,5 +63,4 @@ void frameBufferDispose(framebuffer_t *frameBuffer) {
glDeleteRenderbuffers(1, &frameBuffer->rboId); glDeleteRenderbuffers(1, &frameBuffer->rboId);
glDeleteFramebuffers(1, &frameBuffer->fboId); glDeleteFramebuffers(1, &frameBuffer->fboId);
textureDispose(&frameBuffer->texture); textureDispose(&frameBuffer->texture);
free(frameBuffer);
} }

View File

@ -101,12 +101,11 @@ void cubeBuffer(primitive_t *prim,
free(indices); free(indices);
} }
primitive_t * cubeCreate(float w, float h, float d) { void cubeInit(primitive_t *primitive, float w, float h, float d) {
primitive_t *cube = primitiveCreate(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); primitiveInit(primitive, CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
cubeBuffer(cube, cubeBuffer(primitive,
-w/2, -h/2, -d/2, -w/2, -h/2, -d/2,
w, h, d, w, h, d,
0, 0 0, 0
); );
return cube;
} }

View File

@ -31,9 +31,9 @@ void cubeBuffer(primitive_t *primitive,
/** /**
* Creates a cube primitive of given size. * Creates a cube primitive of given size.
* @param primitive Primitive to create into a cube.
* @param w Width of cube. * @param w Width of cube.
* @param h Height of cube. * @param h Height of cube.
* @param d Depth of cube. * @param d Depth of cube.
* @return Primitive of the cube.
*/ */
primitive_t * cubeCreate(float w, float h, float d); void cubeInit(primitive_t *primitive, float w, float h, float d);

View File

@ -42,19 +42,15 @@ void quadBuffer(primitive_t *primitive, float z,
free(indices); free(indices);
} }
primitive_t * quadCreate(float z, void quadInit(primitive_t *primitive, float z,
float x0, float y0, float u0, float v0, float x0, float y0, float u0, float v0,
float x1, float y1, float u1, float v1 float x1, float y1, float u1, float v1
) { ) {
primitive_t *primitive = primitiveCreate( primitiveInit(primitive, QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT
);
quadBuffer(primitive, z, quadBuffer(primitive, z,
x0, y0, u0, v0, x0, y0, u0, v0,
x1, y1, u1, v1, x1, y1, u1, v1,
0, 0 0, 0
); );
return primitive;
} }

View File

@ -35,6 +35,7 @@ void quadBuffer(primitive_t *primitive, float z,
/** /**
* Creates a new quad primitive. * Creates a new quad primitive.
* *
* @param primitive Primitive to turn into a quad.
* @param z The Z axis coordinate of the quad. * @param z The Z axis coordinate of the quad.
* @param x0 The X lower coordinate. * @param x0 The X lower coordinate.
* @param y0 The Y lower coordinate. * @param y0 The Y lower coordinate.
@ -44,9 +45,8 @@ void quadBuffer(primitive_t *primitive, float z,
* @param y1 The Y higher coordinate. * @param y1 The Y higher coordinate.
* @param u1 The X higher texture coordinate. * @param u1 The X higher texture coordinate.
* @param v1 The Y higher texture coordinate. * @param v1 The Y higher texture coordinate.
* @return The quad primitive.
*/ */
primitive_t * quadCreate(float z, void quadInit(primitive_t *primitive, float z,
float x0, float y0, float u0, float v0, float x0, float y0, float u0, float v0,
float x1, float y1, float u1, float v1 float x1, float y1, float u1, float v1
); );

View File

@ -7,12 +7,13 @@
#include "shader.h" #include "shader.h"
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) { void shaderCompile(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
) {
int isSuccess, maxLength; int isSuccess, maxLength;
char *error; char *error;
GLuint shaderVertex, shaderFragment, shaderProgram; GLuint shaderVertex, shaderFragment, shaderProgram;
// Load the vertex shader first // Load the vertex shader first
shaderVertex = glCreateShader(GL_VERTEX_SHADER); shaderVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderVertex, 1, &vertexShaderSource, 0); glShaderSource(shaderVertex, 1, &vertexShaderSource, 0);
@ -64,21 +65,14 @@ shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
} }
// Everything is okay, let's create the encapsulated shader. // Everything is okay, let's create the encapsulated shader.
shader_t *shader = malloc(sizeof(shader_t));
if(shader == NULL) {
glDeleteProgram(shaderProgram);
glDeleteShader(shaderVertex);
glDeleteShader(shaderFragment);
return NULL;
}
shader->shaderVertex = shaderVertex; shader->shaderVertex = shaderVertex;
shader->shaderFrag = shaderFragment; shader->shaderFrag = shaderFragment;
shader->shaderProgram = shaderProgram; shader->shaderProgram = shaderProgram;
shader->uniProj = glGetUniformLocation(shader->shaderProgram, SHADER_UNI_PROJ); shader->uniProj = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_PROJ);
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);
// Bind the shader // Bind the shader
shaderUse(shader); shaderUse(shader);
@ -90,12 +84,10 @@ shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
return shader; return shader;
} }
bool shaderDispose(shader_t *shader) { void shaderDispose(shader_t *shader) {
glDeleteProgram(shader->shaderProgram); glDeleteProgram(shader->shaderProgram);
glDeleteShader(shader->shaderVertex); glDeleteShader(shader->shaderVertex);
glDeleteShader(shader->shaderFrag); glDeleteShader(shader->shaderFrag);
free(shader);
return true;
} }
void shaderUse(shader_t *shader) { void shaderUse(shader_t *shader) {

View File

@ -9,21 +9,20 @@
#include <dawn/dawn.h> #include <dawn/dawn.h>
/** /**
* Create a shader from vertex and fragment shader code. * Compiles a shader from vertex and fragment shader code.
* * @param shader Shader to compile into.
* @param vertexShaderSource The raw vertex shader code. * @param vertexShaderSource The raw vertex shader code.
* @param fragmentShaderSource The raw fragment shader code. * @param fragmentShaderSource The raw fragment shader code.
* @return Pointer to the loaded shader.
*/ */
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource); void shaderInit(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
);
/** /**
* Cleanup and unload a previously loaded shader. * Cleanup and unload a previously loaded shader.
*
* @param shader The shader to unload * @param shader The shader to unload
* @return True if successfully disposed.
*/ */
bool shaderDispose(shader_t *shader); void shaderDispose(shader_t *shader);
/** /**
* Attaches the supplied shader as the current shader. * Attaches the supplied shader as the current shader.
@ -49,7 +48,6 @@ void shaderUseTexture(shader_t *shader, texture_t *texture);
/** /**
* Set's the current translation matrix onto the shader for the next * Set's the current translation matrix onto the shader for the next
* render to use. Rotation order is set to YZX. * render to use. Rotation order is set to YZX.
*
* @param shader Shader to attach to. * @param shader Shader to attach to.
* @param x X coordinate (world space). * @param x X coordinate (world space).
* @param y Y coordinate (world space). * @param y Y coordinate (world space).

View File

@ -7,7 +7,7 @@
#include "tileset.h" #include "tileset.h"
tileset_t * tilesetInit(tileset_t *tileset, void tilesetInit(tileset_t *tileset,
int32_t columns, int32_t rows, int32_t columns, int32_t rows,
int32_t width, int32_t height, int32_t width, int32_t height,
int32_t gapX, int32_t gapY, int32_t gapX, int32_t gapY,
@ -18,10 +18,6 @@ tileset_t * tilesetInit(tileset_t *tileset,
tileset->count = rows * columns; tileset->count = rows * columns;
tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count); tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
if(tileset->divisions == NULL) {
free(tileset);
return NULL;
}
tileset->columns = columns; tileset->columns = columns;
tileset->rows = rows; tileset->rows = rows;

View File

@ -20,7 +20,7 @@ void engineUpdateStart(engine_t *engine, game_t *game, float delta) {
} }
bool engineUpdateEnd(engine_t *engine, game_t *game) { bool engineUpdateEnd(engine_t *engine, game_t *game) {
if(inputIsPressed(INPUT_NULL)) return false; if(inputIsPressed(&engine->input, INPUT_NULL)) return false;
return true; return true;
} }

View File

@ -36,32 +36,35 @@ int32_t main() {
// Prepare the game // Prepare the game
game_t *game = &GAME_STATE; game_t *game = &GAME_STATE;
input_t *input = &game->engine.input;
// Init the game // Init the game
if(gameInit(game)) { if(gameInit(game)) {
// Bind initial keys // Bind initial keys
inputBind(INPUT_NULL, (inputsource_t)GLFW_KEY_ESCAPE); inputBind(input, INPUT_NULL, (inputsource_t)GLFW_KEY_ESCAPE);
inputBind(INPUT_DEBUG_UP, (inputsource_t)GLFW_KEY_W); inputBind(input, INPUT_DEBUG_UP, (inputsource_t)GLFW_KEY_W);
inputBind(INPUT_DEBUG_DOWN, (inputsource_t)GLFW_KEY_S); inputBind(input, INPUT_DEBUG_DOWN, (inputsource_t)GLFW_KEY_S);
inputBind(INPUT_DEBUG_LEFT, (inputsource_t)GLFW_KEY_A); inputBind(input, INPUT_DEBUG_LEFT, (inputsource_t)GLFW_KEY_A);
inputBind(INPUT_DEBUG_RIGHT, (inputsource_t)GLFW_KEY_D); inputBind(input, INPUT_DEBUG_RIGHT, (inputsource_t)GLFW_KEY_D);
inputBind(INPUT_DEBUG_RAISE, (inputsource_t)GLFW_KEY_Q); inputBind(input, INPUT_DEBUG_RAISE, (inputsource_t)GLFW_KEY_Q);
inputBind(INPUT_DEBUG_LOWER, (inputsource_t)GLFW_KEY_E); inputBind(input, INPUT_DEBUG_LOWER, (inputsource_t)GLFW_KEY_E);
inputBind(INPUT_DEBUG_FINE, (inputsource_t)GLFW_KEY_LEFT_CONTROL); inputBind(input, INPUT_DEBUG_FINE, (inputsource_t)GLFW_KEY_LEFT_CONTROL);
inputBind(INPUT_DEBUG_PLUS, (inputsource_t)GLFW_KEY_EQUAL); inputBind(input, INPUT_DEBUG_PLUS, (inputsource_t)GLFW_KEY_EQUAL);
inputBind(INPUT_DEBUG_MINUS, (inputsource_t)GLFW_KEY_MINUS); inputBind(input, INPUT_DEBUG_MINUS, (inputsource_t)GLFW_KEY_MINUS);
inputBind(INPUT_UP, (inputsource_t)GLFW_KEY_UP); inputBind(input, INPUT_UP, (inputsource_t)GLFW_KEY_UP);
inputBind(INPUT_DOWN, (inputsource_t)GLFW_KEY_DOWN); inputBind(input, INPUT_DOWN, (inputsource_t)GLFW_KEY_DOWN);
inputBind(INPUT_LEFT, (inputsource_t)GLFW_KEY_LEFT); inputBind(input, INPUT_LEFT, (inputsource_t)GLFW_KEY_LEFT);
inputBind(INPUT_RIGHT, (inputsource_t)GLFW_KEY_RIGHT); inputBind(input, INPUT_RIGHT, (inputsource_t)GLFW_KEY_RIGHT);
inputBind(INPUT_UP, (inputsource_t)GLFW_KEY_W); inputBind(input, INPUT_UP, (inputsource_t)GLFW_KEY_W);
inputBind(INPUT_DOWN, (inputsource_t)GLFW_KEY_S); inputBind(input, INPUT_DOWN, (inputsource_t)GLFW_KEY_S);
inputBind(INPUT_LEFT, (inputsource_t)GLFW_KEY_A); inputBind(input, INPUT_LEFT, (inputsource_t)GLFW_KEY_A);
inputBind(INPUT_RIGHT, (inputsource_t)GLFW_KEY_D); inputBind(input, INPUT_RIGHT, (inputsource_t)GLFW_KEY_D);
// Init the render resolution // Init the render resolution
renderSetResolution(WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT); renderSetResolution(&game->engine.render,
WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT
);
// Update the window title. // Update the window title.
glfwSetWindowTitle(window, GAME_STATE.name); glfwSetWindowTitle(window, GAME_STATE.name);
@ -95,16 +98,17 @@ int32_t main() {
} }
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) { void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
renderSetResolution(width, height); renderSetResolution(&GAME_STATE.engine.render, width, height);
} }
void glfwOnKey(GLFWwindow *window, void glfwOnKey(GLFWwindow *window,
int32_t key, int32_t scancode, int32_t action, int32_t mods int32_t key, int32_t scancode, int32_t action, int32_t mods
) { ) {
input_t *input = &GAME_STATE.engine.input;
if(action == GLFW_PRESS) { if(action == GLFW_PRESS) {
INPUT_STATE.buffer[key] = 1; input->buffer[key] = 1;
} else if(action == GLFW_RELEASE) { } else if(action == GLFW_RELEASE) {
INPUT_STATE.buffer[key] = 0; input->buffer[key] = 0;
} }
} }

View File

@ -11,7 +11,6 @@
#include <dawn/dawn.h> #include <dawn/dawn.h>
#include "../../display/render.h" #include "../../display/render.h"
#include "../../game/game.h" #include "../../game/game.h"
#include "../../game/gametime.h"
#include "../../input/input.h" #include "../../input/input.h"
#define WINDOW_WIDTH_DEFAULT 480 #define WINDOW_WIDTH_DEFAULT 480

View File

@ -24,14 +24,14 @@ void pokerInit(poker_t *poker) {
poker->players[x].currentBet = 0; poker->players[x].currentBet = 0;
} }
pokerRoundInit(); pokerRoundInit(poker);
} }
void pokerRoundInit(poker_t *poker) { void pokerRoundInit(poker_t *poker) {
uint8_t x; uint8_t x;
// Refill the deck // Refill the deck
cardDeckFill(&poker->deck); cardDeckFill(poker->deck);
poker->deckSize = CARD_DECK_SIZE; poker->deckSize = CARD_DECK_SIZE;
// Reset the players // Reset the players