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