// Initialises the video subsystem. // To prevent needless screen flickering, config is compared with cache // to see if anything changed. If not, don't recreate the screen. void GraphicsInitialize(GraphicsDevice *g, const bool force) { #ifdef __GCWZERO__ int sdl_flags = SDL_HWSURFACE | SDL_TRIPLEBUF; #else int sdl_flags = SDL_SWSURFACE; #endif unsigned int w, h = 0; unsigned int rw, rh; if (g->IsInitialized && !g->cachedConfig.needRestart) { return; } if (!g->IsWindowInitialized) { /* only do this the first time */ char title[32]; debug(D_NORMAL, "setting caption and icon...\n"); sprintf(title, "C-Dogs SDL %s%s", g->cachedConfig.IsEditor ? "Editor " : "", CDOGS_SDL_VERSION); SDL_WM_SetCaption(title, NULL); char buf[CDOGS_PATH_MAX]; GetDataFilePath(buf, "cdogs_icon.bmp"); g->icon = SDL_LoadBMP(buf); SDL_WM_SetIcon(g->icon, NULL); AddSupportedGraphicsModes(g); } g->IsInitialized = false; sdl_flags |= g->cachedConfig.IsEditor ? SDL_RESIZABLE : 0; if (g->cachedConfig.Fullscreen) { sdl_flags |= SDL_FULLSCREEN; } rw = w = g->cachedConfig.Res.x; rh = h = g->cachedConfig.Res.y; if (g->cachedConfig.ScaleFactor > 1) { rw *= g->cachedConfig.ScaleFactor; rh *= g->cachedConfig.ScaleFactor; } if (!force && !g->cachedConfig.IsEditor) { g->modeIndex = FindValidMode(g, w, h, g->cachedConfig.ScaleFactor); if (g->modeIndex == -1) { g->modeIndex = 0; printf("!!! Invalid Video Mode %dx%d\n", w, h); return; } } printf("Graphics mode:\t%dx%d %dx (actual %dx%d)\n", w, h, g->cachedConfig.ScaleFactor, rw, rh); SDL_FreeSurface(g->screen); g->screen = SDL_SetVideoMode(rw, rh, 32, sdl_flags); if (g->screen == NULL) { printf("ERROR: InitVideo: %s\n", SDL_GetError()); return; } SDL_PixelFormat *f = g->screen->format; g->Amask = -1 & ~(f->Rmask | f->Gmask | f->Bmask); Uint32 aMask = g->Amask; g->Ashift = 0; while (aMask != 0xff) { g->Ashift += 8; aMask >>= 8; } CFREE(g->buf); CCALLOC(g->buf, GraphicsGetMemSize(&g->cachedConfig)); CFREE(g->bkg); CCALLOC(g->bkg, GraphicsGetMemSize(&g->cachedConfig)); debug(D_NORMAL, "Changed video mode...\n"); GraphicsSetBlitClip( g, 0, 0, g->cachedConfig.Res.x - 1, g->cachedConfig.Res.y - 1); debug(D_NORMAL, "Internal dimensions:\t%dx%d\n", g->cachedConfig.Res.x, g->cachedConfig.Res.y); g->IsInitialized = true; g->IsWindowInitialized = true; g->cachedConfig.Res.x = w; g->cachedConfig.Res.y = h; g->cachedConfig.needRestart = false; }
void GraphicsInitialize(GraphicsDevice *g) { if (g->IsInitialized && !g->cachedConfig.RestartFlags) { return; } if (!g->IsWindowInitialized) { char buf[CDOGS_PATH_MAX]; GetDataFilePath(buf, "cdogs_icon.bmp"); g->icon = IMG_Load(buf); AddSupportedGraphicsModes(g); g->IsWindowInitialized = true; } g->IsInitialized = false; const int w = g->cachedConfig.Res.x; const int h = g->cachedConfig.Res.y; const bool initRenderer = !!(g->cachedConfig.RestartFlags & RESTART_RESOLUTION); const bool initTextures = !!(g->cachedConfig.RestartFlags & (RESTART_RESOLUTION | RESTART_SCALE_MODE)); const bool initBrightness = !!(g->cachedConfig.RestartFlags & (RESTART_RESOLUTION | RESTART_SCALE_MODE | RESTART_BRIGHTNESS)); if (initRenderer) { Uint32 sdlFlags = SDL_WINDOW_RESIZABLE; if (g->cachedConfig.Fullscreen) { sdlFlags |= SDL_WINDOW_FULLSCREEN; } LOG(LM_GFX, LL_INFO, "graphics mode(%dx%d %dx)", w, h, g->cachedConfig.ScaleFactor); // Get the previous window's size and recreate it Vec2i windowSize = Vec2iNew( w * g->cachedConfig.ScaleFactor, h * g->cachedConfig.ScaleFactor); if (g->window) { SDL_GetWindowSize(g->window, &windowSize.x, &windowSize.y); } LOG(LM_GFX, LL_DEBUG, "destroying previous renderer"); SDL_DestroyTexture(g->screen); SDL_DestroyTexture(g->bkg); SDL_DestroyTexture(g->brightnessOverlay); SDL_DestroyRenderer(g->renderer); SDL_FreeFormat(g->Format); SDL_DestroyWindow(g->window); LOG(LM_GFX, LL_DEBUG, "creating window %dx%d flags(%X)", windowSize.x, windowSize.y, sdlFlags); if (SDL_CreateWindowAndRenderer( windowSize.x, windowSize.y, sdlFlags, &g->window, &g->renderer) == -1 || g->window == NULL || g->renderer == NULL) { LOG(LM_GFX, LL_ERROR, "cannot create window or renderer: %s", SDL_GetError()); return; } char title[32]; sprintf(title, "C-Dogs SDL %s%s", g->cachedConfig.IsEditor ? "Editor " : "", CDOGS_SDL_VERSION); LOG(LM_GFX, LL_DEBUG, "setting title(%s) and icon", title); SDL_SetWindowTitle(g->window, title); SDL_SetWindowIcon(g->window, g->icon); g->Format = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888); if (SDL_RenderSetLogicalSize(g->renderer, w, h) != 0) { LOG(LM_GFX, LL_ERROR, "cannot set renderer logical size: %s", SDL_GetError()); return; } GraphicsSetBlitClip( g, 0, 0, g->cachedConfig.Res.x - 1, g->cachedConfig.Res.y - 1); } if (initTextures) { if (!initRenderer) { SDL_DestroyTexture(g->screen); SDL_DestroyTexture(g->bkg); SDL_DestroyTexture(g->brightnessOverlay); } // Set render scale mode const char *renderScaleQuality = "nearest"; switch ((ScaleMode)ConfigGetEnum(&gConfig, "Graphics.ScaleMode")) { case SCALE_MODE_NN: renderScaleQuality = "nearest"; break; case SCALE_MODE_BILINEAR: renderScaleQuality = "linear"; break; default: CASSERT(false, "unknown scale mode"); break; } LOG(LM_GFX, LL_DEBUG, "setting scale quality %s", renderScaleQuality); if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, renderScaleQuality)) { LOG(LM_GFX, LL_WARN, "cannot set render quality hint: %s", SDL_GetError()); } g->screen = CreateTexture( g->renderer, SDL_TEXTUREACCESS_STREAMING, Vec2iNew(w, h), SDL_BLENDMODE_BLEND, 255); if (g->screen == NULL) { return; } CFREE(g->buf); CCALLOC(g->buf, GraphicsGetMemSize(&g->cachedConfig)); g->bkg = CreateTexture( g->renderer, SDL_TEXTUREACCESS_STATIC, Vec2iNew(w, h), SDL_BLENDMODE_NONE, 255); if (g->bkg == NULL) { return; } } if (initBrightness) { if (!initRenderer && !initTextures) { SDL_DestroyTexture(g->brightnessOverlay); } const int brightness = ConfigGetInt(&gConfig, "Graphics.Brightness"); // Alpha is approximately 50% max const Uint8 alpha = (Uint8)(brightness > 0 ? brightness : -brightness) * 13; g->brightnessOverlay = CreateTexture( g->renderer, SDL_TEXTUREACCESS_STATIC, Vec2iNew(w, h), SDL_BLENDMODE_BLEND, alpha); if (g->brightnessOverlay == NULL) { return; } const color_t overlayColour = brightness > 0 ? colorWhite : colorBlack; DrawRectangle(g, Vec2iZero(), g->cachedConfig.Res, overlayColour, 0); SDL_UpdateTexture( g->brightnessOverlay, NULL, g->buf, g->cachedConfig.Res.x * sizeof(Uint32)); memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig)); g->cachedConfig.Brightness = brightness; } g->IsInitialized = true; g->cachedConfig.Res.x = w; g->cachedConfig.Res.y = h; g->cachedConfig.RestartFlags = 0; }
// Initialises the video subsystem. // To prevent needless screen flickering, config is compared with cache // to see if anything changed. If not, don't recreate the screen. void GraphicsInitialize( GraphicsDevice *device, GraphicsConfig *config, TPalette palette, int force) { int sdl_flags = 0; unsigned int w, h = 0; unsigned int rw, rh; if (!IsRestartRequiredForConfig(device, config)) { return; } if (!device->IsWindowInitialized) { /* only do this the first time */ char title[32]; debug(D_NORMAL, "setting caption and icon...\n"); sprintf(title, "C-Dogs SDL %s%s", config->IsEditor ? "Editor " : "", CDOGS_SDL_VERSION); SDL_WM_SetCaption(title, NULL); device->icon = SDL_LoadBMP(GetDataFilePath("cdogs_icon.bmp")); SDL_WM_SetIcon(device->icon, NULL); AddSupportedGraphicsModes(device); } device->IsInitialized = 0; sdl_flags |= SDL_SWSURFACE; sdl_flags |= config->IsEditor ? SDL_RESIZABLE : 0; if (config->Fullscreen) { sdl_flags |= SDL_FULLSCREEN; } rw = w = config->Res.x; rh = h = config->Res.y; if (config->ScaleFactor > 1) { rw *= config->ScaleFactor; rh *= config->ScaleFactor; } if (!force && !config->IsEditor) { device->modeIndex = FindValidMode(device, w, h, config->ScaleFactor); if (device->modeIndex == -1) { device->modeIndex = 0; printf("!!! Invalid Video Mode %dx%d\n", w, h); return; } } else { printf("\n"); printf(" BIG FAT WARNING: If this blows up in your face,\n"); printf(" and mutilates your cat, please don't cry.\n"); printf("\n"); } printf("Graphics mode:\t%dx%d %dx (actual %dx%d)\n", w, h, config->ScaleFactor, rw, rh); SDL_FreeSurface(device->screen); device->screen = SDL_SetVideoMode(rw, rh, 32, sdl_flags); if (device->screen == NULL) { printf("ERROR: InitVideo: %s\n", SDL_GetError()); return; } SDL_PixelFormat *f = device->screen->format; device->Amask = -1 & ~(f->Rmask | f->Gmask | f->Bmask); Uint32 aMask = device->Amask; device->Ashift = 0; while (aMask != 0xff) { device->Ashift += 8; aMask >>= 8; } CFREE(device->buf); CCALLOC(device->buf, GraphicsGetMemSize(config)); CFREE(device->bkg); CCALLOC(device->bkg, GraphicsGetMemSize(config)); debug(D_NORMAL, "Changed video mode...\n"); GraphicsSetBlitClip( device, 0, 0, config->Res.x - 1,config->Res.y - 1); debug(D_NORMAL, "Internal dimensions:\t%dx%d\n", config->Res.x, config->Res.y); device->IsInitialized = 1; device->IsWindowInitialized = 1; device->cachedConfig = *config; device->cachedConfig.Res.x = w; device->cachedConfig.Res.y = h; CDogsSetPalette(palette); }