Refactor struct part 2.

This commit is contained in:
2021-05-20 22:35:50 -07:00
parent 5ae1f1c0d4
commit f6a4be2f3c
18 changed files with 61 additions and 214 deletions

View File

@ -7,7 +7,7 @@
#include "pokerchip.h"
primitive_t * pokerChipCreate() {
void pokerChipInit(primitive_t *primitive) {
vertice_t vertices[POKER_CHIP_VERTICE_COUNT] = {
{ .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 },
@ -330,12 +330,7 @@ primitive_t * pokerChipCreate() {
48
};
primitive_t *primitive = primitiveCreate(
POKER_CHIP_VERTICE_COUNT,
POKER_CHIP_INDICE_COUNT
);
primitiveInit(primitive, POKER_CHIP_VERTICE_COUNT, POKER_CHIP_INDICE_COUNT);
primitiveBufferVertices(primitive, 0, POKER_CHIP_VERTICE_COUNT, vertices);
primitiveBufferIndices(primitive, 0, POKER_CHIP_INDICE_COUNT, indices);
return primitive;
}

View File

@ -17,6 +17,6 @@
/**
* Generated Model Poker Chip
* 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"
primitive_t * pokerTableCreate() {
void pokerTableCreate(primitive_t *primitive) {
vertice_t vertices[POKER_TABLE_VERTICE_COUNT] = {
{ .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 },
@ -686,12 +686,7 @@ primitive_t * pokerTableCreate() {
182
};
primitive_t *primitive = primitiveCreate(
POKER_TABLE_VERTICE_COUNT,
POKER_TABLE_INDICE_COUNT
);
primitiveInit(primitive, POKER_TABLE_VERTICE_COUNT, POKER_TABLE_INDICE_COUNT);
primitiveBufferVertices(primitive, 0, POKER_TABLE_VERTICE_COUNT, vertices);
primitiveBufferIndices(primitive, 0, POKER_TABLE_INDICE_COUNT, indices);
return primitive;
}

View File

@ -17,6 +17,6 @@
/**
* Generated Model Poker Table
* 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);
glDeleteFramebuffers(1, &frameBuffer->fboId);
textureDispose(&frameBuffer->texture);
free(frameBuffer);
}

View File

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

View File

@ -31,9 +31,9 @@ void cubeBuffer(primitive_t *primitive,
/**
* Creates a cube primitive of given size.
* @param primitive Primitive to create into a cube.
* @param w Width of cube.
* @param h Height 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);
}
primitive_t * quadCreate(float z,
void quadInit(primitive_t *primitive, float z,
float x0, float y0, float u0, float v0,
float x1, float y1, float u1, float v1
) {
primitive_t *primitive = primitiveCreate(
QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT
);
primitiveInit(primitive, QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
quadBuffer(primitive, z,
x0, y0, u0, v0,
x1, y1, u1, v1,
0, 0
);
return primitive;
}

View File

@ -35,6 +35,7 @@ void quadBuffer(primitive_t *primitive, float z,
/**
* Creates a new quad primitive.
*
* @param primitive Primitive to turn into a quad.
* @param z The Z axis coordinate of the quad.
* @param x0 The X 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 u1 The X 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 x1, float y1, float u1, float v1
);

View File

@ -7,12 +7,13 @@
#include "shader.h"
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
void shaderCompile(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
) {
int isSuccess, maxLength;
char *error;
GLuint shaderVertex, shaderFragment, shaderProgram;
// Load the vertex shader first
shaderVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderVertex, 1, &vertexShaderSource, 0);
@ -64,13 +65,6 @@ shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
}
// 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->shaderFrag = shaderFragment;
shader->shaderProgram = shaderProgram;
@ -90,12 +84,10 @@ shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
return shader;
}
bool shaderDispose(shader_t *shader) {
void shaderDispose(shader_t *shader) {
glDeleteProgram(shader->shaderProgram);
glDeleteShader(shader->shaderVertex);
glDeleteShader(shader->shaderFrag);
free(shader);
return true;
}
void shaderUse(shader_t *shader) {

View File

@ -9,21 +9,20 @@
#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 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.
*
* @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.
@ -49,7 +48,6 @@ void shaderUseTexture(shader_t *shader, texture_t *texture);
/**
* Set's the current translation matrix onto the shader for the next
* render to use. Rotation order is set to YZX.
*
* @param shader Shader to attach to.
* @param x X coordinate (world space).
* @param y Y coordinate (world space).

View File

@ -7,7 +7,7 @@
#include "tileset.h"
tileset_t * tilesetInit(tileset_t *tileset,
void tilesetInit(tileset_t *tileset,
int32_t columns, int32_t rows,
int32_t width, int32_t height,
int32_t gapX, int32_t gapY,
@ -18,10 +18,6 @@ tileset_t * tilesetInit(tileset_t *tileset,
tileset->count = rows * columns;
tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
if(tileset->divisions == NULL) {
free(tileset);
return NULL;
}
tileset->columns = columns;
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) {
if(inputIsPressed(INPUT_NULL)) return false;
if(inputIsPressed(&engine->input, INPUT_NULL)) return false;
return true;
}

View File

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

View File

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