378 lines
11 KiB
C
378 lines
11 KiB
C
#include "pxl8_sdl3.h"
|
|
|
|
#define SDL_MAIN_USE_CALLBACKS
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_main.h>
|
|
|
|
#include "pxl8_atlas.h"
|
|
#include "pxl8_macros.h"
|
|
#include "pxl8_sys.h"
|
|
|
|
typedef struct pxl8_sdl3_context {
|
|
SDL_Texture* atlas_texture;
|
|
SDL_Texture* framebuffer_texture;
|
|
SDL_Renderer* renderer;
|
|
SDL_Window* window;
|
|
|
|
u32* rgba_buffer;
|
|
size_t rgba_buffer_size;
|
|
|
|
u32 atlas_width;
|
|
u32 atlas_height;
|
|
} pxl8_sdl3_context;
|
|
|
|
static void* sdl3_create(pxl8_color_mode mode, pxl8_resolution resolution,
|
|
const char* title, i32 win_w, i32 win_h) {
|
|
(void)mode;
|
|
|
|
pxl8_sdl3_context* ctx = (pxl8_sdl3_context*)SDL_calloc(1, sizeof(pxl8_sdl3_context));
|
|
if (!ctx) {
|
|
pxl8_error("Failed to allocate SDL3 context");
|
|
return NULL;
|
|
}
|
|
|
|
pxl8_size fb_size = pxl8_get_resolution_dimensions(resolution);
|
|
|
|
ctx->window = SDL_CreateWindow(title, win_w, win_h, SDL_WINDOW_RESIZABLE);
|
|
if (!ctx->window) {
|
|
pxl8_error("Failed to create window: %s", SDL_GetError());
|
|
SDL_free(ctx);
|
|
return NULL;
|
|
}
|
|
|
|
ctx->renderer = SDL_CreateRenderer(ctx->window, NULL);
|
|
if (!ctx->renderer) {
|
|
pxl8_error("Failed to create renderer: %s", SDL_GetError());
|
|
SDL_DestroyWindow(ctx->window);
|
|
SDL_free(ctx);
|
|
return NULL;
|
|
}
|
|
|
|
if (!SDL_SetRenderVSync(ctx->renderer, 1)) {
|
|
pxl8_error("Failed to set vsync: %s", SDL_GetError());
|
|
}
|
|
|
|
ctx->framebuffer_texture = SDL_CreateTexture(ctx->renderer,
|
|
SDL_PIXELFORMAT_RGBA32,
|
|
SDL_TEXTUREACCESS_STREAMING,
|
|
fb_size.w, fb_size.h);
|
|
if (!ctx->framebuffer_texture) {
|
|
pxl8_error("Failed to create framebuffer texture: %s", SDL_GetError());
|
|
SDL_DestroyRenderer(ctx->renderer);
|
|
SDL_DestroyWindow(ctx->window);
|
|
SDL_free(ctx);
|
|
return NULL;
|
|
}
|
|
|
|
SDL_SetTextureScaleMode(ctx->framebuffer_texture, SDL_SCALEMODE_NEAREST);
|
|
|
|
ctx->rgba_buffer = NULL;
|
|
ctx->rgba_buffer_size = 0;
|
|
|
|
return ctx;
|
|
}
|
|
|
|
static void sdl3_destroy(void* platform_data) {
|
|
if (!platform_data) return;
|
|
|
|
pxl8_sdl3_context* ctx = (pxl8_sdl3_context*)platform_data;
|
|
|
|
if (ctx->rgba_buffer) {
|
|
SDL_free(ctx->rgba_buffer);
|
|
}
|
|
if (ctx->atlas_texture) {
|
|
SDL_DestroyTexture(ctx->atlas_texture);
|
|
}
|
|
if (ctx->framebuffer_texture) {
|
|
SDL_DestroyTexture(ctx->framebuffer_texture);
|
|
}
|
|
if (ctx->renderer) {
|
|
SDL_DestroyRenderer(ctx->renderer);
|
|
}
|
|
if (ctx->window) {
|
|
SDL_DestroyWindow(ctx->window);
|
|
}
|
|
|
|
SDL_free(ctx);
|
|
}
|
|
|
|
static u64 sdl3_get_ticks(void) {
|
|
return SDL_GetTicksNS();
|
|
}
|
|
|
|
|
|
static void sdl3_present(void* platform_data) {
|
|
if (!platform_data) return;
|
|
|
|
pxl8_sdl3_context* ctx = (pxl8_sdl3_context*)platform_data;
|
|
|
|
SDL_SetRenderDrawColor(ctx->renderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(ctx->renderer);
|
|
|
|
if (ctx->framebuffer_texture) {
|
|
SDL_RenderTexture(ctx->renderer, ctx->framebuffer_texture, NULL, NULL);
|
|
}
|
|
|
|
SDL_RenderPresent(ctx->renderer);
|
|
}
|
|
|
|
static void sdl3_upload_framebuffer(void* platform_data, const u8* fb,
|
|
i32 w, i32 h, const u32* palette,
|
|
pxl8_color_mode mode) {
|
|
if (!platform_data || !fb) return;
|
|
|
|
pxl8_sdl3_context* ctx = (pxl8_sdl3_context*)platform_data;
|
|
|
|
if (mode == PXL8_COLOR_MODE_HICOLOR) {
|
|
SDL_UpdateTexture(ctx->framebuffer_texture, NULL, fb, w * 4);
|
|
} else {
|
|
size_t needed_size = w * h;
|
|
|
|
if (ctx->rgba_buffer_size < needed_size) {
|
|
ctx->rgba_buffer = (u32*)SDL_realloc(ctx->rgba_buffer, needed_size * 4);
|
|
ctx->rgba_buffer_size = needed_size;
|
|
}
|
|
|
|
if (!ctx->rgba_buffer) return;
|
|
|
|
u32 palette_size = pxl8_get_palette_size(mode);
|
|
|
|
for (i32 i = 0; i < w * h; i++) {
|
|
u8 index = fb[i];
|
|
ctx->rgba_buffer[i] = (index < palette_size) ? palette[index] : 0xFF000000;
|
|
}
|
|
|
|
SDL_UpdateTexture(ctx->framebuffer_texture, NULL, ctx->rgba_buffer, w * 4);
|
|
}
|
|
}
|
|
|
|
static void sdl3_upload_atlas(void* platform_data, const pxl8_atlas* atlas,
|
|
const u32* palette, pxl8_color_mode mode) {
|
|
if (!platform_data || !atlas) return;
|
|
|
|
pxl8_sdl3_context* ctx = (pxl8_sdl3_context*)platform_data;
|
|
|
|
if (!pxl8_atlas_is_dirty(atlas)) return;
|
|
|
|
u32 atlas_w = pxl8_atlas_get_width(atlas);
|
|
u32 atlas_h = pxl8_atlas_get_height(atlas);
|
|
const u8* atlas_pixels = pxl8_atlas_get_pixels(atlas);
|
|
|
|
if (!ctx->atlas_texture || ctx->atlas_width != atlas_w || ctx->atlas_height != atlas_h) {
|
|
if (ctx->atlas_texture) {
|
|
SDL_DestroyTexture(ctx->atlas_texture);
|
|
}
|
|
|
|
ctx->atlas_texture = SDL_CreateTexture(
|
|
ctx->renderer,
|
|
SDL_PIXELFORMAT_RGBA32,
|
|
SDL_TEXTUREACCESS_STREAMING,
|
|
atlas_w,
|
|
atlas_h
|
|
);
|
|
|
|
if (!ctx->atlas_texture) {
|
|
pxl8_error("Failed to create atlas texture: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
SDL_SetTextureScaleMode(ctx->atlas_texture, SDL_SCALEMODE_NEAREST);
|
|
SDL_SetTextureBlendMode(ctx->atlas_texture, SDL_BLENDMODE_BLEND);
|
|
|
|
ctx->atlas_width = atlas_w;
|
|
ctx->atlas_height = atlas_h;
|
|
}
|
|
|
|
if (mode == PXL8_COLOR_MODE_HICOLOR) {
|
|
SDL_UpdateTexture(ctx->atlas_texture, NULL, atlas_pixels, atlas_w * 4);
|
|
} else {
|
|
size_t needed_size = atlas_w * atlas_h;
|
|
|
|
if (ctx->rgba_buffer_size < needed_size) {
|
|
ctx->rgba_buffer = (u32*)SDL_realloc(ctx->rgba_buffer, needed_size * 4);
|
|
ctx->rgba_buffer_size = needed_size;
|
|
}
|
|
|
|
if (!ctx->rgba_buffer) return;
|
|
|
|
u32 palette_size = pxl8_get_palette_size(mode);
|
|
|
|
for (u32 i = 0; i < atlas_w * atlas_h; i++) {
|
|
u8 index = atlas_pixels[i];
|
|
ctx->rgba_buffer[i] = (index < palette_size) ? palette[index] : 0x00000000;
|
|
}
|
|
|
|
SDL_UpdateTexture(ctx->atlas_texture, NULL, ctx->rgba_buffer, atlas_w * 4);
|
|
}
|
|
}
|
|
|
|
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
|
pxl8_error("SDL_Init failed: %s", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
pxl8* sys = pxl8_create(&pxl8_hal_sdl3);
|
|
if (!sys) {
|
|
pxl8_error("Failed to create pxl8 system");
|
|
SDL_Quit();
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
pxl8_result result = pxl8_init(sys, argc, argv);
|
|
if (result != PXL8_OK) {
|
|
pxl8_destroy(sys);
|
|
SDL_Quit();
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
*appstate = sys;
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
SDL_AppResult SDL_AppIterate(void* appstate) {
|
|
pxl8* sys = (pxl8*)appstate;
|
|
if (!sys) {
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
pxl8_result update_result = pxl8_update(sys);
|
|
if (update_result != PXL8_OK) {
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
pxl8_result frame_result = pxl8_frame(sys);
|
|
if (frame_result != PXL8_OK) {
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
return pxl8_is_running(sys) ? SDL_APP_CONTINUE : SDL_APP_SUCCESS;
|
|
}
|
|
|
|
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|
pxl8* sys = (pxl8*)appstate;
|
|
if (!sys) {
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
pxl8_input_state* input = pxl8_get_input(sys);
|
|
if (!input) {
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
switch (event->type) {
|
|
case SDL_EVENT_QUIT:
|
|
pxl8_set_running(sys, false);
|
|
break;
|
|
|
|
case SDL_EVENT_KEY_DOWN: {
|
|
SDL_Scancode scancode = event->key.scancode;
|
|
if (scancode < 256) {
|
|
if (!input->keys_down[scancode]) {
|
|
input->keys_pressed[scancode] = true;
|
|
}
|
|
input->keys_down[scancode] = true;
|
|
input->keys_released[scancode] = false;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_KEY_UP: {
|
|
SDL_Scancode scancode = event->key.scancode;
|
|
if (scancode < 256) {
|
|
input->keys_down[scancode] = false;
|
|
input->keys_pressed[scancode] = false;
|
|
input->keys_released[scancode] = true;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
|
|
u8 button = event->button.button - 1;
|
|
if (button < 3) {
|
|
if (!input->mouse_buttons_down[button]) {
|
|
input->mouse_buttons_pressed[button] = true;
|
|
}
|
|
input->mouse_buttons_down[button] = true;
|
|
input->mouse_buttons_released[button] = false;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_MOUSE_BUTTON_UP: {
|
|
u8 button = event->button.button - 1;
|
|
if (button < 3) {
|
|
input->mouse_buttons_down[button] = false;
|
|
input->mouse_buttons_pressed[button] = false;
|
|
input->mouse_buttons_released[button] = true;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_MOUSE_MOTION: {
|
|
pxl8_gfx* gfx = pxl8_get_gfx(sys);
|
|
if (!gfx) break;
|
|
|
|
input->mouse_dx = (i32)event->motion.xrel;
|
|
input->mouse_dy = (i32)event->motion.yrel;
|
|
|
|
i32 window_mouse_x = (i32)event->motion.x;
|
|
i32 window_mouse_y = (i32)event->motion.y;
|
|
|
|
SDL_Window* window = SDL_GetWindowFromID(event->motion.windowID);
|
|
if (!window) break;
|
|
|
|
i32 window_width, window_height;
|
|
SDL_GetWindowSize(window, &window_width, &window_height);
|
|
|
|
pxl8_resolution resolution = pxl8_get_resolution(sys);
|
|
pxl8_size render_size = pxl8_get_resolution_dimensions(resolution);
|
|
|
|
pxl8_bounds window_bounds = {0, 0, window_width, window_height};
|
|
pxl8_viewport vp = pxl8_gfx_viewport(window_bounds, render_size.w, render_size.h);
|
|
|
|
input->mouse_x = (i32)((window_mouse_x - vp.offset_x) / vp.scale);
|
|
input->mouse_y = (i32)((window_mouse_y - vp.offset_y) / vp.scale);
|
|
break;
|
|
}
|
|
|
|
case SDL_EVENT_MOUSE_WHEEL: {
|
|
input->mouse_wheel_x = (i32)event->wheel.x;
|
|
input->mouse_wheel_y = (i32)event->wheel.y;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
|
|
(void)result;
|
|
|
|
pxl8* sys = (pxl8*)appstate;
|
|
if (sys) {
|
|
pxl8_quit(sys);
|
|
pxl8_destroy(sys);
|
|
}
|
|
|
|
SDL_Quit();
|
|
}
|
|
|
|
static void sdl3_set_relative_mouse_mode(void* platform_data, bool enabled) {
|
|
if (!platform_data) return;
|
|
|
|
pxl8_sdl3_context* ctx = (pxl8_sdl3_context*)platform_data;
|
|
if (!SDL_SetWindowRelativeMouseMode(ctx->window, enabled)) {
|
|
pxl8_error("Failed to set relative mouse mode: %s", SDL_GetError());
|
|
}
|
|
}
|
|
|
|
const pxl8_hal pxl8_hal_sdl3 = {
|
|
.create = sdl3_create,
|
|
.destroy = sdl3_destroy,
|
|
.get_ticks = sdl3_get_ticks,
|
|
.present = sdl3_present,
|
|
.upload_atlas = sdl3_upload_atlas,
|
|
.upload_framebuffer = sdl3_upload_framebuffer,
|
|
.set_relative_mouse_mode = sdl3_set_relative_mouse_mode,
|
|
};
|