diff src/video/win32/SDL_d3drender.c @ 3536:0267b8b1595c

Added interfaces for batch drawing of points, lines and rects: SDL_DrawPoints() SDL_BlendPoints() SDL_BlendLines() SDL_DrawLines() SDL_FillRects() SDL_BlendRects() SDL_RenderPoints() SDL_RenderLines() SDL_RenderRects() Renamed SDL_RenderFill() to SDL_RenderRect()
author Sam Lantinga <slouken@libsdl.org>
date Wed, 09 Dec 2009 15:56:56 +0000
parents 444cb12cadb6
children 686f0e69cd37
line wrap: on
line diff
--- a/src/video/win32/SDL_d3drender.c	Mon Dec 07 10:08:24 2009 +0000
+++ b/src/video/win32/SDL_d3drender.c	Wed Dec 09 15:56:56 2009 +0000
@@ -66,10 +66,12 @@
 static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
 static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
                              int numrects, const SDL_Rect * rects);
-static int D3D_RenderPoint(SDL_Renderer * renderer, int x, int y);
-static int D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
-                          int y2);
-static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
+static int D3D_RenderPoints(SDL_Renderer * renderer, const SDL_Point * points,
+                            int count);
+static int D3D_RenderLines(SDL_Renderer * renderer, const SDL_Point * points,
+                           int count);
+static int D3D_RenderRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
+                           int count);
 static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
                           const SDL_Rect * srcrect, const SDL_Rect * dstrect);
 static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
@@ -407,9 +409,9 @@
     renderer->LockTexture = D3D_LockTexture;
     renderer->UnlockTexture = D3D_UnlockTexture;
     renderer->DirtyTexture = D3D_DirtyTexture;
-    renderer->RenderPoint = D3D_RenderPoint;
-    renderer->RenderLine = D3D_RenderLine;
-    renderer->RenderFill = D3D_RenderFill;
+    renderer->RenderPoints = D3D_RenderPoints;
+    renderer->RenderLines = D3D_RenderLines;
+    renderer->RenderRects = D3D_RenderRects;
     renderer->RenderCopy = D3D_RenderCopy;
     renderer->RenderReadPixels = D3D_RenderReadPixels;
     renderer->RenderWritePixels = D3D_RenderWritePixels;
@@ -927,11 +929,12 @@
 }
 
 static int
-D3D_RenderPoint(SDL_Renderer * renderer, int x, int y)
+D3D_RenderPoints(SDL_Renderer * renderer, const SDL_Point * points, int count)
 {
     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
     DWORD color;
-    Vertex vertices[1];
+    Vertex *vertices;
+    int i;
     HRESULT result;
 
     if (data->beginScene) {
@@ -939,15 +942,52 @@
         data->beginScene = SDL_FALSE;
     }
 
+    D3D_SetBlendMode(data, renderer->blendMode);
+
+    result =
+        IDirect3DDevice9_SetTexture(data->device, 0,
+                                    (IDirect3DBaseTexture9 *) 0);
+    if (FAILED(result)) {
+        D3D_SetError("SetTexture()", result);
+        return -1;
+    }
+
     color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
 
-    vertices[0].x = (float) x;
-    vertices[0].y = (float) y;
-    vertices[0].z = 0.0f;
-    vertices[0].rhw = 1.0f;
-    vertices[0].color = color;
-    vertices[0].u = 0.0f;
-    vertices[0].v = 0.0f;
+    vertices = SDL_stack_alloc(Vertex, count);
+    for (i = 0; i < count; ++i) {
+        vertices[i].x = (float) points[i].x;
+        vertices[i].y = (float) points[i].y;
+        vertices[i].z = 0.0f;
+        vertices[i].rhw = 1.0f;
+        vertices[i].color = color;
+        vertices[i].u = 0.0f;
+        vertices[i].v = 0.0f;
+    }
+    result =
+        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
+                                         vertices, sizeof(*vertices));
+    SDL_stack_free(vertices);
+    if (FAILED(result)) {
+        D3D_SetError("DrawPrimitiveUP()", result);
+        return -1;
+    }
+    return 0;
+}
+
+static int
+D3D_RenderLines(SDL_Renderer * renderer, const SDL_Point * points, int count)
+{
+    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
+    DWORD color;
+    Vertex *vertices;
+    int i;
+    HRESULT result;
+
+    if (data->beginScene) {
+        IDirect3DDevice9_BeginScene(data->device);
+        data->beginScene = SDL_FALSE;
+    }
 
     D3D_SetBlendMode(data, renderer->blendMode);
 
@@ -958,9 +998,23 @@
         D3D_SetError("SetTexture()", result);
         return -1;
     }
+
+    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
+
+    vertices = SDL_stack_alloc(Vertex, count);
+    for (i = 0; i < count; ++i) {
+        vertices[i].x = (float) points[i].x;
+        vertices[i].y = (float) points[i].y;
+        vertices[i].z = 0.0f;
+        vertices[i].rhw = 1.0f;
+        vertices[i].color = color;
+        vertices[i].u = 0.0f;
+        vertices[i].v = 0.0f;
+    }
     result =
-        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1,
+        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count,
                                          vertices, sizeof(*vertices));
+    SDL_stack_free(vertices);
     if (FAILED(result)) {
         D3D_SetError("DrawPrimitiveUP()", result);
         return -1;
@@ -969,11 +1023,13 @@
 }
 
 static int
