comparison src/video/nds/SDL_ndsrender.c @ 2672:74ac057679d5 gsoc2008_nds

NDS video driver is now functional, using software rendering and framebuffer.
author Darren Alton <dalton@stevens.edu>
date Tue, 17 Jun 2008 08:26:36 +0000
parents c3e7c0698cbb
children 24a6b3588eac
comparison
equal deleted inserted replaced
2671:c3e7c0698cbb 2672:74ac057679d5
59 int current_screen; 59 int current_screen;
60 SDL_Surface *screens[3]; 60 SDL_Surface *screens[3];
61 int ultimate_answer; 61 int ultimate_answer;
62 } SDL_NDS_RenderData; 62 } SDL_NDS_RenderData;
63 63
64
65
66 /* 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
68 */
69
70 u16
71 sdlds_rgb2bgr(u16 c) {
72 /* hack to get the colors right until I actually add BGR555 to the headers */
73 u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
74 r = (c & Rmask) >> 10;
75 b = (c & Bmask) << 10;
76 return (c & GAmask) | r | b;
77 }
78
79 void
80 sdlds_surf2vram(SDL_Surface * s)
81 {
82
83 int i;
84 /*for (i = 0; i < 30; ++i) swiWaitForVBlank();*/
85 for (i = 0; i < 256 * 192; ++i) {
86 ((u16 *) VRAM_A)[i] = sdlds_rgb2bgr(((u16 *) (s->pixels))[i+160]);
87 }
88 }
89
90 void
91 sdlds_print_pixfmt_info(SDL_PixelFormat *f) {
92 if(!f) return;
93 printf("bpp: %d\nRGBA: %x %x %x %x\n",
94 f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
95 }
96
97 void
98 sdlds_print_surface_info(SDL_Surface *s) {
99 if(!s) return;
100 printf("flags: %x\nsize: %dx%d, pitch: %d\nlocked: %d, refcount: %d\n",
101 s->flags, s->w, s->h, s->pitch, s->locked, s->refcount);
102 sdlds_print_pixfmt_info(s->format);
103 }
104
105
106
64 SDL_Renderer * 107 SDL_Renderer *
65 SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags) 108 SDL_NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
66 { 109 {
67 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 110 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
68 SDL_DisplayMode *displayMode = &display->current_mode; 111 SDL_DisplayMode *displayMode = &display->current_mode;
69 SDL_Renderer *renderer; 112 SDL_Renderer *renderer;
70 SDL_NDS_RenderData *data; 113 SDL_NDS_RenderData *data;
71 int i, n; 114 int i, n;
72 int bpp = 16; 115 int bpp = 15;
116 /*/
73 Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000; 117 Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
74 118 /*/
119 Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
120 //*/
121
122 #if 0
75 printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags); 123 printf("SDL_NDS_CreateRenderer(window, 0x%x)\n", flags);
76 printf(" window: (%d,%d), %dx%d\n", window->x, window->y, window->w, 124 printf(" window: (%d,%d), %dx%d\n",
77 window->h); 125 window->x, window->y, window->w, window->h);
126 #endif
78 127
79 /* hard coded this to ARGB1555 for now 128 /* hard coded this to ARGB1555 for now
80 if (!SDL_PixelFormatEnumToMasks 129 if (!SDL_PixelFormatEnumToMasks
81 (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) { 130 (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
82 SDL_SetError("Unknown display format"); 131 SDL_SetError("Unknown display format");
117 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY; 166 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
118 n = 1; 167 n = 1;
119 } 168 }
120 for (i = 0; i < n; ++i) { 169 for (i = 0; i < n; ++i) {
121 data->screens[i] = 170 data->screens[i] =
122 SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask, 171 SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask, Amask);
123 Amask);
124 if (!data->screens[i]) { 172 if (!data->screens[i]) {
125 SDL_NDS_DestroyRenderer(renderer); 173 SDL_NDS_DestroyRenderer(renderer);
126 return NULL; 174 return NULL;
127 } 175 }
128 SDL_SetSurfacePalette(data->screens[i], display->palette); 176 SDL_SetSurfacePalette(data->screens[i], display->palette);
129 } 177 }
130 178
131 data->current_screen = 0; 179 data->current_screen = 0;
132 data->ultimate_answer = 42; 180 data->ultimate_answer = 42;
133 #if 0 181
134 #define blarg (data->screens[0])
135 printf("hello?\n");
136 if (!data || !(data->screens) || !blarg) {
137 printf("they're null.\n");
138 } else {
139 printf("not null.\n");
140 printf("%d\n%d\n%d\n%d\n%x\n%x\n%x\n%x\n", blarg->w, blarg->h, blarg->pitch, blarg->format->BitsPerPixel, blarg->format->Rmask, blarg->format->Gmask, blarg->format->Bmask, (u32) (blarg->pixels)); /* ARGH WHY DOESN'T THIS PRINT AT ALL? */
141 printf("hurr\n");
142 }
143 #undef blarg
144 #endif
145 return renderer; 182 return renderer;
146 } 183 }
147 184
148 static int 185 static int
149 SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, 186 SDL_NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
157 color = SDL_MapRGBA(target->format, r, g, b, a); 194 color = SDL_MapRGBA(target->format, r, g, b, a);
158 195
159 return SDL_FillRect(target, &real_rect, color); 196 return SDL_FillRect(target, &real_rect, color);
160 } 197 }
161 198
162 /* this is mainly for testing stuff to put a surface where I can see it */
163 void
164 sdlds_surf2vram(SDL_Surface * s)
165 {
166 int i;
167 for (i = 0; i < 256 * 192; ++i) {
168 ((u16 *) VRAM_A)[i] = ((u16 *) (s->pixels))[i];
169 }
170 }
171
172 static int 199 static int
173 SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 200 SDL_NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
174 const SDL_Rect * srcrect, const SDL_Rect * dstrect) 201 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
175 { 202 {
176 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata; 203 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
177 SDL_Window *window = SDL_GetWindowFromID(renderer->window); 204 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
178 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 205 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
206 #if 0
179 printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n"); 207 printf("SDL_NDS_RenderCopy(renderer, texture, srcrect, dstrect)\n");
180 printf(" renderer: %s\n", renderer->info.name); 208 printf(" renderer: %s\n", renderer->info.name);
181 printf(" texture: %dx%d\n", texture->w, texture->h); 209 printf(" texture: %dx%d\n", texture->w, texture->h);
182 printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w, 210 printf(" srcrect: (%d,%d), %dx%d\n", srcrect->x, srcrect->y, srcrect->w,
183 srcrect->h); 211 srcrect->h);
184 printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w, 212 printf(" dstrect: (%d,%d), %dx%d\n", dstrect->x, dstrect->y, dstrect->w,
185 dstrect->h); 213 dstrect->h);
186 214 #endif
187 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { 215 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
188 SDL_Surface *target = data->screens[data->current_screen]; 216 SDL_Surface *target = data->screens[data->current_screen];
189 void *pixels = 217 void *pixels =
190 (Uint8 *) target->pixels + dstrect->y * target->pitch + 218 (Uint8 *) target->pixels + dstrect->y * target->pitch +
191 dstrect->x * target->format->BytesPerPixel; 219 dstrect->x * target->format->BytesPerPixel;
196 } else { 224 } else {
197 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; 225 SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
198 SDL_Surface *target = data->screens[data->current_screen]; 226 SDL_Surface *target = data->screens[data->current_screen];
199 SDL_Rect real_srcrect = *srcrect; 227 SDL_Rect real_srcrect = *srcrect;
200 SDL_Rect real_dstrect = *dstrect; 228 SDL_Rect real_dstrect = *dstrect;
201 printf("Rmask %x Gmask %x Bmask %x Amask %x\n" 229 /*sdlds_print_surface_info(surface);
202 "width %d, height %d, pitch %d\nbpp %d, pixels %x\n", 230 sdlds_print_surface_info(target);*/
203 surface->format->Rmask, surface->format->Gmask,
204 surface->format->Bmask, surface->format->Amask,
205 surface->w, surface->h, surface->pitch,
206 surface->format->BitsPerPixel, (u32) (surface->pixels));
207 sdlds_surf2vram(surface); 231 sdlds_surf2vram(surface);
208 return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect); 232 return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
209 } 233 }
210 #if 0 234 #if 0
211 /* previous attempt to copy it directly to vram */ 235 /* copy it directly to vram */
212 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; 236 SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
213 int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h; 237 int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
214 int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h; 238 int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
215 int si, sj, di, dj; 239 int si, sj, di, dj;
216 /*printf("DEBUG: still alive!\n"); */
217 for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) { 240 for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
218 for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) { 241 for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
219 ((uint16 *) VRAM_A)[(dj + dy) * 256 + di + dx] 242 VRAM_A[(dj + dy) * 256 + di + dx] =
220 = ((Uint16 *) surface->pixels)[(sj + sy) * (surface->w) + si + 243 ((Uint16 *) surface->pixels)[(sj + sy) * (surface->w) + si + sx];
221 sx];
222 } 244 }
223 } 245 }
224 /*printf("DEBUG: still alive!\n"); */ 246 return 0;
225 } 247 #endif
226 248 }
227 return 0; 249
228 #endif
229 }
230 250
231 static void 251 static void
232 SDL_NDS_RenderPresent(SDL_Renderer * renderer) 252 SDL_NDS_RenderPresent(SDL_Renderer * renderer)
233 { 253 {
234 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata; 254 SDL_NDS_RenderData *data = (SDL_NDS_RenderData *) renderer->driverdata;
235 255 #if 0
236 printf("SDL_NDS_RenderPresent(renderer)\n"); 256 printf("SDL_NDS_RenderPresent(renderer)\n");
237 printf(" renderer: %s\n", renderer->info.name); 257 printf(" renderer: %s\n", renderer->info.name);
258 #endif
238 /* Send the data to the display */ 259 /* Send the data to the display */
239 260 /*sdlds_surf2vram(data->screens[data->current_screen]);*/
240 #if 0
241 /*testing to see if rectangles drawn get copied right*/
242 {
243 SDL_Rect ra;
244 ra.x = 0;
245 ra.y = 0;
246 ra.w = 256;
247 ra.h = 192;
248
249 SDL_FillRect(data->screens[data->current_screen], &ra, 0x250);
250 ra.x = 32;
251 ra.y = 32;
252 ra.w = 192;
253 ra.h = 128;
254
255 SDL_FillRect(data->screens[data->current_screen], &ra,
256 SDL_MapRGBA(data->screens[data->current_screen]->format,
257 255, 255, 255, 255));
258 }
259 /*okay so this works but why not when I do it in the main()?
260 for some reason the screen I get from screen=SDL_SetVideoMode(...)
261 doesn't get copied to renderer->driverdata? */
262 for (i = 0; i < 30; ++i)
263 swiWaitForVBlank(); /* delay for debug purpose */
264 #endif
265 sdlds_surf2vram(data->screens[data->current_screen]);
266 261
267 /* Update the flipping chain, if any */ 262 /* Update the flipping chain, if any */
268 if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) { 263 if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
269 data->current_screen = (data->current_screen + 1) % 2; 264 data->current_screen = (data->current_screen + 1) % 2;
270 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) { 265 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {