void MenuLoop(MenuSystem *menu) { assert(menu->numExitTypes > 0); for (;; SDL_Delay(10)) { // Input InputPoll(menu->joysticks, menu->keyboard); // Update if (menu->current->type == MENU_TYPE_KEYS && menu->current->u.normal.changeKeyMenu != NULL) { MenuProcessChangeKey(menu->current); } else { int cmd = GetMenuCmd(); menu->current = MenuProcessCmd(menu->current, cmd); } if (MenuHasExitType(menu, menu->current->type)) { break; } // Draw if (menu->bkg != NULL) { memcpy( GetDstScreen(), menu->bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig)); } ShowControls(); MenuDisplay(menu); CopyToScreen(); } }
void GrafxDrawBackground( GraphicsDevice *g, DrawBuffer *buffer, HSV tint, Vec2i pos, GrafxDrawExtra *extra) { Vec2i v; DrawBufferSetFromMap(buffer, &gMap, pos, X_TILES); DrawBufferDraw(buffer, Vec2iZero(), extra); for (v.y = 0; v.y < g->cachedConfig.Res.y; v.y++) { for (v.x = 0; v.x < g->cachedConfig.Res.x; v.x++) { DrawPointTint(g, v, tint); } } memcpy(g->bkg, g->buf, GraphicsGetMemSize(&g->cachedConfig)); memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig)); }
void GrafxRedrawBackground(GraphicsDevice *g, const struct vec2 pos) { memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig)); DrawBuffer buffer; DrawBufferInit(&buffer, svec2i(X_TILES, Y_TILES), g); const HSV tint = { rand() * 360.0 / RAND_MAX, rand() * 1.0 / RAND_MAX, 0.5 }; GrafxDrawBackground(g, &buffer, tint, pos, NULL); DrawBufferTerminate(&buffer); }
void DisplayTodaysHighScores(void *bkg) { int index = 0; while (index < MAX_ENTRY && todaysHigh[index].score > 0) { memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig)); index = DisplayPage("Today's highest score:", index, todaysHigh, gPlayer1Data.today, gOptions.twoPlayers ? gPlayer2Data. today : -1); GetKey(&gKeyboard); } }
void DisplayAllTimeHighScores(void *bkg) { int index = 0; while (index < MAX_ENTRY && allTimeHigh[index].score > 0) { memcpy( GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig)); index = DisplayPage("All time high scores:", index, allTimeHigh, gPlayer1Data.allTime, gOptions.twoPlayers ? gPlayer2Data. allTime : -1); GetKey(&gKeyboard); } }
int PlayerEquip(void *bkg) { int done1 = 0, done2; debug(D_NORMAL, "\n"); done2 = gOptions.twoPlayers ? 0 : 1; while (!done1 || !done2) { int cmd1 = 0; int cmd2 = 0; InputPoll(&gJoysticks, &gKeyboard); memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig)); GetPlayerCmd(&cmd1, &cmd2, 1); if (KeyIsPressed(&gKeyboard, keyEsc)) return 0; // hack to exit from menu if (gOptions.twoPlayers) { done1 = !WeaponSelection(CenterOfLeft(50), CHARACTER_PLAYER1, &gPlayer1Data, cmd1, done1); ShowSelection(CenterOfLeft(50), &gPlayer1Data,CHARACTER_PLAYER1); ShowPlayerControls(CenterOfLeft(100), &gConfig.Input.PlayerKeys[0]); done2 = !WeaponSelection(CenterOfRight(50), CHARACTER_PLAYER2, &gPlayer2Data, cmd2, done2); ShowSelection(CenterOfRight(50), &gPlayer2Data, CHARACTER_PLAYER2); ShowPlayerControls(CenterOfRight(100), &gConfig.Input.PlayerKeys[1]); } else { done1 = !WeaponSelection(CenterX(80), CHARACTER_PLAYER1, &gPlayer1Data, cmd1, done1); ShowSelection(CenterX(80), &gPlayer1Data, CHARACTER_PLAYER1); ShowPlayerControls(CenterX(100), &gConfig.Input.PlayerKeys[0]); } CopyToScreen(); SDL_Delay(10); } return 1; }
int PlayerSelection(int twoPlayers, void *bkg) { int mode1, mode2; mode1 = MODE_MAIN; mode2 = twoPlayers ? MODE_MAIN : MODE_DONE; SetPlayer(0, &gPlayer1Data); SetPlayer(1, &gPlayer2Data); KeyInit(&gKeyboard); while (mode1 != MODE_DONE || mode2 != MODE_DONE) { int cmd1 = 0; int cmd2 = 0; InputPoll(&gJoysticks, &gKeyboard); memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig)); GetPlayerCmd(&cmd1, &cmd2, 1); if (KeyIsPressed(&gKeyboard, keyEsc)) return 0; // hack to allow exit if (twoPlayers) { mode1 = MakeSelection(mode1, CenterOfLeft(50), CHARACTER_PLAYER1, &gPlayer1Data, cmd1); mode2 = MakeSelection(mode2, CenterOfRight(50), CHARACTER_PLAYER2, &gPlayer2Data, cmd2); } else { mode1 = MakeSelection(mode1, CenterX(50), CHARACTER_PLAYER1, &gPlayer1Data, cmd1); } CopyToScreen(); SDL_Delay(10); } return 1; }
void GraphicsBlitBkg(GraphicsDevice *device) { memcpy(device->buf, device->bkg, GraphicsGetMemSize(&device->cachedConfig)); }
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 *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; }
// 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); }