Adding back GLFW

This commit is contained in:
2024-10-04 09:37:32 -05:00
parent 4205b919d4
commit be27408cf4
32 changed files with 7512 additions and 16 deletions

View File

@ -8,12 +8,16 @@ add_executable(${DAWN_TARGET_NAME})
# Add base
add_subdirectory(dawn)
add_subdirectory(dawnterm)
# Compile entries
if(DAWN_TARGET STREQUAL "linux-x64-glfw")
if(DAWN_TARGET STREQUAL "linux-x64-terminal")
add_subdirectory(dawnlinux)
add_subdirectory(dawnterm)
add_subdirectory(dawntermlinux)
elseif(DAWN_TARGET STREQUAL "linux-x64-glfw")
add_subdirectory(dawnlinux)
add_subdirectory(dawnglfw)
add_subdirectory(dawnopengl)
else()
message(FATAL_ERROR "Unknown target: ${DAWN_TARGET}")
endif()

View File

@ -17,6 +17,7 @@ target_include_directories(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(assert)
add_subdirectory(rpg)
add_subdirectory(ui)
# Sources
target_sources(${DAWN_TARGET_NAME}

View File

@ -10,6 +10,9 @@
#include "input.h"
#include "display/display.h"
#include "rpg/world/maps/testmap.h"
#include "ui/textbox.h"
#include <font8x8_basic.h>
map_t MAP;
game_t GAME;
@ -20,7 +23,8 @@ void gameInit() {
timeInit();
inputInit();
displayInit();
textboxInit();
testMapInit(&MAP);
gameSetMap(&MAP);
}

View File

@ -10,6 +10,7 @@
#include "rpg/world/map.h"
#include "assert/assert.h"
#include "time.h"
#include "ui/textbox.h"
void entityInit(
entity_t *entity,
@ -69,6 +70,7 @@ void entityUpdate(entity_t *entity) {
break;
case ENTITY_STATE_TALKING:
if(!textboxIsOpen()) entity->state = ENTITY_STATE_IDLE;
break;
default:

View File

@ -11,6 +11,7 @@
#include "rpg/world/map.h"
#include "input.h"
#include "assert/assert.h"
#include "ui/textbox.h"
void playerInit(entity_t *entity) {
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity is not a player.");
@ -44,6 +45,7 @@ void playerUpdate(entity_t *entity) {
target->state = ENTITY_STATE_TALKING;
entity->state = ENTITY_STATE_TALKING;
textboxSetText("Hello Player");
return;
}
}

View File

@ -0,0 +1,12 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
textbox.c
)

27
src/dawn/ui/textbox.c Normal file
View File

@ -0,0 +1,27 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "textbox.h"
#include "assert/assert.h"
textbox_t TEXTBOX;
void textboxInit() {
memset(&TEXTBOX, 0, sizeof(textbox_t));
}
void textboxSetText(const char_t *text) {
assertNotNull(text, "Text cannot be NULL.");
size_t len = strlen(text);
assertTrue(len < TEXTBOX_TEXT_MAX - 1, "Text is too long.");
strcpy(TEXTBOX.text, text);
TEXTBOX.open = true;
}
bool_t textboxIsOpen() {
return TEXTBOX.open;
}

37
src/dawn/ui/textbox.h Normal file
View File

@ -0,0 +1,37 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawn.h"
#define TEXTBOX_TEXT_MAX 256
typedef struct {
char_t text[TEXTBOX_TEXT_MAX];
bool_t open;
} textbox_t;
extern textbox_t TEXTBOX;
/**
* Initializes the textbox.
*/
void textboxInit();
/**
* Sets the text of the textbox, and opens it.
*
* @param text Text to set.
*/
void textboxSetText(const char_t *text);
/**
* Returns whether the textbox is open.
*
* @return Whether the textbox is open.
*/
bool_t textboxIsOpen();

View File

@ -0,0 +1,26 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Libraries
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
glfw
)
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
dawnglfw.c
linuxhost.c
input.c
)

143
src/dawnglfw/dawnglfw.c Normal file
View File

@ -0,0 +1,143 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dawnglfw.h"
#include "game.h"
#include "assert/assert.h"
#include "assert/assertgl.h"
#include "display/backbuffer.h"
dawnglfw_t DAWN_GLFW;
int32_t dawnGlfwStart() {
memset(&DAWN_GLFW, 0, sizeof(DAWN_GLFW));
// Init GLFW
if(!glfwInit()) {
assertUnreachable("Failed to initialize GLFW");
return -1;
}
// Setup window hints
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Create Window
DAWN_GLFW.window = glfwCreateWindow(
DAWN_GLFW_WIDTH_DEFAULT,
DAWN_GLFW_HEIGHT_DEFAULT,
"Dawn",
NULL,
NULL
);
if(DAWN_GLFW.window == NULL) {
glfwTerminate();
assertUnreachable("Failed to create GLFW window");
}
// Load GLAD
glfwMakeContextCurrent(DAWN_GLFW.window);
glfwSwapInterval(1);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
assertNoGLError();
// Override the defaults
int32_t fbWidth, fbHeight;
int32_t windowWidth, windowHeight;
glfwGetFramebufferSize(DAWN_GLFW.window, &fbWidth, &fbHeight);
glfwGetWindowSize(DAWN_GLFW.window, &windowWidth, &windowHeight);
assertNoGLError();
assertTrue(fbWidth > 0, "Detected framebuffer width is too small?");
assertTrue(fbWidth > 0, "Detected framebuffer height is too small?");
assertTrue(windowWidth > 0, "Detected window width is too small?");
assertTrue(windowHeight > 0, "Detected window height is too small?");
DAWN_GLFW.width = windowWidth;
DAWN_GLFW.height = windowHeight;
backBufferSetSize(
fbWidth,
fbHeight,
((float_t)fbWidth) / ((float_t)windowWidth)
);
// Set the input binds
dawnGlfwInputBind(INPUT_BIND_UP, GLFW_KEY_W);
dawnGlfwInputBind(INPUT_BIND_UP, GLFW_KEY_UP);
dawnGlfwInputBind(INPUT_BIND_DOWN, GLFW_KEY_S);
dawnGlfwInputBind(INPUT_BIND_DOWN, GLFW_KEY_DOWN);
dawnGlfwInputBind(INPUT_BIND_LEFT, GLFW_KEY_A);
dawnGlfwInputBind(INPUT_BIND_LEFT, GLFW_KEY_LEFT);
dawnGlfwInputBind(INPUT_BIND_RIGHT, GLFW_KEY_D);
dawnGlfwInputBind(INPUT_BIND_RIGHT, GLFW_KEY_RIGHT);
dawnGlfwInputBind(INPUT_BIND_ACCEPT, GLFW_KEY_ENTER);
dawnGlfwInputBind(INPUT_BIND_ACCEPT, GLFW_KEY_SPACE);
dawnGlfwInputBind(INPUT_BIND_ACCEPT, GLFW_KEY_E);
dawnGlfwInputBind(INPUT_BIND_CANCEL, GLFW_KEY_ESCAPE);
dawnGlfwInputBind(INPUT_BIND_CANCEL, GLFW_KEY_Q);
dawnGlfwInputBind(INPUT_BIND_CANCEL, GLFW_KEY_BACKSPACE);
// Event callbacks
glfwSetFramebufferSizeCallback(DAWN_GLFW.window, &dawnGlfwOnResize);
// Main Loop
double_t time, newTime;
float_t fDelta;
uint8_t updateResult;
// Main Render Loop
time = 0.0f;
while(!glfwWindowShouldClose(DAWN_GLFW.window)) {
// Determine the delta.
newTime = glfwGetTime();
fDelta = (float_t)(newTime - time);
time = newTime;
// Perform updates
updateResult = gameUpdate(fDelta);
// Update GLFW
glfwSwapBuffers(DAWN_GLFW.window);
glfwPollEvents();
if(updateResult == GAME_UPDATE_RESULT_EXIT) break;
if(updateResult != GAME_UPDATE_RESULT_CONTINUE) break;
}
// Terminate GLFW
glfwDestroyWindow(DAWN_GLFW.window);
glfwTerminate();
// Reset the state incase of re-start.
DAWN_GLFW.window = NULL;
DAWN_GLFW.width = 0;
DAWN_GLFW.height = 0;
return updateResult;
}
void dawnGlfwInputBind(inputbind_t bind, int32_t key) {
assertTrue(bind < INPUT_BIND_COUNT, "Invalid input bind");
assertTrue(
DAWN_GLFW.inputBinds[bind].keyCount < DAWN_GLFW_BIND_KEYS_MAX,
"Too many keys bound to input"
);
DAWN_GLFW.inputBinds[bind].keys[DAWN_GLFW.inputBinds[bind].keyCount++] = key;
}
void dawnGlfwOnResize(GLFWwindow *window, int32_t w, int32_t h) {
assertTrue(window == DAWN_GLFW.window, "glfwOnResize: Window mismatch");
int32_t windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
// TODO: I may throttle this, it calls for every frame the window's resized.
backBufferSetSize(w, h, ((float_t)w) / ((float_t)windowWidth));
}

51
src/dawnglfw/dawnglfw.h Normal file
View File

@ -0,0 +1,51 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawnopengl.h"
#include "input.h"
#include <GLFW/glfw3.h>
#define DAWN_GLFW_WIDTH_DEFAULT 640
#define DAWN_GLFW_HEIGHT_DEFAULT 480
#define DAWN_GLFW_BIND_KEYS_MAX 8
typedef struct {
int32_t keys[DAWN_GLFW_BIND_KEYS_MAX];
int32_t keyCount;
} dawnglfwbind_t;
typedef struct {
GLFWwindow *window;
int32_t width;
int32_t height;
dawnglfwbind_t inputBinds[INPUT_BIND_COUNT];
} dawnglfw_t;
extern dawnglfw_t DAWN_GLFW;
/**
* Initialies GLFW.
*/
int32_t dawnGlfwStart();
/**
* Binds a key to an input.
*
* @param bind Bind to bind to.
* @param key Key to bind.
*/
void dawnGlfwInputBind(inputbind_t bind, int32_t key);
/**
* Event listener for when the window is resized.
*
* @param window The window that was resized.
* @param w The new width of the window.
* @param h The new height of the window.
*/
void dawnGlfwOnResize(GLFWwindow *window, int32_t w, int32_t h);

20
src/dawnglfw/input.c Normal file
View File

@ -0,0 +1,20 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "input.h"
#include "dawnglfw.h"
#include "assert/assert.h"
inputstate_t inputGetState(const inputbind_t bind) {
assertTrue(bind < INPUT_BIND_COUNT, "Invalid input bind index.");
dawnglfwbind_t *binds = &DAWN_GLFW.inputBinds[bind];
for(int32_t i = 0; i < binds->keyCount; i++) {
if(glfwGetKey(DAWN_GLFW.window, binds->keys[i]) == GLFW_PRESS) return true;
}
return false;
}

13
src/dawnglfw/linuxhost.c Normal file
View File

@ -0,0 +1,13 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "linuxhost.h"
#include "dawnglfw.h"
int32_t linuxHostBeginUpdating() {
return dawnGlfwStart();
}

View File

@ -6,12 +6,9 @@
*/
#pragma once
#include "dusk.h"
#include "dawn.h"
typedef struct {
} cutsceneevent_t;
typedef struct {
} cutscene_t;
/**
* Linux Host Compatibility
*/
int32_t linuxHostBeginUpdating();

View File

@ -13,6 +13,5 @@
int32_t main() {
gameInit();
return linuxHostBeginUpdating();
}

View File

@ -0,0 +1,26 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Libraries
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
font8x8
glad
)
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
add_subdirectory(assert)
add_subdirectory(display)
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
)

View File

@ -0,0 +1,10 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https:#opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
assertgl.c
)

View File

@ -0,0 +1,69 @@
/**
* Copyright (c) 2023 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dawnopengl.h"
#include "assert/assert.h"
#include "assertgl.h"
void assertNotGLErrorCheck(const char_t *file, const int32_t line) {
assertNotNull(file, "assertNotGLErrorCheck: File is an invlaid string");
assertTrue(line > 0, "assertNotGLErrorCheck: line is invalid");
char_t *buffer;
GLenum errorCode;
int32_t errorCount = 0;
while((errorCode = glGetError()) != GL_NO_ERROR) {
if(errorCount == 0) {
buffer = malloc(sizeof(char_t) * 4096);
sprintf(buffer, "assertNotGLErrorCheck: %s:%d\n", file, line);
}
errorCount++;
switch (errorCode) {
case GL_INVALID_ENUM:
sprintf(buffer, "%s\nINVALID_ENUM", buffer);
break;
case GL_INVALID_VALUE:
sprintf(buffer, "%s\nINVALID_ENUM", buffer);
break;
case GL_INVALID_OPERATION:
sprintf(buffer, "%s\nINVALID_OPERATION", buffer);
break;
// case GL_INVALID_FRAMEBUFFER_OPERATION:
// sprintf(buffer, "%s\nINVALID_FRAMEBUFFER_OPERATION", buffer);
// break;
case GL_OUT_OF_MEMORY:
sprintf(buffer, "%s\nOUT_OF_MEMORY", buffer);
break;
case GL_STACK_UNDERFLOW:
sprintf(buffer, "%s\nSTACK_UNDERFLOW", buffer);
break;
case GL_STACK_OVERFLOW:
sprintf(buffer, "%s\nSTACK_OVERFLOW", buffer);
break;
default:
sprintf(buffer, "%s\nUNKNOWN_ERROR", buffer);
break;
}
sprintf(buffer, "%s (%i)\n", buffer, errorCode);
}
if(errorCount > 0) {
assertUnreachable(buffer);
free(buffer);
}
}

View File

@ -0,0 +1,22 @@
/**
* Copyright (c) 2023 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawn.h"
/**
* Asserts that there are no OpenGL errors.
*
* @param file The file the assertion is being made in.
* @param line The line the assertion is being made in.
*/
void assertNotGLErrorCheck(const char_t *file, const int32_t line);
/**
* Asserts that there are no OpenGL errors.
*/
#define assertNoGLError() assertNotGLErrorCheck(__FILE__, __LINE__)

