SDL_Surface * SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) { SDL_DisplayMode desktop_mode; int display = GetVideoDisplay(); int window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display); int window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display); int window_w; int window_h; Uint32 window_flags; Uint32 surface_flags; if (!SDL_GetVideoDevice()) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { return NULL; } } SDL_GetDesktopDisplayMode(display, &desktop_mode); if (width == 0) { width = desktop_mode.w; } if (height == 0) { height = desktop_mode.h; } if (bpp == 0) { bpp = SDL_BITSPERPIXEL(desktop_mode.format); } /* See if we can simply resize the existing window and surface */ if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) { return SDL_PublicSurface; } /* Destroy existing window */ SDL_PublicSurface = NULL; if (SDL_ShadowSurface) { SDL_ShadowSurface->flags &= ~SDL_DONTFREE; SDL_FreeSurface(SDL_ShadowSurface); SDL_ShadowSurface = NULL; } if (SDL_VideoSurface) { SDL_VideoSurface->flags &= ~SDL_DONTFREE; SDL_FreeSurface(SDL_VideoSurface); SDL_VideoSurface = NULL; } if (SDL_VideoContext) { /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */ SDL_GL_DeleteContext(SDL_VideoContext); SDL_VideoContext = NULL; } if (SDL_VideoWindow) { SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y); SDL_DestroyWindow(SDL_VideoWindow); } /* Set up the event filter */ if (!SDL_GetEventFilter(NULL, NULL)) { SDL_SetEventFilter(SDL_CompatEventFilter, NULL); } /* Create a new window */ window_flags = SDL_WINDOW_SHOWN; if (flags & SDL_FULLSCREEN) { window_flags |= SDL_WINDOW_FULLSCREEN; } if (flags & SDL_OPENGL) { window_flags |= SDL_WINDOW_OPENGL; } if (flags & SDL_RESIZABLE) { window_flags |= SDL_WINDOW_RESIZABLE; } if (flags & SDL_NOFRAME) { window_flags |= SDL_WINDOW_BORDERLESS; } GetEnvironmentWindowPosition(width, height, &window_x, &window_y); SDL_VideoWindow = SDL_CreateWindow(wm_title, window_x, window_y, width, height, window_flags); if (!SDL_VideoWindow) { return NULL; } SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon); SetupScreenSaver(flags); window_flags = SDL_GetWindowFlags(SDL_VideoWindow); surface_flags = 0; if (window_flags & SDL_WINDOW_FULLSCREEN) { surface_flags |= SDL_FULLSCREEN; } if ((window_flags & SDL_WINDOW_OPENGL) && (flags & SDL_OPENGL)) { surface_flags |= SDL_OPENGL; } if (window_flags & SDL_WINDOW_RESIZABLE) { surface_flags |= SDL_RESIZABLE; } if (window_flags & SDL_WINDOW_BORDERLESS) { surface_flags |= SDL_NOFRAME; } SDL_VideoFlags = flags; /* If we're in OpenGL mode, just create a stub surface and we're done! */ if (flags & SDL_OPENGL) { SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow); if (!SDL_VideoContext) { return NULL; } if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) { return NULL; } SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0); if (!SDL_VideoSurface) { return NULL; } SDL_VideoSurface->flags |= surface_flags; SDL_PublicSurface = SDL_VideoSurface; return SDL_PublicSurface; } /* Create the screen surface */ SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow); if (!SDL_WindowSurface) { return NULL; } /* Center the public surface in the window surface */ SDL_GetWindowSize(SDL_VideoWindow, &window_w, &window_h); SDL_VideoViewport.x = (window_w - width)/2; SDL_VideoViewport.y = (window_h - height)/2; SDL_VideoViewport.w = width; SDL_VideoViewport.h = height; SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0); SDL_VideoSurface->flags |= surface_flags; SDL_VideoSurface->flags |= SDL_DONTFREE; SDL_FreeFormat(SDL_VideoSurface->format); SDL_VideoSurface->format = SDL_WindowSurface->format; SDL_VideoSurface->format->refcount++; SDL_VideoSurface->w = width; SDL_VideoSurface->h = height; SDL_VideoSurface->pitch = SDL_WindowSurface->pitch; SDL_VideoSurface->pixels = (void *)((Uint8 *)SDL_WindowSurface->pixels + SDL_VideoViewport.y * SDL_VideoSurface->pitch + SDL_VideoViewport.x * SDL_VideoSurface->format->BytesPerPixel); SDL_SetClipRect(SDL_VideoSurface, NULL); /* Create a shadow surface if necessary */ if ((bpp != SDL_VideoSurface->format->BitsPerPixel) && !(flags & SDL_ANYFORMAT)) { SDL_ShadowSurface = SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0); if (!SDL_ShadowSurface) { return NULL; } SDL_ShadowSurface->flags |= surface_flags; SDL_ShadowSurface->flags |= SDL_DONTFREE; /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */ if (SDL_ShadowSurface->format->palette) { SDL_ShadowSurface->flags |= SDL_HWPALETTE; SDL_DitherColors(SDL_ShadowSurface->format->palette->colors, SDL_ShadowSurface->format->BitsPerPixel); } SDL_FillRect(SDL_ShadowSurface, NULL, SDL_MapRGB(SDL_ShadowSurface->format, 0, 0, 0)); } SDL_PublicSurface = (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface); ClearVideoSurface(); /* We're finally done! */ return SDL_PublicSurface; }
/* Win32 icon mask semantics are different from those of SDL: SDL applies the mask to the icon and copies result to desktop. Win32 applies the mask to the desktop and XORs the icon on. This means that the SDL mask needs to be applied to the icon and then inverted and passed to Win32. */ void WIN_SetWMIcon(_THIS, SDL_Surface *icon, Uint8 *mask) { #ifdef DISABLE_ICON_SUPPORT return; #else SDL_Palette *pal_256; SDL_Surface *icon_256; Uint8 *pdata, *pwin32; Uint8 *mdata, *mwin32, m = 0; int icon_len; int icon_plen; int icon_mlen; int icon_pitch; int mask_pitch; SDL_Rect bounds; int i, skip; int row, col; struct /* quasi-BMP format */ Win32Icon { Uint32 biSize; Sint32 biWidth; Sint32 biHeight; Uint16 biPlanes; Uint16 biBitCount; Uint32 biCompression; Uint32 biSizeImage; Sint32 biXPelsPerMeter; Sint32 biYPelsPerMeter; Uint32 biClrUsed; Uint32 biClrImportant; struct /* RGBQUAD -- note it's BGR ordered */ { Uint8 rgbBlue; Uint8 rgbGreen; Uint8 rgbRed; Uint8 rgbReserved; } biColors[256]; /* Pixels: Uint8 pixels[] */ /* Mask: Uint8 mask[] */ } *icon_win32; /* Allocate the win32 bmp icon and set everything to zero */ icon_pitch = ((icon->w+3)&~3); mask_pitch = ((icon->w+7)/8); icon_plen = icon->h*icon_pitch; icon_mlen = icon->h*mask_pitch; icon_len = sizeof(*icon_win32)+icon_plen+icon_mlen; icon_win32 = (struct Win32Icon *)alloca(icon_len); if ( icon_win32 == NULL ) { return; } memset(icon_win32, 0, icon_len); /* Set the basic BMP parameters */ icon_win32->biSize = sizeof(*icon_win32)-sizeof(icon_win32->biColors); icon_win32->biWidth = icon->w; icon_win32->biHeight = icon->h*2; icon_win32->biPlanes = 1; icon_win32->biBitCount = 8; icon_win32->biSizeImage = icon_plen+icon_mlen; /* Allocate a standard 256 color icon surface */ icon_256 = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h, icon_win32->biBitCount, 0, 0, 0, 0); if ( icon_256 == NULL ) { return; } pal_256 = icon_256->format->palette; if (icon->format->palette && (icon->format->BitsPerPixel == icon_256->format->BitsPerPixel)){ Uint8 black; memcpy(pal_256->colors, icon->format->palette->colors, pal_256->ncolors*sizeof(SDL_Color)); /* Make sure that 0 is black! */ black = SDL_FindColor(pal_256, 0x00, 0x00, 0x00); pal_256->colors[black] = pal_256->colors[0]; pal_256->colors[0].r = 0x00; pal_256->colors[0].g = 0x00; pal_256->colors[0].b = 0x00; } else { SDL_DitherColors(pal_256->colors, icon_256->format->BitsPerPixel); } /* Now copy color data to the icon BMP */ for ( i=0; i<(1<<icon_win32->biBitCount); ++i ) { icon_win32->biColors[i].rgbRed = pal_256->colors[i].r; icon_win32->biColors[i].rgbGreen = pal_256->colors[i].g; icon_win32->biColors[i].rgbBlue = pal_256->colors[i].b; } /* Convert icon to a standard surface format. This may not always be necessary, as Windows supports a variety of BMP formats, but it greatly simplifies our code. */ bounds.x = 0; bounds.y = 0; bounds.w = icon->w; bounds.h = icon->h; if ( SDL_LowerBlit(icon, &bounds, icon_256, &bounds) < 0 ) { SDL_FreeSurface(icon_256); return; } /* Copy pixels upside-down to icon BMP, masked with the icon mask */ if ( SDL_MUSTLOCK(icon_256) || (icon_256->pitch != icon_pitch) ) { SDL_FreeSurface(icon_256); SDL_SetError("Warning: Unexpected icon_256 characteristics"); return; } pdata = (Uint8 *)icon_256->pixels; mdata = mask; pwin32 = (Uint8 *)icon_win32+sizeof(*icon_win32)+icon_plen-icon_pitch; skip = icon_pitch - icon->w; for ( row=0; row<icon->h; ++row ) { for ( col=0; col<icon->w; ++col ) { if ( (col%8) == 0 ) { m = *mdata++; } if ( (m&0x80) != 0x00 ) { *pwin32 = *pdata; } m <<= 1; ++pdata; ++pwin32; } pdata += skip; pwin32 += skip; pwin32 -= 2*icon_pitch; } SDL_FreeSurface(icon_256); /* Copy mask inverted and upside-down to icon BMP */ mdata = mask; mwin32 = (Uint8 *)icon_win32 +sizeof(*icon_win32)+icon_plen+icon_mlen-mask_pitch; for ( row=0; row<icon->h; ++row ) { for ( col=0; col<mask_pitch; ++col ) { *mwin32++ = ~*mdata++; } mwin32 -= 2*mask_pitch; } /* Finally, create the icon handle and set the window icon */ screen_icn = CreateIconFromResourceEx((Uint8 *)icon_win32, icon_len, TRUE, 0x00030000, icon->w, icon->h, LR_DEFAULTCOLOR); if ( screen_icn == NULL ) { SDL_SetError("Couldn't create Win32 icon handle"); } else { SetClassLong(SDL_Window, GCL_HICON, (LONG)screen_icn); } #endif /* DISABLE_ICON_SUPPORT */ }
SDL_Surface * SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) { SDL_DisplayMode desktop_mode; SDL_DisplayMode mode; int window_x = SDL_WINDOWPOS_UNDEFINED; int window_y = SDL_WINDOWPOS_UNDEFINED; Uint32 window_flags; Uint32 desktop_format; Uint32 desired_format; Uint32 surface_flags; if (!SDL_GetVideoDevice()) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { return NULL; } } SDL_GetDesktopDisplayMode(&desktop_mode); if (width == 0) { width = desktop_mode.w; } if (height == 0) { height = desktop_mode.h; } /* See if we can simply resize the existing window and surface */ if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) { return SDL_PublicSurface; } /* Destroy existing window */ SDL_PublicSurface = NULL; if (SDL_ShadowSurface) { SDL_FreeSurface(SDL_ShadowSurface); SDL_ShadowSurface = NULL; } if (SDL_VideoSurface) { SDL_DelPaletteWatch(SDL_VideoSurface->format->palette, SDL_VideoPaletteChanged, NULL); SDL_FreeSurface(SDL_VideoSurface); SDL_VideoSurface = NULL; } if (SDL_VideoContext) { /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */ SDL_GL_DeleteContext(SDL_VideoContext); SDL_VideoContext = NULL; } if (SDL_VideoWindow) { SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y); SDL_DestroyWindow(SDL_VideoWindow); } /* Set up the event filter */ if (!SDL_GetEventFilter(NULL, NULL)) { SDL_SetEventFilter(SDL_CompatEventFilter, NULL); } /* Create a new window */ window_flags = SDL_WINDOW_SHOWN; if (flags & SDL_FULLSCREEN) { window_flags |= SDL_WINDOW_FULLSCREEN; } if (flags & SDL_OPENGL) { window_flags |= SDL_WINDOW_OPENGL; } if (flags & SDL_RESIZABLE) { window_flags |= SDL_WINDOW_RESIZABLE; } if (flags & SDL_NOFRAME) { window_flags |= SDL_WINDOW_BORDERLESS; } GetEnvironmentWindowPosition(width, height, &window_x, &window_y); SDL_SetFullscreenDisplayMode(NULL); SDL_VideoWindow = SDL_CreateWindow(wm_title, window_x, window_y, width, height, window_flags); if (!SDL_VideoWindow) { return NULL; } SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon); window_flags = SDL_GetWindowFlags(SDL_VideoWindow); surface_flags = 0; if (window_flags & SDL_WINDOW_FULLSCREEN) { surface_flags |= SDL_FULLSCREEN; } if (window_flags & SDL_WINDOW_OPENGL) { surface_flags |= SDL_OPENGL; } if (window_flags & SDL_WINDOW_RESIZABLE) { surface_flags |= SDL_RESIZABLE; } if (window_flags & SDL_WINDOW_BORDERLESS) { surface_flags |= SDL_NOFRAME; } /* Set up the desired display mode */ desktop_format = desktop_mode.format; if (desktop_format && ((flags & SDL_ANYFORMAT) || (bpp == SDL_BITSPERPIXEL(desktop_format)))) { desired_format = desktop_format; } else { switch (bpp) { case 0: if (desktop_format) { desired_format = desktop_format; } else { desired_format = SDL_PIXELFORMAT_RGB888; } bpp = SDL_BITSPERPIXEL(desired_format); break; case 8: desired_format = SDL_PIXELFORMAT_INDEX8; break; case 15: desired_format = SDL_PIXELFORMAT_RGB555; break; case 16: desired_format = SDL_PIXELFORMAT_RGB565; break; case 24: desired_format = SDL_PIXELFORMAT_RGB24; break; case 32: desired_format = SDL_PIXELFORMAT_RGB888; break; default: SDL_SetError("Unsupported bpp in SDL_SetVideoMode()"); return NULL; } } mode.format = desired_format; mode.w = width; mode.h = height; mode.refresh_rate = 0; /* Set the desired display mode */ if (flags & SDL_FULLSCREEN) { if (SDL_SetFullscreenDisplayMode(&mode) < 0) { return NULL; } } /* If we're in OpenGL mode, just create a stub surface and we're done! */ if (flags & SDL_OPENGL) { SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow); if (!SDL_VideoContext) { return NULL; } if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) { return NULL; } SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0); if (!SDL_VideoSurface) { return NULL; } SDL_VideoSurface->flags |= surface_flags; SDL_PublicSurface = SDL_VideoSurface; return SDL_PublicSurface; } /* Create a renderer for the window */ if (SDL_CreateRenderer (SDL_VideoWindow, -1, SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0) { return NULL; } SDL_GetRendererInfo(&SDL_VideoRendererInfo); /* Create a texture for the screen surface */ SDL_VideoTexture = SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width, height); if (!SDL_VideoTexture) { SDL_VideoTexture = SDL_CreateTexture(desktop_format, SDL_TEXTUREACCESS_STREAMING, width, height); } if (!SDL_VideoTexture) { return NULL; } /* Create the screen surface */ SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture); if (!SDL_VideoSurface) { return NULL; } SDL_VideoSurface->flags |= surface_flags; /* Set a default screen palette */ if (SDL_VideoSurface->format->palette) { SDL_VideoSurface->flags |= SDL_HWPALETTE; SDL_DitherColors(SDL_VideoSurface->format->palette->colors, SDL_VideoSurface->format->BitsPerPixel); SDL_AddPaletteWatch(SDL_VideoSurface->format->palette, SDL_VideoPaletteChanged, SDL_VideoSurface); SDL_SetPaletteColors(SDL_VideoSurface->format->palette, SDL_VideoSurface->format->palette->colors, 0, SDL_VideoSurface->format->palette->ncolors); } /* Create a shadow surface if necessary */ if ((bpp != SDL_VideoSurface->format->BitsPerPixel) && !(flags & SDL_ANYFORMAT)) { SDL_ShadowSurface = SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0); if (!SDL_ShadowSurface) { return NULL; } SDL_ShadowSurface->flags |= surface_flags; /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */ if (SDL_ShadowSurface->format->palette) { SDL_ShadowSurface->flags |= SDL_HWPALETTE; if (SDL_VideoSurface->format->palette) { SDL_SetSurfacePalette(SDL_ShadowSurface, SDL_VideoSurface->format->palette); } else { SDL_DitherColors(SDL_ShadowSurface->format->palette->colors, SDL_ShadowSurface->format->BitsPerPixel); } SDL_AddPaletteWatch(SDL_ShadowSurface->format->palette, SDL_VideoPaletteChanged, SDL_ShadowSurface); } } SDL_PublicSurface = (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface); SDL_VideoFlags = flags; ClearVideoSurface(); SetupScreenSaver(flags); /* We're finally done! */ return SDL_PublicSurface; }