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

Latest commit

 

History

History
522 lines (456 loc) · 16.7 KB

SDL_ndsrender.c

File metadata and controls

522 lines (456 loc) · 16.7 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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>
Feb 18, 2009
Feb 18, 2009
26
27
28
//#include <nds/arm9/video.h>
//#include <nds/arm9/sprite.h>
//#include <nds/arm9/trig_lut.h>
29
30
31
32
33
#include "SDL_config.h"
#include "SDL_video.h"
#include "../SDL_sysvideo.h"
Feb 12, 2011
Feb 12, 2011
34
35
#include "SDL_render.h"
#include "../../render/SDL_sysrender.h"
36
37
38
39
40
41
42
/* SDL NDS renderer implementation */
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);
Feb 12, 2011
Feb 12, 2011
43
#if 0
44
45
46
static int NDS_QueryTexturePixels(SDL_Renderer * renderer,
SDL_Texture * texture, void **pixels,
int *pitch);
Feb 12, 2011
Feb 12, 2011
47
#endif
48
49
50
51
52
53
54
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);
Feb 12, 2011
Feb 12, 2011
55
56
static int NDS_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count);
57
58
59
60
61
62
63
64
65
66
67
68
static int NDS_RenderCopy(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
static void NDS_RenderPresent(SDL_Renderer * renderer);
static void NDS_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void NDS_DestroyRenderer(SDL_Renderer * renderer);
SDL_RenderDriver NDS_RenderDriver = {
NDS_CreateRenderer,
{"nds", /* char* name */
Feb 1, 2011
Feb 1, 2011
69
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), /* u32 flags */
Feb 2, 2011
Feb 2, 2011
70
2, /* u32 num_texture_formats */
71
72
73
{
SDL_PIXELFORMAT_ABGR1555,
SDL_PIXELFORMAT_BGR555,
May 23, 2009
May 23, 2009
74
}, /* u32 texture_formats[20] */
75
76
77
78
79
80
81
82
(256), /* int max_texture_width */
(256), /* int max_texture_height */
}
};
typedef struct
{
u8 bg_taken[4];
Feb 18, 2009
Feb 18, 2009
83
OamState *oam;
84
85
86
87
88
89
90
int sub;
} NDS_RenderData;
typedef struct
{
enum
{ NDSTX_BG, NDSTX_SPR } type; /* represented in a bg or sprite. */
Feb 18, 2009
Feb 18, 2009
91
92
int hw_index; /* index of sprite in OAM or bg from libnds */
int pitch, bpp; /* useful information about the texture */
May 23, 2009
May 23, 2009
93
94
95
96
97
98
99
100
struct
{
int x, y;
} scale; /* x/y stretch (24.8 fixed point) */
struct
{
int x, y;
} scroll; /* x/y offset */
Feb 18, 2009
Feb 18, 2009
101
int rotate; /* -32768 to 32767, texture rotation */
102
103
104
105
106
107
108
109
110
111
u16 *vram_pixels; /* where the pixel data is stored (a pointer into VRAM) */
u16 *vram_palette; /* where the palette data is stored if it's indexed. */
/*int size; */
} NDS_TextureData;
SDL_Renderer *
NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
{
Feb 10, 2011
Feb 10, 2011
112
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
SDL_DisplayMode *displayMode = &display->current_mode;
SDL_Renderer *renderer;
NDS_RenderData *data;
int i, n;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (!SDL_PixelFormatEnumToMasks(displayMode->format, &bpp,
&Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown display format");
return NULL;
}
switch (displayMode->format) {
case SDL_PIXELFORMAT_ABGR1555:
case SDL_PIXELFORMAT_BGR555:
/* okay */
break;
case SDL_PIXELFORMAT_RGB555:
case SDL_PIXELFORMAT_RGB565:
case SDL_PIXELFORMAT_ARGB1555:
/* we'll take these too for now */
break;
default:
SDL_SetError("Warning: wrong display format for NDS!\n");
break;
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (NDS_RenderData *) SDL_malloc(sizeof(*data));
if (!data) {
NDS_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
SDL_zerop(data);
Feb 12, 2011
Feb 12, 2011
154
renderer->RenderFillRects = NDS_RenderFillRects;
155
156
157
158
159
renderer->RenderCopy = NDS_RenderCopy;
renderer->RenderPresent = NDS_RenderPresent;
renderer->DestroyRenderer = NDS_DestroyRenderer;
renderer->info.name = NDS_RenderDriver.info.name;
renderer->info.flags = 0;
Jan 21, 2010
Jan 21, 2010
160
renderer->window = window;
161
162
renderer->driverdata = data;
renderer->CreateTexture = NDS_CreateTexture;
Feb 12, 2011
Feb 12, 2011
163
// renderer->QueryTexturePixels = NDS_QueryTexturePixels;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
renderer->UpdateTexture = NDS_UpdateTexture;
renderer->LockTexture = NDS_LockTexture;
renderer->UnlockTexture = NDS_UnlockTexture;
renderer->DestroyTexture = NDS_DestroyTexture;
renderer->info.num_texture_formats =
NDS_RenderDriver.info.num_texture_formats;
SDL_memcpy(renderer->info.texture_formats,
NDS_RenderDriver.info.texture_formats,
sizeof(renderer->info.texture_formats));
renderer->info.max_texture_width =
NDS_RenderDriver.info.max_texture_width;
renderer->info.max_texture_height =
NDS_RenderDriver.info.max_texture_height;
May 23, 2009
May 23, 2009
179
180
181
data->sub = 0; /* TODO: this is hard-coded to the "main" screen.
figure out how to detect whether to set it to
"sub" screen. window->id, perhaps? */
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
data->bg_taken[2] = data->bg_taken[3] = 0;
return renderer;
}
static int
NDS_ActivateRenderer(SDL_Renderer * renderer)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
return 0;
}
static int
NDS_DisplayModeChanged(SDL_Renderer * renderer)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
return 0;
}
static int
NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
NDS_TextureData *txdat = NULL;
int i;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (!SDL_PixelFormatEnumToMasks
(texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
/* conditional statements on w/h to place it as bg/sprite
depending on which one it fits. */
if (texture->w <= 64 && texture->h <= 64) {
int whichspr = -1;
Feb 18, 2009
Feb 18, 2009
222
printf("NDS_CreateTexture: Tried to make a sprite.\n");
223
txdat->type = NDSTX_SPR;
Feb 18, 2009
Feb 18, 2009
224
#if 0
225
226
227
228
229
230
231
232
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
for (i = 0; i < SPRITE_COUNT; ++i) {
if (data->oam_copy.spriteBuffer[i].attribute[0] & ATTR0_DISABLED) {
whichspr = i;
break;
}
}
if (whichspr >= 0) {
SpriteEntry *sprent = &(data->oam_copy.spriteBuffer[whichspr]);
int maxside = texture->w > texture->h ? texture->w : texture->h;
int pitch;
texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
txdat = (NDS_TextureData *) texture->driverdata;
if (!txdat) {
SDL_OutOfMemory();
return -1;
}
sprent->objMode = OBJMODE_BITMAP;
sprent->posX = 0;
sprent->posY = 0;
sprent->colMode = OBJCOLOR_16; /* OBJCOLOR_256 for INDEX8 */
/* the first 32 sprites get transformation matrices.
first come, first served */
if (whichspr < MATRIX_COUNT) {
sprent->isRotoscale = 1;
sprent->rsMatrixIdx = whichspr;
}
/* containing shape (square or 2:1 rectangles) */
sprent->objShape = OBJSHAPE_SQUARE;
if (texture->w / 2 >= texture->h) {
sprent->objShape = OBJSHAPE_WIDE;
} else if (texture->h / 2 >= texture->w) {
sprent->objShape = OBJSHAPE_TALL;
}
/* size in pixels */
/* FIXME: "pitch" is hardcoded for 2bytes per pixel. */
sprent->objSize = OBJSIZE_64;
pitch = 128;
if (maxside <= 8) {
sprent->objSize = OBJSIZE_8;
pitch = 16;
} else if (maxside <= 16) {
sprent->objSize = OBJSIZE_16;
pitch = 32;
} else if (maxside <= 32) {
sprent->objSize = OBJSIZE_32;
pitch = 64;
}
/* FIXME: this is hard-coded and will obviously only work for one
sprite-texture. tells it to look at the beginning of SPRITE_GFX
for its pixels. */
sprent->tileIdx = 0;
/* now for the texture data */
txdat->type = NDSTX_SPR;
txdat->hw_index = whichspr;
txdat->dim.hdx = 0x100;
txdat->dim.hdy = 0;
txdat->dim.vdx = 0;
txdat->dim.vdy = 0x100;
txdat->dim.pitch = pitch;
txdat->dim.bpp = bpp;
May 23, 2009
May 23, 2009
292
293
txdat->vram_pixels =
(u16 *) (data->sub ? SPRITE_GFX_SUB : SPRITE_GFX);
Feb 18, 2009
Feb 18, 2009
294
/* FIXME: use tileIdx*boundary
May 23, 2009
May 23, 2009
295
to point to proper location */
296
297
298
} else {
SDL_SetError("Out of NDS sprites.");
}
Feb 18, 2009
Feb 18, 2009
299
#endif
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
} else if (texture->w <= 256 && texture->h <= 256) {
int whichbg = -1, base = 0;
if (!data->bg_taken[2]) {
whichbg = 2;
} else if (!data->bg_taken[3]) {
whichbg = 3;
base = 4;
}
if (whichbg >= 0) {
texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
txdat = (NDS_TextureData *) texture->driverdata;
if (!txdat) {
SDL_OutOfMemory();
return -1;
}
Feb 18, 2009
Feb 18, 2009
315
316
// hard-coded for 256x256 for now...
// TODO: a series of if-elseif-else's to find the closest but larger size.
May 23, 2009
May 23, 2009
317
318
319
320
if (!data->sub) {
if (bpp == 8) {
txdat->hw_index =
bgInit(whichbg, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
Feb 18, 2009
Feb 18, 2009
321
} else {
May 23, 2009
May 23, 2009
322
323
324
txdat->hw_index =
bgInit(whichbg, BgType_Bmp16, BgSize_B16_256x256, 0,
0);
Feb 18, 2009
Feb 18, 2009
325
326
}
} else {
May 23, 2009
May 23, 2009
327
328
329
330
if (bpp == 8) {
txdat->hw_index =
bgInitSub(whichbg, BgType_Bmp8, BgSize_B8_256x256, 0,
0);
Feb 18, 2009
Feb 18, 2009
331
} else {
May 23, 2009
May 23, 2009
332
333
334
txdat->hw_index =
bgInitSub(whichbg, BgType_Bmp16, BgSize_B16_256x256,
0, 0);
Feb 18, 2009
Feb 18, 2009
335
336
}
}
Feb 18, 2009
Feb 18, 2009
338
339
340
341
342
343
344
/* useful functions
bgGetGfxPtr(bg3);
bgSetCenter(bg3, rcX, rcY);
bgSetRotateScale(bg3, angle, scaleX, scaleY);
bgSetScroll(bg3, scrollX, scrollY);
bgUpdate(bg3);
*/
345
txdat->type = NDSTX_BG;
Feb 12, 2011
Feb 12, 2011
346
txdat->pitch = (texture->w) * ((bpp+1) / 8);
Feb 18, 2009
Feb 18, 2009
347
348
349
350
351
352
txdat->bpp = bpp;
txdat->rotate = 0;
txdat->scale.x = 0x100;
txdat->scale.y = 0x100;
txdat->scroll.x = 0;
txdat->scroll.y = 0;
May 23, 2009
May 23, 2009
353
txdat->vram_pixels = (u16 *) bgGetGfxPtr(txdat->hw_index);
Feb 18, 2009
Feb 18, 2009
354
355
356
357
358
bgSetCenter(txdat->hw_index, 0, 0);
bgSetRotateScale(txdat->hw_index, txdat->rotate, txdat->scale.x,
txdat->scale.y);
bgSetScroll(txdat->hw_index, txdat->scroll.x, txdat->scroll.y);
Feb 12, 2011
Feb 12, 2011
359
bgUpdate();
Feb 18, 2009
Feb 18, 2009
360
361
data->bg_taken[whichbg] = 1;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*txdat->size = txdat->dim.pitch * texture->h; */
} else {
SDL_SetError("Out of NDS backgrounds.");
}
} else {
SDL_SetError("Texture too big for NDS hardware.");
}
if (!texture->driverdata) {
return -1;
}
return 0;
}
Feb 12, 2011
Feb 12, 2011
377
#if 0
378
379
380
381
382
383
static int
NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
void **pixels, int *pitch)
{
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
*pixels = txdat->vram_pixels;
Feb 18, 2009
Feb 18, 2009
384
*pitch = txdat->pitch;
385
386
return 0;
}
Feb 12, 2011
Feb 12, 2011
387
#endif
388
389
390
391
392
393
394
395
396
397
398
399
400
401
static int
NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
NDS_TextureData *txdat;
Uint8 *src, *dst;
int row;
size_t length;
txdat = (NDS_TextureData *) texture->driverdata;
src = (Uint8 *) pixels;
dst =
Feb 18, 2009
Feb 18, 2009
402
403
404
(Uint8 *) txdat->vram_pixels + rect->y * txdat->pitch + rect->x *
((txdat->bpp + 1) / 8);
length = rect->w * ((txdat->bpp + 1) / 8);
405
406
407
408
409
410
411
if (rect->w == texture->w) {
dmaCopy(src, dst, length * rect->h);
} else {
for (row = 0; row < rect->h; ++row) {
dmaCopy(src, dst, length);
src += pitch;
Feb 18, 2009
Feb 18, 2009
412
dst += txdat->pitch;
413
414
415
416
417
418
419
420
421
422
423
424
425
}
}
return 0;
}
static int
NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
{
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
Feb 18, 2009
Feb 18, 2009
426
427
428
*pixels = (void *) ((u8 *) txdat->vram_pixels + rect->y * txdat->pitch +
rect->x * ((txdat->bpp + 1) / 8));
*pitch = txdat->pitch;
429
430
431
432
433
434
435
436
437
438
439
return 0;
}
static void
NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
/* stub! */
}
static int
Feb 12, 2011
Feb 12, 2011
440
441
NDS_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count)
442
443
444
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
Feb 18, 2009
Feb 18, 2009
445
printf("NDS_RenderFill: stub\n");
Feb 12, 2011
Feb 12, 2011
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* TODO: make a single-color sprite and stretch it.
calculate the "HDX" width modifier of the sprite by:
let S be the actual sprite's width (like, 32 pixels for example)
let R be the rectangle's width (maybe 50 pixels)
HDX = (R<<8) / S;
(it's fixed point, hence the bit shift. same goes for vertical.
be sure to use 32-bit int's for the bit shift before the division!)
*/
return 0;
}
static int
NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
Jan 21, 2010
Jan 21, 2010
465
SDL_Window *window = renderer->window;
Feb 10, 2011
Feb 10, 2011
466
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
Feb 18, 2009
Feb 18, 2009
467
int Bpp = SDL_BYTESPERPIXEL(texture->format);
468
469
if (txdat->type == NDSTX_BG) {
Feb 18, 2009
Feb 18, 2009
470
471
txdat->scroll.x = dstrect->x;
txdat->scroll.y = dstrect->y;
472
473
} else {
/* sprites not fully implemented yet */
Feb 18, 2009
Feb 18, 2009
474
475
476
477
478
479
printf("NDS_RenderCopy: used sprite!\n");
// SpriteEntry *spr = &(data->oam_copy.spriteBuffer[txdat->hw_index]);
// spr->posX = dstrect->x;
// spr->posY = dstrect->y;
// if (txdat->hw_index < MATRIX_COUNT && spr->isRotoscale) {
// }
480
481
482
483
484
485
486
487
488
489
}
return 0;
}
static void
NDS_RenderPresent(SDL_Renderer * renderer)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
490
SDL_Window *window = renderer->window;
Feb 10, 2011
Feb 10, 2011
491
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
492
493
/* update sprites */
Feb 18, 2009
Feb 18, 2009
494
// NDS_OAM_Update(&(data->oam_copy), data->sub);
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* vsync for NDS */
if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
swiWaitForVBlank();
}
}
static void
NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
NDS_TextureData *txdat = texture->driverdata;
/* free anything else allocated for texture */
SDL_free(txdat);
}
static void
NDS_DestroyRenderer(SDL_Renderer * renderer)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
int i;
if (data) {
/* free anything else relevant if anything else is allocated. */
SDL_free(data);
}
SDL_free(renderer);
}
/* vi: set ts=4 sw=4 expandtab: */