rnd-20140103-2-src
authorHolger Schemel <info@artsoft.org>
Fri, 3 Jan 2014 15:54:54 +0000 (16:54 +0100)
committerHolger Schemel <info@artsoft.org>
Sat, 30 Aug 2014 09:00:25 +0000 (11:00 +0200)
* fixed fullscreen option for SDL2 (using only desktop fullscreen now)

ChangeLog
src/conftime.h
src/init.c
src/libgame/sdl.c
src/main.h
src/screens.c

index 1ae9adc1b69a52deeee2cc88d189bc212f145bca..a575452509037cd290f3781b5ef2d39c56bfa1b7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+2014-01-02
+       * fixed fullscreen option for SDL2 (using only desktop fullscreen now)
+
 2013-12-28
        * fixed graphical bugs when using renderer/texture based graphics
 
index 2e16c2fce5bd5478490a12ebbab5f3d8d900cb59..b78c57732ed0f2404a93dddab468eaecca6930be 100644 (file)
@@ -1 +1 @@
-#define COMPILE_DATE_STRING "2014-01-01 20:20"
+#define COMPILE_DATE_STRING "2014-01-03 16:39"
index e3bdd9c8f33fe0ca3f351f13cc0e9a294ecc5ada..723f8af0e2597b1a5924c86104ed4e365cb0b5f6 100644 (file)
@@ -5213,8 +5213,56 @@ void Execute_Command(char *command)
   }
 
 #if DEBUG
-#if defined(TARGET_SDL)
-#if !defined(TARGET_SDL2)
+#if defined(TARGET_SDL2)
+  else if (strEqual(command, "SDL_ListModes"))
+  {
+    SDL_Init(SDL_INIT_VIDEO);
+
+    int num_displays = SDL_GetNumVideoDisplays();
+
+    // check if there are any displays available
+    if (num_displays < 0)
+    {
+      printf("No displays available: %s\n", SDL_GetError());
+
+      exit(-1);
+    }
+
+    for (i = 0; i < num_displays; i++)
+    {
+      int num_modes = SDL_GetNumDisplayModes(i);
+      int j;
+
+      printf("Available display modes for display %d:\n", i);
+
+      // check if there are any display modes available for this display
+      if (num_modes < 0)
+      {
+       printf("No display modes available for display %d: %s\n",
+              i, SDL_GetError());
+
+       exit(-1);
+      }
+
+      for (j = 0; j < num_modes; j++)
+      {
+       SDL_DisplayMode mode;
+
+       if (SDL_GetDisplayMode(i, j, &mode) < 0)
+       {
+         printf("Cannot get display mode %d for display %d: %s\n",
+                j, i, SDL_GetError());
+
+         exit(-1);
+       }
+
+       printf("- %d x %d\n", mode.w, mode.h);
+      }
+    }
+
+    exit(0);
+  }
+#elif defined(TARGET_SDL)
   else if (strEqual(command, "SDL_ListModes"))
   {
     SDL_Rect **modes;
@@ -5240,16 +5288,15 @@ void Execute_Command(char *command)
     }
     else
     {
-      printf("Available Modes:\n");
+      printf("Available display modes:\n");
 
-      for(i = 0; modes[i]; i++)
-       printf("  %d x %d\n", modes[i]->w, modes[i]->h);
+      for (i = 0; modes[i]; i++)
+       printf("- %d x %d\n", modes[i]->w, modes[i]->h);
     }
 
     exit(0);
   }
 #endif
-#endif
 #endif
 
   else
index 74148a4bb7381f58b5488ccaca95e301ba236c29..734307d55709d84060e2894492bb298b7eacd81d 100644 (file)
@@ -56,6 +56,9 @@ static void UpdateScreen(SDL_Rect *rect)
     int bytes_x = screen->pitch / video.width;
     int bytes_y = screen->pitch;
 
+    if (video.fullscreen_enabled)
+      bytes_x = screen->pitch / fullscreen_width;
+
     SDL_UpdateTexture(sdl_texture, rect,
                      screen->pixels + rect->x * bytes_x + rect->y * bytes_y,
                      screen->pitch);