-D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
+D3D_RenderRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
 {
     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
     DWORD color;
-    Vertex vertices[2];
+    int i;
+    float minx, miny, maxx, maxy;
+    Vertex vertices[4];
     HRESULT result;
 
     if (data->beginScene) {
@@ -981,24 +1037,6 @@
         data->beginScene = SDL_FALSE;
     }
 
-    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
-
-    vertices[0].x = (float) x1;
-    vertices[0].y = (float) y1;
-    vertices[0].z = 0.0f;
-    vertices[0].rhw = 1.0f;
-    vertices[0].color = color;
-    vertices[0].u = 0.0f;
-    vertices[0].v = 0.0f;
-
-    vertices[1].x = (float) x2;
-    vertices[1].y = (float) y2;
-    vertices[1].z = 0.0f;
-    vertices[1].rhw = 1.0f;
-    vertices[1].color = color;
-    vertices[1].u = 0.0f;
-    vertices[1].v = 0.0f;
-
     D3D_SetBlendMode(data, renderer->blendMode);
 
     result =
@@ -1008,84 +1046,56 @@
         D3D_SetError("SetTexture()", result);
         return -1;
     }
-    result =
-        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINELIST, 1,
-                                         vertices, sizeof(*vertices));
-    if (FAILED(result)) {
-        D3D_SetError("DrawPrimitiveUP()", result);
-        return -1;
-    }
-    return 0;
-}
-
-static int
-D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
-{
-    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
-    float minx, miny, maxx, maxy;
-    DWORD color;
-    Vertex vertices[4];
-    HRESULT result;
-
-    if (data->beginScene) {
-        IDirect3DDevice9_BeginScene(data->device);
-        data->beginScene = SDL_FALSE;
-    }
-
-    minx = (float) rect->x;
-    miny = (float) rect->y;
-    maxx = (float) rect->x + rect->w;
-    maxy = (float) rect->y + rect->h;
 
     color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
 
-    vertices[0].x = minx;
-    vertices[0].y = miny;
-    vertices[0].z = 0.0f;
-    vertices[0].rhw = 1.0f;
-    vertices[0].color = color;
-    vertices[0].u = 0.0f;
-    vertices[0].v = 0.0f;
+    for (i = 0; i < count; ++i) {
+        const SDL_Rect *rect = rects[i];
+
+        minx = (float) rect->x;
+        miny = (float) rect->y;
+        maxx = (float) rect->x + rect->w;
+        maxy = (float) rect->y + rect->h;
 
-    vertices[1].x = maxx;
-    vertices[1].y = miny;
-    vertices[1].z = 0.0f;
-    vertices[1].rhw = 1.0f;
-    vertices[1].color = color;
-    vertices[1].u = 0.0f;
-    vertices[1].v = 0.0f;
+        vertices[0].x = minx;
+        vertices[0].y = miny;
+        vertices[0].z = 0.0f;
+        vertices[0].rhw = 1.0f;
+        vertices[0].color = color;
+        vertices[0].u = 0.0f;
+        vertices[0].v = 0.0f;
 
-    vertices[2].x = maxx;
-    vertices[2].y = maxy;
-    vertices[2].z = 0.0f;
-    vertices[2].rhw = 1.0f;
-    vertices[2].color = color;
-    vertices[2].u = 0.0f;
-    vertices[2].v = 0.0f;
+        vertices[1].x = maxx;
+        vertices[1].y = miny;
+        vertices[1].z = 0.0f;
+        vertices[1].rhw = 1.0f;
+        vertices[1].color = color;
+        vertices[1].u = 0.0f;
+        vertices[1].v = 0.0f;
 
-    vertices[3].x = minx;
-    vertices[3].y = maxy;
-    vertices[3].z = 0.0f;
-    vertices[3].rhw = 1.0f;
-    vertices[3].color = color;
-    vertices[3].u = 0.0f;
-    vertices[3].v = 0.0f;
-
-    D3D_SetBlendMode(data, renderer->blendMode);
+        vertices[2].x = maxx;
+        vertices[2].y = maxy;
+        vertices[2].z = 0.0f;
+        vertices[2].rhw = 1.0f;
+        vertices[2].color = color;
+        vertices[2].u = 0.0f;
+        vertices[2].v = 0.0f;
 
-    result =
-        IDirect3DDevice9_SetTexture(data->device, 0,
-                                    (IDirect3DBaseTexture9 *) 0);
-    if (FAILED(result)) {
-        D3D_SetError("SetTexture()", result);
-        return -1;
-    }
-    result =
-        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
-                                         vertices, sizeof(*vertices));
-    if (FAILED(result)) {
-        D3D_SetError("DrawPrimitiveUP()", result);
-        return -1;
+        vertices[3].x = minx;
+        vertices[3].y = maxy;
+        vertices[3].z = 0.0f;
+        vertices[3].rhw = 1.0f;
+        vertices[3].color = color;
+        vertices[3].u = 0.0f;
+        vertices[3].v = 0.0f;
+
+        result =
+            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
+                                             2, vertices, sizeof(*vertices));
+        if (FAILED(result)) {
+            D3D_SetError("DrawPrimitiveUP()", result);
+            return -1;
+        }
     }
     return 0;
 }