Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
529 lines (469 loc) · 17.2 KB

SDL_ndsrender.c

File metadata and controls

529 lines (469 loc) · 17.2 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include <stdio.h>
#include <stdlib.h>
#include <nds.h>
#include "SDL_config.h"
#include "SDL_video.h"
#include "../SDL_sysvideo.h"
#include "../SDL_yuv_sw_c.h"
#include "../SDL_renderer_sw.h"
/* SDL surface based renderer implementation */
Jul 2, 2008
Jul 2, 2008
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
static SDL_Renderer *NDS_CreateRenderer(SDL_Window * window, Uint32 flags);
static int NDS_ActivateRenderer(SDL_Renderer * renderer);
static int NDS_DisplayModeChanged(SDL_Renderer * renderer);
static int NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int NDS_QueryTexturePixels(SDL_Renderer * renderer,
SDL_Texture * texture, void **pixels,
int *pitch);
static int NDS_SetTexturePalette(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Color * colors, int firstcolor,
int ncolors);
static int NDS_GetTexturePalette(SDL_Renderer * renderer,
SDL_Texture * texture, SDL_Color * colors,
int firstcolor, int ncolors);
static int NDS_SetTextureColorMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int NDS_SetTextureAlphaMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int NDS_SetTextureBlendMode(SDL_Renderer * renderer,
SDL_Texture * texture);
static int NDS_SetTextureScaleMode(SDL_Renderer * renderer,
SDL_Texture * texture);
static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
static int NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch);
static void NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void NDS_DirtyTexture(SDL_Renderer * renderer,
SDL_Texture * texture, int numrects,
const SDL_Rect * rects);
static int NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
Jun 12, 2008
Jun 12, 2008
70
Uint8 b, Uint8 a, const SDL_Rect * rect);
Jul 2, 2008
Jul 2, 2008
71
static int NDS_RenderCopy(SDL_Renderer * renderer,
Jun 12, 2008
Jun 12, 2008
72
73
74
SDL_Texture * texture,
const SDL_Rect * srcrect,
const SDL_Rect * dstrect);
Jul 2, 2008
Jul 2, 2008
75
76
77
static void NDS_RenderPresent(SDL_Renderer * renderer);
static void NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void NDS_DestroyRenderer(SDL_Renderer * renderer);
Jul 2, 2008
Jul 2, 2008
80
81
SDL_RenderDriver NDS_RenderDriver = {
NDS_CreateRenderer,
Jul 10, 2008
Jul 10, 2008
82
83
84
85
86
87
88
89
90
91
{"nds", SDL_RENDERER_SINGLEBUFFER}
/*SDL_RENDERER_ values
SINGLEBUFFER Render directly to the window, if possible
PRESENTCOPY Present uses a copy from back buffer to the front buffer
PRESENTFLIP2 Present uses a flip, swapping back buffer and front buffer
PRESENTFLIP3 Present uses a flip, rotating two back buf.s and a front buf.
PRESENTDISCARD Present leaves the contents of the backbuffer undefined
PRESENTVSYNC Present is synchronized with the refresh rate
ACCELERATED The renderer uses hardware acceleration
*/
92
93
94
95
96
};
typedef struct
{
int current_screen;
Jul 2, 2008
Jul 2, 2008
97
98
u16* fb;
} NDS_RenderData;
Jul 10, 2008
Jul 10, 2008
100
101
102
103
104
105
typedef struct
{
enum { NDSTX_BG, NDSTX_SPR } type;
struct { int w, h, pitch, bpp; } dim;
u16 *vram;
} NDS_TextureData;
Jun 17, 2008
Jun 17, 2008
106
107
108
109
110
111
112
/* this is mainly hackish testing/debugging stuff to get cleaned up soon
anything named sdlds_blah shouldn't make it into the stable version
*/
u16
Jun 19, 2008
Jun 19, 2008
113
114
115
sdlds_rgb2bgr(u16 c)
{
/* hack to get the proper colors until I actually get BGR555 to work right */
Jun 17, 2008
Jun 17, 2008
116
117
118
119
120
121
122
123
124
u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
r = (c & Rmask) >> 10;
b = (c & Bmask) << 10;
return (c & GAmask) | r | b;
}
void
sdlds_surf2vram(SDL_Surface * s)
{
Jun 19, 2008
Jun 19, 2008
125
if (s->w == 256) {
Jul 2, 2008
Jul 2, 2008
126
127
128
u16 tmpbuf[0x20000];
int i;
Jun 19, 2008
Jun 19, 2008
129
dmaCopy((u8 *) (s->pixels) + 156 * sizeof(u16),
Jul 2, 2008
Jul 2, 2008
130
131
132
133
134
135
tmpbuf, 256 * 192 * sizeof(u16));
/* hack to fix the pixel format until I figure out why BGR doesn't work */
for (i = 0; i < 256 * 192; ++i) {
tmpbuf[i] = sdlds_rgb2bgr(tmpbuf[i]);
}
dmaCopy(tmpbuf, VRAM_A, 256 * 192 * sizeof(u16));
Jun 17, 2008
Jun 17, 2008
136
137
138
139
}
}
void
Jun 19, 2008
Jun 19, 2008
140
141
142
143
144
145
sdlds_print_pixfmt_info(SDL_PixelFormat * f)
{
if (!f)
return;
printf("bpp: %d\nRGBA: %x %x %x %x\n",
f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
Jun 17, 2008
Jun 17, 2008
146
147
148
}
void
Jun 19, 2008
Jun 19, 2008
149
150
151
152
153
154
155
sdlds_print_surface_info(SDL_Surface * s)
{
if (!s)
return;
printf("flags: %x\nsize: %dx%d, pitch: %d\nlocked: %d, refcount: %d\n",
s->flags, s->w, s->h, s->pitch, s->locked, s->refcount);
sdlds_print_pixfmt_info(s->format);
Jun 17, 2008
Jun 17, 2008
156
157
}
Jul 10, 2008
Jul 10, 2008
158
/* again the above shouldn't make it into the stable version */
Jun 17, 2008
Jun 17, 2008
159
160
SDL_Renderer *
Jul 2, 2008
Jul 2, 2008
161
NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
162
163
164
165
{
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
SDL_DisplayMode *displayMode = &display->current_mode;
SDL_Renderer *renderer;
Jul 2, 2008
Jul 2, 2008
166
NDS_RenderData *data;
Jun 17, 2008
Jun 17, 2008
168
int bpp = 15;
Jun 19, 2008
Jun 19, 2008
169
170
Uint32 Rmask, Gmask, Bmask, Amask;
/* Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
Jun 17, 2008
Jun 17, 2008
171
Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
Jun 19, 2008
Jun 19, 2008
172
*/
Jun 19, 2008
Jun 19, 2008
174
175
176
177
178
179
/* hard coded this to BGR555 for now */
if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
&Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown display format");
return NULL;
}
180
181
182
183
184
185
186
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jul 2, 2008
Jul 2, 2008
187
data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
Jul 2, 2008
Jul 2, 2008
189
NDS_DestroyRenderer(renderer);
190
191
192
193
194
SDL_OutOfMemory();
return NULL;
}
SDL_zerop(data);
Jul 2, 2008
Jul 2, 2008
195
196
197
198
199
renderer->RenderFill = NDS_RenderFill;
renderer->RenderCopy = NDS_RenderCopy;
renderer->RenderPresent = NDS_RenderPresent;
renderer->DestroyRenderer = NDS_DestroyRenderer;
renderer->info.name = NDS_RenderDriver.info.name;
200
201
202
renderer->info.flags = 0;
renderer->window = window->id;
renderer->driverdata = data;
Jul 2, 2008
Jul 2, 2008
203
Setup_SoftwareRenderer(renderer); /* TODO: well, "TODON'T" is more like it */
204
205
206
207
208
209
210
211
212
213
if (flags & SDL_RENDERER_PRESENTFLIP2) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
n = 2;
} else if (flags & SDL_RENDERER_PRESENTFLIP3) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
n = 3;
} else {
renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
n = 1;
Jul 10, 2008
Jul 10, 2008
214
215
}
/*
216
217
for (i = 0; i < n; ++i) {
data->screens[i] =
Jun 19, 2008
Jun 19, 2008
218
219
SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
Amask);
220
if (!data->screens[i]) {
Jul 2, 2008
Jul 2, 2008
221
NDS_DestroyRenderer(renderer);
222
223
224
return NULL;
}
SDL_SetSurfacePalette(data->screens[i], display->palette);
Jun 19, 2008
Jun 19, 2008
225
sdlds_print_surface_info(data->screens[i]);
Jul 2, 2008
Jul 2, 2008
226
}*/
Jul 2, 2008
Jul 2, 2008
228
data->fb = (u16*)0x06020000;
Jun 17, 2008
Jun 17, 2008
229
230
231
232
return renderer;
}
Jul 10, 2008
Jul 10, 2008
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
static int
NDS_ActivateRenderer(SDL_Renderer * renderer)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
/* stub. TODO: figure out what needs to be done, if anything. */
return 0;
}
static int
NDS_DisplayModeChanged(SDL_Renderer * renderer)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
/* stub. TODO: figure out what needs to be done */
return 0;
}
static int
NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("Unsupported texture format");
return -1;
} else {
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (!SDL_PixelFormatEnumToMasks
(texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
/* TODO: appropriate checks for ABGR1555 */
texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
/* TODO: conditional statements on w/h to place it as bg/sprite */
}
if (!texture->driverdata) {
return -1;
}
return 0;
}
static int
NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
void **pixels, int *pitch)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("Unsupported texture format");
return -1;
} else {
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
*pixels = txdat->vram;
*pitch = txdat->dim.pitch;
return 0;
}
}
static int
NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Color * colors, int firstcolor, int ncolors)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("YUV textures don't have a palette");
return -1;
} else {
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
/* TODO: mess with 8-bit modes and/or 16-color palette modes */
return 0;
}
}
static int
NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
SDL_Color * colors, int firstcolor, int ncolors)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("YUV textures don't have a palette");
return -1;
} else {
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
/* TODO: mess with 8-bit modes and/or 16-color palette modes */
return 0;
}
}
static int
NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
/* stub. TODO: figure out what needs to be done, if anything */
return 0;
}
static int
NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
/* stub. TODO: figure out what needs to be done, if anything */
return 0;
}
static int
NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
/* stub. TODO: figure out what needs to be done, if anything */
return 0;
}
static int
NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
/* stub. TODO: figure out what needs to be done.
(NDS hardware scaling is nearest neighbor.) */
return 0;
}
static int
NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("Unsupported texture format");
return -1;
} else {
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
Uint8 *src, *dst;
int row;
size_t length;
/* IMPORTANT! copy the new pixels into the sprite or bg. */
src = (Uint8 *) pixels;
dst =
(Uint8 *) txdat->vram + rect->y * txdat->dim.pitch +
rect->x * (txdat->dim.bpp/8);
length = rect->w * (txdat->dim.bpp/8);
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += surface->pitch;
}
return 0;
}
}
static int
NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("Unsupported texture format");
return -1;
} else {
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
*pixels = (void *) ((u8 *)txdat->vram + rect->y * txdat->dim.pitch
+ rect->x * (txdat->dim.bpp/8));
*pitch = txdat->dim.pitch;
return 0;
}
}
static void
NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("Unsupported texture format");
}
}
static void
NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
int numrects, const SDL_Rect * rects)
{ /* stub */
}
Jul 2, 2008
Jul 2, 2008
410
NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
Jun 12, 2008
Jun 12, 2008
411
Uint8 a, const SDL_Rect * rect)
Jul 2, 2008
Jul 2, 2008
413
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
414
SDL_Rect real_rect = *rect;
Jul 2, 2008
Jul 2, 2008
415
416
417
418
419
420
421
422
423
424
425
u16 color;
int i, j;
color = RGB15(r>>3,g>>3,b>>3);
for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
data->fb[(j + real_rect.y) * 256 + i + real_rect.x] =
0x8000 | color;
}
}
return 0;
Jul 10, 2008
Jul 10, 2008
427
Jul 2, 2008
Jul 2, 2008
429
NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Jun 12, 2008
Jun 12, 2008
430
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Jul 2, 2008
Jul 2, 2008
432
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
433
434
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
Jul 2, 2008
Jul 2, 2008
435
Jun 17, 2008
Jun 17, 2008
436
#if 0
437
438
439
440
441
442
443
444
445
446
447
448
449
450
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_Surface *target = data->screens[data->current_screen];
void *pixels =
(Uint8 *) target->pixels + dstrect->y * target->pitch +
dstrect->x * target->format->BytesPerPixel;
return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
srcrect, display->current_mode.format,
dstrect->w, dstrect->h, pixels,
target->pitch);
} else {
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
SDL_Surface *target = data->screens[data->current_screen];
SDL_Rect real_srcrect = *srcrect;
SDL_Rect real_dstrect = *dstrect;
Jun 17, 2008
Jun 17, 2008
451
/*sdlds_print_surface_info(surface);
Jun 19, 2008
Jun 19, 2008
452
sdlds_print_surface_info(target); */
453
454
455
sdlds_surf2vram(surface);
return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
}
Jul 2, 2008
Jul 2, 2008
456
#endif
Jun 17, 2008
Jun 17, 2008
457
/* copy it directly to vram */
Jun 12, 2008
Jun 12, 2008
458
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Jul 2, 2008
Jul 2, 2008
459
460
sdlds_surf2vram(surface);
/*
Jun 12, 2008
Jun 12, 2008
461
462
463
464
465
int sx = srcrect->x, sy = srcrect->y, sw = srcrect->w, sh = srcrect->h;
int dx = dstrect->x, dy = dstrect->y, dw = dstrect->w, dh = dstrect->h;
int si, sj, di, dj;
for (sj = 0, dj = 0; sj < sh && dj < dh; ++sj, ++dj) {
for (si = 0, di = 0; si < sw && di < dw; ++si, ++di) {
Jul 2, 2008
Jul 2, 2008
466
467
data->fb[(dj + dy) * 256 + di + dx] = 0x8000 |
((u16 *) surface->pixels)[(sj + sy) * (surface->w) + si +
Jun 19, 2008
Jun 19, 2008
468
sx];
Jul 2, 2008
Jul 2, 2008
470
}*/
Jun 17, 2008
Jun 17, 2008
471
return 0;
Jun 17, 2008
Jun 17, 2008
474
Jul 2, 2008
Jul 2, 2008
476
NDS_RenderPresent(SDL_Renderer * renderer)
Jul 2, 2008
Jul 2, 2008
478
479
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
/* Send the data to the display TODO */
480
481
482
483
484
485
486
487
488
/* Update the flipping chain, if any */
if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
data->current_screen = (data->current_screen + 1) % 2;
} else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
data->current_screen = (data->current_screen + 1) % 3;
}
}
Jul 10, 2008
Jul 10, 2008
489
490
491
492
493
494
495
496
497
498
499
500
static void
NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_SetError("Unsupported texture format");
} else {
/* TODO: free anything allocated for texture */
/*SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
SDL_FreeSurface(surface);*/
}
}
Jul 2, 2008
Jul 2, 2008
502
NDS_DestroyRenderer(SDL_Renderer * renderer)
Jul 2, 2008
Jul 2, 2008
504
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
Jul 10, 2008
Jul 10, 2008
505
506
/*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
507
508
509
int i;
if (data) {
Jul 10, 2008
Jul 10, 2008
510
511
512
for (i = 0; i < SDL_arraysize(data->texture); ++i) {
if (data->texture[i]) {
DestroyTexture(data->renderer, data->texture[i]);
Jul 10, 2008
Jul 10, 2008
514
515
516
517
518
519
520
521
522
523
}
if (data->surface.format) {
SDL_SetSurfacePalette(&data->surface, NULL);
SDL_FreeFormat(data->surface.format);
}
if (display->palette) {
SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
data);
}
SDL_FreeDirtyRects(&data->dirty);
524
525
526
527
528
529
SDL_free(data);
}
SDL_free(renderer);
}
/* vi: set ts=4 sw=4 expandtab: */