DEADSOFTWARE

gl: fix big texture flip on hardware with texture size limits
[d2df-sdl.git] / src / game / renders / opengl / r_draw.pas
index e836b30d1190e7923f536587de66c616106fd3e0..9db91836645d5460279debd7a4937a1ab3471bb9 100644 (file)
@@ -18,7 +18,7 @@ unit r_draw;
 interface
 
   uses
-    g_textures,
+    g_animations,
     r_textures
   ;
 
@@ -26,10 +26,20 @@ interface
   procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
   procedure r_Draw_TextureRepeatRotate (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
 
-  procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
-  procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
+  procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
+  procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
 
   procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  procedure r_Draw_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+
+  procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
+  procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
+
+  procedure r_Draw_Setup (w, h: Integer);
+  procedure r_Draw_SetRect (l, t, r, b: Integer);
+  procedure r_Draw_GetRect (out l, t, r, b: Integer);
 
 implementation
 
@@ -40,8 +50,7 @@ implementation
       GL, GLEXT,
     {$ENDIF}
     SysUtils, Classes, Math,
-    e_log, utils,
-    g_game // gScreenWidth, gScreenHeight
+    e_log, utils
   ;
 
   const
@@ -50,15 +59,25 @@ implementation
     NTB = $00;
     NTA = $FF;
 
-  procedure SetupMatrix;
+  var
+    sl, st, sr, sb: Integer;
+    ScreenWidth, ScreenHeight: Integer;
+
+  procedure r_Draw_Setup (w, h: Integer);
   begin
-    glScissor(0, 0, gScreenWidth, gScreenHeight);
-    glViewport(0, 0, gScreenWidth, gScreenHeight);
+    ASSERT(w >= 0);
+    ASSERT(h >= 0);
+    ScreenWidth := w;
+    ScreenHeight := h;
+    glScissor(0, 0, w, h);
+    glViewport(0, 0, w, h);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity;
-    glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
+    glOrtho(0, w, h, 0, 0, 1);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity;
+    glEnable(GL_SCISSOR_TEST);
+    r_Draw_SetRect(0, 0, w - 1, h - 1);
   end;
 
   procedure DrawQuad (x, y, w, h: Integer);
@@ -108,7 +127,7 @@ implementation
   end;
 
   procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
-    var i, j, offx, offy: Integer; n: TGLAtlasNode;
+    var i, j, first, last, step: Integer; n: TGLAtlasNode;
   begin
     ASSERT(w >= 0);
     ASSERT(h >= 0);
@@ -116,21 +135,23 @@ implementation
       DrawTile(nil, x, y, w, h, flip, NTR, NTB, NTG, NTA, blend)
     else
     begin
+      if flip then first := img.cols - 1 else first := 0;
+      if flip then last  := -1           else last  := img.cols;
+      if flip then step  := -1           else step  := +1;
       glPushMatrix;
+      glTranslatef(x, y, 0);
       glScalef(w / img.width, h / img.height, 1);
-      offx := 0;
-      offy := 0;
       for j := 0 to img.lines - 1 do
       begin
-        for i := 0 to img.cols - 1 do
-        begin
+        i := first;
+        repeat
           n := img.GetTile(i, j);
           ASSERT(n <> nil);
-          DrawTile(n, x + offx, y + offy, n.width, n.height, flip, r, g, b, a, blend);
-          offx := offx + n.width;
-        end;
-        offx := 0;
-        offy := offy + n.height;
+          DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend);
+          glTranslatef(n.width, 0, 0);
+          i := i + step;
+        until i = last;
+        glTranslatef(-img.width, n.height, 0);
       end;
       glPopMatrix;
     end
@@ -144,8 +165,8 @@ implementation
     if img = nil then
       r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
     else
-      for j := 0 to h div img.height - 1 do
-        for i := 0 to w div img.width - 1 do
+      for j := 0 to (h - 1) div img.height do
+        for i := 0 to (w - 1) div img.width do
           r_Draw_Texture(img, x + i * img.width, y + j * img.height, img.width, img.height, flip, r, g, b, a, blend);
   end;
 
@@ -166,28 +187,23 @@ implementation
       r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
   end;
 