View File

@ -0,0 +1,10 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawn.h"
#include <glad/glad.h>

View File

@ -0,0 +1,13 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
display.c
backbuffer.c
)

View File

@ -0,0 +1,43 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "backbuffer.h"
#include "assert/assert.h"
#include "assert/assertgl.h"
#include "dawnopengl.h"
int32_t BACK_BUFFER_WIDTH;
int32_t BACK_BUFFER_HEIGHT;
float_t BACK_BUFFER_SCALE;
void backBufferSetSize(
const int32_t width,
const int32_t height,
const float_t scale
) {
assertTrue(width >= 0, "backBufferSetSize: Width must be >= 0");
assertTrue(height >= 0, "backBufferSetSize: Height must be >= 0");
assertTrue(scale > 0, "backBufferSetSize: Scale must be > 0");
BACK_BUFFER_WIDTH = width;
BACK_BUFFER_HEIGHT = height;
BACK_BUFFER_SCALE = scale;
}
void backBufferBind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
assertNoGLError();
glViewport(0, 0, BACK_BUFFER_WIDTH, BACK_BUFFER_HEIGHT);
assertNoGLError();
}
void backBufferClear() {
glClearColor(0,0,0,1);
assertNoGLError();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
assertNoGLError();
}

View File

@ -0,0 +1,40 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/color.h"
extern int32_t BACK_BUFFER_WIDTH;
extern int32_t BACK_BUFFER_HEIGHT;
extern float_t BACK_BUFFER_SCALE;
/**
* Called by the host whenever the backbuffer size changes. Also includes a
* scale parameter to deal with highdpi screens.
*
* @param width Width in pixels.
* @param height Height in pixels.
* @param scale Scale/Pixel density.
*/
void backBufferSetSize(
const int32_t width,
const int32_t height,
const float_t scale
);
/**
* Binds the backbuffer to the current framebuffer.
*/
void backBufferBind();
/**
* Clears the backbuffer.
*
* @param clearFlags Specifies rules on how to clear.
* @param color Color to use when clearing (assuming you are clearing colors).
*/
void backBufferClear();

View File

@ -0,0 +1,18 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/display.h"
#include "assert/assert.h"
void displayInit() {
}
void displayUpdate() {
}
void displayDispose() {
}

View File

@ -8,8 +8,9 @@
#pragma once
#include "display/color.h"
#define FRAME_WIDTH 30
#define FRAME_HEIGHT 12
#define FRAME_WIDTH 320/8
#define FRAME_HEIGHT 240/8
extern char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH];
extern uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH];

View File

@ -6,7 +6,6 @@
# Libraries
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
m
)
# Includes