DEADSOFTWARE

Added touchscreen controls
[d2df-sdl.git] / src / game / g_window.pas
index 56df2d169d730e2526a2d373328f85436675c0b1..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;
@@ -153,6 +165,7 @@ begin
 
   SDL_GL_MakeCurrent(h_Wnd, h_GL);
   SDL_ShowCursor(SDL_DISABLE);
+  if (h_GL <> nil) then g_SetVSync(gVSync);
   if (gFullscreen) then
   begin
     nw := 0;
@@ -459,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
@@ -485,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;
@@ -522,8 +540,17 @@ begin
   if (h_GL = nil) then exit;
   fuiScrWdt := gScreenWidth;
   fuiScrHgt := gScreenHeight;
+  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();
-  g_SetVSync(gVSync);
+  if (h_GL <> nil) then g_SetVSync(gVSync);
 {$ENDIF}
 
   e_ResizeWindow(gScreenWidth, gScreenHeight);
@@ -774,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;
 
@@ -935,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;