DEADSOFTWARE

gl: implement texture filtering
[d2df-sdl.git] / src / game / renders / opengl / r_draw.pas
index c2bfc245726f040e249c9455835317666c7726a3..4f28d505c7da6d1f8e01db2c2fe4dbe2fe2c7cd6 100644 (file)
@@ -18,32 +18,41 @@ unit r_draw;
 interface
 
   uses
-    g_textures,
+    g_animations,
     r_textures
   ;
 
+  procedure r_Draw_SetFilter (img: TGLTexture; enable: Boolean);
+
   procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
   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_Line (x0, y0, x1, y1: 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 (sw, sh, gw, gh: Integer);
+  procedure r_Draw_SetRect (l, t, r, b: Integer);
+  procedure r_Draw_GetRect (out l, t, r, b: Integer);
+
+  procedure r_Draw_EnableTexture2D (enable: Boolean);
+  procedure r_Draw_SetColor (r, g, b, a: Byte);
 
 implementation
 
   uses
-    {$IFDEF USE_GLES1}
-      GLES11,
-    {$ELSE}
-      GL, GLEXT,
-    {$ENDIF}
+    {$I ../../../nogl/noGLuses.inc}
     SysUtils, Classes, Math,
-    e_log, utils,
-    g_game // gScreenWidth, gScreenHeight
+    e_log, utils
   ;
 
   const
@@ -52,15 +61,52 @@ implementation
     NTB = $00;
     NTA = $FF;
 
-  procedure SetupMatrix;
+  var
+    sl, st, sr, sb: Integer;
+    ScreenWidth, ScreenHeight: Integer;
+    GameWidth, GameHeight: Integer;
+
+    enableTexture2D: Boolean;
+    curR, curG, curB, curA: Byte;
+
+  procedure r_Draw_EnableTexture2D (enable: Boolean);
+  begin
+    if enable <> enableTexture2D then
+    begin
+      if enable then glEnable(GL_TEXTURE_2D) else glDisable(GL_TEXTURE_2D);
+      enableTexture2D := enable;
+    end;
+  end;
+
+  procedure r_Draw_SetColor (r, g, b, a: Byte);
+  begin
+    if (r <> curR) or (g <> curG) or (b <> curB) or (curA <> a) then
+    begin
+      glColor4ub(r, g, b, a);
+      curR := r;
+      curG := g;
+      curB := b;
+      curA := a;
+    end;
+  end;
+
+  procedure r_Draw_Setup (sw, sh, gw, gh: Integer);
   begin
-    glScissor(0, 0, gScreenWidth, gScreenHeight);
-    glViewport(0, 0, gScreenWidth, gScreenHeight);
+    ASSERT((sw >= 0) and (sh >= 0)); // screen/window size
+    ASSERT((gw >= 0) and (gh >= 0)); // virtual screen size
+    ScreenWidth := sw;
+    ScreenHeight := sh;
+    GameWidth := gw;
+    GameHeight := gh;
+    glScissor(0, 0, sw, sh);
+    glViewport(0, 0, sw, sh);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity;
-    glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
+    glOrtho(0, gw, gh, 0, 0, 1);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity;
+    glEnable(GL_SCISSOR_TEST);
+    r_Draw_SetRect(0, 0, gw - 1, gh - 1);
   end;
 
   procedure DrawQuad (x, y, w, h: Integer);
@@ -73,16 +119,30 @@ implementation
     glEnd();
   end;
 
-  procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
+  procedure DrawTextureError (x, y, w, h: Integer);
+    var w2, h2: Integer;
+  begin
+    w2 := w div 2; h2 := h div 2;
+    r_Draw_FillRect(x,      y,      x + w2, y + h2, 255, 0,   255, 255);
+    r_Draw_FillRect(x + w2, y,      x + w,  y + h2, 255, 255, 255, 255);
+    r_Draw_FillRect(x + w2, y + h2, x + w,  y + h,  255, 0,   255, 255);
+    r_Draw_FillRect(x,      y + h2, x + w2, y + h,  255, 255, 255, 255);
+    if (w > 2) and (h > 2) then
+      r_Draw_Rect(x, y, x + w, y + h, 0, 255, 0, 255);
+  end;
+
+  procedure r_Draw_SetFilter (img: TGLTexture; enable: Boolean);
+  begin
+    ASSERT(img <> nil);
+    img.filter := enable;
+  end;
+
+  procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend, filter: Boolean);
     var nw, nh, ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
   begin
     if tile = nil then
     begin
-      glColor4ub(rr, gg, bb, aa);
-      if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-      glDisable(GL_TEXTURE_2D);
-      glEnable(GL_BLEND);
-      DrawQuad(x, y, w, h);
+      DrawTextureError(x, y, w, h);
     end
     else
     begin
@@ -93,10 +153,28 @@ implementation
       ay := (tile.t) / nw;
       by := (tile.b + 1) / nh;
       l := x; t := y; r := x + w; b := y + h;
-      glBindTexture(GL_TEXTURE_2D, tile.id);
-      glColor4ub(rr, gg, bb, aa);
+      r_Textures_GL_Bind(tile.id);
+      if filter <> tile.base.filter then
+      begin
+        if filter then
+        begin
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        end
+        else
+        begin
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+        end;
+        tile.base.filter := filter;
+      end;
+      r_Draw_SetColor(rr, gg, bb, aa);
+      r_Draw_EnableTexture2D(true);
       if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-      glEnable(GL_TEXTURE_2D);
       glEnable(GL_BLEND);
       glBegin(GL_QUADS);
         glTexCoord2f(ax, ay); glVertex2i(r, t);
@@ -104,51 +182,89 @@ implementation
         glTexCoord2f(bx, by); glVertex2i(l, b);
         glTexCoord2f(ax, by); glVertex2i(r, b);
       glEnd();
-      glDisable(GL_TEXTURE_2D);
-      glBindTexture(GL_TEXTURE_2D, 0);
     end
   end;
 
+  procedure DrawHWTexture (gltex: GLint; nw, nh, x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
+    var ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
+  begin
+    ax := IfThen(flip, 0, w) / nw;
+    bx := IfThen(flip, w, 0) / nh;
+    ay := 0 / nw;
+    by := h / nh;
+    l := x; t := y; r := x + w; b := y + h;
+    r_Textures_GL_Bind(gltex);
+    r_Draw_SetColor(rr, gg, bb, aa);
+    r_Draw_EnableTexture2D(true);
+    if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    glEnable(GL_BLEND);
+    glBegin(GL_QUADS);
+      glTexCoord2f(ax, ay); glVertex2i(r, t);
+      glTexCoord2f(bx, ay); glVertex2i(l, t);
+      glTexCoord2f(bx, by); glVertex2i(l, b);
+      glTexCoord2f(ax, by); glVertex2i(r, b);
+    glEnd();
+  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);
     if img = nil then
-      DrawTile(nil, x, y, w, h, flip, NTR, NTB, NTG, NTA, blend)
+      DrawTextureError(x, y, w, h)
     else
     begin
-      offx := 0;
-      offy := 0;
+      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);
       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);
-          glPushMatrix;
-          glTranslatef(x + offx, y + offy, 0);
-          glScalef(w / img.width, h / img.height, 1);
-          DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend);
-          glPopMatrix;
-          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, img.filter);
+          glTranslatef(n.width, 0, 0);
+          i := i + step;
+        until i = last;
+        glTranslatef(-img.width, n.height, 0);
       end;
+      glPopMatrix;
     end
   end;
 
+  function r_Draw_IsHWRepeatable (img: TGLTexture): Boolean;
+    var n: TGLAtlasNode; a: TGLAtlas;
+  begin
+    ASSERT(img <> nil);
+    result := false;
+    if (img.cols = 1) and (img.lines = 1) then
+    begin
+      n := img.GetTile(0, 0);
+      if (n.width = img.width) and (n.height = img.height) then
+      begin
+        a := n.base;
+        result := (a.GetWidth() = img.width) and (a.GetHeight() = img.height)
+      end;
+    end;
+  end;
+
   procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
     var i, j: Integer;
   begin
     ASSERT(w >= 0);
     ASSERT(h >= 0);
     if img = nil then
