Mercurial > sdl-ios-xcode
comparison src/video/dc/SDL_dcvideo.c @ 509:dad72daf44b3
Added initial support for Dreamcast (thanks HERO!)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 05 Oct 2002 16:50:56 +0000 |
parents | |
children | b8d311d90021 |
comparison
equal
deleted
inserted
replaced
508:9ff7e90aaa94 | 509:dad72daf44b3 |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 BERO | |
20 bero@geocities.co.jp | |
21 | |
22 based on SDL_nullvideo.c by | |
23 | |
24 Sam Lantinga | |
25 slouken@libsdl.org | |
26 */ | |
27 | |
28 #ifdef SAVE_RCSID | |
29 static char rcsid = | |
30 "@(#) $Id$"; | |
31 #endif | |
32 | |
33 /* Dummy SDL video driver implementation; this is just enough to make an | |
34 * SDL-based application THINK it's got a working video driver, for | |
35 * applications that call SDL_Init(SDL_INIT_VIDEO) when they don't need it, | |
36 * and also for use as a collection of stubs when porting SDL to a new | |
37 * platform for which you haven't yet written a valid video driver. | |
38 * | |
39 * This is also a great way to determine bottlenecks: if you think that SDL | |
40 * is a performance problem for a given platform, enable this driver, and | |
41 * then see if your application runs faster without video overhead. | |
42 * | |
43 * Initial work by Ryan C. Gordon (icculus@linuxgames.com). A good portion | |
44 * of this was cut-and-pasted from Stephane Peter's work in the AAlib | |
45 * SDL video driver. Renamed to "DC" by Sam Lantinga. | |
46 */ | |
47 | |
48 #include <stdio.h> | |
49 #include <stdlib.h> | |
50 #include <string.h> | |
51 | |
52 #include "SDL.h" | |
53 #include "SDL_error.h" | |
54 #include "SDL_video.h" | |
55 #include "SDL_mouse.h" | |
56 #include "SDL_sysvideo.h" | |
57 #include "SDL_pixels_c.h" | |
58 #include "SDL_events_c.h" | |
59 | |
60 #include "SDL_dcvideo.h" | |
61 #include "SDL_dcevents_c.h" | |
62 #include "SDL_dcmouse_c.h" | |
63 | |
64 #include <dc/video.h> | |
65 #include <dc/pvr.h> | |
66 | |
67 #ifdef HAVE_OPENGL | |
68 #include <GL/gl.h> | |
69 #endif | |
70 | |
71 | |
72 /* Initialization/Query functions */ | |
73 static int DC_VideoInit(_THIS, SDL_PixelFormat *vformat); | |
74 static SDL_Rect **DC_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); | |
75 static SDL_Surface *DC_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); | |
76 static int DC_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors); | |
77 static void DC_VideoQuit(_THIS); | |
78 | |
79 /* Hardware surface functions */ | |
80 static int DC_AllocHWSurface(_THIS, SDL_Surface *surface); | |
81 static int DC_LockHWSurface(_THIS, SDL_Surface *surface); | |
82 static void DC_UnlockHWSurface(_THIS, SDL_Surface *surface); | |
83 static void DC_FreeHWSurface(_THIS, SDL_Surface *surface); | |
84 static int DC_FlipHWSurface(_THIS, SDL_Surface *surface); | |
85 | |
86 /* etc. */ | |
87 static void DC_UpdateRects(_THIS, int numrects, SDL_Rect *rects); | |
88 | |
89 /* OpenGL */ | |
90 static void *DC_GL_GetProcAddress(_THIS, const char *proc); | |
91 static int DC_GL_LoadLibrary(_THIS, const char *path); | |
92 static int DC_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value); | |
93 static void DC_GL_SwapBuffers(_THIS); | |
94 | |
95 /* DC driver bootstrap functions */ | |
96 | |
97 static int DC_Available(void) | |
98 { | |
99 return 1; | |
100 } | |
101 | |
102 static void DC_DeleteDevice(SDL_VideoDevice *device) | |
103 { | |
104 free(device->hidden); | |
105 free(device); | |
106 } | |
107 | |
108 static SDL_VideoDevice *DC_CreateDevice(int devindex) | |
109 { | |
110 SDL_VideoDevice *device; | |
111 | |
112 /* Initialize all variables that we clean on shutdown */ | |
113 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice)); | |
114 if ( device ) { | |
115 memset(device, 0, (sizeof *device)); | |
116 device->hidden = (struct SDL_PrivateVideoData *) | |
117 malloc((sizeof *device->hidden)); | |
118 } | |
119 if ( (device == NULL) || (device->hidden == NULL) ) { | |
120 SDL_OutOfMemory(); | |
121 if ( device ) { | |
122 free(device); | |
123 } | |
124 return(0); | |
125 } | |
126 memset(device->hidden, 0, (sizeof *device->hidden)); | |
127 | |
128 /* Set the function pointers */ | |
129 device->VideoInit = DC_VideoInit; | |
130 device->ListModes = DC_ListModes; | |
131 device->SetVideoMode = DC_SetVideoMode; | |
132 device->CreateYUVOverlay = NULL; | |
133 device->SetColors = DC_SetColors; | |
134 device->UpdateRects = DC_UpdateRects; | |
135 device->VideoQuit = DC_VideoQuit; | |
136 device->AllocHWSurface = DC_AllocHWSurface; | |
137 device->CheckHWBlit = NULL; | |
138 device->FillHWRect = NULL; | |
139 device->SetHWColorKey = NULL; | |
140 device->SetHWAlpha = NULL; | |
141 device->LockHWSurface = DC_LockHWSurface; | |
142 device->UnlockHWSurface = DC_UnlockHWSurface; | |
143 device->FlipHWSurface = DC_FlipHWSurface; | |
144 device->FreeHWSurface = DC_FreeHWSurface; | |
145 #ifdef HAVE_OPENGL | |
146 device->GL_LoadLibrary = DC_GL_LoadLibrary; | |
147 device->GL_GetProcAddress = DC_GL_GetProcAddress; | |
148 device->GL_GetAttribute = DC_GL_GetAttribute; | |
149 device->GL_MakeCurrent = NULL; | |
150 device->GL_SwapBuffers = DC_GL_SwapBuffers; | |
151 #endif | |
152 device->SetCaption = NULL; | |
153 device->SetIcon = NULL; | |
154 device->IconifyWindow = NULL; | |
155 device->GrabInput = NULL; | |
156 device->GetWMInfo = NULL; | |
157 device->InitOSKeymap = DC_InitOSKeymap; | |
158 device->PumpEvents = DC_PumpEvents; | |
159 | |
160 device->free = DC_DeleteDevice; | |
161 | |
162 return device; | |
163 } | |
164 | |
165 VideoBootStrap DC_bootstrap = { | |
166 "dcvideo", "Dreamcast Video", | |
167 DC_Available, DC_CreateDevice | |
168 }; | |
169 | |
170 | |
171 int DC_VideoInit(_THIS, SDL_PixelFormat *vformat) | |
172 { | |
173 /* Determine the screen depth (use default 8-bit depth) */ | |
174 /* we change this during the SDL_SetVideoMode implementation... */ | |
175 vformat->BitsPerPixel = 16; | |
176 vformat->Rmask = 0x0000f800; | |
177 vformat->Gmask = 0x000007e0; | |
178 vformat->Bmask = 0x0000001f; | |
179 | |
180 /* We're done! */ | |
181 return(0); | |
182 } | |
183 | |
184 const static SDL_Rect | |
185 RECT_800x600 = {0,0,800,600}, | |
186 RECT_640x480 = {0,0,640,480}, | |
187 RECT_320x240 = {0,0,320,240}; | |
188 const static SDL_Rect *vid_modes[] = { | |
189 &RECT_800x600, | |
190 &RECT_640x480, | |
191 &RECT_320x240, | |
192 NULL | |
193 }; | |
194 | |
195 SDL_Rect **DC_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) | |
196 { | |
197 switch(format->BitsPerPixel) { | |
198 case 15: | |
199 case 16: | |
200 return &vid_modes; | |
201 case 32: | |
202 if (!(flags & SDL_OPENGL)) | |
203 return &vid_modes; | |
204 default: | |
205 return NULL; | |
206 } | |
207 // return (SDL_Rect **) -1; | |
208 } | |
209 | |
210 pvr_init_params_t params = { | |
211 /* Enable opaque and translucent polygons with size 16 */ | |
212 { PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16 }, | |
213 | |
214 /* Vertex buffer size */ | |
215 512*1024 | |
216 }; | |
217 | |
218 #ifdef HAVE_OPENGL | |
219 static int pvr_inited; | |
220 #endif | |
221 | |
222 SDL_Surface *DC_SetVideoMode(_THIS, SDL_Surface *current, | |
223 int width, int height, int bpp, Uint32 flags) | |
224 { | |
225 int disp_mode,pixel_mode,pitch; | |
226 Uint32 Rmask, Gmask, Bmask; | |
227 | |
228 if (width==320 && height==240) disp_mode=DM_320x240; | |
229 else if (width==640 && height==480) disp_mode=DM_640x480; | |
230 else if (width==800 && height==600) disp_mode=DM_800x608; | |
231 else { | |
232 SDL_SetError("Couldn't find requested mode in list"); | |
233 return(NULL); | |
234 } | |
235 | |
236 switch(bpp) { | |
237 case 15: pixel_mode = PM_RGB555; pitch = width*2; | |
238 /* 5-5-5 */ | |
239 Rmask = 0x00007c00; | |
240 Gmask = 0x000003e0; | |
241 Bmask = 0x0000001f; | |
242 break; | |
243 case 16: pixel_mode = PM_RGB565; pitch = width*2; | |
244 /* 5-6-5 */ | |
245 Rmask = 0x0000f800; | |
246 Gmask = 0x000007e0; | |
247 Bmask = 0x0000001f; | |
248 break; | |
249 case 24: bpp = 32; | |
250 case 32: pixel_mode = PM_RGB888; pitch = width*4; | |
251 Rmask = 0x00ff0000; | |
252 Gmask = 0x0000ff00; | |
253 Bmask = 0x000000ff; | |
254 #ifdef HAVE_OPENGL | |
255 if (!(flags & SDL_OPENGL)) | |
256 #endif | |
257 break; | |
258 default: | |
259 SDL_SetError("Couldn't find requested mode in list"); | |
260 return(NULL); | |
261 } | |
262 | |
263 // if ( bpp != current->format->BitsPerPixel ) { | |
264 if ( ! SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, 0) ) { | |
265 return(NULL); | |
266 } | |
267 // } | |
268 | |
269 /* Set up the new mode framebuffer */ | |
270 current->flags = (SDL_FULLSCREEN|SDL_HWSURFACE); | |
271 current->w = width; | |
272 current->h = height; | |
273 current->pitch = pitch; | |
274 | |
275 #ifdef HAVE_OPENGL | |
276 if (pvr_inited) { | |
277 pvr_inited = 0; | |
278 pvr_shutdown(); | |
279 } | |
280 #endif | |
281 | |
282 vid_set_mode(disp_mode,pixel_mode); | |
283 | |
284 current->pixels = vram_s; | |
285 | |
286 #ifdef HAVE_OPENGL | |
287 if (flags & SDL_OPENGL) { | |
288 this->gl_config.driver_loaded = 1; | |
289 current->flags = SDL_FULLSCREEN | SDL_OPENGL; | |
290 current->pixels = NULL; | |
291 pvr_inited = 1; | |
292 pvr_init(¶ms); | |
293 glKosInit(); | |
294 glKosBeginFrame(); | |
295 } else | |
296 #endif | |
297 if (flags | SDL_DOUBLEBUF) { | |
298 current->flags |= SDL_DOUBLEBUF; | |
299 current->pixels = (void*)((int)current->pixels | 0x400000); | |
300 } | |
301 | |
302 /* We're done */ | |
303 return(current); | |
304 } | |
305 | |
306 /* We don't actually allow hardware surfaces other than the main one */ | |
307 static int DC_AllocHWSurface(_THIS, SDL_Surface *surface) | |
308 { | |
309 return(-1); | |
310 } | |
311 static void DC_FreeHWSurface(_THIS, SDL_Surface *surface) | |
312 { | |
313 return; | |
314 } | |
315 | |
316 /* We need to wait for vertical retrace on page flipped displays */ | |
317 static int DC_LockHWSurface(_THIS, SDL_Surface *surface) | |
318 { | |
319 return(0); | |
320 } | |
321 | |
322 static void DC_UnlockHWSurface(_THIS, SDL_Surface *surface) | |
323 { | |
324 return; | |
325 } | |
326 | |
327 static int DC_FlipHWSurface(_THIS, SDL_Surface *surface) | |
328 { | |
329 if (surface->flags & SDL_DOUBLEBUF) { | |
330 vid_set_start((int)surface->pixels & 0xffffff); | |
331 surface->pixels = (void*)((int)surface->pixels ^ 0x400000); | |
332 } | |
333 return(0); | |
334 } | |
335 | |
336 static void DC_UpdateRects(_THIS, int numrects, SDL_Rect *rects) | |
337 { | |
338 /* do nothing. */ | |
339 } | |
340 | |
341 static int DC_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) | |
342 { | |
343 /* do nothing of note. */ | |
344 return(1); | |
345 } | |
346 | |
347 /* Note: If we are terminated, this could be called in the middle of | |
348 another SDL video routine -- notably UpdateRects. | |
349 */ | |
350 static void DC_VideoQuit(_THIS) | |
351 { | |
352 #ifdef HAVE_OPENGL | |
353 if (pvr_inited) { | |
354 pvr_inited = 0; | |
355 pvr_shutdown(); | |
356 } | |
357 #endif | |
358 } | |
359 | |
360 #ifdef HAVE_OPENGL | |
361 | |
362 void dmyfunc(void) {} | |
363 | |
364 typedef void (*funcptr)(); | |
365 const static struct { | |
366 char *name; | |
367 funcptr addr; | |
368 } glfuncs[] = { | |
369 #define DEF(func) {#func,&func} | |
370 DEF(glBegin), | |
371 DEF(glBindTexture), | |
372 DEF(glBlendFunc), | |
373 DEF(glColor4f), | |
374 // DEF(glCopyImageID), | |
375 DEF(glDisable), | |
376 DEF(glEnable), | |
377 DEF(glEnd), | |
378 DEF(glFlush), | |
379 DEF(glGenTextures), | |
380 DEF(glGetString), | |
381 DEF(glLoadIdentity), | |
382 DEF(glMatrixMode), | |
383 DEF(glOrtho), | |
384 DEF(glPixelStorei), | |
385 // DEF(glPopAttrib), | |
386 // DEF(glPopClientAttrib), | |
387 {"glPopAttrib",&dmyfunc}, | |
388 {"glPopClientAttrib",&dmyfunc}, | |
389 DEF(glPopMatrix), | |
390 // DEF(glPushAttrib), | |
391 // DEF(glPushClientAttrib), | |
392 {"glPushAttrib",&dmyfunc}, | |
393 {"glPushClientAttrib",&dmyfunc}, | |
394 DEF(glPushMatrix), | |
395 DEF(glTexCoord2f), | |
396 DEF(glTexEnvf), | |
397 DEF(glTexImage2D), | |
398 DEF(glTexParameteri), | |
399 DEF(glTexSubImage2D), | |
400 DEF(glVertex2i), | |
401 DEF(glViewport), | |
402 #undef DEF | |
403 }; | |
404 | |
405 static void *DC_GL_GetProcAddress(_THIS, const char *proc) | |
406 { | |
407 void *ret; | |
408 int i; | |
409 | |
410 ret = glKosGetProcAddress(proc); | |
411 if (ret) return ret; | |
412 | |
413 for(i=0;i<sizeof(glfuncs)/sizeof(glfuncs[0]);i++) { | |
414 if (strcmp(proc,glfuncs[i].name)==0) return glfuncs[i].addr; | |
415 } | |
416 | |
417 return NULL; | |
418 } | |
419 | |
420 static int DC_GL_LoadLibrary(_THIS, const char *path) | |
421 { | |
422 this->gl_config.driver_loaded = 1; | |
423 | |
424 return 0; | |
425 } | |
426 | |
427 static int DC_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value) | |
428 { | |
429 GLenum mesa_attrib; | |
430 int val; | |
431 | |
432 switch(attrib) { | |
433 case SDL_GL_RED_SIZE: | |
434 val = 5; | |
435 break; | |
436 case SDL_GL_GREEN_SIZE: | |
437 val = 6; | |
438 break; | |
439 case SDL_GL_BLUE_SIZE: | |
440 val = 5; | |
441 break; | |
442 case SDL_GL_ALPHA_SIZE: | |
443 val = 0; | |
444 break; | |
445 case SDL_GL_DOUBLEBUFFER: | |
446 val = 1; | |
447 break; | |
448 case SDL_GL_DEPTH_SIZE: | |
449 val = 16; /* or 32? */ | |
450 break; | |
451 case SDL_GL_STENCIL_SIZE: | |
452 val = 0; | |
453 break; | |
454 case SDL_GL_ACCUM_RED_SIZE: | |
455 val = 0; | |
456 break; | |
457 case SDL_GL_ACCUM_GREEN_SIZE: | |
458 val = 0; | |
459 case SDL_GL_ACCUM_BLUE_SIZE: | |
460 val = 0; | |
461 break; | |
462 case SDL_GL_ACCUM_ALPHA_SIZE: | |
463 val = 0; | |
464 break; | |
465 default : | |
466 return -1; | |
467 } | |
468 *value = val; | |
469 return 0; | |
470 } | |
471 | |
472 static void DC_GL_SwapBuffers(_THIS) | |
473 { | |
474 glKosFinishFrame(); | |
475 glKosBeginFrame(); | |
476 } | |
477 #endif |