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/* Pocket PC GAPI SDL video driver implementation;
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallImplemented by Dmitry Yakimov - support@activekitten.com
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallInspired by http://arisme.free.fr/ports/SDL.php
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: copy surface on window when lost focus
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: test buttons rotation
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: test on be300 and HPC ( check WinDib fullscreen keys catching )
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: test on smartphones
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: windib on SH3 PPC2000 landscape test
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: optimize 8bpp landscape mode
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// there is some problems in runnings apps from a device landscape mode
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// due to WinCE bugs. Some works and some - does not.
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: finish Axim Dell X30 and user landscape mode, device landscape mode
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: finish Axim Dell X30 user portrait, device landscape stylus conversion
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TODO: fix running GAPI apps from landscape mode -
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//       wince goes to portrait mode, but does not update video memory
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_error.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../wincommon/SDL_syswm_c.h"
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../wincommon/SDL_sysmouse_c.h"
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../windib/SDL_dibevents_c.h"
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../windib/SDL_gapidibvideo.h"
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gapivideo.h"
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define gapi this->hidden->gapiInfo
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GAPIVID_DRIVER_NAME "gapi"
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(DEBUG) || defined (_DEBUG) || defined(NDEBUG)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REPORT_VIDEO_INFO 1
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REPORT_VIDEO_INFO 0
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// for testing with GapiEmu
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define USE_GAPI_EMU 0
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define EMULATE_AXIM_X30 0
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WITHOUT_GAPI 0
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if USE_GAPI_EMU && !REPORT_VIDEO_INFO
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma message("Warning: Using GapiEmu in release build. I assume you'd like to set USE_GAPI_EMU to zero.")
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _T
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _T(x) L##x
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef ASSERT
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ASSERT(x)
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// defined and used in SDL_sysevents.c
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern HINSTANCE aygshell;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern void SDL_UnregisterApp();
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern int DIB_AddMode(_THIS, int bpp, int w, int h);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_VideoInit(_THIS, SDL_PixelFormat *vformat);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **GAPI_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *GAPI_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_VideoQuit(_THIS);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_AllocHWSurface(_THIS, SDL_Surface *surface);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_LockHWSurface(_THIS, SDL_Surface *surface);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_UnlockHWSurface(_THIS, SDL_Surface *surface);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_FreeHWSurface(_THIS, SDL_Surface *surface);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Windows message handling functions, will not be processed */
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_Activate(_THIS, BOOL active, BOOL minimized);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_RealizePalette(_THIS);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_PaletteChanged(_THIS, HWND window);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_WinPAINT(_THIS, HDC hdc);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* etc. */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic HMODULE g_hGapiLib = 0;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define LINK(type,name,import) \
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( g_hGapiLib ) \
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		name = (PFN##type)GetProcAddress( g_hGapiLib, _T(import) );
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char g_bRawBufferAvailable = 0;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* GAPI driver bootstrap functions */
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* hi res definitions */
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct _RawFrameBufferInfo
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   WORD wFormat;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   WORD wBPP;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   VOID *pFramePointer;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int  cxStride;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int  cyStride;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int  cxPixels;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int  cyPixels;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} RawFrameBufferInfo;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct _RawFrameBufferInfo g_RawFrameBufferInfo = {0};
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GETRAWFRAMEBUFFER   0x00020001
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define FORMAT_565 1
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define FORMAT_555 2
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define FORMAT_OTHER 3
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Dell Axim x30 hangs when we use GAPI from landscape,
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   so lets avoid using GxOpenDisplay there via GETGXINFO trick
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   It seems that GAPI subsystem use the same ExtEscape code.
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GETGXINFO 0x00020000
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct GXDeviceInfo
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halllong Version; //00 (should filled with 100 before calling ExtEscape)
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid * pvFrameBuffer; //04
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallunsigned long cbStride; //08
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallunsigned long cxWidth; //0c
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallunsigned long cyHeight; //10
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallunsigned long cBPP; //14
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallunsigned long ffFormat; //18
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar Unused[0x84-7*4];
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} GXDeviceInfo;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_Available(void)
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// try to use VGA display, even on emulator
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc = GetDC(NULL);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int result = ExtEscape(hdc, GETRAWFRAMEBUFFER, 0, NULL, sizeof(RawFrameBufferInfo), (char *)&g_RawFrameBufferInfo);
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(NULL, hdc);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	g_bRawBufferAvailable = result > 0;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//My Asus MyPAL 696 reports the RAWFRAMEBUFFER as available, but with a size of 0 x 0
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(g_RawFrameBufferInfo.cxPixels <= 0 || g_RawFrameBufferInfo.cyPixels <= 0){
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g_bRawBufferAvailable = 0;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if WITHOUT_GAPI
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return g_bRawBufferAvailable;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if USE_GAPI_EMU
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	g_hGapiLib = LoadLibrary(_T("GAPI_Emu.dll"));
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !g_hGapiLib )
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Gapi Emu not found!");
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return g_hGapiLib != 0;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// try to find gx.dll
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	g_hGapiLib = LoadLibrary(_T("\\Windows\\gx.dll"));
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !g_hGapiLib )
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g_hGapiLib = LoadLibrary(_T("gx.dll"));
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( !g_hGapiLib ) return g_bRawBufferAvailable;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int cmpmodes(const void *va, const void *vb)
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Rect *a = *(SDL_Rect **)va;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Rect *b = *(SDL_Rect **)vb;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( a->w == b->w )
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->h - a->h;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->w - a->w;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_AddMode(_THIS, int bpp, int w, int h)
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect *mode;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, index;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int next_mode;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if we already have this mode */
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( bpp < 8 ) {  /* Not supported */
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	index = ((bpp+7)/8)-1;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<gapi->SDL_nummodes[index]; ++i ) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mode = gapi->SDL_modelist[index][i];
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (mode->w == w) && (mode->h == h) ) {
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(0);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new video mode rectangle */
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mode == NULL ) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->x = 0;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->y = 0;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->w = w;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->h = h;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the new list of modes, and fill in the new mode */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_mode = gapi->SDL_nummodes[index];
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->SDL_modelist[index] = (SDL_Rect **)
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       SDL_realloc(gapi->SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gapi->SDL_modelist[index] == NULL ) {
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->SDL_nummodes[index] = 0;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mode);
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->SDL_modelist[index][next_mode] = mode;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->SDL_modelist[index][next_mode+1] = NULL;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->SDL_nummodes[index]++;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_DeleteDevice(SDL_VideoDevice *device)
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( g_hGapiLib )
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FreeLibrary(g_hGapiLib);
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g_hGapiLib = 0;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden->gapiInfo);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *GAPI_CreateDevice(int devindex)
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *device;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !g_hGapiLib && !g_bRawBufferAvailable)
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( !GAPI_Available() )
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("GAPI dll is not found and VGA mode is not available!");
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(device, 0, (sizeof *device));
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->hidden = (struct SDL_PrivateVideoData *)
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *device->hidden));
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(device->hidden){
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memset(device->hidden, 0, (sizeof *device->hidden));
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			device->hidden->gapiInfo = (GapiInfo *)SDL_malloc((sizeof(GapiInfo)));
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(device->hidden->gapiInfo == NULL)
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(device->hidden);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				device->hidden = NULL;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (device == NULL) || (device->hidden == NULL) ) {
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device ) {
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->hidden->gapiInfo, 0, (sizeof *device->hidden->gapiInfo));
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoInit = GAPI_VideoInit;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ListModes = GAPI_ListModes;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetVideoMode = GAPI_SetVideoMode;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateMouse = WIN_UpdateMouse;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateYUVOverlay = NULL;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetColors = GAPI_SetColors;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateRects = GAPI_UpdateRects;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoQuit = GAPI_VideoQuit;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->AllocHWSurface = GAPI_AllocHWSurface;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckHWBlit = NULL;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FillHWRect = NULL;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWColorKey = NULL;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWAlpha = NULL;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->LockHWSurface = GAPI_LockHWSurface;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UnlockHWSurface = GAPI_UnlockHWSurface;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FlipHWSurface = NULL;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeHWSurface = GAPI_FreeHWSurface;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetCaption = WIN_SetWMCaption;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetIcon = WIN_SetWMIcon;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->IconifyWindow = WIN_IconifyWindow;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GrabInput = WIN_GrabInput;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetWMInfo = WIN_GetWMInfo;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeWMCursor = WIN_FreeWMCursor;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateWMCursor = WIN_CreateWMCursor;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ShowWMCursor = WIN_ShowWMCursor;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->WarpWMCursor = WIN_WarpWMCursor;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    device->CheckMouseMode = WIN_CheckMouseMode;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->InitOSKeymap = DIB_InitOSKeymap;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->PumpEvents = DIB_PumpEvents;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the windows message handling functions */
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_Activate = GAPI_Activate;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_RealizePalette = GAPI_RealizePalette;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_PaletteChanged = GAPI_PaletteChanged;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_WinPAINT = GAPI_WinPAINT;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HandleMessage = DIB_HandleMessage;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->free = GAPI_DeleteDevice;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load gapi library */
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define gx device->hidden->gapiInfo->gxFunc
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXOpenDisplay, gx.GXOpenDisplay,         "?GXOpenDisplay@@YAHPAUHWND__@@K@Z" )
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXCloseDisplay, gx.GXCloseDisplay,        "?GXCloseDisplay@@YAHXZ" )
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXBeginDraw, gx.GXBeginDraw,           "?GXBeginDraw@@YAPAXXZ" )
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXEndDraw, gx.GXEndDraw,             "?GXEndDraw@@YAHXZ" )
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXOpenInput, gx.GXOpenInput,           "?GXOpenInput@@YAHXZ" )
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXCloseInput, gx.GXCloseInput,          "?GXCloseInput@@YAHXZ" )
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXGetDisplayProperties, gx.GXGetDisplayProperties,"?GXGetDisplayProperties@@YA?AUGXDisplayProperties@@XZ" )
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXGetDefaultKeys, gx.GXGetDefaultKeys,      "?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z" )
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXSuspend, gx.GXSuspend,             "?GXSuspend@@YAHXZ" )
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXResume, gx.GXResume,              "?GXResume@@YAHXZ" )
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXSetViewport, gx.GXSetViewport,         "?GXSetViewport@@YAHKKKK@Z" )
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LINK( GXIsDisplayDRAMBuffer, gx.GXIsDisplayDRAMBuffer, "?GXIsDisplayDRAMBuffer@@YAHXZ" )
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* wrong gapi.dll */
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !gx.GXOpenDisplay )
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( g_hGapiLib )
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			FreeLibrary(g_hGapiLib);
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			g_hGapiLib = 0;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !gx.GXOpenDisplay && !g_bRawBufferAvailable)
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Error: damaged or unknown gapi.dll!\n");
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_DeleteDevice(device);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap GAPI_bootstrap = {
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GAPIVID_DRIVER_NAME, "WinCE GAPI video driver",
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GAPI_Available, GAPI_CreateDevice
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void FillStructs(_THIS, BOOL useVga)
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _ARM_
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WCHAR oemstr[100];
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* fill a device properties */
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !useVga )
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxProperties = gapi->gxFunc.GXGetDisplayProperties();
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->needUpdate = 1;
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->hiresFix = 0;
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->useVga = 0;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->useGXOpenDisplay = 1;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _ARM_
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* check some devices and extract addition info */
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SystemParametersInfo( SPI_GETOEMINFO, sizeof( oemstr ), oemstr, 0 );
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// buggy iPaq38xx
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((oemstr[12] == 'H') && (oemstr[13] == '3') && (oemstr[14] == '8') && (gapi->gxProperties.cbxPitch > 0))
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->videoMem = (PIXEL*)0xac0755a0;
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->gxProperties.cbxPitch = -640;
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->gxProperties.cbyPitch = 2;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->needUpdate = 0;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if (EMULATE_AXIM_X30 == 0)
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// buggy Dell Axim X30
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( _tcsncmp(oemstr, L"Dell Axim X30", 13) == 0 )
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GXDeviceInfo gxInfo = {0};
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HDC hdc = GetDC(NULL);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int result;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gxInfo.Version = 100;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			result = ExtEscape(hdc, GETGXINFO, 0, NULL, sizeof(gxInfo), (char *)&gxInfo);
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( result > 0 )
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->useGXOpenDisplay = 0;
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->videoMem = gxInfo.pvFrameBuffer;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->needUpdate = 0;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.cbxPitch = 2;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.cbyPitch = 480;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.cxWidth = gxInfo.cxWidth;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.cyHeight = gxInfo.cyHeight;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.ffFormat = gxInfo.ffFormat;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->needUpdate = 0;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->hiresFix = 0;
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxProperties.cBPP = g_RawFrameBufferInfo.wBPP;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxProperties.cbxPitch = g_RawFrameBufferInfo.cxStride;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxProperties.cbyPitch = g_RawFrameBufferInfo.cyStride;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxProperties.cxWidth = g_RawFrameBufferInfo.cxPixels;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxProperties.cyHeight = g_RawFrameBufferInfo.cyPixels;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMem = g_RawFrameBufferInfo.pFramePointer;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->useVga = 1;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch( g_RawFrameBufferInfo.wFormat )
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case FORMAT_565:
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->gxProperties.ffFormat = kfDirect565;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case FORMAT_555:
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->gxProperties.ffFormat = kfDirect555;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* unknown pixel format, try define by BPP! */
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch( g_RawFrameBufferInfo.wBPP )
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 4:
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 8:
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.ffFormat = kfDirect;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 16:
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.ffFormat = kfDirect565;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->gxProperties.ffFormat = kfDirect;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->gxProperties.cBPP != 16 )
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gapiOrientation = SDL_ORIENTATION_UP;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( (gapi->gxProperties.cbxPitch > 0) && (gapi->gxProperties.cbyPitch > 0 ))
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gapiOrientation = SDL_ORIENTATION_UP;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( (gapi->gxProperties.cbxPitch > 0) && (gapi->gxProperties.cbyPitch < 0 ))
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gapiOrientation = SDL_ORIENTATION_RIGHT; // ipaq 3660
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( (gapi->gxProperties.cbxPitch < 0) && (gapi->gxProperties.cbyPitch > 0 ))
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gapiOrientation = SDL_ORIENTATION_LEFT; // ipaq 3800
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_CreatePalette(int ncolors, SDL_Color *colors)
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Setup a custom color palette
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   BYTE buffer[ sizeof(LOGPALETTE) + 255 * sizeof(PALETTEENTRY) ];
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int i;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   LOGPALETTE*   pLogical = (LOGPALETTE*)buffer;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   PALETTEENTRY* entries  = pLogical->palPalEntry;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   HPALETTE hPalette;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   HDC hdc;
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   for (i = 0; i < ncolors; ++i)
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   {
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Find intensity by replicating the bit patterns over a byte
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      entries[i].peRed   = colors[i].r;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      entries[i].peGreen = colors[i].g;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      entries[i].peBlue  = colors[i].b;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      entries[i].peFlags = 0;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   // Create the GDI palette object
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   pLogical->palVersion    = 0x0300;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   pLogical->palNumEntries = ncolors;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   hPalette = CreatePalette( pLogical );
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   ASSERT(hPalette);
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   // Realize the palette
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   hdc = GetDC(0);
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   SelectPalette( hdc, hPalette, FALSE );
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   RealizePalette( hdc );
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   ReleaseDC( 0, hdc );
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   DeleteObject( hPalette );
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint GAPI_VideoInit(_THIS, SDL_PixelFormat *vformat)
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i,bpp;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the window */
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DIB_CreateWindow(this) < 0 ) {
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( g_hGapiLib )
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FillStructs(this, 0);
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// SDL does not supports 2/4bpp mode, so use 16 bpp
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp = gapi->gxProperties.cBPP < 8 ? 16 : gapi->gxProperties.cBPP;
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* set up normal and landscape mode */
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_AddMode(this, bpp, gapi->gxProperties.cyHeight, gapi->gxProperties.cxWidth);
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_AddMode(this, bpp, gapi->gxProperties.cxWidth, gapi->gxProperties.cyHeight);
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* add hi-res mode */
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( g_bRawBufferAvailable &&
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		!((gapi->gxProperties.cxWidth == (unsigned)g_RawFrameBufferInfo.cxPixels) && (gapi->gxProperties.cyHeight == (unsigned)g_RawFrameBufferInfo.cyPixels)))
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FillStructs(this, 1);
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// SDL does not supports 2/4bpp mode, so use 16 bpp
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp = gapi->gxProperties.cBPP < 8 ? 16 : gapi->gxProperties.cBPP;
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* set up normal and landscape mode */
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_AddMode(this, bpp, gapi->gxProperties.cyHeight, gapi->gxProperties.cxWidth);
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_AddMode(this, bpp, gapi->gxProperties.cxWidth, gapi->gxProperties.cyHeight);
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the current screen size.
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * This is NOT necessarily the size of the Framebuffer or GAPI, as they return
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * the displaysize in ORIENTATION_UP */
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_w = GetSystemMetrics(SM_CXSCREEN);
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_h = GetSystemMetrics(SM_CYSCREEN);
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Sort the mode lists */
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_MODELISTS; ++i ) {
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( gapi->SDL_nummodes[i] > 0 ) {
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_qsort(gapi->SDL_modelist[i], gapi->SDL_nummodes[i], sizeof *gapi->SDL_modelist[i], cmpmodes);
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BitsPerPixel = gapi->gxProperties.cBPP < 8 ? 16 : (unsigned char)gapi->gxProperties.cBPP;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Get color mask
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (gapi->gxProperties.ffFormat & kfDirect565) {
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->BitsPerPixel = 16;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Rmask = 0x0000f800;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Gmask = 0x000007e0;
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Bmask = 0x0000001f;
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMode = GAPI_DIRECT_565;
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (gapi->gxProperties.ffFormat & kfDirect555) {
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->BitsPerPixel = 16;
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Rmask = 0x00007c00;
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Gmask = 0x000003e0;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Bmask = 0x0000001f;
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMode = GAPI_DIRECT_555;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((gapi->gxProperties.ffFormat & kfDirect) && (gapi->gxProperties.cBPP < 8)) {
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// We'll perform the conversion
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->BitsPerPixel = 16;
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Rmask = 0x0000f800; // 16 bit 565
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Gmask = 0x000007e0;
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->Bmask = 0x0000001f;
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (gapi->gxProperties.ffFormat & kfDirectInverted)
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->invert = (1 << gapi->gxProperties.cBPP) - 1;
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->colorscale = gapi->gxProperties.cBPP < 8 ? 8 - gapi->gxProperties.cBPP : 0;
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMode = GAPI_MONO;
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (gapi->gxProperties.ffFormat & kfPalette) {
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMode = GAPI_PALETTE;
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **GAPI_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(gapi->SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//  	 return (SDL_Rect **) -1;
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *GAPI_SetVideoMode(_THIS, SDL_Surface *current,
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int width, int height, int bpp, Uint32 flags)
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *video;
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 Rmask, Gmask, Bmask;
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD style;
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect allScreen;
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( bpp < 4 )
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("1 bpp and 2 bpp modes is not implemented yet!");
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Recalculate bitmasks if necessary */
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (bpp == current->format->BitsPerPixel) {
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video = current;
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else {
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch(bpp) {
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 8:
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Rmask = 0;
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Gmask = 0;
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Bmask = 0;
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 15:
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 16:
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Default is 565 unless the display is specifically 555 */
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (gapi->gxProperties.ffFormat & kfDirect555) {
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Rmask = 0x00007c00;
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Gmask = 0x000003e0;
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Bmask = 0x0000001f;
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				else {
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Rmask = 0x0000f800;
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Gmask = 0x000007e0;
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Bmask = 0x0000001f;
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 24:
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 32:
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Rmask = 0x00ff0000;
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Gmask = 0x0000ff00;
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Bmask = 0x000000ff;
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_SetError("Unsupported Bits Per Pixel format requested");
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return NULL;
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video = SDL_CreateRGBSurface(SDL_SWSURFACE,
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					0, 0, bpp, Rmask, Gmask, Bmask, 0);
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( video == NULL ) {
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->userOrientation = SDL_ORIENTATION_UP;
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->systemOrientation = SDL_ORIENTATION_UP;
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->flags = SDL_FULLSCREEN;	/* Clear flags, GAPI supports fullscreen only */
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* GAPI or VGA? */
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( g_hGapiLib )
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FillStructs(this, 0);
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( (((unsigned)width != gapi->gxProperties.cxWidth) || ((unsigned)height != gapi->gxProperties.cyHeight))
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			&& (((unsigned)width != gapi->gxProperties.cyHeight) || ((unsigned)height != gapi->gxProperties.cxWidth)))
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			FillStructs(this, 1); // gapi is found but we use VGA resolution
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FillStructs(this, 1);
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !gapi->needUpdate && !gapi->videoMem) {
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't get address of video memory, may be unsupported device or bug");
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* detect user landscape mode */
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if( (width > height) && (gapi->gxProperties.cxWidth < gapi->gxProperties.cyHeight))
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->userOrientation = SDL_ORIENTATION_RIGHT;
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if(GetSystemMetrics(SM_CYSCREEN) < GetSystemMetrics(SM_CXSCREEN))
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->systemOrientation = SDL_ORIENTATION_RIGHT;
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->hiresFix = 0;
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* check hires */
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(GetSystemMetrics(SM_CXSCREEN) < width && GetSystemMetrics(SM_CYSCREEN) < height)
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    gapi->hiresFix = 1;
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch( gapi->userOrientation )
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_ORIENTATION_UP:
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->startOffset = 0;
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->dstLineStep = gapi->gxProperties.cbyPitch;
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->dstPixelStep = gapi->gxProperties.cbxPitch;
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_ORIENTATION_RIGHT:
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch( gapi->gapiOrientation )
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_ORIENTATION_UP:
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_ORIENTATION_RIGHT:
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_ORIENTATION_LEFT:
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( (gapi->videoMode == GAPI_MONO) )
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->startOffset = -gapi->gxProperties.cbxPitch + 1; // monochrome mode
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gapi->startOffset = gapi->gxProperties.cbyPitch * (gapi->gxProperties.cyHeight - 1);
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->dstLineStep = gapi->gxProperties.cbxPitch;
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->dstPixelStep = -gapi->gxProperties.cbyPitch;
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->w = gapi->w = width;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->h = gapi->h = height;
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->pitch = SDL_CalculatePitch(video);
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Small fix for WinCE/Win32 - when activating window
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   SDL_VideoSurface is equal to zero, so activating code
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   is not called properly for fullscreen windows because
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   macros WINDIB_FULLSCREEN uses SDL_VideoSurface
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoSurface = video;
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* GAPI is always fullscreen, title bar is useless */
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	style = 0;
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!SDL_windowid)
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetWindowLong(SDL_Window, GWL_STYLE, style);
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate bitmap */
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->buffer )
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free( gapi->buffer );
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->buffer = NULL;
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gapi->buffer = SDL_malloc(video->h * video->pitch);
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->pixels = gapi->buffer;
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! gapi->buffer ) {
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't allocate buffer for requested mode");
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gapi->buffer, 255, video->h * video->pitch);
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MoveWindow(SDL_Window, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), FALSE);
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ShowWindow(SDL_Window, SW_SHOW);
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetForegroundWindow(SDL_Window);
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* JC 14 Mar 2006
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Flush the message loop or this can cause big problems later
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Especially if the user decides to use dialog boxes or assert()!
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_FlushMessageQueue();
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       /* Open GAPI display */
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if( !gapi->useVga && gapi->useGXOpenDisplay && !gapi->alreadyGXOpened )
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       {
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if REPORT_VIDEO_INFO
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               printf("system display width  (orig): %d\n", GetSystemMetrics(SM_CXSCREEN));
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               printf("system display height (orig): %d\n", GetSystemMetrics(SM_CYSCREEN));
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               gapi->alreadyGXOpened = 1;
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( !gapi->gxFunc.GXOpenDisplay(SDL_Window, GX_FULLSCREEN) )
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't initialize GAPI");
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       }
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(gapi->useVga)
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->coordinateTransform = (4 - gapi->systemOrientation + gapi->userOrientation) % 4;
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->coordinateTransform = gapi->userOrientation;
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if REPORT_VIDEO_INFO
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Video properties:\n");
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("display bpp: %d\n", gapi->gxProperties.cBPP);
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("display width: %d\n", gapi->gxProperties.cxWidth);
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("display height: %d\n", gapi->gxProperties.cyHeight);
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       printf("system display width: %d\n", GetSystemMetrics(SM_CXSCREEN));
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       printf("system display height: %d\n", GetSystemMetrics(SM_CYSCREEN));
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("x pitch: %d\n", gapi->gxProperties.cbxPitch);
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("y pitch: %d\n", gapi->gxProperties.cbyPitch);
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("gapi flags: 0x%x\n", gapi->gxProperties.ffFormat);
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       printf("user orientation: %d\n", gapi->userOrientation);
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("system orientation: %d\n", gapi->systemOrientation);
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       printf("gapi orientation: %d\n", gapi->gapiOrientation);
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !gapi->useVga && gapi->useGXOpenDisplay && gapi->needUpdate)
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMem = gapi->gxFunc.GXBeginDraw();
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxFunc.GXEndDraw();
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("video memory: 0x%x\n", gapi->videoMem);
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("need update: %d\n", gapi->needUpdate);
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("hi-res fix: %d\n", gapi->hiresFix);
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("VGA is available on the device: %d\n", g_bRawBufferAvailable);
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("use raw framebuffer: %d\n", gapi->useVga);
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("video surface bpp: %d\n", video->format->BitsPerPixel);
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("video surface width: %d\n", video->w);
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("video surface height: %d\n", video->h);
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("mouse/arrows transformation angle: %d\n", gapi->coordinateTransform);
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Blank screen */
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	allScreen.x = allScreen.y = 0;
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	allScreen.w = video->w - 1;
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	allScreen.h = video->h - 1;
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GAPI_UpdateRects(this, 1, &allScreen);
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done */
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(video);
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't actually allow hardware surfaces other than the main one */
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_AllocHWSurface(_THIS, SDL_Surface *surface)
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_FreeHWSurface(_THIS, SDL_Surface *surface)
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We need to wait for vertical retrace on page flipped displays */
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GAPI_LockHWSurface(_THIS, SDL_Surface *surface)
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_UnlockHWSurface(_THIS, SDL_Surface *surface)
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int updateLine8to8(_THIS, unsigned char *srcPointer, unsigned char *destPointer, int width, int height, int lines)
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->dstPixelStep == 1) /* optimized blitting on most devices */
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(destPointer, srcPointer, width);
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// TODO: read 4 pixels, write DWORD
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int step = gapi->dstPixelStep;
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(width--)
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*destPointer = *srcPointer++;
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			destPointer += step;
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 1;
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Video memory is very slow so lets optimize as much as possible */
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int updateLine16to16(_THIS, PIXEL *srcPointer, PIXEL *destPointer, int width, int height, int lines)
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PIXEL *line1, *line2;
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int step = gapi->dstPixelStep / 2;
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( step == 1 ) /* optimized blitting on most devices */
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(destPointer, srcPointer, width * sizeof(PIXEL));
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( (gapi->gapiOrientation != SDL_ORIENTATION_UP) &&
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(gapi->userOrientation == SDL_ORIENTATION_UP )) // iPaq 3660/3800 and user orientation up
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			// to prevent data misalignment copy only one line
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( ((((unsigned)destPointer & 3) != 0) && (gapi->gapiOrientation == SDL_ORIENTATION_LEFT))
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				|| ((((unsigned)destPointer & 3) == 0) && (gapi->gapiOrientation != SDL_ORIENTATION_LEFT))
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				|| (lines == 1) )
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while(width--)
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*destPointer = *srcPointer++;
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					destPointer += step;
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return 1;
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* read two lines at the same time, write DWORD */
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			line1 = srcPointer;
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			line2 = srcPointer + SDL_VideoSurface->pitch / 2;
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( gapi->gapiOrientation == SDL_ORIENTATION_LEFT )
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while(width--) // iPaq 3800
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*(DWORD*)destPointer =(*line2++ << 16) | *line1++;
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					destPointer += step;
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				destPointer += gapi->gxProperties.cbyPitch / 2;
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while(width--) // iPaq 3660
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*(DWORD*)destPointer =(*line1++ << 16) | *line2++;
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					destPointer += step;
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 2;
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			// iPaq 3800 and user orientation landscape
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( gapi->gapiOrientation == SDL_ORIENTATION_LEFT )
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int w1;
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				// to prevent data misalignment copy only one pixel
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if( (((unsigned)destPointer & 3) == 0) && (width > 0))
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*destPointer-- = *srcPointer++;
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					width--;
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				destPointer--;
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				w1 = width / 2;
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while(w1--)
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					DWORD p = *(DWORD*)srcPointer;
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*((DWORD*)destPointer) = (p << 16) | (p >> 16);
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					destPointer -= 2;
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					srcPointer += 2;
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if( width & 1 ) // copy the last pixel
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					destPointer++;
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*destPointer = *srcPointer;
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return 1;
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			// modern iPaqs and user orientation landscape
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			// read two pixels, write DWORD
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			line1 = srcPointer;
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			line2 = srcPointer + SDL_VideoSurface->pitch / 2;
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( (((unsigned)destPointer & 3) != 0) || (lines == 1) )
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while(width--)
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*destPointer = *srcPointer++;
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					destPointer += step;
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return 1;
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while(width--)
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(DWORD*)destPointer =(*line2++ << 16) | *line1++;
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				destPointer -= gapi->gxProperties.cbyPitch / 2;
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 2;
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Color component masks for 565
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REDMASK (31<<11)
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GREENMASK (63<<5)
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BLUEMASK (31)
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int updateLine16to4(_THIS, PIXEL *srcPointer, unsigned char *destPointer, int width, int height, int lines, int yNibble, int xNibble)
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PIXEL *line1, *line2;
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int step = gapi->dstPixelStep;
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->userOrientation == SDL_ORIENTATION_UP )
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( yNibble ) // copy bottom half of a line
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while(width--)
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				PIXEL c1 = *srcPointer++;
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				c1 = ((c1 & REDMASK) >> 11) + ((c1 & GREENMASK) >> 5) + (c1 & BLUEMASK);
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*destPointer = (*destPointer & 0x0F) | ((~(c1 >> 3) << 4));
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				destPointer += step;
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 1;
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// either 1 pixel picture or tail, anyway this is the last line
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( lines == 1 )
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while(width--)
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				PIXEL c1 = *srcPointer++;
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				c1 = ((c1 & REDMASK) >> 11) + ((c1 & GREENMASK) >> 5) + (c1 & BLUEMASK);
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*destPointer = (*destPointer & 0xF0) | ((~(c1 >> 3) & 0xF));
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				destPointer += step;
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 1;
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		line1 = srcPointer;
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		line2 = srcPointer + SDL_VideoSurface->pitch / 2;
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(width--)
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PIXEL c1 = *line1++;
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PIXEL c2 = *line2++;
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			c1 = ((c1 & REDMASK) >> 11) + ((c1 & GREENMASK) >> 5) + (c1 & BLUEMASK);
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			c2 = ((c2 & REDMASK) >> 11) + ((c2 & GREENMASK) >> 5) + (c2 & BLUEMASK);
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*destPointer = ~((c1 >> 3) + ((c2 >> 3) << 4));
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			destPointer += step;
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 2;
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int w1;
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		w1 = width / 2;
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( xNibble )
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			// copy one pixel
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PIXEL c1 = *srcPointer++;
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			c1 = ((c1 & REDMASK) >> 11) + ((c1 & GREENMASK) >> 5) + (c1 & BLUEMASK);
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*destPointer = (*destPointer & 0xF0) | ((~(c1 >> 3) & 0xF));
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			destPointer++;
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(w1--)
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PIXEL c1 = *srcPointer;
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PIXEL c2 = *(srcPointer + 1);
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			c1 = ((c1 & REDMASK) >> 11) + ((c1 & GREENMASK) >> 5) + (c1 & BLUEMASK);
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			c2 = ((c2 & REDMASK) >> 11) + ((c2 & GREENMASK) >> 5) + (c2 & BLUEMASK);
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*destPointer++ = ~((c2 >> 3) + ((c1 >> 3) << 4));
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcPointer += 2;
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// copy tail
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( (width & 1) && !xNibble )
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			PIXEL c1 = *srcPointer;
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			c1 = ((c1 & REDMASK) >> 11) + ((c1 & GREENMASK) >> 5) + (c1 & BLUEMASK);
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*destPointer = (*destPointer & 0x0F) | ((~(c1 >> 3) << 4));
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_UpdateRectsMono(_THIS, int numrects, SDL_Rect *rects)
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, height;
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int linesProcessed;
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int xNibble, yNibble;
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0; i<numrects; i++)
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned char *destPointer;
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned char *srcPointer;
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( gapi->userOrientation == SDL_ORIENTATION_UP )
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			destPointer = (unsigned char*) gapi->videoMem + gapi->startOffset - rects[i].y * gapi->gxProperties.cBPP / 8 + rects[i].x * gapi->dstPixelStep;
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			destPointer = (unsigned char*) gapi->videoMem + gapi->startOffset + rects[i].x * gapi->gxProperties.cBPP / 8 + rects[i].y * gapi->dstLineStep;
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		srcPointer = ((unsigned char*) SDL_VideoSurface->pixels) + rects[i].y * SDL_VideoSurface->pitch + rects[i].x * 2;
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		yNibble = rects[i].y & 1; // TODO: only for 4 bpp
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xNibble = rects[i].x & 1;
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		height = rects[i].h;
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while (height > 0)
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch(gapi->gxProperties.cBPP)
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 2: // TODO
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 4:
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					linesProcessed = updateLine16to4(this, (PIXEL*) srcPointer, destPointer, rects[i].w, rects[i].h, height, yNibble, xNibble);
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					yNibble = 0;
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			height -= linesProcessed;
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( gapi->userOrientation == SDL_ORIENTATION_UP )
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				destPointer--; // always fill 1 byte
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else destPointer += gapi->dstLineStep;
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcPointer += SDL_VideoSurface->pitch * linesProcessed; // pitch in bytes
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_UpdateRectsColor(_THIS, int numrects, SDL_Rect *rects)
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, height;
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bytesPerPixel = (gapi->gxProperties.cBPP + 1) / 8;
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int linesProcessed;
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0; i<numrects; i++) {
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned char *destPointer = (unsigned char*) gapi->videoMem + gapi->startOffset + rects[i].y * gapi->dstLineStep + rects[i].x * gapi->dstPixelStep;
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned char *srcPointer = ((unsigned char*) SDL_VideoSurface->pixels) + rects[i].y * SDL_VideoSurface->pitch + rects[i].x * bytesPerPixel;
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		height = rects[i].h;
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//		fprintf(stderr, "Starting rect %dx%d, dst=0x%x, w = %d, h = %d\n", rects[i].w, rects[i].h,destPointer,rects[i].w,rects[i].h);
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//		fflush(stderr);
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		linesProcessed = height;
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while (height > 0) {
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch(bytesPerPixel)
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 1:
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				linesProcessed = updateLine8to8(this, srcPointer, (unsigned char *) destPointer, rects[i].w, rects[i].h, height);
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 2:
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma warning(disable: 4133)
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				linesProcessed = updateLine16to16(this, (PIXEL*) srcPointer, destPointer, rects[i].w, rects[i].h, height);
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			height -= linesProcessed;
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			destPointer += gapi->dstLineStep * linesProcessed;
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcPointer += SDL_VideoSurface->pitch * linesProcessed; // pitch in bytes
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//		fprintf(stderr, "End of rect\n");
11449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//		fflush(stderr);
11459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
11509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// we do not want to corrupt video memory
11529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->suspended ) return;
11539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->needUpdate )
11559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->videoMem = gapi->gxFunc.GXBeginDraw();
11569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->gxProperties.cBPP < 8 )
11589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_UpdateRectsMono(this, numrects, rects);
11599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
11609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GAPI_UpdateRectsColor(this, numrects, rects);
11619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->needUpdate )
11639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gapi->gxFunc.GXEndDraw();
11649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Note:  If we are terminated, this could be called in the middle of
11679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   another SDL video routine -- notably UpdateRects.
11689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
11699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GAPI_VideoQuit(_THIS)
11709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
11729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Destroy the window and everything associated with it */
11739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Window )
11749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
11759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if ((g_hGapiLib != 0) && this && gapi && gapi->gxFunc.GXCloseDisplay && !gapi->useVga)
11769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->gxFunc.GXCloseDisplay();
11779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (this->screen->pixels != NULL)
11799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
11809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this->screen->pixels);
11819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->screen->pixels = NULL;
11829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_icn ) {
11849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DestroyIcon(screen_icn);
11859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_icn = NULL;
11869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_DestroyWindow(this);
11899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnregisterApp();
11909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Window = NULL;
11929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_WIN32_WCE)
11939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Unload wince aygshell library to prevent leak
11959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( aygshell )
11969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
11979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			FreeLibrary(aygshell);
11989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			aygshell = NULL;
11999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Free video mode lists */
12039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_MODELISTS; ++i ) {
12049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( gapi->SDL_modelist[i] != NULL ) {
12059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( j=0; gapi->SDL_modelist[i][j]; ++j )
12069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(gapi->SDL_modelist[i][j]);
12079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(gapi->SDL_modelist[i]);
12089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gapi->SDL_modelist[i] = NULL;
12099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_Activate(_THIS, BOOL active, BOOL minimized)
12179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//Nothing to do here (as far as I know)
12199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_RealizePalette(_THIS)
12229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OutputDebugString(TEXT("GAPI_RealizePalette NOT IMPLEMENTED !\r\n"));
12249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_PaletteChanged(_THIS, HWND window)
12279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OutputDebugString(TEXT("GAPI_PaletteChanged NOT IMPLEMENTED !\r\n"));
12299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GAPI_WinPAINT(_THIS, HDC hdc)
12329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// draw current offscreen buffer on hdc
12349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bpp = 16; // we always use either 8 or 16 bpp internally
12369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HGDIOBJ prevObject;
12379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned short *bitmapData;
12389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HBITMAP hb;
12399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC srcDC;
12409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Create a DIB
12429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BYTE buffer[sizeof(BITMAPINFOHEADER) + 3 * sizeof(RGBQUAD)] = {0};
12439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BITMAPINFO*       pBMI    = (BITMAPINFO*)buffer;
12449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BITMAPINFOHEADER* pHeader = &pBMI->bmiHeader;
12459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DWORD*            pColors = (DWORD*)&pBMI->bmiColors;
12469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// CreateDIBSection does not support 332 pixel format on wce
12489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( gapi->gxProperties.cBPP == 8 ) return;
12499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // DIB Header
12519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biSize            = sizeof(BITMAPINFOHEADER);
12529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biWidth           = gapi->w;
12539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biHeight          = -gapi->h;
12549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biPlanes          = 1;
12559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biBitCount        = bpp;
12569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biCompression     = BI_RGB;
12579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pHeader->biSizeImage       = (gapi->w * gapi->h * bpp) / 8;
12589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Color masks
12609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( bpp == 16 )
12619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
12629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pColors[0] = REDMASK;
12639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pColors[1] = GREENMASK;
12649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pColors[2] = BLUEMASK;
12659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pHeader->biCompression = BI_BITFIELDS;
12669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Create the DIB
12689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hb =  CreateDIBSection( 0, pBMI, DIB_RGB_COLORS, (void**)&bitmapData, 0, 0 );
12699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// copy data
12719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// FIXME: prevent misalignment, but I've never seen non aligned width of screen
12729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	memcpy(bitmapData, gapi->buffer, pHeader->biSizeImage);
12739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	srcDC = CreateCompatibleDC(hdc);
12749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	prevObject = SelectObject(srcDC, hb);
12759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BitBlt(hdc, 0, 0, gapi->w, gapi->h, srcDC, 0, 0, SRCCOPY);
12779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SelectObject(srcDC, prevObject);
12799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DeleteObject(hb);
12809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DeleteDC(srcDC);
12819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint GAPI_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
12849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GAPI_CreatePalette(ncolors, colors);
12869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 1;
12879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1288