Skip to content

Commit

Permalink
update SDL port to SDL 3.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Oct 13, 2024
1 parent ca40d22 commit 42fa44f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 51 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ OBJS = $(OBJDIR)/8x8.o \
$(OBJDIR)/audio_shared.o

ifeq (${USE_SDL},1)
CFLAGS += -DUSE_SDL
OBJS += $(OBJDIR)/asset_loader.o \
$(OBJDIR)/posix_vfs.o \
$(OBJDIR)/util.o \
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ AS_CASE([$with_frontend],
PKG_CHECK_MODULES(SDL3, sdl3 >= 3.1.1)
case "${host_os}" in
mingw*)
FRONTEND_CFLAGS="$FRONTEND_CFLAGS -mwindows $SDL3_CFLAGS"
FRONTEND_CFLAGS="$FRONTEND_CFLAGS -mwindows $SDL3_CFLAGS -DUSE_SDL"
FRONTEND_LDFLAGS="$FRONTEND_LDFLAGS -Wl,-Bstatic -lstdc++ -lpthread -lmingw32 -Wl,-Bdynamic $SDL3_LIBS -static-libgcc -static-libstdc++"
;;
*)
FRONTEND_CFLAGS="$FRONTEND_CFLAGS $SDL3_CFLAGS"
FRONTEND_CFLAGS="$FRONTEND_CFLAGS $SDL3_CFLAGS -DUSE_SDL"
FRONTEND_LDFLAGS="$FRONTEND_LDFLAGS $SDL3_LIBS"
;;
esac
Expand Down
75 changes: 38 additions & 37 deletions src/sdl/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static u8 zzt_thread_running;
static atomic_int zzt_renderer_waiting = 0;
static u8 zzt_turbo = 0;

static long first_timer_tick;
static Uint64 first_timer_tick;
static double timer_time;

