Mercurial > sdl-ios-xcode
comparison src/video/nds/SDL_ndsrender.c @ 2677:6386764eb222 gsoc2008_nds
Starting to work on a more complete video driver.
author | Darren Alton <dalton@stevens.edu> |
---|---|
date | Wed, 02 Jul 2008 13:59:30 +0000 |
parents | 24a6b3588eac |
children | 3895761db26a |
comparison
equal
deleted
inserted
replaced
2676:082cc3ffd7d8 | 2677:6386764eb222 |
---|---|
32 #include "../SDL_renderer_sw.h" | 32 #include "../SDL_renderer_sw.h" |
33 | 33 |
34 | 34 |
35 /* SDL surface based renderer implementation */ | 35 /* SDL surface based renderer implementation */ |
36 | 36 |
37 static SDL_Renderer *SDL_NDS_CreateRenderer(SDL_Window * window, | 37 static SDL_Renderer *NDS_CreateRenderer(SDL_Window * window, Uint32 flags); |
38 Uint32 flags); | 38 static int NDS_ActivateRenderer(SDL_Renderer * renderer); |
39 static int SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, | 39 static int NDS_DisplayModeChanged(SDL_Renderer * renderer); |
40 static int NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); | |
41 static int NDS_QueryTexturePixels(SDL_Renderer * renderer, | |
42 SDL_Texture * texture, void **pixels, | |
43 int *pitch); | |
44 static int NDS_SetTexturePalette(SDL_Renderer * renderer, | |
45 SDL_Texture * texture, | |
46 const SDL_Color * colors, int firstcolor, | |
47 int ncolors); | |
48 static int NDS_GetTexturePalette(SDL_Renderer * renderer, | |
49 SDL_Texture * texture, SDL_Color * colors, | |
50 int firstcolor, int ncolors); | |
51 static int NDS_SetTextureColorMod(SDL_Renderer * renderer, | |
52 SDL_Texture * texture); | |
53 static int NDS_SetTextureAlphaMod(SDL_Renderer * renderer, | |
54 SDL_Texture * texture); | |
55 static int NDS_SetTextureBlendMode(SDL_Renderer * renderer, | |
56 SDL_Texture * texture); | |
57 static int NDS_SetTextureScaleMode(SDL_Renderer * renderer, | |
58 SDL_Texture * texture); | |
59 static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, | |
60 const SDL_Rect * rect, const void *pixels, | |
61 int pitch); | |
62 static int NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, | |
63 const SDL_Rect * rect, int markDirty, void **pixels, | |
64 int *pitch); | |
65 static void NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture); | |
66 static void NDS_DirtyTexture(SDL_Renderer * renderer, | |
67 SDL_Texture * texture, int numrects, | |
68 const SDL_Rect * rects); | |
69 static int NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, | |
40 Uint8 b, Uint8 a, const SDL_Rect * rect); | 70 Uint8 b, Uint8 a, const SDL_Rect * rect); |
41 static int SDL_NDS_RenderCopy(SDL_Renderer * renderer, | 71 static int NDS_RenderCopy(SDL_Renderer * renderer, |
42 SDL_Texture * texture, | 72 SDL_Texture * texture, |
43 const SDL_Rect * srcrect, | 73 const SDL_Rect * srcrect, |
44 const SDL_Rect * dstrect); | 74 const SDL_Rect * dstrect); |
45 static void SDL_NDS_RenderPresent(SDL_Renderer * renderer); | 75 static void NDS_RenderPresent(SDL_Renderer * renderer); |
46 static void SDL_NDS_DestroyRenderer(SDL_Renderer * renderer); | 76 static void NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture); |
47 | 77 static void NDS_DestroyRenderer(SDL_Renderer * renderer); |
48 | 78 |
49 SDL_RenderDriver SDL_NDS_RenderDriver = { | 79 |
50 SDL_NDS_CreateRenderer, | 80 SDL_RenderDriver NDS_RenderDriver = { |
81 NDS_CreateRenderer, | |
51 {"nds", SDL_RENDERER_PRESENTCOPY} | 82 {"nds", SDL_RENDERER_PRESENTCOPY} |
52 /* (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY | | 83 /* (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY | |
53 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 | | 84 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 | |
54 SDL_RENDERER_PRESENTDISCARD),*/ | 85 SDL_RENDERER_PRESENTDISCARD),*/ |
55 }; | 86 }; |
56 | 87 |
57 typedef struct | 88 typedef struct |
58 { | 89 { |
59 int current_screen; | 90 int current_screen; |
60 SDL_Surface *screens[3]; | 91 u16* fb; |
61 int ultimate_answer; | 92 } NDS_RenderData; |
62 } SDL_NDS_RenderData; | |
63 | 93 |
64 | 94 |
65 | 95 |
66 /* this is mainly hackish testing/debugging stuff to get cleaned up soon | 96 /* this is mainly hackish testing/debugging stuff to get cleaned up soon |
67 anything named sdlds_blah shouldn't make it into the stable version | 97 anything named sdlds_blah shouldn't make it into the stable version |
79 | 109 |
80 void | 110 void |
81 sdlds_surf2vram(SDL_Surface * s) | 111 sdlds_surf2vram(SDL_Surface * s) |
82 { | 112 { |
83 if (s->w == 256) { | 113 if (s->w == 256) { |
114 u16 tmpbuf[0x20000]; | |
115 int i; | |
116 | |
84 dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16), | 117 dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16), |
85 VRAM_A, 256 * 192 * sizeof(u16)); | 118 tmpbuf, 256 * 192 * sizeof(u16)); |
119 /* hack to fix the pixel format until I figure out why BGR doesn't work */ | |
120 for (i = 0; i < 256 * 192; ++i) { | |
121 tmpbuf[i] = sdlds_rgb2bgr(tmpbuf[i]); | |
122 } | |
123 dmaCopy(tmpbuf, VRAM_A, 256 * 192 * sizeof(u16)); | |
86 } | 124 } |
87 } | 125 } |
88 | 126 |
89 void | 127 void |
90 sdlds_print_pixfmt_info(SDL_PixelFormat * f) | 128 sdlds_print_pixfmt_info(SDL_PixelFormat * f) |
106 } | 144 } |
107 | 145 |
108 | 146 |
109 | 147 |
110 SDL_Renderer * | 148 SDL_Renderer * |
111 SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags) | 149 NDS_CreateRenderer(SDL_Window * window, Uint32 flags) |
112 { | 150 { |
113 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); | 151 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); |
114 SDL_DisplayMode *displayMode = &display->current_mode; | 152 SDL_DisplayMode *displayMode = &display->current_mode; |
115 SDL_Renderer *renderer; | 153 SDL_Renderer *renderer; |
116 SDL_NDS_RenderData *data; | 154 NDS_RenderData *data; |
117 int i, n; | 155 int i, n; |
118 int bpp = 15; | 156 int bpp = 15; |
119 Uint32 Rmask, Gmask, Bmask, Amask; | 157 Uint32 Rmask, Gmask, Bmask, Amask; |
120 /* Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000; | 158 /* Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000; |
121 Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000; | 159 Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000; |
122 */ | 160 */ |
123 | 161 |
124 #if 0 | |
125 printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags); | |
126 printf(" window: (%d,%d), %dx%d\n", | |
127 window->x, window->y, window->w, window->h); | |
128 #endif | |
129 | |
130 /* hard coded this to BGR555 for now */ | 162 /* hard coded this to BGR555 for now */ |
131 if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp, | 163 if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp, |
132 &Rmask, &Gmask, &Bmask, &Amask)) { | 164 &Rmask, &Gmask, &Bmask, &Amask)) { |
133 SDL_SetError("Unknown display format"); | 165 SDL_SetError("Unknown display format"); |
134 return NULL; | 166 return NULL; |
138 if (!renderer) { | 170 if (!renderer) { |
139 SDL_OutOfMemory(); | 171 SDL_OutOfMemory(); |
140 return NULL; | 172 return NULL; |
141 } | 173 } |
142 | 174 |
143 data = (SDL_NDS_RenderData *) SDL_malloc(sizeof(*data)); | 175 data = (NDS_RenderData *) SDL_malloc(sizeof(*data)); |
144 if (!data) { | 176 if (!data) { |
145 SDL_NDS_DestroyRenderer(renderer); | 177 NDS_DestroyRenderer(renderer); |
146 SDL_OutOfMemory(); | 178 SDL_OutOfMemory(); |
147 return NULL; | 179 return NULL; |
148 } | 180 } |
149 SDL_zerop(data); | 181 SDL_zerop(data); |
150 | 182 |
151 renderer->RenderFill = SDL_NDS_RenderFill; | 183 renderer->RenderFill = NDS_RenderFill; |
152 renderer->RenderCopy = SDL_NDS_RenderCopy; | 184 renderer->RenderCopy = NDS_RenderCopy; |
153 renderer->RenderPresent = SDL_NDS_RenderPresent; | 185 renderer->RenderPresent = NDS_RenderPresent; |
154 renderer->DestroyRenderer = SDL_NDS_DestroyRenderer; | 186 renderer->DestroyRenderer = NDS_DestroyRenderer; |
155 renderer->info.name = SDL_NDS_RenderDriver.info.name; | 187 renderer->info.name = NDS_RenderDriver.info.name; |
156 renderer->info.flags = 0; | 188 renderer->info.flags = 0; |
157 renderer->window = window->id; | 189 renderer->window = window->id; |
158 renderer->driverdata = data; | 190 renderer->driverdata = data; |
159 Setup_SoftwareRenderer(renderer); | 191 Setup_SoftwareRenderer(renderer); /* TODO: well, "TODON'T" is more like it */ |
160 | 192 |
161 if (flags & SDL_RENDERER_PRESENTFLIP2) { | 193 if (flags & SDL_RENDERER_PRESENTFLIP2) { |
162 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2; | 194 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2; |
163 n = 2; | 195 n = 2; |
164 } else if (flags & SDL_RENDERER_PRESENTFLIP3) { | 196 } else if (flags & SDL_RENDERER_PRESENTFLIP3) { |
165 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3; | 197 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3; |
166 n = 3; | 198 n = 3; |
167 } else { | 199 } else { |
168 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY; | 200 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY; |
169 n = 1; | 201 n = 1; |
170 } | 202 }/* |
171 for (i = 0; i < n; ++i) { | 203 for (i = 0; i < n; ++i) { |
172 data->screens[i] = | 204 data->screens[i] = |
173 SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask, | 205 SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask, |
174 Amask); | 206 Amask); |
175 if (!data->screens[i]) { | 207 if (!data->screens[i]) { |
176 SDL_NDS_DestroyRenderer(renderer); | 208 NDS_DestroyRenderer(renderer); |
177 return NULL; | 209 return NULL; |
178 } | 210 } |
179 SDL_SetSurfacePalette(data->screens[i], display->palette); | 211 SDL_SetSurfacePalette(data->screens[i], display->palette); |
180 sdlds_print_surface_info(data->screens[i]); | 212 sdlds_print_surface_info(data->screens[i]); |
181 } | 213 }*/ |
182 | 214 |
183 data->current_screen = 0; | 215 data->fb = (u16*)0x06020000; |
184 data->ultimate_answer = 42; | |
185 | 216 |
186 return renderer; | 217 return renderer; |
187 } | 218 } |
188 | 219 |
189 static int | 220 static int |
190 SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, | 221 NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, |
191 Uint8 a, const SDL_Rect * rect) | 222 Uint8 a, const SDL_Rect * rect) |
192 { | 223 { |
193 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata; | 224 NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata; |
194 SDL_Surface *target = data->screens[data->current_screen]; | |
195 Uint32 color; | |
196 SDL_Rect real_rect = *rect; | 225 SDL_Rect real_rect = *rect; |
197 | 226 u16 color; |
198 color = SDL_MapRGBA(target->format, r, g, b, a); | 227 int i, j; |
199 | 228 |
200 return SDL_FillRect(target, &real_rect, color); | 229 color = RGB15(r>>3,g>>3,b>>3); |
201 } | 230 for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) { |
202 | 231 for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) { |
232 data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = | |
233 0x8000 | color; | |
234 } | |
235 } | |
236 return 0; | |
237 } | |
203 static int | 238 static int |
204 SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, | 239 NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, |
205 const SDL_Rect * srcrect, const SDL_Rect * dstrect) | 240 const SDL_Rect * srcrect, const SDL_Rect * dstrect) |
206 { | 241 { |
207 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata; | 242 NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata; |
208 SDL_Window *window = SDL_GetWindowFromID(renderer->window); | 243 SDL_Window *window = SDL_GetWindowFromID(renderer->window); |
209 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); | 244 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); |
245 | |
210 #if 0 | 246 #if 0 |
211 printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n"); | |
212 printf(" renderer: %s\n", renderer->info.name); | |
213 printf(" texture: %dx%d\n", texture->w, texture->h); | |
214 printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w, | |
215 srcrect->h); | |
216 printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w, | |
217 dstrect->h); | |
218 #endif | |
219 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | 247 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { |
220 SDL_Surface *target = data->screens[data->current_screen]; | 248 SDL_Surface *target = data->screens[data->current_screen]; |
221 void *pixels = | 249 void *pixels = |
222 (Uint8 *) target->pixels + dstrect->y * target->pitch + | 250 (Uint8 *) target->pixels + dstrect->y * target->pitch + |
223 dstrect->x * target->format->BytesPerPixel; | 251 dstrect->x * target->format->BytesPerPixel; |
233 /*sdlds_print_surface_info(surface); | 261 /*sdlds_print_surface_info(surface); |
234 sdlds_print_surface_info(target); */ | 262 sdlds_print_surface_info(target); */ |
235 sdlds_surf2vram(surface); | 263 sdlds_surf2vram(surface); |
236 return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect); | 264 return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect); |
237 } | 265 } |
238 #if 0 | 266 #endif |
239 /* copy it directly to vram */ | 267 /* copy it directly to vram */ |
240 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | 268 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; |
269 sdlds_surf2vram(surface); | |
270 /* | |
241 int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h; | 271 int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h; |
242 int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h; | 272 int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h; |
243 int si, sj, di, dj; | 273 int si, sj, di, dj; |
244 for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) { | 274 for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) { |
245 for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) { | 275 for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) { |
246 VRAM_A[(dj + dy) * 256 + di + dx] = | 276 data->fb[(dj + dy) * 256 + di + dx] = 0x8000 | |
247 ((Uint16 *) surface->pixels)[(sj + sy) * (surface->w) + si + | 277 ((u16 *) surface->pixels)[(sj + sy) * (surface->w) + si + |
248 sx]; | 278 sx]; |
249 } | 279 } |
250 } | 280 }*/ |
251 return 0; | 281 return 0; |
252 #endif | |
253 } | 282 } |
254 | 283 |
255 | 284 |
256 static void | 285 static void |
257 SDL_NDS_RenderPresent(SDL_Renderer * renderer) | 286 NDS_RenderPresent(SDL_Renderer * renderer) |
258 { | 287 { |
259 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata; | 288 NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata; |
260 int i; | 289 /* Send the data to the display TODO */ |
261 #if 0 | 290 |
262 printf("SDL_NDS_RenderPresent(renderer)\n"); | |
263 printf(" renderer: %s\n", renderer->info.name); | |
264 #endif | |
265 /* Send the data to the display */ | |
266 | |
267 /* hack to fix the pixel format until I figure out why BGR doesn't work */ | |
268 for (i = 0; i < 256 * 192; ++i) { | |
269 VRAM_A[i] = sdlds_rgb2bgr(VRAM_A[i]); | |
270 } | |
271 /* Update the flipping chain, if any */ | 291 /* Update the flipping chain, if any */ |
272 if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) { | 292 if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) { |
273 data->current_screen = (data->current_screen + 1) % 2; | 293 data->current_screen = (data->current_screen + 1) % 2; |
274 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) { | 294 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) { |
275 data->current_screen = (data->current_screen + 1) % 3; | 295 data->current_screen = (data->current_screen + 1) % 3; |
276 } | 296 } |
277 } | 297 } |
278 | 298 |
279 static void | 299 static void |
280 SDL_NDS_DestroyRenderer(SDL_Renderer * renderer) | 300 NDS_DestroyRenderer(SDL_Renderer * renderer) |
281 { | 301 { |
282 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata; | 302 NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata; |
283 int i; | 303 int i; |
284 | 304 |
285 printf("SDL_NDS_DestroyRenderer(renderer)\n"); | 305 printf("NDS_DestroyRenderer(renderer)\n"); |
286 printf(" renderer: %s\n", renderer->info.name); | 306 printf(" renderer: %s\n", renderer->info.name); |
287 if (data) { | 307 if (data) { |
288 for (i = 0; i < SDL_arraysize(data->screens); ++i) { | 308 /*for (i = 0; i < SDL_arraysize(data->screens); ++i) { |
289 if (data->screens[i]) { | 309 if (data->screens[i]) { |
290 SDL_FreeSurface(data->screens[i]); | 310 SDL_FreeSurface(data->screens[i]); |
291 } | 311 } |
292 } | 312 }*/ |
293 SDL_free(data); | 313 SDL_free(data); |
294 } | 314 } |
295 SDL_free(renderer); | 315 SDL_free(renderer); |
296 } | 316 } |
297 | 317 |