-  procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
-    var img: TGLTexture; cur, total, i: Integer;
+  procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
+    var img: TGLTexture; frame: LongInt;
   begin
     ASSERT(anim.IsValid());
     if m = nil then
       r_Draw_TextureRepeat(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
     else
     begin
-      if m.BackAnim then
-      begin
-        total := m.count * 2 - 1;
-        cur := anim.CurrentFrame mod total;
-        if cur < m.count then i := cur else i := total - cur - 1;
-      end
-      else
-        i := anim.CurrentFrame mod m.count;
-      img := m.GetTexture(i);
+      g_Anim_GetFrameFromState(anim, backanim, frame);
+      ASSERT(frame >= 0);
+      ASSERT(frame < m.count);
+      img := m.GetTexture(frame);
       r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
     end
   end;
 
-  procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
+  procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
   begin
     ASSERT(w >= 0);
     ASSERT(h >= 0);
@@ -197,11 +213,11 @@ implementation
       glTranslatef(x + rx, y + ry, 0);
       glRotatef(angle, 0, 0, 1);
       glTranslatef(-(x + rx), -(y + ry), 0);
-      r_Draw_MultiTextureRepeat(m, anim, x, y, w, h, flip, r, g, b, a, blend);
+      r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
       glPopMatrix;
     end
     else
-      r_Draw_MultiTextureRepeat(m, anim, x, y, w, h, flip, r, g, b, a, blend);
+      r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
   end;
 
   procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
@@ -220,4 +236,115 @@ implementation
     glEnd;
   end;
 
+  procedure r_Draw_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  begin
+    ASSERT(r >= l);
+    ASSERT(b >= t);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    glDisable(GL_TEXTURE_2D);
+    glColor4ub(rr, gg, bb, aa);
+    glBegin(GL_LINE_LOOP);
+{
+      glVertex2i(l, t);
+      glVertex2i(r, t);
+      glVertex2i(r, b);
+      glVertex2i(l, b);
+}
+      glVertex2f(l + 0.5, t + 0.5);
+      glVertex2f(r - 0.5, t + 0.5);
+      glVertex2f(r - 0.5, b - 0.5);
+      glVertex2f(l + 0.5, b - 0.5);
+    glEnd;
+  end;
+
+  procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  begin
+    ASSERT(r >= l);
+    ASSERT(b >= t);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    glDisable(GL_TEXTURE_2D);
+    glColor4ub(rr, gg, bb, aa);
+    glBegin(GL_QUADS);
+{
+      glVertex2i(l, t);
+      glVertex2i(r, t);
+      glVertex2i(r, b);
+      glVertex2i(l, b);
+}
+{
+      glVertex2f(l + 0.5, t + 0.5);
+      glVertex2f(r - 0.5, t + 0.5);
+      glVertex2f(r - 0.5, b - 0.5);
+      glVertex2f(l + 0.5, b - 0.5);
+}
+      glVertex2f(l + 0, t + 0);
+      glVertex2f(r + 0.75, t + 0);
+      glVertex2f(r + 0.75, b + 0.75);
+      glVertex2f(l + 0, b + 0.75);
+    glEnd;
+  end;
+
+  procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  begin
+    ASSERT(r >= l);
+    ASSERT(b >= t);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
+    glDisable(GL_TEXTURE_2D);
+    glColor4ub(rr, gg, bb, aa);
+    glBegin(GL_QUADS);
+      glVertex2i(l, t);
+      glVertex2i(r, t);
+      glVertex2i(r, b);
+      glVertex2i(l, b);
+    glEnd;
+  end;
+
+  procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
+    var i, xoff, spc: Integer; t: TGLTexture; ch: AnsiChar;
+  begin
+    xoff := x; spc := MAX(0, f.GetSpace());
+    for i := 1 to Length(text) do
+    begin
+      ch := text[i];
+      t := f.GetChar(ch);
+      if t <> nil then
+        r_Draw_Texture(t, xoff, y, t.width, t.height, false, r, g, b, a, false);
+      Inc(xoff, f.GetWidth(ch) + spc);
+    end;
+  end;
+
+  procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
+    var i, spc, len: Integer;
+  begin
+    w := 0;
+    h := f.GetMaxHeight();
+    len := Length(text);
+    if len > 0 then
+    begin
+      spc := MAX(0, f.GetSpace());
+      for i := 1 to len - 1 do
+        Inc(w, f.GetWidth(text[i]) + spc);
+      Inc(w, f.GetWidth(text[len]));
+    end;
+  end;
+
+  procedure r_Draw_SetRect (l, t, r, b: Integer);
+    var w, h: Integer;
+  begin
+    ASSERT(l <= r);
+    ASSERT(t <= b);
+    w := r - l + 1;
+    h := b - t + 1;
+    glScissor(l, ScreenHeight - h - t, w, h);
+    sl := l; st := t; sr := r; sb := b;
+  end;
+
+  procedure r_Draw_GetRect (out l, t, r, b: Integer);
+  begin
+    l := sl; t := st; r := sr; b := sb;
+  end;
+
 end.