About to add scene rendering
This commit is contained in:
@@ -10,8 +10,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
|||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
||||||
|
|
||||||
if(NOT DEFINED DUSK_TARGET_SYSTEM)
|
if(NOT DEFINED DUSK_TARGET_SYSTEM)
|
||||||
# set(DUSK_TARGET_SYSTEM "linux")
|
set(DUSK_TARGET_SYSTEM "linux")
|
||||||
set(DUSK_TARGET_SYSTEM "psp")
|
# set(DUSK_TARGET_SYSTEM "psp")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Prep cache
|
# Prep cache
|
||||||
|
@@ -26,9 +26,9 @@ void gameInit(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void gameUpdate(void) {
|
void gameUpdate(void) {
|
||||||
// overworldUpdate();
|
overworldUpdate();
|
||||||
// uiTextboxUpdate();
|
uiTextboxUpdate();
|
||||||
// eventUpdate();
|
eventUpdate();
|
||||||
|
|
||||||
consoleUpdate();
|
consoleUpdate();
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
|
@@ -27,7 +27,7 @@ target_include_directories(${DUSK_TARGET_NAME}
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
input.c
|
dusksdl2input.c
|
||||||
main.c
|
main.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -10,6 +10,7 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
renderbackbuffer.c
|
renderbackbuffer.c
|
||||||
rendertext.c
|
rendertext.c
|
||||||
renderconsole.c
|
renderconsole.c
|
||||||
|
renderscene.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
|
@@ -11,6 +11,8 @@
|
|||||||
#include "rendertext.h"
|
#include "rendertext.h"
|
||||||
#include "renderconsole.h"
|
#include "renderconsole.h"
|
||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
|
#include "dusksdl2input.h"
|
||||||
|
#include "renderscene.h"
|
||||||
|
|
||||||
SDL_Window *RENDER_WINDOW;
|
SDL_Window *RENDER_WINDOW;
|
||||||
SDL_Renderer *RENDER_RENDERER;
|
SDL_Renderer *RENDER_RENDERER;
|
||||||
@@ -18,7 +20,13 @@ bool_t RENDER_RUNNING;
|
|||||||
|
|
||||||
errorret_t renderInit(void) {
|
errorret_t renderInit(void) {
|
||||||
// Init SDL
|
// Init SDL
|
||||||
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) {
|
uint32_t flags = SDL_INIT_VIDEO;
|
||||||
|
|
||||||
|
#if INPUT_SUPPORT_GAMEPAD
|
||||||
|
flags |= SDL_INIT_GAMECONTROLLER;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(SDL_Init(flags) != 0) {
|
||||||
errorThrow(
|
errorThrow(
|
||||||
"SDL Failed to Initialize: %s",
|
"SDL Failed to Initialize: %s",
|
||||||
SDL_GetError()
|
SDL_GetError()
|
||||||
@@ -32,8 +40,7 @@ errorret_t renderInit(void) {
|
|||||||
SDL_WINDOWPOS_UNDEFINED,
|
SDL_WINDOWPOS_UNDEFINED,
|
||||||
RENDER_WINDOW_WIDTH_DEFAULT,
|
RENDER_WINDOW_WIDTH_DEFAULT,
|
||||||
RENDER_WINDOW_HEIGHT_DEFAULT,
|
RENDER_WINDOW_HEIGHT_DEFAULT,
|
||||||
0
|
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
|
||||||
// SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
|
|
||||||
);
|
);
|
||||||
if(!RENDER_WINDOW) {
|
if(!RENDER_WINDOW) {
|
||||||
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
|
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
|
||||||
@@ -54,6 +61,7 @@ errorret_t renderInit(void) {
|
|||||||
|
|
||||||
// Init other things
|
// Init other things
|
||||||
renderTextInit();
|
renderTextInit();
|
||||||
|
renderSceneInit();
|
||||||
|
|
||||||
// Mark ready.
|
// Mark ready.
|
||||||
RENDER_RUNNING = true;
|
RENDER_RUNNING = true;
|
||||||
@@ -78,6 +86,7 @@ errorret_t renderDraw(void) {
|
|||||||
renderBackBufferBind();
|
renderBackBufferBind();
|
||||||
|
|
||||||
// Draw everything
|
// Draw everything
|
||||||
|
renderSceneDraw();
|
||||||
renderConsoleDraw();
|
renderConsoleDraw();
|
||||||
|
|
||||||
// Unbind the backbuffer
|
// Unbind the backbuffer
|
||||||
@@ -93,6 +102,9 @@ errorret_t renderDraw(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t renderDispose(void) {
|
errorret_t renderDispose(void) {
|
||||||
|
renderTextDispose();
|
||||||
|
renderSceneDispose();
|
||||||
|
|
||||||
renderBackBufferDispose();
|
renderBackBufferDispose();
|
||||||
|
|
||||||
SDL_DestroyRenderer(RENDER_RENDERER);
|
SDL_DestroyRenderer(RENDER_RENDERER);
|
||||||
|
20
src/dusksdl2/display/renderscene.c
Normal file
20
src/dusksdl2/display/renderscene.c
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "renderscene.h"
|
||||||
|
|
||||||
|
void renderSceneInit(void) {
|
||||||
|
// Initialize scene-related resources here
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderSceneDraw(void) {
|
||||||
|
// Draw the current scene here
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderSceneDispose(void) {
|
||||||
|
// Dispose of scene-related resources here
|
||||||
|
}
|
24
src/dusksdl2/display/renderscene.h
Normal file
24
src/dusksdl2/display/renderscene.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dusk.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the render scene module.
|
||||||
|
*/
|
||||||
|
void renderSceneInit(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the current scene.
|
||||||
|
*/
|
||||||
|
void renderSceneDraw(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of the render scene module.
|
||||||
|
*/
|
||||||
|
void renderSceneDispose(void);
|
@@ -27,9 +27,11 @@ void renderTextInit(void) {
|
|||||||
);
|
);
|
||||||
assertNotNull(surface, "Failed to create surface for text rendering");
|
assertNotNull(surface, "Failed to create surface for text rendering");
|
||||||
|
|
||||||
|
// Get the pixel format and pitch
|
||||||
const int32_t pitch_px = surface->pitch / 4;
|
const int32_t pitch_px = surface->pitch / 4;
|
||||||
uint32_t *pixels = (uint32_t *)surface->pixels;
|
uint32_t *pixels = (uint32_t *)surface->pixels;
|
||||||
|
|
||||||
|
// Buffer the pixels.
|
||||||
for(int tileIndex = 0; tileIndex < FONT_TILE_COUNT; ++tileIndex) {
|
for(int tileIndex = 0; tileIndex < FONT_TILE_COUNT; ++tileIndex) {
|
||||||
const int32_t tileX = (tileIndex % FONT_COLUMN_COUNT) * FONT_TILE_WIDTH;
|
const int32_t tileX = (tileIndex % FONT_COLUMN_COUNT) * FONT_TILE_WIDTH;
|
||||||
const int32_t tileY = (tileIndex / FONT_COLUMN_COUNT) * FONT_TILE_HEIGHT;
|
const int32_t tileY = (tileIndex / FONT_COLUMN_COUNT) * FONT_TILE_HEIGHT;
|
||||||
@@ -37,20 +39,21 @@ void renderTextInit(void) {
|
|||||||
|
|
||||||
for (int y = 0; y < FONT_TILE_HEIGHT; ++y) {
|
for (int y = 0; y < FONT_TILE_HEIGHT; ++y) {
|
||||||
for (int x = 0; x < FONT_TILE_WIDTH; ++x) {
|
for (int x = 0; x < FONT_TILE_WIDTH; ++x) {
|
||||||
const uint8_t color = tile[y * FONT_TILE_WIDTH + x] ? 0xFF : 0;
|
pixels[(tileY + y) * pitch_px + (tileX + x)] = (
|
||||||
// Convert to RGBA (8 bits per channel)
|
tile[y * FONT_TILE_WIDTH + x] ? 0xFFFFFFFF : 0x00000000
|
||||||
const uint32_t rgba = color ? 0xFFFFFFFF : 0x00000000;
|
);
|
||||||
pixels[(tileY + y) * pitch_px + (tileX + x)] = rgba;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create texture from the surface
|
||||||
RENDER_TEXT_TEXTURE = SDL_CreateTextureFromSurface(
|
RENDER_TEXT_TEXTURE = SDL_CreateTextureFromSurface(
|
||||||
RENDER_RENDERER,
|
RENDER_RENDERER,
|
||||||
surface
|
surface
|
||||||
);
|
);
|
||||||
assertNotNull(RENDER_TEXT_TEXTURE, "Failed to create texture from surface");
|
assertNotNull(RENDER_TEXT_TEXTURE, "Failed to create texture from surface");
|
||||||
|
|
||||||
|
// Cleanup the surface
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
SDL_SetTextureBlendMode(RENDER_TEXT_TEXTURE, SDL_BLENDMODE_BLEND);
|
SDL_SetTextureBlendMode(RENDER_TEXT_TEXTURE, SDL_BLENDMODE_BLEND);
|
||||||
}
|
}
|
||||||
@@ -110,3 +113,9 @@ void renderTextDraw(
|
|||||||
posX += FONT_TILE_WIDTH;
|
posX += FONT_TILE_WIDTH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void renderTextDispose(void) {
|
||||||
|
assertNotNull(RENDER_TEXT_TEXTURE, "Texture cannot be NULL");
|
||||||
|
SDL_DestroyTexture(RENDER_TEXT_TEXTURE);
|
||||||
|
RENDER_TEXT_TEXTURE = NULL;
|
||||||
|
}
|
@@ -41,3 +41,8 @@ void renderTextDraw(
|
|||||||
const float_t y,
|
const float_t y,
|
||||||
const char_t *text
|
const char_t *text
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of the text rendering system, freeing any allocated resources.
|
||||||
|
*/
|
||||||
|
void renderTextDispose(void);
|
44
src/dusksdl2/dusksdl2input.c
Normal file
44
src/dusksdl2/dusksdl2input.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "dusksdl2input.h"
|
||||||
|
|
||||||
|
uint8_t inputStateGet() {
|
||||||
|
uint8_t state = 0;
|
||||||
|
|
||||||
|
#if INPUT_SUPPORT_GAMEPAD
|
||||||
|
// Get gamepad state.
|
||||||
|
for(int32_t i = 0; i < SDL_NumJoysticks(); i++) {
|
||||||
|
if(!SDL_IsGameController(i)) continue;
|
||||||
|
|
||||||
|
SDL_GameController *controller = SDL_GameControllerOpen(i);
|
||||||
|
if(!controller) continue;
|
||||||
|
|
||||||
|
inputsdlbuttonmap_t *map = INPUT_SDL_BUTTON_MAP;
|
||||||
|
do {
|
||||||
|
if(SDL_GameControllerGetButton(controller, map->button)) {
|
||||||
|
state |= map->bind;
|
||||||
|
}
|
||||||
|
map++;
|
||||||
|
} while(map->bind != 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Get keyboard state.
|
||||||
|
#if INPUT_SUPPORT_KEYBOARD
|
||||||
|
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
|
||||||
|
inputsdlkbmap_t *kbmap = INPUT_SDL_KEYBOARD_MAP;
|
||||||
|
do {
|
||||||
|
if(keyboardState[kbmap->code]) {
|
||||||
|
state |= kbmap->bind;
|
||||||
|
}
|
||||||
|
kbmap++;
|
||||||
|
} while(kbmap->bind != 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return state;
|
||||||
|
}
|
52
src/dusksdl2/dusksdl2input.h
Normal file
52
src/dusksdl2/dusksdl2input.h
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dusksdl2.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
|
#ifndef INPUT_SUPPORT_GAMEPAD
|
||||||
|
#define INPUT_SUPPORT_GAMEPAD 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef INPUT_SUPPORT_KEYBOARD
|
||||||
|
#define INPUT_SUPPORT_KEYBOARD 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if INPUT_SUPPORT_GAMEPAD
|
||||||
|
typedef struct {
|
||||||
|
const SDL_GameControllerButton button;
|
||||||
|
const uint8_t bind;
|
||||||
|
} inputsdlbuttonmap_t;
|
||||||
|
|
||||||
|
static const inputsdlbuttonmap_t INPUT_SDL_BUTTON_MAP[] = {
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_UP, INPUT_BIND_UP },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_DOWN, INPUT_BIND_DOWN },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_LEFT, INPUT_BIND_LEFT },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_RIGHT, INPUT_BIND_RIGHT },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_A, INPUT_BIND_ACTION },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_B, INPUT_BIND_CANCEL },
|
||||||
|
{ 0, 0 }
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if INPUT_SUPPORT_KEYBOARD
|
||||||
|
typedef struct {
|
||||||
|
SDL_Scancode code;
|
||||||
|
uint8_t bind;
|
||||||
|
} inputsdlkbmap_t;
|
||||||
|
|
||||||
|
static const inputsdlkbmap_t INPUT_SDL_KEYBOARD_MAP[] = {
|
||||||
|
{ SDL_SCANCODE_W, INPUT_BIND_UP },
|
||||||
|
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
|
||||||
|
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
|
||||||
|
{ SDL_SCANCODE_D, INPUT_BIND_RIGHT },
|
||||||
|
{ SDL_SCANCODE_SPACE, INPUT_BIND_ACTION },
|
||||||
|
{ SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL },
|
||||||
|
{ 0, 0 }
|
||||||
|
};
|
||||||
|
#endif
|
@@ -1,71 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "dusksdl2.h"
|
|
||||||
#include "input.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const SDL_GameControllerButton button;
|
|
||||||
const uint8_t bind;
|
|
||||||
} inputsdlbuttonmap_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
SDL_Scancode code;
|
|
||||||
uint8_t bind;
|
|
||||||
} inputsdlkbmap_t;
|
|
||||||
|
|
||||||
inputsdlbuttonmap_t INPUT_SDL_BUTTON_MAP[] = {
|
|
||||||
{ SDL_CONTROLLER_BUTTON_DPAD_UP, INPUT_BIND_UP },
|
|
||||||
{ SDL_CONTROLLER_BUTTON_DPAD_DOWN, INPUT_BIND_DOWN },
|
|
||||||
{ SDL_CONTROLLER_BUTTON_DPAD_LEFT, INPUT_BIND_LEFT },
|
|
||||||
{ SDL_CONTROLLER_BUTTON_DPAD_RIGHT, INPUT_BIND_RIGHT },
|
|
||||||
{ SDL_CONTROLLER_BUTTON_A, INPUT_BIND_ACTION },
|
|
||||||
{ SDL_CONTROLLER_BUTTON_B, INPUT_BIND_CANCEL },
|
|
||||||
{ 0, 0 }
|
|
||||||
};
|
|
||||||
|
|
||||||
inputsdlkbmap_t INPUT_SDL_KEYBOARD_MAP[] = {
|
|
||||||
{ SDL_SCANCODE_W, INPUT_BIND_UP },
|
|
||||||
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
|
|
||||||
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
|
|
||||||
{ SDL_SCANCODE_D, INPUT_BIND_RIGHT },
|
|
||||||
{ SDL_SCANCODE_SPACE, INPUT_BIND_ACTION },
|
|
||||||
{ SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL },
|
|
||||||
{ 0, 0 }
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t inputStateGet() {
|
|
||||||
uint8_t state = 0;
|
|
||||||
|
|
||||||
// Get gamepad state.
|
|
||||||
for(int32_t i = 0; i < SDL_NumJoysticks(); i++) {
|
|
||||||
if(!SDL_IsGameController(i)) continue;
|
|
||||||
|
|
||||||
SDL_GameController *controller = SDL_GameControllerOpen(i);
|
|
||||||
if(!controller) continue;
|
|
||||||
|
|
||||||
inputsdlbuttonmap_t *map = INPUT_SDL_BUTTON_MAP;
|
|
||||||
do {
|
|
||||||
if(SDL_GameControllerGetButton(controller, map->button)) {
|
|
||||||
state |= map->bind;
|
|
||||||
}
|
|
||||||
map++;
|
|
||||||
} while(map->bind != 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get keyboard state.
|
|
||||||
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
|
|
||||||
inputsdlkbmap_t *kbmap = INPUT_SDL_KEYBOARD_MAP;
|
|
||||||
do {
|
|
||||||
if(keyboardState[kbmap->code]) {
|
|
||||||
state |= kbmap->bind;
|
|
||||||
}
|
|
||||||
kbmap++;
|
|
||||||
} while(kbmap->bind != 0);
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
|
@@ -25,13 +25,6 @@ int main(int argc, char *argv[]) {
|
|||||||
while(RENDER_RUNNING) {
|
while(RENDER_RUNNING) {
|
||||||
gameUpdate();
|
gameUpdate();
|
||||||
mainError(renderDraw());
|
mainError(renderDraw());
|
||||||
|
|
||||||
if(inputPressed(INPUT_BIND_UP)) consolePrint("Up pressed");
|
|
||||||
if(inputPressed(INPUT_BIND_DOWN)) consolePrint("Down pressed");
|
|
||||||
if(inputPressed(INPUT_BIND_LEFT)) consolePrint("Left pressed");
|
|
||||||
if(inputPressed(INPUT_BIND_RIGHT)) consolePrint("Right pressed");
|
|
||||||
if(inputPressed(INPUT_BIND_ACTION)) consolePrint("Action pressed");
|
|
||||||
if(inputPressed(INPUT_BIND_CANCEL)) consolePrint("Cancel pressed");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gameDispose();
|
gameDispose();
|
||||||
|
Reference in New Issue
Block a user