Ported AC to SDL2

This commit is contained in:
Jani-Markus Maunus
2018-02-10 11:28:48 +01:00
committed by aurhat
parent 9f537b0876
commit f86579533a
119 changed files with 33154 additions and 10826 deletions
+164 -80
View File
@@ -79,7 +79,8 @@ void fatal(const char *s, ...) // failure exit
exit(EXIT_FAILURE);
}
SDL_Surface *screen = NULL;
SDL_Window *screen = NULL;
SDL_GLContext glcontext = 0;
static int initing = NOT_INITING;
static bool restoredinits = false;
@@ -102,16 +103,32 @@ VARF(stencilbits, 0, 0, 32, initwarning("stencil-buffer precision"));
VARF(fsaa, -1, -1, 16, initwarning("anti-aliasing"));
VARF(vsync, -1, -1, 1, initwarning("vertical sync"));
int windowwidth()
{
int w;
SDL_GetWindowSize(screen, &w, NULL);
return w;
}
int windowheight()
{
int h;
SDL_GetWindowSize(screen, NULL, &h);
return h;
}
static bool grabinput = false, minimized = false;
void inputgrab(bool on)
{
#ifndef WIN32
if(!(screen->flags & SDL_FULLSCREEN)) SDL_WM_GrabInput(SDL_GRAB_OFF);
if(!(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) SDL_SetRelativeMouseMode(SDL_FALSE);
//if(!(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) SDL_SetWindowGrab(screen, SDL_FALSE);
else
#endif
SDL_WM_GrabInput(on ? SDL_GRAB_ON : SDL_GRAB_OFF);
SDL_ShowCursor(on ? SDL_DISABLE : SDL_ENABLE);
SDL_SetRelativeMouseMode(on ? SDL_TRUE : SDL_FALSE);
SDL_SetWindowGrab(screen, on ? SDL_TRUE : SDL_FALSE);
SDL_ShowCursor(on ? 0 : 1);
}
void setfullscreen(bool enable)
@@ -120,9 +137,9 @@ void setfullscreen(bool enable)
#if defined(WIN32) || defined(__APPLE__)
initwarning(enable ? "fullscreen" : "windowed");
#else
if(enable == !(screen->flags&SDL_FULLSCREEN))
if(enable == !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN))
{
SDL_WM_ToggleFullScreen(screen);
SDL_SetWindowFullscreen(screen, enable ? SDL_WINDOW_FULLSCREEN : 0);
inputgrab(grabinput);
}
#endif
@@ -283,8 +300,8 @@ error:
void mapscreenshot(const char *imagepath, bool mapshot, int fileformat, float scale, int height, int quality)
{
extern int minimaplastsize;
int src_w = mapshot ? minimaplastsize : screen->w;
int src_h = mapshot ? minimaplastsize : screen->h;
int src_w = mapshot ? minimaplastsize : windowwidth();
int src_h = mapshot ? minimaplastsize : windowheight();
int dst_w = mapshot ? src_w : src_w * scale, img_w = dst_w;
int dst_h = mapshot ? src_h : src_h * scale, img_h = dst_h;
if(height)
@@ -434,11 +451,22 @@ void screenres(int w, int h)
#else
return;
}
SDL_Surface *surf = SDL_SetVideoMode(w, h, 0, SDL_OPENGL|SDL_RESIZABLE|(screen->flags&SDL_FULLSCREEN));
if(!surf) return;
screen = surf;
scr_w = screen->w;
scr_h = screen->h;
//SDL_Surface *surf = SDL_SetVideoMode(w, h, 0, SDL_OPENGL|SDL_RESIZABLE|(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN));
//if(!surf) return;
//screen = surf;
// TODO SDL2: check if this works / is right
if(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN) {
SDL_DisplayMode dm;
memset(&dm, 0, sizeof(dm));
dm.format = SDL_PIXELFORMAT_UNKNOWN;
dm.w = w;
dm.h = h;
SDL_SetWindowDisplayMode(screen, &dm);
} else {
SDL_SetWindowSize(screen, w, h);
}
scr_w = windowwidth();
scr_h = windowheight();
glViewport(0, 0, scr_w, scr_h);
VIRTW = scr_w*VIRTH/scr_h;
#endif
@@ -456,6 +484,13 @@ COMMANDF(screenres, "ii", (int *w, int *h) { screenres(*w, *h); });
VAR(desktopw, 1, 0, 0);
VAR(desktoph, 1, 0, 0);
int SDL_SetGamma(float r, float g, float b)
{
// FIXME SDL2: implement gamma and get rid of this function
// this is here only to keep the compiler happy.
return -1;
}
static int curgamma = 100;
VARNFP(gamma, vgamma, 30, 100, 300,
{
@@ -481,14 +516,11 @@ void restoregamma()
void getdisplayresolutions()
{
string res = "";
int lh = 0, lw = 0;
SDL_Rect **modes = SDL_ListModes(NULL, SDL_OPENGL|SDL_RESIZABLE|SDL_FULLSCREEN);
if(modes && modes!=(SDL_Rect **)-1)
int lh = 0, lw = 0, n = SDL_GetNumDisplayModes(0);
SDL_DisplayMode mode;
loopi(n)
{
for(int i = 0; modes[i]; i++)
{
if(lw != modes[i]->w && lh != modes[i]->h) concatformatstring(res, "%s%d %d", *res ? " " : "", (lw = modes[i]->w), (lh = modes[i]->h));
}
if(SDL_GetDisplayMode(0, i, &mode) == 0 && lw != mode.w && lh != mode.h) concatformatstring(res, "%s%d %d", *res ? " " : "", (lw = mode.w), (lh = mode.h));
}
result(res);
}
@@ -496,12 +528,14 @@ COMMAND(getdisplayresolutions, "");
void setupscreen(int &usedcolorbits, int &useddepthbits, int &usedfsaa)
{
int flags = SDL_RESIZABLE;
int flags = SDL_WINDOW_RESIZABLE;
#if defined(WIN32) || defined(__APPLE__)
flags = 0;
putenv(newstring("SDL_VIDEO_CENTERED=1")); //Center window
#endif
if(fullscreen) flags |= SDL_FULLSCREEN;
if(fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
/* FIXME SDL2: do we need the following? Everything seems alright...
SDL_Rect **modes = SDL_ListModes(NULL, SDL_OPENGL|flags);
if(modes && modes!=(SDL_Rect **)-1)
{
@@ -512,14 +546,15 @@ void setupscreen(int &usedcolorbits, int &useddepthbits, int &usedfsaa)
}
if(!hasmode) { scr_w = modes[0]->w; scr_h = modes[0]->h; }
}
*/
bool hasbpp = true;
if(colorbits && modes)
hasbpp = SDL_VideoModeOK(modes!=(SDL_Rect **)-1 ? modes[0]->w : scr_w, modes!=(SDL_Rect **)-1 ? modes[0]->h : scr_h, colorbits, SDL_OPENGL|flags)==colorbits;
// FIXME SDL2: do this right or get rid of it
//if(colorbits && modes)
// hasbpp = SDL_VideoModeOK(modes!=(SDL_Rect **)-1 ? modes[0]->w : scr_w, modes!=(SDL_Rect **)-1 ? modes[0]->h : scr_h, colorbits, SDL_OPENGL|flags)==colorbits;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#if SDL_VERSION_ATLEAST(1, 2, 11)
if(vsync>=0) SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vsync);
#endif
if(vsync>=0) SDL_GL_SetSwapInterval(1);
static int configs[] =
{
0x7, /* try everything */
@@ -553,8 +588,17 @@ void setupscreen(int &usedcolorbits, int &useddepthbits, int &usedfsaa)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, config&4 ? 1 : 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, config&4 ? fsaa : 0);
}
screen = SDL_SetVideoMode(scr_w, scr_h, hasbpp ? colorbits : 0, SDL_OPENGL|flags);
if(screen) break;
screen = SDL_CreateWindow("AssaultCube",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
scr_w, scr_h,
SDL_WINDOW_OPENGL | flags);
//if(screen) break;
if(screen)
{
glcontext = SDL_GL_CreateContext(screen);
if(glcontext) break;
SDL_DestroyWindow(screen);
}
}
if(!screen) fatal("Unable to create OpenGL screen");
else
@@ -565,15 +609,11 @@ void setupscreen(int &usedcolorbits, int &useddepthbits, int &usedfsaa)
if(fsaa>0 && (config&4)==0) conoutf("%dx anti-aliasing not supported - disabling", fsaa);
}
scr_w = screen->w;
scr_h = screen->h;
scr_w = windowwidth();
scr_h = windowheight();
VIRTW = scr_w*VIRTH/scr_h;
#ifdef WIN32
SDL_WM_GrabInput(SDL_GRAB_ON);
#else
SDL_WM_GrabInput(fullscreen ? SDL_GRAB_ON : SDL_GRAB_OFF);
#endif
inputgrab(grabinput = fullscreen);
usedcolorbits = hasbpp ? colorbits : 0;
useddepthbits = config&1 ? depthbits : 0;
@@ -601,7 +641,12 @@ void resetgl()
uniformtexres = !hirestextures;
c2skeepalive();
SDL_SetVideoMode(0, 0, 0, 0);
if(screen) {
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(screen);
glcontext = 0;
screen = NULL;
}
int usedcolorbits = 0, useddepthbits = 0, usedfsaa = 0;
setupscreen(usedcolorbits, useddepthbits, usedfsaa);
@@ -670,8 +715,9 @@ COMMAND(fpsrange, "ii");
void keyrepeat(bool on)
{
SDL_EnableKeyRepeat(on ? SDL_DEFAULT_REPEAT_DELAY : 0,
SDL_DEFAULT_REPEAT_INTERVAL);
// FIXME SDL2: implement this
//SDL_EnableKeyRepeat(on ? SDL_DEFAULT_REPEAT_DELAY : 0,
// SDL_DEFAULT_REPEAT_INTERVAL);
}
vector<SDL_Event> events;
@@ -701,16 +747,7 @@ bool interceptkey(int sym)
void togglegrab()
{
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GrabMode(0))
{
SDL_WM_GrabInput(SDL_GRAB_ON);
conoutf("mouse input locked");
}
else
{
SDL_WM_GrabInput(SDL_GrabMode(0));
conoutf("mouse input released");
}
inputgrab(grabinput = !grabinput);
}
COMMAND(togglegrab, "");
@@ -718,9 +755,9 @@ COMMAND(togglegrab, "");
static void resetmousemotion()
{
#ifndef WIN32
if(!(screen->flags&SDL_FULLSCREEN))
if(!(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN))
{
SDL_WarpMouse(screen->w / 2, screen->h / 2);
SDL_WarpMouseInWindow(screen, windowwidth() / 2, windowheight() / 2);
}
#endif
}
@@ -729,12 +766,12 @@ static inline bool skipmousemotion(SDL_Event &event)
{
if(event.type != SDL_MOUSEMOTION) return true;
#ifndef WIN32
if(!(screen->flags&SDL_FULLSCREEN))
if(!(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN))
{
#ifdef __APPLE__
if(event.motion.y == 0) return true; // let mac users drag windows via the title bar
#endif
if(event.motion.x == screen->w / 2 && event.motion.y == screen->h / 2) return true; // ignore any motion events generated SDL_WarpMouse
if(event.motion.x == windowwidth() / 2 && event.motion.y == windowheight() / 2) return true; // ignore any motion events generated SDL_WarpMouse
}
#endif
return false;
@@ -778,6 +815,7 @@ void checkinput()
{
if(events.length()) event = events.remove(0);
extern void textinput(const char *);
switch(event.type)
{
case SDL_QUIT:
@@ -785,8 +823,8 @@ void checkinput()
break;
#if !defined(WIN32) && !defined(__APPLE__)
case SDL_VIDEORESIZE:
screenres(event.resize.w, event.resize.h);
case SDL_WINDOWEVENT_RESIZED:
screenres(event.window.data1, event.window.data2);
break;
#endif
@@ -794,19 +832,40 @@ void checkinput()
if(bootstrapentropy > 0 && (--bootstrapentropy & 2)) mapscreenshot(NULL, false, -1, 1000.0f / (1000 + rnd(400)), 0, 0);
case SDL_KEYUP:
entropy_add_byte(event.key.keysym.sym ^ totalmillis);
keypress(event.key.keysym.sym, event.key.state==SDL_PRESSED, event.key.keysym.unicode, event.key.keysym.mod);
//keypress(event.key.keysym.sym, event.key.state==SDL_PRESSED, event.key.keysym.unicode, event.key.keysym.mod);
// FIXME console input broken here
keypress(event.key.keysym.sym, event.key.state==SDL_PRESSED, SDL_GetModState());
break;
case SDL_ACTIVEEVENT:
if(event.active.state & SDL_APPINPUTFOCUS)
inputgrab(grabinput = event.active.gain!=0);
if(event.active.state & SDL_APPACTIVE)
minimized = !event.active.gain;
#if 0
if(event.active.state==SDL_APPMOUSEFOCUS) setprocesspriority(event.active.gain > 0); // switch priority on focus change
#endif
case SDL_TEXTINPUT:
textinput(event.text.text);
break;
case SDL_WINDOWEVENT:
{
switch(event.window.event)
{
case SDL_WINDOWEVENT_FOCUS_GAINED:
inputgrab(grabinput);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
inputgrab(grabinput = false);
break;
case SDL_WINDOWEVENT_MINIMIZED:
inputgrab(false);
minimized = 1;
break;
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_MAXIMIZED:
minimized = 0;
inputgrab(grabinput);
break;
}
}
case SDL_MOUSEMOTION:
if(ignoremouse) { ignoremouse--; break; }
if(grabinput && !skipmousemotion(event))
@@ -820,12 +879,25 @@ void checkinput()
break;
case SDL_MOUSEBUTTONDOWN:
if(!grabinput)
{
inputgrab(grabinput = true);
break;
}
case SDL_MOUSEBUTTONUP:
if(lasttype==event.type && lastbut==event.button.button) break;
keypress(-event.button.button, event.button.state!=0, 0);
keypress(-event.button.button, event.button.state!=0);
lasttype = event.type;
lastbut = event.button.button;
break;
case SDL_MOUSEWHEEL:
int key = event.wheel.y > 0 ? SDL_AC_BUTTON_WHEELUP : SDL_AC_BUTTON_WHEELDOWN;
// Emulate SDL1-style mouse wheel events by immediately "releasing" the wheel "button"
keypress(key, true);
keypress(key, false);
break;
}
}
if(tdx || tdy)
@@ -939,7 +1011,7 @@ void connectprotocol(char *protocolstring) // assaultcube://example.org[:28763][
DELSTRING(passwd);
}
#ifdef WIN32
#ifdef THISISNOTDEFINED //WIN32
static char *parsecommandline(const char *src, vector<char *> &args)
{
char *buf = new char[strlen(src) + 1], *dst = buf;
@@ -985,6 +1057,16 @@ void initclientlog() // rotate old logfiles and create new one
DELETEP(bootclientlog);
}
/// SDL event filter that drops text input events unless some kind of
/// text input is enabled (text field in menu item active, chat prompt...)
int textinputfilter(void *userdata, SDL_Event *event)
{
extern bool saycommandon;
extern bool menutextinputon();
if(event->type != SDL_TEXTINPUT) return 1;
return (saycommandon || menutextinputon()) ? 1 : 0;
}
#ifdef _DEBUG
void sanitychecks()
{
@@ -1125,9 +1207,10 @@ int main(int argc, char **argv)
par = SDL_INIT_NOPARACHUTE;
#endif
if(SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO|par)<0) fatal("Unable to initialize SDL");
const SDL_version *sdlver = SDL_Linked_Version();
if(SDL_COMPILEDVERSION != SDL_VERSIONNUM(sdlver->major, sdlver->minor, sdlver->patch))
clientlogf("SDL: compiled version " SDLVERSIONSTRING ", linked version %u.%u.%u", sdlver->major, sdlver->minor, sdlver->patch);
SDL_version sdlver;
SDL_GetVersion(&sdlver);
if(SDL_COMPILEDVERSION != SDL_VERSIONNUM(sdlver.major, sdlver.minor, sdlver.patch))
clientlogf("SDL: compiled version " SDLVERSIONSTRING ", linked version %u.%u.%u", sdlver.major, sdlver.minor, sdlver.patch);
#if 0
if(highprocesspriority) setprocesspriority(true);
@@ -1146,18 +1229,13 @@ int main(int argc, char **argv)
initlog("video: sdl");
if(SDL_InitSubSystem(SDL_INIT_VIDEO)<0) fatal("Unable to initialize SDL Video");
#ifndef __APPLE__
SDL_Surface *icon = IMG_Load("packages/misc/icon.png");
SDL_WM_SetIcon(icon, NULL);
#endif
initlog("video: mode");
const SDL_VideoInfo *video = SDL_GetVideoInfo();
/* const SDL_VideoInfo *video = SDL_GetVideoInfo();
if(video)
{
desktopw = video->current_w;
desktoph = video->current_h;
}
}*/
int usedcolorbits = 0, useddepthbits = 0, usedfsaa = 0;
setupscreen(usedcolorbits, useddepthbits, usedfsaa);
@@ -1166,7 +1244,11 @@ int main(int argc, char **argv)
//initfont();
initlog("video: misc");
SDL_WM_SetCaption("AssaultCube", NULL);
#ifndef __APPLE__
SDL_Surface *icon = IMG_Load("packages/misc/icon.png");
SDL_SetWindowIcon(screen, icon);
#endif
keyrepeat(false);
SDL_ShowCursor(0);
@@ -1285,6 +1367,8 @@ int main(int argc, char **argv)
if(initscript) execute(initscript);
SDL_SetEventFilter(textinputfilter, NULL);
initlog("mainloop");
inputgrab(grabinput = true);
@@ -1344,8 +1428,8 @@ int main(int argc, char **argv)
computeraytable(camera1->o.x, camera1->o.y, dynfov());
if(frames>3 && !minimized)
{
gl_drawframe(screen->w, screen->h, fps<lowfps ? fps/lowfps : (fps>highfps ? fps/highfps : 1.0f), fps, elapsed);
if(frames>4) SDL_GL_SwapBuffers();
gl_drawframe(windowwidth(), windowheight(), fps<lowfps ? fps/lowfps : (fps>highfps ? fps/highfps : 1.0f), fps, elapsed);
if(frames>4) SDL_GL_SwapWindow(screen);
}
if(needsautoscreenshot)
@@ -1355,7 +1439,7 @@ int main(int argc, char **argv)
// in the screenshot regardless of which frame buffer is current
if(!minimized)
{
gl_drawframe(screen->w, screen->h, fps<lowfps ? fps/lowfps : (fps>highfps ? fps/highfps : 1.0f), fps, elapsed);
gl_drawframe(windowwidth(), windowheight(), fps<lowfps ? fps/lowfps : (fps>highfps ? fps/highfps : 1.0f), fps, elapsed);
}
addsleep(0, "screenshot");
needsautoscreenshot = false;