📄 sdl_dx5video.c
字号:
#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); } 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 ( !SDL_windowX && !SDL_windowY ) { 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 =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -