This commit is contained in:
2025-08-20 21:56:55 -05:00
parent 84f2735246
commit 3a753b1299
10 changed files with 220 additions and 97 deletions

View File

@@ -51,13 +51,23 @@ add_executable(${DUSK_TARGET_NAME})
# Add tools
add_subdirectory(tools)
# Add libraries
if(DUSK_TARGET_SYSTEM STREQUAL "linux")
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)
target_link_libraries(${DUSK_TARGET_NAME} PRIVATE
SDL2::SDL2
OpenGL::GL
GL
)
endif()
# Add code
add_subdirectory(src)
# Include generated headers
target_include_directories(${DUSK_TARGET_NAME}
PUBLIC
${DUSK_GENERATED_HEADERS_DIR}
target_include_directories(${DUSK_TARGET_NAME} PUBLIC
${DUSK_GENERATED_HEADERS_DIR}
)
# Postbuild, create PBP file for PSP.

View File

@@ -28,8 +28,6 @@ void consoleInit() {
consolePrint(" = Dawn Console = ");
#if DUSK_CONSOLE_POSIX
threadInit(&CONSOLE.thread, consoleInputThread);
threadMutexInit(&CONSOLE.execMutex);
threadStartRequest(&CONSOLE.thread);
@@ -77,7 +75,9 @@ void consolePrint(const char_t *message, ...) {
}
void consoleExec(const char_t *line) {
threadMutexLock(&CONSOLE.execMutex);
#if DUSK_CONSOLE_POSIX
threadMutexLock(&CONSOLE.execMutex);
#endif
assertNotNull(line, "line must not be NULL");
assertTrue(
@@ -279,59 +279,28 @@ void consoleExec(const char_t *line) {
}
}
threadMutexUnlock(&CONSOLE.execMutex);
#if DUSK_CONSOLE_POSIX
threadMutexUnlock(&CONSOLE.execMutex);
#endif
}
// May move these later
void consoleUpdate() {
#if DUSK_CONSOLE_POSIX
threadMutexLock(&CONSOLE.execMutex);
#endif
threadMutexLock(&CONSOLE.execMutex);
for(uint32_t i = 0; i < CONSOLE.execBufferCount; i++) {
consolecmdexec_t *exec = &CONSOLE.execBuffer[i];
assertNotNull(exec->cmd, "Command execution has no command.");
exec->cmd->function(exec);
}
threadMutexUnlock(&CONSOLE.execMutex);
// #if DUSK_KEYBOARD_SUPPORT == 1
// uint8_t key;
// while((key = inputKeyboardPop()) != 0) {
// printf("Key pressed: %c\n", key);
// switch(key) {
// case 0:
// break;
// case INPUT_KEY_ENTER:
// consoleExec(CONSOLE.inputBuffer);
// CONSOLE.inputIndex = 0;
// CONSOLE.inputBuffer[0] = '\0';
// break;
// case INPUT_KEY_BACKSPACE:
// if(CONSOLE.inputIndex > 0) {
// CONSOLE.inputIndex--;
// CONSOLE.inputBuffer[CONSOLE.inputIndex] = '\0';
// }
// break;
// default:
// if(
// key >= INPUT_KEY_ASCII_START && key <= INPUT_KEY_ASCII_END &&
// CONSOLE.inputIndex < CONSOLE_LINE_MAX - 1
// ) {
// CONSOLE.inputBuffer[CONSOLE.inputIndex++] = key;
// CONSOLE.inputBuffer[CONSOLE.inputIndex] = '\0';
// }
// break;
// }
// }
// #endif
// Clear the exec buffer
CONSOLE.execBufferCount = 0;
#if DUSK_CONSOLE_POSIX
threadMutexUnlock(&CONSOLE.execMutex);
#endif
}
void consoleDispose(void) {
@@ -343,7 +312,6 @@ void consoleDispose(void) {
consolePrint(" = Console shutting down = ");
}
#if DUSK_CONSOLE_POSIX
void consoleInputThread(thread_t *thread) {
assertNotNull(thread, "Thread cannot be NULL.");

View File

@@ -6,5 +6,12 @@
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
render.c
)
display.c
)
if(DUSK_TARGET_SYSTEM STREQUAL "linux")
target_compile_definitions(${DUSK_TARGET_NAME}
PRIVATE
DUSK_DISPLAY_SDL2=1
)
endif()

96
src/display/display.c Normal file
View File

@@ -0,0 +1,96 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/display.h"
#include "console/console.h"
display_t DISPLAY;
errorret_t displayInit(void) {
#if DUSK_DISPLAY_SDL2
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) {
errorThrow("SDL Failed to Initialize: %s", SDL_GetError());
}
// Set OpenGL attributes (Needs to be done now or later?)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Create window with OpenGL flag.
DISPLAY.window = SDL_CreateWindow(
"DuskSDL2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
DISPLAY_WINDOW_WIDTH_DEFAULT,
DISPLAY_WINDOW_HEIGHT_DEFAULT,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
SDL_WINDOW_OPENGL
);
if(!DISPLAY.window) {
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
}
// Create OpenGL context
DISPLAY.glContext = SDL_GL_CreateContext(DISPLAY.window);
if(!DISPLAY.glContext) {
errorThrow("SDL_GL_CreateContext failed: %s", SDL_GetError());
}
SDL_GL_SetSwapInterval(1);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);// PSP defaults this on?
glShadeModel(GL_SMOOTH); // Fixes color on PSP?
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnableClientState(GL_COLOR_ARRAY);// To confirm: every frame on PSP?
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
#endif
// For now, we just return an OK error.
errorOk();
}
errorret_t displayUpdate(void) {
#if DUSK_DISPLAY_SDL2
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
consoleExec("quit");
break;
default:
break;
}
}
SDL_GL_SwapWindow(DISPLAY.window);
#endif
// For now, we just return an OK error.
errorOk();
}
errorret_t displayDispose(void) {
#if DUSK_DISPLAY_SDL2
if(DISPLAY.glContext) {
SDL_GL_DeleteContext(DISPLAY.glContext);
DISPLAY.glContext = NULL;
}
if(DISPLAY.window) {
SDL_DestroyWindow(DISPLAY.window);
DISPLAY.window = NULL;
}
SDL_Quit();
#endif
// For now, we just return an OK error.
errorOk();
}

