From: Timo Korvola Date: Sun, 25 Mar 2018 19:31:50 +0000 (+0300) Subject: First take on switching to SDL 2. X-Git-Url: https://scope-eye.net/git/?a=commitdiff_plain;h=f7829902651103052ffe1384e240d49524ac019c;p=liskon_frotz.git First take on switching to SDL 2. Compiles and somewhat works. Mouse positions are not properly mapped in fullscreen mode (window mode seems to work). SDL_TEXTINPUT events are not yet handled; only lower-case ASCII keyboard input for now. --- diff --git a/Makefile b/Makefile index 8d5ce0d..382f781 100644 --- a/Makefile +++ b/Makefile @@ -127,7 +127,7 @@ BLORB_LIB = $(BLORB_DIR)/blorblib.a SDL_DIR = $(SRCDIR)/sdl SDL_LIB = $(SDL_DIR)/frotz_sdl.a -export SDL_PKGS = libpng libjpeg sdl SDL_mixer freetype2 zlib +export SDL_PKGS = libpng libjpeg sdl2 SDL2_mixer freetype2 zlib SDL_LDFLAGS = `pkg-config $(SDL_PKGS) --libs` -lm diff --git a/src/sdl/Makefile b/src/sdl/Makefile index c32a521..f95f28c 100644 --- a/src/sdl/Makefile +++ b/src/sdl/Makefile @@ -1,6 +1,6 @@ # For GNU Make. -SDL_PKGS ?= libpng libjpeg sdl SDL_mixer freetype2 zlib +SDL_PKGS ?= libpng libjpeg sdl2 SDL2_mixer freetype2 zlib # Dependency generation requires GCC. CC = gcc diff --git a/src/sdl/sf_sound.c b/src/sdl/sf_sound.c index 504c00d..626493e 100644 --- a/src/sdl/sf_sound.c +++ b/src/sdl/sf_sound.c @@ -226,12 +226,12 @@ static EFFECT *getmodule( FILE *f, size_t pos, int len, int num) res->destroy(res); return NULL; } - res->mod = Mix_LoadMUS_RW(SDL_RWFromMem( data, size)); + res->mod = Mix_LoadMUS_RW(SDL_RWFromMem( data, size), true); free(data); } else { - res->mod = Mix_LoadMUS_RW(SDL_RWFromFP(f, false)); + res->mod = Mix_LoadMUS_RW(SDL_RWFromFP(f, false), true); } if (!res->mod) { diff --git a/src/sdl/sf_video.c b/src/sdl/sf_video.c index 38fa815..15d4952 100644 --- a/src/sdl/sf_video.c +++ b/src/sdl/sf_video.c @@ -11,17 +11,14 @@ #include "sf_frotz.h" -static SDL_Rect blitrect = {0,0,0,0}; static char banner[256]; static int isfullscreen; static ulong *sbuffer = NULL; static int sbpitch; // in longs static int dirty = 0; -static int bitsperpixel = 32; -static int RBswap = 0; static int ewidth, eheight; -static SDL_Surface *screen, *off = NULL; -static int mustlockscreen = 0; +static SDL_Renderer *renderer = NULL; +static SDL_Texture *texture = NULL; int m_timerinterval = 100; static void sf_quitconf(); @@ -53,12 +50,10 @@ static int mywcslen( zchar *b) } static void myGrefresh(){ - if (off) { - if (mustlockscreen) SDL_LockSurface(screen); - SDL_BlitSurface(off,NULL,screen,&blitrect); - if (mustlockscreen) SDL_UnlockSurface(screen); - } - SDL_UpdateRect(screen,0,0,0,0); + SDL_UpdateTexture(texture, NULL, sbuffer, sbpitch * sizeof(ulong)); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); } void sf_wpixel( int x, int y, ulong c) @@ -414,95 +409,73 @@ static int check( int R, int G, int B){ extern char stripped_story_name[]; void sf_initvideo( int W, int H, int full) - { - int desired_bpp, needoff, reqW, reqH; - Uint32 video_flags; - SDL_PixelFormat *format; - Uint32 initflags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER | - SDL_INIT_AUDIO; +{ + int reqW, reqH; + Uint32 video_flags, pixfmt; + SDL_Window *win; + Uint32 initflags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER + | SDL_INIT_AUDIO; sprintf(banner, "SDL Frotz v%s - %s (z%d)", frotz_version, f_setup.story_name, h_version); - desired_bpp = 32; - video_flags = 0; - - if ( SDL_Init(initflags) < 0 ) { - os_fatal("Couldn't initialize SDL: %s", SDL_GetError()); - } + video_flags = 0; - CLEANREG(cleanvideo); + if ( SDL_Init(initflags) < 0 ) { + os_fatal("Couldn't initialize SDL: %s", SDL_GetError()); + } - isfullscreen = full; - if (full) video_flags = SDL_FULLSCREEN; - reqW = W; reqH = H; - if (full) - { - if (m_reqW == -1) - { - const SDL_VideoInfo * v = SDL_GetVideoInfo(); - if (v) { m_reqW = v->current_w; m_reqH = v->current_h;} - } - if (m_reqW > reqW) reqW = m_reqW; - if (m_reqH > reqH) reqH = m_reqH; - } - screen = SDL_SetVideoMode( reqW, reqH, desired_bpp, video_flags); - if ( screen == NULL ) { - os_fatal("Couldn't set %dx%dx%d video mode: %s", - reqW, reqH, desired_bpp, SDL_GetError()); - } - SDL_WM_SetCaption(banner,NULL); - bitsperpixel = 32; - - mustlockscreen = SDL_MUSTLOCK(screen); - format = screen->format; - - needoff = (mustlockscreen) || (screen->w != W) || (screen->h != H) || - (format->BitsPerPixel != 24) || - (screen->pitch != 3*W); - - RBswap = 0; - if (!needoff) { - needoff = check(format->Rmask,format->Gmask,format->Bmask); - if ((needoff == -1)){ - RBswap = 1; - needoff = 0; - } + CLEANREG(cleanvideo); + + isfullscreen = full; + reqW = W; reqH = H; + if (full) { + if (m_reqW == -1) { + video_flags = SDL_WINDOW_FULLSCREEN_DESKTOP; + reqW = reqH = 0; + } else { + video_flags = SDL_WINDOW_FULLSCREEN; + if (m_reqW > reqW) reqW = m_reqW; + if (m_reqH > reqH) reqH = m_reqH; + } + } + if ((win = SDL_CreateWindow( + banner, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + reqW, reqH, video_flags))) + renderer = SDL_CreateRenderer(win, -1, 0); else - needoff = 1; + renderer = NULL; + if (renderer == NULL ) { + os_fatal("Couldn't create %dx%d window: %s", + reqW, reqH, SDL_GetError()); + } + if (reqW != W || reqH != H) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + if (SDL_RenderSetLogicalSize(renderer, W, H)) + os_fatal("Failed to set logical rendering size to %dx%d: %s", + W, H, SDL_GetError()); } -// printf("setvideo: gm %dx%d rq %dx%d(f%d) got %dx%d needoff %d\n", W,H,reqW,reqH,full,screen->w,screen->h,needoff); + pixfmt = SDL_MasksToPixelFormatEnum(32, 0xff, 0xff00, 0xff0000, 0); + if (!(texture = SDL_CreateTexture(renderer, pixfmt, + SDL_TEXTUREACCESS_STREAMING, W, H))) + os_fatal("Failed to create texture: %s", SDL_GetError()); + +// printf("setvideo: gm %dx%d rq %dx%d(f%d)\n",W,H,reqW,reqH,full); - if (needoff) { sbuffer = calloc(W*H,sizeof(ulong)); - if (!sbuffer){ - os_fatal("Could not create gc"); - } - off = SDL_CreateRGBSurfaceFrom(sbuffer, - W,H,32,4*W,0xff,0xff00,0xff0000,0); -// off = SDL_CreateRGBSurfaceFrom(sbuffer, -// W,H,32,4*screen->w,0xff,0xff00,0xff0000,0); - if (!off){ - os_fatal("Could not create offscreen surface"); - } - } - else { - sbuffer = (ulong *)screen->pixels; - } + if (!sbuffer) + os_fatal("Could not create gc"); - blitrect.w = W; blitrect.h = H; - blitrect.x = (screen->w - W)/2; - blitrect.y = (screen->h - H)/2; - SDL_EnableUNICODE(1); - SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + // SDL_EnableUNICODE(1); + // SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - SDL_AddTimer(SFdticks,mytimer,NULL); + SDL_AddTimer(SFdticks,mytimer,NULL); - xmin = ymin = 0; - xmax = ewidth = W; - ymax = eheight = H; - sbpitch = W; - dirty = 1; - } + xmin = ymin = 0; + xmax = ewidth = W; + ymax = eheight = H; + sbpitch = W; + dirty = 1; +} /* * os_draw_picture @@ -579,9 +552,16 @@ static ulong mytimeout; int mouse_button; static int numAltQ = 0; +static void set_mouse_xy(int x, int y) +{ + //TODO Scale to logical coordinates. + mouse_x = x + 1; + mouse_y = y + 1; +} + static zword goodzkey( SDL_Event *e, int allowed) { - zword c; + SDL_Keycode c; if (e->type == SDL_QUIT) { sf_quitconf(); @@ -594,8 +574,7 @@ static zword goodzkey( SDL_Event *e, int allowed) if (true) //(e->button.button == SDL_BUTTON_LEFT) { mouse_button = e->button.button; - mouse_x = e->button.x+1-blitrect.x; - mouse_y = e->button.y+1-blitrect.y; + set_mouse_xy(e->button.x, e->button.y); return ZC_SINGLE_CLICK; } return 0; @@ -642,16 +621,16 @@ static zword goodzkey( SDL_Event *e, int allowed) case SDLK_TAB: return (allowed ? VK_TAB : 0); case SDLK_PAGEUP: return (allowed ? VK_PAGE_UP : 0); case SDLK_PAGEDOWN: return (allowed ? VK_PAGE_DOWN : 0); - case SDLK_KP0: return ZC_NUMPAD_MIN+0; - case SDLK_KP1: return ZC_NUMPAD_MIN+1; - case SDLK_KP2: return ZC_NUMPAD_MIN+2; - case SDLK_KP3: return ZC_NUMPAD_MIN+3; - case SDLK_KP4: return ZC_NUMPAD_MIN+4; - case SDLK_KP5: return ZC_NUMPAD_MIN+5; - case SDLK_KP6: return ZC_NUMPAD_MIN+6; - case SDLK_KP7: return ZC_NUMPAD_MIN+7; - case SDLK_KP8: return ZC_NUMPAD_MIN+8; - case SDLK_KP9: return ZC_NUMPAD_MIN+9; + case SDLK_KP_0: return ZC_NUMPAD_MIN+0; + case SDLK_KP_1: return ZC_NUMPAD_MIN+1; + case SDLK_KP_2: return ZC_NUMPAD_MIN+2; + case SDLK_KP_3: return ZC_NUMPAD_MIN+3; + case SDLK_KP_4: return ZC_NUMPAD_MIN+4; + case SDLK_KP_5: return ZC_NUMPAD_MIN+5; + case SDLK_KP_6: return ZC_NUMPAD_MIN+6; + case SDLK_KP_7: return ZC_NUMPAD_MIN+7; + case SDLK_KP_8: return ZC_NUMPAD_MIN+8; + case SDLK_KP_9: return ZC_NUMPAD_MIN+9; case SDLK_F1: return ZC_FKEY_MIN+0; case SDLK_F2: return ZC_FKEY_MIN+1; case SDLK_F3: return ZC_FKEY_MIN+2; @@ -664,10 +643,10 @@ static zword goodzkey( SDL_Event *e, int allowed) case SDLK_F10: return ZC_FKEY_MIN+9; case SDLK_F11: return ZC_FKEY_MIN+10; case SDLK_F12: return ZC_FKEY_MIN+11; - default: break; } - c = e->key.keysym.unicode; - if ((c >= 32 && c <= 126) || (c >= 160)) return c; + c = e->key.keysym.sym; + if (c >= 32 && c <= 126) + return c; return 0; } @@ -983,8 +962,7 @@ zword os_read_mouse(void) // Get the mouse position SDL_PumpEvents(); c = SDL_GetMouseState(&x,&y); - mouse_x = x+1-blitrect.x; - mouse_y = y+1-blitrect.y; + set_mouse_xy(x, y); // Get the last selected menu item // menu_selected = theWnd->GetMenuClick(); //printf("%04x\n",c); @@ -1123,11 +1101,3 @@ static void sf_quitconf() } } -void os_check_events(void) - { - SDL_Event event; - SDL_PumpEvents(); - if (SDL_PeepEvents(&event,1,SDL_GETEVENT,SDL_QUITMASK)) - sf_quitconf(); - } -