From a855d6202536ff28e5aae4f22a0f31d8f5b325d0 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sat, 21 Jan 2017 15:18:31 -0500 Subject: Port of Duke Nukem 3D This ports Fabien Sanglard's Chocolate Duke to run on a version of SDL for Rockbox. Change-Id: I8f2c4c78af19de10c1633ed7bb7a997b43256dd9 --- apps/plugins/sdl/src/video/windx5/SDL_dx5events.c | 1005 ++++++++ .../plugins/sdl/src/video/windx5/SDL_dx5events_c.h | 37 + apps/plugins/sdl/src/video/windx5/SDL_dx5video.c | 2537 ++++++++++++++++++++ apps/plugins/sdl/src/video/windx5/SDL_dx5video.h | 61 + apps/plugins/sdl/src/video/windx5/SDL_dx5yuv.c | 296 +++ apps/plugins/sdl/src/video/windx5/SDL_dx5yuv_c.h | 38 + apps/plugins/sdl/src/video/windx5/directx.h | 97 + 7 files changed, 4071 insertions(+) create mode 100644 apps/plugins/sdl/src/video/windx5/SDL_dx5events.c create mode 100644 apps/plugins/sdl/src/video/windx5/SDL_dx5events_c.h create mode 100644 apps/plugins/sdl/src/video/windx5/SDL_dx5video.c create mode 100644 apps/plugins/sdl/src/video/windx5/SDL_dx5video.h create mode 100644 apps/plugins/sdl/src/video/windx5/SDL_dx5yuv.c create mode 100644 apps/plugins/sdl/src/video/windx5/SDL_dx5yuv_c.h create mode 100644 apps/plugins/sdl/src/video/windx5/directx.h (limited to 'apps/plugins/sdl/src/video/windx5') diff --git a/apps/plugins/sdl/src/video/windx5/SDL_dx5events.c b/apps/plugins/sdl/src/video/windx5/SDL_dx5events.c new file mode 100644 index 0000000000..e12092fd68 --- /dev/null +++ b/apps/plugins/sdl/src/video/windx5/SDL_dx5events.c @@ -0,0 +1,1005 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2012 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +/* CAUTION!!!! If you modify this file, check ../windib/SDL_sysevents.c */ + +#include "directx.h" + +#include "SDL_main.h" +#include "SDL_events.h" +#include "SDL_video.h" +#include "SDL_syswm.h" +#include "../../events/SDL_sysevents.h" +#include "../../events/SDL_events_c.h" +#include "../wincommon/SDL_lowvideo.h" +#include "SDL_dx5video.h" + +#ifndef WM_APP +#define WM_APP 0x8000 +#endif + +#ifdef _WIN32_WCE +#define NO_GETKEYBOARDSTATE +#endif + +/* The keyboard and mouse device input */ +#define MAX_INPUTS 2 +#define INPUT_QSIZE 512 /* Buffer up to 512 input messages */ + +static LPDIRECTINPUT dinput = NULL; +static LPDIRECTINPUTDEVICE2 SDL_DIdev[MAX_INPUTS]; +static HANDLE SDL_DIevt[MAX_INPUTS]; +static void (*SDL_DIfun[MAX_INPUTS])(const int, DIDEVICEOBJECTDATA *); +static int SDL_DIndev = 0; +static int mouse_lost; +static int mouse_pressed; +static int mouse_buttons_swapped = 0; + +/* The translation table from a DirectInput scancode to an SDL keysym */ +static SDLKey DIK_keymap[256]; +static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym *keysym, int pressed); + +/* DJM: If the user setup the window for us, we want to save his window proc, + and give him a chance to handle some messages. */ +#ifdef STRICT +#define WNDPROCTYPE WNDPROC +#else +#define WNDPROCTYPE FARPROC +#endif +static WNDPROCTYPE userWindowProc = NULL; + +static HWND GetTopLevelParent(HWND hWnd) +{ + HWND hParentWnd; + while (1) + { + hParentWnd = GetParent(hWnd); + if (hParentWnd == NULL) + break; + hWnd = hParentWnd; + } + return hWnd; +} + +/* Convert a DirectInput return code to a text message */ +static void SetDIerror(char *function, int code) +{ + static char *error; + static char errbuf[1024]; + + errbuf[0] = 0; + switch (code) { + case DIERR_GENERIC: + error = "Undefined error!"; + break; + case DIERR_OLDDIRECTINPUTVERSION: + error = "Your version of DirectInput needs upgrading"; + break; + case DIERR_INVALIDPARAM: + error = "Invalid parameters"; + break; + case DIERR_OUTOFMEMORY: + error = "Out of memory"; + break; + case DIERR_DEVICENOTREG: + error = "Device not registered"; + break; + case DIERR_NOINTERFACE: + error = "Interface not supported"; + break; + case DIERR_NOTINITIALIZED: + error = "Device not initialized"; + break; + default: + SDL_snprintf(errbuf, SDL_arraysize(errbuf), + "%s: Unknown DirectInput error: 0x%x", + function, code); + break; + } + if ( ! errbuf[0] ) { + SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error); + } + SDL_SetError("%s", errbuf); + return; +} + +/* Initialize DirectInput + Note: If NONEXCLUSIVE access is requested for the devices, normal + windows input messages will continue to be generated for that + input device, in addition to DirectInput messages. + */ +static void handle_keyboard(const int numevents, DIDEVICEOBJECTDATA *events); +static void handle_mouse(const int numevents, DIDEVICEOBJECTDATA *events); +struct { + char *name; + REFGUID guid; + LPCDIDATAFORMAT format; + DWORD win_level; + DWORD raw_level; + void (*fun)(const int numevents, DIDEVICEOBJECTDATA *events); +} inputs[] = { + { "keyboard", + &GUID_SysKeyboard, &c_dfDIKeyboard, + (DISCL_FOREGROUND|DISCL_NONEXCLUSIVE), + (DISCL_FOREGROUND|DISCL_NONEXCLUSIVE), handle_keyboard }, + { "mouse", + &GUID_SysMouse, +#if DIRECTINPUT_VERSION >= 0x700 + &c_dfDIMouse2, +#else + &c_dfDIMouse, +#endif + (DISCL_BACKGROUND|DISCL_NONEXCLUSIVE), + (DISCL_BACKGROUND|DISCL_NONEXCLUSIVE), handle_mouse }, + { NULL, NULL, NULL, 0, 0, NULL } +}; + +static int DX5_DInputInit(_THIS) +{ + int i; + LPDIRECTINPUTDEVICE device; + HRESULT result; + DIPROPDWORD dipdw; + HWND topwnd; + + /* Create the DirectInput object */ + result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION, + &dinput, NULL); + if ( result != DI_OK ) { + SetDIerror("DirectInputCreate", result); + return(-1); + } + + /* Create all of our registered input devices */ + SDL_DIndev = 0; + for ( i=0; inputs[i].name; ++i ) { + /* Create the DirectInput device */ + result = IDirectInput_CreateDevice(dinput, inputs[i].guid, + &device, NULL); + if ( result != DI_OK ) { + SetDIerror("DirectInput::CreateDevice", result); + return(-1); + } + result = IDirectInputDevice_QueryInterface(device, + &IID_IDirectInputDevice2, (LPVOID *)&SDL_DIdev[i]); + IDirectInputDevice_Release(device); + if ( result != DI_OK ) { + SetDIerror("DirectInputDevice::QueryInterface", result); + return(-1); + } + topwnd = GetTopLevelParent(SDL_Window); + result = IDirectInputDevice2_SetCooperativeLevel(SDL_DIdev[i], + topwnd, inputs[i].win_level); + if ( result != DI_OK ) { + SetDIerror("DirectInputDevice::SetCooperativeLevel", + result); + return(-1); + } + result = IDirectInputDevice2_SetDataFormat(SDL_DIdev[i], + inputs[i].format); + if ( result != DI_OK ) { + SetDIerror("DirectInputDevice::SetDataFormat", result); + return(-1); + } + + /* Set buffered input -- we aren't polling */ + SDL_memset(&dipdw, 0, sizeof(dipdw)); + dipdw.diph.dwSize = sizeof(dipdw); + dipdw.diph.dwHeaderSize = sizeof(dipdw.diph); + dipdw.diph.dwObj = 0; + dipdw.diph.dwHow = DIPH_DEVICE; + dipdw.dwData = INPUT_QSIZE; + result = IDirectInputDevice2_SetProperty(SDL_DIdev[i], + DIPROP_BUFFERSIZE, &dipdw.diph); + if ( result != DI_OK ) { + SetDIerror("DirectInputDevice::SetProperty", result); + return(-1); + } + + /* Create an event to be signaled when input is ready */ + SDL_DIevt[i] = CreateEvent(NULL, FALSE, FALSE, NULL); + if ( SDL_DIevt[i] == NULL ) { + SDL_SetError("Couldn't create DirectInput event"); + return(-1); + } + result = IDirectInputDevice2_SetEventNotification(SDL_DIdev[i], + SDL_DIevt[i]); + if ( result != DI_OK ) { + SetDIerror("DirectInputDevice::SetEventNotification", + result); + return(-1); + } + SDL_DIfun[i] = inputs[i].fun; + + /* Acquire the device for input */ + IDirectInputDevice2_Acquire(SDL_DIdev[i]); + + /* Increment the number of devices we have */ + ++SDL_DIndev; + } + mouse_pressed = 0; + mouse_buttons_swapped = GetSystemMetrics(SM_SWAPBUTTON); + + /* DirectInput is ready! */ + return(0); +} + +/* Clean up DirectInput */ +static void DX5_DInputQuit(_THIS) +{ + int i; + + if ( dinput != NULL ) { + /* Close and release all DirectInput devices */ + for ( i=0; iw/2); + center.y = (SDL_VideoSurface->h/2); + ClientToScreen(SDL_Window, ¢er); + SetCursorPos(center.x, center.y); + } + } +} + +static void handle_mouse(const int numevents, DIDEVICEOBJECTDATA *ptrbuf) +{ + int i; + Sint16 xrel, yrel; + Uint8 state; + Uint8 button; + DWORD timestamp = 0; + + /* Sanity check. Mailing list reports this being NULL unexpectedly. */ + if (SDL_PublicSurface == NULL) { + return; + } + + /* If mouse focus has been lost, make sure we release the cursor. */ + if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) { + mouse_lost = 1; + ClipCursor(NULL); + } else { + /* If the mouse was lost, regain some sense of mouse state */ + if ( mouse_lost ) { + POINT mouse_pos; + Uint8 old_state; + Uint8 new_state; + + /* Set ourselves up with the current cursor position */ + GetCursorPos(&mouse_pos); + ScreenToClient(SDL_Window, &mouse_pos); + post_mouse_motion( 0, (Sint16)mouse_pos.x, (Sint16)mouse_pos.y); + + /* Check for mouse button changes */ + old_state = SDL_GetMouseState(NULL, NULL); + new_state = 0; + { /* Get the new DirectInput button state for the mouse */ + #if DIRECTINPUT_VERSION >= 0x700 + DIMOUSESTATE2 distate; + #else + DIMOUSESTATE distate; + #endif + HRESULT result; + + result=IDirectInputDevice2_GetDeviceState(SDL_DIdev[1], + sizeof(distate), &distate); + if ( result != DI_OK ) { + /* Try again next time */ + SetDIerror( + "IDirectInputDevice2::GetDeviceState", result); + return; + } + for ( i=3; i>=0; --i ) { + if ( (distate.rgbButtons[i]&0x80) == 0x80 ) { + new_state |= 0x01; + } + new_state <<= 1; + } + } + for ( i=0; i<8; ++i ) { + if ( (old_state&0x01) != (new_state&0x01) ) { + button = (Uint8)(i+1); + /* Map DI button numbers to SDL */ + switch ( button ) { + case 2: button = SDL_BUTTON_RIGHT; break; + case 3: button = SDL_BUTTON_MIDDLE; break; + case 4: button = SDL_BUTTON_X1; break; + case 5: button = SDL_BUTTON_X2; break; + default: break; + } + if ( new_state & 0x01 ) { + /* Grab mouse so we get mouse-up */ + if ( ++mouse_pressed > 0 ) { + SetCapture(SDL_Window); + } + state = SDL_PRESSED; + } else { + /* Release mouse after all mouse-ups */ + if ( --mouse_pressed <= 0 ) { + ReleaseCapture(); + mouse_pressed = 0; + } + state = SDL_RELEASED; + } + if ( mouse_buttons_swapped ) { + if ( button == 1 ) button = 3; + else + if ( button == 3 ) button = 1; + } + posted = SDL_PrivateMouseButton(state, button, + 0, 0); + } + old_state >>= 1; + new_state >>= 1; + } + mouse_lost = 0; + return; + } + + /* Translate mouse messages */ + xrel = 0; + yrel = 0; + for ( i=0; i<(int)numevents; ++i ) { + switch (ptrbuf[i].dwOfs) { + case DIMOFS_X: + if ( timestamp != ptrbuf[i].dwTimeStamp ) { + if ( xrel || yrel ) { + post_mouse_motion(1, xrel, yrel); + xrel = 0; + yrel = 0; + } + timestamp = ptrbuf[i].dwTimeStamp; + } + xrel += (Sint16)ptrbuf[i].dwData; + break; + case DIMOFS_Y: + if ( timestamp != ptrbuf[i].dwTimeStamp ) { + if ( xrel || yrel ) { + post_mouse_motion(1, xrel, yrel); + xrel = 0; + yrel = 0; + } + timestamp = ptrbuf[i].dwTimeStamp; + } + yrel += (Sint16)ptrbuf[i].dwData; + break; + case DIMOFS_Z: + if ( xrel || yrel ) { + post_mouse_motion(1, xrel, yrel); + xrel = 0; + yrel = 0; + } + timestamp = 0; + if((int)ptrbuf[i].dwData > 0) + button = SDL_BUTTON_WHEELUP; + else + button = SDL_BUTTON_WHEELDOWN; + posted = SDL_PrivateMouseButton( + SDL_PRESSED, button, 0, 0); + posted |= SDL_PrivateMouseButton( + SDL_RELEASED, button, 0, 0); + break; + case DIMOFS_BUTTON0: + case DIMOFS_BUTTON1: + case DIMOFS_BUTTON2: + case DIMOFS_BUTTON3: + #if DIRECTINPUT_VERSION >= 0x700 + case DIMOFS_BUTTON4: + case DIMOFS_BUTTON5: + case DIMOFS_BUTTON6: + case DIMOFS_BUTTON7: + #endif + if ( xrel || yrel ) { + post_mouse_motion(1, xrel, yrel); + xrel = 0; + yrel = 0; + } + timestamp = 0; + button = (Uint8)(ptrbuf[i].dwOfs-DIMOFS_BUTTON0)+1; + /* Map DI button numbers to SDL */ + switch ( button ) { + case 2: button = SDL_BUTTON_RIGHT; break; + case 3: button = SDL_BUTTON_MIDDLE; break; + case 4: button = SDL_BUTTON_X1; break; + case 5: button = SDL_BUTTON_X2; break; + default: break; + } + if ( ptrbuf[i].dwData & 0x80 ) { + /* Grab mouse so we get mouse-up */ + if ( ++mouse_pressed > 0 ) { + SetCapture(SDL_Window); + } + state = SDL_PRESSED; + } else { + /* Release mouse after all mouse-ups */ + if ( --mouse_pressed <= 0 ) { + ReleaseCapture(); + mouse_pressed = 0; + } + state = SDL_RELEASED; + } + if ( mouse_buttons_swapped ) { + if ( button == 1 ) button = 3; + else + if ( button == 3 ) button = 1; + } + posted = SDL_PrivateMouseButton(state, button, + 0, 0); + break; + } + } + if ( xrel || yrel ) { + post_mouse_motion(1, xrel, yrel); + } + } +} + +/* The main Win32 event handler */ +LRESULT DX5_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + switch (msg) { +#ifdef WM_ACTIVATEAPP + case WM_ACTIVATEAPP: { + int i, active; + + active = (wParam && (GetForegroundWindow() == hwnd)); + if ( active ) { + for ( i=0; i 0 ) { + DispatchMessage(&msg); + } else { + return(-1); + } + } + if ( posted ) { + return(1); + } + + /* Pump the DirectInput flow */ + if ( SDL_GetAppState() & SDL_APPMOUSEFOCUS ) { + for ( i=0; i= WAIT_OBJECT_0) && (event < (WAIT_OBJECT_0+SDL_DIndev))) { + DWORD numevents; + static DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE]; + + event -= WAIT_OBJECT_0; + numevents = INPUT_QSIZE; + result = IDirectInputDevice2_GetDeviceData( + SDL_DIdev[event], sizeof(DIDEVICEOBJECTDATA), + evtbuf, &numevents, 0); + if ( (result == DIERR_INPUTLOST) || + (result == DIERR_NOTACQUIRED) ) { + if ( SDL_strcmp(inputs[event].name, "mouse") == 0 ) { + mouse_lost = 1; + } + IDirectInputDevice2_Acquire(SDL_DIdev[event]); + result = IDirectInputDevice2_GetDeviceData( + SDL_DIdev[event], sizeof(DIDEVICEOBJECTDATA), + evtbuf, &numevents, 0); + } + /* Handle the events */ + if ( result == DI_OK && processInput ) { + /* Note: This can post multiple events to event queue + */ + (*SDL_DIfun[event])((int)numevents, evtbuf); + return(1); + } + } + if ( event != WAIT_TIMEOUT ) { + /* Maybe there was a windows message? */ + if ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) { + if ( GetMessage(&msg, NULL, 0, 0) > 0 ) { + DispatchMessage(&msg); + } else { + return(-1); + } + return(1); + } + } + return(0); +} + +/* Change cooperative level based on whether or not we are fullscreen */ +void DX5_DInputReset(_THIS, int fullscreen) +{ + DWORD level; + int i; + HRESULT result; + HWND topwnd; + + for ( i=0; i 0 ) { + /* Loop and check again */; + } +} + +void DX5_InitOSKeymap(_THIS) +{ +#ifndef DIK_PAUSE +#define DIK_PAUSE 0xC5 +#endif +#ifndef DIK_OEM_102 +#define DIK_OEM_102 0x56 /* < > | on UK/Germany keyboards */ +#endif + int i; + + /* Map the DIK scancodes to SDL keysyms */ + for ( i=0; iscancode = (unsigned char)scancode; + keysym->sym = DIK_keymap[scancode]; + keysym->mod = KMOD_NONE; + keysym->unicode = 0; + if ( pressed && SDL_TranslateUNICODE ) { + UINT vkey; +#ifndef NO_GETKEYBOARDSTATE + BYTE keystate[256]; + Uint16 wchars[2]; +#endif + + vkey = MapVirtualKey(scancode, 1); +#ifdef NO_GETKEYBOARDSTATE + /* Uh oh, better hope the vkey is close enough.. */ + keysym->unicode = vkey; +#else + GetKeyboardState(keystate); + /* Numlock isn't taken into account in ToUnicode, + * so we handle it as a special case here */ + if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9) + { + keysym->unicode = vkey - VK_NUMPAD0 + '0'; + } + else if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0) + { + keysym->unicode = wchars[0]; + } +#endif /* NO_GETKEYBOARDSTATE */ + } + return(keysym); +} + +int DX5_CreateWindow(_THIS) +{ + char *windowid = SDL_getenv("SDL_WINDOWID"); + int i; + + /* Clear out DirectInput variables in case we fail */ + for ( i=0; ihidden ) { + SDL_free(this->hidden); + } + if ( this->gl_data ) { + SDL_free(this->gl_data); + } + SDL_free(this); + } +} + +static SDL_VideoDevice *DX5_CreateDevice(int devindex) +{ + SDL_VideoDevice *device; + + /* Load DirectX */ + if ( DX5_Load() < 0 ) { + return(NULL); + } + + /* Initialize all variables that we clean on shutdown */ + device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice)); + if ( device ) { + SDL_memset(device, 0, (sizeof *device)); + device->hidden = (struct SDL_PrivateVideoData *) + SDL_malloc((sizeof *device->hidden)); + device->gl_data = (struct SDL_PrivateGLData *) + SDL_malloc((sizeof *device->gl_data)); + } + if ( (device == NULL) || (device->hidden == NULL) || + (device->gl_data == NULL) ) { + SDL_OutOfMemory(); + DX5_DeleteDevice(device); + return(NULL); + } + SDL_memset(device->hidden, 0, (sizeof *device->hidden)); + SDL_memset(device->gl_data, 0, (sizeof *device->gl_data)); + + /* Set the function pointers */ + device->VideoInit = DX5_VideoInit; + device->ListModes = DX5_ListModes; + device->SetVideoMode = DX5_SetVideoMode; + device->UpdateMouse = WIN_UpdateMouse; + device->CreateYUVOverlay = DX5_CreateYUVOverlay; + device->SetColors = DX5_SetColors; + device->UpdateRects = NULL; + device->VideoQuit = DX5_VideoQuit; + device->AllocHWSurface = DX5_AllocHWSurface; + device->CheckHWBlit = DX5_CheckHWBlit; + device->FillHWRect = DX5_FillHWRect; + device->SetHWColorKey = DX5_SetHWColorKey; + device->SetHWAlpha = DX5_SetHWAlpha; + device->LockHWSurface = DX5_LockHWSurface; + device->UnlockHWSurface = DX5_UnlockHWSurface; + device->FlipHWSurface = DX5_FlipHWSurface; + device->FreeHWSurface = DX5_FreeHWSurface; + device->SetGammaRamp = DX5_SetGammaRamp; + device->GetGammaRamp = DX5_GetGammaRamp; +#if SDL_VIDEO_OPENGL + device->GL_LoadLibrary = WIN_GL_LoadLibrary; + device->GL_GetProcAddress = WIN_GL_GetProcAddress; + device->GL_GetAttribute = WIN_GL_GetAttribute; + device->GL_MakeCurrent = WIN_GL_MakeCurrent; + device->GL_SwapBuffers = WIN_GL_SwapBuffers; +#endif + device->SetCaption = WIN_SetWMCaption; + device->SetIcon = WIN_SetWMIcon; + device->IconifyWindow = WIN_IconifyWindow; + device->GrabInput = WIN_GrabInput; + device->GetWMInfo = WIN_GetWMInfo; + device->FreeWMCursor = WIN_FreeWMCursor; + device->CreateWMCursor = WIN_CreateWMCursor; + device->ShowWMCursor = WIN_ShowWMCursor; + device->WarpWMCursor = WIN_WarpWMCursor; + device->CheckMouseMode = WIN_CheckMouseMode; + device->InitOSKeymap = DX5_InitOSKeymap; + device->PumpEvents = DX5_PumpEvents; + + /* Set up the windows message handling functions */ + WIN_Activate = DX5_Activate; + WIN_RealizePalette = DX5_RealizePalette; + WIN_PaletteChanged = DX5_PaletteChanged; + WIN_WinPAINT = DX5_WinPAINT; + HandleMessage = DX5_HandleMessage; + + device->free = DX5_DeleteDevice; + + /* We're finally ready */ + return device; +} + +VideoBootStrap DIRECTX_bootstrap = { + "directx", "Win95/98/2000 DirectX", + DX5_Available, DX5_CreateDevice +}; + +static int cmpmodes(const void *va, const void *vb) +{ + SDL_Rect *a = *(SDL_Rect **)va; + SDL_Rect *b = *(SDL_Rect **)vb; + if ( a->w == b->w ) + return b->h - a->h; + else + return b->w - a->w; +} + +static HRESULT WINAPI EnumModes2(DDSURFACEDESC *desc, VOID *udata) +{ + SDL_VideoDevice *this = (SDL_VideoDevice *)udata; + struct DX5EnumRect *enumrect; +#if defined(NONAMELESSUNION) + int bpp = desc->ddpfPixelFormat.u1.dwRGBBitCount; + int refreshRate = desc->u2.dwRefreshRate; +#else + int bpp = desc->ddpfPixelFormat.dwRGBBitCount; + int refreshRate = desc->dwRefreshRate; +#endif + int maxRefreshRate; + + if ( desc->dwWidth <= SDL_desktop_mode.dmPelsWidth && + desc->dwHeight <= SDL_desktop_mode.dmPelsHeight ) { + maxRefreshRate = SDL_desktop_mode.dmDisplayFrequency; + } else { + maxRefreshRate = 85; /* safe value? */ + } + + switch (bpp) { + case 8: + case 16: + case 24: + case 32: + bpp /= 8; --bpp; + if ( enumlists[bpp] && + enumlists[bpp]->r.w == (Uint16)desc->dwWidth && + enumlists[bpp]->r.h == (Uint16)desc->dwHeight ) { + if ( refreshRate > enumlists[bpp]->refreshRate && + refreshRate <= maxRefreshRate ) { + enumlists[bpp]->refreshRate = refreshRate; +#ifdef DDRAW_DEBUG + fprintf(stderr, "New refresh rate for %d bpp: %dx%d at %d Hz\n", (bpp+1)*8, (int)desc->dwWidth, (int)desc->dwHeight, refreshRate); +#endif + } + break; + } + ++SDL_nummodes[bpp]; + enumrect = (struct DX5EnumRect*)SDL_malloc(sizeof(struct DX5EnumRect)); + if ( !enumrect ) { + SDL_OutOfMemory(); + return(DDENUMRET_CANCEL); + } + enumrect->refreshRate = refreshRate; + enumrect->r.x = 0; + enumrect->r.y = 0; + enumrect->r.w = (Uint16)desc->dwWidth; + enumrect->r.h = (Uint16)desc->dwHeight; + enumrect->next = enumlists[bpp]; + enumlists[bpp] = enumrect; +#ifdef DDRAW_DEBUG + fprintf(stderr, "New mode for %d bpp: %dx%d at %d Hz\n", (bpp+1)*8, (int)desc->dwWidth, (int)desc->dwHeight, refreshRate); +#endif + break; + } + + return(DDENUMRET_OK); +} + +void SetDDerror(const char *function, int code) +{ + static char *error; + static char errbuf[1024]; + + errbuf[0] = 0; + switch (code) { + case DDERR_GENERIC: + error = "Undefined error!"; + break; + case DDERR_EXCEPTION: + error = "Exception encountered"; + break; + case DDERR_INVALIDOBJECT: + error = "Invalid object"; + break; + case DDERR_INVALIDPARAMS: + error = "Invalid parameters"; + break; + case DDERR_NOTFOUND: + error = "Object not found"; + break; + case DDERR_INVALIDRECT: + error = "Invalid rectangle"; + break; + case DDERR_INVALIDCAPS: + error = "Invalid caps member"; + break; + case DDERR_INVALIDPIXELFORMAT: + error = "Invalid pixel format"; + break; + case DDERR_OUTOFMEMORY: + error = "Out of memory"; + break; + case DDERR_OUTOFVIDEOMEMORY: + error = "Out of video memory"; + break; + case DDERR_SURFACEBUSY: + error = "Surface busy"; + break; + case DDERR_SURFACELOST: + error = "Surface was lost"; + break; + case DDERR_WASSTILLDRAWING: + error = "DirectDraw is still drawing"; + break; + case DDERR_INVALIDSURFACETYPE: + error = "Invalid surface type"; + break; + case DDERR_NOEXCLUSIVEMODE: + error = "Not in exclusive access mode"; + break; + case DDERR_NOPALETTEATTACHED: + error = "No palette attached"; + break; + case DDERR_NOPALETTEHW: + error = "No palette hardware"; + break; + case DDERR_NOT8BITCOLOR: + error = "Not 8-bit color"; + break; + case DDERR_EXCLUSIVEMODEALREADYSET: + error = "Exclusive mode was already set"; + break; + case DDERR_HWNDALREADYSET: + error = "Window handle already set"; + break; + case DDERR_HWNDSUBCLASSED: + error = "Window handle is subclassed"; + break; + case DDERR_NOBLTHW: + error = "No blit hardware"; + break; + case DDERR_IMPLICITLYCREATED: + error = "Surface was implicitly created"; + break; + case DDERR_INCOMPATIBLEPRIMARY: + error = "Incompatible primary surface"; + break; + case DDERR_NOCOOPERATIVELEVELSET: + error = "No cooperative level set"; + break; + case DDERR_NODIRECTDRAWHW: + error = "No DirectDraw hardware"; + break; + case DDERR_NOEMULATION: + error = "No emulation available"; + break; + case DDERR_NOFLIPHW: + error = "No flip hardware"; + break; + case DDERR_NOTFLIPPABLE: + error = "Surface not flippable"; + break; + case DDERR_PRIMARYSURFACEALREADYEXISTS: + error = "Primary surface already exists"; + break; + case DDERR_UNSUPPORTEDMODE: + error = "Unsupported mode"; + break; + case DDERR_WRONGMODE: + error = "Surface created in different mode"; + break; + case DDERR_UNSUPPORTED: + error = "Operation not supported"; + break; + case E_NOINTERFACE: + error = "Interface not present"; + break; + default: + SDL_snprintf(errbuf, SDL_arraysize(errbuf), + "%s: Unknown DirectDraw error: 0x%x", + function, code); + break; + } + if ( ! errbuf[0] ) { + SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error); + } + SDL_SetError("%s", errbuf); + return; +} + + +static int DX5_UpdateVideoInfo(_THIS) +{ + /* This needs to be DDCAPS_DX5 for the DirectDraw2 interface */ +#if DIRECTDRAW_VERSION <= 0x300 +#error Your version of DirectX must be greater than or equal to 5.0 +#endif +#ifndef IDirectDrawGammaControl_SetGammaRamp + /*if gamma is undefined then we really have directx <= 0x500*/ + DDCAPS DDCaps; +#else + DDCAPS_DX5 DDCaps; +#endif + HRESULT result; + + /* Fill in our hardware acceleration capabilities */ + SDL_memset(&DDCaps, 0, sizeof(DDCaps)); + DDCaps.dwSize = sizeof(DDCaps); + result = IDirectDraw2_GetCaps(ddraw2, (DDCAPS *)&DDCaps, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::GetCaps", result); + return(-1); + } + this->info.hw_available = 1; + if ( (DDCaps.dwCaps & DDCAPS_BLT) == DDCAPS_BLT ) { + this->info.blit_hw = 1; + } + if ( ((DDCaps.dwCaps & DDCAPS_COLORKEY) == DDCAPS_COLORKEY) && + ((DDCaps.dwCKeyCaps & DDCKEYCAPS_SRCBLT) == DDCKEYCAPS_SRCBLT) ) { + this->info.blit_hw_CC = 1; + } + if ( (DDCaps.dwCaps & DDCAPS_ALPHA) == DDCAPS_ALPHA ) { + /* This is only for alpha channel, and DirectX 6 + doesn't support 2D alpha blits yet, so set it 0 + */ + this->info.blit_hw_A = 0; + } + if ( (DDCaps.dwCaps & DDCAPS_CANBLTSYSMEM) == DDCAPS_CANBLTSYSMEM ) { + this->info.blit_sw = 1; + /* This isn't necessarily true, but the HEL will cover us */ + this->info.blit_sw_CC = this->info.blit_hw_CC; + this->info.blit_sw_A = this->info.blit_hw_A; + } + if ( (DDCaps.dwCaps & DDCAPS_BLTCOLORFILL) == DDCAPS_BLTCOLORFILL ) { + this->info.blit_fill = 1; + } + + /* Find out how much video memory is available */ + { DDSCAPS ddsCaps; + DWORD total_mem; + ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY; + result = IDirectDraw2_GetAvailableVidMem(ddraw2, + &ddsCaps, &total_mem, NULL); + if ( result != DD_OK ) { + total_mem = DDCaps.dwVidMemTotal; + } + this->info.video_mem = total_mem/1024; + } + return(0); +} + +int DX5_VideoInit(_THIS, SDL_PixelFormat *vformat) +{ + HRESULT result; + LPDIRECTDRAW ddraw; + int i, j; + HDC hdc; + + /* Intialize everything */ + ddraw2 = NULL; + SDL_primary = NULL; + SDL_clipper = NULL; + SDL_palette = NULL; + for ( i=0; iBitsPerPixel = GetDeviceCaps(hdc,PLANES) * + GetDeviceCaps(hdc,BITSPIXEL); + ReleaseDC(SDL_Window, hdc); + +#ifndef NO_CHANGEDISPLAYSETTINGS + /* Query for the desktop resolution */ + EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &SDL_desktop_mode); + this->info.current_w = SDL_desktop_mode.dmPelsWidth; + this->info.current_h = SDL_desktop_mode.dmPelsHeight; +#endif + + /* Enumerate the available fullscreen modes */ + for ( i=0; inext ) { + SDL_modelist[i][j] = &rect->r; + } + SDL_modelist[i][j] = NULL; + + if ( SDL_nummodes[i] > 0 ) { + SDL_qsort(SDL_modelist[i], SDL_nummodes[i], sizeof *SDL_modelist[i], cmpmodes); + } + } + + /* Fill in some window manager capabilities */ + this->info.wm_available = 1; + + /* Fill in the video hardware capabilities */ + DX5_UpdateVideoInfo(this); + + return(0); +} + +SDL_Rect **DX5_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) +{ + int bpp; + + bpp = format->BitsPerPixel; + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + /* FIXME: No support for 1 bpp or 4 bpp formats */ + switch (bpp) { /* Does windows support other BPP? */ + case 8: + case 16: + case 24: + case 32: + bpp = (bpp/8)-1; + if ( SDL_nummodes[bpp] > 0 ) + return(SDL_modelist[bpp]); + /* Fall through */ + default: + return((SDL_Rect **)0); + } + } else { + if ( this->screen->format->BitsPerPixel == bpp ) { + return((SDL_Rect **)-1); + } else { + return((SDL_Rect **)0); + } + } +} + +/* Various screen update functions available */ +static void DX5_WindowUpdate(_THIS, int numrects, SDL_Rect *rects); +static void DX5_DirectUpdate(_THIS, int numrects, SDL_Rect *rects); + +SDL_Surface *DX5_SetVideoMode(_THIS, SDL_Surface *current, + int width, int height, int bpp, Uint32 flags) +{ + SDL_Surface *video; + int prev_w = -1; + int prev_h = -1; + HRESULT result; + DWORD sharemode; + DWORD style; + const DWORD directstyle = + (WS_POPUP); + const DWORD windowstyle = + (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX); + const DWORD resizestyle = + (WS_THICKFRAME|WS_MAXIMIZEBOX); + DDSURFACEDESC ddsd; + LPDIRECTDRAWSURFACE dd_surface1; + LPDIRECTDRAWSURFACE3 dd_surface3; + + SDL_resizing = 1; +#ifdef DDRAW_DEBUG + fprintf(stderr, "Setting %dx%dx%d video mode\n", width, height, bpp); +#endif + /* Clean up any previous DirectDraw surfaces */ + if ( current->hwdata ) { + this->FreeHWSurface(this, current); + current->hwdata = NULL; + } + if ( SDL_primary != NULL ) { + IDirectDrawSurface3_Release(SDL_primary); + SDL_primary = NULL; + } + +#ifndef NO_CHANGEDISPLAYSETTINGS + /* Unset any previous OpenGL fullscreen mode */ + if ( (current->flags & (SDL_OPENGL|SDL_FULLSCREEN)) == + (SDL_OPENGL|SDL_FULLSCREEN) ) { + ChangeDisplaySettings(NULL, 0); + } +#endif + + /* Clean up any GL context that may be hanging around */ + if ( current->flags & SDL_OPENGL ) { + WIN_GL_ShutDown(this); + } + + /* If we are setting a GL mode, use GDI, not DirectX (yuck) */ + if ( flags & SDL_OPENGL ) { + Uint32 Rmask, Gmask, Bmask; + + /* Recalculate the bitmasks if necessary */ + if ( bpp == current->format->BitsPerPixel ) { + video = current; + } else { + switch (bpp) { + case 15: + case 16: + if ( 0 /*DIB_SussScreenDepth() == 15*/ ) { + /* 5-5-5 */ + Rmask = 0x00007c00; + Gmask = 0x000003e0; + Bmask = 0x0000001f; + } else { + /* 5-6-5 */ + Rmask = 0x0000f800; + Gmask = 0x000007e0; + Bmask = 0x0000001f; + } + break; + case 24: + case 32: + /* GDI defined as 8-8-8 */ + Rmask = 0x00ff0000; + Gmask = 0x0000ff00; + Bmask = 0x000000ff; + break; + default: + Rmask = 0x00000000; + Gmask = 0x00000000; + Bmask = 0x00000000; + break; + } + video = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, bpp, + Rmask, Gmask, Bmask, 0); + if ( video == NULL ) { + SDL_OutOfMemory(); + return(NULL); + } + } + + /* Fill in part of the video surface */ + prev_w = video->w; + prev_h = video->h; + video->flags = 0; /* Clear flags */ + video->w = width; + video->h = height; + video->pitch = SDL_CalculatePitch(video); + +#ifndef NO_CHANGEDISPLAYSETTINGS + /* Set fullscreen mode if appropriate. + Ugh, since our list of valid video modes comes from + the DirectX driver, we may not actually be able to + change to the desired resolution here. + FIXME: Should we do a closest match? + */ + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + DEVMODE settings; + BOOL changed; + + SDL_memset(&settings, 0, sizeof(DEVMODE)); + settings.dmSize = sizeof(DEVMODE); + settings.dmBitsPerPel = video->format->BitsPerPixel; + settings.dmPelsWidth = width; + settings.dmPelsHeight = height; + settings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL; + if ( width <= (int)SDL_desktop_mode.dmPelsWidth && + height <= (int)SDL_desktop_mode.dmPelsHeight ) { + settings.dmDisplayFrequency = SDL_desktop_mode.dmDisplayFrequency; + settings.dmFields |= DM_DISPLAYFREQUENCY; + } + changed = (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL); + if ( ! changed && (settings.dmFields & DM_DISPLAYFREQUENCY) ) { + settings.dmFields &= ~DM_DISPLAYFREQUENCY; + changed = (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL); + } + if ( changed ) { + video->flags |= SDL_FULLSCREEN; + SDL_fullscreen_mode = settings; + } + } +#endif /* !NO_CHANGEDISPLAYSETTINGS */ + + style = GetWindowLong(SDL_Window, GWL_STYLE); + style &= ~(resizestyle|WS_MAXIMIZE); + if ( video->flags & SDL_FULLSCREEN ) { + style &= ~windowstyle; + style |= directstyle; + } else { + if ( flags & SDL_NOFRAME ) { + style &= ~windowstyle; + style |= directstyle; + video->flags |= SDL_NOFRAME; + } else { + style &= ~directstyle; + style |= windowstyle; + if ( flags & SDL_RESIZABLE ) { + style |= resizestyle; + video->flags |= SDL_RESIZABLE; + } + } +#if WS_MAXIMIZE + if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE; +#endif + } + + /* DJM: Don't piss of anyone who has setup his own window */ + if ( !SDL_windowid ) + SetWindowLong(SDL_Window, GWL_STYLE, style); + + /* Resize the window (copied from SDL WinDIB driver) */ + if ( !SDL_windowid && !IsZoomed(SDL_Window) ) { + RECT bounds; + int x, y; + HWND top; + UINT swp_flags; + const char *window = NULL; + const char *center = NULL; + + if ( video->w != prev_w || video->h != prev_h ) { + window = SDL_getenv("SDL_VIDEO_WINDOW_POS"); + center = SDL_getenv("SDL_VIDEO_CENTERED"); + if ( window ) { + if ( SDL_sscanf(window, "%d,%d", &x, &y) == 2 ) { + SDL_windowX = x; + SDL_windowY = y; + } + if ( SDL_strcmp(window, "center") == 0 ) { + center = window; + } + } + } + swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW); + + bounds.left = SDL_windowX; + bounds.top = SDL_windowY; + bounds.right = SDL_windowX+video->w; + bounds.bottom = SDL_windowY+video->h; + AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), (GetMenu(SDL_Window) != NULL), 0); + width = bounds.right-bounds.left; + height = bounds.bottom-bounds.top; + if ( (flags & SDL_FULLSCREEN) ) { + x = (GetSystemMetrics(SM_CXSCREEN)-width)/2; + y = (GetSystemMetrics(SM_CYSCREEN)-height)/2; + } else if ( center ) { + x = (GetSystemMetrics(SM_CXSCREEN)-width)/2; + y = (GetSystemMetrics(SM_CYSCREEN)-height)/2; + } else if ( SDL_windowX || SDL_windowY || window ) { + x = bounds.left; + y = bounds.top; + } else { + x = y = -1; + swp_flags |= SWP_NOMOVE; + } + if ( flags & SDL_FULLSCREEN ) { + top = HWND_TOPMOST; + } else { + top = HWND_NOTOPMOST; + } + SetWindowPos(SDL_Window, top, x, y, width, height, swp_flags); + if ( !(flags & SDL_FULLSCREEN) ) { + SDL_windowX = SDL_bounds.left; + SDL_windowY = SDL_bounds.top; + } + SetForegroundWindow(SDL_Window); + } + SDL_resizing = 0; + + /* Set up for OpenGL */ + if ( WIN_GL_SetupWindow(this) < 0 ) { + return(NULL); + } + video->flags |= SDL_OPENGL; + return(video); + } + + /* Set the appropriate window style */ + style = GetWindowLong(SDL_Window, GWL_STYLE); + style &= ~(resizestyle|WS_MAXIMIZE); + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + style &= ~windowstyle; + style |= directstyle; + } else { + if ( flags & SDL_NOFRAME ) { + style &= ~windowstyle; + style |= directstyle; + } else { + style &= ~directstyle; + style |= windowstyle; + if ( flags & SDL_RESIZABLE ) { + style |= resizestyle; + } + } +#if WS_MAXIMIZE + if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE; +#endif + } + /* DJM: Don't piss of anyone who has setup his own window */ + if ( !SDL_windowid ) + SetWindowLong(SDL_Window, GWL_STYLE, style); + + /* Set DirectDraw sharing mode.. exclusive when fullscreen */ + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + sharemode = DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT; + } else { + sharemode = DDSCL_NORMAL; + } + result = IDirectDraw2_SetCooperativeLevel(ddraw2,SDL_Window,sharemode); + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::SetCooperativeLevel", result); + return(NULL); + } + + /* Set the display mode, if we are in fullscreen mode */ + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + RECT bounds; + struct DX5EnumRect *rect; + int maxRefreshRate; + + /* Cover up desktop during mode change */ + bounds.left = 0; + bounds.top = 0; + bounds.right = GetSystemMetrics(SM_CXSCREEN); + bounds.bottom = GetSystemMetrics(SM_CYSCREEN); + AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), (GetMenu(SDL_Window) != NULL), 0); + SetWindowPos(SDL_Window, HWND_TOPMOST, + bounds.left, bounds.top, + bounds.right - bounds.left, + bounds.bottom - bounds.top, SWP_NOCOPYBITS); + ShowWindow(SDL_Window, SW_SHOW); + while ( GetForegroundWindow() != SDL_Window ) { + SetForegroundWindow(SDL_Window); + SDL_Delay(100); + } + + /* find maximum monitor refresh rate for this resolution */ + /* Dmitry Yakimov ftech@tula.net */ + maxRefreshRate = 0; /* system default */ + for ( rect = enumlists[bpp / 8 - 1]; rect; rect = rect->next ) { + if ( (width == rect->r.w) && (height == rect->r.h) ) { + maxRefreshRate = rect->refreshRate; + break; + } + } +#ifdef DDRAW_DEBUG + fprintf(stderr, "refresh rate = %d Hz\n", maxRefreshRate); +#endif + + result = IDirectDraw2_SetDisplayMode(ddraw2, width, height, bpp, maxRefreshRate, 0); + if ( result != DD_OK ) { + result = IDirectDraw2_SetDisplayMode(ddraw2, width, height, bpp, 0, 0); + if ( result != DD_OK ) { + /* We couldn't set fullscreen mode, try window */ + return(DX5_SetVideoMode(this, current, width, height, bpp, flags & ~SDL_FULLSCREEN)); + } + } + DX5_DInputReset(this, 1); + } else { + DX5_DInputReset(this, 0); + } + DX5_UpdateVideoInfo(this); + + /* Create a primary DirectDraw surface */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS; + ddsd.ddsCaps.dwCaps = (DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY); + if ( (flags & SDL_FULLSCREEN) != SDL_FULLSCREEN ) { + /* There's no windowed double-buffering */ + flags &= ~SDL_DOUBLEBUF; + } + if ( (flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { + ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT; + ddsd.ddsCaps.dwCaps |= (DDSCAPS_COMPLEX|DDSCAPS_FLIP); + ddsd.dwBackBufferCount = 1; + } + result = IDirectDraw2_CreateSurface(ddraw2, &ddsd, &dd_surface1, NULL); + if ( (result != DD_OK) && ((flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) ) { + ddsd.dwFlags &= ~DDSD_BACKBUFFERCOUNT; + ddsd.ddsCaps.dwCaps &= ~(DDSCAPS_COMPLEX|DDSCAPS_FLIP); + ddsd.dwBackBufferCount = 0; + result = IDirectDraw2_CreateSurface(ddraw2, + &ddsd, &dd_surface1, NULL); + } + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::CreateSurface(PRIMARY)", result); + return(NULL); + } + result = IDirectDrawSurface_QueryInterface(dd_surface1, + &IID_IDirectDrawSurface3, (LPVOID *)&SDL_primary); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface::QueryInterface", result); + return(NULL); + } + IDirectDrawSurface_Release(dd_surface1); + + /* Get the format of the primary DirectDraw surface */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_PIXELFORMAT|DDSD_CAPS; + result = IDirectDrawSurface3_GetSurfaceDesc(SDL_primary, &ddsd); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface::GetSurfaceDesc", result); + return(NULL); + } + if ( ! (ddsd.ddpfPixelFormat.dwFlags&DDPF_RGB) ) { + SDL_SetError("Primary DDRAW surface is not RGB format"); + return(NULL); + } + + /* Free old palette and create a new one if we're in 8-bit mode */ + if ( SDL_palette != NULL ) { + IDirectDrawPalette_Release(SDL_palette); + SDL_palette = NULL; + } +#if defined(NONAMELESSUNION) + if ( ddsd.ddpfPixelFormat.u1.dwRGBBitCount == 8 ) { +#else + if ( ddsd.ddpfPixelFormat.dwRGBBitCount == 8 ) { +#endif + int i; + + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + /* We have access to the entire palette */ + for ( i=0; i<256; ++i ) { + SDL_colors[i].peFlags = + (PC_NOCOLLAPSE|PC_RESERVED); + SDL_colors[i].peRed = 0; + SDL_colors[i].peGreen = 0; + SDL_colors[i].peBlue = 0; + } + } else { + /* First 10 colors are reserved by Windows */ + for ( i=0; i<10; ++i ) { + SDL_colors[i].peFlags = PC_EXPLICIT; + SDL_colors[i].peRed = i; + SDL_colors[i].peGreen = 0; + SDL_colors[i].peBlue = 0; + } + for ( i=10; i<(10+236); ++i ) { + SDL_colors[i].peFlags = PC_NOCOLLAPSE; + SDL_colors[i].peRed = 0; + SDL_colors[i].peGreen = 0; + SDL_colors[i].peBlue = 0; + } + /* Last 10 colors are reserved by Windows */ + for ( i=246; i<256; ++i ) { + SDL_colors[i].peFlags = PC_EXPLICIT; + SDL_colors[i].peRed = i; + SDL_colors[i].peGreen = 0; + SDL_colors[i].peBlue = 0; + } + } + result = IDirectDraw2_CreatePalette(ddraw2, + (DDPCAPS_8BIT|DDPCAPS_ALLOW256), + SDL_colors, &SDL_palette, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::CreatePalette", result); + return(NULL); + } + result = IDirectDrawSurface3_SetPalette(SDL_primary, + SDL_palette); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::SetPalette", result); + return(NULL); + } + } + + /* Create our video surface using the same pixel format */ + video = current; + if ( (width != video->w) || (height != video->h) + || (video->format->BitsPerPixel != +#if defined(NONAMELESSUNION) + ddsd.ddpfPixelFormat.u1.dwRGBBitCount) ) { +#else + ddsd.ddpfPixelFormat.dwRGBBitCount) ) { +#endif + SDL_FreeSurface(video); + video = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, +#if defined(NONAMELESSUNION) + ddsd.ddpfPixelFormat.u1.dwRGBBitCount, + ddsd.ddpfPixelFormat.u2.dwRBitMask, + ddsd.ddpfPixelFormat.u3.dwGBitMask, + ddsd.ddpfPixelFormat.u4.dwBBitMask, +#else + ddsd.ddpfPixelFormat.dwRGBBitCount, + ddsd.ddpfPixelFormat.dwRBitMask, + ddsd.ddpfPixelFormat.dwGBitMask, + ddsd.ddpfPixelFormat.dwBBitMask, +#endif + 0); + if ( video == NULL ) { + SDL_OutOfMemory(); + return(NULL); + } + prev_w = video->w; + prev_h = video->h; + video->w = width; + video->h = height; + video->pitch = 0; + } + video->flags = 0; /* Clear flags */ + + /* If not fullscreen, locking is possible, but it doesn't do what + the caller really expects -- if the locked surface is written to, + the appropriate portion of the entire screen is modified, not + the application window, as we would like. + Note that it is still possible to write directly to display + memory, but the application must respect the clip list of + the surface. There might be some odd timing interactions + involving clip list updates and background refreshing as + Windows moves other windows across our window. + We currently don't support this, even though it might be a + good idea since BeOS has an implementation of BDirectWindow + that does the same thing. This would be most useful for + applications that do complete screen updates every frame. + -- Fixme? + */ + if ( (flags & SDL_FULLSCREEN) != SDL_FULLSCREEN ) { + /* Necessary if we're going from fullscreen to window */ + if ( video->pixels == NULL ) { + video->pitch = (width*video->format->BytesPerPixel); + /* Pitch needs to be QWORD (8-byte) aligned */ + video->pitch = (video->pitch + 7) & ~7; + video->pixels = (void *)SDL_malloc(video->h*video->pitch); + if ( video->pixels == NULL ) { + if ( video != current ) { + SDL_FreeSurface(video); + } + SDL_OutOfMemory(); + return(NULL); + } + } + dd_surface3 = NULL; +#if 0 /* FIXME: enable this when SDL consistently reports lost surfaces */ + if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { + video->flags |= SDL_HWSURFACE; + } else { + video->flags |= SDL_SWSURFACE; + } +#else + video->flags |= SDL_SWSURFACE; +#endif + if ( (flags & SDL_RESIZABLE) && !(flags & SDL_NOFRAME) ) { + video->flags |= SDL_RESIZABLE; + } + if ( flags & SDL_NOFRAME ) { + video->flags |= SDL_NOFRAME; + } + } else { + /* Necessary if we're going from window to fullscreen */ + if ( video->pixels != NULL ) { + SDL_free(video->pixels); + video->pixels = NULL; + } + dd_surface3 = SDL_primary; + video->flags |= SDL_HWSURFACE; + } + + /* See if the primary surface has double-buffering enabled */ + if ( (ddsd.ddsCaps.dwCaps & DDSCAPS_FLIP) == DDSCAPS_FLIP ) { + video->flags |= SDL_DOUBLEBUF; + } + + /* Allocate the SDL surface associated with the primary surface */ + if ( DX5_AllocDDSurface(this, video, dd_surface3, + video->flags&SDL_HWSURFACE) < 0 ) { + if ( video != current ) { + SDL_FreeSurface(video); + } + return(NULL); + } + + /* Use the appropriate blitting function */ + if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + video->flags |= SDL_FULLSCREEN; + if ( video->format->palette != NULL ) { + video->flags |= SDL_HWPALETTE; + } + this->UpdateRects = DX5_DirectUpdate; + } else { + this->UpdateRects = DX5_WindowUpdate; + } + + /* Make our window the proper size, set the clipper, then show it */ + if ( (flags & SDL_FULLSCREEN) != SDL_FULLSCREEN ) { + /* Create and set a clipper on our primary surface */ + if ( SDL_clipper == NULL ) { + result = IDirectDraw2_CreateClipper(ddraw2, + 0, &SDL_clipper, NULL); + if ( result != DD_OK ) { + if ( video != current ) { + SDL_FreeSurface(video); + } + SetDDerror("DirectDraw2::CreateClipper",result); + return(NULL); + } + } + result = IDirectDrawClipper_SetHWnd(SDL_clipper, 0, SDL_Window); + if ( result != DD_OK ) { + if ( video != current ) { + SDL_FreeSurface(video); + } + SetDDerror("DirectDrawClipper::SetHWnd", result); + return(NULL); + } + result = IDirectDrawSurface3_SetClipper(SDL_primary, + SDL_clipper); + if ( result != DD_OK ) { + if ( video != current ) { + SDL_FreeSurface(video); + } + SetDDerror("DirectDrawSurface3::SetClipper", result); + return(NULL); + } + + /* Resize the window (copied from SDL WinDIB driver) */ + if ( !SDL_windowid && !IsZoomed(SDL_Window) ) { + RECT bounds; + int x, y; + UINT swp_flags; + const char *window = NULL; + const char *center = NULL; + + if ( video->w != prev_w || video->h != prev_h ) { + window = SDL_getenv("SDL_VIDEO_WINDOW_POS"); + center = SDL_getenv("SDL_VIDEO_CENTERED"); + if ( window ) { + if ( SDL_sscanf(window, "%d,%d", &x, &y) == 2 ) { + SDL_windowX = x; + SDL_windowY = y; + } + if ( SDL_strcmp(window, "center") == 0 ) { + center = window; + } + } + } + swp_flags = SWP_NOCOPYBITS; + + bounds.left = SDL_windowX; + bounds.top = SDL_windowY; + bounds.right = SDL_windowX+video->w; + bounds.bottom = SDL_windowY+video->h; + AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), (GetMenu(SDL_Window) != NULL), 0); + width = bounds.right-bounds.left; + height = bounds.bottom-bounds.top; + if ( center ) { + x = (GetSystemMetrics(SM_CXSCREEN)-width)/2; + y = (GetSystemMetrics(SM_CYSCREEN)-height)/2; + } else if ( SDL_windowX || SDL_windowY || window ) { + x = bounds.left; + y = bounds.top; + } else { + x = y = -1; + swp_flags |= SWP_NOMOVE; + } + SetWindowPos(SDL_Window, HWND_NOTOPMOST, x, y, width, height, swp_flags); + SDL_windowX = SDL_bounds.left; + SDL_windowY = SDL_bounds.top; + } + + } + ShowWindow(SDL_Window, SW_SHOW); + SetForegroundWindow(SDL_Window); + SDL_resizing = 0; + + /* JC 14 Mar 2006 + Flush the message loop or this can cause big problems later + Especially if the user decides to use dialog boxes or assert()! + */ + WIN_FlushMessageQueue(); + + /* We're live! */ + return(video); +} + +struct private_hwdata { + LPDIRECTDRAWSURFACE3 dd_surface; + LPDIRECTDRAWSURFACE3 dd_writebuf; +}; + +static int DX5_AllocDDSurface(_THIS, SDL_Surface *surface, + LPDIRECTDRAWSURFACE3 requested, Uint32 flag) +{ + LPDIRECTDRAWSURFACE dd_surface1; + LPDIRECTDRAWSURFACE3 dd_surface3; + DDSURFACEDESC ddsd; + HRESULT result; + + /* Clear the hardware flag, in case we fail */ + surface->flags &= ~flag; + + /* Allocate the hardware acceleration data */ + surface->hwdata = (struct private_hwdata *) + SDL_malloc(sizeof(*surface->hwdata)); + if ( surface->hwdata == NULL ) { + SDL_OutOfMemory(); + return(-1); + } + dd_surface3 = NULL; + + /* Set up the surface description */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = (DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS| + DDSD_PITCH|DDSD_PIXELFORMAT); + ddsd.dwWidth = surface->w; + ddsd.dwHeight= surface->h; +#if defined(NONAMELESSUNION) + ddsd.u1.lPitch = surface->pitch; +#else + ddsd.lPitch = surface->pitch; +#endif + if ( (flag & SDL_HWSURFACE) == SDL_HWSURFACE ) { + ddsd.ddsCaps.dwCaps = + (DDSCAPS_OFFSCREENPLAIN|DDSCAPS_VIDEOMEMORY); + } else { + ddsd.ddsCaps.dwCaps = + (DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY); + } + ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat); + ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB; + if ( surface->format->palette ) { + ddsd.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED8; + } +#if defined(NONAMELESSUNION) + ddsd.ddpfPixelFormat.u1.dwRGBBitCount = surface->format->BitsPerPixel; + ddsd.ddpfPixelFormat.u2.dwRBitMask = surface->format->Rmask; + ddsd.ddpfPixelFormat.u3.dwGBitMask = surface->format->Gmask; + ddsd.ddpfPixelFormat.u4.dwBBitMask = surface->format->Bmask; +#else + ddsd.ddpfPixelFormat.dwRGBBitCount = surface->format->BitsPerPixel; + ddsd.ddpfPixelFormat.dwRBitMask = surface->format->Rmask; + ddsd.ddpfPixelFormat.dwGBitMask = surface->format->Gmask; + ddsd.ddpfPixelFormat.dwBBitMask = surface->format->Bmask; +#endif + + /* Create the DirectDraw video surface */ + if ( requested != NULL ) { + dd_surface3 = requested; + } else { + result = IDirectDraw2_CreateSurface(ddraw2, + &ddsd, &dd_surface1, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::CreateSurface", result); + goto error_end; + } + result = IDirectDrawSurface_QueryInterface(dd_surface1, + &IID_IDirectDrawSurface3, (LPVOID *)&dd_surface3); + IDirectDrawSurface_Release(dd_surface1); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface::QueryInterface", result); + goto error_end; + } + } + + if ( (flag & SDL_HWSURFACE) == SDL_HWSURFACE ) { + /* Check to see whether the surface actually ended up + in video memory, and fail if not. We expect the + surfaces we create here to actually be in hardware! + */ + result = IDirectDrawSurface3_GetCaps(dd_surface3,&ddsd.ddsCaps); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::GetCaps", result); + goto error_end; + } + if ( (ddsd.ddsCaps.dwCaps&DDSCAPS_VIDEOMEMORY) != + DDSCAPS_VIDEOMEMORY ) { + SDL_SetError("No room in video memory"); + goto error_end; + } + } else { + /* Try to hook our surface memory */ + ddsd.dwFlags = DDSD_LPSURFACE; + ddsd.lpSurface = surface->pixels; + result = IDirectDrawSurface3_SetSurfaceDesc(dd_surface3, + &ddsd, 0); + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::SetSurfaceDesc", result); + goto error_end; + } + + } + + /* Make sure the surface format was set properly */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + result = IDirectDrawSurface3_Lock(dd_surface3, NULL, + &ddsd, (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::Lock", result); + goto error_end; + } + IDirectDrawSurface3_Unlock(dd_surface3, NULL); + + if ( (flag & SDL_HWSURFACE) == SDL_SWSURFACE ) { + if ( ddsd.lpSurface != surface->pixels ) { + SDL_SetError("DDraw didn't use SDL surface memory"); + goto error_end; + } + if ( +#if defined(NONAMELESSUNION) + ddsd.u1.lPitch +#else + ddsd.lPitch +#endif + != (LONG)surface->pitch ) { + SDL_SetError("DDraw created surface with wrong pitch"); + goto error_end; + } + } else { +#if defined(NONAMELESSUNION) + surface->pitch = (Uint16)ddsd.u1.lPitch; +#else + surface->pitch = (Uint16)ddsd.lPitch; +#endif + } +#if defined(NONAMELESSUNION) + if ( (ddsd.ddpfPixelFormat.u1.dwRGBBitCount != + surface->format->BitsPerPixel) || + (ddsd.ddpfPixelFormat.u2.dwRBitMask != surface->format->Rmask) || + (ddsd.ddpfPixelFormat.u3.dwGBitMask != surface->format->Gmask) || + (ddsd.ddpfPixelFormat.u4.dwBBitMask != surface->format->Bmask) ){ +#else + if ( (ddsd.ddpfPixelFormat.dwRGBBitCount != + surface->format->BitsPerPixel) || + (ddsd.ddpfPixelFormat.dwRBitMask != surface->format->Rmask) || + (ddsd.ddpfPixelFormat.dwGBitMask != surface->format->Gmask) || + (ddsd.ddpfPixelFormat.dwBBitMask != surface->format->Bmask) ){ +#endif + SDL_SetError("DDraw didn't use SDL surface description"); + goto error_end; + } + if ( (ddsd.dwWidth != (DWORD)surface->w) || + (ddsd.dwHeight != (DWORD)surface->h) ) { + SDL_SetError("DDraw created surface with wrong size"); + goto error_end; + } + + /* Set the surface private data */ + surface->flags |= flag; + surface->hwdata->dd_surface = dd_surface3; + if ( (surface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { + LPDIRECTDRAWSURFACE3 dd_writebuf; + + ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; + result = IDirectDrawSurface3_GetAttachedSurface(dd_surface3, + &ddsd.ddsCaps, &dd_writebuf); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::GetAttachedSurface", + result); + } else { + dd_surface3 = dd_writebuf; + } + } + surface->hwdata->dd_writebuf = dd_surface3; + + /* We're ready to go! */ + return(0); + + /* Okay, so goto's are cheesy, but there are so many possible + errors in this function, and the cleanup is the same in + every single case. Is there a better way, other than deeply + nesting the code? + */ +error_end: + if ( (dd_surface3 != NULL) && (dd_surface3 != requested) ) { + IDirectDrawSurface_Release(dd_surface3); + } + SDL_free(surface->hwdata); + surface->hwdata = NULL; + return(-1); +} + +static int DX5_AllocHWSurface(_THIS, SDL_Surface *surface) +{ + /* DDraw limitation -- you need to set cooperative level first */ + if ( SDL_primary == NULL ) { + SDL_SetError("You must set a non-GL video mode first"); + return(-1); + } + return(DX5_AllocDDSurface(this, surface, NULL, SDL_HWSURFACE)); +} + +#ifdef DDRAW_DEBUG +void PrintSurface(char *title, LPDIRECTDRAWSURFACE3 surface, Uint32 flags) +{ + DDSURFACEDESC ddsd; + + /* Lock and load! */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + if ( IDirectDrawSurface3_Lock(surface, NULL, &ddsd, + (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL) != DD_OK ) { + return; + } + IDirectDrawSurface3_Unlock(surface, NULL); + + fprintf(stderr, "%s:\n", title); + fprintf(stderr, "\tSize: %dx%d in %s at %ld bpp (pitch = %ld)\n", + ddsd.dwWidth, ddsd.dwHeight, + (flags & SDL_HWSURFACE) ? "hardware" : "software", +#if defined(NONAMELESSUNION) + ddsd.ddpfPixelFormat.u1.dwRGBBitCount, ddsd.u1.lPitch); +#else + ddsd.ddpfPixelFormat.dwRGBBitCount, ddsd.lPitch); +#endif + fprintf(stderr, "\tR = 0x%X, G = 0x%X, B = 0x%X\n", +#if defined(NONAMELESSUNION) + ddsd.ddpfPixelFormat.u2.dwRBitMask, + ddsd.ddpfPixelFormat.u3.dwGBitMask, + ddsd.ddpfPixelFormat.u4.dwBBitMask); +#else + ddsd.ddpfPixelFormat.dwRBitMask, + ddsd.ddpfPixelFormat.dwGBitMask, + ddsd.ddpfPixelFormat.dwBBitMask); +#endif +} +#endif /* DDRAW_DEBUG */ + +static int DX5_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, + SDL_Surface *dst, SDL_Rect *dstrect) +{ + LPDIRECTDRAWSURFACE3 src_surface; + LPDIRECTDRAWSURFACE3 dst_surface; + DWORD flags; + RECT rect; + HRESULT result; + + /* Set it up.. the desination must have a DDRAW surface */ + src_surface = src->hwdata->dd_writebuf; + dst_surface = dst->hwdata->dd_writebuf; + rect.top = (LONG)srcrect->y; + rect.bottom = (LONG)srcrect->y+srcrect->h; + rect.left = (LONG)srcrect->x; + rect.right = (LONG)srcrect->x+srcrect->w; + if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) + flags = DDBLTFAST_SRCCOLORKEY; + else + flags = DDBLTFAST_NOCOLORKEY; + /* FIXME: We can remove this flag for _really_ fast blit queuing, + but it will affect the return values of locks and flips. + */ + flags |= DDBLTFAST_WAIT; + + /* Do the blit! */ + result = IDirectDrawSurface3_BltFast(dst_surface, + dstrect->x, dstrect->y, src_surface, &rect, flags); + if ( result != DD_OK ) { + if ( result == DDERR_SURFACELOST ) { + result = IDirectDrawSurface3_Restore(src_surface); + result = IDirectDrawSurface3_Restore(dst_surface); + /* The surfaces need to be reloaded with artwork */ + SDL_SetError("Blit surfaces were lost, reload them"); + return(-2); + } + SetDDerror("IDirectDrawSurface3::BltFast", result); +#ifdef DDRAW_DEBUG + fprintf(stderr, "Original dest rect: %dx%d at %d,%d\n", dstrect->w, dstrect->h, dstrect->x, dstrect->y); + fprintf(stderr, "HW accelerated %sblit to from 0x%p to 0x%p at (%d,%d)\n", + (src->flags & SDL_SRCCOLORKEY) ? "colorkey " : "", src, dst, + dstrect->x, dstrect->y); + PrintSurface("SRC", src_surface, src->flags); + PrintSurface("DST", dst_surface, dst->flags); + fprintf(stderr, "Source rectangle: (%d,%d) - (%d,%d)\n", + rect.left, rect.top, rect.right, rect.bottom); +#endif + /* Unexpected error, fall back to software blit */ + return(src->map->sw_blit(src, srcrect, dst, dstrect)); + } + return(0); +} + +static int DX5_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst) +{ + int accelerated; + + /* We need to have a DDraw surface for HW blits */ + if ( (src->flags & SDL_HWSURFACE) == SDL_SWSURFACE ) { + /* Allocate a DDraw surface for the blit */ + if ( src->hwdata == NULL ) { + DX5_AllocDDSurface(this, src, NULL, SDL_SWSURFACE); + } + } + if ( src->hwdata == NULL ) { + return(0); + } + + /* Set initial acceleration on */ + src->flags |= SDL_HWACCEL; + + /* Set the surface attributes */ + if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { + if ( DX5_SetHWColorKey(this, src, src->format->colorkey) < 0 ) { + src->flags &= ~SDL_HWACCEL; + } + } + if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { + if ( DX5_SetHWAlpha(this, src, src->format->alpha) < 0 ) { + src->flags &= ~SDL_HWACCEL; + } + } + + /* Check to see if final surface blit is accelerated */ + accelerated = !!(src->flags & SDL_HWACCEL); + if ( accelerated ) { +#ifdef DDRAW_DEBUG + fprintf(stderr, "Setting accelerated blit on 0x%p\n", src); +#endif + src->map->hw_blit = DX5_HWAccelBlit; + } + return(accelerated); +} + +static int DX5_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color) +{ + LPDIRECTDRAWSURFACE3 dst_surface; + RECT area; + DDBLTFX bltfx; + HRESULT result; + +#ifdef DDRAW_DEBUG + fprintf(stderr, "HW accelerated fill at (%d,%d)\n", dstrect->x, dstrect->y); +#endif + dst_surface = dst->hwdata->dd_writebuf; + area.top = (LONG)dstrect->y; + area.bottom = (LONG)dstrect->y+dstrect->h; + area.left = (LONG)dstrect->x; + area.right = (LONG)dstrect->x+dstrect->w; + bltfx.dwSize = sizeof(bltfx); +#if defined(NONAMELESSUNION) + bltfx.u5.dwFillColor = color; +#else + bltfx.dwFillColor = color; +#endif + result = IDirectDrawSurface3_Blt(dst_surface, + &area, NULL, NULL, DDBLT_COLORFILL|DDBLT_WAIT, &bltfx); + if ( result == DDERR_SURFACELOST ) { + IDirectDrawSurface3_Restore(dst_surface); + result = IDirectDrawSurface3_Blt(dst_surface, + &area, NULL, NULL, DDBLT_COLORFILL|DDBLT_WAIT, &bltfx); + } + if ( result != DD_OK ) { + SetDDerror("IDirectDrawSurface3::Blt", result); + return(-1); + } + return(0); +} + +static int DX5_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key) +{ + DDCOLORKEY colorkey; + HRESULT result; + + /* Set the surface colorkey */ + colorkey.dwColorSpaceLowValue = key; + colorkey.dwColorSpaceHighValue = key; + result = IDirectDrawSurface3_SetColorKey( + surface->hwdata->dd_surface, DDCKEY_SRCBLT, &colorkey); + if ( result != DD_OK ) { + SetDDerror("IDirectDrawSurface3::SetColorKey", result); + return(-1); + } + return(0); +} +static int DX5_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha) +{ + return(-1); +} + +static int DX5_LockHWSurface(_THIS, SDL_Surface *surface) +{ + HRESULT result; + LPDIRECTDRAWSURFACE3 dd_surface; + DDSURFACEDESC ddsd; + + /* Lock and load! */ + dd_surface = surface->hwdata->dd_writebuf; + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + result = IDirectDrawSurface3_Lock(dd_surface, NULL, &ddsd, + (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL); + if ( result == DDERR_SURFACELOST ) { + result = IDirectDrawSurface3_Restore( + surface->hwdata->dd_surface); + result = IDirectDrawSurface3_Lock(dd_surface, NULL, &ddsd, + (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL); + } + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::Lock", result); + return(-1); + } + /* Pitch might have changed -- recalculate pitch and offset */ +#if defined(NONAMELESSUNION) + if ( surface->pitch != ddsd.u1.lPitch ) { + surface->pitch = ddsd.u1.lPitch; +#else + if ( surface->pitch != ddsd.lPitch ) { + surface->pitch = (Uint16)ddsd.lPitch; +#endif + surface->offset = + ((ddsd.dwHeight-surface->h)/2)*surface->pitch + + ((ddsd.dwWidth-surface->w)/2)* + surface->format->BytesPerPixel; + } + surface->pixels = ddsd.lpSurface; + return(0); +} + +static void DX5_UnlockHWSurface(_THIS, SDL_Surface *surface) +{ + IDirectDrawSurface3_Unlock(surface->hwdata->dd_writebuf, NULL); + surface->pixels = NULL; +} + +static int DX5_FlipHWSurface(_THIS, SDL_Surface *surface) +{ + HRESULT result; + LPDIRECTDRAWSURFACE3 dd_surface; + + dd_surface = surface->hwdata->dd_surface; + + /* to prevent big slowdown on fast computers, wait here instead of driver ring 0 code */ + /* Dmitry Yakimov (ftech@tula.net) */ + while(IDirectDrawSurface3_GetFlipStatus(dd_surface, DDGBS_ISBLTDONE) == DDERR_WASSTILLDRAWING); + + result = IDirectDrawSurface3_Flip(dd_surface, NULL, DDFLIP_WAIT); + if ( result == DDERR_SURFACELOST ) { + result = IDirectDrawSurface3_Restore( + surface->hwdata->dd_surface); + while(IDirectDrawSurface3_GetFlipStatus(dd_surface, DDGBS_ISBLTDONE) == DDERR_WASSTILLDRAWING); + result = IDirectDrawSurface3_Flip(dd_surface, NULL, DDFLIP_WAIT); + } + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::Flip", result); + return(-1); + } + return(0); +} + +static void DX5_FreeHWSurface(_THIS, SDL_Surface *surface) +{ + if ( surface->hwdata ) { + if ( surface->hwdata->dd_surface != SDL_primary ) { + IDirectDrawSurface3_Release(surface->hwdata->dd_surface); + } + SDL_free(surface->hwdata); + surface->hwdata = NULL; + } +} + +void DX5_WindowUpdate(_THIS, int numrects, SDL_Rect *rects) +{ + HRESULT result; + int i; + RECT src, dst; + + for ( i=0; iscreen->hwdata->dd_surface, &src, + DDBLT_WAIT, NULL); + /* Doh! Check for lost surface and restore it */ + if ( result == DDERR_SURFACELOST ) { + IDirectDrawSurface3_Restore(SDL_primary); + IDirectDrawSurface3_Blt(SDL_primary, &dst, + this->screen->hwdata->dd_surface, &src, + DDBLT_WAIT, NULL); + } + } +} + +void DX5_DirectUpdate(_THIS, int numrects, SDL_Rect *rects) +{ +} + +/* Compress a full palette into the limited number of colors given to us + by windows. + + The "best" way to do this is to sort the colors by diversity and place + the most diverse colors into the limited palette. Unfortunately this + results in widely varying colors being displayed in the interval during + which the windows palette has been set, and the mapping of the shadow + surface to the new palette. This is especially noticeable during fades. + + To deal with this problem, we can copy a predetermined portion of the + full palette, and use that as the limited palette. This allows colors + to fade smoothly as the remapping is very similar on each palette change. + Unfortunately, this breaks applications which partition the palette into + distinct and widely varying areas, expecting all colors to be available. + + I'm making them both available, chosen at compile time. + If you want the chunk-o-palette algorithm, define SIMPLE_COMPRESSION, + otherwise the sort-by-diversity algorithm will be used. +*/ +#define SIMPLE_COMPRESSION +#define CS_CS_DIST(A, B) ({ \ + int r = (A.r - B.r); \ + int g = (A.g - B.g); \ + int b = (A.b - B.b); \ + (r*r + g*g + b*b); \ +}) +static void DX5_CompressPalette(_THIS, SDL_Color *colors, int ncolors, int maxcolors) +{ +#ifdef SIMPLE_COMPRESSION + int i, j; +#else + static SDL_Color zero = { 0, 0, 0, 0 }; + int i, j; + int max, dist; + int prev, next; + int *pool; + int *seen, *order; +#endif + + /* Does this happen? */ + if ( maxcolors > ncolors ) { + maxcolors = ncolors; + } + +#ifdef SIMPLE_COMPRESSION + /* Just copy the first "maxcolors" colors */ + for ( j=10, i=0; i= max ) { + max = dist; + next = i; + } + } + j = 0; + order[j++] = next; + seen[next] = 1; + prev = next; + + /* Keep going through all the colors */ + while ( j < maxcolors ) { + max = 0; + for ( i=0; i= max ) { + max = dist; + next = i; + } + } + order[j++] = next; + seen[next] = 1; + prev = next; + } + + /* Compress the colors to the palette */ + for ( j=10, i=0; iscreen->flags&SDL_FULLSCREEN) == SDL_FULLSCREEN ) { + /* We can set all entries explicitly */ + for ( i=0; i< ncolors; ++i ) { + int j = firstcolor + i; + SDL_colors[j].peRed = colors[i].r; + SDL_colors[j].peGreen = colors[i].g; + SDL_colors[j].peBlue = colors[i].b; + } + /* This sends an WM_PALETTECHANGED message to us */ + colorchange_expected = 1; + IDirectDrawPalette_SetEntries(SDL_palette, 0, + firstcolor, ncolors, &SDL_colors[firstcolor]); + alloct_all = 1; + } else { + /* Grab the 236 most diverse colors in the palette */ + DX5_CompressPalette(this, colors, ncolors, 236); + /* This sends an WM_PALETTECHANGED message to us */ + colorchange_expected = 1; + IDirectDrawPalette_SetEntries(SDL_palette, 0, + 0, 256, SDL_colors); + } + } + return(alloct_all); +} + +/* Gamma code is only available on DirectX 7 and newer */ +static int DX5_SetGammaRamp(_THIS, Uint16 *ramp) +{ +#ifdef IDirectDrawGammaControl_SetGammaRamp + LPDIRECTDRAWGAMMACONTROL gamma; + DDGAMMARAMP gamma_ramp; + HRESULT result; +#endif + + /* In windowed or OpenGL mode, use windib gamma code */ + if ( ! DDRAW_FULLSCREEN() ) { + return DIB_SetGammaRamp(this, ramp); + } + +#ifndef IDirectDrawGammaControl_SetGammaRamp + SDL_SetError("SDL compiled without DirectX gamma ramp support"); + return -1; +#else + /* Check for a video mode! */ + if ( ! SDL_primary ) { + SDL_SetError("A video mode must be set for gamma correction"); + return(-1); + } + + /* Get the gamma control object */ + result = IDirectDrawSurface3_QueryInterface(SDL_primary, + &IID_IDirectDrawGammaControl, (LPVOID *)&gamma); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::QueryInterface(GAMMA)", result); + return(-1); + } + + /* Set up the gamma ramp */ + SDL_memcpy(gamma_ramp.red, &ramp[0*256], 256*sizeof(*ramp)); + SDL_memcpy(gamma_ramp.green, &ramp[1*256], 256*sizeof(*ramp)); + SDL_memcpy(gamma_ramp.blue, &ramp[2*256], 256*sizeof(*ramp)); + result = IDirectDrawGammaControl_SetGammaRamp(gamma, 0, &gamma_ramp); + if ( result != DD_OK ) { + SetDDerror("DirectDrawGammaControl::SetGammaRamp()", result); + } + + /* Release the interface and return */ + IDirectDrawGammaControl_Release(gamma); + return (result == DD_OK) ? 0 : -1; +#endif /* !IDirectDrawGammaControl_SetGammaRamp */ +} + +static int DX5_GetGammaRamp(_THIS, Uint16 *ramp) +{ +#ifdef IDirectDrawGammaControl_SetGammaRamp + LPDIRECTDRAWGAMMACONTROL gamma; + DDGAMMARAMP gamma_ramp; + HRESULT result; +#endif + + /* In windowed or OpenGL mode, use windib gamma code */ + if ( ! DDRAW_FULLSCREEN() ) { + return DIB_GetGammaRamp(this, ramp); + } + +#ifndef IDirectDrawGammaControl_SetGammaRamp + SDL_SetError("SDL compiled without DirectX gamma ramp support"); + return -1; +#else + /* Check for a video mode! */ + if ( ! SDL_primary ) { + SDL_SetError("A video mode must be set for gamma correction"); + return(-1); + } + + /* Get the gamma control object */ + result = IDirectDrawSurface3_QueryInterface(SDL_primary, + &IID_IDirectDrawGammaControl, (LPVOID *)&gamma); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::QueryInterface(GAMMA)", result); + return(-1); + } + + /* Set up the gamma ramp */ + result = IDirectDrawGammaControl_GetGammaRamp(gamma, 0, &gamma_ramp); + if ( result == DD_OK ) { + SDL_memcpy(&ramp[0*256], gamma_ramp.red, 256*sizeof(*ramp)); + SDL_memcpy(&ramp[1*256], gamma_ramp.green, 256*sizeof(*ramp)); + SDL_memcpy(&ramp[2*256], gamma_ramp.blue, 256*sizeof(*ramp)); + } else { + SetDDerror("DirectDrawGammaControl::GetGammaRamp()", result); + } + + /* Release the interface and return */ + IDirectDrawGammaControl_Release(gamma); + return (result == DD_OK) ? 0 : -1; +#endif /* !IDirectDrawGammaControl_SetGammaRamp */ +} + +void DX5_VideoQuit(_THIS) +{ + int i, j; + + /* If we're fullscreen GL, we need to reset the display */ + if ( this->screen != NULL ) { +#ifndef NO_CHANGEDISPLAYSETTINGS + if ( (this->screen->flags & (SDL_OPENGL|SDL_FULLSCREEN)) == + (SDL_OPENGL|SDL_FULLSCREEN) ) { + ChangeDisplaySettings(NULL, 0); + ShowWindow(SDL_Window, SW_HIDE); + } +#endif + if ( this->screen->flags & SDL_OPENGL ) { + WIN_GL_ShutDown(this); + } + } + + /* Free any palettes we used */ + if ( SDL_palette != NULL ) { + IDirectDrawPalette_Release(SDL_palette); + SDL_palette = NULL; + } + + /* Allow the primary surface to be freed */ + if ( SDL_primary != NULL ) { + SDL_primary = NULL; + } + + /* Free video mode lists */ + for ( i=0; iw && surface->h ) { + if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { + if ( this->LockHWSurface(this, surface) < 0 ) { + return; + } + } + for ( row=0; rowh; ++row ) { + pixels = (Uint8 *)surface->pixels+row*surface->pitch; + for ( col=0; colw; ++col, ++pixels ) { + *pixels = mapping[*pixels]; + } + } + if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { + this->UnlockHWSurface(this, surface); + } + SDL_UpdateRect(surface, 0, 0, 0, 0); + } +} +void DX5_PaletteChanged(_THIS, HWND window) +{ + SDL_Palette *palette; + SDL_Color *saved = NULL; + HDC hdc; + int i; + PALETTEENTRY *entries; + + /* This is true when the window is closing */ + if ( (SDL_primary == NULL) || (SDL_VideoSurface == NULL) ) + return; + + /* We need to get the colors as they were set */ + palette = this->physpal; + if(!palette) + palette = SDL_VideoSurface->format->palette; + if ( palette == NULL ) { /* Sometimes we don't have a palette */ + return; + } + entries = SDL_stack_alloc(PALETTEENTRY, palette->ncolors); + hdc = GetDC(window); + GetSystemPaletteEntries(hdc, 0, palette->ncolors, entries); + ReleaseDC(window, hdc); + if ( ! colorchange_expected ) { + saved = SDL_stack_alloc(SDL_Color, palette->ncolors); + SDL_memcpy(saved, palette->colors, + palette->ncolors*sizeof(SDL_Color)); + } + for ( i=0; incolors; ++i ) { + palette->colors[i].r = entries[i].peRed; + palette->colors[i].g = entries[i].peGreen; + palette->colors[i].b = entries[i].peBlue; + } + SDL_stack_free(entries); + if ( ! colorchange_expected ) { + Uint8 mapping[256]; + + SDL_memset(mapping, 0, sizeof(mapping)); + for ( i=0; incolors; ++i ) { + mapping[i] = SDL_FindColor(palette, + saved[i].r, saved[i].g, saved[i].b); + } + DX5_Recolor8Bit(this, SDL_VideoSurface, mapping); + SDL_stack_free(saved); + } + colorchange_expected = 0; + + /* Notify all mapped surfaces of the change */ + SDL_FormatChanged(SDL_VideoSurface); +} + +/* Exported for the windows message loop only */ +void DX5_WinPAINT(_THIS, HDC hdc) +{ + SDL_UpdateRect(SDL_PublicSurface, 0, 0, 0, 0); +} diff --git a/apps/plugins/sdl/src/video/windx5/SDL_dx5video.h b/apps/plugins/sdl/src/video/windx5/SDL_dx5video.h new file mode 100644 index 0000000000..3d754a0e2e --- /dev/null +++ b/apps/plugins/sdl/src/video/windx5/SDL_dx5video.h @@ -0,0 +1,61 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2012 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +#ifndef _SDL_dx5video_h +#define _SDL_dx5video_h + +#include "directx.h" + +/* Private display data */ +struct SDL_PrivateVideoData { + LPDIRECTDRAW2 ddraw2; + LPDIRECTDRAWSURFACE3 SDL_primary; + LPDIRECTDRAWCLIPPER SDL_clipper; + LPDIRECTDRAWPALETTE SDL_palette; + PALETTEENTRY SDL_colors[256]; + int colorchange_expected; + +#define NUM_MODELISTS 4 /* 8, 16, 24, and 32 bits-per-pixel */ + int SDL_nummodes[NUM_MODELISTS]; + SDL_Rect **SDL_modelist[NUM_MODELISTS]; + int SDL_modeindex[NUM_MODELISTS]; +}; +/* Old variable names */ +#define ddraw2 (this->hidden->ddraw2) +#define SDL_primary (this->hidden->SDL_primary) +#define SDL_clipper (this->hidden->SDL_clipper) +#define SDL_palette (this->hidden->SDL_palette) +#define SDL_colors (this->hidden->SDL_colors) +#define colorchange_expected (this->hidden->colorchange_expected) +#define SDL_nummodes (this->hidden->SDL_nummodes) +#define SDL_modelist (this->hidden->SDL_modelist) +#define SDL_modeindex (this->hidden->SDL_modeindex) + +/* DirectX function pointers for video and events */ +extern HRESULT (WINAPI *DDrawCreate)( GUID FAR *lpGUID, LPDIRECTDRAW FAR *lplpDD, IUnknown FAR *pUnkOuter ); +extern HRESULT (WINAPI *DInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT *ppDI, LPUNKNOWN punkOuter); + +/* DirectDraw error reporting function */ +extern void SetDDerror(const char *function, int code); + +#endif /* _SDL_dx5video_h */ diff --git a/apps/plugins/sdl/src/video/windx5/SDL_dx5yuv.c b/apps/plugins/sdl/src/video/windx5/SDL_dx5yuv.c new file mode 100644 index 0000000000..cb89fdc9bb --- /dev/null +++ b/apps/plugins/sdl/src/video/windx5/SDL_dx5yuv.c @@ -0,0 +1,296 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2012 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +/* This is the DirectDraw implementation of YUV video overlays */ +#include "directx.h" +#include "SDL_video.h" +#include "SDL_dx5yuv_c.h" +#include "../SDL_yuvfuncs.h" + +//#define USE_DIRECTX_OVERLAY + +/* The functions used to manipulate software video overlays */ +static struct private_yuvhwfuncs dx5_yuvfuncs = { + DX5_LockYUVOverlay, + DX5_UnlockYUVOverlay, + DX5_DisplayYUVOverlay, + DX5_FreeYUVOverlay +}; + +struct private_yuvhwdata { + LPDIRECTDRAWSURFACE3 surface; + + /* These are just so we don't have to allocate them separately */ + Uint16 pitches[3]; + Uint8 *planes[3]; +}; + + +static LPDIRECTDRAWSURFACE3 CreateYUVSurface(_THIS, + int width, int height, Uint32 format) +{ + HRESULT result; + LPDIRECTDRAWSURFACE dd_surface1; + LPDIRECTDRAWSURFACE3 dd_surface3; + DDSURFACEDESC ddsd; + + /* Set up the surface description */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = (DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|DDSD_PIXELFORMAT); + ddsd.dwWidth = width; + ddsd.dwHeight= height; +#ifdef USE_DIRECTX_OVERLAY + ddsd.ddsCaps.dwCaps = (DDSCAPS_OVERLAY|DDSCAPS_VIDEOMEMORY); +#else + ddsd.ddsCaps.dwCaps = (DDSCAPS_OFFSCREENPLAIN|DDSCAPS_VIDEOMEMORY); +#endif + ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat); + ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC; + ddsd.ddpfPixelFormat.dwFourCC = format; + + /* Create the DirectDraw video surface */ + result = IDirectDraw2_CreateSurface(ddraw2, &ddsd, &dd_surface1, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDraw2::CreateSurface", result); + return(NULL); + } + result = IDirectDrawSurface_QueryInterface(dd_surface1, + &IID_IDirectDrawSurface3, (LPVOID *)&dd_surface3); + IDirectDrawSurface_Release(dd_surface1); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface::QueryInterface", result); + return(NULL); + } + + /* Make sure the surface format was set properly */ + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + result = IDirectDrawSurface3_Lock(dd_surface3, NULL, + &ddsd, DDLOCK_NOSYSLOCK, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::Lock", result); + IDirectDrawSurface_Release(dd_surface3); + return(NULL); + } + IDirectDrawSurface3_Unlock(dd_surface3, NULL); + + if ( !(ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC) || + (ddsd.ddpfPixelFormat.dwFourCC != format) ) { + SDL_SetError("DDraw didn't use requested FourCC format"); + IDirectDrawSurface_Release(dd_surface3); + return(NULL); + } + + /* We're ready to go! */ + return(dd_surface3); +} + +#ifdef DEBUG_YUV +static char *PrintFOURCC(Uint32 code) +{ + static char buf[5]; + + buf[3] = code >> 24; + buf[2] = (code >> 16) & 0xFF; + buf[1] = (code >> 8) & 0xFF; + buf[0] = (code & 0xFF); + return(buf); +} +#endif + +SDL_Overlay *DX5_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display) +{ + SDL_Overlay *overlay; + struct private_yuvhwdata *hwdata; + +#ifdef DEBUG_YUV + DWORD numcodes; + DWORD *codes; + + printf("FOURCC format requested: 0x%x\n", PrintFOURCC(format)); + IDirectDraw2_GetFourCCCodes(ddraw2, &numcodes, NULL); + if ( numcodes ) { + DWORD i; + codes = SDL_malloc(numcodes*sizeof(*codes)); + if ( codes ) { + IDirectDraw2_GetFourCCCodes(ddraw2, &numcodes, codes); + for ( i=0; iformat = format; + overlay->w = width; + overlay->h = height; + + /* Set up the YUV surface function structure */ + overlay->hwfuncs = &dx5_yuvfuncs; + + /* Create the pixel data and lookup tables */ + hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata); + overlay->hwdata = hwdata; + if ( hwdata == NULL ) { + SDL_OutOfMemory(); + SDL_FreeYUVOverlay(overlay); + return(NULL); + } + hwdata->surface = CreateYUVSurface(this, width, height, format); + if ( hwdata->surface == NULL ) { + SDL_FreeYUVOverlay(overlay); + return(NULL); + } + overlay->hw_overlay = 1; + + /* Set up the plane pointers */ + overlay->pitches = hwdata->pitches; + overlay->pixels = hwdata->planes; + switch (format) { + case SDL_YV12_OVERLAY: + case SDL_IYUV_OVERLAY: + overlay->planes = 3; + break; + default: + overlay->planes = 1; + break; + } + + /* We're all done.. */ + return(overlay); +} + +int DX5_LockYUVOverlay(_THIS, SDL_Overlay *overlay) +{ + HRESULT result; + LPDIRECTDRAWSURFACE3 surface; + DDSURFACEDESC ddsd; + + surface = overlay->hwdata->surface; + SDL_memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + result = IDirectDrawSurface3_Lock(surface, NULL, + &ddsd, DDLOCK_NOSYSLOCK, NULL); + if ( result == DDERR_SURFACELOST ) { + result = IDirectDrawSurface3_Restore(surface); + result = IDirectDrawSurface3_Lock(surface, NULL, &ddsd, + (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL); + } + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::Lock", result); + return(-1); + } + + /* Find the pitch and offset values for the overlay */ +#if defined(NONAMELESSUNION) + overlay->pitches[0] = (Uint16)ddsd.u1.lPitch; +#else + overlay->pitches[0] = (Uint16)ddsd.lPitch; +#endif + overlay->pixels[0] = (Uint8 *)ddsd.lpSurface; + switch (overlay->format) { + case SDL_YV12_OVERLAY: + case SDL_IYUV_OVERLAY: + /* Add the two extra planes */ + overlay->pitches[1] = overlay->pitches[0] / 2; + overlay->pitches[2] = overlay->pitches[0] / 2; + overlay->pixels[1] = overlay->pixels[0] + + overlay->pitches[0] * overlay->h; + overlay->pixels[2] = overlay->pixels[1] + + overlay->pitches[1] * overlay->h / 2; + break; + default: + /* Only one plane, no worries */ + break; + } + return(0); +} + +void DX5_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay) +{ + LPDIRECTDRAWSURFACE3 surface; + + surface = overlay->hwdata->surface; + IDirectDrawSurface3_Unlock(surface, NULL); +} + +int DX5_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst) +{ + HRESULT result; + LPDIRECTDRAWSURFACE3 surface; + RECT srcrect, dstrect; + + surface = overlay->hwdata->surface; + srcrect.top = src->y; + srcrect.bottom = srcrect.top+src->h; + srcrect.left = src->x; + srcrect.right = srcrect.left+src->w; + dstrect.top = SDL_bounds.top+dst->y; + dstrect.left = SDL_bounds.left+dst->x; + dstrect.bottom = dstrect.top+dst->h; + dstrect.right = dstrect.left+dst->w; +#ifdef USE_DIRECTX_OVERLAY + result = IDirectDrawSurface3_UpdateOverlay(surface, &srcrect, + SDL_primary, &dstrect, DDOVER_SHOW, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::UpdateOverlay", result); + return(-1); + } +#else + result = IDirectDrawSurface3_Blt(SDL_primary, &dstrect, surface, &srcrect, + DDBLT_WAIT, NULL); + if ( result != DD_OK ) { + SetDDerror("DirectDrawSurface3::Blt", result); + return(-1); + } +#endif + return(0); +} + +void DX5_FreeYUVOverlay(_THIS, SDL_Overlay *overlay) +{ + struct private_yuvhwdata *hwdata; + + hwdata = overlay->hwdata; + if ( hwdata ) { + if ( hwdata->surface ) { + IDirectDrawSurface_Release(hwdata->surface); + } + SDL_free(hwdata); + overlay->hwdata = NULL; + } +} + diff --git a/apps/plugins/sdl/src/video/windx5/SDL_dx5yuv_c.h b/apps/plugins/sdl/src/video/windx5/SDL_dx5yuv_c.h new file mode 100644 index 0000000000..dfceaf929a --- /dev/null +++ b/apps/plugins/sdl/src/video/windx5/SDL_dx5yuv_c.h @@ -0,0 +1,38 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2012 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +/* This is the DirectDraw implementation of YUV video overlays */ + +#include "SDL_video.h" +#include "../wincommon/SDL_lowvideo.h" +#include "SDL_dx5video.h" + +extern SDL_Overlay *DX5_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display); + +extern int DX5_LockYUVOverlay(_THIS, SDL_Overlay *overlay); + +extern void DX5_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay); + +extern int DX5_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst); + +extern void DX5_FreeYUVOverlay(_THIS, SDL_Overlay *overlay); diff --git a/apps/plugins/sdl/src/video/windx5/directx.h b/apps/plugins/sdl/src/video/windx5/directx.h new file mode 100644 index 0000000000..c7f536530b --- /dev/null +++ b/apps/plugins/sdl/src/video/windx5/directx.h @@ -0,0 +1,97 @@ + +#ifndef _directx_h +#define _directx_h + +/* Include all of the DirectX 5.0 headers and adds any necessary tweaks */ + +#define WIN32_LEAN_AND_MEAN +#include +#include +#ifndef WIN32 +#define WIN32 +#endif +#undef WINNT + +/* Far pointers don't exist in 32-bit code */ +#ifndef FAR +#define FAR +#endif + +/* Error codes not yet included in Win32 API header files */ +#ifndef MAKE_HRESULT +#define MAKE_HRESULT(sev,fac,code) \ + ((HRESULT)(((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) | ((unsigned long)(code)))) +#endif + +#ifndef S_OK +#define S_OK (HRESULT)0x00000000L +#endif + +#ifndef SUCCEEDED +#define SUCCEEDED(x) ((HRESULT)(x) >= 0) +#endif +#ifndef FAILED +#define FAILED(x) ((HRESULT)(x)<0) +#endif + +#ifndef E_FAIL +#define E_FAIL (HRESULT)0x80000008L +#endif +#ifndef E_NOINTERFACE +#define E_NOINTERFACE (HRESULT)0x80004002L +#endif +#ifndef E_OUTOFMEMORY +#define E_OUTOFMEMORY (HRESULT)0x8007000EL +#endif +#ifndef E_INVALIDARG +#define E_INVALIDARG (HRESULT)0x80070057L +#endif +#ifndef E_NOTIMPL +#define E_NOTIMPL (HRESULT)0x80004001L +#endif +#ifndef REGDB_E_CLASSNOTREG +#define REGDB_E_CLASSNOTREG (HRESULT)0x80040154L +#endif + +/* Severity codes */ +#ifndef SEVERITY_ERROR +#define SEVERITY_ERROR 1 +#endif + +/* Error facility codes */ +#ifndef FACILITY_WIN32 +#define FACILITY_WIN32 7 +#endif + +#ifndef FIELD_OFFSET +#define FIELD_OFFSET(type, field) ((LONG)&(((type *)0)->field)) +#endif + +/* DirectX headers (if it isn't included, I haven't tested it yet) + */ +/* We need these defines to mark what version of DirectX API we use */ +#define DIRECTDRAW_VERSION 0x0700 +#define DIRECTSOUND_VERSION 0x0500 +#define DIRECTINPUT_VERSION 0x0700 + +#include +#include +#include + +#if DIRECTINPUT_VERSION >= 0x0700 && !defined(DIMOFS_BUTTON4) +typedef struct _DIMOUSESTATE2 { + LONG lX; + LONG lY; + LONG lZ; + BYTE rgbButtons[8]; +} DIMOUSESTATE2, *LPDIMOUSESTATE2; + +#define DIMOFS_BUTTON4 (FIELD_OFFSET(DIMOUSESTATE2, rgbButtons) + 4) +#define DIMOFS_BUTTON5 (FIELD_OFFSET(DIMOUSESTATE2, rgbButtons) + 5) +#define DIMOFS_BUTTON6 (FIELD_OFFSET(DIMOUSESTATE2, rgbButtons) + 6) +#define DIMOFS_BUTTON7 (FIELD_OFFSET(DIMOUSESTATE2, rgbButtons) + 7) + +extern const DIDATAFORMAT c_dfDIMouse2; +#endif + +#endif /* _directx_h */ -- cgit v1.2.3