Posix: Replace SDL 1.2 with SDL2

This is required for DeSmuME to start without X11, for instance on a
pure Wayland system.
This commit is contained in:
Emmanuel Gil Peyrot 2020-09-12 20:04:19 +02:00
parent 72386f28de
commit 101fa1ef58
8 changed files with 44 additions and 58 deletions

View File

@ -77,7 +77,8 @@ static float nds_screen_size_ratio = 1.0f;
#define FPS_LIMITER_FPS 60 #define FPS_LIMITER_FPS 60
static SDL_Surface * surface; static SDL_Window * window;
static SDL_Renderer * renderer;
/* Flags to pass to SDL_SetVideoMode */ /* Flags to pass to SDL_SetVideoMode */
static int sdl_videoFlags; static int sdl_videoFlags;
@ -109,7 +110,7 @@ const char * save_type_names[] = {
/* Our keyboard config is different because of the directional keys */ /* Our keyboard config is different because of the directional keys */
/* Please note that m is used for fake microphone */ /* Please note that m is used for fake microphone */
const u16 cli_kb_cfg[NB_KEYS] = const u32 cli_kb_cfg[NB_KEYS] =
{ {
SDLK_x, // A SDLK_x, // A
SDLK_z, // B SDLK_z, // B
@ -330,7 +331,6 @@ resizeWindow( u16 width, u16 height, GLuint *screen_texture) {
int comp_height = 2 * height; int comp_height = 2 * height;
GLenum errCode; GLenum errCode;
surface = SDL_SetVideoMode(width, height, 32, sdl_videoFlags);
initGL(screen_texture); initGL(screen_texture);
#ifdef HAVE_LIBAGG #ifdef HAVE_LIBAGG
@ -438,9 +438,10 @@ Draw( void) {
SDL_Surface *rawImage = SDL_CreateRGBSurfaceFrom(displayInfo.masterNativeBuffer, GPU_FRAMEBUFFER_NATIVE_WIDTH, GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2, 16, GPU_FRAMEBUFFER_NATIVE_WIDTH * sizeof(u16), 0x001F, 0x03E0, 0x7C00, 0); SDL_Surface *rawImage = SDL_CreateRGBSurfaceFrom(displayInfo.masterNativeBuffer, GPU_FRAMEBUFFER_NATIVE_WIDTH, GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2, 16, GPU_FRAMEBUFFER_NATIVE_WIDTH * sizeof(u16), 0x001F, 0x03E0, 0x7C00, 0);
if(rawImage == NULL) return; if(rawImage == NULL) return;
SDL_BlitSurface(rawImage, 0, surface, 0); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, rawImage);
SDL_UpdateRect(surface, 0, 0, 0, 0);
SDL_FreeSurface(rawImage); SDL_FreeSurface(rawImage);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
return; return;
} }
@ -497,8 +498,6 @@ int main(int argc, char ** argv) {
#ifdef INCLUDE_OPENGL_2D #ifdef INCLUDE_OPENGL_2D
GLuint screen_texture[2]; GLuint screen_texture[2];
#endif #endif
/* this holds some info about our display */
const SDL_VideoInfo *videoInfo;
NDS_Init(); NDS_Init();
@ -641,43 +640,23 @@ int main(int argc, char ** argv) {
SDL_GetError()); SDL_GetError());
return 1; return 1;
} }
SDL_WM_SetCaption("Desmume SDL", NULL);
/* Fetch the video info */
videoInfo = SDL_GetVideoInfo( );
if ( !videoInfo ) {
fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) );
exit( -1);
}
/* This checks if hardware blits can be done */
if ( videoInfo->blit_hw )
sdl_videoFlags |= SDL_HWACCEL;
#ifdef INCLUDE_OPENGL_2D #ifdef INCLUDE_OPENGL_2D
if ( my_config.opengl_2d) { if ( my_config.opengl_2d) {
/* the flags to pass to SDL_SetVideoMode */ /* the flags to pass to SDL_SetVideoMode */
sdl_videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */ sdl_videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */
sdl_videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */
sdl_videoFlags |= SDL_RESIZABLE; /* Enable window resizing */ sdl_videoFlags |= SDL_RESIZABLE; /* Enable window resizing */
/* This checks to see if surfaces can be stored in memory */
if ( videoInfo->hw_available )
sdl_videoFlags |= SDL_HWSURFACE;
else
sdl_videoFlags |= SDL_SWSURFACE;
/* Sets up OpenGL double buffering */ /* Sets up OpenGL double buffering */
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
surface = SDL_SetVideoMode( 256, 192 * 2, 32, window = SDL_CreateWindow( "Desmume SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 256, 192 * 2,
sdl_videoFlags ); sdl_videoFlags );
/* Verify there is a surface */ /* Verify there is a window */
if ( !surface ) { if ( !window ) {
fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); fprintf( stderr, "Window creation failed: %s\n", SDL_GetError( ) );
exit( -1); exit( -1);
} }
@ -693,12 +672,14 @@ int main(int argc, char ** argv) {
if ( !my_config.opengl_2d) { if ( !my_config.opengl_2d) {
#endif #endif
sdl_videoFlags |= SDL_SWSURFACE; sdl_videoFlags |= SDL_SWSURFACE;
surface = SDL_SetVideoMode(256, 384, 32, sdl_videoFlags); window = SDL_CreateWindow( "Desmume SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 256, 384, sdl_videoFlags );
if ( !surface ) { if ( !window ) {
fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); fprintf( stderr, "Window creation failed: %s\n", SDL_GetError( ) );
exit( -1); exit( -1);
} }
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
#ifdef INCLUDE_OPENGL_2D #ifdef INCLUDE_OPENGL_2D
} }
@ -804,7 +785,7 @@ int main(int argc, char ** argv) {
snprintf( win_title, sizeof(win_title), "Desmume %f", fps); snprintf( win_title, sizeof(win_title), "Desmume %f", fps);
SDL_WM_SetCaption( win_title, NULL); SDL_SetWindowTitle( window, win_title );
} }
#endif #endif
} }

