ecere/gfx/drivers/LFB; gui/EditBox: Splitting Font Rendering & Management; Italic...
[sdk] / ecere / src / gfx / drivers / Direct3D9DisplayDriver.ec
index 1ad34c4..0be479f 100644 (file)
@@ -4,6 +4,10 @@ import "instance"
 
 #if defined(__WIN32__)
 
+#ifndef D3DPRESENT_DONOTWAIT
+#define D3DPRESENT_DONOTWAIT 0x00000001L
+#endif
+
 #define D3D_DEBUG_INFO  1
 
 #define Method _Method
@@ -657,8 +661,8 @@ class Direct3D9DisplayDriver : DisplayDriver
 
    void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
    {
-      if(bitmap.picture)
-         IDirect3DTexture9_Release((IDirect3DTexture9 *)bitmap.picture);
+      if(bitmap.driverData)
+         IDirect3DTexture9_Release((IDirect3DTexture9 *)bitmap.driverData);
    }
 
    bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
@@ -732,7 +736,7 @@ class Direct3D9DisplayDriver : DisplayDriver
 
             bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
             bitmap.driver = displaySystem.driver;
-            bitmap.picture = (void *)texture;
+            bitmap.driverData = (void *)texture;
 
             if(!result)
                FreeBitmap(displaySystem, bitmap);
@@ -925,7 +929,7 @@ class Direct3D9DisplayDriver : DisplayDriver
             (float)(sx+w) / (src.width-1), (float)(sy+h)/ (src.height-1) }
       };
 
-      IDirect3DDevice9_SetTexture(d3dSystem.d3dDevice, 0, (IDirect3DBaseTexture9 *)src.picture);
+      IDirect3DDevice9_SetTexture(d3dSystem.d3dDevice, 0, (IDirect3DBaseTexture9 *)src.driverData);
       IDirect3DDevice9_DrawPrimitiveUP(d3dSystem.d3dDevice, D3DPT_TRIANGLESTRIP, 2,
          vertex, sizeof(Vertex));
       IDirect3DDevice9_SetTexture(d3dSystem.d3dDevice, 0, null);
@@ -947,7 +951,7 @@ class Direct3D9DisplayDriver : DisplayDriver
             (float)(sx+sw) / (src.width-1), (float)(sy+sh)/ (src.height-1) }
       };
 
-      IDirect3DDevice9_SetTexture(d3dSystem.d3dDevice, 0, (IDirect3DBaseTexture9 *)src.picture);
+      IDirect3DDevice9_SetTexture(d3dSystem.d3dDevice, 0, (IDirect3DBaseTexture9 *)src.driverData);
       IDirect3DDevice9_DrawPrimitiveUP(d3dSystem.d3dDevice, D3DPT_TRIANGLESTRIP, 2,
          vertex, sizeof(Vertex));
       IDirect3DDevice9_SetTexture(d3dSystem.d3dDevice, 0, null);
@@ -1010,12 +1014,12 @@ class Direct3D9DisplayDriver : DisplayDriver
       d3dSurface.opaqueText = opaque;
    }
 
-   void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
+   void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * adv)
    {
-      ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
+      ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height, prevGlyph, rPrevGlyph, adv);
    }
 