54
src/display/display.h Normal file
View File

@@ -0,0 +1,54 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#if DUSK_DISPLAY_SDL2
#include <SDL2/SDL.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#endif
#ifndef DISPLAY_WIDTH
#define DISPLAY_WIDTH 320
#endif
#ifndef DISPLAY_HEIGHT
#define DISPLAY_HEIGHT 240
#endif
#ifndef DISPLAY_WINDOW_WIDTH_DEFAULT
#define DISPLAY_WINDOW_WIDTH_DEFAULT DISPLAY_WIDTH
#endif
#ifndef DISPLAY_WINDOW_HEIGHT_DEFAULT
#define DISPLAY_WINDOW_HEIGHT_DEFAULT DISPLAY_HEIGHT
#endif
typedef struct {
#if DUSK_DISPLAY_SDL2
SDL_Window *window;
SDL_GLContext glContext;
#endif
} display_t;
extern display_t DISPLAY;
/**
* Initializes the display system.
*/
errorret_t displayInit(void);
/**
* Tells the display system to actually draw the frame.
*/
errorret_t displayUpdate(void);
/**
* Disposes of the display system.
*/
errorret_t displayDispose(void);

View File

@@ -1,8 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/render.h"

View File

@@ -1,31 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#ifndef RENDER_WIDTH
#define RENDER_WIDTH 320
#endif
#ifndef RENDER_HEIGHT
#define RENDER_HEIGHT 240
#endif
/**
* Initializes the rendering system.
*/
errorret_t renderInit(void);
/**
* Tells the rendering system to actually draw the frame.
*/
errorret_t renderDraw(void);
/**
* Disposes of the rendering system.
*/
errorret_t renderDispose(void);

View File

@@ -9,23 +9,33 @@
#include "util/memory.h"
#include "time/time.h"
#include "console/console.h"
#include "display/display.h"
engine_t ENGINE;
void engineInit(void) {
errorret_t engineInit(void) {
memoryZero(&ENGINE, sizeof(engine_t));
ENGINE.running = true;
// Init systems. Order is important.
timeInit();
consoleInit();
errorChain(displayInit());
errorOk();
}
void engineUpdate(void) {
errorret_t engineUpdate(void) {
timeUpdate();
consoleUpdate();
errorChain(displayUpdate());
errorOk();
}
void engineDispose(void) {
errorret_t engineDispose(void) {
errorChain(displayDispose());
consoleDispose();
errorOk();
}

View File

@@ -6,7 +6,7 @@
*/
#pragma once
#include "dusk.h"
#include "error/error.h"
typedef struct {
bool_t running;
@@ -17,14 +17,14 @@ extern engine_t ENGINE;
/**
* Initializes the engine.
*/
void engineInit(void);
errorret_t engineInit(void);
/**
* Updates the engine.
*/
void engineUpdate(void);
errorret_t engineUpdate(void);
/**
* Shuts down the engine.
*/
void engineDispose(void);
errorret_t engineDispose(void);

View File

@@ -8,10 +8,27 @@
#include "engine/engine.h"
int main(int argc, char **argv) {
engineInit();
errorret_t ret;
ret = engineInit();
if(ret.code != ERROR_OK) {
errorCatch(errorPrint(ret));
return ret.code;
}
do {
engineUpdate();
ret = engineUpdate();
if(ret.code != ERROR_OK) {
errorCatch(errorPrint(ret));
return ret.code;
}
} while(ENGINE.running);
engineDispose();
ret = engineDispose();
if(ret.code != ERROR_OK) {
errorCatch(errorPrint(ret));
return ret.code;
}
return 0;
}