DEADSOFTWARE

Added touchscreen controls
[d2df-sdl.git] / src / game / g_window.pas
index eda53a85bd8db361f379d4b21e13010a34a97bb9..3a29d0803081a585868b03fdd87b0bd01b7e8748 100644 (file)
@@ -51,12 +51,18 @@ implementation
 
 uses
 {$IFDEF WINDOWS}Windows,{$ENDIF}
+{$IFDEF USE_NANOGL}
+  nanoGL,
+{$ELSE}
+  GL, GLExt,
+{$ENDIF}
   SysUtils, Classes, MAPDEF,
-  SDL2, GL, GLExt, e_graphics, e_log, e_texture, g_main,
+  SDL2, e_graphics, e_log, e_texture, g_main,
   g_console, e_input, g_options, g_game,
   g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
   g_map, g_gfx, g_monsters, g_holmes, xprofiler,
-  sdlcarcass, fui_ctls;
+  sdlcarcass, fui_ctls,
+  g_touch;
 
 
 const
@@ -86,7 +92,13 @@ procedure KillGLWindow (preserveGL: Boolean);
 begin
   if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end;
   if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
-  if (h_GL <> nil) and (not preserveGL) then SDL_GL_DeleteContext(h_GL);
+  if (h_GL <> nil) and (not preserveGL) then
+  begin
+{$IFDEF USE_NANOGL}
+    nanoGL_Destroy;
+{$ENDIF USE_NANOGL}
+    SDL_GL_DeleteContext(h_GL);
+  end;
   h_Wnd := nil;
   if (not preserveGL) then h_GL := nil;
 end;
@@ -460,6 +472,8 @@ begin
     SDL_KEYDOWN, SDL_KEYUP:
       begin
         key := ev.key.keysym.scancode;
+        if key = SDL_SCANCODE_AC_BACK then
+          key := SDL_SCANCODE_ESCAPE;
         down := (ev.type_ = SDL_KEYDOWN);
         {$IF not DEFINED(HEADLESS)}
         if fuiOnSDLEvent(ev) then
@@ -486,6 +500,9 @@ begin
         if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
       end;
 
+    SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP:
+      g_Touch_HandleEvent(ev.tfinger);
+
     // other key presses and joysticks are handled in e_input
   end;
 end;
@@ -523,8 +540,16 @@ begin
   if (h_GL = nil) then exit;
   fuiScrWdt := gScreenWidth;
   fuiScrHgt := gScreenHeight;
-  if (assigned(oglInitCB)) then oglInitCB();
   SDL_GL_MakeCurrent(h_Wnd, h_GL);
+{$IFDEF USE_NANOGL}
+  if nanoGL_Init() = 0 then
+  begin
+    KillGLWindow(false);
+    e_WriteLog('nanoGL initialization error', TMsgType.Fatal);
+    exit;
+  end;
+{$ENDIF}
+  if (assigned(oglInitCB)) then oglInitCB();
   if (h_GL <> nil) then g_SetVSync(gVSync);
 {$ENDIF}
 
@@ -776,14 +801,20 @@ end;
 procedure InitOpenGL ();
 begin
 {$IF not DEFINED(HEADLESS)}
-  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
-  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
-  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
-  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
-  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
-  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
-  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-  SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
+  {$IFDEF USE_NANOGL}
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);    
+  {$ELSE}
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
+    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
+    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
+    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
+    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
+    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
+  {$ENDIF}
 {$ENDIF}
 end;
 
@@ -937,6 +968,7 @@ begin
   if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
   begin
     result := 0;
+    e_WriteLog('Unable to create GL window: ' + SDL_GetError(), TMsgType.Fatal);
     exit;
   end;