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

Latest commit

 

History

History
executable file
·
401 lines (320 loc) · 10 KB

SDL_ndsvideo.c

File metadata and controls

executable file
·
401 lines (320 loc) · 10 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Jun 22, 2011
Jun 22, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
Oct 31, 2011
Oct 31, 2011
23
24
#if SDL_VIDEO_DRIVER_NDS
Jun 22, 2011
Jun 22, 2011
25
26
27
28
29
30
31
32
33
34
35
/* SDL Nintendo DS video driver implementation */
#include <stdio.h>
#include <stdlib.h>
#include <nds.h>
#include <fat.h>
#include "SDL_video.h"
#include "SDL_ndsvideo.h"
#include "SDL_ndsevents_c.h"
#include "../../render/SDL_sysrender.h"
Feb 13, 2012
Feb 13, 2012
36
#include "../../render/nds/SDL_libgl2D.h"
Jun 22, 2011
Jun 22, 2011
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
222
223
224
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
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
#include "SDL_log.h"
#define NDSVID_DRIVER_NAME "nds"
static SDL_DisplayMode display_modes[] =
{
/* Only one screen */
{
.format = SDL_PIXELFORMAT_ABGR1555,
.w = SCREEN_WIDTH,
.h = SCREEN_HEIGHT,
.refresh_rate = 60,
},
/* Aggregated display (two screens) with no gap. */
{
.format = SDL_PIXELFORMAT_ABGR1555,
.w = SCREEN_WIDTH,
.h = 2*SCREEN_HEIGHT+SCREEN_GAP,
.refresh_rate = 60,
},
/* Aggregated display (two screens) with a gap. */
{
.format = SDL_PIXELFORMAT_ABGR1555,
.w = SCREEN_WIDTH,
.h = 2*SCREEN_HEIGHT,
.refresh_rate = 60,
},
/* Last entry */
{
.w = 0,
}
};
/* This function must not be optimized nor inlined, else the pointer
* to the message will be in the wrong register, and the emulator won't
* find the string. */
__attribute__ ((noinline, optimize (0)))
static void NDS_DebugOutput2(const char* message)
{
#ifdef __thumb__
asm volatile ("swi #0xfc");
#else
asm volatile ("swi #0xfc0000");
#endif
}
static void NDS_DebugOutput(void *userdata, int category, SDL_LogPriority priority, const char *message)
{
NDS_DebugOutput2(message);
}
/* SDL NDS driver bootstrap functions */
static int NDS_Available(void)
{
return 1; /* always here */
}
#ifndef USE_HW_RENDERER
static int NDS_CreateWindowFramebuffer(_THIS, SDL_Window *window,
Uint32 *format, void **pixels,
int *pitch)
{
struct NDS_WindowData *wdata;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
const SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
const SDL_DisplayMode *mode = display->driverdata;
const Uint32 fmt = mode->format;
if (fmt != SDL_PIXELFORMAT_ABGR1555) {
SDL_SetError("Unsupported pixel format (%x)", fmt);
return -1;
}
if (!SDL_PixelFormatEnumToMasks
(fmt, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
wdata = SDL_calloc(1, sizeof(struct NDS_WindowData));
if (!wdata) {
SDL_OutOfMemory();
return -1;
}
if (bpp == 8) {
wdata->pixels_length = (SCREEN_HEIGHT+SCREEN_GAP+SCREEN_HEIGHT)*SCREEN_WIDTH;
} else {
wdata->pixels_length = (SCREEN_HEIGHT+SCREEN_GAP+SCREEN_HEIGHT)*SCREEN_WIDTH*2;
}
wdata->pixels = SDL_calloc(1, wdata->pixels_length);
if (!wdata->pixels) {
SDL_free(wdata);
SDL_SetError("Not enough memory");
return -1;
}
if (bpp == 8) {
wdata->main.bg_id = bgInit(2, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
wdata->sub.bg_id = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
wdata->main.length = SCREEN_HEIGHT*SCREEN_WIDTH;
wdata->main.pixels = wdata->pixels;
wdata->sub.length = SCREEN_HEIGHT*SCREEN_WIDTH;
wdata->sub.pixels = (u8 *)wdata->pixels + wdata->main.length; /* or ...+SCREEN_GAP */
} else {
wdata->main.bg_id = bgInit(2, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
wdata->sub.bg_id = bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
wdata->main.length = SCREEN_HEIGHT*SCREEN_WIDTH*2;
wdata->main.pixels = wdata->pixels;
wdata->sub.length = SCREEN_HEIGHT*SCREEN_WIDTH*2;
wdata->sub.pixels = (u8 *)wdata->pixels + wdata->main.length; /* or ...+SCREEN_GAP */
}
wdata->pitch = (window->w) * ((bpp+1) / 8);
wdata->bpp = bpp;
wdata->rotate = 0;
wdata->scale.x = 0x100;
wdata->scale.y = 0x100;
wdata->scroll.x = 0;
wdata->scroll.y = 0;
wdata->main.vram_pixels = bgGetGfxPtr(wdata->main.bg_id);
wdata->sub.vram_pixels = bgGetGfxPtr(wdata->sub.bg_id);
#if 0
bgSetCenter(wdata->main.bg_id, 0, 0);
bgSetRotateScale(wdata->main.bg_id, wdata->rotate, wdata->scale.x,
wdata->scale.y);
bgSetScroll(wdata->main.bg_id, wdata->scroll.x, wdata->scroll.y);
#endif
#if 0
bgSetCenter(wdata->sub.bg_id, 0, 0);
bgSetRotateScale(wdata->sub.bg_id, wdata->rotate, wdata->scale.x,
wdata->scale.y);
bgSetScroll(wdata->sub.bg_id, wdata->scroll.x, wdata->scroll.y);
#endif
bgUpdate();
*format = fmt;
*pixels = wdata->pixels;
*pitch = wdata->pitch;
window->driverdata = wdata;
return 0;
}
static int NDS_UpdateWindowFramebuffer(_THIS, SDL_Window * window,
SDL_Rect * rects, int numrects)
{
struct NDS_WindowData *wdata = window->driverdata;
/* Copy everything. TODO: use rects/numrects. */
DC_FlushRange(wdata->pixels, wdata->pixels_length);
swiWaitForVBlank();
dmaCopy(wdata->main.pixels, wdata->main.vram_pixels, wdata->main.length);
dmaCopy(wdata->sub.pixels, wdata->sub.vram_pixels, wdata->sub.length);
return 0;
}
static void NDS_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
{
struct NDS_WindowData *wdata = window->driverdata;
SDL_free(wdata->pixels);
SDL_free(wdata);
}
#endif
#ifdef USE_HW_RENDERER
/* Set up a 2D layer construced of bitmap sprites. This holds the
* image when rendering to the top screen. From libnds example.
*/
static void initSubSprites(void)
{
oamInit(&oamSub, SpriteMapping_Bmp_2D_256, false);
int x = 0;
int y = 0;
int id = 0;
//set up a 4x3 grid of 64x64 sprites to cover the screen
for(y = 0; y < 3; y++)
for(x = 0; x < 4; x++)
{
oamSub.oamMemory[id].attribute[0] = ATTR0_BMP | ATTR0_SQUARE | (64 * y);
oamSub.oamMemory[id].attribute[1] = ATTR1_SIZE_64 | (64 * x);
oamSub.oamMemory[id].attribute[2] = ATTR2_ALPHA(1) | (8 * 32 * y) | (8 * x);
id++;
}
swiWaitForVBlank();
oamUpdate(&oamSub);
}
#endif
static int NDS_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
{
display->driverdata = mode->driverdata;
powerOn(POWER_ALL_2D);
#ifdef USE_HW_RENDERER
videoSetMode(MODE_5_3D);
videoSetModeSub(MODE_5_2D);
/* initialize gl2d */
glScreen2D();
glBegin2D();
vramSetBankA(VRAM_A_TEXTURE);
vramSetBankB(VRAM_B_TEXTURE );
vramSetBankC(VRAM_C_SUB_BG_0x06200000);
vramSetBankE(VRAM_E_TEX_PALETTE);
// sub sprites hold the bottom image when 3D directed to top
initSubSprites();
// sub background holds the top image when 3D directed to bottom
bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
#else
/* Select mode 5 for both screens. Can do Extended Rotation
* Background on both (BG 2 and 3). */
videoSetMode(MODE_5_2D);
videoSetModeSub(MODE_5_2D);
vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
vramSetBankB(VRAM_B_TEXTURE );
vramSetBankC(VRAM_C_SUB_BG_0x06200000);
vramSetBankE(VRAM_E_TEX_PALETTE);
#endif
return 0;
}
void NDS_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{
SDL_DisplayMode *mode;
for (mode = display_modes; mode->w; mode++) {
mode->driverdata = mode; /* point back to self */
SDL_AddDisplayMode(display, mode);
}
}
static int NDS_VideoInit(_THIS)
{
SDL_VideoDisplay display;
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_UNKNOWN; // should be SDL_PIXELFORMAT_ABGR1555;
mode.w = SCREEN_WIDTH;
mode.h = 2*SCREEN_HEIGHT+SCREEN_GAP;
mode.refresh_rate = 60;
SDL_zero(display);
display.desktop_mode = mode;
SDL_AddVideoDisplay(&display);
return 0;
}
static void NDS_VideoQuit(_THIS)
{
videoSetMode(DISPLAY_SCREEN_OFF);
videoSetModeSub(DISPLAY_SCREEN_OFF);
vramSetBankA(VRAM_A_LCD);
vramSetBankB(VRAM_B_LCD);
vramSetBankC(VRAM_C_LCD);
vramSetBankD(VRAM_D_LCD);
vramSetBankE(VRAM_E_LCD);
vramSetBankF(VRAM_F_LCD);
vramSetBankG(VRAM_G_LCD);
vramSetBankH(VRAM_H_LCD);
vramSetBankI(VRAM_I_LCD);
}
static void NDS_DeleteDevice(SDL_VideoDevice * device)
{
SDL_free(device);
}
static SDL_VideoDevice *NDS_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
fatInitDefault();
/* Initialize all variables that we clean on shutdown */
device = SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_OutOfMemory();
return NULL;
}
device->driverdata = SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_free(device);
SDL_OutOfMemory();
return NULL;
}
/* Set the function pointers */
device->VideoInit = NDS_VideoInit;
device->VideoQuit = NDS_VideoQuit;
device->GetDisplayModes = NDS_GetDisplayModes;
device->SetDisplayMode = NDS_SetDisplayMode;
device->CreateWindow = NDS_CreateWindow;
#ifndef USE_HW_RENDERER
device->CreateWindowFramebuffer = NDS_CreateWindowFramebuffer;
device->UpdateWindowFramebuffer = NDS_UpdateWindowFramebuffer;
device->DestroyWindowFramebuffer = NDS_DestroyWindowFramebuffer;
#endif
device->PumpEvents = NDS_PumpEvents;
device->free = NDS_DeleteDevice;
/* Set the debug output. Use only under an emulator. Will crash the DS. */
#if 0
SDL_LogSetOutputFunction(NDS_DebugOutput, NULL);
#endif
return device;
}
VideoBootStrap NDS_bootstrap = {
NDSVID_DRIVER_NAME, "SDL NDS video driver",
NDS_Available, NDS_CreateDevice
};
double SDLCALL SDL_pow(double x, double y)
{
static int once = 1;
if (once) {
SDL_Log("SDL_pow called but not supported on this platform");
once = 0;
}
return 0;
}
Oct 31, 2011
Oct 31, 2011
399
400
#endif /* SDL_VIDEO_DRIVER_NDS */
Jun 22, 2011
Jun 22, 2011
401
/* vi: set ts=4 sw=4 expandtab: */