comparison src/video/win32/SDL_d3drender.c @ 2932:676754cc1acb

Implemented Direct3D line and point drawing
author Sam Lantinga <slouken@libsdl.org>
date Wed, 31 Dec 2008 06:17:24 +0000
parents cdb01906cb7e
children 73f84b1e25e5
comparison
equal deleted inserted replaced
2931:e705adf6f3dc 2932:676754cc1acb
26 #include "SDL_win32video.h" 26 #include "SDL_win32video.h"
27 27
28 /* Direct3D renderer implementation */ 28 /* Direct3D renderer implementation */
29 29
30 #if 1 /* This takes more memory but you won't lose your texture data */ 30 #if 1 /* This takes more memory but you won't lose your texture data */
31 #define D3DPOOL_SDL D3DPOOL_MANAGED 31 #define D3DPOOL_SDL D3DPOOL_MANAGED
32 #define SDL_MEMORY_POOL_MANAGED 32 #define SDL_MEMORY_POOL_MANAGED
33 #else 33 #else
34 #define D3DPOOL_SDL D3DPOOL_DEFAULT 34 #define D3DPOOL_SDL D3DPOOL_DEFAULT
35 #define SDL_MEMORY_POOL_DEFAULT 35 #define SDL_MEMORY_POOL_DEFAULT
36 #endif 36 #endif
37 37
38 static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags); 38 static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
39 static int D3D_DisplayModeChanged(SDL_Renderer * renderer); 39 static int D3D_DisplayModeChanged(SDL_Renderer * renderer);
60 const SDL_Rect * rect, int markDirty, 60 const SDL_Rect * rect, int markDirty,
61 void **pixels, int *pitch); 61 void **pixels, int *pitch);
62 static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture); 62 static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
63 static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, 63 static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
64 int numrects, const SDL_Rect * rects); 64 int numrects, const SDL_Rect * rects);
65 static int D3D_RenderPoint(SDL_Renderer * renderer, int x, int y);
66 static int D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
67 int y2);
65 static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect); 68 static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
66 static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 69 static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
67 const SDL_Rect * srcrect, const SDL_Rect * dstrect); 70 const SDL_Rect * srcrect, const SDL_Rect * dstrect);
68 static void D3D_RenderPresent(SDL_Renderer * renderer); 71 static void D3D_RenderPresent(SDL_Renderer * renderer);
69 static void D3D_DestroyTexture(SDL_Renderer * renderer, 72 static void D3D_DestroyTexture(SDL_Renderer * renderer,
281 renderer->SetTextureScaleMode = D3D_SetTextureScaleMode; 284 renderer->SetTextureScaleMode = D3D_SetTextureScaleMode;
282 renderer->UpdateTexture = D3D_UpdateTexture; 285 renderer->UpdateTexture = D3D_UpdateTexture;
283 renderer->LockTexture = D3D_LockTexture; 286 renderer->LockTexture = D3D_LockTexture;
284 renderer->UnlockTexture = D3D_UnlockTexture; 287 renderer->UnlockTexture = D3D_UnlockTexture;
285 renderer->DirtyTexture = D3D_DirtyTexture; 288 renderer->DirtyTexture = D3D_DirtyTexture;
289 renderer->RenderPoint = D3D_RenderPoint;
290 renderer->RenderLine = D3D_RenderLine;
286 renderer->RenderFill = D3D_RenderFill; 291 renderer->RenderFill = D3D_RenderFill;
287 renderer->RenderCopy = D3D_RenderCopy; 292 renderer->RenderCopy = D3D_RenderCopy;
288 renderer->RenderPresent = D3D_RenderPresent; 293 renderer->RenderPresent = D3D_RenderPresent;
289 renderer->DestroyTexture = D3D_DestroyTexture; 294 renderer->DestroyTexture = D3D_DestroyTexture;
290 renderer->DestroyRenderer = D3D_DestroyRenderer; 295 renderer->DestroyRenderer = D3D_DestroyRenderer;
704 709
705 IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect); 710 IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
706 } 711 }
707 } 712 }
708 713
709 static int 714 static void
710 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect) 715 D3D_SetBlendMode(D3D_RenderData *data, int blendMode)
711 { 716 {
712 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata; 717 switch (blendMode) {
713 D3DRECT d3drect;
714 HRESULT result;
715
716 if (data->beginScene) {
717 IDirect3DDevice9_BeginScene(data->device);
718 data->beginScene = SDL_FALSE;
719 }
720
721 d3drect.x1 = rect->x;
722 d3drect.x2 = rect->x + rect->w;
723 d3drect.y1 = rect->y;
724 d3drect.y2 = rect->y + rect->h;
725
726 result =
727 IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
728 D3DCOLOR_ARGB(renderer->a,
729 renderer->r,
730 renderer->g,
731 renderer->b), 1.0f, 0);
732 if (FAILED(result)) {
733 D3D_SetError("Clear()", result);
734 return -1;
735 }
736 return 0;
737 }
738
739 static int
740 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
741 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
742 {
743 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
744 D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
745 float minx, miny, maxx, maxy;
746 float minu, maxu, minv, maxv;
747 DWORD color;
748 Vertex vertices[4];
749 HRESULT result;
750
751 if (data->beginScene) {
752 IDirect3DDevice9_BeginScene(data->device);
753 data->beginScene = SDL_FALSE;
754 }
755
756 minx = (float) dstrect->x - 0.5f;
757 miny = (float) dstrect->y - 0.5f;
758 maxx = (float) dstrect->x + dstrect->w - 0.5f;
759 maxy = (float) dstrect->y + dstrect->h - 0.5f;
760
761 minu = (float) srcrect->x / texture->w;
762 maxu = (float) (srcrect->x + srcrect->w) / texture->w;
763 minv = (float) srcrect->y / texture->h;
764 maxv = (float) (srcrect->y + srcrect->h) / texture->h;
765
766 color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
767
768 vertices[0].x = minx;
769 vertices[0].y = miny;
770 vertices[0].z = 0.0f;
771 vertices[0].rhw = 1.0f;
772 vertices[0].color = color;
773 vertices[0].u = minu;
774 vertices[0].v = minv;
775
776 vertices[1].x = maxx;
777 vertices[1].y = miny;
778 vertices[1].z = 0.0f;
779 vertices[1].rhw = 1.0f;
780 vertices[1].color = color;
781 vertices[1].u = maxu;
782 vertices[1].v = minv;
783
784 vertices[2].x = maxx;
785 vertices[2].y = maxy;
786 vertices[2].z = 0.0f;
787 vertices[2].rhw = 1.0f;
788 vertices[2].color = color;
789 vertices[2].u = maxu;
790 vertices[2].v = maxv;
791
792 vertices[3].x = minx;
793 vertices[3].y = maxy;
794 vertices[3].z = 0.0f;
795 vertices[3].rhw = 1.0f;
796 vertices[3].color = color;
797 vertices[3].u = minu;
798 vertices[3].v = maxv;
799
800 switch (texture->blendMode) {
801 case SDL_BLENDMODE_NONE: 718 case SDL_BLENDMODE_NONE:
802 IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, 719 IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
803 FALSE); 720 FALSE);
804 break; 721 break;
805 case SDL_BLENDMODE_MASK: 722 case SDL_BLENDMODE_MASK:
826 D3DBLEND_ZERO); 743 D3DBLEND_ZERO);
827 IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND, 744 IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
828 D3DBLEND_SRCCOLOR); 745 D3DBLEND_SRCCOLOR);
829 break; 746 break;
830 } 747 }
748 }
749
750 static int
751 D3D_RenderPoint(SDL_Renderer * renderer, int x, int y)
752 {
753 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
754 DWORD color;
755 Vertex vertices[1];
756 HRESULT result;
757
758 if (data->beginScene) {
759 IDirect3DDevice9_BeginScene(data->device);
760 data->beginScene = SDL_FALSE;
761 }
762
763 color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
764
765 vertices[0].x = (float) x - 0.5f;
766 vertices[0].y = (float) y - 0.5f;
767 vertices[0].z = 0.0f;
768 vertices[0].rhw = 1.0f;
769 vertices[0].color = color;
770 vertices[0].u = 0.0f;
771 vertices[0].v = 0.0f;
772
773 D3D_SetBlendMode(data, renderer->blendMode);
774
775 result = IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)0);
776 if (FAILED(result)) {
777 D3D_SetError("SetTexture()", result);
778 return -1;
779 }
780 result =
781 IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1,
782 vertices, sizeof(*vertices));
783 if (FAILED(result)) {
784 D3D_SetError("DrawPrimitiveUP()", result);
785 return -1;
786 }
787 return 0;
788 }
789
790 static int
791 D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
792 {
793 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
794 DWORD color;
795 Vertex vertices[2];
796 HRESULT result;
797
798 if (data->beginScene) {
799 IDirect3DDevice9_BeginScene(data->device);
800 data->beginScene = SDL_FALSE;
801 }
802
803 color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
804
805 vertices[0].x = (float) x1 - 0.5f;
806 vertices[0].y = (float) y1 - 0.5f;
807 vertices[0].z = 0.0f;
808 vertices[0].rhw = 1.0f;
809 vertices[0].color = color;
810 vertices[0].u = 0.0f;
811 vertices[0].v = 0.0f;
812
813 vertices[1].x = (float) x2 - 0.5f;
814 vertices[1].y = (float) y2 - 0.5f;
815 vertices[1].z = 0.0f;
816 vertices[1].rhw = 1.0f;
817 vertices[1].color = color;
818 vertices[1].u = 0.0f;
819 vertices[1].v = 0.0f;
820
821 D3D_SetBlendMode(data, renderer->blendMode);
822
823 result = IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)0);
824 if (FAILED(result)) {
825 D3D_SetError("SetTexture()", result);
826 return -1;
827 }
828 result =
829 IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINELIST, 1,
830 vertices, sizeof(*vertices));
831 if (FAILED(result)) {
832 D3D_SetError("DrawPrimitiveUP()", result);
833 return -1;
834 }
835 return 0;
836 }
837
838 static int
839 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
840 {
841 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
842 float minx, miny, maxx, maxy;
843 DWORD color;
844 Vertex vertices[4];
845 HRESULT result;
846
847 if (data->beginScene) {
848 IDirect3DDevice9_BeginScene(data->device);
849 data->beginScene = SDL_FALSE;
850 }
851
852 minx = (float) rect->x - 0.5f;
853 miny = (float) rect->y - 0.5f;
854 maxx = (float) rect->x + rect->w - 0.5f;
855 maxy = (float) rect->y + rect->h - 0.5f;
856
857 color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
858
859 vertices[0].x = minx;
860 vertices[0].y = miny;
861 vertices[0].z = 0.0f;
862 vertices[0].rhw = 1.0f;
863 vertices[0].color = color;
864 vertices[0].u = 0.0f;
865 vertices[0].v = 0.0f;
866
867 vertices[1].x = maxx;
868 vertices[1].y = miny;
869 vertices[1].z = 0.0f;
870 vertices[1].rhw = 1.0f;
871 vertices[1].color = color;
872 vertices[1].u = 0.0f;
873 vertices[1].v = 0.0f;
874
875 vertices[2].x = maxx;
876 vertices[2].y = maxy;
877 vertices[2].z = 0.0f;
878 vertices[2].rhw = 1.0f;
879 vertices[2].color = color;
880 vertices[2].u = 0.0f;
881 vertices[2].v = 0.0f;
882
883 vertices[3].x = minx;
884 vertices[3].y = maxy;
885 vertices[3].z = 0.0f;
886 vertices[3].rhw = 1.0f;
887 vertices[3].color = color;
888 vertices[3].u = 0.0f;
889 vertices[3].v = 0.0f;
890
891 D3D_SetBlendMode(data, renderer->blendMode);
892
893 result = IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)0);
894 if (FAILED(result)) {
895 D3D_SetError("SetTexture()", result);
896 return -1;
897 }
898 result =
899 IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
900 vertices, sizeof(*vertices));
901 if (FAILED(result)) {
902 D3D_SetError("DrawPrimitiveUP()", result);
903 return -1;
904 }
905 return 0;
906 }
907
908 static int
909 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
910 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
911 {
912 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
913 D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
914 float minx, miny, maxx, maxy;
915 float minu, maxu, minv, maxv;
916 DWORD color;
917 Vertex vertices[4];
918 HRESULT result;
919
920 if (data->beginScene) {
921 IDirect3DDevice9_BeginScene(data->device);
922 data->beginScene = SDL_FALSE;
923 }
924
925 minx = (float) dstrect->x - 0.5f;
926 miny = (float) dstrect->y - 0.5f;
927 maxx = (float) dstrect->x + dstrect->w - 0.5f;
928 maxy = (float) dstrect->y + dstrect->h - 0.5f;
929
930 minu = (float) srcrect->x / texture->w;
931 maxu = (float) (srcrect->x + srcrect->w) / texture->w;
932 minv = (float) srcrect->y / texture->h;
933 maxv = (float) (srcrect->y + srcrect->h) / texture->h;
934
935 color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
936
937 vertices[0].x = minx;
938 vertices[0].y = miny;
939 vertices[0].z = 0.0f;
940 vertices[0].rhw = 1.0f;
941 vertices[0].color = color;
942 vertices[0].u = minu;
943 vertices[0].v = minv;
944
945 vertices[1].x = maxx;
946 vertices[1].y = miny;
947 vertices[1].z = 0.0f;
948 vertices[1].rhw = 1.0f;
949 vertices[1].color = color;
950 vertices[1].u = maxu;
951 vertices[1].v = minv;
952
953 vertices[2].x = maxx;
954 vertices[2].y = maxy;
955 vertices[2].z = 0.0f;
956 vertices[2].rhw = 1.0f;
957 vertices[2].color = color;
958 vertices[2].u = maxu;
959 vertices[2].v = maxv;
960
961 vertices[3].x = minx;
962 vertices[3].y = maxy;
963 vertices[3].z = 0.0f;
964 vertices[3].rhw = 1.0f;
965 vertices[3].color = color;
966 vertices[3].u = minu;
967 vertices[3].v = maxv;
968
969 D3D_SetBlendMode(data, texture->blendMode);
831 970
832 switch (texture->scaleMode) { 971 switch (texture->scaleMode) {
833 case SDL_TEXTURESCALEMODE_NONE: 972 case SDL_TEXTURESCALEMODE_NONE:
834 case SDL_TEXTURESCALEMODE_FAST: 973 case SDL_TEXTURESCALEMODE_FAST:
835 IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER, 974 IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,