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