Added shader loading, added glad

This commit is contained in:
2021-02-17 23:17:19 +11:00
parent 1407d028e8
commit 24218cca63
13 changed files with 178 additions and 393 deletions

View File

@ -1,75 +0,0 @@
#include "render.h"
render_t *RENDER_CURRENT = NULL;
render_t * renderInit(int32_t width, int32_t height, char *name) {
// Can't contextualize twice
if(RENDER_CURRENT != NULL) return NULL;
// Attempt to init GLFW
if(!glfwInit()) return NULL;
// Initialize the renderer
render_t *render = malloc(sizeof(render_t));
if(!render) return NULL;
render->width = width;
render->height = height;
// Create the window.
render->window = glfwCreateWindow(width, height, name, NULL, NULL);
if(!render->window) {
free(render);
glfwTerminate();
return NULL;
}
// Make the window the context current
glfwMakeContextCurrent(render->window);
RENDER_CURRENT = render;
// Listeners
glfwSetWindowSizeCallback(render->window, &renderOnResize);
//GLEnable Things
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
return render;
}
void renderFrame(render_t *render) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-1, -1, 0);
glColor3f(0, 1, 0);
glVertex3f(0, 1, 0);
glColor3f(0, 0, 1);
glVertex3f(1, -1, 0);
glEnd();
}
bool renderDispose(render_t *render) {
// Cleanup the callback
glfwSetWindowSizeCallback(render->window, NULL);
// Free up the renderer
free(render);
RENDER_CURRENT = NULL;
// Terminate the GLFW context.
glfwTerminate();
return true;
}
void renderOnResize(GLFWwindow *window, int32_t width, int32_t height) {
RENDER_CURRENT->width = width;
RENDER_CURRENT->height = height;
glViewport(0, 0, width, height);
}

View File

@ -1,62 +0,0 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <malloc.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <cglm/vec4.h>
#include <cglm/mat4.h>
/**
* Contains information about the current render state, can be used for querying
* how the renderer is currently set up.
*/
typedef struct {
/** The GLFW Window Context Pointer */
GLFWwindow *window;
/** Resolution (in pixels) */
int32_t width, height;
} render_t;
/** Current active renderer. */
extern render_t *RENDER_CURRENT;
/**
* Initialize the renderer.
*
* @param width Width (in pixels) of the window.
* @param height Height (in pixels) of the window.
* @param name String of the windows' name.
* @return Rendering Context information.
*/
render_t * renderInit(int32_t width, int32_t height, char *name);
/**
* Render a single frame of the render loop. The renderer is not (currently)
* responsible for render looping.
*
* @param render The renderer to loop for.
*/
void renderFrame(render_t *render);
/**
* Cleanup a render context.
*
* @param render Renderer to cleanup.
* @return True or False if Successful/Not.
*/
bool renderDispose(render_t *render);
/**
* Resize callbacks.
*
* @param window Window that was resized.
* @param width New window width.
* @param height New window height.
*/
void renderOnResize(GLFWwindow *window, int32_t width, int32_t height);

View File

@ -1,9 +0,0 @@
#include "shader.h"
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
}
bool shaderDipose(shader_t *shader) {
return true;
}

View File

@ -1,36 +0,0 @@
#pragma once
#include <GL/gl3w.h>
#include <GLFW/glfw3.h>
#include <stdbool.h>
/**
* Structure containing information about an OpenGL Shader. For simplicity sake
* we demand certain uninforms to be present on the shader target.
*/
typedef struct {
/** Pointer to an uploaded vertex shader program */
unsigned int shaderVertex;
/** Pointer to an uploaded fragment shader program */
unsigned int shaderFrag;
/** Pointer to an uploaded shader program linked */
unsigned int shaderProgram;
} shader_t;
/**
* Create a shader from vertex and fragment shader code.
*
* @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);
/**
* Cleanup and unload a previously loaded shader.
*
* @param shader The shader to unload
* @return True if successfully disposed.
*/
bool shaderDipose(shader_t *shader);

View File

