This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_ndsrender.c
361 lines (297 loc) · 9.14 KB
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
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.
20
*/
21
22
23
#include "SDL_config.h"
#if SDL_VIDEO_RENDER_NDS
24
25
26
27
28
29
30
31
32
33
34
35
36
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
#include <stdio.h>
#include <stdlib.h>
#include <nds.h>
#include <gl2d.h>
#include "SDL_video.h"
#include "../../video/SDL_sysvideo.h"
#include "SDL_render.h"
#include "../SDL_sysrender.h"
#include "SDL_log.h"
/* SDL NDS renderer implementation */
extern SDL_RenderDriver NDS_RenderDriver;
typedef struct
{
/* Whether current 3D engine is on the main or sub screen. */
int is_sub;
} NDS_RenderData;
typedef struct
{
glImage image[1];
} NDS_TextureData;
static int NDS_UpdateViewport(SDL_Renderer *renderer)
{
/* Nothing to do. */
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;
int dest_y;
if (data->is_sub) {
dest_y = dstrect->y;
} else {
dest_y = dstrect->y-SCREEN_HEIGHT;
}
if (texture->w == dstrect->w && texture->h == dstrect->h) {
/* No scaling */
glSprite(dstrect->x, dest_y, GL_FLIP_NONE, txdat->image);
} else {
/* Convert the scaling proportion into a 20.12 value. */
s32 scale_w = divf32(dstrect->w << 12, texture->w << 12);
s32 scale_h = divf32(dstrect->h << 12, texture->h << 12);
glSpriteScaleXY(dstrect->x, dest_y, scale_w, scale_h, GL_FLIP_NONE, txdat->image);
}
return 0;
}
static int NDS_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
NDS_TextureData *txdat = NULL;
int i;
SDL_Log("NDS_CreateTexture: NDS_CreateTexture.\n");
/* Sanity checks. */
for (i=0; i<NDS_RenderDriver.info.num_texture_formats; i++) {
if (texture->format == NDS_RenderDriver.info.texture_formats[i])
break;
}
if (i == NDS_RenderDriver.info.num_texture_formats) {
SDL_SetError("Unsupported texture format (%x)", texture->format);
return -1;
}
if (texture->w > NDS_RenderDriver.info.max_texture_width) {
SDL_SetError("Texture too large (%d)", texture->w);
return -1;
}
if (texture->h > NDS_RenderDriver.info.max_texture_height) {
SDL_SetError("Texture too tall (%d)", texture->h);
return -1;
}
texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
txdat = (NDS_TextureData *) texture->driverdata;
if (!txdat) {
SDL_OutOfMemory();
return -1;
}
return 0;
}
static void
NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
NDS_TextureData *txdat = texture->driverdata;
/* free anything else allocated for texture */
SDL_free(txdat);
}
133
/* size is no more than 512. */
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
static int get_gltexture_size(unsigned int size)
{
if (size > 256)
return TEXTURE_SIZE_512;
else if (size > 128)
return TEXTURE_SIZE_256;
else if (size > 64)
return TEXTURE_SIZE_128;
else if (size > 32)
return TEXTURE_SIZE_64;
else if (size > 16)
return TEXTURE_SIZE_32;
else if (size > 8)
return TEXTURE_SIZE_16;
else
return TEXTURE_SIZE_8;
}
static int NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
glLoadTileSet(txdat->image,
rect->w, rect->h,
rect->w, rect->h,
160
texture->format == SDL_PIXELFORMAT_ABGR1555 ? GL_RGBA : GL_RGB,
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
get_gltexture_size(rect->w),
get_gltexture_size(rect->h),
TEXGEN_OFF, 0, NULL,
pixels);
return 0;
}
static int NDS_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, void **pixels, int *pitch)
{
SDL_Log("enter %s (todo)\n", __func__);
return 0;
}
static void NDS_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
SDL_Log("enter %s\n", __func__);
/* stub! */
}
static int NDS_RenderClear(SDL_Renderer *renderer)
184
185
186
187
188
189
190
191
192
193
{
glClearColor(renderer->r >> 3,
renderer->g >> 3,
renderer->b >> 3,
renderer->a >> 3);
return 0;
}
static void NDS_RenderPresent(SDL_Renderer * renderer)
194
195
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
196
197
198
199
200
201
202
static int frame =0;
glEnd2D();
glFlush(0);
swiWaitForVBlank();
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
/* wait for capture unit to be ready */
while(REG_DISPCAPCNT & DCAP_ENABLE);
/* 3D engine can only work on one screen at a time. */
data->is_sub = !data->is_sub;
if (data->is_sub) {
lcdMainOnBottom();
vramSetBankC(VRAM_C_LCD);
vramSetBankD(VRAM_D_SUB_SPRITE);
REG_DISPCAPCNT = DCAP_BANK(2) | DCAP_ENABLE | DCAP_SIZE(3);
} else {
lcdMainOnTop();
vramSetBankD(VRAM_D_LCD);
vramSetBankC(VRAM_C_SUB_BG);
REG_DISPCAPCNT = DCAP_BANK(3) | DCAP_ENABLE | DCAP_SIZE(3);
}
glBegin2D();
}
static int NDS_RenderDrawPoints(SDL_Renderer *renderer, const SDL_Point *points,
int count)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
int i;
int color = RGB15(renderer->r >> 3,
renderer->g >> 3,
renderer->b >> 3);
for (i=0; i < count; i++) {
if (data->is_sub) {
glPutPixel(points[i].x, points[i].y, color);
} else {
glPutPixel(points[i].x, points[i].y - SCREEN_HEIGHT, color);
}
}
return 0;
}
static int NDS_RenderDrawLines(SDL_Renderer *renderer, const SDL_Point *points,
int count)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
int i;
int color = RGB15(renderer->r >> 3,
renderer->g >> 3,
renderer->b >> 3);
for (i=0; i < count-1; i++) {
if (data->is_sub) {
glLine(points[i].x, points[i].y, points[i+1].x, points[i+1].y, color);
} else {
glLine(points[i].x, points[i].y - SCREEN_HEIGHT,
points[i+1].x, points[i+1].y - SCREEN_HEIGHT, color);
}
}
return 0;
}
static int NDS_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects,
int count)
{
NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
int i;
int color = RGB15(renderer->r >> 3,
renderer->g >> 3,
renderer->b >> 3);
for (i=0; i<count; i++) {
if (data->is_sub) {
glBoxFilled(rects[i].x, rects[i].y,
rects[i].x + rects[i].w,
rects[i].y + rects[i].h, color);
} else {
glBoxFilled(rects[i].x, rects[i].y - SCREEN_HEIGHT,
rects[i].x + rects[i].w,
rects[i].y + rects[i].h - SCREEN_HEIGHT,
color);
}
}
return 0;
}
static SDL_Renderer *
NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayMode *displayMode = &display->current_mode;
SDL_Renderer *renderer;
NDS_RenderData *data;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (displayMode->format != SDL_PIXELFORMAT_ABGR1555) {
SDL_SetError("Unsupported pixel format (%x)", displayMode->format);
return NULL;
}
if (!SDL_PixelFormatEnumToMasks(displayMode->format, &bpp,
&Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown display format");
return NULL;
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (NDS_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) {
SDL_free(renderer);
SDL_OutOfMemory();
return NULL;
}
324
325
renderer->info = NDS_RenderDriver.info;
renderer->info.flags = SDL_RENDERER_ACCELERATED;
326
327
328
329
330
331
332
333
334
335
336
337
338
339
renderer->UpdateViewport = NDS_UpdateViewport;
renderer->CreateTexture = NDS_CreateTexture;
renderer->DestroyTexture = NDS_DestroyTexture;
renderer->RenderCopy = NDS_RenderCopy;
renderer->UpdateTexture = NDS_UpdateTexture;
renderer->LockTexture = NDS_LockTexture;
renderer->UnlockTexture = NDS_UnlockTexture;
renderer->RenderClear = NDS_RenderClear;
renderer->RenderPresent = NDS_RenderPresent;
renderer->RenderDrawPoints = NDS_RenderDrawPoints;
renderer->RenderDrawLines = NDS_RenderDrawLines;
renderer->RenderFillRects = NDS_RenderFillRects;
340
341
renderer->driverdata = data;
342
343
344
345
346
347
348
349
return renderer;
}
SDL_RenderDriver NDS_RenderDriver = {
.CreateRenderer = NDS_CreateRenderer,
.info = {
.name = "nds",
.flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC,
350
.num_texture_formats = 2,
351
352
353
354
355
356
357
358
.texture_formats = { [0] = SDL_PIXELFORMAT_ABGR1555,
[1] = SDL_PIXELFORMAT_BGR555,
},
.max_texture_width = 512,
.max_texture_height = 512,
}
};
359
360
#endif /* SDL_VIDEO_RENDER_NDS */
361
/* vi: set ts=4 sw=4 expandtab: */