rnd-20131212-2-src
authorHolger Schemel <info@artsoft.org>
Thu, 12 Dec 2013 16:58:35 +0000 (17:58 +0100)
committerHolger Schemel <info@artsoft.org>
Sat, 30 Aug 2014 09:00:17 +0000 (11:00 +0200)
* added SDL2 renderer/texture based graphics frame handling to allow for
  "desktop" style fullscreen mode and scaling of game screen/window

ChangeLog
src/conftime.h
src/events.c
src/libgame/sdl.c

index 75151cfd2abc822318023f908c30307d607f973e..24523b82fa6be638842bc8d6428d3537dad9a9b6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-12-12
+       * added SDL2 renderer/texture based graphics frame handling to allow for
+         "desktop" style fullscreen mode and scaling of game screen/window
+
 2013-12-11
        * removed limitation of artwork files to selected file types (this means
          that every file type supported by SDL_image and SDL_mixer can be used)
index 22aa5b68d40be22453c9538170b15b5a7b381b86..7c5c7076f178706045a6c41acd0b12896636b60a 100644 (file)
@@ -1 +1 @@
-#define COMPILE_DATE_STRING "2013-12-11 23:41"
+#define COMPILE_DATE_STRING "2013-12-12 17:46"
index 4c9ecc8b569b0629a879f0e41746c163f299d343..d1880faee1297faa523d874dd3fda6c4b9ed28f4 100644 (file)
@@ -388,6 +388,9 @@ void HandleMotionEvent(MotionEvent *event)
 #if defined(TARGET_SDL2)
 void HandleFingerEvent(FingerEvent *event)
 {
+  static int num_events = 0;
+  int max_events = 10;
+
   // #if DEBUG_EVENTS
   Error(ERR_DEBUG, "FINGER EVENT: finger was %s, touch ID %lld, finger ID %lld, x/y %f/%f, dx/dy %f/%f, pressure %f",
        (event->type == EVENT_FINGERPRESS ? "pressed" :
@@ -399,15 +402,45 @@ void HandleFingerEvent(FingerEvent *event)
        event->pressure);
   // #endif
 
+  int x = (int)(event->x * video.width);
+  int y = (int)(event->y * video.height);
+  int button = MB_LEFTBUTTON;
+
+  Error(ERR_DEBUG, "=> screen x/y %d/%d", x, y);
+
+#if 0
+  if (++num_events >= max_events)
+    CloseAllAndExit(0);
+#endif
+
 #if 1
-  CloseAllAndExit(0);
-#else
   if (event->type == EVENT_FINGERPRESS)
-    button_status = event->button;
+    button_status = button;
   else
     button_status = MB_RELEASED;
 
-  HandleButton(event->x, event->y, button_status, event->button);
+  int max_x = SX + SXSIZE;
+  int max_y = SY + SYSIZE;
+
+  if (game_status == GAME_MODE_PLAYING &&
+      x < max_x)
+  {
+    int key_status = (event->type == EVENT_FINGERRELEASE ? KEY_RELEASED :
+                     KEY_PRESSED);
+    Key key = (y <     max_y / 3 ? setup.input[0].key.up :
+              y > 2 * max_y / 3 ? setup.input[0].key.down :
+              x <     max_x / 3 ? setup.input[0].key.left :
+              x > 2 * max_x / 3 ? setup.input[0].key.right :
+              setup.input[0].key.drop);
+
+    Error(ERR_DEBUG, "=> key == %d, key_status == %d", key, key_status);
+
+    HandleKey(key, key_status);
+  }
+  else
+  {
+    HandleButton(x, y, button_status, button);
+  }
 #endif
 }
 #endif
index c7a2b1171561546dd2a9c5f4739283627dd48196..52766b17d600c54f88dad072c26e258b0ca91022 100644 (file)
@@ -27,8 +27,8 @@
 /* SDL internal variables */
 #if defined(TARGET_SDL2)
 static SDL_Window *sdl_window = NULL;
-// SDL_Window *sdl_window = NULL;
-// static SDL_Renderer *sdl_renderer = NULL;
+static SDL_Renderer *sdl_renderer = NULL;
+static SDL_Texture *sdl_texture = NULL;
 #endif
 
 /* stuff needed to work around SDL/Windows fullscreen drawing bug */
@@ -42,6 +42,23 @@ static int video_yoffset;
 /* functions from SGE library */
 void sge_Line(SDL_Surface *, Sint16, Sint16, Sint16, Sint16, Uint32);
 
+static void UpdateScreen(SDL_Rect *rect)
+{
+#if 1
+  SDL_Surface *screen = backbuffer->surface;
+
+  SDL_UpdateTexture(sdl_texture, NULL, screen->pixels, screen->pitch);
+  SDL_RenderClear(sdl_renderer);
+  SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL);
+  SDL_RenderPresent(sdl_renderer);
+#else
+  if (rect)
+    SDL_UpdateWindowSurfaceRects(sdl_window, rect, 1);
+  else
+    SDL_UpdateWindowSurface(sdl_window);
+#endif
+}
+
 static void setFullscreenParameters(char *fullscreen_mode_string)
 {
   struct ScreenModeInfo *fullscreen_mode;
@@ -290,7 +307,8 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 {
   boolean success = TRUE;
 #if defined(TARGET_SDL2)
-  int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN;
+  // int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN;
+  int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN_DESKTOP;
   int surface_flags_window = SURFACE_FLAGS;
 #else
   int surface_flags_fullscreen = SURFACE_FLAGS | SDL_FULLSCREEN;
@@ -323,7 +341,8 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
     {
       new_surface = SDL_GetWindowSurface(sdl_window);
 
-      SDL_UpdateWindowSurface(sdl_window);     // immediately map window
+      // SDL_UpdateWindowSurface(sdl_window);  // immediately map window
+      UpdateScreen(NULL);      // immediately map window
     }
 #else
     new_surface = SDL_SetVideoMode(fullscreen_width, fullscreen_height,
@@ -358,17 +377,76 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 
     /* switch display to window mode */
 #if defined(TARGET_SDL2)
+
+#if 1
+    float scale_factor = 1;
+    int test_fullscreen = 0;
+    int surface_flags = (test_fullscreen ? surface_flags_fullscreen :
+                        surface_flags_window);
+
+    sdl_window = SDL_CreateWindow(program.window_title,
+                                 SDL_WINDOWPOS_CENTERED,
+                                 SDL_WINDOWPOS_CENTERED,
+                                 (int)(scale_factor * video.width),
+                                 (int)(scale_factor * video.height),
+                                 surface_flags);
+
+    if (sdl_window != NULL)
+    {
+      sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
+
+      if (sdl_renderer != NULL)
+      {
+       SDL_RenderSetLogicalSize(sdl_renderer, video.width, video.height);
+       SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
+
+       sdl_texture = SDL_CreateTexture(sdl_renderer,
+                                       SDL_PIXELFORMAT_ARGB8888,
+                                       SDL_TEXTUREACCESS_STREAMING,
+                                       video.width, video.height);
+
+       if (sdl_texture != NULL)
+       {
+         new_surface = SDL_CreateRGBSurface(0, video.width, video.height, 32,
+                                            0x00FF0000,
+                                            0x0000FF00,
+                                            0x000000FF,
+                                            0xFF000000);
+
+         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
     sdl_window = SDL_CreateWindow(program.window_title,
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  video.width, video.height,
                                  surface_flags_window);
+
     if (sdl_window != NULL)
     {
       new_surface = SDL_GetWindowSurface(sdl_window);
 
-      SDL_UpdateWindowSurface(sdl_window);     // immediately map window
+      // SDL_UpdateWindowSurface(sdl_window);  // immediately map window
+      UpdateScreen(NULL);      // immediately map window
     }
+#endif
+
 #else
     new_surface = SDL_SetVideoMode(video.width, video.height,
                                   video.depth, surface_flags_window);
@@ -479,7 +557,8 @@ void SDLCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
   if (dst_bitmap == window)
   {
     // SDL_UpdateWindowSurface(sdl_window);
-    SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect, 1);
+    // SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect, 1);
+    UpdateScreen(&dst_rect);
   }
 #else
   if (dst_bitmap == window)
@@ -510,7 +589,8 @@ void SDLFillRectangle(Bitmap *dst_bitmap, int x, int y, int width, int height,
   if (dst_bitmap == window)
   {
     // SDL_UpdateWindowSurface(sdl_window);
-    SDL_UpdateWindowSurfaceRects(sdl_window, &rect, 1);
+    // SDL_UpdateWindowSurfaceRects(sdl_window, &rect, 1);
+    UpdateScreen(&rect);
   }
 #else
   if (dst_bitmap == window)
@@ -760,7 +840,8 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
 
 #if defined(TARGET_SDL2)
        // SDL_UpdateWindowSurface(sdl_window);
-       SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect2, 1);
+       // SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect2, 1);
+       UpdateScreen(&dst_rect2);
 #else
        SDL_UpdateRect(surface_screen, dst_x, dst_y, width, height);
 #endif
@@ -798,7 +879,8 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
       /* only update the region of the screen that is affected from fading */
 #if defined(TARGET_SDL2)
       // SDL_UpdateWindowSurface(sdl_window);
-      SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect, 1);
+      // SDL_UpdateWindowSurfaceRects(sdl_window, &dst_rect, 1);
+      UpdateScreen(&dst_rect);
 #else
       SDL_UpdateRect(surface_screen, dst_x, dst_y, width, height);
 #endif