View File

@ -1325,7 +1325,7 @@ GPU3DInterface *core3DList[] = {
int multisampleSizes[] = {0, 2, 4, 8, 16, 32}; int multisampleSizes[] = {0, 2, 4, 8, 16, 32};
static const u16 gtk_kb_cfg[NB_KEYS] = { static const u32 gtk_kb_cfg[NB_KEYS] = {
GDK_KEY_x, // A GDK_KEY_x, // A
GDK_KEY_z, // B GDK_KEY_z, // B
GDK_KEY_Shift_R, // select GDK_KEY_Shift_R, // select
@ -3898,7 +3898,7 @@ common_gtk_main(GApplication *app, gpointer user_data)
driver = new GtkDriver(); driver = new GtkDriver();
SDL_TimerID limiter_timer = NULL; SDL_TimerID limiter_timer = 0;
GtkWidget *pBox; GtkWidget *pBox;

View File

@ -11,7 +11,7 @@ project('desmume',
) )
dep_glib2 = dependency('glib-2.0') dep_glib2 = dependency('glib-2.0')
dep_sdl = dependency('sdl') dep_sdl = dependency('sdl2')
dep_pcap = dependency('pcap') dep_pcap = dependency('pcap')
dep_zlib = dependency('zlib') dep_zlib = dependency('zlib')
dep_threads = dependency('threads') dep_threads = dependency('threads')

View File

@ -80,7 +80,7 @@ const u16 default_joypad_cfg[NB_KEYS] =
}; };
/* Load default joystick and keyboard configurations */ /* Load default joystick and keyboard configurations */
void load_default_config(const u16 kbCfg[]) void load_default_config(const u32 kbCfg[])
{ {
memcpy(keyboard_cfg, kbCfg, sizeof(keyboard_cfg)); memcpy(keyboard_cfg, kbCfg, sizeof(keyboard_cfg));
memcpy(joypad_cfg, default_joypad_cfg, sizeof(joypad_cfg)); memcpy(joypad_cfg, default_joypad_cfg, sizeof(joypad_cfg));
@ -123,7 +123,7 @@ BOOL init_joy( void) {
for (i = 0; i < nbr_joy; i++) for (i = 0; i < nbr_joy; i++)
{ {
SDL_Joystick * joy = SDL_JoystickOpen(i); SDL_Joystick * joy = SDL_JoystickOpen(i);
printf("Joystick %d %s\n", i, SDL_JoystickName(i)); printf("Joystick %d %s\n", i, SDL_JoystickNameForIndex(i));
printf("Axes: %d\n", SDL_JoystickNumAxes(joy)); printf("Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); printf("Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy));
@ -476,20 +476,24 @@ process_ctrls_event( SDL_Event& event,
if ( !do_process_joystick_events( &cfg->keypad, &event)) { if ( !do_process_joystick_events( &cfg->keypad, &event)) {
switch (event.type) switch (event.type)
{ {
case SDL_VIDEORESIZE: case SDL_WINDOWEVENT:
cfg->resize_cb( event.resize.w, event.resize.h, cfg->screen_texture); switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
cfg->resize_cb(event.window.data1, event.window.data2, cfg->screen_texture);
break; break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_ACTIVEEVENT: if (cfg->auto_pause) {
if (cfg->auto_pause && (event.active.state & SDL_APPINPUTFOCUS )) {
if (event.active.gain) {
cfg->focused = 1; cfg->focused = 1;
SPU_Pause(0); SPU_Pause(0);
driver->AddLine("Auto pause disabled"); driver->AddLine("Auto pause disabled");
} else { }
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
if (cfg->auto_pause) {
cfg->focused = 0; cfg->focused = 0;
SPU_Pause(1); SPU_Pause(1);
} }
break;
} }
break; break;

View File

@ -100,7 +100,7 @@ struct ctrls_event_config {
#endif #endif
}; };
void load_default_config(const u16 kbCfg[]); void load_default_config(const u32 kbCfg[]);
BOOL init_joy( void); BOOL init_joy( void);
void uninit_joy( void); void uninit_joy( void);
u16 get_joy_key(int index); u16 get_joy_key(int index);

View File

@ -25,7 +25,7 @@ static const gchar *desmume_old_config_file = ".desmume.ini";
static const gchar *desmume_config_dir = "desmume"; static const gchar *desmume_config_dir = "desmume";
static const gchar *desmume_config_file = "config"; static const gchar *desmume_config_file = "config";
GKeyFile *desmume_config_read_file(const u16 *kb_cfg) GKeyFile *desmume_config_read_file(const u32 *kb_cfg)
{ {
gchar *config_file, *config_dir, *old_config_file; gchar *config_file, *config_dir, *old_config_file;
GKeyFile *keyfile; GKeyFile *keyfile;

View File

@ -18,7 +18,7 @@
#ifndef _DESMUME_GTK_CONFIG #ifndef _DESMUME_GTK_CONFIG
#define _DESMUME_GTK_CONFIG #define _DESMUME_GTK_CONFIG
GKeyFile *desmume_config_read_file(const u16 *); GKeyFile *desmume_config_read_file(const u32 *);
void desmume_config_dispose(GKeyFile *); void desmume_config_dispose(GKeyFile *);
gboolean desmume_config_update_keys(GKeyFile*); gboolean desmume_config_update_keys(GKeyFile*);

View File

@ -92,6 +92,7 @@ static void MixAudio(void *userdata, Uint8 *stream, int len) {
stream_tmp[i] = soundbuf[soundpos]; stream_tmp[i] = soundbuf[soundpos];
soundpos++; soundpos++;
} }
memset(stream, 0, len);
SDL_MixAudio(stream, stream_tmp, len, audio_volume); SDL_MixAudio(stream, stream_tmp, len, audio_volume);
free(stream_tmp); free(stream_tmp);
} }