-   void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
+   void WriteText(Display display, Surface surface, int x, int y, const char * text, int len, int prevGlyph, int * rPrevGlyph)
    {
       DisplaySystem displaySystem = display.displaySystem;
       D3DSystem d3dSystem = displaySystem.driverData;
@@ -1023,8 +1027,9 @@ class Direct3D9DisplayDriver : DisplayDriver
 
       if(surface.textOpacity)
       {
-         int w, h;
-         FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
+         int w, h, adv;
+         FontExtent(display.displaySystem, surface.font, text, len, &w, &h, prevGlyph, rPrevGlyph, &adv);
+         w += adv;
          {
             int x1 = x, y1 = y, x2 = x+w-1, y2 = y+h-1;
             Vertex vertex[4] =
@@ -1046,7 +1051,7 @@ class Direct3D9DisplayDriver : DisplayDriver
       IDirect3DDevice9_SetSamplerState(d3dSystem.d3dDevice, 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
       d3dSurface.writingText = true;
 
-      ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
+      ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len, prevGlyph, rPrevGlyph);
 
       d3dSurface.writingText = false;
       IDirect3DDevice9_SetSamplerState(d3dSystem.d3dDevice, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
@@ -1054,9 +1059,9 @@ class Direct3D9DisplayDriver : DisplayDriver
       IDirect3DDevice9_SetSamplerState(d3dSystem.d3dDevice, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    }
 
-   void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
+   void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * adv)
    {
-      ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextExtent(display, surface, text, len, width, height);
+      ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextExtent(display, surface, text, len, width, height, prevGlyph, rPrevGlyph, adv);
    }
 
    void DrawingChar(Display display, Surface surface, char character)
@@ -1279,7 +1284,7 @@ class Direct3D9DisplayDriver : DisplayDriver
       {
          Bitmap map = material.baseMap;
 
-         IDirect3DDevice9_SetTexture(d3dDevice, 0, (IDirect3DBaseTexture9 *)map.picture);
+         IDirect3DDevice9_SetTexture(d3dDevice, 0, (IDirect3DBaseTexture9 *)map.driverData);
 
          if(material.flags.tile)
          {
@@ -1373,7 +1378,7 @@ class Direct3D9DisplayDriver : DisplayDriver
       }
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       D3DSystem d3dSystem = displaySystem.driverData;
       bool result = false;
@@ -1385,26 +1390,76 @@ class Direct3D9DisplayDriver : DisplayDriver
       {
          D3DMesh d3dMesh = mesh.data;
          result = true;
-         if((mesh.flags.vertices) && !d3dMesh.vertices)
-         {
-            mesh.vertices = new Vector3Df[mesh.nVertices];
-            if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices, null))
-               result = false;
-         }
-         if((mesh.flags.normals) && !d3dMesh.normals)
+         if(mesh.nVertices == nVertices)
          {
-            mesh.normals = new Vector3Df[mesh.nVertices];
-            if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals, null))
-               result = false;
+            if(mesh.flags != flags)
+            {
+               // Same number of vertices, adding features (Leaves the other features pointers alone)
+               if(flags.vertices && !d3dMesh.vertices)
+               {
+                  mesh.vertices = new Vector3Df[nVertices];
+                  if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices, null))
+                     result = false;
+               }
+               if(flags.normals && !d3dMesh.normals)
+               {
+                  mesh.normals = new Vector3Df[nVertices];
+                  if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals, null))
+                     result = false;
+               }
+               if(flags.texCoords1 && !d3dMesh.texCoords)
+               {
+                  mesh.texCoords = new Pointf[nVertices];
+                  if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords, null))
+                     result = false;
+               }
+            }
          }
-         if((mesh.flags.texCoords1) && !d3dMesh.texCoords)
+         else
          {
-            mesh.texCoords = new Pointf[mesh.nVertices];
-            if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords, null))
-               result = false;
+            // New number of vertices, reallocate all current and new features
+            flags |= mesh.flags;
+
+            // Same number of vertices, adding features (Leaves the other features pointers alone)
+            if(flags.vertices)
+            {
+               if(d3dMesh.vertices)
+               {
+                  IDirect3DVertexBuffer9_Release(d3dMesh.vertices);
+                  d3dMesh.vertices = null;
+               }
+               mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
+               if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices, null))
+                  result = false;
+            }
+            if(flags.normals)
+            {
+               if(d3dMesh.normals)
+               {
+                  IDirect3DVertexBuffer9_Release(d3dMesh.normals);
+                  d3dMesh.normals = null;
+               }
+               mesh.normals = renew mesh.normals Vector3Df[nVertices];
+               if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals, null))
+                  result = false;
+            }
+            if(flags.texCoords1)
+            {
+               if(d3dMesh.texCoords)
+               {
+                  IDirect3DVertexBuffer9_Release(d3dMesh.texCoords);
+                  d3dMesh.texCoords = null;
+               }
+               mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+               if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords, null))
+                  result = false;
+            }
          }
       }
       return result;