@ -1,62 +0,0 @@
#include "asset.h"
char * assetLoadString(char *assetName) {
// Open a buffer.
FILE *fptr = assetBufferOpen(assetName);
if(fptr == NULL) return NULL;
// Read the count of bytes in the file
fseek(fptr, 0, SEEK_END);// Seek to the end
size_t length = ftell(fptr);// Get our current position (the end)
fseek(fptr, 0, SEEK_SET);// Reset the seek
// Create the string buffer
char *str = malloc(length + 1);// Add 1 for the null terminator.
if(str == NULL) {
assetBufferClose(fptr);
return NULL;
}
// Read and seal the string.
fread(str, 1, length, fptr);// Read all the bytes
str[length] = '\0';// Null terminate.
assetBufferClose(fptr); // Close the buffer.
return str;
}
FILE * assetBufferOpen(char *assetName) {
// Get the directory based on the raw input by creating a new string.
size_t lenAsset = strlen(assetName);// Get the length of asset
size_t lenPrefix = strlen(ASSET_PREFIX);// Get the length of the prefix
// Create str to house both the prefix and asset, and null terminator
char *joined = malloc(lenAsset + lenPrefix + 1);
if(joined == NULL) return NULL;// Mem okay?
joined[0] = '\0';//Start at null
strcat(joined, ASSET_PREFIX);//Add prefix
strcat(joined, assetName);//Add body
// Open the file pointer now.
FILE *fptr = fopen(joined, "rb");
free(joined);// Free the string we just created
if(!fptr) return NULL;// File available?
return fptr;
}
bool assetBufferClose(FILE *buffer) {
return fclose(buffer);
}
int32_t assetBufferRead(FILE *buffer, char *data, int32_t size) {
return (int32_t)fread(data, 1, size, buffer);
}
int32_t assetBufferEnd(FILE *buffer) {
return feof(buffer);
}
void assetBufferSkip(FILE *buffer, int32_t n) {
fseek(buffer, n, SEEK_CUR);
}

View File

@ -1,59 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
#include <string.h>
/** Prefix of all asset load methods, may be customizable in future. */
#define ASSET_PREFIX "../assets/"
/**
* Method to load an asset into memory as a raw string.
*
* @param assetName Path leading to the asset within the root asset directory.
* @return Pointer to char array of data from asset, NULL if unsuccesful.
*/
char * assetLoadString(char *assetName);
/**
* Platform-centric method to open a file buffer to an asset.
*
* @param assetName The asset name to open a buffer for.
* @return Pointer to a buffer, NULL if unsuccessfuil.
*/
FILE * assetBufferOpen(char *assetName);
/**
* Closes a previously opened asset buffer.
*
* @param buffer Buffer to close.
* @return True if successful, otherwise false.
*/
bool assetBufferClose(FILE *buffer);
/**
* Read bytes from buffer.
*
* @param buffer The buffer pointing to an asset.
* @param data Pointer to a ubyte array to buffer data into.
* @param size Length of the data buffer. Represents how many bytes can be read.
* @return The count of bytes read. Complete when less than data array size.
*/
int32_t assetBufferRead(FILE *buffer, char *data, int32_t size);
/**
* Skip to the end of the buffer, useful to find the length of the buffer.
*
* @param Buffer The buffer pointing to an asset.
* @return How many bytes were skipped
*/
int32_t assetBufferEnd(FILE *buffer);
/**
* Method to skip n bytes in the buffer
*
* @param buffer The buffer pointing to an asset.
* @param n Count of bytes to skip.
*/
void assetBufferSkip(FILE *buffer, int32_t n);

View File

@ -1,32 +0,0 @@
#include "game.h"
game_t * gameInit(char *gameName) {
// Create the game instance
game_t *game = malloc(sizeof(game_t));
if(game == NULL) return NULL;
// Setup the renderer
game->render = renderInit(640, 480, gameName);
if(game->render == NULL) {
free(game);
return NULL;
}
return game;
}
void gameStart(game_t *game) {
while(!glfwWindowShouldClose(game->render->window)) {
renderFrame(game->render);
glfwSwapBuffers(game->render->window);
glfwPollEvents();
}
}
bool gameDispose(game_t *game) {
if(!renderDispose(game->render)) return false;
free(game);
return true;
}

View File

@ -1,33 +0,0 @@
#pragma once
#include <stdbool.h>
#include "../display/render.h"
/** Information about the current game context. */
typedef struct {
/** Renderer for the game */
render_t *render;
} game_t;
/**
* Initialize the game context.
*
* @param gameName Name of the game being initialized.
* @return The game instance context.
*/
game_t * gameInit(char *gameName);
/**
* Start the main game loop.
*
* @param game The game to start the loop for.
*/
void gameStart(game_t *game);
/**
* Cleanup a previously constructed.
*
* @param game The game to cleanup.
* @return True if successful or not.
*/
bool gameDispose(game_t *game);

View File

@ -1,17 +0,0 @@
#include "main.h"
int32_t main() {
// Create the game instance
game_t *game = gameInit("Dawn");
if(game == NULL) return 1;
// Start running the game instance
gameStart(game);
// Game has finished running, cleanup.
if(!gameDispose(game)) {
return 1;
}
return 0;
}