@@ -81,6 +84,14 @@ static void UpdateScreen(SDL_Rect *rect)
 
 static void setFullscreenParameters(char *fullscreen_mode_string)
 {
+#if defined(TARGET_SDL2)
+  fullscreen_width = video.width;
+  fullscreen_height = video.height;
+  fullscreen_xoffset = 0;
+  fullscreen_yoffset = 0;  
+
+#else
+
   struct ScreenModeInfo *fullscreen_mode;
   int i;
 
@@ -103,6 +114,7 @@ static void setFullscreenParameters(char *fullscreen_mode_string)
       break;
     }
   }
+#endif
 }
 
 static void SDLSetWindowIcon(char *basename)
@@ -175,6 +187,7 @@ void SDLInitVideoDisplay(void)
 void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
                        boolean fullscreen)
 {
+#if !defined(TARGET_SDL2)
   static int screen_xy[][2] =
   {
     {  640, 480 },
@@ -182,7 +195,8 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
     { 1024, 768 },
     {   -1,  -1 }
   };
-  SDL_Rect **modes;
+#endif
+  SDL_Rect **modes = NULL;
   int i, j;
 
   /* default: normal game window size */
@@ -191,6 +205,7 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
   fullscreen_xoffset = 0;
   fullscreen_yoffset = 0;
 
+#if !defined(TARGET_SDL2)
   for (i = 0; screen_xy[i][0] != -1; i++)
   {
     if (screen_xy[i][0] >= video.width && screen_xy[i][1] >= video.height)
@@ -204,6 +219,7 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
 
   fullscreen_xoffset = (fullscreen_width  - video.width)  / 2;
   fullscreen_yoffset = (fullscreen_height - video.height) / 2;
+#endif
 
 #if 1
   checked_free(video.fullscreen_modes);
@@ -212,12 +228,37 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
   video.fullscreen_mode_current = NULL;
 #endif
 
-#if !defined(TARGET_SDL2)
+#if defined(TARGET_SDL2)
+  int num_displays = SDL_GetNumVideoDisplays();
+
+  if (num_displays > 0)
+  {
+    // currently only display modes of first display supported
+    int num_modes = SDL_GetNumDisplayModes(0);
+
+    if (num_modes > 0)
+    {
+      modes = checked_calloc((num_modes + 1) * sizeof(SDL_Rect *));
+
+      for (i = 0; i < num_modes; i++)
+      {
+       SDL_DisplayMode mode;
+
+       if (SDL_GetDisplayMode(0, i, &mode) < 0)
+         break;
+
+       modes[i] = checked_calloc(sizeof(SDL_Rect));
+
+       modes[i]->w = mode.w;
+       modes[i]->h = mode.h;
+      }
+    }
+  }
+
+#else
+
   /* get available hardware supported fullscreen modes */
   modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
-#else
-  // (for now, no display modes in SDL2 -- change this later)
-  modes = NULL;
 #endif
 
   if (modes == NULL)
@@ -242,7 +283,7 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
     /* in this case, a certain number of screen modes is available */
     int num_modes = 0;
 
-    for(i = 0; modes[i] != NULL; i++)
+    for (i = 0; modes[i] != NULL; i++)
     {
       boolean found_mode = FALSE;
 
@@ -281,6 +322,16 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
     }
   }
 
+#if defined(TARGET_SDL2)
+  if (modes)
+  {
+    for (i = 0; modes[i] != NULL; i++)
+      checked_free(modes[i]);
+
+    checked_free(modes);
+  }
+#endif
+
 #if 0
   /* set window icon */
   SDLSetWindowIcon(program.sdl_icon_filename);
@@ -324,9 +375,132 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
 #endif
 }
 
