DEADSOFTWARE

Added support OpenGL ES 1.1 through nanoGL (have some bugs) and fix build for ARM
[d2df-sdl.git] / src / flexui / fui_gfx_gl.pas
index 476d74909df582c9eecd5b07672e6a646b83bbd2..67ad97cf3ffe6f94424964d2fbc0e2fc14584ec9 100644 (file)
@@ -21,8 +21,13 @@ unit fui_gfx_gl;
 interface
 
 uses
+{$IFDEF USE_NANOGL}
+  nanoGL,
+{$ELSE}
+  GL, GLExt,
+{$ENDIF}
   SysUtils, Classes,
-  GL, GLExt, SDL2,
+  SDL2,
   sdlcarcass,
   fui_common, fui_events;
 
@@ -115,6 +120,14 @@ type
 
     function combineClip (constref aclip: TGxRect): TGxRect; // returns previous clip
 
+    // vertical scrollbar
+    procedure drawVSBar (x, y, wdt, hgt: Integer; cur, min, max: Integer; constref clrfull, clrempty: TGxRGBA);
+    // horizontal scrollbar
+    procedure drawHSBar (x, y, wdt, hgt: Integer; cur, min, max: Integer; constref clrfull, clrempty: TGxRGBA);
+
+    class function sbarFilled (wh: Integer; cur, min, max: Integer): Integer;
+    class function sbarPos (cxy: Integer; xy, wh: Integer; min, max: Integer): Integer;
+
   public //HACK!
     procedure glSetScale (ascale: Single);
     procedure glSetTrans (ax, ay: Single);
@@ -180,7 +193,9 @@ function isScaled (): Boolean;
 var
   mt: packed array [0..15] of Double;
 begin
+{$IFNDEF USE_NANOGL}
   glGetDoublev(GL_MODELVIEW_MATRIX, @mt[0]);
+{$ENDIF}
   result := (mt[0] <> 1.0) or (mt[1*4+1] <> 1.0);
 end;
 
@@ -222,14 +237,18 @@ begin
   glMatrixMode(GL_MODELVIEW); glPushMatrix();
   glMatrixMode(GL_TEXTURE); glPushMatrix();
   glMatrixMode(GL_COLOR); glPushMatrix();
+{$IFNDEF USE_NANOGL}
   glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS); // let's play safe
+{$ENDIF}
   saved := true;
 end;
 
 procedure TSavedGLState.restore ();
 begin
   if (not saved) then raise Exception.Create('cannot restore unsaved OpenGL state');
+{$IFNDEF USE_NANOGL}
   glPopAttrib({GL_ENABLE_BIT});
+{$ENDIF}
   glMatrixMode(GL_PROJECTION); glPopMatrix();
   glMatrixMode(GL_MODELVIEW); glPopMatrix();
   glMatrixMode(GL_TEXTURE); glPopMatrix();
@@ -278,7 +297,9 @@ begin
     else
     begin
       // assume uniform scale
+{$IFNDEF USE_NANOGL}
       glGetDoublev(GL_MODELVIEW_MATRIX, @mt[0]);
+{$ENDIF}
       ctx.mScaled := (mt[0] <> 1.0) or (mt[1*4+1] <> 1.0);
       ctx.mScale := mt[0];
       oglSetup2DState();
@@ -312,7 +333,11 @@ type
 
 procedure TScissorSave.save (enableScissoring: Boolean);
 begin
+{$IFDEF USE_NANOGL}
+  wassc := false; // FIXIT
+{$ELSE}
   wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
+{$ENDIF}
   if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
   //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
   if enableScissoring and (not wassc) then glEnable(GL_SCISSOR_TEST);
@@ -1312,6 +1337,61 @@ begin
 end;
 
 
+// vertical scroll bar
+procedure TGxContext.drawVSBar (x, y, wdt, hgt: Integer; cur, min, max: Integer; constref clrfull, clrempty: TGxRGBA);
+var
+  filled: Integer;
+begin
+  if (wdt < 1) or (hgt < 1) then exit;
+  filled := sbarFilled(hgt, cur, min, max);
+  color := clrfull;
+  fillRect(x, y, wdt, filled);
+  color := clrempty;
+  fillRect(x, y+filled, wdt, hgt-filled);
+end;
+
+
+// horizontal scrollbar
+procedure TGxContext.drawHSBar (x, y, wdt, hgt: Integer; cur, min, max: Integer; constref clrfull, clrempty: TGxRGBA);
+var
+  filled: Integer;
+begin
+  if (wdt < 1) or (hgt < 1) then exit;
+  filled := sbarFilled(wdt, cur, min, max);
+  color := clrfull;
+  fillRect(x, y, filled, hgt);
+  color := clrempty;
+  fillRect(x+filled, y, wdt-filled, hgt);
+end;
+
+
+class function TGxContext.sbarFilled (wh: Integer; cur, min, max: Integer): Integer;
+begin
+       if (wh < 1) then result := 0
+  else if (min > max) then result := 0
+  else if (min = max) then result := wh
+  else
+  begin
+    if (cur < min) then cur := min else if (cur > max) then cur := max;
+    result := wh*(cur-min) div (max-min);
+  end;
+end;
+
+
+class function TGxContext.sbarPos (cxy: Integer; xy, wh: Integer; min, max: Integer): Integer;
+begin
+  if (wh < 1) then begin result := 0; exit; end;
+  if (min > max) then begin result := 0; exit; end;
+  if (min = max) then begin result := max; exit; end;
+  if (cxy < xy) then begin result := min; exit; end;
+  if (cxy >= xy+wh) then begin result := max; exit; end;
+  result := min+((max-min)*(cxy-xy) div wh);
+  assert((result >= min) and (result <= max));
+end;
+
+
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 (*
 procedure oglRestoreMode (doClear: Boolean);
@@ -1347,8 +1427,8 @@ end;
 *)
 
 
-//procedure onWinFocus (); begin end;
-//procedure onWinBlur (); begin fuiResetKMState(true); end;
+//procedure onWinFocus (); begin uiFocus(); end;
+//procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
 
 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
 procedure onPostRender (); begin oglDrawCursor(); end;