From 8dafbaeda20aa8692b726d6777aad2cb5897dbc9 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 24 Oct 2019 12:43:17 +0300 Subject: [PATCH] engine: platform: sdl: dirty wip sdl1.2 port --- engine/platform/sdl/events.c | 84 ++++++++++++++++++++++++++++++- engine/platform/sdl/in_sdl.c | 29 +++++++++-- engine/platform/sdl/s_sdl.c | 20 +++++++- engine/platform/sdl/vid_sdl.c | 95 +++++++++++++++++++++-------------- 4 files changed, 184 insertions(+), 44 deletions(-) diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c index 5da4341a..0db9a15b 100644 --- a/engine/platform/sdl/events.c +++ b/engine/platform/sdl/events.c @@ -39,6 +39,7 @@ static void SDLash_KeyEvent( SDL_KeyboardEvent key ) int keynum = key.keysym.scancode; qboolean numLock = SDL_GetModState() & KMOD_NUM; +#if XASH_SDL == 2 if( SDL_IsTextInputActive() && down ) { if( SDL_GetModState() & KMOD_CTRL ) @@ -52,6 +53,7 @@ static void SDLash_KeyEvent( SDL_KeyboardEvent key ) return; } } +#endif #define DECLARE_KEY_RANGE( min, max, repl ) \ if( keynum >= (min) && keynum <= (max) ) \ @@ -59,10 +61,10 @@ static void SDLash_KeyEvent( SDL_KeyboardEvent key ) keynum = keynum - (min) + (repl); \ } +#if XASH_SDL == 2 DECLARE_KEY_RANGE( SDL_SCANCODE_A, SDL_SCANCODE_Z, 'a' ) else DECLARE_KEY_RANGE( SDL_SCANCODE_1, SDL_SCANCODE_9, '1' ) else DECLARE_KEY_RANGE( SDL_SCANCODE_F1, SDL_SCANCODE_F12, K_F1 ) -#undef DECLARE_KEY_RANGE else { switch( keynum ) @@ -144,6 +146,86 @@ static void SDLash_KeyEvent( SDL_KeyboardEvent key ) return; } } +#else + DECLARE_KEY_RANGE( SDLK_a, SDLK_z, 'a' ) + else DECLARE_KEY_RANGE( SDLK_1, SDLK_9, '1' ) + else DECLARE_KEY_RANGE( SDLK_F1, SDLK_F12, K_F1 ) + else + { + switch( keynum ) + { + case SDLK_BACKQUOTE: keynum = '`'; break; + case SDLK_0: keynum = '0'; break; + case SDLK_BACKSLASH: keynum = '\\'; break; + case SDLK_LEFTBRACKET: keynum = '['; break; + case SDLK_RIGHTBRACKET: keynum = ']'; break; + case SDLK_EQUALS: keynum = '='; break; + case SDLK_MINUS: keynum = '-'; break; + case SDLK_TAB: keynum = K_TAB; break; + case SDLK_RETURN: keynum = K_ENTER; break; + case SDLK_ESCAPE: keynum = K_ESCAPE; break; + case SDLK_SPACE: keynum = K_SPACE; break; + case SDLK_BACKSPACE: keynum = K_BACKSPACE; break; + case SDLK_UP: keynum = K_UPARROW; break; + case SDLK_LEFT: keynum = K_LEFTARROW; break; + case SDLK_DOWN: keynum = K_DOWNARROW; break; + case SDLK_RIGHT: keynum = K_RIGHTARROW; break; + case SDLK_LALT: + case SDLK_RALT: keynum = K_ALT; break; + case SDLK_LCTRL: + case SDLK_RCTRL: keynum = K_CTRL; break; + case SDLK_LSHIFT: + case SDLK_RSHIFT: keynum = K_SHIFT; break; + case SDLK_LMETA: + case SDLK_RMETA: keynum = K_WIN; break; + case SDLK_INSERT: keynum = K_INS; break; + case SDLK_DELETE: keynum = K_DEL; break; + case SDLK_PAGEDOWN: keynum = K_PGDN; break; + case SDLK_PAGEUP: keynum = K_PGUP; break; + case SDLK_HOME: keynum = K_HOME; break; + case SDLK_END: keynum = K_END; break; + case SDLK_KP1: keynum = numLock ? '1' : K_KP_END; break; + case SDLK_KP2: keynum = numLock ? '2' : K_KP_DOWNARROW; break; + case SDLK_KP3: keynum = numLock ? '3' : K_KP_PGDN; break; + case SDLK_KP4: keynum = numLock ? '4' : K_KP_LEFTARROW; break; + case SDLK_KP5: keynum = numLock ? '5' : K_KP_5; break; + case SDLK_KP6: keynum = numLock ? '6' : K_KP_RIGHTARROW; break; + case SDLK_KP7: keynum = numLock ? '7' : K_KP_HOME; break; + case SDLK_KP8: keynum = numLock ? '8' : K_KP_UPARROW; break; + case SDLK_KP9: keynum = numLock ? '9' : K_KP_PGUP; break; + case SDLK_KP0: keynum = numLock ? '0' : K_KP_INS; break; + case SDLK_KP_PERIOD: keynum = K_KP_DEL; break; + case SDLK_KP_ENTER: keynum = K_KP_ENTER; break; + case SDLK_KP_PLUS: keynum = K_KP_PLUS; break; + case SDLK_KP_MINUS: keynum = K_KP_MINUS; break; + case SDLK_KP_DIVIDE: keynum = K_KP_SLASH; break; + case SDLK_KP_MULTIPLY: keynum = '*'; break; + case SDLK_NUMLOCK: keynum = K_KP_NUMLOCK; break; + case SDLK_CAPSLOCK: keynum = K_CAPSLOCK; break; + case SDLK_SLASH: keynum = '/'; break; + case SDLK_PERIOD: keynum = '.'; break; + case SDLK_SEMICOLON: keynum = ';'; break; + case SDLK_QUOTE: keynum = '\''; break; + case SDLK_COMMA: keynum = ','; break; + case SDLK_PRINT: + { + host.force_draw_version = true; + host.force_draw_version_time = host.realtime + FORCE_DRAW_VERSION_TIME; + break; + } + case SDLK_UNKNOWN: + { + if( down ) Con_Reportf( "SDLash_KeyEvent: Unknown scancode\n" ); + return; + } + default: + if( down ) Con_Reportf( "SDLash_KeyEvent: Unknown key: %s = %i\n", SDL_GetScancodeName( keynum ), keynum ); + return; + } + } +#endif + +#undef DECLARE_KEY_RANGE Key_Event( keynum, down ); } diff --git a/engine/platform/sdl/in_sdl.c b/engine/platform/sdl/in_sdl.c index 3321da04..3486c9be 100644 --- a/engine/platform/sdl/in_sdl.c +++ b/engine/platform/sdl/in_sdl.c @@ -25,7 +25,9 @@ GNU General Public License for more details. #include "vid_common.h" static SDL_Joystick *joy; +#if XASH_SDL == 2 static SDL_GameController *gamecontroller; +#endif // XASH_SDL == 2 /* ============= @@ -46,7 +48,11 @@ Platform_SetMousePos */ void Platform_SetMousePos( int x, int y ) { +#if XASH_SDL == 2 SDL_WarpMouseInWindow( host.hWnd, x, y ); +#else + SDL_WarpMouse( x, y ); +#endif } /* @@ -57,6 +63,7 @@ Platform_GetClipobardText */ void Platform_GetClipboardText( char *buffer, size_t size ) { +#if XASH_SDL == 2 char *sdlbuffer = SDL_GetClipboardText(); if( !sdlbuffer ) @@ -64,6 +71,9 @@ void Platform_GetClipboardText( char *buffer, size_t size ) Q_strncpy( buffer, sdlbuffer, size ); SDL_free( sdlbuffer ); +#else + buffer[0] = 0; +#endif } /* @@ -74,7 +84,9 @@ Platform_SetClipobardText */ void Platform_SetClipboardText( const char *buffer, size_t size ) { +#if XASH_SDL == 2 SDL_SetClipboardText( buffer ); +#endif } /* @@ -96,7 +108,9 @@ SDLash_EnableTextInput */ void Platform_EnableTextInput( qboolean enable ) { +#if XASH_SDL == 2 enable ? SDL_StartTextInput() : SDL_StopTextInput(); +#endif } /* @@ -134,8 +148,10 @@ static int SDLash_JoyInit_Old( int numjoy ) return 0; } +#if XASH_SDL == 2 for( i = 0; i < num; i++ ) Con_Reportf( "%i\t: %s\n", i, SDL_JoystickNameForIndex( i ) ); +#endif Con_Reportf( "Pass +set joy_index N to command line, where N is number, to select active joystick\n" ); @@ -147,6 +163,7 @@ static int SDLash_JoyInit_Old( int numjoy ) return 0; } +#if XASH_SDL == 2 Con_Reportf( "Selected joystick: %s\n" "\tAxes: %i\n" "\tHats: %i\n" @@ -156,11 +173,13 @@ static int SDLash_JoyInit_Old( int numjoy ) SDL_JoystickNumButtons( joy ), SDL_JoystickNumBalls( joy ) ); SDL_GameControllerEventState( SDL_DISABLE ); +#endif SDL_JoystickEventState( SDL_ENABLE ); return num; } +#if XASH_SDL == 2 /* ============= SDLash_JoyInit_New @@ -231,6 +250,7 @@ static int SDLash_JoyInit_New( int numjoy ) return num; } +#endif // XASH_SDL == 2 /* ============= @@ -240,12 +260,13 @@ Platform_JoyInit */ int Platform_JoyInit( int numjoy ) { +#if XASH_SDL == 2 // SDL_Joystick is now an old API // SDL_GameController is preferred - if( Sys_CheckParm( "-sdl_joy_old_api" ) ) - return SDLash_JoyInit_Old(numjoy); - - return SDLash_JoyInit_New(numjoy); + if( !Sys_CheckParm( "-sdl_joy_old_api" ) ) + return SDLash_JoyInit_New(numjoy); +#endif // XASH_SDL == 2 + return SDLash_JoyInit_Old(numjoy); } #endif // XASH_DEDICATED diff --git a/engine/platform/sdl/s_sdl.c b/engine/platform/sdl/s_sdl.c index 8c5a3551..3a3497c5 100644 --- a/engine/platform/sdl/s_sdl.c +++ b/engine/platform/sdl/s_sdl.c @@ -76,8 +76,10 @@ qboolean SNDDMA_Init( void ) // even if we don't have PA // we still can safely set env variables +#if XASH_SDL == 2 SDL_setenv( "PULSE_PROP_application.name", GI->title, 1 ); SDL_setenv( "PULSE_PROP_media.role", "game", 1 ); +#endif memset( &desired, 0, sizeof( desired ) ); desired.freq = SOUND_DMA_SPEED; @@ -86,7 +88,10 @@ qboolean SNDDMA_Init( void ) desired.channels = 2; desired.callback = SDL_SoundCallback; +#if XASH_SDL == 2 sdl_dev = SDL_OpenAudioDevice( NULL, 0, &desired, &obtained, 0 ); +#endif + sdl_dev = SDL_OpenAudio( &desired, &obtained ); if( !sdl_dev ) { @@ -116,9 +121,13 @@ qboolean SNDDMA_Init( void ) dma.buffer = Z_Malloc( dma.samples * 2 ); dma.samplepos = 0; +#if XASH_SDL == 2 Con_Printf( "Using SDL audio driver: %s @ %d Hz\n", SDL_GetCurrentAudioDriver( ), obtained.freq ); +#else + Con_Printf( "Using SDL audio driver @ %d Hz\n", obtained.freq ); +#endif - SDL_PauseAudioDevice( sdl_dev, 0 ); + SNDDMA_Activate( true ); dma.initialized = true; return true; @@ -218,9 +227,12 @@ void SNDDMA_Shutdown( void ) if( sdl_dev ) { - SDL_PauseAudioDevice( sdl_dev, 1 ); + SNDDMA_Activate( false ); + #ifndef __EMSCRIPTEN__ +#if XASH_SDL == 2 SDL_CloseAudioDevice( sdl_dev ); +#endif SDL_CloseAudio( ); #endif } @@ -247,6 +259,10 @@ between a deactivate and an activate. */ void SNDDMA_Activate( qboolean active ) { +#if XASH_SDL == 2 SDL_PauseAudioDevice( sdl_dev, !active ); +#else + SDL_PauseAudio( !active ); +#endif } #endif // XASH_SOUND == SOUND_SDL diff --git a/engine/platform/sdl/vid_sdl.c b/engine/platform/sdl/vid_sdl.c index 7816a38e..45205462 100644 --- a/engine/platform/sdl/vid_sdl.c +++ b/engine/platform/sdl/vid_sdl.c @@ -31,8 +31,10 @@ struct struct { +#if XASH_SDL == 2 SDL_Renderer *renderer; SDL_Texture *tex; +#endif int width, height; SDL_Surface *surf; SDL_Surface *win; @@ -43,6 +45,7 @@ qboolean SW_CreateBuffer( int width, int height, uint *stride, uint *bpp, uint * sw.width = width; sw.height = height; +#if XASH_SDL == 2 if( sw.renderer ) { unsigned int format = SDL_GetWindowPixelFormat( host.hWnd ); @@ -110,10 +113,16 @@ qboolean SW_CreateBuffer( int width, int height, uint *stride, uint *bpp, uint * sw.renderer = NULL; } } +#endif +#if XASH_SDL == 2 if( !sw.renderer ) { sw.win = SDL_GetWindowSurface( host.hWnd ); +#else + { + sw.win = SDL_GetVideoSurface(); +#endif // sdl will create renderer if hw framebuffer unavailiable, so cannot fallback here // if it is failed, it is not possible to draw with SDL in REF_SOFTWARE mode @@ -145,6 +154,7 @@ qboolean SW_CreateBuffer( int width, int height, uint *stride, uint *bpp, uint * void *SW_LockBuffer( void ) { +#if XASH_SDL == 2 if( sw.renderer ) { void *pixels; @@ -154,35 +164,38 @@ void *SW_LockBuffer( void ) Sys_Error("%s", SDL_GetError()); return pixels; } - else - { - // ensure it not changed (do we really need this?) - sw.win = SDL_GetWindowSurface( host.hWnd ); - //if( !sw.win ) - //SDL_GetWindowSurface( host.hWnd ); + // ensure it not changed (do we really need this?) + sw.win = SDL_GetWindowSurface( host.hWnd ); + //if( !sw.win ) + //SDL_GetWindowSurface( host.hWnd ); +#else + sw.win = SDL_GetVideoSurface(); +#endif - // prevent buffer overrun - if( !sw.win || sw.win->w < sw.width || sw.win->h < sw.height ) - return NULL; + // prevent buffer overrun + if( !sw.win || sw.win->w < sw.width || sw.win->h < sw.height ) + return NULL; - if( sw.surf ) - { - SDL_LockSurface( sw.surf ); - return sw.surf->pixels; - } - else - { - // real window pixels (x11 shm region, dma buffer, etc) - // or SDL_Renderer texture if not supported - SDL_LockSurface( sw.win ); - return sw.win->pixels; - } +#if XASH_SDL == 2 + if( sw.surf ) + { + SDL_LockSurface( sw.surf ); + return sw.surf->pixels; + } + else +#endif + { + // real window pixels (x11 shm region, dma buffer, etc) + // or SDL_Renderer texture if not supported + SDL_LockSurface( sw.win ); + return sw.win->pixels; } } void SW_UnlockBuffer( void ) { +#if XASH_SDL == 2 if( sw.renderer ) { SDL_Rect src, dst; @@ -197,26 +210,31 @@ void SW_UnlockBuffer( void ) SDL_RenderCopy(sw.renderer, sw.tex, &src, &dst); SDL_RenderPresent(sw.renderer); + + return; //Con_Printf("%s\n", SDL_GetError()); } - else - { - // blit if blitting surface availiable - if( sw.surf ) - { - SDL_Rect src, dst; - src.x = src.y = 0; - src.w = sw.width; - src.h = sw.height; - dst = src; - SDL_UnlockSurface( sw.surf ); - SDL_BlitSurface( sw.surf, &src, sw.win, &dst ); - } - else // already blitted - SDL_UnlockSurface( sw.win ); - SDL_UpdateWindowSurface( host.hWnd ); + // blit if blitting surface availiable + if( sw.surf ) + { + SDL_Rect src, dst; + src.x = src.y = 0; + src.w = sw.width; + src.h = sw.height; + dst = src; + SDL_UnlockSurface( sw.surf ); + SDL_BlitSurface( sw.surf, &src, sw.win, &dst ); + return; } +#endif + + // already blitted + SDL_UnlockSurface( sw.win ); + +#if XASH_SDL == 2 + SDL_UpdateWindowSurface( host.hWnd ); +#endif } int R_MaxVideoModes( void ) @@ -236,6 +254,7 @@ vidmode_t *R_GetVideoMode( int num ) static void R_InitVideoModes( void ) { +#if XASH_SDL == 2 int displayIndex = 0; // TODO: handle multiple displays somehow int i, modes; @@ -280,6 +299,8 @@ static void R_InitVideoModes( void ) num_vidmodes++; } +#else + int i; } static void R_FreeVideoModes( void )