+static SDL_Surface *SDLCreateScreen(DrawBuffer **backbuffer,
+                                   boolean fullscreen)
+{
+  SDL_Surface *new_surface = NULL;
+
+  int surface_flags_window = SURFACE_FLAGS;
+#if defined(TARGET_SDL2)
+
+#if 1
+  int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN_DESKTOP;
+#else
+  // (never used with SDL2 now)
+  int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN;
+#endif
+
+#else
+  int surface_flags_fullscreen = SURFACE_FLAGS | SDL_FULLSCREEN;
+#endif
+
+  int width  = (fullscreen ? fullscreen_width  : video.width);
+  int height = (fullscreen ? fullscreen_height : video.height);
+  int surface_flags = (fullscreen ? surface_flags_fullscreen :
+                      surface_flags_window);
+
+#if defined(TARGET_SDL2)
+
+#if USE_RENDERER
+  float scale_factor = 1.2;
+
+  if ((*backbuffer)->surface)
+    SDL_FreeSurface((*backbuffer)->surface);
+
+  if (sdl_texture)
+    SDL_DestroyTexture(sdl_texture);
+
+  if (sdl_renderer)
+    SDL_DestroyRenderer(sdl_renderer);
+
+  if (sdl_window)
+    SDL_DestroyWindow(sdl_window);
+
+  sdl_window = SDL_CreateWindow(program.window_title,
+                               SDL_WINDOWPOS_CENTERED,
+                               SDL_WINDOWPOS_CENTERED,
+                               (int)(scale_factor * width),
+                               (int)(scale_factor * height),
+                               surface_flags);
+
+  if (sdl_window != NULL)
+  {
+    sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
+
+    if (sdl_renderer != NULL)
+    {
+      SDL_RenderSetLogicalSize(sdl_renderer, width, height);
+      SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
+
+      sdl_texture = SDL_CreateTexture(sdl_renderer,
+                                     SDL_PIXELFORMAT_ARGB8888,
+                                     SDL_TEXTUREACCESS_STREAMING,
+                                     width, height);
+
+      if (sdl_texture != NULL)
+      {
+#if 1
+       // (do not use alpha channel)
+       new_surface = SDL_CreateRGBSurface(0, width, height, 32,
+                                          0x00FF0000,
+                                          0x0000FF00,
+                                          0x000000FF,
+                                          0x00000000);
+#else
+       // (this uses an alpha channel, which we don't want here)
+       new_surface = SDL_CreateRGBSurface(0, width, height, 32,
+                                          0x00FF0000,
+                                          0x0000FF00,
+                                          0x000000FF,
+                                          0xFF000000);
+#endif
+
+       if (new_surface == NULL)
+         Error(ERR_WARN, "SDL_CreateRGBSurface() failed: %s",
+               SDL_GetError());
+      }
+      else
+      {
+       Error(ERR_WARN, "SDL_CreateTexture() failed: %s", SDL_GetError());
+      }
+    }
+    else
+    {
+      Error(ERR_WARN, "SDL_CreateRenderer() failed: %s", SDL_GetError());
+    }
+  }
+  else
+  {
+    Error(ERR_WARN, "SDL_CreateWindow() failed: %s", SDL_GetError());
+  }
+
+#else
+
+  if (sdl_window)
+    SDL_DestroyWindow(sdl_window);
+
+  sdl_window = SDL_CreateWindow(program.window_title,
+                               SDL_WINDOWPOS_CENTERED,
+                               SDL_WINDOWPOS_CENTERED,
+                               width, height,
+                               surface_flags);
+
+  if (sdl_window != NULL)
+    new_surface = SDL_GetWindowSurface(sdl_window);
+#endif
+
+#else
+  new_surface = SDL_SetVideoMode(width, height, video.depth, surface_flags);
+#endif
+
+  return new_surface;
+}
+
 boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 {
   boolean success = TRUE;
+#if 1
+#else
 #if defined(TARGET_SDL2)
   // int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN;
   int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN_DESKTOP;
@@ -334,6 +508,7 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 #else
   int surface_flags_fullscreen = SURFACE_FLAGS | SDL_FULLSCREEN;
   int surface_flags_window = SURFACE_FLAGS;
+#endif
 #endif
   SDL_Surface *new_surface = NULL;
 
@@ -352,6 +527,10 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
     video_yoffset = fullscreen_yoffset;
 
     /* switch display to fullscreen mode, if available */
+#if 1
+    new_surface = SDLCreateScreen(backbuffer, TRUE);
+#else
+
 #if defined(TARGET_SDL2)
     sdl_window = SDL_CreateWindow(program.window_title,
                                  SDL_WINDOWPOS_CENTERED,
@@ -368,6 +547,7 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 #else
     new_surface = SDL_SetVideoMode(fullscreen_width, fullscreen_height,
                                   video.depth, surface_flags_fullscreen);
+#endif
 #endif
 
     if (new_surface == NULL)
@@ -397,6 +577,10 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
     video_yoffset = 0;
 
     /* switch display to window mode */
+#if 1
+    new_surface = SDLCreateScreen(backbuffer, FALSE);
+#else
+
 #if defined(TARGET_SDL2)
 
 #if USE_RENDERER
@@ -405,6 +589,18 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
     int surface_flags = (test_fullscreen ? surface_flags_fullscreen :
                         surface_flags_window);
 
+    if ((*backbuffer)->surface)
+      SDL_FreeSurface((*backbuffer)->surface);
+
+    if (sdl_texture)
+      SDL_DestroyTexture(sdl_texture);
+
+    if (sdl_renderer)
+      SDL_DestroyRenderer(sdl_renderer);
+
+    if (sdl_window)
+      SDL_DestroyWindow(sdl_window);
+
     sdl_window = SDL_CreateWindow(program.window_title,
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
@@ -462,7 +658,12 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
     {
       Error(ERR_WARN, "SDL_CreateWindow() failed: %s", SDL_GetError());
     }
+
 #else
+
+    if (sdl_window)
+      SDL_DestroyWindow(sdl_window);
+
     sdl_window = SDL_CreateWindow(program.window_title,
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
@@ -481,6 +682,7 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 #else
     new_surface = SDL_SetVideoMode(video.width, video.height,
                                   video.depth, surface_flags_window);
+#endif
 #endif
 
     if (new_surface == NULL)
@@ -522,7 +724,6 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 #endif
 #endif
 
-
   return success;
 }
 
index 9f02e0e6ccb9e4581eed7de6b0eefc61f31f8d18..2bdefb22840470d4259f6e790a6f633ee877b769 100644 (file)
 
 #define PROGRAM_TITLE_STRING           "Rocks'n'Diamonds"
 #define PROGRAM_AUTHOR_STRING          "Holger Schemel"
-#define PROGRAM_COPYRIGHT_STRING       "Copyright ©1995-2013 by Holger Schemel"
+#define PROGRAM_COPYRIGHT_STRING       "Copyright ©1995-2014 by Holger Schemel"
 #define PROGRAM_EMAIL_STRING           "info@artsoft.org"
 #define PROGRAM_WEBSITE_STRING         "http://www.artsoft.org/"
 #define PROGRAM_GAME_BY_STRING         "A Game by Artsoft Entertainment"
index 4a4dffdd3acc227eb46b97553add39ffa7d6e143..2392bcf0a159648444733d651b82ce164d379e61 100644 (file)
@@ -4242,6 +4242,7 @@ static void execSetupGraphics()
   DrawSetupScreen();
 }
 
+#if !defined(TARGET_SDL2)
 static void execSetupChooseScreenMode()
 {
   if (!video.fullscreen_available)
@@ -4251,6 +4252,7 @@ static void execSetupChooseScreenMode()
 
   DrawSetupScreen();
 }
+#endif
 
 static void execSetupChooseScrollDelay()
 {
@@ -4601,8 +4603,10 @@ static struct TokenInfo setup_info_editor[] =
 static struct TokenInfo setup_info_graphics[] =
 {
   { TYPE_SWITCH,       &setup.fullscreen,      "Fullscreen:"           },
+#if !defined(TARGET_SDL2)
   { TYPE_ENTER_LIST,   execSetupChooseScreenMode, "Fullscreen Mode:"   },
   { TYPE_STRING,       &screen_mode_text,      ""                      },
+#endif
 #if 0
   { TYPE_SWITCH,       &setup.scroll_delay,    "Scroll Delay:"         },
 #endif