From 7390d11505566346d8872ae4dca7d001bf15aa13 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 22 Oct 2018 00:28:24 +0300 Subject: [PATCH] platform_sdl: adapt SDL backend code to new platform backends system --- engine/platform/sdl/events.c | 168 +--------------------------- engine/platform/sdl/events.h | 20 +++- engine/platform/sdl/in_sdl.c | 202 ++++++++++++++++++++++++++++++++++ engine/platform/sdl/vid_sdl.c | 66 +++++------ 4 files changed, 253 insertions(+), 203 deletions(-) create mode 100644 engine/platform/sdl/in_sdl.c diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c index fb4c645d..2d070dc1 100644 --- a/engine/platform/sdl/events.c +++ b/engine/platform/sdl/events.c @@ -26,13 +26,7 @@ GNU General Public License for more details. #include "vid_common.h" #include "gl_local.h" -extern convar_t *vid_fullscreen; -extern convar_t *snd_mute_losefocus; static int wheelbutton; -static SDL_Joystick *joy; -static SDL_GameController *gamecontroller; - -void R_ChangeDisplaySettingsFast( int w, int h ); /* ============= @@ -194,17 +188,6 @@ static void SDLash_InputEvent( SDL_TextInputEvent input ) } } -/* -============= -SDLash_EnableTextInput - -============= -*/ -void SDLash_EnableTextInput( qboolean enable ) -{ - enable ? SDL_StartTextInput() : SDL_StopTextInput(); -} - /* ============= SDLash_EventFilter @@ -479,7 +462,7 @@ SDLash_RunEvents ============= */ -void SDLash_RunEvents( void ) +void Platform_RunEvents( void ) { SDL_Event event; @@ -487,154 +470,9 @@ void SDLash_RunEvents( void ) SDLash_EventFilter( &event ); } -/* -============= -SDLash_JoyInit_Old - -============= -*/ -static int SDLash_JoyInit_Old( int numjoy ) +void* Platform_GetNativeObject( void ) { - int num; - int i; - - MsgDev( D_INFO, "Joystick: SDL\n" ); - - if( SDL_WasInit( SDL_INIT_JOYSTICK ) != SDL_INIT_JOYSTICK && - SDL_InitSubSystem( SDL_INIT_JOYSTICK ) ) - { - MsgDev( D_INFO, "Failed to initialize SDL Joysitck: %s\n", SDL_GetError() ); - return 0; - } - - if( joy ) - { - SDL_JoystickClose( joy ); - } - - num = SDL_NumJoysticks(); - - if( num > 0 ) - MsgDev( D_INFO, "%i joysticks found:\n", num ); - else - { - MsgDev( D_INFO, "No joystick found.\n" ); - return 0; - } - - for( i = 0; i < num; i++ ) - MsgDev( D_INFO, "%i\t: %s\n", i, SDL_JoystickNameForIndex( i ) ); - - MsgDev( D_INFO, "Pass +set joy_index N to command line, where N is number, to select active joystick\n" ); - - joy = SDL_JoystickOpen( numjoy ); - - if( !joy ) - { - MsgDev( D_INFO, "Failed to select joystick: %s\n", SDL_GetError( ) ); - return 0; - } - - MsgDev( D_INFO, "Selected joystick: %s\n" - "\tAxes: %i\n" - "\tHats: %i\n" - "\tButtons: %i\n" - "\tBalls: %i\n", - SDL_JoystickName( joy ), SDL_JoystickNumAxes( joy ), SDL_JoystickNumHats( joy ), - SDL_JoystickNumButtons( joy ), SDL_JoystickNumBalls( joy ) ); - - SDL_GameControllerEventState( SDL_DISABLE ); - SDL_JoystickEventState( SDL_ENABLE ); - - return num; + return NULL; // SDL don't have it } -/* -============= -SDLash_JoyInit_New - -============= -*/ -static int SDLash_JoyInit_New( int numjoy ) -{ - int temp, num; - int i; - - MsgDev( D_INFO, "Joystick: SDL GameController API\n" ); - - if( SDL_WasInit( SDL_INIT_GAMECONTROLLER ) != SDL_INIT_GAMECONTROLLER && - SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER ) ) - { - MsgDev( D_INFO, "Failed to initialize SDL GameController API: %s\n", SDL_GetError() ); - return 0; - } - - // chance to add mappings from file - SDL_GameControllerAddMappingsFromFile( "controllermappings.txt" ); - - if( gamecontroller ) - { - SDL_GameControllerClose( gamecontroller ); - } - - temp = SDL_NumJoysticks(); - num = 0; - - for( i = 0; i < temp; i++ ) - { - if( SDL_IsGameController( i )) - num++; - } - - if( num > 0 ) - MsgDev( D_INFO, "%i joysticks found:\n", num ); - else - { - MsgDev( D_INFO, "No joystick found.\n" ); - return 0; - } - - for( i = 0; i < num; i++ ) - MsgDev( D_INFO, "%i\t: %s\n", i, SDL_GameControllerNameForIndex( i ) ); - - MsgDev( D_INFO, "Pass +set joy_index N to command line, where N is number, to select active joystick\n" ); - - gamecontroller = SDL_GameControllerOpen( numjoy ); - - if( !gamecontroller ) - { - MsgDev( D_INFO, "Failed to select joystick: %s\n", SDL_GetError( ) ); - return 0; - } -// was added in SDL2-2.0.6, allow build with earlier versions just in case -#if SDL_MAJOR_VERSION > 2 || SDL_MINOR_VERSION > 0 || SDL_PATCHLEVEL >= 6 - MsgDev( D_INFO, "Selected joystick: %s (%i:%i:%i)\n", - SDL_GameControllerName( gamecontroller ), - SDL_GameControllerGetVendor( gamecontroller ), - SDL_GameControllerGetProduct( gamecontroller ), - SDL_GameControllerGetProductVersion( gamecontroller )); -#endif - SDL_GameControllerEventState( SDL_ENABLE ); - SDL_JoystickEventState( SDL_DISABLE ); - - return num; -} - -/* -============= -SDLash_JoyInit - -============= -*/ -int SDLash_JoyInit( int numjoy ) -{ - // 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); -} - - #endif // defined( XASH_SDL ) && !defined( XASH_DEDICATED ) diff --git a/engine/platform/sdl/events.h b/engine/platform/sdl/events.h index 57c5cfd3..a1ab29b2 100644 --- a/engine/platform/sdl/events.h +++ b/engine/platform/sdl/events.h @@ -1,6 +1,6 @@ /* -events.h - SDL event system handlers -Copyright (C) 2015-2017 a1batross +events.h - SDL backend internal header +Copyright (C) 2015-2018 a1batross This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,12 +16,20 @@ GNU General Public License for more details. #pragma once #ifndef KEYWRAPPER_H #define KEYWRAPPER_H +#ifdef XASH_SDL -#ifdef XASH_SDL +#include "platform/platform.h" + +// window management +void VID_RestoreScreenResolution( void ); +void R_ChangeDisplaySettingsFast( int width, int height ); // for fast resizing +qboolean VID_CreateWindow( int width, int height, qboolean fullscreen ); +void VID_DestroyWindow( void ); +void GL_InitExtensions( void ); +qboolean GL_CreateContext( void ); +qboolean GL_UpdateContext( void ); +qboolean GL_DeleteContext( void ); -void SDLash_RunEvents( void ); -void SDLash_EnableTextInput( qboolean enable ); -int SDLash_JoyInit( int numjoy ); // pass -1 to init every joystick #endif // XASH_SDL #endif // KEYWRAPPER_H diff --git a/engine/platform/sdl/in_sdl.c b/engine/platform/sdl/in_sdl.c new file mode 100644 index 00000000..78ed366b --- /dev/null +++ b/engine/platform/sdl/in_sdl.c @@ -0,0 +1,202 @@ +/* +vid_sdl.c - SDL input component +Copyright (C) 2018 a1batross + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ +#ifndef XASH_DEDICATED +#include + +#include "common.h" +#include "keydefs.h" +#include "input.h" +#include "client.h" +#include "vgui_draw.h" +#include "events.h" +#include "sound.h" +#include "vid_common.h" +#include "gl_local.h" + +static SDL_Joystick *joy; +static SDL_GameController *gamecontroller; + +/* +============= +Platform_Vibrate + +============= +*/ +void Platform_Vibrate( float time, char flags ) +{ + // stub +} + +/* +============= +SDLash_EnableTextInput + +============= +*/ +void Platform_EnableTextInput( qboolean enable ) +{ + enable ? SDL_StartTextInput() : SDL_StopTextInput(); +} + +/* +============= +SDLash_JoyInit_Old + +============= +*/ +static int SDLash_JoyInit_Old( int numjoy ) +{ + int num; + int i; + + MsgDev( D_INFO, "Joystick: SDL\n" ); + + if( SDL_WasInit( SDL_INIT_JOYSTICK ) != SDL_INIT_JOYSTICK && + SDL_InitSubSystem( SDL_INIT_JOYSTICK ) ) + { + MsgDev( D_INFO, "Failed to initialize SDL Joysitck: %s\n", SDL_GetError() ); + return 0; + } + + if( joy ) + { + SDL_JoystickClose( joy ); + } + + num = SDL_NumJoysticks(); + + if( num > 0 ) + MsgDev( D_INFO, "%i joysticks found:\n", num ); + else + { + MsgDev( D_INFO, "No joystick found.\n" ); + return 0; + } + + for( i = 0; i < num; i++ ) + MsgDev( D_INFO, "%i\t: %s\n", i, SDL_JoystickNameForIndex( i ) ); + + MsgDev( D_INFO, "Pass +set joy_index N to command line, where N is number, to select active joystick\n" ); + + joy = SDL_JoystickOpen( numjoy ); + + if( !joy ) + { + MsgDev( D_INFO, "Failed to select joystick: %s\n", SDL_GetError( ) ); + return 0; + } + + MsgDev( D_INFO, "Selected joystick: %s\n" + "\tAxes: %i\n" + "\tHats: %i\n" + "\tButtons: %i\n" + "\tBalls: %i\n", + SDL_JoystickName( joy ), SDL_JoystickNumAxes( joy ), SDL_JoystickNumHats( joy ), + SDL_JoystickNumButtons( joy ), SDL_JoystickNumBalls( joy ) ); + + SDL_GameControllerEventState( SDL_DISABLE ); + SDL_JoystickEventState( SDL_ENABLE ); + + return num; +} + +/* +============= +SDLash_JoyInit_New + +============= +*/ +static int SDLash_JoyInit_New( int numjoy ) +{ + int temp, num; + int i; + + MsgDev( D_INFO, "Joystick: SDL GameController API\n" ); + + if( SDL_WasInit( SDL_INIT_GAMECONTROLLER ) != SDL_INIT_GAMECONTROLLER && + SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER ) ) + { + MsgDev( D_INFO, "Failed to initialize SDL GameController API: %s\n", SDL_GetError() ); + return 0; + } + + // chance to add mappings from file + SDL_GameControllerAddMappingsFromFile( "controllermappings.txt" ); + + if( gamecontroller ) + { + SDL_GameControllerClose( gamecontroller ); + } + + temp = SDL_NumJoysticks(); + num = 0; + + for( i = 0; i < temp; i++ ) + { + if( SDL_IsGameController( i )) + num++; + } + + if( num > 0 ) + MsgDev( D_INFO, "%i joysticks found:\n", num ); + else + { + MsgDev( D_INFO, "No joystick found.\n" ); + return 0; + } + + for( i = 0; i < num; i++ ) + MsgDev( D_INFO, "%i\t: %s\n", i, SDL_GameControllerNameForIndex( i ) ); + + MsgDev( D_INFO, "Pass +set joy_index N to command line, where N is number, to select active joystick\n" ); + + gamecontroller = SDL_GameControllerOpen( numjoy ); + + if( !gamecontroller ) + { + MsgDev( D_INFO, "Failed to select joystick: %s\n", SDL_GetError( ) ); + return 0; + } +// was added in SDL2-2.0.6, allow build with earlier versions just in case +#if SDL_MAJOR_VERSION > 2 || SDL_MINOR_VERSION > 0 || SDL_PATCHLEVEL >= 6 + MsgDev( D_INFO, "Selected joystick: %s (%i:%i:%i)\n", + SDL_GameControllerName( gamecontroller ), + SDL_GameControllerGetVendor( gamecontroller ), + SDL_GameControllerGetProduct( gamecontroller ), + SDL_GameControllerGetProductVersion( gamecontroller )); +#endif + SDL_GameControllerEventState( SDL_ENABLE ); + SDL_JoystickEventState( SDL_DISABLE ); + + return num; +} + +/* +============= +Platform_JoyInit + +============= +*/ +int Platform_JoyInit( int numjoy ) +{ + // 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); +} + +#endif // XASH_DEDICATED diff --git a/engine/platform/sdl/vid_sdl.c b/engine/platform/sdl/vid_sdl.c index ca335f6b..2ceb5c8b 100644 --- a/engine/platform/sdl/vid_sdl.c +++ b/engine/platform/sdl/vid_sdl.c @@ -13,14 +13,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ #ifndef XASH_DEDICATED - +#include #include "common.h" #include "client.h" #include "gl_local.h" #include "mod_local.h" #include "input.h" #include "vid_common.h" -#include +#include "platform/sdl/events.h" static vidmode_t *vidmodes = NULL; static int num_vidmodes = 0; @@ -204,18 +204,14 @@ int R_MaxVideoModes( void ) return num_vidmodes; } -vidmode_t R_GetVideoMode( int num ) +vidmode_t *R_GetVideoMode( int num ) { - static vidmode_t error = { NULL }; - if( !vidmodes || num < 0 || num >= R_MaxVideoModes() ) { - error.width = glState.width; - error.height = glState.height; - return error; + return NULL; } - return vidmodes[num]; + return vidmodes + num; } static void R_InitVideoModes( void ) @@ -417,6 +413,24 @@ void GL_UpdateSwapInterval( void ) } } +/* +================= +GL_DeleteContext + +always return false +================= +*/ +qboolean GL_DeleteContext( void ) +{ + if( glw_state.context ) + { + SDL_GL_DeleteContext(glw_state.context); + glw_state.context = NULL; + } + + return false; +} + /* ================= GL_CreateContext @@ -471,24 +485,6 @@ qboolean GL_UpdateContext( void ) return true; } -/* -================= -GL_DeleteContext - -always return false -================= -*/ -qboolean GL_DeleteContext( void ) -{ - if( glw_state.context ) - { - SDL_GL_DeleteContext(glw_state.context); - glw_state.context = NULL; - } - - return false; -} - qboolean VID_SetScreenResolution( int width, int height ) { SDL_DisplayMode want, got; @@ -837,13 +833,14 @@ static void GL_SetupAttributes( void ) /* ================== -R_Init_OpenGL +R_Init_Video ================== */ -qboolean R_Init_OpenGL( void ) +qboolean R_Init_Video( void ) { SDL_DisplayMode displayMode; string safe; + qboolean retval; SDL_GetCurrentDisplayMode(0, &displayMode); glw_state.desktopBitsPixel = SDL_BITSPERPIXEL(displayMode.format); @@ -875,7 +872,12 @@ qboolean R_Init_OpenGL( void ) WIN_SetDPIAwareness(); #endif - return VID_SetMode(); + if( !(retval = VID_SetMode()) ) + { + return retval; + } + + GL_InitExtensions(); } #ifdef XASH_GLES @@ -1253,10 +1255,10 @@ qboolean VID_SetMode( void ) /* ================== -R_Free_OpenGL +R_Free_Video ================== */ -void R_Free_OpenGL( void ) +void R_Free_Video( void ) { GL_DeleteContext ();