Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
426 lines (360 loc) · 11.5 KB

SDL_aavideo.c

File metadata and controls

426 lines (360 loc) · 11.5 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
28
29
30
31
32
/* AAlib based SDL video driver implementation.
*/
#include <unistd.h>
#include <sys/stat.h>
#include "SDL_video.h"
#include "SDL_mouse.h"
Feb 16, 2006
Feb 16, 2006
33
34
35
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"
Apr 26, 2001
Apr 26, 2001
36
37
38
39
40
41
42
43
#include "SDL_aavideo.h"
#include "SDL_aaevents_c.h"
#include "SDL_aamouse_c.h"
#include <aalib.h>
/* Initialization/Query functions */
May 28, 2006
May 28, 2006
44
45
46
47
48
49
50
51
static int AA_VideoInit (_THIS, SDL_PixelFormat * vformat);
static SDL_Rect **AA_ListModes (_THIS, SDL_PixelFormat * format,
Uint32 flags);
static SDL_Surface *AA_SetVideoMode (_THIS, SDL_Surface * current, int width,
int height, int bpp, Uint32 flags);
static int AA_SetColors (_THIS, int firstcolor, int ncolors,
SDL_Color * colors);
static void AA_VideoQuit (_THIS);
Apr 26, 2001
Apr 26, 2001
52
53
/* Hardware surface functions */
May 28, 2006
May 28, 2006
54
55
56
57
58
static int AA_AllocHWSurface (_THIS, SDL_Surface * surface);
static int AA_LockHWSurface (_THIS, SDL_Surface * surface);
static int AA_FlipHWSurface (_THIS, SDL_Surface * surface);
static void AA_UnlockHWSurface (_THIS, SDL_Surface * surface);
static void AA_FreeHWSurface (_THIS, SDL_Surface * surface);
Apr 26, 2001
Apr 26, 2001
59
60
61
62
63
64
/* Cache the VideoDevice struct */
static struct SDL_VideoDevice *local_this;
/* AAlib driver bootstrap functions */
May 28, 2006
May 28, 2006
65
66
static int
AA_Available (void)
Apr 26, 2001
Apr 26, 2001
67
{
May 28, 2006
May 28, 2006
68
return 1; /* Always available ! */
Apr 26, 2001
Apr 26, 2001
69
70
}
May 28, 2006
May 28, 2006
71
72
static void
AA_DeleteDevice (SDL_VideoDevice * device)
Apr 26, 2001
Apr 26, 2001
73
{
May 28, 2006
May 28, 2006
74
75
SDL_free (device->hidden);
SDL_free (device);
Apr 26, 2001
Apr 26, 2001
76
77
}
May 28, 2006
May 28, 2006
78
79
static SDL_VideoDevice *
AA_CreateDevice (int devindex)
Apr 26, 2001
Apr 26, 2001
80
{
May 28, 2006
May 28, 2006
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
SDL_VideoDevice *device;
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_malloc (sizeof (SDL_VideoDevice));
if (device) {
SDL_memset (device, 0, (sizeof *device));
device->hidden = (struct SDL_PrivateVideoData *)
SDL_malloc ((sizeof *device->hidden));
}
if ((device == NULL) || (device->hidden == NULL)) {
SDL_OutOfMemory ();
if (device) {
SDL_free (device);
}
return (0);
}
SDL_memset (device->hidden, 0, (sizeof *device->hidden));
/* Set the function pointers */
device->VideoInit = AA_VideoInit;
device->ListModes = AA_ListModes;
device->SetVideoMode = AA_SetVideoMode;
device->CreateYUVOverlay = NULL;
device->SetColors = AA_SetColors;
device->UpdateRects = NULL;
device->VideoQuit = AA_VideoQuit;
device->AllocHWSurface = AA_AllocHWSurface;
device->CheckHWBlit = NULL;
device->FillHWRect = NULL;
device->SetHWColorKey = NULL;
device->SetHWAlpha = NULL;
device->LockHWSurface = AA_LockHWSurface;
device->UnlockHWSurface = AA_UnlockHWSurface;
device->FlipHWSurface = NULL;
device->FreeHWSurface = AA_FreeHWSurface;
device->SetCaption = NULL;
device->SetIcon = NULL;
device->IconifyWindow = NULL;
device->GrabInput = NULL;
device->GetWMInfo = NULL;
device->InitOSKeymap = AA_InitOSKeymap;
device->PumpEvents = AA_PumpEvents;
device->free = AA_DeleteDevice;
return device;
Apr 26, 2001
Apr 26, 2001
127
128
129
}
VideoBootStrap AALIB_bootstrap = {
May 28, 2006
May 28, 2006
130
131
"aalib", "ASCII Art Library",
AA_Available, AA_CreateDevice
Apr 26, 2001
Apr 26, 2001
132
133
};
May 28, 2006
May 28, 2006
134
static void AA_ResizeHandler (aa_context *);
Apr 26, 2001
Apr 26, 2001
135
May 28, 2006
May 28, 2006
136
137
int
AA_VideoInit (_THIS, SDL_PixelFormat * vformat)
Apr 26, 2001
Apr 26, 2001
138
{
May 28, 2006
May 28, 2006
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
int keyboard;
int i;
/* Initialize all variables that we clean on shutdown */
for (i = 0; i < SDL_NUMMODES; ++i) {
SDL_modelist[i] = SDL_malloc (sizeof (SDL_Rect));
SDL_modelist[i]->x = SDL_modelist[i]->y = 0;
}
/* Modes sorted largest to smallest */
SDL_modelist[0]->w = 1024;
SDL_modelist[0]->h = 768;
SDL_modelist[1]->w = 800;
SDL_modelist[1]->h = 600;
SDL_modelist[2]->w = 640;
SDL_modelist[2]->h = 480;
SDL_modelist[3]->w = 320;
SDL_modelist[3]->h = 400;
SDL_modelist[4]->w = 320;
SDL_modelist[4]->h = 240;
SDL_modelist[5]->w = 320;
SDL_modelist[5]->h = 200;
SDL_modelist[6] = NULL;
/* Initialize the library */
AA_mutex = SDL_CreateMutex ();
aa_parseoptions (NULL, NULL, NULL, NULL);
AA_context = aa_autoinit (&aa_defparams);
if (!AA_context) {
SDL_SetError ("Unable to initialize AAlib");
return (-1);
}
/* Enable mouse and keyboard support */
if (!aa_autoinitkbd (AA_context, AA_SENDRELEASE)) {
SDL_SetError ("Unable to initialize AAlib keyboard");
return (-1);
}
if (!aa_autoinitmouse (AA_context, AA_SENDRELEASE)) {
fprintf (stderr, "Warning: Unable to initialize AAlib mouse");
}
AA_rparams = aa_getrenderparams ();
local_this = this;
aa_resizehandler (AA_context, AA_ResizeHandler);
fprintf (stderr, "Using AAlib driver: %s (%s)\n",
AA_context->driver->name, AA_context->driver->shortname);
AA_in_x11 = (SDL_strcmp (AA_context->driver->shortname, "X11") == 0);
/* Determine the screen depth (use default 8-bit depth) */
vformat->BitsPerPixel = 8;
vformat->BytesPerPixel = 1;
/* We're done! */
return (0);
Apr 26, 2001
Apr 26, 2001
199
200
}
May 28, 2006
May 28, 2006
201
202
SDL_Rect **
AA_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
Apr 26, 2001
Apr 26, 2001
203
{
May 28, 2006
May 28, 2006
204
205
206
207
208
209
210
211
if (format->BitsPerPixel != 8)
return NULL;
if (flags & SDL_FULLSCREEN) {
return SDL_modelist;
} else {
return (SDL_Rect **) - 1;
}
Apr 26, 2001
Apr 26, 2001
212
213
214
215
216
217
}
/* From aavga.c
AAlib does not give us the choice of the actual resolution, thus we have to simulate additional
resolution by scaling down manually each frame
*/
May 28, 2006
May 28, 2006
218
219
220
static void
fastscale (register char *b1, register char *b2, int x1, int x2, int y1,
int y2)
Apr 26, 2001
Apr 26, 2001
221
{
May 28, 2006
May 28, 2006
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
register int ex, spx = 0, ddx, ddx1;
int ddy1, ddy, spy = 0, ey;
int x;
char *bb1 = b1;
if (!x1 || !x2 || !y1 || !y2)
return;
ddx = x1 + x1;
ddx1 = x2 + x2;
if (ddx1 < ddx)
spx = ddx / ddx1, ddx %= ddx1;
ddy = y1 + y1;
ddy1 = y2 + y2;
if (ddy1 < ddy)
spy = (ddy / ddy1) * x1, ddy %= ddy1;
ey = -ddy1;
for (; y2; y2--) {
ex = -ddx1;
for (x = x2; x; x--) {
*b2 = *b1;
b2++;
b1 += spx;
ex += ddx;
if (ex > 0) {
b1++;
ex -= ddx1;
}
}
bb1 += spy;
ey += ddy;
if (ey > 0) {
bb1 += x1;
ey -= ddy1;
}
b1 = bb1;
}
Apr 26, 2001
Apr 26, 2001
257
258
259
}
/* Various screen update functions available */
May 28, 2006
May 28, 2006
260
static void AA_DirectUpdate (_THIS, int numrects, SDL_Rect * rects);
Apr 26, 2001
Apr 26, 2001
261
May 28, 2006
May 28, 2006
262
263
264
SDL_Surface *
AA_SetVideoMode (_THIS, SDL_Surface * current,
int width, int height, int bpp, Uint32 flags)
Apr 26, 2001
Apr 26, 2001
265
{
May 28, 2006
May 28, 2006
266
int mode;
Apr 26, 2001
Apr 26, 2001
267
May 28, 2006
May 28, 2006
268
269
270
if (AA_buffer) {
SDL_free (AA_buffer);
}
Apr 26, 2001
Apr 26, 2001
271
May 28, 2006
May 28, 2006
272
273
274
275
276
AA_buffer = SDL_malloc (width * height);
if (!AA_buffer) {
SDL_SetError ("Couldn't allocate buffer for requested mode");
return (NULL);
}
Apr 26, 2001
Apr 26, 2001
277
278
279
/* printf("Setting mode %dx%d\n", width, height); */
May 28, 2006
May 28, 2006
280
281
282
SDL_memset (aa_image (AA_context), 0,
aa_imgwidth (AA_context) * aa_imgheight (AA_context));
SDL_memset (AA_buffer, 0, width * height);
Apr 26, 2001
Apr 26, 2001
283
May 28, 2006
May 28, 2006
284
285
286
287
/* Allocate the new pixel format for the screen */
if (!SDL_ReallocFormat (current, 8, 0, 0, 0, 0)) {
return (NULL);
}
Apr 26, 2001
Apr 26, 2001
288
May 28, 2006
May 28, 2006
289
290
291
292
293
294
/* Set up the new mode framebuffer */
current->flags = SDL_FULLSCREEN;
AA_w = current->w = width;
AA_h = current->h = height;
current->pitch = current->w;
current->pixels = AA_buffer;
Apr 26, 2001
Apr 26, 2001
295
May 28, 2006
May 28, 2006
296
297
AA_x_ratio = ((double) aa_imgwidth (AA_context)) / ((double) width);
AA_y_ratio = ((double) aa_imgheight (AA_context)) / ((double) height);
Apr 26, 2001
Apr 26, 2001
298
May 28, 2006
May 28, 2006
299
300
/* Set the blit function */
this->UpdateRects = AA_DirectUpdate;
Apr 26, 2001
Apr 26, 2001
301
May 28, 2006
May 28, 2006
302
303
/* We're done */
return (current);
Apr 26, 2001
Apr 26, 2001
304
305
}
May 28, 2006
May 28, 2006
306
307
static void
AA_ResizeHandler (aa_context * context)
Apr 26, 2001
Apr 26, 2001
308
{
May 28, 2006
May 28, 2006
309
310
311
312
313
314
315
316
317
318
319
320
321
aa_resize (context);
local_this->hidden->x_ratio =
((double) aa_imgwidth (context)) / ((double) local_this->screen->w);
local_this->hidden->y_ratio =
((double) aa_imgheight (context)) / ((double) local_this->screen->h);
fastscale (local_this->hidden->buffer, aa_image (context),
local_this->hidden->w, aa_imgwidth (context),
local_this->hidden->h, aa_imgheight (context));
aa_renderpalette (context, local_this->hidden->palette,
local_this->hidden->rparams, 0, 0,
aa_scrwidth (context), aa_scrheight (context));
aa_flush (context);
Apr 26, 2001
Apr 26, 2001
322
323
324
}
/* We don't actually allow hardware surfaces other than the main one */
May 28, 2006
May 28, 2006
325
326
static int
AA_AllocHWSurface (_THIS, SDL_Surface * surface)
Apr 26, 2001
Apr 26, 2001
327
{
May 28, 2006
May 28, 2006
328
return (-1);
Apr 26, 2001
Apr 26, 2001
329
}
May 28, 2006
May 28, 2006
330
331
static void
AA_FreeHWSurface (_THIS, SDL_Surface * surface)
Apr 26, 2001
Apr 26, 2001
332
{
May 28, 2006
May 28, 2006
333
return;
Apr 26, 2001
Apr 26, 2001
334
335
336
}
/* We need to wait for vertical retrace on page flipped displays */
May 28, 2006
May 28, 2006
337
338
static int
AA_LockHWSurface (_THIS, SDL_Surface * surface)
Apr 26, 2001
Apr 26, 2001
339
{
May 28, 2006
May 28, 2006
340
341
/* TODO ? */
return (0);
Apr 26, 2001
Apr 26, 2001
342
}
May 28, 2006
May 28, 2006
343
344
static void
AA_UnlockHWSurface (_THIS, SDL_Surface * surface)
Apr 26, 2001
Apr 26, 2001
345
{
May 28, 2006
May 28, 2006
346
return;
Apr 26, 2001
Apr 26, 2001
347
348
349
}
/* FIXME: How is this done with AAlib? */
May 28, 2006
May 28, 2006
350
351
static int
AA_FlipHWSurface (_THIS, SDL_Surface * surface)
Apr 26, 2001
Apr 26, 2001
352
{
May 28, 2006
May 28, 2006
353
354
355
356
SDL_mutexP (AA_mutex);
aa_flush (AA_context);
SDL_mutexV (AA_mutex);
return (0);
Apr 26, 2001
Apr 26, 2001
357
358
}
May 28, 2006
May 28, 2006
359
360
static void
AA_DirectUpdate (_THIS, int numrects, SDL_Rect * rects)
Apr 26, 2001
Apr 26, 2001
361
{
May 28, 2006
May 28, 2006
362
363
int i;
SDL_Rect *rect;
Apr 26, 2001
Apr 26, 2001
364
May 28, 2006
May 28, 2006
365
366
fastscale (AA_buffer, aa_image (AA_context), AA_w,
aa_imgwidth (AA_context), AA_h, aa_imgheight (AA_context));
Apr 26, 2001
Apr 26, 2001
367
#if 1
May 28, 2006
May 28, 2006
368
369
aa_renderpalette (AA_context, AA_palette, AA_rparams, 0, 0,
aa_scrwidth (AA_context), aa_scrheight (AA_context));
Apr 26, 2001
Apr 26, 2001
370
#else
May 28, 2006
May 28, 2006
371
372
373
374
375
376
377
378
379
380
/* Render only the rectangles in the list */
printf ("Update rects : ");
for (i = 0; i < numrects; ++i) {
rect = &rects[i];
printf ("(%d,%d-%d,%d)", rect->x, rect->y, rect->w, rect->h);
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);
}
printf ("\n");
Apr 26, 2001
Apr 26, 2001
381
#endif
May 28, 2006
May 28, 2006
382
383
384
385
SDL_mutexP (AA_mutex);
aa_flush (AA_context);
SDL_mutexV (AA_mutex);
return;
Apr 26, 2001
Apr 26, 2001
386
387
}
May 28, 2006
May 28, 2006
388
389
int
AA_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors)
Apr 26, 2001
Apr 26, 2001
390
{
May 28, 2006
May 28, 2006
391
392
393
394
395
396
397
int i;
for (i = 0; i < ncolors; i++) {
aa_setpalette (AA_palette, firstcolor + i,
colors[i].r >> 2, colors[i].g >> 2, colors[i].b >> 2);
}
return (1);
Apr 26, 2001
Apr 26, 2001
398
399
400
401
402
}
/* Note: If we are terminated, this could be called in the middle of
another SDL video routine -- notably UpdateRects.
*/
May 28, 2006
May 28, 2006
403
404
void
AA_VideoQuit (_THIS)
Apr 26, 2001
Apr 26, 2001
405
{
May 28, 2006
May 28, 2006
406
int i;
Apr 26, 2001
Apr 26, 2001
407
May 28, 2006
May 28, 2006
408
409
aa_uninitkbd (AA_context);
aa_uninitmouse (AA_context);
Apr 26, 2001
Apr 26, 2001
410
May 28, 2006
May 28, 2006
411
412
413
414
415
416
417
/* Free video mode lists */
for (i = 0; i < SDL_NUMMODES; ++i) {
if (SDL_modelist[i] != NULL) {
SDL_free (SDL_modelist[i]);
SDL_modelist[i] = NULL;
}
}
Apr 26, 2001
Apr 26, 2001
418
May 28, 2006
May 28, 2006
419
aa_close (AA_context);
Apr 26, 2001
Apr 26, 2001
420
May 28, 2006
May 28, 2006
421
422
423
SDL_DestroyMutex (AA_mutex);
this->screen->pixels = NULL;
Apr 26, 2001
Apr 26, 2001
424
}
May 28, 2006
May 28, 2006
425
426
/* vi: set ts=4 sw=4 expandtab: */