-      r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
+      DrawTextureError(x, y, w, h)
+    else if r_Draw_IsHWRepeatable(img) then
+      DrawHWTexture(img.GetTile(0, 0).base.id, img.width, img.height, x, y, w, h, flip, r, g, b, a, 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;
 
@@ -169,28 +285,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)
+      DrawTextureError(x, y, w, h)
     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);
@@ -200,59 +311,163 @@ 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);
+  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_ZERO, GL_SRC_COLOR);
-    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;
+    ASSERT(l <= r);
+    ASSERT(t <= b);
+    if (l < r) and (t < b) then
+    begin
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      r_Draw_EnableTexture2D(false);
+      r_Draw_SetColor(rr, gg, bb, aa);
+      glBegin(GL_QUADS);
+        (* top *)
+        glVertex2i(l, t);
+        glVertex2i(r, t);
+        glVertex2i(r, t+1);
+        glVertex2i(l, t+1);
+        (* bottom *)
+        glVertex2i(l, b-1);
+        glVertex2i(r, b-1);
+        glVertex2i(r, b);
+        glVertex2i(l, b);
+        (* left *)
+        glVertex2i(l,   t+1);
+        glVertex2i(l+1, t+1);
+        glVertex2i(l+1, b-1);
+        glVertex2i(l,   b-1);
+        (* right *)
+        glVertex2i(r-1, t+1);
+        glVertex2i(r,   t+1);
+        glVertex2i(r,   b-1);
+        glVertex2i(r-1, b-1);
+      glEnd;
+    end;
   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);
-    glEnd;
+    ASSERT(l <= r);
+    ASSERT(t <= b);
+    if (l < r) and (t < b) then
+    begin
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      r_Draw_EnableTexture2D(false);
+      r_Draw_SetColor(rr, gg, bb, aa);
+      glBegin(GL_QUADS);
+        glVertex2i(l, t);
+        glVertex2i(r, t);
+        glVertex2i(r, b);
+        glVertex2i(l, b);
+      glEnd;
+    end;
+  end;
+
+  procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
+  begin
+    ASSERT(l <= r);
+    ASSERT(t <= b);
+    if (l < r) and (t < b) then
+    begin
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_ZERO, GL_SRC_COLOR);
+      r_Draw_EnableTexture2D(false);
+      r_Draw_SetColor(rr, gg, bb, aa);
+      glBegin(GL_QUADS);
+        glVertex2i(l, t);
+        glVertex2i(r, t);
+        glVertex2i(r, b);
+        glVertex2i(l, b);
+      glEnd;
+    end;
   end;
 
   procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
   begin
-    ASSERT(r >= l);
-    ASSERT(b >= t);
+    ASSERT(l <= r);
+    ASSERT(t <= b);
+    if (l < r) and (t < b) then
+    begin
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
+      r_Draw_EnableTexture2D(false);
+      r_Draw_SetColor(rr, gg, bb, aa);
+      glBegin(GL_QUADS);
+        glVertex2i(l, t);
+        glVertex2i(r, t);
+        glVertex2i(r, b);
+        glVertex2i(l, b);
+      glEnd;
+    end;
+  end;
+
+  procedure r_Draw_Line (x0, y0, x1, y1: Integer; rr, gg, bb, aa: Byte);
+  begin
     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);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    r_Draw_EnableTexture2D(false);
+    r_Draw_SetColor(rr, gg, bb, aa);
+    glLineWidth(1);
+    glBegin(GL_LINES);
+      glVertex2i(x0, y0);
+      glVertex2i(x1, y1);
     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 x, y, w, h: Integer;
+  begin
+    ASSERT(l <= r);
+    ASSERT(t <= b);
+    x := l * ScreenWidth div GameWidth;
+    y := t * ScreenHeight div GameHeight;
+    w := (r - l + 1) * ScreenWidth div GameWidth;
+    h := (b - t + 1) * ScreenHeight div GameHeight;
+    glScissor(x, ScreenHeight - h - y, 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.