19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* AAlib based SDL video driver implementation.
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_aavideo.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_aaevents_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_aamouse_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <aalib.h>
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_VideoInit(_THIS, SDL_PixelFormat *vformat);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **AA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *AA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_VideoQuit(_THIS);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_AllocHWSurface(_THIS, SDL_Surface *surface);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_LockHWSurface(_THIS, SDL_Surface *surface);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_FlipHWSurface(_THIS, SDL_Surface *surface);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_UnlockHWSurface(_THIS, SDL_Surface *surface);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_FreeHWSurface(_THIS, SDL_Surface *surface);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Cache the VideoDevice struct */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct SDL_VideoDevice *local_this;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* AAlib driver bootstrap functions */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_Available(void)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 1; /* Always available ! */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_DeleteDevice(SDL_VideoDevice *device)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *AA_CreateDevice(int devindex)
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *device;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(device, 0, (sizeof *device));
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->hidden = (struct SDL_PrivateVideoData *)
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *device->hidden));
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (device == NULL) || (device->hidden == NULL) ) {
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoInit = AA_VideoInit;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ListModes = AA_ListModes;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetVideoMode = AA_SetVideoMode;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateYUVOverlay = NULL;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetColors = AA_SetColors;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateRects = NULL;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoQuit = AA_VideoQuit;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->AllocHWSurface = AA_AllocHWSurface;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckHWBlit = NULL;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FillHWRect = NULL;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWColorKey = NULL;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWAlpha = NULL;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->LockHWSurface = AA_LockHWSurface;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UnlockHWSurface = AA_UnlockHWSurface;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FlipHWSurface = NULL;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeHWSurface = AA_FreeHWSurface;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetCaption = NULL;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetIcon = NULL;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->IconifyWindow = NULL;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GrabInput = NULL;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetWMInfo = NULL;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->InitOSKeymap = AA_InitOSKeymap;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->PumpEvents = AA_PumpEvents;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->free = AA_DeleteDevice;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap AALIB_bootstrap = {
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"aalib", "ASCII Art Library",
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_Available, AA_CreateDevice
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_ResizeHandler(aa_context *);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint AA_VideoInit(_THIS, SDL_PixelFormat *vformat)
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int keyboard;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_NUMMODES; ++i ) {
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_modelist[i] = SDL_malloc(sizeof(SDL_Rect));
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_modelist[i]->x = SDL_modelist[i]->y = 0;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Modes sorted largest to smallest */
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[0]->w = 1024; SDL_modelist[0]->h = 768;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[1]->w = 800; SDL_modelist[1]->h = 600;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[2]->w = 640; SDL_modelist[2]->h = 480;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[3]->w = 320; SDL_modelist[3]->h = 400;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[4]->w = 320; SDL_modelist[4]->h = 240;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[5]->w = 320; SDL_modelist[5]->h = 200;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[6] = NULL;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize the library */
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_mutex = SDL_CreateMutex();
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_parseoptions (NULL, NULL, NULL, NULL);
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_context = aa_autoinit(&aa_defparams);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! AA_context ) {
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to initialize AAlib");
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Enable mouse and keyboard support */
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! aa_autoinitkbd (AA_context, AA_SENDRELEASE) ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to initialize AAlib keyboard");
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! aa_autoinitmouse (AA_context, AA_SENDRELEASE) ) {
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr,"Warning: Unable to initialize AAlib mouse");
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_rparams = aa_getrenderparams();
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	local_this = this;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_resizehandler(AA_context, AA_ResizeHandler);
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr,"Using AAlib driver: %s (%s)\n", AA_context->driver->name, AA_context->driver->shortname);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_in_x11 = (SDL_strcmp(AA_context->driver->shortname,"X11") == 0);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the screen depth (use default 8-bit depth) */
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BitsPerPixel = 8;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BytesPerPixel = 1;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **AA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     if(format->BitsPerPixel != 8)
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		return NULL;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 if ( flags & SDL_FULLSCREEN ) {
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 return SDL_modelist;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 } else {
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 return (SDL_Rect **) -1;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 }
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* From aavga.c
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   AAlib does not give us the choice of the actual resolution, thus we have to simulate additional
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   resolution by scaling down manually each frame
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void fastscale (register char *b1, register char *b2, int x1, int x2, int y1, int y2)
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	register int ex, spx = 0, ddx, ddx1;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int ddy1, ddy, spy = 0, ey;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *bb1 = b1;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!x1 || !x2 || !y1 || !y2)
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ddx = x1 + x1;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ddx1 = x2 + x2;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ddx1 < ddx)
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spx = ddx / ddx1, ddx %= ddx1;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ddy = y1 + y1;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ddy1 = y2 + y2;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ddy1 < ddy)
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spy = (ddy / ddy1) * x1, ddy %= ddy1;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ey = -ddy1;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (; y2; y2--) {
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ex = -ddx1;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (x = x2; x; x--) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*b2 = *b1;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			b2++;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			b1 += spx;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ex += ddx;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (ex > 0) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				b1++;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ex -= ddx1;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bb1 += spy;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ey += ddy;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (ey > 0) {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bb1 += x1;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ey -= ddy1;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		b1 = bb1;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Various screen update functions available */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *AA_SetVideoMode(_THIS, SDL_Surface *current,
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int width, int height, int bpp, Uint32 flags)
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int mode;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( AA_buffer ) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free( AA_buffer );
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_buffer = SDL_malloc(width * height);
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! AA_buffer ) {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't allocate buffer for requested mode");
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 	printf("Setting mode %dx%d\n", width, height); */
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(aa_image(AA_context), 0, aa_imgwidth(AA_context) * aa_imgheight(AA_context));
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(AA_buffer, 0, width * height);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the new pixel format for the screen */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_ReallocFormat(current, 8, 0, 0, 0, 0) ) {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new mode framebuffer */
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->flags = SDL_FULLSCREEN;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_w = current->w = width;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_h = current->h = height;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pitch = current->w;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pixels = AA_buffer;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_x_ratio = ((double)aa_imgwidth(AA_context)) / ((double)width);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AA_y_ratio = ((double)aa_imgheight(AA_context)) / ((double)height);
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the blit function */
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->UpdateRects = AA_DirectUpdate;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done */
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(current);
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_ResizeHandler(aa_context *context)
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_resize(context);
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	local_this->hidden->x_ratio = ((double)aa_imgwidth(context)) / ((double)local_this->screen->w);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	local_this->hidden->y_ratio = ((double)aa_imgheight(context)) / ((double)local_this->screen->h);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fastscale (local_this->hidden->buffer, aa_image(context), local_this->hidden->w, aa_imgwidth (context), local_this->hidden->h, aa_imgheight (context));
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_renderpalette(context, local_this->hidden->palette, local_this->hidden->rparams, 0, 0, aa_scrwidth(context), aa_scrheight(context));
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_flush(context);
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't actually allow hardware surfaces other than the main one */
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_AllocHWSurface(_THIS, SDL_Surface *surface)
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_FreeHWSurface(_THIS, SDL_Surface *surface)
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We need to wait for vertical retrace on page flipped displays */
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_LockHWSurface(_THIS, SDL_Surface *surface)
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* TODO ? */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_UnlockHWSurface(_THIS, SDL_Surface *surface)
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* FIXME: How is this done with AAlib? */
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int AA_FlipHWSurface(_THIS, SDL_Surface *surface)
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(AA_mutex);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_flush(AA_context);
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(AA_mutex);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect *rect;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fastscale (AA_buffer, aa_image(AA_context), AA_w, aa_imgwidth (AA_context), AA_h, aa_imgheight (AA_context));
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 1
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_renderpalette(AA_context, AA_palette, AA_rparams, 0, 0, aa_scrwidth(AA_context), aa_scrheight(AA_context));
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Render only the rectangles in the list */
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Update rects : ");
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i < numrects; ++i ) {
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rect = &rects[i];
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		printf("(%d,%d-%d,%d)", rect->x, rect->y, rect->w, rect->h);
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		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);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("\n");
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(AA_mutex);
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_flush(AA_context);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(AA_mutex);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint AA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i < ncolors; i++ ) {
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        aa_setpalette(AA_palette, firstcolor + i,
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      colors[i].r>>2,
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      colors[i].g>>2,
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      colors[i].b>>2);
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Note:  If we are terminated, this could be called in the middle of
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   another SDL video routine -- notably UpdateRects.
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid AA_VideoQuit(_THIS)
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_uninitkbd(AA_context);
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_uninitmouse(AA_context);
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Free video mode lists */
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_NUMMODES; ++i ) {
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_modelist[i] != NULL ) {
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_modelist[i]);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_modelist[i] = NULL;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aa_close(AA_context);
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DestroyMutex(AA_mutex);
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->screen->pixels = NULL;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
389