improve 3d renderer
This commit is contained in:
parent
f19b06d705
commit
a9de20cdea
17 changed files with 572 additions and 489 deletions
|
|
@ -31,17 +31,7 @@ static void* sdl3_create(pxl8_color_mode mode, pxl8_resolution resolution,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
i32 fb_w, fb_h;
|
||||
switch (resolution) {
|
||||
case PXL8_RESOLUTION_240x160: fb_w = 240; fb_h = 160; break;
|
||||
case PXL8_RESOLUTION_320x180: fb_w = 320; fb_h = 180; break;
|
||||
case PXL8_RESOLUTION_320x240: fb_w = 320; fb_h = 240; break;
|
||||
case PXL8_RESOLUTION_640x360: fb_w = 640; fb_h = 360; break;
|
||||
case PXL8_RESOLUTION_640x480: fb_w = 640; fb_h = 480; break;
|
||||
case PXL8_RESOLUTION_800x600: fb_w = 800; fb_h = 600; break;
|
||||
case PXL8_RESOLUTION_960x540: fb_w = 960; fb_h = 540; break;
|
||||
default: fb_w = 640; fb_h = 360; break;
|
||||
}
|
||||
pxl8_size fb_size = pxl8_get_resolution_dimensions(resolution);
|
||||
|
||||
ctx->window = SDL_CreateWindow(title, win_w, win_h, SDL_WINDOW_RESIZABLE);
|
||||
if (!ctx->window) {
|
||||
|
|
@ -65,7 +55,7 @@ static void* sdl3_create(pxl8_color_mode mode, pxl8_resolution resolution,
|
|||
ctx->framebuffer_texture = SDL_CreateTexture(ctx->renderer,
|
||||
SDL_PIXELFORMAT_RGBA32,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
fb_w, fb_h);
|
||||
fb_size.w, fb_size.h);
|
||||
if (!ctx->framebuffer_texture) {
|
||||
pxl8_error("Failed to create framebuffer texture: %s", SDL_GetError());
|
||||
SDL_DestroyRenderer(ctx->renderer);
|
||||
|
|
@ -145,14 +135,7 @@ static void sdl3_upload_framebuffer(void* platform_data, const u8* fb,
|
|||
|
||||
if (!ctx->rgba_buffer) return;
|
||||
|
||||
u32 palette_size;
|
||||
switch (mode) {
|
||||
case PXL8_COLOR_MODE_FAMI: palette_size = 64; break;
|
||||
case PXL8_COLOR_MODE_MEGA: palette_size = 512; break;
|
||||
case PXL8_COLOR_MODE_GBA: palette_size = 32768; break;
|
||||
case PXL8_COLOR_MODE_SNES: palette_size = 32768; break;
|
||||
default: palette_size = 256; break;
|
||||
}
|
||||
u32 palette_size = pxl8_get_palette_size(mode);
|
||||
|
||||
for (i32 i = 0; i < w * h; i++) {
|
||||
u8 index = fb[i];
|
||||
|
|
@ -212,14 +195,7 @@ static void sdl3_upload_atlas(void* platform_data, const pxl8_atlas* atlas,
|
|||
|
||||
if (!ctx->rgba_buffer) return;
|
||||
|
||||
u32 palette_size;
|
||||
switch (mode) {
|
||||
case PXL8_COLOR_MODE_FAMI: palette_size = 64; break;
|
||||
case PXL8_COLOR_MODE_MEGA: palette_size = 512; break;
|
||||
case PXL8_COLOR_MODE_GBA: palette_size = 32768; break;
|
||||
case PXL8_COLOR_MODE_SNES: palette_size = 32768; break;
|
||||
default: palette_size = 256; break;
|
||||
}
|
||||
u32 palette_size = pxl8_get_palette_size(mode);
|
||||
|
||||
for (u32 i = 0; i < atlas_w * atlas_h; i++) {
|
||||
u8 index = atlas_pixels[i];
|
||||
|
|
@ -290,12 +266,6 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
break;
|
||||
|
||||
case SDL_EVENT_KEY_DOWN: {
|
||||
#ifdef DEBUG
|
||||
if (event->key.key == SDLK_ESCAPE) {
|
||||
pxl8_set_running(sys, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_Scancode scancode = event->key.scancode;
|
||||
if (scancode < 256) {
|
||||
if (!input->keys_down[scancode]) {
|
||||
|
|
@ -343,6 +313,9 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
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;
|
||||
|
||||
|
|
@ -352,12 +325,11 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
i32 window_width, window_height;
|
||||
SDL_GetWindowSize(window, &window_width, &window_height);
|
||||
|
||||
i32 render_width, render_height;
|
||||
pxl8_resolution resolution = pxl8_get_resolution(sys);
|
||||
pxl8_gfx_get_resolution_dimensions(resolution, &render_width, &render_height);
|
||||
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_width, render_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);
|
||||
|
|
@ -386,6 +358,15 @@ void SDL_AppQuit(void* appstate, SDL_AppResult result) {
|
|||
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,
|
||||
|
|
@ -393,4 +374,5 @@ const pxl8_hal pxl8_hal_sdl3 = {
|
|||
.present = sdl3_present,
|
||||
.upload_atlas = sdl3_upload_atlas,
|
||||
.upload_framebuffer = sdl3_upload_framebuffer,
|
||||
.set_relative_mouse_mode = sdl3_set_relative_mouse_mode,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue