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