diff --git a/src/games/dawn/dawngame.c b/src/dawn/dawngame.c similarity index 74% rename from src/games/dawn/dawngame.c rename to src/dawn/dawngame.c index 73b97078..39600788 100644 --- a/src/games/dawn/dawngame.c +++ b/src/dawn/dawngame.c @@ -13,7 +13,7 @@ game_t * gameInit(platform_t *platform) { if(dawn == NULL) return NULL; // Load the game engine - dawn->engine = engineInit(platform, GAME_NAME, INPUT_COUNT); + dawn->engine = engineInit(platform, GAME_NAME, GAME_INPUT_COUNT); if(dawn->engine == NULL) { free(dawn); return NULL; @@ -23,13 +23,17 @@ game_t * gameInit(platform_t *platform) { return (game_t *)dawn; } -void gameStart(game_t *game) { +void gameUpdate(game_t *game) { dawngame_t *dawn = (dawngame_t *)game; - engineStart(dawn->engine); + engineUpdate(dawn->engine); } void gameDispose(game_t *game) { dawngame_t *dawn = (dawngame_t *)game; engineDispose(dawn->engine); free(dawn); +} + +engine_t * gameGetEngine(game_t *game) { + return ((dawngame_t *)game)->engine; } \ No newline at end of file diff --git a/src/games/dawn/dawngame.h b/src/dawn/dawngame.h similarity index 64% rename from src/games/dawn/dawngame.h rename to src/dawn/dawngame.h index eb42a87f..341bd1e4 100644 --- a/src/games/dawn/dawngame.h +++ b/src/dawn/dawngame.h @@ -5,7 +5,8 @@ #pragma once #include -#include "../../game/game.h" +#include "../engine/game/game.h" +#include "../engine/engine.h" /////////////////////////////////// CONSTANTS ////////////////////////////////// @@ -13,12 +14,12 @@ #define GAME_NAME "Dawn Game" /** Inputs */ -#define INPUT_UP (inputbind_t)0x01 -#define INPUT_DOWN (inputbind_t)0x02 -#define INPUT_LEFT (inputbind_t)0x03 -#define INPUT_RIGHT (inputbind_t)0x04 +#define GAME_INPUT_UP (inputbind_t)0x01 +#define GAME_INPUT_DOWN (inputbind_t)0x02 +#define GAME_INPUT_LEFT (inputbind_t)0x03 +#define GAME_INPUT_RIGHT (inputbind_t)0x04 -#define INPUT_COUNT 5 +#define GAME_INPUT_COUNT 5 /////////////////////////////// Type Definitions /////////////////////////////// /** Context about Dawn Game */ diff --git a/src/display/render.c b/src/display/render.c deleted file mode 100644 index 5e47599d..00000000 --- a/src/display/render.c +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Copyright (c) 2021 Dominic Msters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "render.h" - -render_t *RENDER_CURRENT = NULL; - -render_t * renderInit(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 = WINDOW_WIDTH_DEFAULT; - render->height = WINDOW_HEIGHT_DEFAULT; - - // Create the window. - render->window = glfwCreateWindow(render->width, render->height, - name, NULL, NULL - ); - if(!render->window) { - free(render); - glfwTerminate(); - return NULL; - } - - // Make the window the context current - glfwMakeContextCurrent(render->window); - - // Load GLAD - gladLoadGL((GLADloadproc)glfwGetProcAddress); - - 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); -} \ No newline at end of file diff --git a/src/engine/display/render.c b/src/engine/display/render.c new file mode 100644 index 00000000..2d08a8c0 --- /dev/null +++ b/src/engine/display/render.c @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2021 Dominic Msters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "render.h" + +render_t * renderInit(char *name) { + // Initialize the renderer + render_t *render = malloc(sizeof(render_t)); + if(!render) return NULL; + // render->width = WINDOW_WIDTH_DEFAULT; + // render->height = WINDOW_HEIGHT_DEFAULT; + + //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) { + // Free up the renderer + free(render); + return true; +} + +void renderSetResolution(render_t *render, uint32_t width, uint32_t height) { + render->width = width; + render->height = height; + glViewport(0, 0, width, height); +} \ No newline at end of file diff --git a/src/display/render.h b/src/engine/display/render.h similarity index 67% rename from src/display/render.h rename to src/engine/display/render.h index 452fd074..3988e960 100644 --- a/src/display/render.h +++ b/src/engine/display/render.h @@ -11,31 +11,18 @@ #include #include #include - -// I load GLAD and GLFW Here because they need to be included in specific orders #include -#include - #include -#define WINDOW_WIDTH_DEFAULT 480 -#define WINDOW_HEIGHT_DEFAULT 270 - /** * 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. * @@ -61,10 +48,10 @@ void renderFrame(render_t *render); bool renderDispose(render_t *render); /** - * Resize callbacks. + * Set the internal display resolution. * - * @param window Window that was resized. - * @param width New window width. - * @param height New window height. + * @param render Renderer to set resolution for. + * @param width Width (in pixels) of the viewport. + * @param height Height (in pixels) of the viewport. */ -void renderOnResize(GLFWwindow *window, int32_t width, int32_t height); \ No newline at end of file +void renderSetResolution(render_t *render, uint32_t width, uint32_t height); \ No newline at end of file diff --git a/src/display/shader.c b/src/engine/display/shader.c similarity index 100% rename from src/display/shader.c rename to src/engine/display/shader.c diff --git a/src/display/shader.h b/src/engine/display/shader.h similarity index 100% rename from src/display/shader.h rename to src/engine/display/shader.h diff --git a/src/engine/engine.c b/src/engine/engine.c index 71cf2edf..1aeacb04 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -7,24 +7,23 @@ #include "engine.h" -engine_t * engineInit( - platform_t *platform, char *gameName, uint32_t inputCount -) { +engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { // Create the engine instance. engine_t *engine = malloc(sizeof(engine_t)); if(engine == NULL) return NULL; + engine->name = name; engine->platform = platform; // Setup the renderer. - engine->render = renderInit(gameName); + engine->render = renderInit(name); if(engine->render == NULL) { free(engine); return NULL; } // Setup the input manager. - engine->input = inputInit(platform, inputCount); + engine->input = inputInit(inputCount); if(engine->input == NULL) { free(engine->render); free(engine); @@ -34,13 +33,9 @@ engine_t * engineInit( return engine; } -void engineStart(engine_t *engine) { - while(!glfwWindowShouldClose(engine->render->window)) { - inputUpdate(engine->platform, engine->input); - renderFrame(engine->render); - glfwSwapBuffers(engine->render->window); - glfwPollEvents(); - } +void engineUpdate(engine_t *engine) { + renderFrame(engine->render); + inputUpdate(engine->input); } bool engineDispose(engine_t *engine) { diff --git a/src/engine/engine.h b/src/engine/engine.h index 4239fab3..168c275f 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -5,17 +5,17 @@ #pragma once #include -#include "../platform/platform.h" -#include "../display/render.h" -#include "../file/asset.h" -#include "../input/input.h" +#include "display/render.h" +#include "file/asset.h" +#include "input/input.h" +#include "./platform.h" /** Information about the current engine context. */ typedef struct { /** Name of the game running. */ - char *gameName; + char *name; - /** Platform for the game */ + /** Platform the game is running on */ platform_t *platform; /** Renderer for the engine. */ @@ -29,20 +29,18 @@ typedef struct { * Initialize the engine context. * * @param platform The platform that the game is running on. - * @param gameName Name of the game being initialized. + * @param name Name of the game being initialized. * @param inputCount Count of input binds that exist in-engine. * @return The engine context. */ -engine_t * engineInit( - platform_t *platform, char *gameName, uint32_t inputCount -); +engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount); /** * Start the main engine loop. * * @param engine The game to start the loop for. */ -void engineStart(engine_t *engine); +void engineUpdate(engine_t *engine); /** * Cleanup a previously constructed game engine instance. diff --git a/src/file/asset.c b/src/engine/file/asset.c similarity index 100% rename from src/file/asset.c rename to src/engine/file/asset.c diff --git a/src/file/asset.h b/src/engine/file/asset.h similarity index 100% rename from src/file/asset.h rename to src/engine/file/asset.h diff --git a/src/game/game.h b/src/engine/game/game.h similarity index 58% rename from src/game/game.h rename to src/engine/game/game.h index 48c4ab4a..1332b4af 100644 --- a/src/game/game.h +++ b/src/engine/game/game.h @@ -7,8 +7,8 @@ #pragma once #include -#include "../platform/platform.h" -#include "../engine/engine.h" +#include "../engine.h" +#include "../platform.h" /** Information about the current game context. */ typedef void game_t; @@ -16,7 +16,6 @@ typedef void game_t; /** * Initialize the game context. * - * @param platform The platform that the game is running on. * @return The game instance context. */ game_t * gameInit(platform_t *platform); @@ -26,10 +25,19 @@ game_t * gameInit(platform_t *platform); * * @param game The game to start the loop for. */ -void gameStart(game_t *game); +void gameUpdate(game_t *game); /** * Cleanup a previously constructed. * @param game The game to cleanup. */ -void gameDispose(game_t *game); \ No newline at end of file +void gameDispose(game_t *game); + +/** + * Because games are anonymously typed we simply request that they allow other + * parts of the software to access the engine directly. + * + * @param game The game context + * @return The engine context attached to the running game. + */ +engine_t * gameGetEngine(game_t *game); \ No newline at end of file diff --git a/src/input/input.c b/src/engine/input/input.c similarity index 67% rename from src/input/input.c rename to src/engine/input/input.c index 1b835470..7d3b4395 100644 --- a/src/input/input.c +++ b/src/engine/input/input.c @@ -7,74 +7,68 @@ #include "input.h" -input_t * inputInit(platform_t *platform, uint32_t inputBindCount) { +input_t * inputInit(uint32_t inputBindCount) { + uint32_t i; + // Create the input manager input_t *input = malloc(sizeof(input_t)); if(!input) return NULL; - // Setup the binds + // Setup the bind lists input->inputBindCount = inputBindCount; - input->binds = malloc(sizeof(inputbind_t) * inputBindCount); - if(input->binds == NULL) { - free(input); - return NULL; + input->bindMap = malloc(sizeof(list_t) * inputBindCount); + for(i = 0; i < inputBindCount; i++) { + input->bindMap[i] = listCreate(); } - // Create the current input states + // Create the current & previous input states input->current = malloc(sizeof(float) * inputBindCount); - if(input->current == NULL) { - free(input->binds); - free(input); - return NULL; - } - - // Create the previous input states. input->previous = malloc(sizeof(float) * inputBindCount); - if(input->previous == NULL) { - free(input->current); - free(input->binds); - free(input); - return NULL; - } // Create the input actuate times array. input->times = malloc(sizeof(float) * inputBindCount); - if(input->times == NULL) { - free(input->previous); - free(input->current); - free(input->binds); - free(input); - return NULL; - } // Pass off the input manager return input; } -void inputUpdate(platform_t *platform, input_t *input) { +void inputUpdate(input_t *input) { + uint32_t i; + // Flip the states to save memory. float * currentCurrent = input->current; input->current = input->previous; input->previous = currentCurrent; // Now look at each bind, get the state from the platform - for(uint32_t i = 0; i < input->inputBindCount; i++) { + for(i = 0; i < input->inputBindCount; i++) { // For this bind, get the sources. } } void inputDispose(input_t *input) { + uint32_t i; + + // Free up the bind lists + for(i = 0; i < input->inputBindCount; i++) { + listDispose(input->bindMap[i], false); + } + free(input->times); free(input->previous); free(input->current); - free(input->binds); + free(input->bindMap); free(input); } -inputbindmap_t inputBind(input_t *input, inputbind_t bind, - platforminputsource_t *source +void inputBind(input_t *input, inputbind_t bind, inputsource_t *source) { + listAdd(input->bindMap[bind], source); +} + +void inputUnbind(input_t *input, inputbind_t bind, + inputsource_t *source ) { - return NULL; + listRemove(input->bindMap[bind], source); } bool inputIsDown(input_t *input, inputbind_t binding) { diff --git a/src/input/input.h b/src/engine/input/input.h similarity index 84% rename from src/input/input.h rename to src/engine/input/input.h index 8fa894ab..d028b293 100644 --- a/src/input/input.h +++ b/src/engine/input/input.h @@ -7,7 +7,7 @@ #include #include #include -#include "../platform/platform.h" +#include "../util/list/list.h" /////////////////////////////////// CONSTANTS ////////////////////////////////// #define INPUT_NULL (inputbind_t)0x00 @@ -19,12 +19,7 @@ * e.g. "Jump" or "Walk Forward". */ typedef uint8_t inputbind_t; - -/** - * Input Bind Map, contains the bound link between an input source and an - * inputbind_t. Bind Maps can be added and removed. - */ -typedef uint16_t inputbindmap_t; +typedef void inputsource_t; /** * Structure for the entire input mapping. @@ -41,12 +36,12 @@ typedef struct { /** Float of the GameTime that the input was actuated last. */ float *times; - + /** - * Array of input bindings. This is the list of "possible actions" that the - * user can perform. This would contain "jump", "walk forward", "run"... etc. + * Binding Map, Array of lists where index = binding and entry is a list of + * input sources. */ - inputbind_t *binds; + list_t **bindMap; } input_t; //////////////////////////////////// Methods /////////////////////////////////// @@ -57,13 +52,13 @@ typedef struct { * @param inputCount The input binding counts to allow for. * @return The input manager */ -input_t * inputInit(platform_t *platform, uint32_t inputBindCount); +input_t * inputInit(uint32_t inputBindCount); /** * Tick the input manager. * @param input The input to update. */ -void inputUpdate(platform_t *platform, input_t *input); +void inputUpdate(input_t *input); /** * Destroy the input manager and cleanup. @@ -76,15 +71,19 @@ void inputDispose(input_t *input); * time we fetch the state of bind, we will read the value from source. * * @param input The input manager to bind for. - * @param bind The binding to bind against + * @param bind The binding to bind against. * @param source The source that is being bound. - * @return The map between the bind and the source, needed to unbind later. */ -inputbindmap_t inputBind(input_t *input, inputbind_t bind, - platforminputsource_t *source -); +void inputBind(input_t *input, inputbind_t bind, inputsource_t *source); -float inputGetState(inputbind_t binding, platforminputsource_t *source); +/** + * Unbind a previously bound input source from a binding. This method is costly. + * + * @param input The input manager to bind for. + * @param bind The binding to unbind from. + * @param source The source that is being unbound. + */ +void inputUnbind(input_t *input, inputbind_t bind, inputsource_t *source); /** * Is the current input "down", not being pressed, being moved, not in a state diff --git a/src/engine/platform.h b/src/engine/platform.h new file mode 100644 index 00000000..103183b0 --- /dev/null +++ b/src/engine/platform.h @@ -0,0 +1,22 @@ +// Copyright (c) 2021 Dominic Msters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include +#include + +/** + * Contains information about the running platform. Essentially this is just + * some context as to what is running the game engine itself. It's mostly for + * metadata purposes but there may be some technical information required for + * the engine. + */ +typedef struct { + /** Internal name of the platform */ + char *name; + + /** Dimensions of the screen (in pixels) */ + uint32_t screenWidth, screenHeight; +} platform_t; \ No newline at end of file diff --git a/src/util/list/list.c b/src/engine/util/list/list.c similarity index 60% rename from src/util/list/list.c rename to src/engine/util/list/list.c index 03f5f001..6e865a4f 100644 --- a/src/util/list/list.c +++ b/src/engine/util/list/list.c @@ -7,8 +7,8 @@ #include "list.h" -List * listCreate() { - List *list = malloc(sizeof(List)); +list_t * listCreate() { + list_t *list = malloc(sizeof(list_t)); if(list == NULL) return NULL; list->start = NULL; @@ -17,9 +17,9 @@ List * listCreate() { return list; } -ListEntry * listAdd(List *list, void *data) { +listentry_t * listAdd(list_t *list, void *data) { //Create the list entry - ListEntry *entry = malloc(sizeof(ListEntry)); + listentry_t *entry = malloc(sizeof(listentry_t)); if(entry == NULL) return NULL; entry->data = data; @@ -33,7 +33,7 @@ ListEntry * listAdd(List *list, void *data) { return entry; } -void listAddEntry(List *list, ListEntry *entry) { +void listAddEntry(list_t *list, listentry_t *entry) { //Is this the first / only thing in the list? if(list->start == NULL) { list->start = entry; @@ -48,8 +48,21 @@ void listAddEntry(List *list, ListEntry *entry) { list->size++; } +void listRemove(list_t *list, void *data) { + uint32_t i = 0; + listentry_t *previous = list->start; -void listRemove(List *list, ListEntry *entry, bool freeData) { + while(previous != NULL) { + if(previous->data == data) { + listRemoveEntry(list, previous, false); + break; + } + i++; + previous = previous->next; + } +} + +void listRemoveEntry(list_t *list, listentry_t *entry, bool freeData) { //Update next and prev if(entry->prev != NULL) entry->prev->next = entry->next; if(entry->next != NULL) entry->next->prev = entry->prev; @@ -64,14 +77,15 @@ void listRemove(List *list, ListEntry *entry, bool freeData) { list->size--; } -ListEntry * listGetByIndex(List *list, int index) { + +listentry_t * listGetByIndex(list_t *list, uint32_t index) { if(index >= list->size) return NULL; //TODO: We can probably make this more efficient by deciding which way we //should loop from based on the list size. - int i = 0; - ListEntry *previous = list->start; + uint32_t i = 0; + listentry_t *previous = list->start; while(previous != NULL) { if(i == index) return previous; @@ -82,9 +96,22 @@ ListEntry * listGetByIndex(List *list, int index) { return NULL; } -int listGetIndex(List *list, ListEntry *entry) { - int i = 0; - ListEntry *previous = list->start; +uint32_t listGetIndex(list_t *list, void* data) { + uint32_t i = 0; + listentry_t *previous = list->start; + + while(previous != NULL) { + if(previous->data == data) return i; + i++; + previous = previous->next; + } + + return -1; +} + +uint32_t listGetEntryIndex(list_t *list, listentry_t *entry) { + uint32_t i = 0; + listentry_t *previous = list->start; while(previous != NULL) { if(previous == entry) return i; @@ -95,10 +122,10 @@ int listGetIndex(List *list, ListEntry *entry) { return -1; } -void listDispose(List *list, bool freeData) { +void listDispose(list_t *list, bool freeData) { //Free all of the entries - ListEntry *next = list->start; - ListEntry *current; + listentry_t *next = list->start; + listentry_t *current; while(next != NULL) { current = next; diff --git a/src/util/list/list.h b/src/engine/util/list/list.h similarity index 58% rename from src/util/list/list.h rename to src/engine/util/list/list.h index 9e25f29f..91b2f6fe 100644 --- a/src/util/list/list.h +++ b/src/engine/util/list/list.h @@ -7,6 +7,7 @@ #include #include #include +#include /** * Entry within a given linked list. @@ -14,11 +15,11 @@ * @param prev* Pointer to the previous entry in the list. * @param next* Pointer to the next entry in the list. */ -typedef struct ListEntry { +typedef struct listentry_t { void *data; - struct ListEntry *prev; - struct ListEntry *next; -} ListEntry; + struct listentry_t *prev; + struct listentry_t *next; +} listentry_t; /** * Linked List of elements, Doubly Linked. @@ -27,10 +28,10 @@ typedef struct ListEntry { * @param end* Last element within the list. */ typedef struct { - int size; - ListEntry *start; - ListEntry *end; -} List; + uint32_t size; + listentry_t *start; + listentry_t *end; +} list_t; //Method definitions @@ -38,7 +39,7 @@ typedef struct { * Creates a new linked list * @return Pointer to a new linked list. */ -List * listCreate(); +list_t * listCreate(); /** * Adds data to a linked list @@ -47,20 +48,29 @@ List * listCreate(); * @param data* Pointer to your data * @return A pointer to the new entry in the linked list that was created */ -ListEntry * listAdd(List *list, void *data); +listentry_t * listAdd(list_t *list, void *data); /** * Internal function */ -void listAddEntry(List *list, ListEntry *entry); +void listAddEntry(list_t *list, listentry_t *entry); /** - * Remove a list entry from the linked list - * @param list* Pointer to a previously created linked list - * @param entry* Pointer to the entry within the linked list. - * @param freeData If true the data of the ListEntry will be free()'d + * Remove data from a linked list. + * + * @param list Pointer to a previously created linked list. + * @param data* Pointer to your data. */ -void listRemove(List *list, ListEntry *entry, bool freeData); +void listRemove(list_t *list, void *data); + +/** + * Remove a list entry from the linked list. + * + * @param list* Pointer to a previously created linked list. + * @param entry* Pointer to the entry within the linked list. + * @param freeData If true the data of the listentry_t will be free()'d + */ +void listRemoveEntry(list_t *list, listentry_t *entry, bool freeData); /** * Returns the entry at a given index. This method is costly. @@ -68,19 +78,29 @@ void listRemove(List *list, ListEntry *entry, bool freeData); * @param index Index of element within the linked list to remove * @return The entry at the index or NULL if outside the bounds. */ -ListEntry * listGetByIndex(List *list, int index); +listentry_t * listGetByIndex(list_t *list, uint32_t index); + +/** + * Returns the index of data within the linked list. This method is costly. + * + * @param list* Pointer to a previously created linked list + * @param data* Pointer to your data. + * @return The index within the list the entry is in, or -1 if not found. + */ +uint32_t listGetIndex(list_t *list, void* data); /** * Returns the index of an entry within the linked list. This method is costly. + * * @param list* Pointer to a previously created linked list * @param entry* Pointer to the entry within the linked list. * @return The index within the list the entry is in, or -1 if not found. */ -int listGetIndex(List *list, ListEntry *entry); +uint32_t listGetEntryIndex(list_t *list, listentry_t *entry); /** * Dispose a previously created link list. * @param list* Pointer to a previously created linked list - * @param freeData If true the data within each ListEntry will be free()'d + * @param freeData If true the data within each listentry_t will be free()'d */ -void listDispose(List *list, bool freeData); \ No newline at end of file +void listDispose(list_t *list, bool freeData); \ No newline at end of file diff --git a/src/main.c b/src/main.c deleted file mode 100644 index fb65611e..00000000 --- a/src/main.c +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (c) 2021 Dominic Msters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "main.h" - -int32_t main() { - platform_t *platform = platformInit(); - if(platform == NULL) return 1; - - // Create the game instance - game_t *game = gameInit(platform); - if(game == NULL) return 1; - - // Start running the game instance - gameStart(game); - - // Game has finished running, cleanup. - gameDispose(game); - platformDispose(platform); - - return 0; -} \ No newline at end of file diff --git a/src/main.h b/src/main.h deleted file mode 100644 index f0b0ffb9..00000000 --- a/src/main.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright (c) 2021 Dominic Msters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include -#include "platform/platform.h" -#include "game/game.h" - -/** - * Entry of the program - * @return 0 if success, anything else for failure. - */ -int32_t main(); diff --git a/src/platform/glfw/glwfwplatform.c b/src/platform/glfw/glwfwplatform.c new file mode 100644 index 00000000..f5704602 --- /dev/null +++ b/src/platform/glfw/glwfwplatform.c @@ -0,0 +1,75 @@ +// Copyright (c) 2021 Dominic Msters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "glwfwplatform.h" + +GLFWwindow *window; +game_t *runningGame; + +int32_t main() { + // Create out platform context + platform_t platform = { + .name = "glfw", + .screenWidth = WINDOW_WIDTH_DEFAULT, + .screenHeight = WINDOW_HEIGHT_DEFAULT + }; + + // Attempt to init GLFW + if(!glfwInit()) return NULL; + window = glfwCreateWindow(platform.screenWidth, platform.screenHeight, + "", NULL, NULL + ); + if(!window) { + glfwTerminate(); + return 1; + } + + // Load GLAD + glfwMakeContextCurrent(window); + gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); + + // Setup window listeners + glfwSetWindowSizeCallback(window, &glfwOnResize); + glfwSetKeyCallback(window, &glfwOnKey); + + // Create the game instance + runningGame = gameInit(&platform); + if(runningGame == NULL) return 1; + + // Main Render Loop + while(!glfwWindowShouldClose(window)) { + gameUpdate(runningGame); + glfwSwapBuffers(window); + glfwPollEvents(); + } + + // Game has finished running, cleanup. + gameDispose(runningGame); + + // Terminate the GLFW context. + glfwSetWindowSizeCallback(window, NULL); + glfwTerminate(); + + return 0; +} + +void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) { + engine_t *engine = gameGetEngine(runningGame); + engine->platform->screenWidth = width; + engine->platform->screenWidth = height; + renderSetResolution(engine->render, width, height); +} + +void glfwOnKey(GLFWwindow *window, + int32_t key, int32_t scancode, int32_t action, int32_t mods +) { + char *title = glfwGetKeyName(key, scancode); + if(title == NULL) { + printf("Unknown Key %d", scancode); + } else { + printf(title); + } + printf("\n"); +} \ No newline at end of file diff --git a/src/platform/glfw/glwfwplatform.h b/src/platform/glfw/glwfwplatform.h new file mode 100644 index 00000000..23fdb1ed --- /dev/null +++ b/src/platform/glfw/glwfwplatform.h @@ -0,0 +1,47 @@ +// Copyright (c) 2021 Dominic Msters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once + +// I load GLAD and GLFW Here because they need to be included in specific orders +#include +#include +#include +#include "../../engine/platform.h" +#include "../../engine/game/game.h" +#include "../../engine/display/render.h" + +#define WINDOW_WIDTH_DEFAULT 480 +#define WINDOW_HEIGHT_DEFAULT 270 + +/** The GLFW Window Context Pointer */ +extern GLFWwindow *window; +extern game_t *runningGame; + +/** + * Entry of the program + * @return 0 if success, anything else for failure. + */ +int32_t main(); + + +/** + * Resize callbacks. + * + * @param window Window that was resized. + * @param width New window width. + * @param height New window height. + */ +void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height); + + +/** + * Keyboard Input callbacks. + * + * @param window Window that was resized. + */ +void glfwOnKey(GLFWwindow *window, + int32_t key, int32_t scancode, int32_t action, int32_t mods +); \ No newline at end of file diff --git a/src/platform/platform.h b/src/platform/platform.h deleted file mode 100644 index 9ce506ba..00000000 --- a/src/platform/platform.h +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) 2021 Dominic Msters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include -#include - -typedef void platforminputsource_t; - -/** - * Platform stuct, contains information about the device/method that is running - * the program that the game itself is running on, typically this will be either - * the operating system, or perhaps the specific device information. - */ -typedef struct { - /** Internal name of the platform */ - char *name; - - /** List of known input sources */ - platforminputsource_t **inputSources; - /** Count of known input source */ - uint32_t *inputSourceCount; - /** State of the input sources, updated by the platform. */ - float *inputStates; -} platform_t; - -/** - * Initialize the current platform context. The platform itself is responsible - * for deciding how this method is implemented. - * - * @return The context information for the platform. - */ -platform_t * platformInit(); - -/** - * Cleanup a previously created platform instance. - * - * @param platform The platform instance to cleanse. - */ -void platformDispose(platform_t *platform); - -/** - * Method will be polled for each known input source to request the current - * state from the device. Values returned will be defined within the 0 to 1 - * range. - * - * @param source The input source. - * @return Input state between 0 and 1. - */ -float platformInputGetState(platforminputsource_t *source); \ No newline at end of file diff --git a/src/platform/win32/win32platform.c b/src/platform/win32/win32platform.c deleted file mode 100644 index 1358f471..00000000 --- a/src/platform/win32/win32platform.c +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2021 Dominic Msters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "win32platform.h" - -platform_t * platformInit() { - platform_t *platform = malloc(sizeof(platform_t)); - - platform->name = WIN32_NAME; - - platform->inputSourceCount = 0; - platform->inputSources = NULL; - platform->inputStates = NULL; - - return platform; -} - -void platformDispose(platform_t *platform) { - free(platform); -} - -float platformInputGetState(platforminputsource_t *source) { - return 0; -} \ No newline at end of file diff --git a/src/platform/win32/win32platform.h b/src/platform/win32/win32platform.h deleted file mode 100644 index e3cf6c14..00000000 --- a/src/platform/win32/win32platform.h +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2021 Dominic Msters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "../platform.h" - -#define WIN32_NAME "Win32" \ No newline at end of file