//------------------------------------------------------------ // Name: Initiate // Desc: Init everything //------------------------------------------------------------ int Initiate() { // create dd object and test for an error hr = DirectDrawCreate(NULL,&lpdd,NULL); if (DDFailedCheck(hr, "DirectDrawCreate failed", cpErrorBuf )) return 0; // set cooperation level to windowed mode normal hr = lpdd->SetCooperativeLevel(main_window_handle, DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT ); if (DDFailedCheck(hr, "SetCooperativeLevel", cpErrorBuf )) return 0; // set the display mode. use this for full screen programs hr = lpdd->SetDisplayMode(WINDOW_WIDTH ,WINDOW_HEIGHT,16); if (DDFailedCheck(hr, "SetDisplayMode", cpErrorBuf )) return 0; // Create the primary surface memset(&ddsd,0,sizeof(ddsd));//clear ddsd structure ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; hr = lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL); if (DDFailedCheck(hr, "CreateSurface", cpErrorBuf )) return 0; // return success return(1); }//End Initiate
int DDInitFullscreen(int width, int height, int bpp, HWND hwnd) { HRESULT ret; // create object and test for error if (DirectDrawCreate(NULL, &lpDD, NULL) != DD_OK) { return(0); } // set cooperation level to windowed mode normal if (lpDD->SetCooperativeLevel(hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) != DD_OK) { return(0); } // set the display mode if (lpDD->SetDisplayMode(width, height, bpp) != DD_OK) { return(0); } // set globals screen_height = height; screen_width = width; screen_bpp = bpp; // Create the primary surface memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; // we need to let dd know that we want a complex // flippable surface structure, set flags for that ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; // set the backbuffer count to 1 ddsd.dwBackBufferCount = 1; // create the primary surface ret = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL); // query for the backbuffer i.e the secondary surface ddscaps.dwCaps = DDSCAPS_BACKBUFFER; lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack); // clear out both primary and secondary surfaces DDFillSurface(lpDDSPrimary, 0); DDFillSurface(lpDDSBack, 0); DDGetRGB16(); return 1; }
BOOL InitDDraw() { DDSURFACEDESC ddsd; HRESULT ddrval; DirectDrawCreate(NULL, &lpDD, NULL);//产生一个DDRAW对象。 /*第一个参数是要创建的驱动类型,设为NULL,表示使用当前的显示驱动 第三个参数用于今后与COM对象兼容,目前都设为NULL*/ lpDD->SetCooperativeLevel (hWnd, DDSCL_NORMAL);//设置协作级别 ZeroMemory( &ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; /*主页面的大小和像素格式都是由当前的显示模式决定的, 所以不能指定,否则发生错误*/ ddsd.ddsCaps .dwCaps = DDSCAPS_PRIMARYSURFACE; ddrval = lpDD->CreateSurface (&ddsd, &lpDDSPrimary, NULL); /*第三个参数也是用于与COM对象兼容,目前只能设为NULL*/ if(ddrval != DD_OK) { return FALSE; } ZeroMemory( &ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS| DDSD_HEIGHT| DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwHeight = 600; ddsd.dwWidth = 800; /*页面的大小可以和实际图片大小不一样,但会产生失真*/ RectTemp.left = 0; RectTemp.top = 0; RectTemp.right = ddsd.dwWidth; RectTemp.bottom = ddsd.dwHeight; lpDD->CreateSurface (&ddsd, &lpDDSTemp, NULL); if(ddrval != DD_OK) { return FALSE; } DDReLoadBitmap( lpDDSTemp, "gameover.bmp"); return TRUE; }
BOOL mfc_render_init(HWND hWnd) { DDSURFACEDESC ddsd; DDCAPS ddcaps; HRESULT hRet; /////////////////////////////////////////////////////////////////////////// // Create the main DirectDraw object /////////////////////////////////////////////////////////////////////////// hRet = DirectDrawCreate(NULL, &g_pDD, NULL); if (hRet != DD_OK) return PRINT_ERRMSG(TEXT("DirectDrawCreate FAILED")); // Get exclusive mode hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL); if (hRet != DD_OK) return PRINT_ERRMSG(TEXT("SetCooperativeLevel FAILED")); // Get a primary surface interface pointer (only needed for init.) memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL); if (hRet != DD_OK) return PRINT_ERRMSG(TEXT("CreateSurface FAILED")); // See if we can support overlays. memset(&ddcaps, 0, sizeof(ddcaps)); ddcaps.dwSize = sizeof(ddcaps); hRet = g_pDD->GetCaps(&ddcaps, NULL); if (hRet != DD_OK) return PRINT_ERRMSG(TEXT("GetCaps FAILED")); if (ddcaps.dwOverlayCaps == 0) return PRINT_ERRMSG(TEXT("Overlays are not supported in hardware!")); // SW YUV->RGB conversion table initialization _initConvTab(); return TRUE; }
int DDInitWindowed(int width, int height, int bpp, HWND hwnd) { HRESULT ret; // create object and test for error if (DirectDrawCreate(NULL, &lpDD, NULL) != DD_OK) { return(0); } // set cooperation level to windowed mode normal if (lpDD->SetCooperativeLevel(hwnd, DDSCL_NORMAL) != DD_OK) { return(0); } // set globals screen_height = height; screen_width = width; screen_bpp = bpp; // Create the primary surface memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; // all we need for windowed mode is access to the primary surface ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; // create the primary surface ret = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL); // create an offscreen and system mem back surface lpDDSBack = DDCreateSurface(width, height, NULL); lpDD->CreateClipper(0, &lpDDClipper, NULL); lpDDClipper->SetHWnd(0, hwnd); lpDDSPrimary->SetClipper(lpDDClipper); // clear out both primary and secondary surfaces DDFillSurface(lpDDSPrimary, 0); DDFillSurface(lpDDSBack, 0); DDGetRGB16(); return 1; }
static void init_DirectDraw() { /** * Note: if DirectDraw fails to initialize, we will use GDI to * draw to do the screenBuffer->LCD copying. */ HRESULT hRet; hRet = DirectDrawCreate(NULL, &g_pDD, NULL); if (hRet != DD_OK) { return; } hRet = g_pDD->SetCooperativeLevel(hwndMain, DDSCL_NORMAL); if (hRet != DD_OK) { g_pDD->Release(); g_pDD = NULL; return; } wince_init_fonts(); }
PRBool nsWindowGfx::InitDDraw() { HRESULT hr; hr = DirectDrawCreate(NULL, &glpDD, NULL); NS_ENSURE_SUCCESS(hr, PR_FALSE); hr = glpDD->SetCooperativeLevel(NULL, DDSCL_NORMAL); NS_ENSURE_SUCCESS(hr, PR_FALSE); DDSURFACEDESC ddsd; memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat); ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; hr = glpDD->CreateSurface(&ddsd, &glpDDPrimary, NULL); NS_ENSURE_SUCCESS(hr, PR_FALSE); hr = glpDD->CreateClipper(0, &glpDDClipper, NULL); NS_ENSURE_SUCCESS(hr, PR_FALSE); hr = glpDDPrimary->SetClipper(glpDDClipper); NS_ENSURE_SUCCESS(hr, PR_FALSE); // We do not use the cairo ddraw surface for IMAGE_DDRAW16. Instead, we // use an 24bpp image surface, convert that to 565, then blit using ddraw. if (!IsRenderMode(gfxWindowsPlatform::RENDER_IMAGE_DDRAW16)) { gfxIntSize screen_size(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); gpDDSurf = new gfxDDrawSurface(glpDD, screen_size, gfxASurface::ImageFormatRGB24); if (!gpDDSurf) { /*XXX*/ fprintf(stderr, "couldn't create ddsurf\n"); return PR_FALSE; } } return PR_TRUE; }
void q_initDD() { if (initialized) return; DirectDrawCreate(NULL, &g_pDD, NULL); HRESULT h; h = g_pDD->SetCooperativeLevel(0, DDSCL_NORMAL); if (h != DD_OK) qDebug() << "cooperation level failed"; h = g_pDD->TestCooperativeLevel(); if (h != DD_OK) qDebug() << "cooperation level failed test"; DDSURFACEDESC ddsd; memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; h = g_pDD->CreateSurface(&ddsd, &g_pDDSSurface, NULL); if (h != DD_OK) qDebug() << "CreateSurface failed!"; if (g_pDDSSurface->GetCaps(&ddsCaps) != DD_OK) qDebug() << "GetCaps failed"; q_lock(); q_unlock(); initialized = true; }
/******************************************************************** * Function : WinMain() * Purpose : Mandatory Windows Init function. ********************************************************************/ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwnd; WNDCLASS wc; static char ClassName[] = "ChromeTestingFacility"; DDSURFACEDESC ddsd; DDSCAPS ddscaps; HRESULT ddreturn; int n; // Set all key booleans to FALSE, assume no key is pressed. bForwardKey = FALSE; bBackKey = FALSE; bLeftKey = FALSE; bRightKey = FALSE; nState = 0; nGauge = 0; lpCmdLine = lpCmdLine; hPrevInstance = hPrevInstance; RealTime = 0; /* Start of using spacebar for frameflipping. */ /* Register and realize our display window */ wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = ClassName; wc.lpszClassName = ClassName; RegisterClass(&wc); /* Initialize our test world. */ if (!InitWorld(XRES, YRES, Colormap)) { return FALSE; } /* Convert the Chrome colormap to a windows colormap. */ for (n = 0; n < 256; n++) { WinColormap[n].peRed = (unsigned char)((Colormap[n] & 0xFF0000) >> 16); WinColormap[n].peGreen = (unsigned char)((Colormap[n] & 0xFF00) >> 8); WinColormap[n].peBlue = (unsigned char)((Colormap[n] & 0xFF)); WinColormap[n].peFlags = 0; } /* Create a full screen window so that GDI won't ever be * called. */ hwnd = CreateWindowEx(WS_EX_TOPMOST, ClassName, ClassName, WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL); if (hwnd == NULL) return FALSE; ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); SetFocus(hwnd); ShowCursor(FALSE); /* Remove cursor to prevent GDI from writing. */ /* Instanciate our DirectDraw object */ ddreturn = DirectDrawCreate(NULL, &lpDirectDrawObject, NULL); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } ddreturn = lpDirectDrawObject->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN| DDSCL_ALLOWMODEX); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } /* Create a palette for the surfaces. */ ddreturn = lpDirectDrawObject->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, (LPPALETTEENTRY)WinColormap, &lpPalette, NULL); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } /* Set the video mode to XRESxYRESx8. */ ddreturn = lpDirectDrawObject->SetDisplayMode(XRES, YRES, 8); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } /* Create a default font for the application. */ AppFont = CreateFont(11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, "Comic Sans MS"); /* Create the primary surface and one back buffer surface */ ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; ddreturn = lpDirectDrawObject->CreateSurface(&ddsd, &lpPrimary, NULL); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } ddreturn = lpPrimary->SetPalette(lpPalette); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } /* Get a surface pointer to our back buffer. */ ddscaps.dwCaps = DDSCAPS_BACKBUFFER; ddreturn = lpPrimary->GetAttachedSurface(&ddscaps, &lpBackbuffer); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } /* ddreturn = lpBackbuffer->SetPalette(lpPalette); if (ddreturn != DD_OK) { DestroyWindow(hwnd); return FALSE; } */ { /* Clear the background once for both buffers so we don't get anoying flicker effect. */ DDBLTFX BltFx; BltFx.dwSize = sizeof(BltFx); BltFx.dwFillColor = 255; ddreturn = lpBackbuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &BltFx); BltFx.dwSize = sizeof(BltFx); BltFx.dwFillColor = 255; ddreturn = lpPrimary->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &BltFx); } while (1) { if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) return msg.wParam; TranslateMessage(&msg); DispatchMessage(&msg); } else { if (ActiveApp && RealTime) { // Simulation Iteration should go here. // Only do this when running Realtime. SimLoop(); } else { WaitMessage(); } } } }
int dd_SetMode(int xres, int yres, int bpp, bool windowflag) { HRESULT hr; if (!dd_initd) dd_init(); //do this now for the gamewindow, because this is the first time we know what size to make the gamewindow if(!dd_bGameWindowRectInitialized) { RECT r; dd_gameWindow->adjust(xres,yres,&r); int base_win_x_res = getInitialWindowXres(); int base_win_y_res = getInitialWindowYres(); /// this is for the windowsize verge.cfg vars. if( base_win_x_res > 0 && base_win_y_res > 0 ) { int win_offset_x = (r.right-r.left) - xres; int win_offset_y = (r.bottom-r.top) - yres; dd_gameWindow->winw = win_offset_x+base_win_x_res; dd_gameWindow->winh = win_offset_y+base_win_y_res; } else { dd_gameWindow->winw = r.right-r.left; dd_gameWindow->winh = r.bottom-r.top; } WINDOWPLACEMENT wp; wp.length = sizeof(WINDOWPLACEMENT); GetWindowPlacement(dd_gameWindow->hwnd,&wp); wp.rcNormalPosition.left = GetSystemMetrics(SM_CXSCREEN)/2-xres/2; wp.rcNormalPosition.top = GetSystemMetrics(SM_CYSCREEN)/2-yres/2; wp.rcNormalPosition.right = wp.rcNormalPosition.left + dd_gameWindow->winw; wp.rcNormalPosition.bottom = wp.rcNormalPosition.top + dd_gameWindow->winh; SetWindowPlacement(dd_gameWindow->hwnd,&wp); dd_bGameWindowRectInitialized = true; } //must deactivate all auxwindows if(vid_window && !windowflag) for(std::vector<dd_Window*>::iterator it = dd_windows.begin(); it != dd_windows.end(); it++) { if(!(*it)->bGameWindow) (*it)->deactivate(); } if (!windowflag) { //if we are switching into fullscreen, we are going to lose these sizes and positions //save them now so we can restore them when we flip back to windowmode if(vid_window) { WINDOWPLACEMENT wp; wp.length = sizeof(WINDOWPLACEMENT); GetWindowPlacement(dd_gameWindow->hwnd,&wp); dd_gameWindow->winx = wp.rcNormalPosition.left; dd_gameWindow->winy = wp.rcNormalPosition.top; dd_gameWindow->winw = wp.rcNormalPosition.right - wp.rcNormalPosition.left; dd_gameWindow->winh = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top; dd_bWasMaximized = (wp.showCmd == SW_SHOWMAXIMIZED); } //ShowWindow(dd_gameWindow->hwnd,SW_SHOWMAXIMIZED); ShowWindow(dd_gameWindow->hwnd,SW_HIDE); int ret = dd_gameWindow->set_fullscreen(xres,yres,bpp); if(!ret) return 0; ShowWindow(dd_gameWindow->hwnd,SW_SHOW); dd_gameWindow->xres = xres; dd_gameWindow->yres = yres; vid_xres = xres; vid_yres = yres; vid_window = false; dd_bHasBeenFullscreen = true; dd_RegisterBlitters(); return ret; } else { DX_RELEASE(dx_ps); DX_RELEASE(dx_win_ps); if (bpp != DesktopBPP) return 0; if (!vid_window) dx_dd->RestoreDisplayMode(); dx_dd->SetCooperativeLevel(dd_gameWindow->hwnd, DDSCL_NORMAL); hr = dx_dd->CreateSurface(&dx_win_psd, &dx_win_ps, NULL); if (hr != DD_OK) { return 0; } int ret = dd_gameWindow->set_win(xres,yres,bpp); if(!ret) return 0; if(dd_bHasBeenFullscreen) { WINDOWPLACEMENT wp; wp.length = sizeof(WINDOWPLACEMENT); GetWindowPlacement(dd_gameWindow->hwnd,&wp); wp.rcNormalPosition.left = dd_gameWindow->winx; wp.rcNormalPosition.top = dd_gameWindow->winy; wp.rcNormalPosition.right = wp.rcNormalPosition.left + dd_gameWindow->winw; wp.rcNormalPosition.bottom = wp.rcNormalPosition.top + dd_gameWindow->winh; SetWindowPlacement(dd_gameWindow->hwnd,&wp); if(dd_bWasMaximized) ShowWindow(dd_gameWindow->hwnd,SW_SHOWMAXIMIZED); } //must activate all auxwindows if(!vid_window) for(std::vector<dd_Window*>::iterator it = dd_windows.begin(); it != dd_windows.end(); it++) if(!(*it)->bGameWindow) (*it)->activate(); //bring the gamewindow back to the front SetWindowPos(dd_gameWindow->hwnd,HWND_TOP,0,0,0,0,SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE); dd_gameWindow->xres = xres; dd_gameWindow->yres = yres; vid_xres = xres; vid_yres = yres; vid_window = true; dd_RegisterBlitters(); return 1; } return 0; }
BOOL _GameMode( HINSTANCE hInstance, int nCmdShow, int x, int y, int bpp ) { HRESULT result; WNDCLASS wc; DDSURFACEDESC ddsd; DDSCAPS ddscaps; LPDIRECTDRAW pdd; wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = GetStockBrush(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "EXAM3"; RegisterClass( &wc ); MainHwnd = CreateWindowEx ( 0, "EXAM3", NULL, WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL ); if ( !MainHwnd ) return FALSE; SetFocus( MainHwnd ); ShowWindow( MainHwnd, nCmdShow ); UpdateWindow( MainHwnd ); ShowCursor( FALSE ); result = DirectDrawCreate( NULL, &pdd, NULL ); if ( result != DD_OK ) return Fail( MainHwnd ); result = pdd->QueryInterface(IID_IDirectDraw, (LPVOID *) &DirectOBJ); if ( result != DD_OK ) return Fail( MainHwnd ); gWidth=x; gHeight=y; // 윈도우 핸들의 협력 단계를 설정한다. if(gFullScreen){ result = DirectOBJ->SetCooperativeLevel( MainHwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN ); if ( result != DD_OK ) return Fail( MainHwnd ); result = DirectOBJ->SetDisplayMode( x, y, bpp); if ( result != DD_OK ) return Fail( MainHwnd ); memset( &ddsd, 0, sizeof(ddsd) ); ddsd.dwSize = sizeof( ddsd ); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; result = DirectOBJ -> CreateSurface( &ddsd, &RealScreen, NULL ); if ( result != DD_OK ) return Fail( MainHwnd ); memset( &ddscaps, 0, sizeof(ddscaps) ); ddscaps.dwCaps = DDSCAPS_BACKBUFFER; result = RealScreen -> GetAttachedSurface( &ddscaps, &BackScreen ); if ( result != DD_OK ) return Fail( MainHwnd ); } else{ result = DirectOBJ->SetCooperativeLevel( MainHwnd, DDSCL_NORMAL ); if ( result != DD_OK ) return Fail( MainHwnd ); memset( &ddsd, 0, sizeof(ddsd) ); ddsd.dwSize = sizeof( ddsd ); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ddsd.dwBackBufferCount = 0; result = DirectOBJ -> CreateSurface( &ddsd, &RealScreen, NULL ); if(result != DD_OK) return Fail(MainHwnd); memset( &ddsd, 0, sizeof(ddsd) ); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = x; ddsd.dwHeight = y; result = DirectOBJ->CreateSurface( &ddsd, &BackScreen, NULL ); if ( result != DD_OK ) return Fail( MainHwnd ); result = DirectOBJ->CreateClipper( 0, &ClipScreen, NULL); if ( result != DD_OK ) return Fail( MainHwnd ); result = ClipScreen->SetHWnd( 0, MainHwnd ); if ( result != DD_OK ) return Fail( MainHwnd ); result = RealScreen->SetClipper( ClipScreen ); if ( result != DD_OK ) return Fail( MainHwnd ); SetWindowPos(MainHwnd, NULL, 100, 100, x, y, SWP_NOZORDER | SWP_NOACTIVATE); } return TRUE; }
//----------------------------------------------------------------------------- // Name: GetDXVersion() // Desc: This function returns the DirectX version number as follows: // 0x0000 = No DirectX installed // 0x0100 = DirectX version 1 installed // 0x0200 = DirectX 2 installed // 0x0300 = DirectX 3 installed // 0x0500 = At least DirectX 5 installed. // 0x0600 = At least DirectX 6 installed. // 0x0601 = At least DirectX 6.1 installed. // 0x0700 = At least DirectX 7 installed. // 0x0800 = At least DirectX 8 installed. // // Please note that this code is intended as a general guideline. Your // app will probably be able to simply query for functionality (via // QueryInterface) for one or two components. // // Please also note: // "if( dwDXVersion != 0x500 ) return FALSE;" is VERY BAD. // "if( dwDXVersion < 0x500 ) return FALSE;" is MUCH BETTER. // to ensure your app will run on future releases of DirectX. //----------------------------------------------------------------------------- DWORD GetDXVersion() { DIRECTDRAWCREATE DirectDrawCreate = NULL; DIRECTDRAWCREATEEX DirectDrawCreateEx = NULL; DIRECTINPUTCREATE DirectInputCreate = NULL; HINSTANCE hDDrawDLL = NULL; HINSTANCE hDInputDLL = NULL; HINSTANCE hD3D8DLL = NULL; HINSTANCE hDPNHPASTDLL = NULL; LPDIRECTDRAW pDDraw = NULL; LPDIRECTDRAW2 pDDraw2 = NULL; LPDIRECTDRAWSURFACE pSurf = NULL; LPDIRECTDRAWSURFACE3 pSurf3 = NULL; LPDIRECTDRAWSURFACE4 pSurf4 = NULL; DWORD dwDXVersion = 0; HRESULT hr; // First see if DDRAW.DLL even exists. hDDrawDLL = LoadLibrary(_T("DDRAW.DLL")); if( hDDrawDLL == NULL ) { dwDXVersion = 0; OutputDebugString(_T("Couldn't LoadLibrary DDraw\r\n")); return dwDXVersion; } // See if we can create the DirectDraw object. DirectDrawCreate = (DIRECTDRAWCREATE)GetProcAddress( hDDrawDLL, "DirectDrawCreate" ); if( DirectDrawCreate == NULL ) { dwDXVersion = 0; FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't GetProcAddress DirectDrawCreate\r\n")); return dwDXVersion; } hr = DirectDrawCreate( NULL, &pDDraw, NULL ); if( FAILED(hr) ) { dwDXVersion = 0; FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't create DDraw\r\n")); return dwDXVersion; } // So DirectDraw exists. We are at least DX1. dwDXVersion = 0x100; // Let's see if IID_IDirectDraw2 exists. hr = pDDraw->QueryInterface( IID_IDirectDraw2, (VOID**)&pDDraw2 ); if( FAILED(hr) ) { // No IDirectDraw2 exists... must be DX1 pDDraw->Release(); FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't QI DDraw2\r\n")); return dwDXVersion; } // IDirectDraw2 exists. We must be at least DX2 pDDraw2->Release(); dwDXVersion = 0x200; //------------------------------------------------------------------------- // DirectX 3.0 Checks //------------------------------------------------------------------------- // DirectInput was added for DX3 hDInputDLL = LoadLibrary(_T("DINPUT.DLL")); if( hDInputDLL == NULL ) { // No DInput... must not be DX3 pDDraw->Release(); FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't LoadLibrary DInput\r\n")); return dwDXVersion; } DirectInputCreate = (DIRECTINPUTCREATE)GetProcAddress( hDInputDLL, "DirectInputCreateA" ); if( DirectInputCreate == NULL ) { // No DInput... must be DX2 FreeLibrary( hDInputDLL ); pDDraw->Release(); FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't GetProcAddress DInputCreate\r\n")); return dwDXVersion; } // DirectInputCreate exists. We are at least DX3 dwDXVersion = 0x300; FreeLibrary( hDInputDLL ); // Can do checks for 3a vs 3b here //------------------------------------------------------------------------- // DirectX 5.0 Checks //------------------------------------------------------------------------- // We can tell if DX5 is present by checking for the existence of // IDirectDrawSurface3. First, we need a surface to QI off of. DDSURFACEDESC ddsd; ZeroMemory( &ddsd, sizeof(ddsd) ); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; hr = pDDraw->SetCooperativeLevel( NULL, DDSCL_NORMAL ); if( FAILED(hr) ) { // Failure. This means DDraw isn't properly installed. pDDraw->Release(); FreeLibrary( hDDrawDLL ); dwDXVersion = 0; OutputDebugString(_T("Couldn't Set coop level\r\n")); return dwDXVersion; } hr = pDDraw->CreateSurface( &ddsd, &pSurf, NULL ); if( FAILED(hr) ) { // Failure. This means DDraw isn't properly installed. pDDraw->Release(); FreeLibrary( hDDrawDLL ); dwDXVersion = 0; OutputDebugString(_T("Couldn't CreateSurface\r\n")); return dwDXVersion; } // Query for the IDirectDrawSurface3 interface if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface3, (VOID**)&pSurf3 ) ) ) { pSurf->Release(); pDDraw->Release(); FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't QI DDS3\r\n")); return dwDXVersion; } // QI for IDirectDrawSurface3 succeeded. We must be at least DX5 dwDXVersion = 0x500; pSurf3->Release(); //------------------------------------------------------------------------- // DirectX 6.0 Checks //------------------------------------------------------------------------- // The IDirectDrawSurface4 interface was introduced with DX 6.0 if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface4, (VOID**)&pSurf4 ) ) ) { pSurf->Release(); pDDraw->Release(); FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't QI DDS4\r\n")); return dwDXVersion; } // IDirectDrawSurface4 was create successfully. We must be at least DX6 dwDXVersion = 0x600; pSurf4->Release(); pSurf->Release(); pDDraw->Release(); //------------------------------------------------------------------------- // DirectX 6.1 Checks //------------------------------------------------------------------------- // Check for DMusic, which was introduced with DX6.1 LPDIRECTMUSIC pDMusic = NULL; CoInitialize( NULL ); hr = CoCreateInstance( CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, IID_IDirectMusic, (VOID**)&pDMusic ); if( FAILED(hr) ) { FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't create CLSID_DirectMusic\r\n")); return dwDXVersion; } // DirectMusic was created successfully. We must be at least DX6.1 dwDXVersion = 0x601; pDMusic->Release(); CoUninitialize(); //------------------------------------------------------------------------- // DirectX 7.0 Checks //------------------------------------------------------------------------- // Check for DirectX 7 by creating a DDraw7 object LPDIRECTDRAW7 pDD7; DirectDrawCreateEx = (DIRECTDRAWCREATEEX)GetProcAddress( hDDrawDLL, "DirectDrawCreateEx" ); if( NULL == DirectDrawCreateEx ) { FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't GetProcAddress DirectDrawCreateEx\r\n")); return dwDXVersion; } if( FAILED( DirectDrawCreateEx( NULL, (VOID**)&pDD7, IID_IDirectDraw7, NULL ) ) ) { FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't DirectDrawCreateEx\r\n")); return dwDXVersion; } // DDraw7 was created successfully. We must be at least DX7.0 dwDXVersion = 0x700; pDD7->Release(); //------------------------------------------------------------------------- // DirectX 8.0 Checks //------------------------------------------------------------------------- // Simply see if D3D8.dll exists. hD3D8DLL = LoadLibrary(_T("D3D8.DLL")); if( hD3D8DLL == NULL ) { FreeLibrary( hDDrawDLL ); OutputDebugString(_T("Couldn't LoadLibrary D3D8.DLL\r\n")); return dwDXVersion; } // D3D8.dll exists. We must be at least DX8.0 dwDXVersion = 0x800; //------------------------------------------------------------------------- // DirectX 8.1 Checks //------------------------------------------------------------------------- // Simply see if dpnhpast.dll exists. hDPNHPASTDLL = LoadLibrary(_T("dpnhpast.dll")); if( hDPNHPASTDLL == NULL ) { FreeLibrary( hDPNHPASTDLL ); OutputDebugString(_T("Couldn't LoadLibrary dpnhpast.dll\r\n")); return dwDXVersion; } // dpnhpast.dll exists. We must be at least DX8.1 dwDXVersion = 0x801; //------------------------------------------------------------------------- // End of checking for versions of DirectX //------------------------------------------------------------------------- // Close open libraries and return FreeLibrary( hDDrawDLL ); FreeLibrary( hD3D8DLL ); return dwDXVersion; }
//----------------------------------------------------------------------------- // Name: GetDXVersion() // Desc: This function returns two arguments: // dwDXVersion: // 0x0000 = No DirectX installed // 0x0100 = DirectX version 1 installed // 0x0200 = DirectX 2 installed // 0x0300 = DirectX 3 installed // 0x0500 = At least DirectX 5 installed. // 0x0600 = At least DirectX 6 installed. // 0x0601 = At least DirectX 6.1 installed. // 0x0700 = At least DirectX 7 installed. // dwDXPlatform: // 0 = Unknown (This is a failure case) // VER_PLATFORM_WIN32_WINDOWS = Windows 9X platform // VER_PLATFORM_WIN32_NT = Windows NT platform // // Please note that this code is intended as a general guideline. Your // app will probably be able to simply query for functionality (via // QueryInterface) for one or two components. // // Please also note: // "if (dxVer != 0x500) return FALSE;" is BAD. // "if (dxVer < 0x500) return FALSE;" is MUCH BETTER. // to ensure your app will run on future releases of DirectX. //----------------------------------------------------------------------------- VOID GetDXVersion( DWORD* pdwDXVersion, DWORD* pdwDXPlatform ) { HRESULT hr; HINSTANCE DDHinst = 0; HINSTANCE DIHinst = 0; LPDIRECTDRAW pDDraw = 0; LPDIRECTDRAW2 pDDraw2 = 0; DIRECTDRAWCREATE DirectDrawCreate = 0; DIRECTDRAWCREATEEX DirectDrawCreateEx = 0; DIRECTINPUTCREATE DirectInputCreate = 0; OSVERSIONINFO osVer; LPDIRECTDRAWSURFACE pSurf = 0; LPDIRECTDRAWSURFACE3 pSurf3 = 0; LPDIRECTDRAWSURFACE4 pSurf4 = 0; // First get the windows platform osVer.dwOSVersionInfoSize = sizeof(osVer); if( !GetVersionEx( &osVer ) ) { (*pdwDXPlatform) = 0; (*pdwDXVersion) = 0; return; } if( osVer.dwPlatformId == VER_PLATFORM_WIN32_NT ) { (*pdwDXPlatform) = VER_PLATFORM_WIN32_NT; // NT is easy... NT 4.0 is DX2, 4.0 SP3 is DX3, 5.0 is DX5 // and no DX on earlier versions. if( osVer.dwMajorVersion < 4 ) { (*pdwDXVersion) = 0; // No DX on NT3.51 or earlier return; } if( osVer.dwMajorVersion == 4 ) { // NT4 up to SP2 is DX2, and SP3 onwards is DX3, so we are at least DX2 (*pdwDXVersion) = 0x200; // We're not supposed to be able to tell which SP we're on, so check for dinput DIHinst = LoadLibrary( "DINPUT.DLL" ); if( DIHinst == 0 ) { // No DInput... must be DX2 on NT 4 pre-SP3 OutputDebugString( "Couldn't LoadLibrary DInput\r\n" ); return; } DirectInputCreate = (DIRECTINPUTCREATE)GetProcAddress( DIHinst, "DirectInputCreateA" ); FreeLibrary( DIHinst ); if( DirectInputCreate == 0 ) { // No DInput... must be pre-SP3 DX2 OutputDebugString( "Couldn't GetProcAddress DInputCreate\r\n" ); return; } // It must be NT4, DX2 (*pdwDXVersion) = 0x300; // DX3 on NT4 SP3 or higher return; } // Else it's NT5 or higher, and it's DX5a or higher: Drop through to // Win9x tests for a test of DDraw (DX6 or higher) } else { // Not NT... must be Win9x (*pdwDXPlatform) = VER_PLATFORM_WIN32_WINDOWS; } // Now we know we are in Windows 9x (or maybe 3.1), so anything's possible. // First see if DDRAW.DLL even exists. DDHinst = LoadLibrary( "DDRAW.DLL" ); if( DDHinst == 0 ) { (*pdwDXVersion) = 0; (*pdwDXPlatform) = 0; FreeLibrary( DDHinst ); return; } // See if we can create the DirectDraw object. DirectDrawCreate = (DIRECTDRAWCREATE)GetProcAddress( DDHinst, "DirectDrawCreate" ); if( DirectDrawCreate == 0 ) { (*pdwDXVersion) = 0; (*pdwDXPlatform) = 0; FreeLibrary( DDHinst ); OutputDebugString( "Couldn't LoadLibrary DDraw\r\n" ); return; } hr = DirectDrawCreate( NULL, &pDDraw, NULL ); if( FAILED(hr) ) { (*pdwDXVersion) = 0; (*pdwDXPlatform) = 0; FreeLibrary( DDHinst ); OutputDebugString( "Couldn't create DDraw\r\n" ); return; } // So DirectDraw exists. We are at least DX1. (*pdwDXVersion) = 0x100; // Let's see if IID_IDirectDraw2 exists. hr = pDDraw->QueryInterface( IID_IDirectDraw2, (VOID**)&pDDraw2 ); if( FAILED(hr) ) { // No IDirectDraw2 exists... must be DX1 pDDraw->Release(); FreeLibrary( DDHinst ); OutputDebugString( "Couldn't QI DDraw2\r\n" ); return; } // IDirectDraw2 exists. We must be at least DX2 pDDraw2->Release(); (*pdwDXVersion) = 0x200; /////////////////////////////////////////////////////////////////////////// // DirectX 3.0 Checks /////////////////////////////////////////////////////////////////////////// // DirectInput was added for DX3 DIHinst = LoadLibrary( "DINPUT.DLL" ); if( DIHinst == 0 ) { // No DInput... must not be DX3 OutputDebugString( "Couldn't LoadLibrary DInput\r\n" ); pDDraw->Release(); FreeLibrary( DDHinst ); return; } DirectInputCreate = (DIRECTINPUTCREATE)GetProcAddress( DIHinst, "DirectInputCreateA" ); if( DirectInputCreate == 0 ) { // No DInput... must be DX2 FreeLibrary( DIHinst ); FreeLibrary( DDHinst ); pDDraw->Release(); OutputDebugString( "Couldn't GetProcAddress DInputCreate\r\n" ); return; } // DirectInputCreate exists. We are at least DX3 (*pdwDXVersion) = 0x300; FreeLibrary( DIHinst ); // Can do checks for 3a vs 3b here /////////////////////////////////////////////////////////////////////////// // DirectX 5.0 Checks /////////////////////////////////////////////////////////////////////////// // We can tell if DX5 is present by checking for the existence of // IDirectDrawSurface3. First, we need a surface to QI off of. DDSURFACEDESC ddsd; ZeroMemory( &ddsd, sizeof(ddsd) ); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; hr = pDDraw->SetCooperativeLevel( NULL, DDSCL_NORMAL ); if( FAILED(hr) ) { // Failure. This means DDraw isn't properly installed. pDDraw->Release(); FreeLibrary( DDHinst ); (*pdwDXVersion) = 0; OutputDebugString( "Couldn't Set coop level\r\n" ); return; } hr = pDDraw->CreateSurface( &ddsd, &pSurf, NULL ); if( FAILED(hr) ) { // Failure. This means DDraw isn't properly installed. pDDraw->Release(); FreeLibrary( DDHinst ); *pdwDXVersion = 0; OutputDebugString( "Couldn't CreateSurface\r\n" ); return; } // Query for the IDirectDrawSurface3 interface if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface3, (VOID**)&pSurf3 ) ) ) { pDDraw->Release(); FreeLibrary( DDHinst ); return; } // QI for IDirectDrawSurface3 succeeded. We must be at least DX5 (*pdwDXVersion) = 0x500; /////////////////////////////////////////////////////////////////////////// // DirectX 6.0 Checks /////////////////////////////////////////////////////////////////////////// // The IDirectDrawSurface4 interface was introduced with DX 6.0 if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface4, (VOID**)&pSurf4 ) ) ) { pDDraw->Release(); FreeLibrary( DDHinst ); return; } // IDirectDrawSurface4 was create successfully. We must be at least DX6 (*pdwDXVersion) = 0x600; pSurf->Release(); pDDraw->Release(); /////////////////////////////////////////////////////////////////////////// // DirectX 6.1 Checks /////////////////////////////////////////////////////////////////////////// // Check for DMusic, which was introduced with DX6.1 LPDIRECTMUSIC pDMusic = NULL; CoInitialize( NULL ); hr = CoCreateInstance( CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, IID_IDirectMusic, (VOID**)&pDMusic ); if( FAILED(hr) ) { OutputDebugString( "Couldn't create CLSID_DirectMusic\r\n" ); FreeLibrary( DDHinst ); return; } // DirectMusic was created successfully. We must be at least DX6.1 (*pdwDXVersion) = 0x601; pDMusic->Release(); CoUninitialize(); /////////////////////////////////////////////////////////////////////////// // DirectX 7.0 Checks /////////////////////////////////////////////////////////////////////////// // Check for DirectX 7 by creating a DDraw7 object LPDIRECTDRAW7 pDD7; DirectDrawCreateEx = (DIRECTDRAWCREATEEX)GetProcAddress( DDHinst, "DirectDrawCreateEx" ); if( NULL == DirectDrawCreateEx ) { FreeLibrary( DDHinst ); return; } if( FAILED( DirectDrawCreateEx( NULL, (VOID**)&pDD7, IID_IDirectDraw7, NULL ) ) ) { FreeLibrary( DDHinst ); return; } // DDraw7 was created successfully. We must be at least DX7.0 (*pdwDXVersion) = 0x700; pDD7->Release(); /////////////////////////////////////////////////////////////////////////// // End of checks /////////////////////////////////////////////////////////////////////////// // Close open libraries and return FreeLibrary( DDHinst ); return; }
extern "C" unsigned char rglswCreateWindow(GLint ihwnd, GLint width) { DDSURFACEDESC ddsd; HRESULT ddrval; DDPIXELFORMAT pix; DDSCAPS ddscaps; DDCAPS ddcaps; HWND hWnd = (HWND)ihwnd; SCRWIDTH = width; switch (width) { case 1600: SCRHEIGHT = 1200; break; case 1280: SCRHEIGHT = 1024; break; case 1024: SCRHEIGHT = 768; break; case 800: SCRHEIGHT = 600; break; default: SCRWIDTH = 640; SCRHEIGHT = 480; } hwnd = hWnd; ddrval = DirectDrawCreate(NULL, &lpDD, NULL); if (ddrval != DD_OK) { return FALSE; } ZeroMemory(&ddcaps, sizeof(ddcaps)); ddcaps.dwSize = sizeof(ddcaps); lpDD->GetCaps(&ddcaps, NULL); if (WINDOWED) { ddrval = lpDD->SetCooperativeLevel(hwnd, DDSCL_NORMAL); if (ddrval != DD_OK) { return FALSE; } } else { ddrval = lpDD->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN); if (ddrval != DD_OK) { return FALSE; } ddrval = lpDD->SetDisplayMode(SCRWIDTH, SCRHEIGHT, 16); if (ddrval != DD_OK) { return FALSE; } } ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); if (WINDOWED) { ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ddrval = lpDD->CreateSurface(&ddsd, &PrimarySurface, NULL); } else { ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ddrval = lpDD->CreateSurface(&ddsd, &PrimarySurface, NULL); } if (ddrval != DD_OK) { return FALSE; } ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY; ddsd.dwWidth = SCRWIDTH; ddsd.dwHeight = SCRHEIGHT; ddrval = lpDD->CreateSurface(&ddsd, &BackSurface, NULL); if (ddrval != DD_OK) { ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddrval = lpDD->CreateSurface(&ddsd, &BackSurface, NULL); } if (ddrval != DD_OK) { return FALSE; } pix.dwSize = sizeof(pix); pix.dwFlags = DDPF_RGB; BackSurface->GetPixelFormat(&pix); if (pix.dwRBitMask == 0x7C00) { depth = 15; } else { depth = 16; } bActive = TRUE; if (useDirectDraw) { scratch_pitch = GetPitch(); scratch_buffer_2 = NULL; } else { depth = 15; scratch_pitch = 2*SCRWIDTH; scratch_buffer_2 = (BYTE*)malloc(scratch_pitch*SCRHEIGHT); } scratch_buffer = (BYTE*)malloc(scratch_pitch*SCRHEIGHT); memset(scratch_buffer, 0, scratch_pitch*SCRHEIGHT); clear_buffers(); return TRUE; }
//must be the game window int dd_Window::set_fullscreen(int w, int h, int bpp) { _ASSERTE(bGameWindow); quad ws; HRESULT hr; DX_RELEASE(dx_os); DX_RELEASE(dx_ps); DX_RELEASE(dx_win_ps); hr = dx_dd->SetCooperativeLevel(hMainWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN); if (hr != DD_OK) { return 0; } //hack for 320x200 letterboxing if(w == 320 && h == 200) dx_dd->SetDisplayMode(320, 240, bpp); else dx_dd->SetDisplayMode(w, h, bpp); hr = dx_dd->CreateSurface(&dx_psd, &dx_ps, NULL); if (hr != DD_OK) { return 0; } dx_osd.dwWidth=w; dx_osd.dwHeight=h; hr=dx_dd->CreateSurface(&dx_osd,&dx_os,NULL); if(hr!=DD_OK) { return 0; } hr = dx_ps->GetAttachedSurface(&dx_bsd.ddsCaps, &dx_bs); if (hr != DD_OK) { DX_RELEASE(dx_os); return 0; } ws = GetWindowLong(hwnd, GWL_STYLE); ws &= ~WS_OVERLAPPEDWINDOW; ws |= WS_POPUP; SetWindowLong(hwnd, GWL_STYLE, ws); SetWindowPos(hwnd,0,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED | SWP_NOZORDER); //make window take up entire screen //choose one or the other //SetWindowPos(hwnd,0,0,0,w,h,SWP_NOACTIVATE | SWP_NOZORDER); ShowWindow(hwnd,SW_SHOWMAXIMIZED); //set pixelformat parameters { DDPIXELFORMAT ddpf; ddpf.dwSize = sizeof(ddpf); ddpf.dwFlags = DDPF_RGB; hr = dx_ps->GetPixelFormat(&ddpf); if (hr != DD_OK) err("Could not get pixel format!"); //if (ddpf.dwRBitMask == 0x7C00 && bpp == 16) // vid_bpp = 15, vid_bytesperpixel = 2; //else // vid_bpp = bpp, vid_bytesperpixel = bpp / 8; vid_bpp = 32; } if(img) delete img; img = new image(); img->shell = true; SetHandleImage(1,img); screen = img; // img->alphamap = 0; img->width = w; img->height = h; img->cx1 = 0; img->cx2 = w-1; img->cy1 = 0; img->cy2 = h-1; img->data=0; flip_fullscreen(); return 1; }
extern "C" HRESULT InitDirectDraw(HWND hWnd, int w, int h, void* pixelData) { WNDCLASS wc; DDSURFACEDESC ddsd; HRESULT hRet; hRet = DirectDrawCreate(NULL, &g_pDD, NULL); if (hRet != DD_OK) return InitFail(hWnd, hRet, szDDrawFailedMsg); // Get exclusive mode hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN); //DDSCL_NORMAL);// if (hRet != DD_OK) return InitFail(hWnd, hRet, szSetCooperativeFailMsg); #if 0 DDCAPS ddCaps; DDCAPS ddHelCaps; g_pDD->GetCaps(&ddCaps, &ddHelCaps); if (!(ddCaps.ddsCaps.dwCaps & DDSCAPS_BACKBUFFER)) { return InitFail(hWnd, E_FAIL, szNoBackBufferMsg); } if (!(ddCaps.ddsCaps.dwCaps & DDSCAPS_FLIP)) { return InitFail(hWnd, E_FAIL, szNoFlipSurfacesMsg); } #endif // Create the primary surface with 1 back buffer memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS;// | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; //| DDSCAPS_FLIP; //ddsd.dwBackBufferCount = 1; hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL); if (hRet != DD_OK) { return InitFail(hWnd, hRet, szCreateSurfaceFailMsg); } DWORD dwID; g_hRefreshScreen = CreateEvent(NULL, FALSE, FALSE, NULL); CreateThread(NULL, 0, ScreenRefresThread, NULL, 0, &dwID); return DD_OK; #if 0 #define WIDTH w // in pixels #define HEIGHT h #define DEPTH 2 // in bytes (2 bytes == 16 bits) if (ddsd.dwBackBufferCount > 0) { hRet = g_pDDSPrimary->EnumAttachedSurfaces(&g_pDDSBack, EnumFunction); if (hRet != DD_OK) return InitFail(hWnd, hRet, szEnumAttachedSurfacesFailMsg); } ZeroMemory(&ddsd, sizeof(DDSURFACEDESC)); ZeroMemory(&ddsd.ddpfPixelFormat, sizeof(DDPIXELFORMAT)); ddsd.dwSize = sizeof(ddsd); hRet = g_pDDSBack->Lock(NULL, &ddsd, DDLOCK_WAITNOTBUSY, NULL); g_pDDSPrimary->Unlock(NULL); ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_LPSURFACE | DDSD_PITCH | DDSD_PIXELFORMAT; ddsd.dwWidth = WIDTH; ddsd.dwHeight= HEIGHT; //ddsd.lPitch = (LONG)DEPTH * WIDTH; ddsd.lpSurface = pixelData; /* // Set up the pixel format for 24-bit RGB (8-8-8). ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT); ddsd.ddpfPixelFormat.dwFlags= DDPF_RGB; ddsd.ddpfPixelFormat.dwRGBBitCount = (DWORD)DEPTH*8; ddsd.ddpfPixelFormat.dwRBitMask = 0xF800;//surface->format->Rmask; ddsd.ddpfPixelFormat.dwGBitMask = 0x07E0;//surface->format->Gmask; ddsd.ddpfPixelFormat.dwBBitMask = 0x001F;//surface->format->Bmask; //ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000; //ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00; //ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF; */ // Create the surface hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL); if (hRet != DD_OK) return InitFail(hWnd, hRet, szEnumAttachedSurfacesFailMsg); return DD_OK; #endif if (ddsd.dwBackBufferCount > 0) { hRet = g_pDDSPrimary->EnumAttachedSurfaces(&g_pDDSBack, EnumFunction); if (hRet != DD_OK) return InitFail(hWnd, hRet, szEnumAttachedSurfacesFailMsg); } return DD_OK; #if 1 ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = (DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PITCH | DDSD_LPSURFACE | DDSD_PIXELFORMAT); ddsd.dwWidth = w; ddsd.dwHeight= h; #if defined(NONAMELESSUNION) ddsd.u1.lPitch = SDL_CalculatePitch(w, 2);; #else ddsd.lPitch = SDL_CalculatePitch(w, 2);; #endif ddsd.lpSurface = pixelData; const int SDL_HWSURFACE = 1; int flag = 1; 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 = 16;//surface->format->BitsPerPixel; ddsd.ddpfPixelFormat.dwRBitMask = 0xF800;//surface->format->Rmask; ddsd.ddpfPixelFormat.dwGBitMask = 0x07E0;//surface->format->Gmask; ddsd.ddpfPixelFormat.dwBBitMask = 0x001F;//surface->format->Bmask; #endif /* Create the DirectDraw video surface */ //if ( requested != NULL ) { // g_pDDSOne = requested; //} else { hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL); #if 0 if ( hRet != DD_OK ) { SetDDerror("DirectDraw2::CreateSurface", hRet); goto error_end; } hRet = g_pDDSBack->QueryInterface( &IID_IDirectDrawSurface3, (LPVOID *)&g_pDDSOne); g_pDDSBack->Release(); if ( hRet != DD_OK ) { SetDDerror("DirectDrawSurface::QueryInterface", hRet); goto error_end; } #endif } #if 0 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! */ hRet = g_pDDSOne->GetCaps(&ddsd.ddsCaps); if ( hRet != DD_OK ) { //SetDDerror("DirectDrawSurface3::GetCaps", hRet); 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 = pixelData;//surface->pixels; hRet = g_pDDSOne->SetSurfaceDesc( &ddsd, 0); if ( hRet != DD_OK ) { //SetDDerror("DirectDraw2::SetSurfaceDesc", hRet); goto error_end; } } /* Make sure the surface format was set properly */ SDL_memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); hRet = IDirectDrawSurface3_Lock(g_pDDSOne, NULL, &ddsd, (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL); if ( hRet != DD_OK ) { //SetDDerror("DirectDrawSurface3::Lock", hRet); goto error_end; } IDirectDrawSurface3_Unlock(g_pDDSOne, 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 = g_pDDSOne; if ( (surface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { LPDIRECTDRAWSURFACE3 dd_writebuf; ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; hRet = IDirectDrawSurface3_GetAttachedSurface(g_pDDSOne, &ddsd.ddsCaps, &dd_writebuf); if ( hRet != DD_OK ) { //SetDDerror("DirectDrawSurface3::GetAttachedSurface", hRet); } else {
//----------------------------------------------------------------------------- // Name: InitGraphics() // Desc: //----------------------------------------------------------------------------- HRESULT InitGraphics() { DDCAPS ddcaps; HRESULT hr; DDSURFACEDESC ddsd; DDSCAPS ddscaps; // Create a window g_hwndMain = CreateWindowEx( WS_EX_APPWINDOW, TEXT("DuelClass"), TEXT("Duel"), WS_POPUP | WS_SYSMENU, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, g_hInst, NULL ); if( NULL == g_hwndMain ) return E_FAIL; UpdateWindow( g_hwndMain ); SetFocus( g_hwndMain ); // DDraw stuff begins here if( FAILED( hr = DirectDrawCreate( NULL, &g_pDD, NULL ) ) ) { ShowError(IDS_DDRAW_ERROR_DDC); return E_FAIL; } // Set access mode based on fullscreen/window if( g_bFullscreen ) { hr = g_pDD->SetCooperativeLevel( g_hwndMain, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN ); } else { hr = g_pDD->SetCooperativeLevel( g_hwndMain, DDSCL_NORMAL); } if( FAILED(hr) ) { ShowError(IDS_DDRAW_ERROR_SCL); return E_FAIL; } if( g_bFullscreen ) { // Set the mode to 640 by 480 by 8 if( FAILED( g_pDD->SetDisplayMode( 640, 480, 8 ) ) ) { ShowError(IDS_DDRAW_ERROR_SDM); return E_FAIL; } } else { RECT rcWork; RECT rc; DWORD dwStyle; // If we are still a WS_POPUP window we should convert to a // normal app window so we look like a windows app. dwStyle = GetWindowStyle(g_hwndMain); dwStyle &= ~WS_POPUP; dwStyle |= WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX; SetWindowLong( g_hwndMain, GWL_STYLE, dwStyle ); // Aet window size SetRect( &rc, 0, 0, MAX_DEFWIN_X, MAX_DEFWIN_Y ); AdjustWindowRectEx( &rc, GetWindowStyle(g_hwndMain), GetMenu(g_hwndMain) != NULL, GetWindowExStyle(g_hwndMain) ); SetWindowPos( g_hwndMain, NULL, 0, 0, rc.right-rc.left, rc.bottom-rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); SetWindowPos( g_hwndMain, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE); // Make sure our window does not hang outside of the work area SystemParametersInfo( SPI_GETWORKAREA, 0, &rcWork, 0 ); GetWindowRect( g_hwndMain, &rc ); if( rc.left < rcWork.left ) rc.left = rcWork.left; if( rc.top < rcWork.top ) rc.top = rcWork.top; SetWindowPos( g_hwndMain, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE ); } // Check the color key hardware capabilities ddcaps.dwSize = sizeof( ddcaps ); memset( &ddsd, 0, sizeof( ddsd ) ); ddsd.dwSize = sizeof( ddsd ); if( g_bFullscreen ) { // Create surfaces ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; if( FAILED( hr = g_pDD->CreateSurface( &ddsd, &g_pddsFrontBuffer, NULL ) ) ) { ShowError(IDS_DDRAW_ERROR_CREATESURFACE); return E_FAIL; } // Get a pointer to the back buffer ddscaps.dwCaps = DDSCAPS_BACKBUFFER; if( FAILED( hr = g_pddsFrontBuffer->GetAttachedSurface( &ddscaps, &g_pddsBackBuffer ) ) ) { ShowError(IDS_DDRAW_ERROR_GAS); return E_FAIL; } } else { LPDIRECTDRAWCLIPPER pcClipper; // Window case, create the primary surface // and create a backbuffer in offscreen memory ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; if( FAILED( g_pDD->CreateSurface( &ddsd, &g_pddsFrontBuffer, NULL ) ) ) { ShowError(IDS_DDRAW_ERROR_CREATESURFACE); return E_FAIL; } ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = MAX_DEFWIN_X; ddsd.dwHeight = MAX_DEFWIN_Y; if( FAILED( hr = g_pDD->CreateSurface( &ddsd, &g_pddsBackBuffer, NULL ) ) ) { ShowError(IDS_DDRAW_ERROR_CREATESURFACE); return E_FAIL; } if( FAILED( hr = g_pDD->CreateClipper( 0, &pcClipper, NULL) ) ) { ShowError(IDS_DDRAW_ERROR_CC); return E_FAIL; } if( FAILED( hr = pcClipper->SetHWnd( 0, g_hwndMain) ) ) { pcClipper->Release(); ShowError(IDS_DDRAW_ERROR_SH); return E_FAIL; } if( FAILED( hr = g_pddsFrontBuffer->SetClipper( pcClipper) ) ) { pcClipper->Release(); ShowError(IDS_DDRAW_ERROR_SC); return E_FAIL; } // Done with clipper pcClipper->Release(); } ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = 320; ddsd.dwHeight = 128; for( DWORD i=0; i<4; i++ ) { if( FAILED( hr = g_pDD->CreateSurface( &ddsd, &g_pddsShip[i], NULL ) ) ) { ShowError(IDS_DDRAW_ERROR_CREATESURFACE); return E_FAIL; } } ddsd.dwHeight = 16; if( FAILED( hr = g_pDD->CreateSurface( &ddsd, &g_pddsNumbers, NULL ) ) ) { ShowError(IDS_DDRAW_ERROR_CREATESURFACE); return E_FAIL; } if( FAILED( RestoreSurfaces() ) ) { ShowError(IDS_DDRAW_ERROR_RS); return E_FAIL; } g_dwKeys = 0; ShowWindow( g_hwndMain, SW_SHOW); return S_OK; }