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/* BWindow based framebuffer implementation */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_BWin.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern "C" {
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysevents_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysmouse_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syswm_c.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_lowvideo.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_yuvfuncs.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysyuv.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../blank_cursor.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BEOS_HIDDEN_SIZE	32	/* starting hidden window size */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_VideoInit(_THIS, SDL_PixelFormat *vformat);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **BE_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_UpdateMouse(_THIS);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_VideoQuit(_THIS);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_AllocHWSurface(_THIS, SDL_Surface *surface);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_LockHWSurface(_THIS, SDL_Surface *surface);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_UnlockHWSurface(_THIS, SDL_Surface *surface);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_FreeHWSurface(_THIS, SDL_Surface *surface);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_ToggleFullScreen(_THIS, int fullscreen);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* OpenGL functions */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_GL_LoadLibrary(_THIS, const char *path);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void* BE_GL_GetProcAddress(_THIS, const char *proc);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_GL_MakeCurrent(_THIS);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_GL_SwapBuffers(_THIS);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* FB driver bootstrap functions */
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_Available(void)
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_DeleteDevice(SDL_VideoDevice *device)
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *BE_CreateDevice(int devindex)
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *device;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(device, 0, (sizeof *device));
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->hidden = (struct SDL_PrivateVideoData *)
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *device->hidden));
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (device == NULL) || (device->hidden == NULL) ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device ) {
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialization/Query functions */
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoInit = BE_VideoInit;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ListModes = BE_ListModes;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetVideoMode = BE_SetVideoMode;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ToggleFullScreen = BE_ToggleFullScreen;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateMouse = BE_UpdateMouse;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateYUVOverlay = BE_CreateYUVOverlay;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetColors = BE_SetColors;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateRects = NULL;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoQuit = BE_VideoQuit;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Hardware acceleration functions */
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->AllocHWSurface = BE_AllocHWSurface;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckHWBlit = NULL;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FillHWRect = NULL;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWColorKey = NULL;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWAlpha = NULL;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->LockHWSurface = BE_LockHWSurface;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UnlockHWSurface = BE_UnlockHWSurface;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FlipHWSurface = NULL;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeHWSurface = BE_FreeHWSurface;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Gamma support */
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* OpenGL support */
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_LoadLibrary = BE_GL_LoadLibrary;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_GetProcAddress = BE_GL_GetProcAddress;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_GetAttribute = BE_GL_GetAttribute;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_MakeCurrent = BE_GL_MakeCurrent;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_SwapBuffers = BE_GL_SwapBuffers;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Window manager functions */
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetCaption = BE_SetWMCaption;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetIcon = NULL;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->IconifyWindow = BE_IconifyWindow;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GrabInput = BE_GrabInput;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetWMInfo = BE_GetWMInfo;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Cursor manager functions */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeWMCursor = BE_FreeWMCursor;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateWMCursor = BE_CreateWMCursor;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ShowWMCursor = BE_ShowWMCursor;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->WarpWMCursor = BE_WarpWMCursor;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->MoveWMCursor = NULL;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckMouseMode = BE_CheckMouseMode;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Event manager functions */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->InitOSKeymap = BE_InitOSKeymap;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->PumpEvents = BE_PumpEvents;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->free = BE_DeleteDevice;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the driver flags */
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->handles_any_size = 1;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap BWINDOW_bootstrap = {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"bwindow", "BDirectWindow graphics",
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BE_Available, BE_CreateDevice
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic inline int ColorSpaceToBitsPerPixel(uint32 colorspace)
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bitsperpixel;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bitsperpixel = 0;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (colorspace) {
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_CMAP8:
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bitsperpixel = 8;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGB15:
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGBA15:
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGB15_BIG:
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGBA15_BIG:
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bitsperpixel = 15;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGB16:
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGB16_BIG:
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bitsperpixel = 16;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGB32:
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGBA32:
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGB32_BIG:
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case B_RGBA32_BIG:
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bitsperpixel = 32;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(bitsperpixel);
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to sort the display_list in bscreen */
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CompareModes(const void *A, const void *B)
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const display_mode *a = (display_mode *)A;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const display_mode *b = (display_mode *)B;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( a->space == b->space ) {
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return((b->virtual_width*b->virtual_height)-
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       (a->virtual_width*a->virtual_height));
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(ColorSpaceToBitsPerPixel(b->space)-
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       ColorSpaceToBitsPerPixel(a->space));
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Yes, this isn't the fastest it could be, but it works nicely */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_AddMode(_THIS, int index, unsigned int w, unsigned int h)
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect *mode;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int next_mode;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if we already have this mode */
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_nummodes[index] > 0 ) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=SDL_nummodes[index]-1; i >= 0; --i ) {
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mode = SDL_modelist[index][i];
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (mode->w == w) && (mode->h == h) ) {
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BWINDOW_DEBUG
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				fprintf(stderr, "We already have mode %dx%d at %d bytes per pixel\n", w, h, index+1);
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(0);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new video mode rectangle */
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mode == NULL ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->x = 0;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->y = 0;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->w = w;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->h = h;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BWINDOW_DEBUG
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Adding mode %dx%d at %d bytes per pixel\n", w, h, index+1);
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the new list of modes, and fill in the new mode */
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_mode = SDL_nummodes[index];
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index] = (SDL_Rect **)
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_modelist[index] == NULL ) {
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_nummodes[index] = 0;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mode);
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index][next_mode] = mode;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index][next_mode+1] = NULL;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_nummodes[index]++;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_VideoInit(_THIS, SDL_PixelFormat *vformat)
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	display_mode *modes;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 i, nmodes;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bpp;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BRect bounds;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize the Be Application for appserver interaction */
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_InitBeApp() < 0 ) {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* It is important that this be created after SDL_InitBeApp() */
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BScreen bscreen;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Save the current display mode */
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bscreen.GetMode(&saved_mode);
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_this->info.current_w = saved_mode.virtual_width;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_this->info.current_h = saved_mode.virtual_height;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the screen depth */
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BitsPerPixel = ColorSpaceToBitsPerPixel(bscreen.ColorSpace());
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( vformat->BitsPerPixel == 0 ) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unknown BScreen colorspace: 0x%x",
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						bscreen.ColorSpace());
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the video modes we can switch to in fullscreen mode */
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bscreen.GetModeList(&modes, &nmodes);
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_qsort(modes, nmodes, sizeof *modes, CompareModes);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<nmodes; ++i ) {
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp = ColorSpaceToBitsPerPixel(modes[i].space);
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		//if ( bpp != 0 ) { // There are bugs in changing colorspace
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( modes[i].space == saved_mode.space ) {
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			BE_AddMode(_this, ((bpp+7)/8)-1,
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				modes[i].virtual_width,
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				modes[i].virtual_height);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the window and view */
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.top = 0; bounds.left = 0;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.right = BEOS_HIDDEN_SIZE;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.bottom = BEOS_HIDDEN_SIZE;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win = new SDL_BWin(bounds);
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* testgl application doesn't load library, just tries to load symbols */
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* is it correct? if so we have to load library here */
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BE_GL_LoadLibrary(_this, NULL);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the clear cursor */
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_BlankCursor = BE_CreateWMCursor(_this, blank_cdata, blank_cmask,
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			BLANK_CWIDTH, BLANK_CHEIGHT, BLANK_CHOTX, BLANK_CHOTY);
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in some window manager capabilities */
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_this->info.wm_available = 1;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We support any dimension at our bit-depth */
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **BE_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect **modes;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modes = ((SDL_Rect **)0);
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		modes = SDL_modelist[((format->BitsPerPixel+7)/8)-1];
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( format->BitsPerPixel ==
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			_this->screen->format->BitsPerPixel ) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			modes = ((SDL_Rect **)-1);
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(modes);
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Various screen update functions available */
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Find the closest display mode for fullscreen */
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic bool BE_FindClosestFSMode(_THIS, int width, int height, int bpp,
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					 display_mode *mode)
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BScreen bscreen;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 i, nmodes;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect **modes;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	display_mode *dmodes;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	display_mode current;
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	float current_refresh;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bscreen.GetMode(&current);
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current_refresh = (1000 * current.timing.pixel_clock) /
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                  (current.timing.h_total * current.timing.v_total);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modes = SDL_modelist[((bpp+7)/8)-1];
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// find end of list (lowest-resolution mode; modes are ordered
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// highest-to-lowest).
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	i = 0; while(modes[i]) i++;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!i) return false;		// what? no modes at all?
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// find first mode with resolution >= requested in both dimensions
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (--i; i >= 0; --i)
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (modes[i]->w >= width && modes[i]->h >= height)
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// unable to find any mode with that high a resolution!
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (i < 0)
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return false;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	width = modes[i]->w;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	height = modes[i]->h;
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bscreen.GetModeList(&dmodes, &nmodes);
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < nmodes; ++i ) {
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (bpp == ColorSpaceToBitsPerPixel(dmodes[i].space)) &&
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (width == dmodes[i].virtual_width) &&
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (height == dmodes[i].virtual_height) ) {
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( i != nmodes ) {
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*mode = dmodes[i];
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((mode->virtual_width <= current.virtual_width) &&
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (mode->virtual_height <= current.virtual_height)) {
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			float new_refresh = (1000 * mode->timing.pixel_clock) /
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			                    (mode->timing.h_total * mode->timing.v_total);
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (new_refresh < current_refresh) {
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mode->timing.pixel_clock = (uint32)((mode->timing.h_total * mode->timing.v_total)
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				                                    * current_refresh / 1000);
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return true;
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return false;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// printf("SetFullScreen(%d)\n", fullscreen);
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BScreen bscreen;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// SetFullSscreen() does not work as expected if called in a window
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// that was never shown. This is probably a bug in the Haiku Game Kit that needs
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// to be investigated.
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_Win->Lock()) {
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// Show our window.
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->Show();
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_Win->IsLocked()) {
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// Unlock the window if it was locked. This is needed as only the
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// first call to Show() unlocks the looper. All other calls to it
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// will not.
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->Unlock();
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int width = screen->w;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int height = screen->h;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (fullscreen) {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// Set resolution to the closest available one that matches the
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// current SDL resolution.
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		display_mode mode;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bscreen.GetMode(&mode);
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int bpp = screen->format->BitsPerPixel;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (bpp != ColorSpaceToBitsPerPixel(mode.space) ||
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			width != mode.virtual_width || height != mode.virtual_height) {
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(BE_FindClosestFSMode(_this, width, height, bpp, &mode)) {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bscreen.SetMode(&mode);
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				// printf("Could not set new mode.\n");
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(0);
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// Reset to the previous known resolution as we are now in window
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// mode.
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bscreen.SetMode(&saved_mode);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Effectivelly set/reset full screen mode. If we are already in
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// full screen mode, we reset back to windowed mode first so the
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// window can resize when going fullscreen.
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// if (fullscreen)
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// printf("Going fullscreen\n");
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// else
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// printf("Going windowed\n");
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win->SetFullScreen(fullscreen);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Calculate offsets for centering the window (in window mode) and for
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// dentering the bitmap (in full screen mode).
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BRect bounds = bscreen.Frame();
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.PrintToStream();
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int32 cx = (bounds.IntegerWidth() - width)/2;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int32 cy = (bounds.IntegerHeight() - height)/2;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// printf ("cx = %d, cy = %d\n", cx, cy);
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!SDL_Win->IsFullScreen()) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// printf("Doing not fullscreen stuff.\n");
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// We are not in full screen mode, so we want to change the window
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// size to match the resolution in SDL.
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->ResizeTo(width, height);
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// And also center the window and reset the drawing offset.
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->MoveTo(cx, cy);
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->SetXYOffset(0, 0);
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// printf("Doing fullscreen stuff.");
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// Center the bitmap whenever we are in full screen mode.
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->SetXYOffset(cx, cy);
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Set relevant internal SDL screen flags.
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_Win->IsFullScreen()) {
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen->flags |= SDL_FULLSCREEN;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen->flags &= ~SDL_FULLSCREEN;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_ToggleFullScreen(_THIS, int fullscreen)
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return BE_SetFullScreen(_this, _this->screen, fullscreen);
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* FIXME: check return values and cleanup here */
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface *current,
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int width, int height, int bpp, Uint32 flags)
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BScreen bscreen;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BBitmap *bbitmap;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BRect bounds;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 gl_flags = 0;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Only RGB works on r5 currently */
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gl_flags = BGL_RGB;
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.double_buffer)
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gl_flags |= BGL_DOUBLE;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gl_flags |= BGL_SINGLE;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.alpha_size > 0 || bpp == 32)
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gl_flags |= BGL_ALPHA;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.depth_size > 0)
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gl_flags |= BGL_DEPTH;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.stencil_size > 0)
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gl_flags |= BGL_STENCIL;
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.accum_red_size > 0
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		|| _this->gl_config.accum_green_size > 0
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		|| _this->gl_config.accum_blue_size > 0
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		|| _this->gl_config.accum_alpha_size > 0)
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gl_flags |= BGL_ACCUM;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the view for this window, using found flags */
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Win->CreateView(flags, gl_flags) < 0 ) {
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->flags = 0;		/* Clear flags */
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->w = width;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->h = height;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win->SetType(B_TITLED_WINDOW);
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flags & SDL_NOFRAME ) {
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->flags |= SDL_NOFRAME;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->SetLook(B_NO_BORDER_WINDOW_LOOK);
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (flags & SDL_RESIZABLE) && !(flags & SDL_OPENGL) )  {
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			current->flags |= SDL_RESIZABLE;
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* We don't want opaque resizing (TM). :-) */
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Win->SetFlags(B_OUTLINE_RESIZE);
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Win->SetFlags(B_NOT_RESIZABLE|B_NOT_ZOOMABLE);
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flags & SDL_OPENGL ) {
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->flags |= SDL_OPENGL;
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pitch = 0;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pixels = NULL;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		_this->UpdateRects = NULL;
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Create the BBitmap framebuffer */
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.top = 0; bounds.left = 0;
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.right = width-1;
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.bottom = height-1;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bbitmap = new BBitmap(bounds, bscreen.ColorSpace());
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! bbitmap->IsValid() ) {
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't create screen bitmap");
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			delete bbitmap;
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pitch = bbitmap->BytesPerRow();
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->pixels = (void *)bbitmap->Bits();
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->SetBitmap(bbitmap);
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		_this->UpdateRects = BE_NormalUpdate;
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the correct fullscreen mode */
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BE_SetFullScreen(_this, current, flags & SDL_FULLSCREEN ? 1 : 0);
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done */
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(current);
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Update the current mouse state and position */
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid BE_UpdateMouse(_THIS)
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BPoint point;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 buttons;
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Win->Lock() ) {
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Get new input state, if still active */
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_Win->IsActive() ) {
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(SDL_Win->View())->GetMouse(&point, &buttons, true);
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			point.x = -1;
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			point.y = -1;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->Unlock();
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (point.x >= 0) && (point.x < SDL_VideoSurface->w) &&
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (point.y >= 0) && (point.y < SDL_VideoSurface->h) ) {
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateMouseMotion(0, 0,
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					(Sint16)point.x, (Sint16)point.y);
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't actually allow hardware surfaces other than the main one */
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_AllocHWSurface(_THIS, SDL_Surface *surface)
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_FreeHWSurface(_THIS, SDL_Surface *surface)
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int BE_LockHWSurface(_THIS, SDL_Surface *surface)
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_UnlockHWSurface(_THIS, SDL_Surface *surface)
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void BE_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Win->BeginDraw() ) {
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int i;
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<numrects; ++i ) {
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			BRect rect;
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rect.top = rects[i].y;
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rect.left = rects[i].x;
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rect.bottom = rect.top+rects[i].h-1;
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rect.right = rect.left+rects[i].w-1;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Win->DrawAsync(rect);
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Win->EndDraw();
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Passing a NULL path means load pointers from the application */
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_GL_LoadLibrary(_THIS, const char *path)
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (path == NULL) {
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (_this->gl_config.dll_handle == NULL) {
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			image_info info;
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int32 cookie = 0;
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while (get_next_image_info(0,&cookie,&info) == B_OK) {
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				void *location = NULL;
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __HAIKU__
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (get_image_symbol(info.id,"glBegin",B_SYMBOL_TYPE_ANY,&location) == B_OK) { // This is how it actually works in Haiku
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (get_image_symbol((image_id)cookie,"glBegin",B_SYMBOL_TYPE_ANY,&location) == B_OK) { // I don't know if that *did* work in BeOS
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					_this->gl_config.dll_handle = (void*)info.id;
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					_this->gl_config.driver_loaded = 1;
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_strlcpy(_this->gl_config.driver_path, "libGL.so", SDL_arraysize(_this->gl_config.driver_path));
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			FIXME None of BeOS libGL.so implementations have exported functions
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			to load BGLView, which should be reloaded from new lib.
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			So for now just "load" linked libGL.so :(
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (_this->gl_config.dll_handle == NULL) {
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return BE_GL_LoadLibrary(_this, NULL);
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Unload old first */
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*if (_this->gl_config.dll_handle != NULL) {*/
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Do not try to unload application itself (if LoadLibrary was called before with NULL ;) */
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*	image_info info;
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (get_image_info((image_id)_this->gl_config.dll_handle, &info) == B_OK) {
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (info.type != B_APP_IMAGE) {
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					unload_add_on((image_id)_this->gl_config.dll_handle);
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((_this->gl_config.dll_handle = (void*)load_add_on(path)) != (void*)B_ERROR) {
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			_this->gl_config.driver_loaded = 1;
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_strlcpy(_this->gl_config.driver_path, path, SDL_arraysize(_this->gl_config.driver_path));
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}*/
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.dll_handle != NULL) {
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		_this->gl_config.dll_handle = NULL;
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		_this->gl_config.driver_loaded = 0;
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*_this->gl_config.driver_path = '\0';
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid* BE_GL_GetProcAddress(_THIS, const char *proc)
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.dll_handle != NULL) {
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		void *location = NULL;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status_t err;
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((err = get_image_symbol((image_id)_this->gl_config.dll_handle, proc, B_SYMBOL_TYPE_ANY, &location)) == B_OK) {
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return location;
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't find OpenGL symbol");
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return NULL;
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("OpenGL library not loaded");
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FIXME? Right now BE_GL_GetAttribute shouldn't be called between glBegin() and glEnd() - it doesn't use "cached" values
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (attrib)
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_RED_SIZE:
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_RED_BITS, (GLint*)value);
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_GREEN_SIZE:
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_GREEN_BITS, (GLint*)value);
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_BLUE_SIZE:
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_BLUE_BITS, (GLint*)value);
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_ALPHA_SIZE:
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_ALPHA_BITS, (GLint*)value);
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_DOUBLEBUFFER:
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetBooleanv(GL_DOUBLEBUFFER, (GLboolean*)value);
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_BUFFER_SIZE:
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int v;
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_RED_BITS, (GLint*)&v);
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*value = v;
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_GREEN_BITS, (GLint*)&v);
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*value += v;
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_BLUE_BITS, (GLint*)&v);
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*value += v;
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_ALPHA_BITS, (GLint*)&v);
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*value += v;
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_DEPTH_SIZE:
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_DEPTH_BITS, (GLint*)value); /* Mesa creates 16 only? r5 always 32 */
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_STENCIL_SIZE:
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_STENCIL_BITS, (GLint*)value);
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_ACCUM_RED_SIZE:
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_ACCUM_RED_BITS, (GLint*)value);
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_ACCUM_GREEN_SIZE:
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_ACCUM_GREEN_BITS, (GLint*)value);
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_ACCUM_BLUE_SIZE:
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_ACCUM_BLUE_BITS, (GLint*)value);
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_ACCUM_ALPHA_SIZE:
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glGetIntegerv(GL_ACCUM_ALPHA_BITS, (GLint*)value);
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_STEREO:
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_MULTISAMPLEBUFFERS:
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_GL_MULTISAMPLESAMPLES:
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*value=0;
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_GL_MakeCurrent(_THIS)
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* FIXME: should we glview->unlock and then glview->lock()? */
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid BE_GL_SwapBuffers(_THIS)
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win->SwapBuffers();
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Is the system palette settable? */
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint BE_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Palette *palette;
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const color_map *cmap = BScreen().ColorMap();
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the screen colormap */
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	palette = _this->screen->format->palette;
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<256; ++i ) {
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		palette->colors[i].r = cmap->color_list[i].red;
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		palette->colors[i].g = cmap->color_list[i].green;
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		palette->colors[i].b = cmap->color_list[i].blue;
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid BE_VideoQuit(_THIS)
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win->Quit();
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Win = NULL;
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_BlankCursor != NULL ) {
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BE_FreeWMCursor(_this, SDL_BlankCursor);
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_BlankCursor = NULL;
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_MODELISTS; ++i ) {
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_modelist[i] ) {
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( j=0; SDL_modelist[i][j]; ++j ) {
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(SDL_modelist[i][j]);
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_modelist[i]);
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_modelist[i] = NULL;
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Restore the original video mode */
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( _this->screen ) {
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (_this->screen->flags&SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			BScreen bscreen;
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bscreen.SetMode(&saved_mode);
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		_this->screen->pixels = NULL;
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_this->gl_config.dll_handle != NULL)
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unload_add_on((image_id)_this->gl_config.dll_handle);
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_QuitBeApp();
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; /* Extern C */
842