From f3ffc93927d53c72877ce1c9bd51c31d1de27e99 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 6 May 2021 21:13:37 -0700 Subject: [PATCH] Court is now in session --- include/dawn/cards/poker/holdem.h | 12 ++++-- src/card/poker/holdemgame.c | 59 +++++++++++++++++++++++----- src/card/poker/holdemgame.h | 1 + src/card/poker/render/holdemrender.c | 52 ++++++++++++++++++++++-- src/card/poker/render/holdemrender.h | 6 +++ src/display/camera.c | 6 ++- src/display/camera.h | 3 +- src/display/framebuffer.c | 4 ++ src/display/shader.c | 8 ++-- src/platform/glfw/glwfwplatform.c | 11 ++++++ src/platform/glfw/glwfwplatform.h | 5 ++- src/util/math.h | 4 +- 12 files changed, 148 insertions(+), 23 deletions(-) diff --git a/include/dawn/cards/poker/holdem.h b/include/dawn/cards/poker/holdem.h index bfc076d9..f15e348e 100644 --- a/include/dawn/cards/poker/holdem.h +++ b/include/dawn/cards/poker/holdem.h @@ -30,6 +30,9 @@ #define HOLDEM_GAME_FRAME_RIGHT_WIDTH (\ RENDER_STATE.width - HOLDEM_GAME_FRAME_LEFT_WIDTH - 1\ ) +#define HOLDEM_GAME_CARD_WIDTH 0.05 +#define HOLDEM_GAME_CARD_HEIGHT 0.07 +#define HOLDEM_GAME_CARD_DEPTH 0.001 /** Texas Hold'em Player State */ @@ -71,13 +74,16 @@ typedef struct { holdemplayer_t players[HOLDEM_PLAYER_COUNT]; } holdemmatch_t; - typedef struct { holdemmatch_t match; + texture_t *kagamiTexture; + tileset_t *kagamiTileset; + primitive_t *kagamiQuad; - primitive_t *cube; - texture_t *texture; + texture_t *cardTexture; + tileset_t *cardTileset; + primitive_t *cardPrimitive; /** Game Render Frames */ framebuffer_t *frameLeft; diff --git a/src/card/poker/holdemgame.c b/src/card/poker/holdemgame.c index b2fa6963..ac0bd711 100644 --- a/src/card/poker/holdemgame.c +++ b/src/card/poker/holdemgame.c @@ -10,6 +10,8 @@ holdemgame_t HOLDEM_GAME_STATE; void holdemGameInit() { int32_t lWidth, rWidth, height; + card_t card; + tilesetdiv_t *cardBack; // Create the initial frame buffers lWidth = HOLDEM_GAME_FRAME_LEFT_WIDTH, rWidth = HOLDEM_GAME_FRAME_RIGHT_WIDTH; @@ -19,9 +21,36 @@ void holdemGameInit() { HOLDEM_GAME_STATE.quadLeft = quadCreate(0, 0, 0, 0, 0, lWidth, height, 1, 1); HOLDEM_GAME_STATE.quadRight = quadCreate(0, 0, 0, 0, 0, rWidth, height, 1, 1); - //TESTING - HOLDEM_GAME_STATE.cube = cubeCreate(1, 1, 1); - HOLDEM_GAME_STATE.texture = assetTextureLoad("bruh.png"); + // Kagami + HOLDEM_GAME_STATE.kagamiTexture = assetTextureLoad("kagami.png"); + HOLDEM_GAME_STATE.kagamiTileset = tilesetCreate(3, 2, + HOLDEM_GAME_STATE.kagamiTexture->width, + HOLDEM_GAME_STATE.kagamiTexture->height, + 0, 0, 0, 0 + ); + HOLDEM_GAME_STATE.kagamiQuad = quadCreate(0, 0, 0, 0, 0, 1, 1, 1, 1); + + // Load Cards Texture + HOLDEM_GAME_STATE.cardTexture = assetTextureLoad("cards_normal.png"); + HOLDEM_GAME_STATE.cardTileset = tilesetCreate(CARD_COUNT_PER_SUIT, 6, + HOLDEM_GAME_STATE.cardTexture->width, HOLDEM_GAME_STATE.cardTexture->height, + 0, 0, 0, 0 + ); + + // Cards Primitive + cardBack = HOLDEM_GAME_STATE.cardTileset->divisions+( + HOLDEM_GAME_STATE.cardTileset->columns * 4 + ); + HOLDEM_GAME_STATE.cardPrimitive = primitiveCreate( + QUAD_VERTICE_COUNT * 2, QUAD_INDICE_COUNT * 2 + ); + quadBuffer(HOLDEM_GAME_STATE.cardPrimitive, -HOLDEM_GAME_CARD_DEPTH, + -HOLDEM_GAME_CARD_WIDTH, -HOLDEM_GAME_CARD_HEIGHT, + cardBack->x0, cardBack->y1, + HOLDEM_GAME_CARD_WIDTH, HOLDEM_GAME_CARD_HEIGHT, + cardBack->x1, cardBack->y0, + QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT + ); // Prepare match holdemMatchInit(&HOLDEM_GAME_STATE.match); @@ -38,37 +67,49 @@ void holdemGameUpdate() { frameBufferDispose(HOLDEM_GAME_STATE.frameRight); HOLDEM_GAME_STATE.frameLeft = frameBufferCreate(lWidth, height); HOLDEM_GAME_STATE.frameRight = frameBufferCreate(rWidth, height); - quadBuffer(HOLDEM_GAME_STATE.quadLeft, 0, 0, 0, 0, 0, lWidth, height, 1, 1, 0, 0); - quadBuffer(HOLDEM_GAME_STATE.quadRight, 0, 0, 0, 0, 0, rWidth, height, 1, 1, 0, 0); + quadBuffer(HOLDEM_GAME_STATE.quadLeft, 0, + 0, 0, 0, 1, + lWidth, height, 1, 0, + 0, 0 + ); + quadBuffer(HOLDEM_GAME_STATE.quadRight, 0, + 0, 0, 0, 1, + rWidth, height, 1, 0, + 0, 0 + ); } // Render things on the left frame buffer + glClearColor(0.3, 0, 0, 1); frameBufferUse(HOLDEM_GAME_STATE.frameLeft, true); - cameraPerspective(&HOLDEM_GAME_STATE.cameraLeft, 75, - ((float)lWidth/height), 0.25f, 100.0f + cameraPerspective(&HOLDEM_GAME_STATE.cameraLeft, 45, + ((float)lWidth/height), 1.0f, 1000.0f ); - cameraLookAt(&HOLDEM_GAME_STATE.cameraLeft, 5, 5, 5, 0, 0, 0); + cameraLookAt(&HOLDEM_GAME_STATE.cameraLeft, 2, 2, 2, 0, 0, 0); shaderUseCamera(GAME_STATE.shaderWorld, &HOLDEM_GAME_STATE.cameraLeft); holdemRenderWorld(); // Render things on the right frame buffer + glClearColor(0.3, 0.3, 0, 1); frameBufferUse(HOLDEM_GAME_STATE.frameRight, true); cameraPerspective(&HOLDEM_GAME_STATE.cameraRight, 45, ((float)rWidth/height), 0.25f, 100.0f ); - cameraLookAt(&HOLDEM_GAME_STATE.cameraRight, 0, 5, 5, 0, 0, 0); + cameraLookAt(&HOLDEM_GAME_STATE.cameraRight, 0, 3, 3, 0, 0, 0); shaderUseCamera(GAME_STATE.shaderWorld, &HOLDEM_GAME_STATE.cameraRight); holdemRenderWorld(); // Finally, render the frame buffers to the back buffer. + glClearColor(0, 0, 0, 1); frameBufferUse(NULL, true); cameraOrtho(&GAME_STATE.cameraWorld, 0, RENDER_STATE.width, RENDER_STATE.height, 1, 0, 1 ); cameraLookAt(&GAME_STATE.cameraWorld, 0, 0, 0.5f, 0, 0, 0); shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraWorld); + // L shaderUsePosition(GAME_STATE.shaderWorld, 0, 0, 0, 0, 0, 0); shaderUseTexture(GAME_STATE.shaderWorld, HOLDEM_GAME_STATE.frameLeft->texture); diff --git a/src/card/poker/holdemgame.h b/src/card/poker/holdemgame.h index dccf99b1..4be4d33b 100644 --- a/src/card/poker/holdemgame.h +++ b/src/card/poker/holdemgame.h @@ -14,6 +14,7 @@ #include "../../display/shader.h" #include "../../display/camera.h" #include "../../display/primitives/cube.h" +#include "../../display/tileset.h" #include "../../display/primitives/quad.h" #include "../../file/asset.h" diff --git a/src/card/poker/render/holdemrender.c b/src/card/poker/render/holdemrender.c index a454d2b8..794ff711 100644 --- a/src/card/poker/render/holdemrender.c +++ b/src/card/poker/render/holdemrender.c @@ -7,9 +7,53 @@ #include "holdemrender.h" +void holdemRenderPlayer(float x, float y, float z, float yaw) { + float w, h; + w = 1, h = ( + (float)HOLDEM_GAME_STATE.kagamiTileset->divY / + (float)HOLDEM_GAME_STATE.kagamiTileset->divX + ); + + int i = (int32_t)(TIME_STATE.current*10)%HOLDEM_GAME_STATE.kagamiTileset->count; + quadBuffer(HOLDEM_GAME_STATE.kagamiQuad, 0, + -w/2, -h/2, + HOLDEM_GAME_STATE.kagamiTileset->divisions[i].x0, + HOLDEM_GAME_STATE.kagamiTileset->divisions[i].y1, + w/2, h/2, + HOLDEM_GAME_STATE.kagamiTileset->divisions[i].x1, + HOLDEM_GAME_STATE.kagamiTileset->divisions[i].y0, + 0, 0 + ); + + shaderUseTexture(GAME_STATE.shaderWorld, HOLDEM_GAME_STATE.kagamiTexture); + shaderUsePosition(GAME_STATE.shaderWorld, x, y, z, 0, mathDeg2Rad(yaw), 0); + primitiveDraw(HOLDEM_GAME_STATE.kagamiQuad, 0, -1); +} + +void holdemRenderCard(card_t card, + float x, float y, float z, float pitch, float yaw, float roll +) { + tilesetdiv_t *cardFront = HOLDEM_GAME_STATE.cardTileset->divisions + card; + quadBuffer(HOLDEM_GAME_STATE.cardPrimitive, HOLDEM_GAME_CARD_DEPTH, + -HOLDEM_GAME_CARD_WIDTH, -HOLDEM_GAME_CARD_HEIGHT, + cardFront->x0, cardFront->y1, + HOLDEM_GAME_CARD_WIDTH, HOLDEM_GAME_CARD_HEIGHT, + cardFront->x1, cardFront->y0, + 0, 0 + ); + + shaderUseTexture(GAME_STATE.shaderWorld, HOLDEM_GAME_STATE.cardTexture); + shaderUsePosition(GAME_STATE.shaderWorld, x, y, z, pitch, yaw, roll); + primitiveDraw(HOLDEM_GAME_STATE.cardPrimitive, 0, -1); +} + void holdemRenderWorld() { - shaderUseTexture(GAME_STATE.shaderWorld, HOLDEM_GAME_STATE.texture); - shaderUsePosition(GAME_STATE.shaderWorld, 0, 0, 0, 0, 0, 0); - shaderUsePosition(GAME_STATE.shaderWorld, 0, 0, 0, TIME_STATE.current, TIME_STATE.current, 0); - primitiveDraw(HOLDEM_GAME_STATE.cube, 0, -1); + holdemRenderCard(CARD_HEARTS_QUEEN, 0, 0, 0, mathDeg2Rad(-90), 0, 0); + + // Draw the players + holdemRenderPlayer(0, 0, -1, 0); + holdemRenderPlayer(-1, 0, -0, 90); + holdemRenderPlayer(-0.75, 0, 0.75, -45); + holdemRenderPlayer(0.75, 0, 0.75, 45); + holdemRenderPlayer(1, 0, 0, -90); } \ No newline at end of file diff --git a/src/card/poker/render/holdemrender.h b/src/card/poker/render/holdemrender.h index e377ec54..21289b57 100644 --- a/src/card/poker/render/holdemrender.h +++ b/src/card/poker/render/holdemrender.h @@ -9,5 +9,11 @@ #include #include "../../../display/shader.h" #include "../../../display/primitive.h" +#include "../../../display/primitives/quad.h" +#include "../../../util/math.h" +void holdemRenderPlayer(float x, float y, float z, float yaw); +void holdemRenderCard(card_t card, + float x, float y, float z, float pitch, float yaw, float roll +); void holdemRenderWorld(); \ No newline at end of file diff --git a/src/display/camera.c b/src/display/camera.c index 32308267..ebe63835 100644 --- a/src/display/camera.c +++ b/src/display/camera.c @@ -10,6 +10,7 @@ void cameraLookAt(camera_t *camera, float x, float y, float z, float targetX, float targetY, float targetZ ) { + glm_mat4_identity(camera->view); glm_lookat( (vec3){ x, y, z }, (vec3){ targetX, targetY, targetZ }, @@ -22,6 +23,7 @@ void cameraLook(camera_t *camera, float x, float y, float z, float pitch, float yaw, float roll ) { + glm_mat4_identity(camera->view); glm_look( (vec3){ x, y, z }, (vec3){ pitch, yaw, roll }, @@ -33,11 +35,13 @@ void cameraLook(camera_t *camera, void cameraPerspective(camera_t *camera, float fov, float aspect, float near, float far ) { - glm_perspective(fov, aspect, near, far, camera->projection); + glm_mat4_identity(camera->projection); + glm_perspective(mathDeg2Rad(fov), aspect, near, far, camera->projection); } void cameraOrtho(camera_t *camera, float left, float right, float bottom, float top, float near, float far ) { + glm_mat4_identity(camera->projection); glm_ortho(left, right, bottom, top, near, far, camera->projection); } \ No newline at end of file diff --git a/src/display/camera.h b/src/display/camera.h index bcc79912..bbb7ce47 100644 --- a/src/display/camera.h +++ b/src/display/camera.h @@ -5,6 +5,7 @@ #pragma once #include +#include "../util/math.h" /** * Make a camera look at a position in world space while itself being positioned @@ -42,7 +43,7 @@ void cameraLook(camera_t *camera, * Make a camera's projection be a 3D Perspective view. * * @param camera The camera to project. - * @param fov The field of view of the camera. + * @param fov The field of view of the camera (in degrees). * @param aspect The aspect ratio of the camera (w / h) * @param near The near plane clip. * @param far the far plane clip. diff --git a/src/display/framebuffer.c b/src/display/framebuffer.c index 90b26b98..25d83461 100644 --- a/src/display/framebuffer.c +++ b/src/display/framebuffer.c @@ -11,6 +11,10 @@ framebuffer_t * frameBufferCreate(int32_t w, int32_t h) { framebuffer_t *fb = malloc(sizeof(framebuffer_t)); if(fb == NULL) return NULL; + // At least one pixel + if(w <= 0) w = 1; + if(h <= 0) h = 1; + // Create Color Attachment texture. fb->texture = textureCreate(w, h, NULL); if(fb->texture == NULL) { diff --git a/src/display/shader.c b/src/display/shader.c index 8060c51b..daf84eb6 100644 --- a/src/display/shader.c +++ b/src/display/shader.c @@ -103,8 +103,8 @@ void shaderUse(shader_t *shader) { } void shaderUseCamera(shader_t *shader, camera_t *camera) { - glUniformMatrix4fv(shader->uniView, 1, GL_FALSE, (float*)camera->view); - glUniformMatrix4fv(shader->uniProj, 1, GL_FALSE, (float*)camera->projection); + glUniformMatrix4fv(shader->uniView, 1, GL_FALSE, camera->view[0]); + glUniformMatrix4fv(shader->uniProj, 1, GL_FALSE, camera->projection[0]); } void shaderUseTexture(shader_t *shader, texture_t *texture) { @@ -131,11 +131,13 @@ void shaderUsePosition(shader_t *shader, //Rotation, we do each axis individually axis[0] = 1, axis[1] = 0, axis[2] = 0; glm_rotate(MATRIX_POSITION, pitch, axis); + axis[0] = 0, axis[1] = 1; glm_rotate(MATRIX_POSITION, yaw, axis); + axis[1] = 0, axis[2] = 1; glm_rotate(MATRIX_POSITION, roll, axis); //Send to the shader. - glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, (float*)MATRIX_POSITION); + glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, MATRIX_POSITION[0]); } \ No newline at end of file diff --git a/src/platform/glfw/glwfwplatform.c b/src/platform/glfw/glwfwplatform.c index eb504fb3..118c8886 100644 --- a/src/platform/glfw/glwfwplatform.c +++ b/src/platform/glfw/glwfwplatform.c @@ -11,6 +11,11 @@ game_t *runningGame = NULL; int32_t main() { // Attempt to init GLFW if(!glfwInit()) return NULL; + + // Setup window hints + glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false); + + // Create Window window = glfwCreateWindow(WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT, "", NULL, NULL ); @@ -28,6 +33,8 @@ int32_t main() { glfwSetWindowSizeCallback(window, &glfwOnResize); glfwSetKeyCallback(window, &glfwOnKey); + glfwSetErrorCallback (&glfwOnError); + // Init the game if(gameInit()) { // Bind initial keys @@ -87,4 +94,8 @@ void glfwOnKey(GLFWwindow *window, } else if(action == GLFW_RELEASE) { INPUT_STATE.buffer[key] = 0; } +} + +void glfwOnError(int error, const char* description) { + fputs(description, stderr); } \ No newline at end of file diff --git a/src/platform/glfw/glwfwplatform.h b/src/platform/glfw/glwfwplatform.h index 061fa8f5..cd5e3fb1 100644 --- a/src/platform/glfw/glwfwplatform.h +++ b/src/platform/glfw/glwfwplatform.h @@ -42,4 +42,7 @@ void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height); */ void glfwOnKey(GLFWwindow *window, int32_t key, int32_t scancode, int32_t action, int32_t mods -); \ No newline at end of file +); + + +void glfwOnError(int error, const char* description); \ No newline at end of file diff --git a/src/util/math.h b/src/util/math.h index b7da5a68..e202a65d 100644 --- a/src/util/math.h +++ b/src/util/math.h @@ -8,4 +8,6 @@ #define mathMod(a,b) (a%b+b)%b #define mathMax(a,b) (ab?b:a) -#define mathAbs(n) (n<0?-n:n) \ No newline at end of file +#define mathAbs(n) (n<0?-n:n) +#define mathDeg2Rad(n) (n * M_PI / 180.0) +#define mathRad2Deg(n) (n * 180 / M_PI) \ No newline at end of file