Added frame buffer support.
This commit is contained in:
@ -8,5 +8,6 @@ out vec4 FragColor;
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 color = texture(u_Text, TexCoord);
|
vec4 color = texture(u_Text, TexCoord);
|
||||||
FragColor = color;
|
FragColor = color + vec4(0.25, 0.25, 0.25, 1);
|
||||||
|
// FragColor = vec4(1, 0, 0, 1);
|
||||||
}
|
}
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
// Display / Rendering
|
// Display / Rendering
|
||||||
#include "display/camera.h"
|
#include "display/camera.h"
|
||||||
|
#include "display/framebuffer.h"
|
||||||
#include "display/primitive.h"
|
#include "display/primitive.h"
|
||||||
#include "display/render.h"
|
#include "display/render.h"
|
||||||
#include "display/shader.h"
|
#include "display/shader.h"
|
||||||
|
16
include/dawn/display/framebuffer.h
Normal file
16
include/dawn/display/framebuffer.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "../libs.h"
|
||||||
|
#include "texture.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
GLuint fboId;
|
||||||
|
GLuint rboId;
|
||||||
|
texture_t *texture;
|
||||||
|
} framebuffer_t;
|
@ -19,5 +19,5 @@ typedef struct {
|
|||||||
|
|
||||||
/** Information about a single pixel. */
|
/** Information about a single pixel. */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t r, g, b, a ;
|
uint8_t r, g, b, a;
|
||||||
} pixel_t;
|
} pixel_t;
|
||||||
|
68
src/display/framebuffer.c
Normal file
68
src/display/framebuffer.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "framebuffer.h"
|
||||||
|
|
||||||
|
framebuffer_t * frameBufferCreate(int32_t w, int32_t h) {
|
||||||
|
framebuffer_t *fb = malloc(sizeof(framebuffer_t));
|
||||||
|
if(fb == NULL) return NULL;
|
||||||
|
|
||||||
|
// Create Color Attachment texture.
|
||||||
|
fb->texture = textureCreate(w, h, NULL);
|
||||||
|
if(fb->texture == NULL) {
|
||||||
|
free(fb);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Frame Buffer
|
||||||
|
glGenFramebuffers(1, &fb->fboId);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, fb->fboId);
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||||
|
GL_TEXTURE_2D, fb->texture->id, 0
|
||||||
|
);
|
||||||
|
|
||||||
|
// Render Buffer
|
||||||
|
glGenRenderbuffers(1, &fb->rboId);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, fb->rboId);
|
||||||
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
||||||
|
GL_RENDERBUFFER, fb->rboId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
uint32_t error;
|
||||||
|
error = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
|
return fb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void frameBufferUse(framebuffer_t *frameBuffer, bool clear) {
|
||||||
|
if(frameBuffer == NULL) {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
glViewport(0, 0, RENDER_STATE.width, RENDER_STATE.height);
|
||||||
|
} else {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer->fboId);
|
||||||
|
glViewport(0, 0, frameBuffer->texture->width, frameBuffer->texture->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(clear) {
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void frameBufferDispose(framebuffer_t *frameBuffer) {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||||
|
glDeleteRenderbuffers(1, &frameBuffer->rboId);
|
||||||
|
glDeleteFramebuffers(1, &frameBuffer->fboId);
|
||||||
|
textureDispose(frameBuffer->texture);
|
||||||
|
free(frameBuffer);
|
||||||
|
}
|
34
src/display/framebuffer.h
Normal file
34
src/display/framebuffer.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* 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 "texture.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new frame buffer that can be rendered to.
|
||||||
|
*
|
||||||
|
* @param width Width of the frame buffer (in pixels).
|
||||||
|
* @param height Height of the frame buffer (in pixels).
|
||||||
|
* @return A renderable frame buffer.
|
||||||
|
*/
|
||||||
|
framebuffer_t * frameBufferCreate(int32_t width, int32_t height);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use a given frame buffer as the current rendering context.
|
||||||
|
*
|
||||||
|
* @param frameBuffer Frame buffer to use, or NULL to not use any.
|
||||||
|
* @param clear Whether or not to clear the frame buffer prior to rendering.
|
||||||
|
*/
|
||||||
|
void frameBufferUse(framebuffer_t *frameBuffer, bool clear);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispose/cleanup a previously created frame buffer.
|
||||||
|
*
|
||||||
|
* @param frameBuffer Frame Buffer to clean.
|
||||||
|
*/
|
||||||
|
void frameBufferDispose(framebuffer_t *frameBuffer);
|
@ -11,7 +11,28 @@
|
|||||||
|
|
||||||
#define FONT_CHAR_START 33
|
#define FONT_CHAR_START 33
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the division for a given character.
|
||||||
|
*
|
||||||
|
* @param tileset Tileset to get the division from.
|
||||||
|
* @param character Character to get the division for.
|
||||||
|
* @return The division from the tileset for the character.
|
||||||
|
*/
|
||||||
tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character);
|
tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a set of font characters to the sprite. Coordinates are anchored to
|
||||||
|
* the top left (0,0) origin.
|
||||||
|
*
|
||||||
|
* @param batch Sprite Batch to render to.
|
||||||
|
* @param tileset Tileset for the font.
|
||||||
|
* @param string String to render.
|
||||||
|
* @param x Position in X space.
|
||||||
|
* @param y Position in Y space.
|
||||||
|
* @param z Position in Z space.
|
||||||
|
* @param charWidth Width of each character.
|
||||||
|
* @param charHeight Height of each character.
|
||||||
|
*/
|
||||||
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
|
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
|
||||||
char *string, float x, float y, float z, float charWidth, float charHeight
|
char *string, float x, float y, float z, float charWidth, float charHeight
|
||||||
);
|
);
|
@ -32,5 +32,4 @@ void renderDispose() {
|
|||||||
void renderSetResolution(int32_t width, int32_t height) {
|
void renderSetResolution(int32_t width, int32_t height) {
|
||||||
RENDER_STATE.width = width;
|
RENDER_STATE.width = width;
|
||||||
RENDER_STATE.height = height;
|
RENDER_STATE.height = height;
|
||||||
glViewport(0, 0, width, height);
|
|
||||||
}
|
}
|
@ -41,6 +41,8 @@ texture_t * textureCreate(int32_t width, int32_t height, pixel_t *pixels) {
|
|||||||
GL_RGBA, GL_UNSIGNED_BYTE, pixels
|
GL_RGBA, GL_UNSIGNED_BYTE, pixels
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,9 @@
|
|||||||
|
|
||||||
game_t GAME_STATE;
|
game_t GAME_STATE;
|
||||||
|
|
||||||
holdemgame_t holdem;
|
primitive_t *prim;
|
||||||
holdemrender_t render;
|
framebuffer_t *fbo;
|
||||||
|
texture_t *text;
|
||||||
texture_t *fontTexture;
|
|
||||||
spritebatch_t *fontBatch;
|
|
||||||
|
|
||||||
bool gameInit() {
|
bool gameInit() {
|
||||||
// Init the game
|
// Init the game
|
||||||
@ -25,29 +23,14 @@ bool gameInit() {
|
|||||||
inputInit();
|
inputInit();
|
||||||
worldInit();
|
worldInit();
|
||||||
|
|
||||||
holdemGameInit(&holdem);
|
prim = cubeCreate(1, 1, 1);
|
||||||
holdemRenderInit(&render);
|
text = assetTextureLoad("bruh.png");
|
||||||
|
|
||||||
holdemRoundInit(&holdem);
|
fbo = frameBufferCreate(128, 128);
|
||||||
cardShuffle(holdem.deck, holdem.deckSize);
|
|
||||||
|
|
||||||
// Deal Card
|
|
||||||
holdemDealAll(&holdem, 2);
|
|
||||||
holdemFlop(&holdem);
|
|
||||||
|
|
||||||
fontTexture = assetTextureLoad("font.png");
|
|
||||||
fontBatch = spriteBatchCreate(100);
|
|
||||||
tileset_t *tileset = tilesetCreate(20, 20,
|
|
||||||
fontTexture->width, fontTexture->height,
|
|
||||||
1, 1, 1, 1
|
|
||||||
);
|
|
||||||
|
|
||||||
char *buffer = "!\"#$%";
|
|
||||||
fontSpriteBatchBuffer(fontBatch, tileset, buffer, 0, 0, 0, 1.1, 1.5);
|
|
||||||
|
|
||||||
// Load the world shader.
|
// Load the world shader.
|
||||||
GAME_STATE.shaderWorld = assetShaderLoad(
|
GAME_STATE.shaderWorld = assetShaderLoad(
|
||||||
"shaders/test.vert", "shaders/test.frag"
|
"shaders/textured.vert", "shaders/textured.frag"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Init the input manger.
|
// Init the input manger.
|
||||||
@ -59,25 +42,27 @@ bool gameUpdate(float platformDelta) {
|
|||||||
renderFrameStart();
|
renderFrameStart();
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
|
|
||||||
// Attach the world shader
|
// Set up the camera and shader.
|
||||||
shaderUse(GAME_STATE.shaderWorld);
|
shaderUse(GAME_STATE.shaderWorld);
|
||||||
|
frameBufferUse(fbo, true);
|
||||||
|
cameraLookAt(&GAME_STATE.cameraWorld, 2, 2, 2, 0, 0, 0);
|
||||||
|
cameraPerspective(&GAME_STATE.cameraWorld, 45.0f,
|
||||||
|
((float)fbo->texture->width) / ((float)fbo->texture->height),
|
||||||
|
0.5f, 500.0f
|
||||||
|
);
|
||||||
|
shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraWorld);
|
||||||
|
shaderUsePosition(GAME_STATE.shaderWorld, 0, 0, 0, TIME_STATE.current, TIME_STATE.current, 0);
|
||||||
|
shaderUseTexture(GAME_STATE.shaderWorld, text);
|
||||||
|
primitiveDraw(prim, 0, prim->indiceCount);
|
||||||
|
|
||||||
// Set up the camera.
|
frameBufferUse(NULL, true);
|
||||||
int x = 10;
|
shaderUseTexture(GAME_STATE.shaderWorld, fbo->texture);
|
||||||
cameraLookAt(&GAME_STATE.cameraWorld, x, 2.5f, 50, x, 2, 0);
|
|
||||||
cameraPerspective(&GAME_STATE.cameraWorld, 45.0f,
|
cameraPerspective(&GAME_STATE.cameraWorld, 45.0f,
|
||||||
((float)RENDER_STATE.width) / ((float)RENDER_STATE.height),
|
((float)RENDER_STATE.width) / ((float)RENDER_STATE.height),
|
||||||
0.5f, 500.0f
|
0.5f, 500.0f
|
||||||
);
|
);
|
||||||
shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraWorld);
|
shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraWorld);
|
||||||
|
primitiveDraw(prim, 0, prim->indiceCount);
|
||||||
|
|
||||||
shaderUseTexture(GAME_STATE.shaderWorld, fontTexture);
|
|
||||||
spriteBatchDraw(fontBatch, 0, -1);
|
|
||||||
|
|
||||||
// holdemRender(&render, &holdem);
|
|
||||||
// Render the game scene.
|
|
||||||
// worldRender();
|
|
||||||
|
|
||||||
if(inputIsPressed(INPUT_NULL)) return false;
|
if(inputIsPressed(INPUT_NULL)) return false;
|
||||||
return true;
|
return true;
|
||||||
|
@ -11,13 +11,11 @@
|
|||||||
#include "../display/shader.h"
|
#include "../display/shader.h"
|
||||||
#include "../file/asset.h"
|
#include "../file/asset.h"
|
||||||
#include "../input/input.h"
|
#include "../input/input.h"
|
||||||
#include "../world/world.h"
|
|
||||||
#include "../world/entity/entity.h"
|
|
||||||
|
|
||||||
#include "../card/poker/holdem.h"
|
#include "../display/primitive.h"
|
||||||
#include "../display/gui/font.h"
|
#include "../display/framebuffer.h"
|
||||||
#include "../display/spritebatch.h"
|
#include "../display/primitives/cube.h"
|
||||||
#include "../display/tileset.h"
|
#include "../display/primitives/quad.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the game context.
|
* Initialize the game context.
|
||||||
|
Reference in New Issue
Block a user