comparison dmgfx.c @ 0:32250b436bca

Initial re-import.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Sep 2012 01:54:23 +0300
parents
children 1ab3fd8b9afc
comparison
equal deleted inserted replaced
-1:000000000000 0:32250b436bca
1 #include "dmlib.h"
2
3
4 void dmFillRect(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 col)
5 {
6 SDL_Rect rc;
7 rc.x = x0;
8 rc.y = y0;
9 rc.w = x1 - x0 + 1;
10 rc.h = y1 - y0 + 1;
11 SDL_FillRect(screen, &rc, col);
12 }
13
14 #define DM_SWAP(T, A, B) { if ((B) < (A)) { T swtmp = (B); B = (A); A = swtmp; } }
15
16 void dmDrawHLine(SDL_Surface *screen, int x0, int x1, int yc, const Uint32 col)
17 {
18 int x;
19 const int bpp = screen->format->BytesPerPixel,
20 cx0 = screen->clip_rect.x, cy0 = screen->clip_rect.y,
21 cx1 = screen->clip_rect.x + screen->clip_rect.w - 1,
22 cy1 = screen->clip_rect.y + screen->clip_rect.h - 1;
23
24 DM_SWAP(int, x0, x1);
25 if (yc < cy0|| yc > cy1 || x1 < cx0 || x0 > cx1) return;
26 if (x0 < cx0) x0 = cx0;
27 if (x1 > cx1) x1 = cx1;
28
29 Uint8 *pix = screen->pixels + yc * screen->pitch + (x0 * bpp);
30 switch (screen->format->BitsPerPixel)
31 {
32 case 8:
33 for (x = x0; x <= x1; x++)
34 *pix++ = col;
35 break;
36
37 case 15:
38 case 16:
39 {
40 Uint16 *p = (Uint16 *) pix;
41 for (x = x0; x <= x1; x++)
42 *p++ = col;
43 }
44 break;
45
46 case 24:
47 for (x = x0; x <= x1; x++)
48 {
49 *pix++ = col;
50 *pix++ = col;
51 *pix++ = col;
52 }
53 break;
54
55 case 32:
56 {
57 Uint32 *p = (Uint32 *) pix;
58 for (x = x0; x <= x1; x++)
59 *p++ = col;
60 }
61 break;
62 }
63 }
64
65
66 void dmDrawVLine(SDL_Surface *screen, int y0, int y1, int xc, const Uint32 col)
67 {
68 int y;
69 const int bpp = screen->format->BytesPerPixel,
70 pitch = screen->pitch / bpp,
71 cx0 = screen->clip_rect.x, cy0 = screen->clip_rect.y,
72 cx1 = screen->clip_rect.x + screen->clip_rect.w - 1,
73 cy1 = screen->clip_rect.y + screen->clip_rect.h - 1;
74
75 DM_SWAP(int, y0, y1);
76 if (xc < cx0 || xc > cx1 || y1 < cy0 || y0 > cy1) return;
77 if (y0 < cy0) y0 = cy0;
78 if (y1 > cy1) y1 = cy1;
79
80 Uint8 *pix = screen->pixels + y0 * screen->pitch + (xc * bpp);
81 switch (screen->format->BitsPerPixel)
82 {
83 case 8:
84 for (y = y0; y <= y1; y++, pix += pitch)
85 *pix = col;
86 break;
87
88 case 16:
89 case 15:
90 {
91 Uint16 *p = (Uint16 *) pix;
92 for (y = y0; y <= y1; y++, p += pitch)
93 *p = col;
94 }
95 break;
96
97 case 24:
98 for (y = y0; y <= y1; y++, pix += pitch)
99 pix[0] = pix[1] = pix[2] = col;
100 break;
101
102 case 32:
103 {
104 Uint32 *p = (Uint32 *) pix;
105 for (y = y0; y <= y1; y++, p += pitch)
106 *p = col;
107 }
108 break;
109 }
110 }
111
112
113 void dmDrawBox3D(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 bgcol, Uint32 ucol, Uint32 dcol)
114 {
115 SDL_Rect rc;
116
117 rc.x = x0 + 1;
118 rc.y = y0 + 1;
119 rc.w = x1 - x0 - 1;
120 rc.h = y1 - y0 - 1;
121 SDL_FillRect(screen, &rc, bgcol);
122
123 dmDrawHLine(screen, x0 , x1 - 1, y0, ucol);
124 dmDrawHLine(screen, x0 + 1, x1 , y1, dcol);
125
126 dmDrawVLine(screen, y0 , y1 - 1, x0, ucol);
127 dmDrawVLine(screen, y0 + 1, y1 , x1, dcol);
128 }
129
130
131 void dmClearSurface(SDL_Surface *screen, const Uint32 col)
132 {
133 SDL_FillRect(screen, NULL, col);
134 }
135
136
137 Uint32 dmMapRGB(SDL_Surface *screen, int r, int g, int b)
138 {
139 return SDL_MapRGB(screen->format, r, g, b);
140 }
141
142
143 Uint32 dmMapRGBA(SDL_Surface *screen, int r, int g, int b, int a)
144 {
145 return SDL_MapRGBA(screen->format, r, g, b, a);
146 }
147
148
149 int dmDirectBlitSurface(SDL_Surface *bmp, SDL_Surface *screen)
150 {
151 return SDL_BlitSurface(bmp, NULL, screen, NULL);
152 }