1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2025-06-26 23:12:51 +00:00

migrated to SDL3

This commit is contained in:
Zeno Rogue 2025-06-07 16:16:57 +02:00
parent 41ffba600f
commit a80b74dc62
25 changed files with 435 additions and 235 deletions

View File

@ -165,7 +165,7 @@ void fix_font_size(int& size) {
#if CAP_SDL
#if !CAP_SDL2
#if SDLVER == 1
#if HDR
typedef SDL_Surface SDL_Renderer;
#define srend s
@ -174,7 +174,7 @@ typedef SDL_Surface SDL_Renderer;
EX SDL_Surface *s;
EX SDL_Surface *s_screen;
#if CAP_SDL2
#if SDLVER >= 2
EX SDL_Renderer *s_renderer, *s_software_renderer;
#if HDR
#define srend s_software_renderer
@ -196,10 +196,14 @@ EX color_t& qpixel(SDL_Surface *surf, int x, int y) {
}
EX void present_surface() {
#if CAP_SDL2
#if SDLVER >= 2
SDL_UpdateTexture(s_texture, nullptr, s->pixels, s->w * sizeof (Uint32));
SDL_RenderClear(s_renderer);
#if SDLVER >= 3
SDL_RenderTexture(s_renderer, s_texture, nullptr, nullptr);
#else
SDL_RenderCopy(s_renderer, s_texture, nullptr, nullptr);
#endif
SDL_RenderPresent(s_renderer);
#else
SDL_UpdateRect(s, 0, 0, 0, 0);
@ -209,7 +213,7 @@ EX void present_surface() {
EX void present_screen() {
#if CAP_GL
if(vid.usingGL) {
#if CAP_SDL2
#if SDLVER >= 2
SDL_GL_SwapWindow(s_window);
#else
SDL_GL_SwapBuffers();
@ -610,7 +614,7 @@ EX void init_glfont(int size) {
fix_font_size(siz);
if(ch < 128) {
str[0] = ch;
txt = TTF_RenderText_Blended(cfont->font[siz], str, white);
txt = TTF_RenderUTF8_Blended(cfont->font[siz], str, white);
}
else {
txt = TTF_RenderUTF8_Blended(cfont->font[siz], natchars[ch-128], white);
@ -620,7 +624,7 @@ EX void init_glfont(int size) {
generateFont(ch, txt);
#endif
sdltogl(txt, f, ch);
SDL_FreeSurface(txt);
SDL_DestroySurface(txt);
#endif
}
@ -895,7 +899,9 @@ EX bool displaystr(int x, int y, int shift, int size, const char *str, color_t c
bool clicked = (mousex >= rect.x && mousey >= rect.y && mousex <= rect.x+rect.w && mousey <= rect.y+rect.h);
if(shift) {
#if CAP_SDL2
#if SDLVER == 3
SDL_Surface* txt2 = SDL_ConvertSurface(txt, SDL_PIXELFORMAT_RGBA8888);
#elif SDLVER == 2
SDL_Surface* txt2 = SDL_ConvertSurfaceFormat(txt, SDL_PIXELFORMAT_RGBA8888, 0);
#else
SDL_Surface* txt2 = SDL_DisplayFormat(txt);
@ -909,12 +915,12 @@ EX bool displaystr(int x, int y, int shift, int size, const char *str, color_t c
qpixel(s, rect.x+xx+shift, rect.y+yy) |= color & 0x00FFFF;
SDL_UnlockSurface(s);
SDL_UnlockSurface(txt2);
SDL_FreeSurface(txt2);
SDL_DestroySurface(txt2);
}
else {
SDL_BlitSurface(txt, NULL, s,&rect);
}
SDL_FreeSurface(txt);
SDL_DestroySurface(txt);
return clicked;
#endif
@ -1256,19 +1262,19 @@ EX bool need_to_apply_screen_settings() {
}
EX void close_renderer() {
#if CAP_SDL2
#if SDLVER >= 2
if(s_renderer) SDL_DestroyRenderer(s_renderer), s_renderer = nullptr;
if(s_texture) SDL_DestroyTexture(s_texture), s_texture = nullptr;
if(s) SDL_FreeSurface(s), s = nullptr;
if(s) SDL_DestroySurface(s), s = nullptr;
if(s_software_renderer) SDL_DestroyRenderer(s_software_renderer), s_software_renderer = nullptr;
#endif
}
EX void close_window() {
#if CAP_SDL2
#if SDLVER >= 2
close_renderer();
if(s_have_context) {
SDL_GL_DeleteContext(s_context), s_have_context = false;
SDL_GL_DestroyContext(s_context), s_have_context = false;
}
if(s_window) SDL_DestroyWindow(s_window), s_window = nullptr;
#endif
@ -1289,7 +1295,7 @@ EX void apply_screen_settings() {
#endif
#if CAP_SDL
#if !CAP_SDL2
#if SDLVER == 1
if(need_to_reopen_window())
SDL_QuitSubSystem(SDL_INIT_VIDEO);
#endif
@ -1352,12 +1358,12 @@ EX void setvideomode() {
#if CAP_GL
vid.usingGL = vid.wantGL;
if(vid.usingGL) {
flags = SDL12(SDL_OPENGL | SDL_HWSURFACE, SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI);
flags = SDL12(SDL_OPENGL | SDL_HWSURFACE, SDL_WINDOW_OPENGL | SDL_WINDOW_HIGH_PIXEL_DENSITY);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
vid.current_vsync = want_vsync();
#if !ISMOBWEB && !CAP_SDL2
#if !ISMOBWEB && SDLVER == 1
if(vid.current_vsync)
SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 1 );
else
@ -1393,15 +1399,15 @@ EX void setvideomode() {
#endif
#endif
#if CAP_SDL2
#if SDLVER >= 2
if(s_renderer) SDL_DestroyRenderer(s_renderer), s_renderer = nullptr;
#endif
auto create_win = [&] {
#if CAP_SDL2
#if SDLVER >= 2
if(s_window && current_window_flags != (flags | sizeflag)) {
if(s_have_context) {
SDL_GL_DeleteContext(s_context), s_have_context = false;
SDL_GL_DestroyContext(s_context), s_have_context = false;
glhr::glew = false;
}
SDL_DestroyWindow(s_window), s_window = nullptr;
@ -1409,10 +1415,7 @@ EX void setvideomode() {
if(s_window)
SDL_SetWindowSize(s_window, vid.xres, vid.yres);
else
s_window = SDL_CreateWindow(CUSTOM_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
vid.xres, vid.yres,
flags | sizeflag
);
s_window = SDL_CreateWindow(CUSTOM_CAPTION, vid.xres, vid.yres, flags | sizeflag);
current_window_flags = (flags | sizeflag);
#else
s = SDL_SetVideoMode(vid.xres, vid.yres, 32, flags | sizeflag);
@ -1441,15 +1444,23 @@ EX void setvideomode() {
create_win();
}
#if CAP_SDL2
#if SDLVER >= 2
if(s_renderer) SDL_DestroyRenderer(s_renderer), s_renderer = nullptr;
#if SDLVER >= 3
s_renderer = SDL_CreateRenderer(s_window, nullptr);
SDL_SetRenderVSync(s_renderer, vid.current_vsync ? 1 : SDL_RENDERER_VSYNC_DISABLED);
// todo VSYNC -- , SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER
#else
s_renderer = SDL_CreateRenderer(s_window, -1, vid.current_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
SDL_GetRendererOutputSize(s_renderer, &vid.xres, &vid.yres);
#endif
SDL_GetCurrentRenderOutputSize(s_renderer, &vid.xres, &vid.yres);
if(s_texture) SDL_DestroyTexture(s_texture), s_texture = nullptr;
s_texture = SDL_CreateTexture(s_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, vid.xres, vid.yres);
if(s) SDL_FreeSurface(s), s = nullptr;
if(s) SDL_DestroySurface(s), s = nullptr;
s = shot::empty_surface(vid.xres, vid.yres, false);
if(s_software_renderer) SDL_DestroyRenderer(s_software_renderer), s_software_renderer = nullptr;
@ -1469,8 +1480,8 @@ EX void setvideomode() {
glDisable(GL_MULTISAMPLE_ARB);
}
#if CAP_SDL2
if(s_have_context) SDL_GL_DeleteContext(s_context), s_have_context = false;
#if SDLVER >= 2
if(s_have_context) SDL_GL_DestroyContext(s_context), s_have_context = false;
if(!s_have_context) s_context = SDL_GL_CreateContext(s_window);
s_have_context = true; glhr::glew = false;
#endif
@ -1487,13 +1498,14 @@ EX bool noGUI = false;
#if CAP_SDL
EX bool sdl_on = false;
EX int SDL_Init1(Uint32 flags) {
EX bool SDL_Init1(Uint32 flags) {
if(!sdl_on) {
sdl_on = true;
return SDL_Init(flags);
return !SDL_error_in(SDL_Init(flags));
}
else {
return !SDL_error_in(SDL_InitSubSystem(flags));
}
else
return SDL_InitSubSystem(flags);
}
#endif
@ -1510,7 +1522,7 @@ EX void set_cfont() {
EX void init_font() {
#if CAP_SDLTTF
if(TTF_Init() != 0) {
if(SDL_error_in(TTF_Init())) {
printf("Failed to initialize TTF.\n");
exit(2);
}
@ -1542,7 +1554,7 @@ EX void close_font() {
EX void init_graph() {
#if CAP_SDL
if (SDL_Init1(SDL_INIT_VIDEO) == -1)
if (!SDL_Init1(SDL_INIT_VIDEO))
{
printf("Failed to initialize video.\n");
exit(2);
@ -1552,7 +1564,15 @@ EX void init_graph() {
get_canvas_size();
#else
if(!vid.xscr) {
#if CAP_SDL2
#if SDLVER >= 3
int count;
auto displays = SDL_GetDisplays(&count);
if(!count) { println(hlog, "error: no displays"); return; }
const SDL_DisplayMode* dm = SDL_GetCurrentDisplayMode(displays[0]);
if(!dm) println(hlog, "SDL_GetCurrentDisplayMode error: ", SDL_GetError());
if(dm) vid.xscr = vid.xres = dm->w;
if(dm) vid.yscr = vid.yres = dm->h;
#elif SDLVER >= 2
SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(0, &dm);
vid.xscr = vid.xres = dm.w;
@ -1565,7 +1585,7 @@ EX void init_graph() {
}
#endif
#if !CAP_SDL2
#if SDLVER == 1
SDL_WM_SetCaption(CUSTOM_CAPTION, CUSTOM_CAPTION);
#endif
#endif
@ -1587,7 +1607,7 @@ EX void init_graph() {
exit(2);
}
#if !CAP_SDL2
#if SDLVER == 1
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
SDL_EnableUNICODE(1);
#endif

View File

@ -118,7 +118,7 @@ EX color_t rainbow_color(ld sat, ld hue) {
/** Adjust col to SDL_gfx functions. No adjustment is needed in SDL 1.2, but it is needed in SDL2 */
EX color_t align(color_t col) {
#if CAP_SDL2
#if SDLVER >= 2
swap(part(col, 0), part(col, 3));
swap(part(col, 1), part(col, 2));
#endif

View File

@ -32,7 +32,7 @@ EX int slider_x;
EX function <void(int sym, int uni)> keyhandler = [] (int sym, int uni) {};
EX function <bool(SDL_Event &ev)> joyhandler = [] (SDL_Event &ev) {return false;};
#if CAP_SDL2
#if SDLVER >= 2
EX void ignore_text(const SDL_TextInputEvent&) {}
EX function <void(const SDL_TextInputEvent&)> texthandler = ignore_text;
#endif
@ -40,7 +40,7 @@ EX function <void(const SDL_TextInputEvent&)> texthandler = ignore_text;
EX void reset_handlers() {
keyhandler = [] (int sym, int uni) {};
joyhandler = [] (SDL_Event &ev) {return false;};
#if CAP_SDL2
#if SDLVER >= 2
texthandler = ignore_text;
#endif
}
@ -253,10 +253,14 @@ EX void initJoysticks_async() {
EX void countJoysticks() {
DEBB(DF_INIT, ("opening joysticks"));
#if SDLVER <= 2
numsticks = SDL_NumJoysticks();
#else
SDL_GetJoysticks(&numsticks);
#endif
if(numsticks > 8) numsticks = 8;
for(int i=0; i<numsticks; i++) {
sticks[i] = SDL_JoystickOpen(i);
sticks[i] = SDL_OpenJoystick(i);
/* printf("axes = %d, balls = %d, buttons = %d, hats = %d\n",
SDL_JoystickNumAxes(sticks[i]),
SDL_JoystickNumBalls(sticks[i]),
@ -271,7 +275,7 @@ EX void initJoysticks() {
DEBBI(DF_INIT, ("init joystick"));
DEBB(DF_INIT, ("init joystick subsystem"));
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
if (SDL_error_in(SDL_InitSubSystem(SDL_INIT_JOYSTICK)))
{
printf("Failed to initialize joysticks.\n");
numsticks = 0;
@ -285,7 +289,7 @@ EX void initJoysticks() {
EX void closeJoysticks() {
DEBB(DF_INIT, ("close joysticks"));
for(int i=0; i<numsticks; i++) {
SDL_JoystickClose(sticks[i]), sticks[i] = NULL;
SDL_CloseJoystick(sticks[i]), sticks[i] = NULL;
}
numsticks = 0;
}
@ -834,8 +838,10 @@ EX void mainloopiter() {
oldmousepan = mousepan;
#if CAP_MOUSEGRAB
if(mousepan) {
#if CAP_SDL2
SDL_SetRelativeMouseMode(SDL_TRUE);
#if SDLVER >= 3
SDL_SetWindowRelativeMouseMode(s_window, true);
#elif SDLVER >= 2
SDL_SetRelativeMouseMode(SDL23(SDL_TRUE, true));
#else
SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_ShowCursor(SDL_DISABLE);
@ -843,8 +849,10 @@ EX void mainloopiter() {
mouseaim_x = mouseaim_y = 0;
}
else {
#if CAP_SDL2
SDL_SetRelativeMouseMode(SDL_FALSE);
#if SDLVER >= 3
SDL_SetWindowRelativeMouseMode(s_window, false);
#elif SDLVER >= 2
SDL_SetRelativeMouseMode(SDL23(SDL_FALSE, false));
#else
SDL_WM_GrabInput( SDL_GRAB_OFF );
SDL_ShowCursor(SDL_ENABLE);
@ -860,7 +868,7 @@ EX void mainloopiter() {
#endif
if(timetowait > 0) {
#if !CAP_SDL2
#if SDLVER == 1
SDL_Delay(timetowait);
#endif
}
@ -890,10 +898,9 @@ EX void mainloopiter() {
getcshift = 1;
#if CAP_SDL2
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
#if SDLVER >= 2
pandora_rightclick = keystate[SDL_SCANCODE_RCTRL];
pandora_leftclick = keystate[SDL_SCANCODE_RSHIFT];
@ -909,9 +916,6 @@ EX void mainloopiter() {
if(keystate[SDL_SCANCODE_LALT] || keystate[SDL_SCANCODE_RALT]) getcshift *= 10;
#else
Uint8 *keystate = SDL_GetKeyState(NULL);
pandora_rightclick = keystate[SDLK_RCTRL];
pandora_leftclick = keystate[SDLK_RSHIFT];
@ -1002,7 +1006,7 @@ EX void mainloopiter() {
ld t = (ticks - lastticks) * shiftmul / 1000.;
lastticks = ticks;
#if CAP_SDL2
#if SDLVER >= 2
if(keystate[SDL_SCANCODE_END] && GDIM == 3 && DEFAULTNOR(SDL_SCANCODE_END)) full_forward_camera(-t);
if(keystate[SDL_SCANCODE_HOME] && GDIM == 3 && DEFAULTNOR(SDL_SCANCODE_HOME)) full_forward_camera(t);
if(keystate[SDL_SCANCODE_RIGHT] && DEFAULTNOR(SDL_SCANCODE_RIGHT)) full_rotate_camera(0, -t);
@ -1032,7 +1036,7 @@ EX void mainloopiter() {
ld t = (ticks - lastticks) * shiftmul / 1000.;
lastticks = ticks;
#if CAP_SDL2
#if SDLVER >= 2
#define dkey(x) keystate[int(x - 'a' + 4)] && DEFAULTNOR(x - 'a' + 4)
#else
#define dkey(x) keystate[int(x)] && DEFAULTNOR(x)
@ -1073,7 +1077,7 @@ EX void mainloopiter() {
if(((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON_MMASK)) && !mouseout2())
currently_scrolling = true;
#if CAP_SDL2
#if SDLVER >= 2
if(timetowait > 0) {
if(SDL_WaitEventTimeout(&ev, timetowait)) handle_event(ev);
}
@ -1102,7 +1106,14 @@ EX void handle_event(SDL_Event& ev) {
countJoysticks();
} */
#if CAP_SDL2
#if SDLVER == 3
if(ev.type == SDL_EVENT_WINDOW_MOUSE_ENTER) outoffocus = false;
if(ev.type == SDL_EVENT_WINDOW_MOUSE_LEAVE) outoffocus = true;
if(ev.type == SDL_EVENT_WINDOW_EXPOSED) drawscreen();
if(ev.type == SDL_EVENT_WINDOW_RESIZED) resize_screen_to(ev.window.data1, ev.window.data2);
#endif
#if SDLVER == 2
if(ev.type == SDL_WINDOWEVENT) {
auto w = ev.window.event;
if(w == SDL_WINDOWEVENT_ENTER)
@ -1114,8 +1125,9 @@ EX void handle_event(SDL_Event& ev) {
if(w == SDL_WINDOWEVENT_RESIZED)
resize_screen_to(ev.window.data1, ev.window.data2);
}
#endif
#else
#if SDLVER == 1
if(ev.type == SDL_ACTIVEEVENT) {
if(ev.active.state & SDL_APPINPUTFOCUS) {
if(ev.active.gain) {
@ -1136,7 +1148,7 @@ EX void handle_event(SDL_Event& ev) {
#endif
#if CAP_SDLJOY
if(ev.type == SDL_JOYAXISMOTION && normal && DEFAULTCONTROL) {
if(ev.type == SDL_EVENT_JOYSTICK_AXIS_MOTION && normal && DEFAULTCONTROL) {
if(ev.jaxis.which == 0) {
if(ev.jaxis.axis == 0)
joyx = ev.jaxis.value;
@ -1159,64 +1171,65 @@ EX void handle_event(SDL_Event& ev) {
if(joyhandler && joyhandler(ev)) ;
else if(ev.type == SDL_JOYHATMOTION && !normal) {
else if(ev.type == SDL_EVENT_JOYSTICK_HAT_MOTION && !normal) {
if(ev.jhat.value == SDL_HAT_UP) sym = SDLK_UP;
if(ev.jhat.value == SDL_HAT_DOWN) sym = SDLK_DOWN;
if(ev.jhat.value == SDL_HAT_LEFT) sym = SDLK_LEFT;
if(ev.jhat.value == SDL_HAT_RIGHT) sym = SDLK_RIGHT;
}
else if(ev.type == SDL_JOYBUTTONDOWN && normal && DEFAULTCONTROL) {
else if(ev.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN && normal && DEFAULTCONTROL) {
flashMessages();
movepcto(joydir);
joy_ignore_next = true;
checkjoy();
}
else if(ev.type == SDL_JOYBUTTONDOWN && !normal) {
else if(ev.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN && !normal) {
sym = uni = SDLK_RETURN;
}
#endif
if(ev.type == SDL_KEYDOWN) {
if(ev.type == SDL_EVENT_KEY_DOWN) {
flashMessages();
mousing = false;
sym = ev.key.keysym.sym;
#if CAP_SDL2
uni = ev.key.keysym.sym;
if(uni == '=' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '+';
if(uni == '1' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '!';
if(uni == '2' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '@';
if(uni == '3' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '#';
if(uni == '4' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '$';
if(uni == '5' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '%';
if(uni == '6' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '^';
if(uni == '7' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '&';
if(uni == '8' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '*';
if(uni == '9' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = '(';
if(uni == '0' && (ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) uni = ')';
sym = SDL23(ev.key.keysym.sym, ev.key.key);
auto mod = SDL23(ev.key.keysym.mod, ev.key.mod);
#if SDLVER >= 2
uni = SDL23(ev.key.keysym.sym, ev.key.key);
if(uni == '=' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '+';
if(uni == '1' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '!';
if(uni == '2' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '@';
if(uni == '3' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '#';
if(uni == '4' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '$';
if(uni == '5' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '%';
if(uni == '6' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '^';
if(uni == '7' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '&';
if(uni == '8' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '*';
if(uni == '9' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = '(';
if(uni == '0' && (mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT))) uni = ')';
if(uni >= 'a' && uni <= 'z') {
if(ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) uni -= 32;
else if(ev.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) uni -= 96;
if(mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT)) uni -= 32;
else if(mod & (SDL_KMOD_LCTRL | SDL_KMOD_RCTRL)) uni -= 96;
}
#else
uni = ev.key.keysym.unicode;
if(uni == 0 && (sym >= 'a' && sym <= 'z')) {
if(ev.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) uni = sym - 96;
if(ev.key.keysym.mod & (SDL_KMOD_LCTRL | SDL_KMOD_RCTRL)) uni = sym - 96;
}
if(ev.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) shiftmul = -1;
if(ev.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) shiftmul /= 10;
if(ev.key.keysym.mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT)) shiftmul = -1;
if(ev.key.keysym.mod & (SDL_KMOD_LCTRL | SDL_KMOD_RCTRL)) shiftmul /= 10;
#endif
numlock_on = ev.key.keysym.mod & KMOD_NUM;
if(sym == SDLK_RETURN && (ev.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) {
numlock_on = mod & SDL_KMOD_NUM;
if(sym == SDLK_RETURN && (mod & (SDL_KMOD_LALT | SDL_KMOD_RALT))) {
sym = 0; uni = 0;
vid.want_fullscreen = !vid.want_fullscreen;
apply_screen_settings();
}
}
#if CAP_SDL2
if(ev.type == SDL_TEXTINPUT) {
#if SDLVER >= 2
if(ev.type == SDL_EVENT_TEXT_INPUT) {
texthandler(ev.text);
}
#endif
@ -1228,8 +1241,8 @@ EX void handle_event(SDL_Event& ev) {
bool rollchange = (cmode & sm::OVERVIEW) && getcstat >= 2000 && cheater;
if(ev.type == SDL_MOUSEBUTTONDOWN || ev.type == SDL_MOUSEBUTTONUP SDL12(, || ev.type == SDL_MOUSEWHEEL)) {
mousepressed = ev.type == SDL_MOUSEBUTTONDOWN;
if(ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN || ev.type == SDL_EVENT_MOUSE_BUTTON_UP SDL12(, || ev.type == SDL_EVENT_MOUSE_WHEEL)) {
mousepressed = ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN;
if(mousepressed) flashMessages();
mousing = true;
which_pointer = 0;
@ -1237,8 +1250,8 @@ EX void handle_event(SDL_Event& ev) {
holdmouse = false;
invslider = false;
bool down = ev.type == SDL_MOUSEBUTTONDOWN SDL12(, || ev.type == SDL_MOUSEWHEEL);
bool up = ev.type == SDL_MOUSEBUTTONUP;
bool down = ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN SDL12(, || ev.type == SDL_EVENT_MOUSE_WHEEL);
bool up = ev.type == SDL_EVENT_MOUSE_BUTTON_UP;
bool act = false;
@ -1273,8 +1286,8 @@ EX void handle_event(SDL_Event& ev) {
sym = getcstat, uni = getcstat, shiftmul = getcshift;
}
else if(SDL12(ev.button.button==SDL_BUTTON_WHEELDOWN || ev.button.button == SDL_BUTTON_WHEELUP, ev.type == SDL_MOUSEWHEEL)) {
#if CAP_SDL2
else if(SDL12(ev.button.button==SDL_BUTTON_WHEELDOWN || ev.button.button == SDL_BUTTON_WHEELUP, ev.type == SDL_EVENT_MOUSE_WHEEL)) {
#if SDLVER >= 2
ld dir = ev.wheel.y * 0.25;
#else
ld dir = ev.button.button == SDL_BUTTON_WHEELUP ? 0.25 : -0.25;
@ -1304,7 +1317,7 @@ EX void handle_event(SDL_Event& ev) {
}
}
if(ev.type == SDL_MOUSEMOTION) {
if(ev.type == SDL_EVENT_MOUSE_MOTION) {
mouseoh = mouseh;
int lmousex = mousex, lmousey = mousey;
@ -1350,7 +1363,7 @@ EX void handle_event(SDL_Event& ev) {
}
}
if(ev.type == SDL_QUIT) {
if(ev.type == SDL_EVENT_QUIT) {
#if CAP_DAILY
if(daily::on) daily::handleQuit(3);
else
@ -1429,7 +1442,7 @@ EX bool interpret_as_direction(int sym, int uni) {
#ifdef FAKE_SDL
return false;
#else
return (sym >= SDLK_KP0 && sym <= SDLK_KP9 && !numlock_on);
return (sym >= int(SDLK_KP0) && sym <= int(SDLK_KP9) && !numlock_on);
#endif
}

View File

@ -1455,7 +1455,7 @@ EX namespace dialog {
display();
#if CAP_SDL2
#if SDLVER >= 2
texthandler = [&ne] (const SDL_TextInputEvent& ev) {
if(key_actions.count(ev.text[0])) return;
ne.s += ev.text;
@ -1466,7 +1466,7 @@ EX namespace dialog {
keyhandler = [this, &ne] (int sym, int uni) {
handleNavigation(sym, uni);
if((uni >= '0' && uni <= '9') || among(uni, '.', '+', '-', '*', '/', '^', '(', ')', ',', '|', 3) || (uni >= 'a' && uni <= 'z')) {
#if !CAP_SDL2
#if SDLVER == 1
if(uni == 3) ne.s += "pi";
else ne.s += uni;
apply_edit();
@ -1791,7 +1791,7 @@ EX namespace dialog {
dialog::addItem("cancel", SDLK_ESCAPE);
dialog::display();
#if CAP_SDL2
#if SDLVER >= 2
texthandler = [this] (const SDL_TextInputEvent& ev) {
int i = isize(*cfileptr) - (editext?0:4);
cfileptr->insert(i, ev.text);
@ -1819,7 +1819,7 @@ EX namespace dialog {
highlight_text = "//missing";
list_skip = 0;
}
#if !CAP_SDL2
#if SDLVER == 1
else if(uni >= 32 && uni < 127) {
s.insert(i, s0 + char(uni));
highlight_text = "//missing";
@ -1946,7 +1946,7 @@ EX namespace dialog {
if(reaction) reaction();
}
else if((u2 = checker(sym, uni)) != "") {
#if !CAP_SDL2
#if SDLVER == 1
for(char c: u2) {
es.insert(editpos, 1, c);
editpos ++;
@ -1959,7 +1959,7 @@ EX namespace dialog {
}
void string_dialog::handle_textinput() {
#if CAP_SDL2
#if SDLVER >= 2
texthandler = [this] (const SDL_TextInputEvent& ev) {
auto& es = *edited_string;
string txt = ev.text;

View File

@ -253,7 +253,7 @@ EX void glflush() {
#if CAP_SDL && !ISMOBILE
SDL_Surface *aux;
#if CAP_SDL2
#if SDLVER >= 2
SDL_Renderer *auxrend;
#else
#define auxrend aux
@ -2661,15 +2661,15 @@ EX void drawqueue() {
if(current_display->separate_eyes() && !vid.usingGL) {
if(aux && (aux->w != s->w || aux->h != s->h)) {
SDL_FreeSurface(aux);
#if CAP_SDL2
SDL_DestroySurface(aux);
#if SDLVER >= 2
SDL_DestroyRenderer(auxrend);
#endif
}
if(!aux) {
aux = SDL_CreateRGBSurface(SDL_SWSURFACE,s->w,s->h,32,0,0,0,0);
#if CAP_SDL2
#if SDLVER >= 2
auxrend = SDL_CreateSoftwareRenderer(aux);
#endif
}

View File

@ -99,7 +99,7 @@ bool rawdisplaystr(int x, int y, int shift, int size, const char *str, int color
bool clicked = (mousex >= rect.x && mousey >= rect.y && mousex <= rect.x+rect.w && mousey <= rect.y+rect.h);
SDL_BlitSurface(txt, NULL, s,&rect);
SDL_FreeSurface(txt);
SDL_DestroySurface(txt);
return clicked;
}

View File

@ -449,7 +449,7 @@ void geometry_information::generate_floorshapes_for(int id, cell *c) {
// special
ld sca = 3 * shFullFloor.rad0 / fsh.rad0;
vector<hyperpoint> cornerlist;
int cor = c->type;

View File

@ -5549,7 +5549,7 @@ EX void drawthemap() {
}
#if CAP_SDL
const Uint8 *keystate = SDL12_GetKeyState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
lmouseover = mouseover;
lmouseover_distant = lmouseover;
bool useRangedOrb = (!(vid.shifttarget & 1) && haveRangedOrb() && lmouseover && lmouseover->cpdist > 1) || (keystate[SDL12(SDLK_RSHIFT, SDL_SCANCODE_RSHIFT)] | keystate[SDL12(SDLK_LSHIFT, SDL_SCANCODE_LSHIFT)]);
@ -6045,7 +6045,7 @@ EX void drawscreen() {
#if CAP_GL
if(!vid.usingGL)
#endif
SDL_FillRect(s, NULL, backcolor);
SDL_FillSurfaceRect(s, NULL, backcolor);
#endif
// displaynum(vx,100, 0, 24, 0xc0c0c0, celldist(cwt.at), ":");

View File

@ -143,14 +143,14 @@ namespace spiral {
SDL_Event event;
while(SDL_PollEvent(&event)) switch (event.type) {
#if !CAP_SDL2
#if SDLVER == 1
case SDL_VIDEORESIZE: {
resize_screen_to(event.resize.w, event.resize.h);
precompute();
break;
}
#endif
#if CAP_SDL2
#if SDLVER == 2
case SDL_WINDOWEVENT: {
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
resize_screen_to(event.window.data1, event.window.data2);
@ -158,13 +158,20 @@ namespace spiral {
break;
}
#endif
case SDL_QUIT: case SDL_MOUSEBUTTONDOWN:
#if SDLVER == 3
case SDL_EVENT_WINDOW_RESIZED: {
resize_screen_to(event.window.data1, event.window.data2);
precompute();
break;
}
#endif
case SDL_EVENT_QUIT: case SDL_EVENT_MOUSE_BUTTON_DOWN:
goto breakloop;
case SDL_KEYDOWN: {
int sym = event.key.keysym.sym;
case SDL_EVENT_KEY_DOWN: {
int sym = SDL23(event.key.keysym.sym, event.key.key);
int uni = 0;
numlock_on = event.key.keysym.mod & KMOD_NUM;
numlock_on = SDL23(event.key.keysym.mod, event.key.mod) & SDL_KMOD_NUM;
if(DKEY == SDLK_RIGHT) velx++;
if(DKEY == SDLK_LEFT) velx--;
if(DKEY == SDLK_UP) vely++;
@ -481,7 +488,7 @@ EX namespace history {
if(dospiral)
bands.push_back(band);
else
SDL_FreeSurface(band);
SDL_DestroySurface(band);
};
if(!band) {
@ -557,7 +564,7 @@ EX namespace history {
if(dospiral) {
spiral::loop(bands);
for(int i=0; i<isize(bands); i++) SDL_FreeSurface(bands[i]);
for(int i=0; i<isize(bands); i++) SDL_DestroySurface(bands[i]);
}
}

View File

@ -188,17 +188,17 @@ string listkeys(config& scfg, int id) {
string lk = "";
for(int i=0; i<SCANCODES; i++)
if(scfg.keyaction[i] == id)
#if CAP_SDL2
#if SDLVER >= 2
lk = lk + " " + SDL_GetScancodeName(SDL_Scancode(i));
#else
lk = lk + " " + SDL_GetKeyName(SDLKey(i));
#endif
#if CAP_SDLJOY
for(int i=0; i<numsticks; i++) for(int k=0; k<SDL_JoystickNumButtons(sticks[i]) && k<MAXBUTTON; k++)
for(int i=0; i<numsticks; i++) for(int k=0; k<SDL_GetNumJoystickButtons(sticks[i]) && k<MAXBUTTON; k++)
if(scfg.joyaction[i][k] == id) {
lk = lk + " " + cts('A'+i)+"-B"+its(k);
}
for(int i=0; i<numsticks; i++) for(int k=0; k<SDL_JoystickNumHats(sticks[i]) && k<MAXHAT; k++)
for(int i=0; i<numsticks; i++) for(int k=0; k<SDL_GetNumJoystickHats(sticks[i]) && k<MAXHAT; k++)
for(int d=0; d<4; d++)
if(scfg.hataction[i][k][d] == id) {
lk = lk + " " + cts('A'+i)+"-"+"URDL"[d];
@ -293,7 +293,7 @@ struct key_configurer {
#if CAP_SDLJOY
joyhandler = [this] (SDL_Event& ev) {
if(ev.type == SDL_JOYBUTTONDOWN && setwhat) {
if(ev.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN && setwhat) {
int joyid = ev.jbutton.which;
int button = ev.jbutton.button;
if(joyid < 8 && button < 32)
@ -302,7 +302,7 @@ struct key_configurer {
return true;
}
else if(ev.type == SDL_JOYHATMOTION && setwhat) {
else if(ev.type == SDL_EVENT_JOYSTICK_HAT_MOTION && setwhat) {
int joyid = ev.jhat.which;
int hat = ev.jhat.hat;
int dir = 4;
@ -357,8 +357,8 @@ struct joy_configurer {
getcstat = ' ';
numaxeconfigs = 0;
for(int j=0; j<numsticks; j++) {
for(int ax=0; ax<SDL_JoystickNumAxes(sticks[j]) && ax < MAXAXE; ax++) if(numaxeconfigs<24) {
int y = SDL_JoystickGetAxis(sticks[j], ax);
for(int ax=0; ax<SDL_GetNumJoystickAxes(sticks[j]) && ax < MAXAXE; ax++) if(numaxeconfigs<24) {
int y = SDL_GetJoystickAxis(sticks[j], ax);
string buf = " ";
if(configdead)
buf += its(y);
@ -614,7 +614,7 @@ void pressaction(int id) {
}
EX int key_to_scan(int sym) {
#if CAP_SDL2
#if SDLVER >= 2
return SDL_GetScancodeFromKey(sym);
#else
return sym;
@ -669,7 +669,7 @@ EX void initConfig() {
int* t = scfg.keyaction;
#if CAP_SDL2
#if SDLVER >= 2
t[SDL_SCANCODE_W] = 16 + 4;
t[SDL_SCANCODE_D] = 16 + 5;
@ -830,7 +830,7 @@ EX void initConfig() {
EX void get_actions(config& scfg) {
#if !ISMOBILE
const Uint8 *keystate = SDL12_GetKeyState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
for(auto& a: action_states_flat) a.last = a.held, a.held = 0;
@ -842,20 +842,20 @@ EX void get_actions(config& scfg) {
#if CAP_SDLJOY
for(int j=0; j<numsticks; j++) {
for(int b=0; b<SDL_JoystickNumButtons(sticks[j]) && b<MAXBUTTON; b++)
if(SDL_JoystickGetButton(sticks[j], b))
for(int b=0; b<SDL_GetNumJoystickButtons(sticks[j]) && b<MAXBUTTON; b++)
if(SDL_GetJoystickButton(sticks[j], b))
pressaction(scfg.joyaction[j][b]);
for(int b=0; b<SDL_JoystickNumHats(sticks[j]) && b<MAXHAT; b++) {
int stat = SDL_JoystickGetHat(sticks[j], b);
for(int b=0; b<SDL_GetNumJoystickHats(sticks[j]) && b<MAXHAT; b++) {
int stat = SDL_GetJoystickHat(sticks[j], b);
if(stat & SDL_HAT_UP) pressaction(scfg.hataction[j][b][0]);
if(stat & SDL_HAT_RIGHT) pressaction(scfg.hataction[j][b][1]);
if(stat & SDL_HAT_DOWN) pressaction(scfg.hataction[j][b][2]);
if(stat & SDL_HAT_LEFT) pressaction(scfg.hataction[j][b][3]);
}
for(int b=0; b<SDL_JoystickNumAxes(sticks[j]) && b<MAXAXE; b++) {
int value = SDL_JoystickGetAxis(sticks[j], b);
for(int b=0; b<SDL_GetNumJoystickAxes(sticks[j]) && b<MAXAXE; b++) {
int value = SDL_GetJoystickAxis(sticks[j], b);
int dz = scfg.deadzoneval[j][b];
if(value > dz) value -= dz; else if(value < -dz) value += dz;
else value = 0;
@ -876,7 +876,7 @@ EX void handleInput(int delta, config &scfg) {
get_actions(scfg);
const Uint8 *keystate = SDL12_GetKeyState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
if(keystate[SDL12(SDLK_LCTRL, SDL_SCANCODE_LCTRL)] || keystate[SDL12(SDLK_RCTRL, SDL_SCANCODE_RCTRL)]) d /= 5;

View File

@ -72,6 +72,10 @@ void set_linux() {
opts = "-DFHS -DLINUX -I/usr/include/SDL2";
libs = " -lSDL2 -lSDL2_ttf -lSDL2_mixer -lSDL2_gfx -lGLEW -lGL -lpng -rdynamic -lpthread -lz";
}
else if(sdlver == 3) {
opts = "-DFHS -DLINUX -I/usr/include/SDL3";
libs = " -lSDL3 -lSDL3_ttf -lSDL3_mixer -lSDL3_gfx -lGLEW -lGL -lpng -rdynamic -lpthread -lz";
}
else if(sdlver == 1) {
opts = "-DFHS -DLINUX -I/usr/include/SDL";
libs = " -lSDL -lSDL_ttf -lSDL_mixer -lSDL_gfx -lGLEW -lGL -lpng -rdynamic -lpthread -lz";
@ -114,6 +118,7 @@ void set_mingw64_cross() {
libs = " hyper64.res -static-libgcc -lopengl32";
if(sdlver == 1) libs += " -lSDL -lSDL_gfx -lSDL_mixer -lSDL_ttf";
if(sdlver == 2) libs += " -lSDL2 -lSDL2_gfx -lSDL2_mixer -lSDL2_ttf";
if(sdlver == 3) libs += " -lSDL3 -lSDL3_gfx -lSDL3_mixer -lSDL3_ttf";
libs += " -lpthread -lz -lglew32 -lpng";
setvbuf(stdout, NULL, _IONBF, 0); // MinGW is quirky with output buffering
if(!file_exists("hyper64.res"))
@ -238,7 +243,7 @@ int main(int argc, char **argv) {
set_os(os);
obj_dir += "/sdl1";
setdir += "../";
opts += " -DCAP_SDL2=0";
opts += " -DSDLVER=1";
}
else if(s == "-sdl0") {
sdlver = 0;
@ -252,7 +257,14 @@ int main(int argc, char **argv) {
set_os(os);
obj_dir += "/sdl2";
setdir += "../";
opts += " -DCAP_SDL2=1";
opts += " -DSDLVER=2";
}
else if(s == "-sdl3") {
sdlver = 3;
set_os(os);
obj_dir += "/sdl3";
setdir += "../";
opts += " -DSDLVER=3";
}
else if(s.substr(0, 2) == "-f" || s.substr(0, 2) == "-m") {
opts += " " + s;

View File

@ -342,12 +342,12 @@ EX namespace netgen {
dynamicval<SDL_Surface*> ds(s);
s = net = SDL_CreateRGBSurface(SDL_SWSURFACE,SX*nscale,SY*nscale,32,0,0,0,0);
#if CAP_SDL2
#if SDLVER >= 2
dynamicval<SDL_Renderer*> dr(srend);
srend = SDL_CreateSoftwareRenderer(s);
#endif
SDL_FillRect(net, NULL, 0xFFFFFF);
SDL_FillSurfaceRect(net, NULL, 0xFFFFFF);
int pateks = 0;
for(int i=0; i<CELLS; i++) patek[i].resize(ct[i]);
@ -442,9 +442,9 @@ EX namespace netgen {
IMAGESAVE(quarter, hr::format("papermodel-page%d%d" IMAGEEXT, iy, ix).c_str());
}
SDL_FreeSurface(net);
SDL_FreeSurface(quarter);
#if CAP_SDL2
SDL_DestroySurface(net);
SDL_DestroySurface(quarter);
#if SDLVER >= 2
SDL_DestroyRenderer(srend);
#endif
}
@ -624,28 +624,32 @@ EX namespace netgen {
SDL_Event event;
while(SDL_PollEvent(&event)) switch (event.type) {
case SDL_QUIT:
case SDL_EVENT_QUIT:
exit(1);
return;
case SDL_MOUSEBUTTONDOWN: {
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
clicked(event.button.x, event.button.y, event.button.button);
break;
}
case SDL_MOUSEBUTTONUP: {
case SDL_EVENT_MOUSE_BUTTON_UP: {
clicked(event.button.x, event.button.y, 16+event.button.button);
break;
}
case SDL_MOUSEMOTION: {
case SDL_EVENT_MOUSE_MOTION: {
clicked(event.motion.x, event.motion.y, 32);
break;
}
case SDL_KEYDOWN: {
case SDL_EVENT_KEY_DOWN: {
#if SDLVER >= 3
int key = event.key.key;
#else
int key = event.key.keysym.sym;
#if CAP_SDL2
#endif
#if SDLVER >= 2
int uni = key;
#else
int uni = event.key.keysym.unicode;
@ -663,7 +667,7 @@ EX namespace netgen {
break;
}
case SDL_KEYUP: {
case SDL_EVENT_KEY_UP: {
rs = 0;
rz = 0;
break;

View File

@ -226,7 +226,7 @@ renderbuffer::~renderbuffer() {
#endif
#if CAP_SDL
if(srf)
SDL_FreeSurface(srf);
SDL_DestroySurface(srf);
#endif
}
@ -238,7 +238,7 @@ void renderbuffer::clear(color_t col) {
}
#endif
#if CAP_SDL
SDL_FillRect(srf, NULL, col);
SDL_FillSurfaceRect(srf, NULL, col);
#endif
}

View File

@ -153,7 +153,7 @@ void pass_time() {
ld delta = t - last_t;
dynamicval<eGeometry> g(geometry, geometry == gTwistedProduct ? geometry : gCubeTiling);
const Uint8 *keystate = SDL12_GetKeyState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
if(keystate['a'] || forcekey == 'a') current = apply_lorentz(current, lorentz(0, 2, delta*accel)), ang = 180, acc = true;
if(keystate['d'] || forcekey == 'd') current = apply_lorentz(current, lorentz(0, 2, -delta*accel)), ang = 0, acc = true;
if(keystate['w'] || forcekey == 'w') current = apply_lorentz(current, lorentz(1, 2, delta*accel)), ang = 90, acc = true;

View File

@ -432,7 +432,7 @@ void bantar_anim() {
SDL_Event ev;
while(SDL_PollEvent(&ev))
if(ev.type == SDL_KEYDOWN || ev.type == SDL_MOUSEBUTTONDOWN)
if(ev.type == SDL_EVENT_KEY_DOWN || ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
breakanim = true;
}
mapeditor::drawplayer = true;

View File

@ -384,7 +384,7 @@ bool last_mkey = false;
extern int mousepx, mousepy;
void game_frame() {
const Uint8 *keystate = SDL12_GetKeyState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
if(keystate['3'])
current_room->place_block_full(mousepx / block_x, mousepy / block_y, 0);
if(keystate['1'])

View File

@ -1347,7 +1347,7 @@ EX void actDraw() {
#endif
#if CAP_HOLDKEYS
const Uint8 *keystate = SDL12_GetKeyState(NULL);
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
if(keystate[SDL12(SDLK_LALT, SDL_SCANCODE_LALT)]) alpha /= 10;
#endif

View File

@ -5,17 +5,24 @@
* http://www.libpng.org/pub/png/src/libpng-LICENSE.txt
*/
#ifdef CAP_SDL2
#if CAP_SDL2
#define USE_SDL2
#endif
#ifndef SDLVER
#define SDLVER 1
#endif
#ifdef USE_SDL2
#include <SDL2/SDL.h>
#else
#if SDLVER == 1
#include <SDL/SDL.h>
#define SDL23(x,y) x
#endif
#if SDLVER == 2
#include <SDL2/SDL.h>
#define SDL23(x,y) x
#endif
#if SDLVER == 3
#include <SDL3/SDL.h>
#define SDL23(x,y) y
#endif
#include <cstdlib>
#include <png.h>
#define SUCCESS 0
@ -35,6 +42,18 @@
#define amask 0xFF000000
#endif
#if SDLVER >= 3
struct pixformat { int BitsPerPixel; int BytesPerPixel; Uint32 Rmask, Gmask, Bmask, Amask; };
pixformat get_format(SDL_PixelFormat val) { pixformat pf; SDL_GetMasksForPixelFormat(val, &pf.BitsPerPixel, &pf.Rmask, &pf.Gmask, &pf.Bmask, &pf.Amask); pf.BytesPerPixel = (pf.BitsPerPixel+7) / 8; return pf; }
#else
SDL_PixelFormat get_format(SDL_PixelFormat *val) { return *val; }
#endif
/* libpng callbacks */
static void png_error_SDL(png_structp ctx, png_const_charp str)
{
@ -42,8 +61,13 @@ static void png_error_SDL(png_structp ctx, png_const_charp str)
}
static void png_write_SDL(png_structp png_ptr, png_bytep data, png_size_t length)
{
SDL_RWops *rw = (SDL_RWops*)png_get_io_ptr(png_ptr);
#if SDLVER >= 3
SDL_IOStream *rw = (SDL_IOStream*)png_get_io_ptr(png_ptr);
SDL_WriteIO(rw, data, length);
#else
SDL_RWops *rw = (SDL_RWops*)png_get_io_ptr(png_ptr);
SDL_RWwrite(rw, data, sizeof(png_byte), length);
#endif
}
#ifdef __cplusplus
@ -54,8 +78,10 @@ SDL_Surface *SDL_PNGFormatAlpha(SDL_Surface *src)
SDL_Surface *surf;
SDL_Rect rect = { 0, 0, 0, 0 };
auto pf = get_format(src->format);
/* NO-OP for images < 32bpp and 32bpp images that already have Alpha channel */
if (src->format->BitsPerPixel <= 24 || src->format->Amask) {
if (pf.BitsPerPixel <= 24 || pf.Amask) {
src->refcount++;
return src;
}
@ -63,22 +89,31 @@ SDL_Surface *SDL_PNGFormatAlpha(SDL_Surface *src)
/* Convert 32bpp alpha-less image to 24bpp alpha-less image */
rect.w = src->w;
rect.h = src->h;
surf = SDL_CreateRGBSurface(src->flags, src->w, src->h, 24,
src->format->Rmask, src->format->Gmask, src->format->Bmask, 0);
#if SDLVER >= 3
surf = SDL_CreateSurface(src->w, src->h, SDL_GetPixelFormatForMasks(24, pf.Rmask, pf.Gmask, pf.Bmask, 0));
SDL_BlitSurfaceUnchecked(src, &rect, surf, &rect);
#else
surf = SDL_CreateRGBSurface(src->flags, src->w, src->h, 24, pf.Rmask, pf.Gmask, pf.Bmask, 0);
SDL_LowerBlit(src, &rect, surf, &rect);
#endif
return surf;
}
#ifdef __cplusplus
extern "C"
#define ERR SDL23(if (freedst) SDL_RWclose(dst); return (ERROR), if (freedst) SDL_CloseIO(dst); return (ERROR))
#endif
int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
int SDL_SavePNG_RW(SDL_Surface *surface, SDL23(SDL_RWops, SDL_IOStream) *dst, int freedst)
{
png_structp png_ptr;
png_infop info_ptr;
#if SDLVER <= 2
png_colorp pal_ptr;
SDL_Palette *pal;
#endif
int i, colortype;
#ifdef USE_ROW_POINTERS
png_bytep *row_pointers;
@ -92,29 +127,25 @@ int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
if (!surface)
{
SDL_SetError("Argument 1 to SDL_SavePNG_RW can't be NULL, expecting SDL_Surface*\n");
if (freedst) SDL_RWclose(dst);
return (ERROR);
ERR;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, png_error_SDL, NULL); /* err_ptr, err_fn, warn_fn */
if (!png_ptr)
{
SDL_SetError("Unable to png_create_write_struct on %s\n", PNG_LIBPNG_VER_STRING);
if (freedst) SDL_RWclose(dst);
return (ERROR);
ERR;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
SDL_SetError("Unable to png_create_info_struct\n");
png_destroy_write_struct(&png_ptr, NULL);
if (freedst) SDL_RWclose(dst);
return (ERROR);
ERR;
}
if (setjmp(png_jmpbuf(png_ptr))) /* All other errors, see also "png_error_SDL" */
{
png_destroy_write_struct(&png_ptr, &info_ptr);
if (freedst) SDL_RWclose(dst);
return (ERROR);
ERR;
}
/* Setup our RWops writer */
@ -122,9 +153,10 @@ int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
/* Prepare chunks */
colortype = PNG_COLOR_MASK_COLOR;
if (surface->format->BytesPerPixel > 0
&& surface->format->BytesPerPixel <= 8
&& (pal = surface->format->palette))
auto pf = get_format(surface->format);
#if SDLVER <= 2
if (pf.BytesPerPixel > 0 && pf.BytesPerPixel <= 8 && (pal = surface->format->palette))
{
colortype |= PNG_COLOR_MASK_PALETTE;
pal_ptr = (png_colorp)malloc(pal->ncolors * sizeof(png_color));
@ -136,7 +168,10 @@ int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
png_set_PLTE(png_ptr, info_ptr, pal_ptr, pal->ncolors);
free(pal_ptr);
}
else if (surface->format->BytesPerPixel > 3 || surface->format->Amask)
#else
if(false) ;
#endif
else if (pf.BytesPerPixel > 3 || pf.Amask)
colortype |= PNG_COLOR_MASK_ALPHA;
png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
@ -145,9 +180,9 @@ int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
// png_set_packing(png_ptr);
/* Allow BGR surfaces */
if (surface->format->Rmask == bmask
&& surface->format->Gmask == gmask
&& surface->format->Bmask == rmask)
if (pf.Rmask == bmask
&& pf.Gmask == gmask
&& pf.Bmask == rmask)
png_set_bgr(png_ptr);
/* Write everything */
@ -166,6 +201,6 @@ int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
/* Done */
png_destroy_write_struct(&png_ptr, &info_ptr);
if (freedst) SDL_RWclose(dst);
if (freedst) SDL23(SDL_RWclose, SDL_CloseIO)(dst);
return (SUCCESS);
}

View File

@ -18,8 +18,13 @@ extern "C" { /* This helps CPP projects that include this header */
* Returns 0 success or -1 on failure, the error message is then retrievable
* via SDL_GetError().
*/
#if SDLVER >= 3
#define SDL_SavePNG(surface, file) \
SDL_SavePNG_RW(surface, SDL_IOFromFile(file, "wb"), 1)
#else
#define SDL_SavePNG(surface, file) \
SDL_SavePNG_RW(surface, SDL_RWFromFile(file, "wb"), 1)
#endif
/*
* Save an SDL_Surface as a PNG file, using writable RWops.
@ -31,7 +36,11 @@ extern "C" { /* This helps CPP projects that include this header */
* Returns 0 success or -1 on failure, the error message is then retrievable
* via SDL_GetError().
*/
#if SDLVER >= 3
extern int SDL_SavePNG_RW(SDL_Surface *surface, SDL_IOStream *rw, int freedst);
#else
extern int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *rw, int freedst);
#endif
/*
* Return new SDL_Surface with a format suitable for PNG output.

View File

@ -626,7 +626,7 @@ EX always_false in;
}
}
IMAGESAVE(s, (filename + "-floors.png").c_str());
SDL_FreeSurface(s);
SDL_DestroySurface(s);
}
#endif
#endif
@ -641,7 +641,7 @@ EX }
void IMAGESAVE(SDL_Surface *s, const char *fname) {
SDL_Surface *s2 = SDL_PNGFormatAlpha(s);
SDL_SavePNG(s2, fname);
if(s != s2) SDL_FreeSurface(s2);
if(s != s2) SDL_DestroySurface(s2);
}
#endif
@ -741,7 +741,7 @@ EX void postprocess(string fname, SDL_Surface *sdark, SDL_Surface *sbright) {
}
}
output(sout, fname);
SDL_FreeSurface(sout);
SDL_DestroySurface(sout);
}
#endif

View File

@ -897,12 +897,8 @@ void movePlayer(monster *m, int delta) {
}
#if CAP_SDL
const Uint8 *keystate = SDL12_GetKeyState(NULL);
#if CAP_SDL2
bool forcetarget = (keystate[SDL_SCANCODE_RSHIFT] | keystate[SDL_SCANCODE_LSHIFT]);
#else
bool forcetarget = (keystate[SDLK_RSHIFT] | keystate[SDLK_LSHIFT]);
#endif
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
bool forcetarget = (keystate[SDL12(SDLK_RSHIFT, SDL_SCANCODE_RSHIFT)] | keystate[SDL12(SDLK_LSHIFT, SDL_SCANCODE_LSHIFT)]);
if(((mousepressed && !forcetarget) || facemouse) && delta > 0 && !mouseout() && !stdracing && GDIM == 2) {
// playermoved = true;
hyperpoint h = inverse_shift(m->pat, mouseh);

View File

@ -214,7 +214,7 @@ EX void initAudio() {
audio = loadMusicInfo();
if(audio) {
if(Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 2048) != 0) {
if(SDL23(Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 2048) != 0, !Mix_OpenAudio(0, nullptr))) {
fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError());
audio = false;
}

View File

@ -129,8 +129,12 @@
#define CAP_SDL (!ISMOBILE)
#endif
#ifndef CAP_SDL2
#define CAP_SDL2 0
#ifndef SDLVER
#ifdef CAP_SDL
#define SDLVER 1
#else
#define SDLVER 0
#endif
#endif
#ifndef CAP_TIMEOFDAY
@ -357,57 +361,76 @@
#include <stdio.h>
#if CAP_SDL
#if CAP_SDL2
#include <SDL2/SDL.h>
#define SDL12(x,y) y
#define SDLK_KP1 SDLK_KP_1
#define SDLK_KP2 SDLK_KP_2
#define SDLK_KP3 SDLK_KP_3
#define SDLK_KP4 SDLK_KP_4
#define SDLK_KP5 SDLK_KP_5
#define SDLK_KP6 SDLK_KP_6
#define SDLK_KP7 SDLK_KP_7
#define SDLK_KP8 SDLK_KP_8
#define SDLK_KP9 SDLK_KP_9
#define SDLK_KP0 SDLK_KP_0
#define SDL12_GetKeyState SDL_GetKeyboardState
#define KEYSTATES SDL_NUM_SCANCODES
#else
#include <SDL/SDL.h>
#define SDL12(x,y) x
#define SDL12_GetKeyState SDL_GetKeyState
#define KEYSTATES SDLK_LAST
#if SDLVER == 3
#include <SDL3/SDL.h>
#endif
#if SDLVER == 2
#include <SDL2/SDL.h>
#endif
#if SDLVER == 1
#include <SDL/SDL.h>
#endif
#endif
#if SDLVER >= 3
#define SDL23(x,y) y
#else
#define SDL23(x,y) x
#endif
#if SDLVER >= 2
#define SDL12(x,y) y
#else
#define SDL12(x,y) x
#endif
#define SDL123(x,y,z) SDL12(x,SDL23(y,z))
#define SDL12_GetKeyState SDL12(SDL_GetKeyState, SDL_GetKeyboardState)
#define sdl_keystate_type SDL23(Uint8, bool)
#define KEYSTATES SDL123(SDLK_LAST, SDL_NUM_SCANCODES, SDL_SCANCODE_COUNT)
#if !ISMAC
#undef main
#endif
#if CAP_SDLAUDIO
#if CAP_SDL2
#include <SDL2/SDL_mixer.h>
#else
#if SDLVER == 1
#include <SDL/SDL_mixer.h>
#endif
#if SDLVER == 2
#include <SDL2/SDL_mixer.h>
#endif
#if SDLVER == 3
#include <SDL3_mixer/SDL_mixer.h>
#endif
#endif
#if CAP_SDLTTF
#if CAP_SDL2
#include <SDL2/SDL_ttf.h>
#else
#if SDLVER == 1
#include <SDL/SDL_ttf.h>
#endif
#if SDLVER == 2
#include <SDL2/SDL_ttf.h>
#endif
#if SDLVER == 3
#include <SDL3_ttf/SDL_ttf.h>
#endif
#endif
#if CAP_SDLGFX
#if CAP_SDL2
#include <SDL2/SDL2_gfxPrimitives.h>
#else
#if SDLVER == 1
#include <SDL/SDL_gfxPrimitives.h>
#endif
#if SDLVER == 2
#include <SDL2/SDL2_gfxPrimitives.h>
#endif
#if SDLVER == 3
#include <SDL3_gfx/SDL3_gfxPrimitives.h>
#endif
#endif
#elif !ISFAKEMOBILE
#if !CAP_SDLGFX && !ISFAKEMOBILE
#define SDLK_F1 (123001)
#define SDLK_F2 (123002)
#define SDLK_F3 (123003)
@ -451,6 +474,86 @@ typedef int SDL_Event;
typedef unsigned int Uint32;
#endif
#if CAP_SDL
#define SDL_error_in(x) SDL23(((x) < 0), !(x))
#if SDLVER == 3
#define SDL_SWSURFACE 0 /* unused parameter */
inline SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) {
return SDL_CreateSurface(width, height, SDL_GetPixelFormatForMasks(depth, Rmask, Gmask, Bmask, Amask));
}
inline int TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h) {
return TTF_GetStringSize(font, text, strlen(text), w, h);
}
inline SDL_Surface* TTF_RenderUTF8_Blended(TTF_Font *font, const char *text, SDL_Color fg) {
return TTF_RenderText_Blended(font, text, strlen(text), fg);
}
inline SDL_Surface* TTF_RenderUTF8_Solid(TTF_Font *font, const char *text, SDL_Color fg) {
return TTF_RenderText_Solid(font, text, strlen(text), fg);
}
#define Mix_GetError SDL_GetError
#endif
#if SDLVER >= 2
#define SDLK_KP1 SDLK_KP_1
#define SDLK_KP2 SDLK_KP_2
#define SDLK_KP3 SDLK_KP_3
#define SDLK_KP4 SDLK_KP_4
#define SDLK_KP5 SDLK_KP_5
#define SDLK_KP6 SDLK_KP_6
#define SDLK_KP7 SDLK_KP_7
#define SDLK_KP8 SDLK_KP_8
#define SDLK_KP9 SDLK_KP_9
#define SDLK_KP0 SDLK_KP_0
#endif
#if SDLVER < 3
#define SDL_EVENT_QUIT SDL_QUIT
#define SDL_EVENT_MOUSE_BUTTON_DOWN SDL_MOUSEBUTTONDOWN
#define SDL_EVENT_MOUSE_BUTTON_UP SDL_MOUSEBUTTONUP
#define SDL_EVENT_MOUSE_MOTION SDL_MOUSEMOTION
#define SDL_EVENT_MOUSE_WHEEL SDL_MOUSEWHEEL
#define SDL_EVENT_KEY_DOWN SDL_KEYDOWN
#define SDL_EVENT_KEY_UP SDL_KEYUP
#define SDL_EVENT_JOYSTICK_BUTTON_DOWN SDL_JOYBUTTONDOWN
#define SDL_EVENT_JOYSTICK_HAT_MOTION SDL_JOYHATMOTION
#define SDL_EVENT_JOYSTICK_AXIS_MOTION SDL_JOYAXISMOTION
#define SDL_EVENT_WINDOW SDL_WINDOWEVENT
#define SDL_EVENT_TEXT_INPUT SDL_TEXTINPUT
#define SDL_KMOD_NUM KMOD_NUM
#define SDL_KMOD_LSHIFT KMOD_LSHIFT
#define SDL_KMOD_LCTRL KMOD_LCTRL
#define SDL_KMOD_LALT KMOD_LALT
#define SDL_KMOD_RSHIFT KMOD_RSHIFT
#define SDL_KMOD_RCTRL KMOD_RCTRL
#define SDL_KMOD_RALT KMOD_RALT
#define SDL_DestroySurface SDL_FreeSurface
#define SDL_FillSurfaceRect SDL_FillRect
#define SDL_GL_DestroyContext SDL_GL_DeleteContext
#define SDL_WINDOW_HIGH_PIXEL_DENSITY SDL_WINDOW_ALLOW_HIGHDPI
#define SDL_CreateWindow(a,b,c,d) SDL_CreateWindow(a,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,b,c,d)
#define SDL_GetCurrentRenderOutputSize SDL_GetRendererOutputSize
#define SDL_GetNumJoystickButtons SDL_JoystickNumButtons
#define SDL_GetJoystickButton SDL_JoystickGetButton
#define SDL_GetJoystickHat SDL_JoystickGetHat
#define SDL_GetNumJoystickHats SDL_JoystickNumHats
#define SDL_GetNumJoystickAxes SDL_JoystickNumAxes
#define SDL_GetJoystickAxis SDL_JoystickGetAxis
#define SDL_OpenJoystick SDL_JoystickOpen
#define SDL_CloseJoystick SDL_JoystickClose
#endif
#if SDLVER >= 3
#define SDL_GetScancodeFromKey(x) SDL_GetScancodeFromKey(x, nullptr)
#endif
#endif
#if ISWEB
extern "C" {
Uint8 *SDL_GetKeyState(void*);

View File

@ -143,6 +143,10 @@ EX cpatterntype cgroup;
#if CAP_PNG
SDL_Surface *convertSurface(SDL_Surface* s) {
#if SDLVER >= 3
return SDL_ConvertSurface(s, SDL_PIXELFORMAT_BGRA8888);
#else
SDL_PixelFormat fmt;
// fmt.format = SDL_PIXELFORMAT_BGRA8888;
fmt.BitsPerPixel = 32;
@ -159,12 +163,13 @@ SDL_Surface *convertSurface(SDL_Surface* s) {
fmt.Aloss = fmt.Rloss = fmt.Gloss = fmt.Bloss = 0;
fmt.palette = NULL;
#if !CAP_SDL2
#if SDLVER <= 1
fmt.alpha = 0;
fmt.colorkey = 0x1ffffff;
#endif
return SDL_ConvertSurface(s, &fmt, SDL_SWSURFACE);
#endif
}
#endif
@ -244,7 +249,7 @@ bool texture_data::readtexture(string tn) {
return false;
}
auto txt2 = convertSurface(txt);
SDL_FreeSurface(txt);
SDL_DestroySurface(txt);
tx = txt2->w, ty = txt2->h;
@ -367,7 +372,7 @@ bool texture_data::readtexture(string tn) {
}
#if CAP_SDL_IMG
SDL_FreeSurface(txt2);
SDL_DestroySurface(txt2);
#endif
return true;
@ -382,7 +387,7 @@ void texture_data::saveRawTexture(string tn) {
for(int x=0; x<twidth; x++)
qpixel(sraw,x,y) = get_texture_pixel(x, y);
IMAGESAVE(sraw, tn.c_str());
SDL_FreeSurface(sraw);
SDL_DestroySurface(sraw);
addMessage(XLAT("Saved the raw texture to %1", tn));
}

8
vr.cpp
View File

@ -1636,12 +1636,8 @@ EX void show_vr_quickmenu() {
bool vr_keys(int sym, int uni) {
#if !ISMOBILE
if(!(cmode & sm::NORMAL)) return false;
const Uint8 *keystate = SDL12_GetKeyState(NULL);
#if CAP_SDL2
if(keystate[SDL_SCANCODE_LALT] || keystate[SDL_SCANCODE_RALT])
#else
if(keystate[SDLK_LALT] || keystate[SDLK_RALT])
#endif
const sdl_keystate_type *keystate = SDL12_GetKeyState(NULL);
if(keystate[SDL12(SDLK_LALT, SDL_SCANCODE_LALT)] || keystate[SDL12(SDLK_RALT, SDL_SCANCODE_RALT)])
{
dialog::key_actions.clear();
callhooks(vr_quickmenu_extensions);