static void sdl_pit_tick(void) {
Expand All @@ -192,9 +192,9 @@ static void sdl_pit_tick(void) {
zzt_mark_timer();
}

static Uint32 sdl_timer_thread(Uint32 interval, void *param) {
static Uint64 sdl_timer_thread(void *param, SDL_TimerID timerID, Uint64 interval) {
if (!zzt_thread_running) return 0;
long curr_timer_tick = zeta_time_ms();
Uint64 curr_timer_tick = SDL_GetTicksNS();

atomic_fetch_add(&zzt_renderer_waiting, 1);
SDL_LockMutex(zzt_thread_lock);
Expand All @@ -203,13 +203,13 @@ static Uint32 sdl_timer_thread(Uint32 interval, void *param) {

audio_time = zeta_time_ms();
timer_time += SYS_TIMER_TIME;
long duration = curr_timer_tick - first_timer_tick;
long tick_time = ((long) (timer_time + SYS_TIMER_TIME)) - duration;
Sint64 duration = curr_timer_tick - first_timer_tick;
Sint64 tick_time = ((Sint64) ((timer_time + SYS_TIMER_TIME) * 1000000)) - duration;

while (tick_time <= 0) {
sdl_pit_tick();
timer_time += SYS_TIMER_TIME;
tick_time = ((long) (timer_time + SYS_TIMER_TIME)) - duration;
tick_time = ((Sint64) ((timer_time + SYS_TIMER_TIME) * 1000000)) - duration;
}

SDL_BroadcastCondition(zzt_thread_cond);
Expand All @@ -218,9 +218,9 @@ static Uint32 sdl_timer_thread(Uint32 interval, void *param) {
}

static void sdl_timer_init(void) {
first_timer_tick = zeta_time_ms();
first_timer_tick = SDL_GetTicksNS();
timer_time = 0;
SDL_AddTimer((int) SYS_TIMER_TIME, sdl_timer_thread, (void*)NULL);
SDL_AddTimerNS((Uint64) (SYS_TIMER_TIME * 1000000), sdl_timer_thread, (void*)NULL);
}

static bool sdl_is_blink_phase(long curr_time) {
Expand Down Expand Up @@ -293,7 +293,7 @@ static u32* palette_update_data = NULL;
static u8 windowed = 1;
static int windowed_old_w, windowed_old_h;

void calc_render_area(SDL_Rect *rect, int w, int h, int *scale_out, int flags) {
void calc_render_area(SDL_FRect *rect, int w, int h, int *scale_out, int flags) {
int iw = 80*charw;
int ih = 25*charh;

Expand Down Expand Up @@ -397,12 +397,12 @@ int main(int argc, char **argv) {

SDL_Thread* zzt_thread;

if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_GAMEPAD) < 0) {
SDL_SetAppMetadata("zeta", VERSION, "pl.asie.zeta");
if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed! %s", SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Could not initialize SDL!", NULL);
return 1;
}
SDL_StartTextInput();

render_data_update_mutex = SDL_CreateMutex();
zzt_thread_lock = SDL_CreateMutex();
Expand Down Expand Up @@ -455,6 +455,7 @@ int main(int argc, char **argv) {
}
window = renderer->get_window();
sdl_resize_window(0, false, false);
SDL_StartTextInput(window);

#ifdef _WIN32
{
Expand All @@ -468,7 +469,7 @@ int main(int argc, char **argv) {
#endif

const SDL_AudioSpec audio_spec = { SDL_AUDIO_S16LE, 1, 48000 };
audio_stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &audio_spec, audio_callback, NULL);
audio_stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_spec, audio_callback, NULL);
if (audio_stream == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not open audio device! %s", SDL_GetError());
}
Expand Down Expand Up @@ -530,17 +531,17 @@ int main(int argc, char **argv) {
}
break;
case SDL_EVENT_KEY_DOWN:
if (windowed && (event.key.keysym.sym == 'q' || event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)) {
if (SDL_GetRelativeMouseMode() != 0) {
SDL_SetRelativeMouseMode(0);
if (windowed && (event.key.key == 'q' || event.key.scancode == SDL_SCANCODE_ESCAPE)) {
if (SDL_GetWindowRelativeMouseMode(window) != 0) {
SDL_SetWindowRelativeMouseMode(window, 0);
break;
}
}
if (event.key.keysym.sym == SDLK_F11) {
if (event.key.key == SDLK_F11) {
ui_activate();
}
#ifdef ENABLE_SCREENSHOTS
if (event.key.keysym.sym == SDLK_F12) {
if (event.key.key == SDLK_F12) {
FILE *file;
char filename[24];

Expand Down Expand Up @@ -573,13 +574,13 @@ int main(int argc, char **argv) {
break;
}
#endif
if (event.key.keysym.sym == SDLK_F9) {
if (event.key.key == SDLK_F9) {
zzt_turbo = 1;
break;
}

#ifdef ENABLE_AUDIO_WRITER
if (event.key.keysym.sym == SDLK_F6 && KEYMOD_CTRL(event.key.keysym.mod)) {
if (event.key.key == SDLK_F6 && KEYMOD_CTRL(event.key.mod)) {
// audio writer
if (audio_writer_s == NULL) {
FILE *file;
Expand All @@ -603,15 +604,15 @@ int main(int argc, char **argv) {
#endif

#ifdef ENABLE_GIF_WRITER
if (event.key.keysym.sym == SDLK_F5 && KEYMOD_CTRL(event.key.keysym.mod)) {
if (event.key.key == SDLK_F5 && KEYMOD_CTRL(event.key.mod)) {
// gif writer
if (gif_writer_s == NULL) {
FILE *file;
char filename[24];
file = create_inc_file(filename, 23, "screen%d.gif", "wb");
if (file != NULL) {
fclose(file);
bool optimized = !KEYMOD_SHIFT(event.key.keysym.mod);
bool optimized = !KEYMOD_SHIFT(event.key.mod);
gif_writer_ticks = 0;
if ((gif_writer_s = gif_writer_start(filename, optimized, true)) != NULL) {
fprintf(stderr, "GIF writing started [%s, %s].\n", filename, optimized ? "optimized" : "unoptimized");
Expand All @@ -628,7 +629,7 @@ int main(int argc, char **argv) {
}
#endif

if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && KEYMOD_ALT(event.key.keysym.mod)) {
if (event.key.scancode == SDL_SCANCODE_RETURN && KEYMOD_ALT(event.key.mod)) {
// Alt+ENTER
if (windowed) {
SDL_GetWindowSize(window, &windowed_old_w, &windowed_old_h);
Expand All @@ -638,20 +639,20 @@ int main(int argc, char **argv) {
}
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
// force focus
SDL_SetRelativeMouseMode(1);
SDL_SetWindowRelativeMouseMode(window, 1);
} else {
SDL_SetWindowFullscreen(window, 0);
SDL_SetWindowSize(window, windowed_old_w, windowed_old_h);
sdl_resize_window(0, true, false);
// drop focus
SDL_SetRelativeMouseMode(0);
SDL_SetWindowRelativeMouseMode(window, 0);
}
windowed = 1 - windowed;
break;
}
update_keymod(event.key.keysym.mod);
scode = event.key.keysym.scancode;
kcode = event.key.keysym.sym;
update_keymod(event.key.mod);
scode = event.key.scancode;
kcode = event.key.key;
if (zzt_kmod_get() & ZZT_KMOD_CTRL) {
if (!((kcode >= 97 && kcode <= 122) || (kcode >= 65 && kcode <= 90))) break;
kcode &= 0x1F;
Expand All @@ -666,8 +667,8 @@ int main(int argc, char **argv) {
}
break;
case SDL_EVENT_KEY_UP:
if (KEYMOD_CTRL(event.key.keysym.mod)) {
kcode = event.key.keysym.sym;
if (KEYMOD_CTRL(event.key.mod)) {
kcode = event.key.key;
if (kcode == '-' || kcode == SDLK_KP_MINUS) {
sdl_resize_window(-1, false, false);
break;
Expand All @@ -676,13 +677,13 @@ int main(int argc, char **argv) {
break;
}
}
if (event.key.keysym.sym == SDLK_F9) {
if (event.key.key == SDLK_F9) {
zzt_turbo = 0;
break;
}
update_keymod(event.key.keysym.mod);
scode = event.key.keysym.scancode;
kcode = event.key.keysym.sym;
update_keymod(event.key.mod);
scode = event.key.scancode;
kcode = event.key.key;
if (zzt_kmod_get() & ZZT_KMOD_CTRL) {
if (!((kcode >= 97 && kcode <= 122) || (kcode >= 65 && kcode <= 90))) break;
kcode &= 0x1F;
Expand All @@ -697,9 +698,9 @@ int main(int argc, char **argv) {
}
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
if (SDL_GetRelativeMouseMode() == 0) {
if (SDL_GetWindowRelativeMouseMode(window) == 0) {
if (SDL_GetWindowFlags(window) & SDL_WINDOW_INPUT_FOCUS) {
SDL_SetRelativeMouseMode(1);
SDL_SetWindowRelativeMouseMode(window, 1);
}
} else {
if (event.button.button == SDL_BUTTON_LEFT) {
Expand All @@ -721,7 +722,7 @@ int main(int argc, char **argv) {
}
break;
case SDL_EVENT_MOUSE_MOTION:
if (SDL_GetRelativeMouseMode() != 0) {
if (SDL_GetWindowRelativeMouseMode(window) != 0) {
zzt_mouse_axis(0, event.motion.xrel);
zzt_mouse_axis(1, event.motion.yrel);
}
Expand Down Expand Up @@ -770,11 +771,11 @@ int main(int argc, char **argv) {
#endif

zzt_thread_running = 0;
SDL_StopTextInput(window);
if (audio_stream != 0) {
SDL_CloseAudioDevice(SDL_GetAudioStreamDevice(audio_stream));
}
renderer->deinit();
SDL_StopTextInput();

if (developer_mode) {
char *debug_line = malloc(1025);
Expand Down
2 changes: 1 addition & 1 deletion src/sdl/frontend_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ typedef struct {

#define AREA_WITHOUT_SCALE 1

void calc_render_area(SDL_Rect *rect, int w, int h, int *scale_out, int flags);
void calc_render_area(SDL_FRect *rect, int w, int h, int *scale_out, int flags);

#endif /* __FRONTEND_SDL_H__ */
5 changes: 3 additions & 2 deletions src/sdl/render_opengl.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static SDL_Texture *create_texture_from_array(SDL_Renderer *renderer, int access
}

static void prepare_render_opengl(void) {
SDL_Rect rect;
SDL_FRect rect;
int w, h, scale;
SDL_GetWindowSizeInPixels(window, &w, &h);
calc_render_area(&rect, w, h, &scale, AREA_WITHOUT_SCALE);
Expand Down Expand Up @@ -348,8 +348,9 @@ static int sdl_render_opengl_init(const char *window_name, int charw, int charh)

SDL_GL_SetSwapInterval(1);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");

renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_PRESENTVSYNC);
renderer = SDL_CreateRenderer(window, NULL);
if (renderer == NULL) {
SDL_DestroyWindow(window);
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/sdl/render_software.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static int sdl_render_software_init(const char *window_name, int charw, int char

SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");

renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_PRESENTVSYNC);
renderer = SDL_CreateRenderer(window, NULL);
if (renderer == NULL) {
return -1;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ static void sdl_render_software_update_palette(u32 *data_arg) {
}

static void sdl_render_software_draw(u8 *vram, int blink_mode) {
SDL_Rect dest;
SDL_FRect dest;
void *buffer;
int w, h, pitch;

Expand Down
37 changes: 30 additions & 7 deletions src/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#include "audio_stream.h"
#include "ui.h"
#include "zzt.h"
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#elif defined(USE_SDL)
#include <SDL3/SDL.h>
#endif

typedef struct {
uint8_t screen_backup[80 * 25 * 2];
Expand All @@ -43,12 +48,10 @@ static ui_state_t *ui_state = NULL;

#ifdef __EMSCRIPTEN__
static const char* ui_lines_header[] = {
"zeta " VERSION,
};
#define UI_LINES_HEADER_COUNT 1
#define UI_LINES_HEADER_COUNT 0
#else
static const char* ui_lines_header[] = {
"zeta " VERSION,
"",
//2345678901234567890123456789012345
"CTRL-F5/F6 .GIF/.WAV record mode",
Expand All @@ -57,7 +60,7 @@ static const char* ui_lines_header[] = {
"CTRL-+/- window resize up/down",
"ALT-ENTER fullscreen toggle ",
};
#define UI_LINES_HEADER_COUNT 7
#define UI_LINES_HEADER_COUNT 6
#endif

static const char* ui_lines_options[] = {
Expand Down Expand Up @@ -139,7 +142,7 @@ static void ui_draw_check(int x, int y, const char *s, bool value, uint8_t col)
void ui_tick(void) {
char sbuf[37];

int wwidth = 36, wheight = UI_LINES_HEADER_COUNT + UI_LINES_OPTIONS_COUNT + 3;
int wwidth = 36, wheight = 1 + UI_LINES_HEADER_COUNT + UI_LINES_OPTIONS_COUNT + 3;
int swidth, sheight;
zzt_get_screen_size(&swidth, &sheight);
int wx = (swidth - wwidth) >> 1;
Expand Down Expand Up @@ -172,16 +175,36 @@ void ui_tick(void) {
ui_draw_char(wx+wwidth-1, wy+wheight-1, 0xBC, 0x1F);

// draw header lines
{
#if defined(__EMSCRIPTEN__)
char is[80];
snprintf(is, sizeof(is) - 1, "zeta " VERSION " (emsdk %d.%d.%d)",
__EMSCRIPTEN_major__,
__EMSCRIPTEN_minor__,
__EMSCRIPTEN_tiny__);
#elif defined(USE_SDL)
int sdl_v = SDL_GetVersion();
char is[80];
snprintf(is, sizeof(is) - 1, "zeta " VERSION " (SDL %d.%d.%d)",
SDL_VERSIONNUM_MAJOR(sdl_v),
SDL_VERSIONNUM_MINOR(sdl_v),
SDL_VERSIONNUM_MICRO(sdl_v));
#else
const char *is = "zeta " VERSION;
#endif
ui_draw_string((swidth - strlen(is)) >> 1, wy + 1, is, 0x1E);
}

for (int iy = 0; iy < UI_LINES_HEADER_COUNT; iy++) {
const char *is = ui_lines_header[iy];
ui_draw_string((swidth - strlen(is)) >> 1, wy + 1 + iy, is, iy == 0 ? 0x1E : 0x1F);
ui_draw_string((swidth - strlen(is)) >> 1, wy + 2 + iy, is, 0x1F);
}

ui_state->screen_redraw = false;
}

{
int woptx = wx + 2, wopty = wy + 2 + UI_LINES_HEADER_COUNT;
int woptx = wx + 2, wopty = wy + 3 + UI_LINES_HEADER_COUNT;

// draw options lines
for (int iy = 0; iy < UI_LINES_OPTIONS_COUNT; iy++) {
Expand Down

0 comments on commit 42fa44f

Please sign in to comment.