Mercurial > sdl-ios-xcode
annotate src/video/aalib/SDL_aavideo.c @ 1320:2a2f31cc4c8b
AAlib driver was using C runtime string functions without including string.h.
Fixes Bugzilla #131.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 02 Feb 2006 05:35:42 +0000 |
parents | c9b51268668f |
children | 3692456e7b0f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* AAlib based SDL video driver implementation. | |
24 */ | |
25 | |
26 #include <stdlib.h> | |
27 #include <stdio.h> | |
1320
2a2f31cc4c8b
AAlib driver was using C runtime string functions without including string.h.
Ryan C. Gordon <icculus@icculus.org>
parents:
1312
diff
changeset
|
28 #include <string.h> |
0 | 29 #include <unistd.h> |
30 #include <sys/stat.h> | |
31 | |
32 | |
33 #include "SDL.h" | |
34 #include "SDL_error.h" | |
35 #include "SDL_video.h" | |
36 #include "SDL_mouse.h" | |
37 #include "SDL_sysvideo.h" | |
38 #include "SDL_pixels_c.h" | |
39 #include "SDL_events_c.h" | |
40 | |
41 #include "SDL_aavideo.h" | |
42 #include "SDL_aaevents_c.h" | |
43 #include "SDL_aamouse_c.h" | |
44 | |
45 #include <aalib.h> | |
46 | |
47 /* Initialization/Query functions */ | |
48 static int AA_VideoInit(_THIS, SDL_PixelFormat *vformat); | |
49 static SDL_Rect **AA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); | |
50 static SDL_Surface *AA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); | |
51 static int AA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors); | |
52 static void AA_VideoQuit(_THIS); | |
53 | |
54 /* Hardware surface functions */ | |
55 static int AA_AllocHWSurface(_THIS, SDL_Surface *surface); | |
56 static int AA_LockHWSurface(_THIS, SDL_Surface *surface); | |
57 static int AA_FlipHWSurface(_THIS, SDL_Surface *surface); | |
58 static void AA_UnlockHWSurface(_THIS, SDL_Surface *surface); | |
59 static void AA_FreeHWSurface(_THIS, SDL_Surface *surface); | |
60 | |
61 /* Cache the VideoDevice struct */ | |
62 static struct SDL_VideoDevice *local_this; | |
63 | |
64 /* AAlib driver bootstrap functions */ | |
65 | |
66 static int AA_Available(void) | |
67 { | |
68 return 1; /* Always available ! */ | |
69 } | |
70 | |
71 static void AA_DeleteDevice(SDL_VideoDevice *device) | |
72 { | |
73 free(device->hidden); | |
74 free(device); | |
75 } | |
76 | |
77 static SDL_VideoDevice *AA_CreateDevice(int devindex) | |
78 { | |
79 SDL_VideoDevice *device; | |
80 | |
81 /* Initialize all variables that we clean on shutdown */ | |
82 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice)); | |
83 if ( device ) { | |
84 memset(device, 0, (sizeof *device)); | |
85 device->hidden = (struct SDL_PrivateVideoData *) | |
86 malloc((sizeof *device->hidden)); | |
87 } | |
88 if ( (device == NULL) || (device->hidden == NULL) ) { | |
89 SDL_OutOfMemory(); | |
90 if ( device ) { | |
91 free(device); | |
92 } | |
93 return(0); | |
94 } | |
95 memset(device->hidden, 0, (sizeof *device->hidden)); | |
96 | |
97 /* Set the function pointers */ | |
98 device->VideoInit = AA_VideoInit; | |
99 device->ListModes = AA_ListModes; | |
100 device->SetVideoMode = AA_SetVideoMode; | |
101 device->CreateYUVOverlay = NULL; | |
102 device->SetColors = AA_SetColors; | |
103 device->UpdateRects = NULL; | |
104 device->VideoQuit = AA_VideoQuit; | |
105 device->AllocHWSurface = AA_AllocHWSurface; | |
106 device->CheckHWBlit = NULL; | |
107 device->FillHWRect = NULL; | |
108 device->SetHWColorKey = NULL; | |
109 device->SetHWAlpha = NULL; | |
110 device->LockHWSurface = AA_LockHWSurface; | |
111 device->UnlockHWSurface = AA_UnlockHWSurface; | |
112 device->FlipHWSurface = NULL; | |
113 device->FreeHWSurface = AA_FreeHWSurface; | |
114 device->SetCaption = NULL; | |
115 device->SetIcon = NULL; | |
116 device->IconifyWindow = NULL; | |
117 device->GrabInput = NULL; | |
118 device->GetWMInfo = NULL; | |
119 device->InitOSKeymap = AA_InitOSKeymap; | |
120 device->PumpEvents = AA_PumpEvents; | |
121 | |
122 device->free = AA_DeleteDevice; | |
123 | |
124 return device; | |
125 } | |
126 | |
127 VideoBootStrap AALIB_bootstrap = { | |
128 "aalib", "ASCII Art Library", | |
129 AA_Available, AA_CreateDevice | |
130 }; | |
131 | |
132 static void AA_ResizeHandler(aa_context *); | |
133 | |
134 int AA_VideoInit(_THIS, SDL_PixelFormat *vformat) | |
135 { | |
136 int keyboard; | |
137 int i; | |
138 | |
139 /* Initialize all variables that we clean on shutdown */ | |
140 for ( i=0; i<SDL_NUMMODES; ++i ) { | |
141 SDL_modelist[i] = malloc(sizeof(SDL_Rect)); | |
142 SDL_modelist[i]->x = SDL_modelist[i]->y = 0; | |
143 } | |
144 /* Modes sorted largest to smallest */ | |
145 SDL_modelist[0]->w = 1024; SDL_modelist[0]->h = 768; | |
146 SDL_modelist[1]->w = 800; SDL_modelist[1]->h = 600; | |
147 SDL_modelist[2]->w = 640; SDL_modelist[2]->h = 480; | |
148 SDL_modelist[3]->w = 320; SDL_modelist[3]->h = 400; | |
149 SDL_modelist[4]->w = 320; SDL_modelist[4]->h = 240; | |
150 SDL_modelist[5]->w = 320; SDL_modelist[5]->h = 200; | |
151 SDL_modelist[6] = NULL; | |
152 | |
153 /* Initialize the library */ | |
154 | |
155 AA_mutex = SDL_CreateMutex(); | |
156 | |
157 aa_parseoptions (NULL, NULL, NULL, NULL); | |
158 | |
159 AA_context = aa_autoinit(&aa_defparams); | |
160 if ( ! AA_context ) { | |
161 SDL_SetError("Unable to initialize AAlib"); | |
162 return(-1); | |
163 } | |
164 | |
165 /* Enable mouse and keyboard support */ | |
166 | |
167 if ( ! aa_autoinitkbd (AA_context, AA_SENDRELEASE) ) { | |
168 SDL_SetError("Unable to initialize AAlib keyboard"); | |
169 return(-1); | |
170 } | |
171 if ( ! aa_autoinitmouse (AA_context, AA_SENDRELEASE) ) { | |
172 fprintf(stderr,"Warning: Unable to initialize AAlib mouse"); | |
173 } | |
174 AA_rparams = aa_getrenderparams(); | |
175 | |
176 local_this = this; | |
177 | |
178 aa_resizehandler(AA_context, AA_ResizeHandler); | |
179 | |
180 fprintf(stderr,"Using AAlib driver: %s (%s)\n", AA_context->driver->name, AA_context->driver->shortname); | |
181 | |
182 AA_in_x11 = (strcmp(AA_context->driver->shortname,"X11") == 0); | |
183 /* Determine the screen depth (use default 8-bit depth) */ | |
184 vformat->BitsPerPixel = 8; | |
185 vformat->BytesPerPixel = 1; | |
186 | |
187 /* We're done! */ | |
188 return(0); | |
189 } | |
190 | |
191 SDL_Rect **AA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) | |
192 { | |
193 if(format->BitsPerPixel != 8) | |
194 return NULL; | |
195 | |
196 if ( flags & SDL_FULLSCREEN ) { | |
197 return SDL_modelist; | |
198 } else { | |
199 return (SDL_Rect **) -1; | |
200 } | |
201 } | |
202 | |
203 /* From aavga.c | |
204 AAlib does not give us the choice of the actual resolution, thus we have to simulate additional | |
205 resolution by scaling down manually each frame | |
206 */ | |
207 static void fastscale (register char *b1, register char *b2, int x1, int x2, int y1, int y2) | |
208 { | |
209 register int ex, spx = 0, ddx, ddx1; | |
210 int ddy1, ddy, spy = 0, ey; | |
211 int x; | |
212 char *bb1 = b1; | |
213 if (!x1 || !x2 || !y1 || !y2) | |
214 return; | |
215 ddx = x1 + x1; | |
216 ddx1 = x2 + x2; | |
217 if (ddx1 < ddx) | |
218 spx = ddx / ddx1, ddx %= ddx1; | |
219 ddy = y1 + y1; | |
220 ddy1 = y2 + y2; | |
221 if (ddy1 < ddy) | |
222 spy = (ddy / ddy1) * x1, ddy %= ddy1; | |
223 ey = -ddy1; | |
224 for (; y2; y2--) { | |
225 ex = -ddx1; | |
226 for (x = x2; x; x--) { | |
227 *b2 = *b1; | |
228 b2++; | |
229 b1 += spx; | |
230 ex += ddx; | |
231 if (ex > 0) { | |
232 b1++; | |
233 ex -= ddx1; | |
234 } | |
235 } | |
236 bb1 += spy; | |
237 ey += ddy; | |
238 if (ey > 0) { | |
239 bb1 += x1; | |
240 ey -= ddy1; | |
241 } | |
242 b1 = bb1; | |
243 } | |
244 } | |
245 | |
246 /* Various screen update functions available */ | |
247 static void AA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects); | |
248 | |
249 SDL_Surface *AA_SetVideoMode(_THIS, SDL_Surface *current, | |
250 int width, int height, int bpp, Uint32 flags) | |
251 { | |
252 int mode; | |
253 | |
254 if ( AA_buffer ) { | |
255 free( AA_buffer ); | |
256 } | |
257 | |
258 AA_buffer = malloc(width * height); | |
259 if ( ! AA_buffer ) { | |
260 SDL_SetError("Couldn't allocate buffer for requested mode"); | |
261 return(NULL); | |
262 } | |
263 | |
264 /* printf("Setting mode %dx%d\n", width, height); */ | |
265 | |
266 memset(aa_image(AA_context), 0, aa_imgwidth(AA_context) * aa_imgheight(AA_context)); | |
267 memset(AA_buffer, 0, width * height); | |
268 | |
269 /* Allocate the new pixel format for the screen */ | |
270 if ( ! SDL_ReallocFormat(current, 8, 0, 0, 0, 0) ) { | |
271 return(NULL); | |
272 } | |
273 | |
274 /* Set up the new mode framebuffer */ | |
275 current->flags = SDL_FULLSCREEN; | |
276 AA_w = current->w = width; | |
277 AA_h = current->h = height; | |
278 current->pitch = current->w; | |
279 current->pixels = AA_buffer; | |
280 | |
281 AA_x_ratio = ((double)aa_imgwidth(AA_context)) / ((double)width); | |
282 AA_y_ratio = ((double)aa_imgheight(AA_context)) / ((double)height); | |
283 | |
284 /* Set the blit function */ | |
285 this->UpdateRects = AA_DirectUpdate; | |
286 | |
287 /* We're done */ | |
288 return(current); | |
289 } | |
290 | |
291 static void AA_ResizeHandler(aa_context *context) | |
292 { | |
293 aa_resize(context); | |
294 local_this->hidden->x_ratio = ((double)aa_imgwidth(context)) / ((double)local_this->screen->w); | |
295 local_this->hidden->y_ratio = ((double)aa_imgheight(context)) / ((double)local_this->screen->h); | |
296 | |
297 fastscale (local_this->hidden->buffer, aa_image(context), local_this->hidden->w, aa_imgwidth (context), local_this->hidden->h, aa_imgheight (context)); | |
298 aa_renderpalette(context, local_this->hidden->palette, local_this->hidden->rparams, 0, 0, aa_scrwidth(context), aa_scrheight(context)); | |
299 aa_flush(context); | |
300 } | |
301 | |
302 /* We don't actually allow hardware surfaces other than the main one */ | |
303 static int AA_AllocHWSurface(_THIS, SDL_Surface *surface) | |
304 { | |
305 return(-1); | |
306 } | |
307 static void AA_FreeHWSurface(_THIS, SDL_Surface *surface) | |
308 { | |
309 return; | |
310 } | |
311 | |
312 /* We need to wait for vertical retrace on page flipped displays */ | |
313 static int AA_LockHWSurface(_THIS, SDL_Surface *surface) | |
314 { | |
315 /* TODO ? */ | |
316 return(0); | |
317 } | |
318 static void AA_UnlockHWSurface(_THIS, SDL_Surface *surface) | |
319 { | |
320 return; | |
321 } | |
322 | |
323 /* FIXME: How is this done with AAlib? */ | |
324 static int AA_FlipHWSurface(_THIS, SDL_Surface *surface) | |
325 { | |
326 SDL_mutexP(AA_mutex); | |
327 aa_flush(AA_context); | |
328 SDL_mutexV(AA_mutex); | |
329 return(0); | |
330 } | |
331 | |
332 static void AA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects) | |
333 { | |
334 int i; | |
335 SDL_Rect *rect; | |
336 | |
337 fastscale (AA_buffer, aa_image(AA_context), AA_w, aa_imgwidth (AA_context), AA_h, aa_imgheight (AA_context)); | |
338 #if 1 | |
339 aa_renderpalette(AA_context, AA_palette, AA_rparams, 0, 0, aa_scrwidth(AA_context), aa_scrheight(AA_context)); | |
340 #else | |
341 /* Render only the rectangles in the list */ | |
342 printf("Update rects : "); | |
343 for ( i=0; i < numrects; ++i ) { | |
344 rect = &rects[i]; | |
345 printf("(%d,%d-%d,%d)", rect->x, rect->y, rect->w, rect->h); | |
346 aa_renderpalette(AA_context, AA_palette, AA_rparams, rect->x * AA_x_ratio, rect->y * AA_y_ratio, rect->w * AA_x_ratio, rect->h * AA_y_ratio); | |
347 } | |
348 printf("\n"); | |
349 #endif | |
350 SDL_mutexP(AA_mutex); | |
351 aa_flush(AA_context); | |
352 SDL_mutexV(AA_mutex); | |
353 return; | |
354 } | |
355 | |
356 int AA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) | |
357 { | |
358 int i; | |
359 | |
360 for ( i=0; i < ncolors; i++ ) { | |
361 aa_setpalette(AA_palette, firstcolor + i, | |
362 colors[i].r>>2, | |
363 colors[i].g>>2, | |
364 colors[i].b>>2); | |
365 } | |
366 return(1); | |
367 } | |
368 | |
369 /* Note: If we are terminated, this could be called in the middle of | |
370 another SDL video routine -- notably UpdateRects. | |
371 */ | |
372 void AA_VideoQuit(_THIS) | |
373 { | |
374 int i; | |
375 | |
376 aa_uninitkbd(AA_context); | |
377 aa_uninitmouse(AA_context); | |
378 | |
379 /* Free video mode lists */ | |
380 for ( i=0; i<SDL_NUMMODES; ++i ) { | |
381 if ( SDL_modelist[i] != NULL ) { | |
382 free(SDL_modelist[i]); | |
383 SDL_modelist[i] = NULL; | |
384 } | |
385 } | |
386 | |
387 aa_close(AA_context); | |
388 | |
389 SDL_DestroyMutex(AA_mutex); | |
390 | |
391 this->screen->pixels = NULL; | |
392 } |