void display_canvas_text(Menu* main_menu) { int win_width, win_height; int canvas_text_x, canvas_text_y, canvas_text_w, canvas_text_h; SDL_Texture* image = load_image("display/images/program_a_work_of_art.bmp", &main_menu->window); SDL_GetWindowSize(main_menu->window.win, &win_width, &win_height); canvas_text_x = main_menu->canvas_button.rect.w + win_width / LEFT_MARGIN; canvas_text_y = main_menu->canvas_button.rect.y; canvas_text_w = MENU_POPUP_WIDTH; canvas_text_h = MENU_POPUP_HEIGHT; make_rect(&main_menu->window, &main_menu->canvas_text, canvas_text_x, canvas_text_y, canvas_text_w, canvas_text_h, 230, 230, 230); SDL_RenderCopy(main_menu->window.renderer, image, NULL, &main_menu->canvas_text.rect); SDL_DestroyTexture(image); }
static inline void _gut_updatePhysic(SDL_Window *window) { int w, h; w = gut.core->window.physic.w; h = gut.core->window.physic.h; SDL_GetWindowSize( window, &gut.core->window.physic.w, &gut.core->window.physic.h ); if (gut.reshape && (w != gut.core->window.physic.w || h != gut.core->window.physic.h)) { gut.reshape(gut.core->window.physic.w, gut.core->window.physic.h); } if (!(gut.window.flags & GUT_NO_AUTO_VIEWPORT)) { glViewport( 0, 0, gut.core->window.physic.w, gut.core->window.physic.h ); } }
void SDLAppDisplay::resize(int width, int height) { int resized_width, resized_height; #if SDL_VERSION_ATLEAST(1,3,0) SDL_GetWindowSize(sdl_window, &resized_width, &resized_height); #else setVideoMode(width, height, fullscreen); const SDL_VideoInfo* display_info = SDL_GetVideoInfo(); resized_width = display_info->current_w; resized_height = display_info->current_h; #endif //set viewport to match what we ended up on glViewport(0, 0, resized_width, resized_height); this->width = resized_width; this->height = resized_height; }
int SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect) { CHECK_RENDERER_MAGIC(renderer, -1); if (rect) { renderer->viewport = *rect; } else { renderer->viewport.x = 0; renderer->viewport.y = 0; if (renderer->window) { SDL_GetWindowSize(renderer->window, &renderer->viewport.w, &renderer->viewport.h); } else { /* This will be filled in by UpdateViewport() */ renderer->viewport.w = 0; renderer->viewport.h = 0; } } return renderer->UpdateViewport(renderer); }
void display_expert_text(Menu* challenges) { int win_width, win_height; int expert_text_x, expert_text_y, expert_text_w, expert_text_h; SDL_Texture* image = load_image("display/images/expert_text.bmp", &challenges->window); SDL_GetWindowSize(challenges->window.win, &win_width, &win_height); expert_text_x = challenges->expert.rect.w + win_width / LEFT_MARGIN; expert_text_y = challenges->expert.rect.y; expert_text_w = MENU_POPUP_WIDTH; expert_text_h = MENU_POPUP_HEIGHT; make_rect(&challenges->window, &challenges->expert_text, expert_text_x, expert_text_y, expert_text_w, expert_text_h, 230, 230, 230); SDL_RenderCopy(challenges->window.renderer, image, NULL, &challenges->expert_text.rect); SDL_DestroyTexture(image); }
void setNavigationOffset(GraphicsData *graphicsData, int x, int y){ int sx,sy; SDL_GetWindowSize(graphicsData->window,&sx,&sy); if(x + sx > X_SIZE_OF_WORLD){ x = X_SIZE_OF_WORLD - sx; } else if(x < 0){ x = 0; } if(y + sy > Y_SIZE_OF_WORLD){ y = Y_SIZE_OF_WORLD - sy; } else if(y < 0){ y = 0; } x = -x; y = -y; graphicsData->navigationOffset.x = x; graphicsData->navigationOffset.y = y; }
void CSprite::SetFullScreen(const bool bFull) { //Invert bool this->m_bFullScreen = bFull; //Fullscreen if (this->m_bFullScreen) { //Resize (First) SDL_GetWindowSize(this->m_pWin, &this->m_tScreenSize.w, &this->m_tScreenSize.h); SDL_SetWindowSize(this->m_pWin, SCREEN_WIDTH, SCREEN_HEIGHT); //Goto Windowed/FullScreen SDL_SetWindowFullscreen(this->m_pWin, this->m_bFullScreen); } else// Windowed { //Goto Windowed/FullScreen SDL_SetWindowFullscreen(this->m_pWin, this->m_bFullScreen); //Resize SDL_SetWindowSize(this->m_pWin, this->m_tScreenSize.w, this->m_tScreenSize.h); } }
void ImGui_ImplSDL2_NewFrame(SDL_Window* window) { ImGuiIO& io = ImGui::GetIO(); IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame() // Setup display size (every frame to accommodate for window resizing) int w, h; int display_w, display_h; SDL_GetWindowSize(window, &w, &h); SDL_GL_GetDrawableSize(window, &display_w, &display_h); io.DisplaySize = ImVec2((float)w, (float)h); io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0); // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) static Uint64 frequency = SDL_GetPerformanceFrequency(); Uint64 current_time = SDL_GetPerformanceCounter(); io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f); g_Time = current_time; ImGui_ImplSDL2_UpdateMousePosAndButtons(); ImGui_ImplSDL2_UpdateMouseCursor(); }
/// \brief Wrap the mouse to the viewport void InputWrapper::_wrapMousePointer(const SDL_MouseMotionEvent& evt) { //don't wrap if we don't want relative movements, support relative //movements natively, or aren't grabbing anyways if(!mMouseRelative || !mWrapPointer || !mGrabPointer) return; int width = 0; int height = 0; SDL_GetWindowSize(mSDLWindow, &width, &height); const int FUDGE_FACTOR_X = width/4; const int FUDGE_FACTOR_Y = height/4; //warp the mouse if it's about to go outside the window if(evt.x - FUDGE_FACTOR_X < 0 || evt.x + FUDGE_FACTOR_X > width || evt.y - FUDGE_FACTOR_Y < 0 || evt.y + FUDGE_FACTOR_Y > height) { warpMouse(width / 2, height / 2); } }
void SDLHardwareRenderDevice::windowResize() { int w,h; SDL_GetWindowSize(window, &w, &h); SCREEN_W = w; SCREEN_H = h; float scale = (float)VIEW_H / (float)SCREEN_H; VIEW_W = (int)((float)SCREEN_W * scale); // letterbox if too tall if (VIEW_W < MIN_SCREEN_W) { VIEW_W = MIN_SCREEN_W; } VIEW_W_HALF = VIEW_W/2; SDL_RenderSetLogicalSize(renderer, VIEW_W, VIEW_H); if (texture) SDL_DestroyTexture(texture); texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, VIEW_W, VIEW_H); SDL_SetRenderTarget(renderer, texture); }
/** * Resize the screen filling the greatest area possible, but ignoring the *original aspect ratio. This shouldn't be used at all! Ò.Ó * @param update_window Whether the windows dimensions should be updated */ void GFraMe_screen_set_maximize_double(int update_window) { int w = 0, h = 0; // Get the actual window's dimension if (update_window) { SDL_GetWindowSize(GFraMe_window, &w, &h); GFraMe_window_w = w; GFraMe_window_h = h; } else { w = GFraMe_window_w; h = GFraMe_window_h; } // Letterbox the game GFraMe_buffer_x = 0; GFraMe_buffer_y = 0; GFraMe_buffer_w = w; GFraMe_buffer_h = h; GFraMe_screen_ratio_h = (double)w / GFraMe_screen_w; GFraMe_screen_ratio_v = (double)h / GFraMe_screen_h; GFraMe_screen_log_dimensions(0); GFraMe_screen_cache_dimensions(); }
int sdl_env(int **map) { t_env *e; if (map == NULL) return (0); e = init_env(map); menu_loop(e); while (1) { while (SDL_PollEvent(&e->event)) { SDL_GetWindowSize(e->win_sdl, &e->w, &e->h); handle_event(e); draw(e); } } if (ft_free(e)) return (1); else return (0); }
static int D3D_ActivateRenderer(SDL_Renderer * renderer) { D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata; HRESULT result; if (data->updateSize) { SDL_Window *window = renderer->window; int w, h; SDL_GetWindowSize(window, &w, &h); data->pparams.BackBufferWidth = w; data->pparams.BackBufferHeight = h; if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) { data->pparams.BackBufferFormat = PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window)); } else { data->pparams.BackBufferFormat = D3DFMT_UNKNOWN; } /* XXX if (D3D_Reset(renderer) < 0) { return -1; }*/ data->updateSize = SDL_FALSE; } if (data->beginScene) { result = IDirect3DDevice9_BeginScene(data->device); if (result == D3DERR_DEVICELOST) { /* XXX if (D3D_Reset(renderer) < 0) { return -1./include/GL/glext.h; }*/ result = IDirect3DDevice9_BeginScene(data->device); } if (FAILED(result)) { return SDL_SetError("SDL_Shader: D3D BeginScene() failed, errno %d\n", result); } data->beginScene = SDL_FALSE; } return 0; }
void MoveSprites(SDL_Window * window, SDL_Texture * sprite) { int i, n; int window_w, window_h; int sprite_w, sprite_h; SDL_Rect *position, *velocity; SDL_SelectRenderer(window); /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h); /* Move the sprite, bounce at the wall, and draw */ n = 0; SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); for (i = 0; i < NUM_SPRITES; ++i) { position = &positions[i]; velocity = &velocities[i]; position->x += velocity->x; if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { velocity->x = -velocity->x; position->x += velocity->x; } position->y += velocity->y; if ((position->y < 0) || (position->y >= (window_h - sprite_h))) { velocity->y = -velocity->y; position->y += velocity->y; } /* Blit the sprite onto the screen */ SDL_RenderCopy(sprite, NULL, position); } /* Update the screen! */ SDL_RenderPresent(); }
void SDLHardwareRenderDevice::windowResize() { int w,h; SDL_GetWindowSize(window, &w, &h); SCREEN_W = static_cast<unsigned short>(w); SCREEN_H = static_cast<unsigned short>(h); float scale = static_cast<float>(VIEW_H) / static_cast<float>(SCREEN_H); VIEW_W = static_cast<unsigned short>(static_cast<float>(SCREEN_W) * scale); // letterbox if too tall if (VIEW_W < MIN_SCREEN_W) { VIEW_W = MIN_SCREEN_W; } VIEW_W_HALF = VIEW_W/2; SDL_RenderSetLogicalSize(renderer, VIEW_W, VIEW_H); if (texture) SDL_DestroyTexture(texture); texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, VIEW_W, VIEW_H); SDL_SetRenderTarget(renderer, texture); }
//----------------------------------------------------------------------------------- void SdlInputHandler::wrapMousePointer( const SDL_MouseMotionEvent& evt ) { //Don't wrap if we don't want relative movements, support //relative movements natively, or aren't grabbing anyways if( mIsMouseRelative || !mWrapPointerManually || !mGrabPointer ) return; int width = 0; int height = 0; SDL_GetWindowSize( mSdlWindow, &width, &height ); const int FUDGE_FACTOR_X = width; const int FUDGE_FACTOR_Y = height; //Warp the mouse if it's about to go outside the window if( evt.x - FUDGE_FACTOR_X < 0 || evt.x + FUDGE_FACTOR_X > width || evt.y - FUDGE_FACTOR_Y < 0 || evt.y + FUDGE_FACTOR_Y > height ) { warpMouse( width / 2, height / 2 ); } }
void Snake::Game::handleEvents() { SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: is_running = false; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_UP: player->setDirection(up); break; case SDLK_DOWN: player->setDirection(down); break; case SDLK_LEFT: player->setDirection(left); break; case SDLK_RIGHT: player->setDirection(right); break; case SDLK_ESCAPE: is_running = false; break; } break; case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_SHOWN: case SDL_WINDOWEVENT_SIZE_CHANGED: SDL_GetWindowSize(window, &screen_w, &screen_h); break; } break; default: break; } } }
void Renderer::toggleFullScreen(SDL_Window* mainWindow) { int w, h; if(!isFullScreen) { SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN_DESKTOP); updateProjection( glm::perspective(45.0f, 16.0f / 9.0f, 0.1f, 500.f) ); SDL_GetWindowSize(mainWindow, &w, &h); printf("Going fullscreen: %ix%i\n", w, h); glViewport(0,0,w,h); isFullScreen = true; } else { SDL_SetWindowFullscreen(mainWindow, 0); SDL_SetWindowSize( mainWindow, 800, 600); updateProjection( glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 500.f)) ; printf("Leaving fullscreen!\n"); glViewport(0,0,800,600); isFullScreen = false; } }
// New method to initialize with ability to load PNG files. int CSDLManagerLite::initializeSDL(int width, int height, const std::string & title) { if(SDL_Init(SDL_INIT_VIDEO) < 0) return -1; m_pWindow = SDL_CreateWindow( title.c_str(), // window title SDL_WINDOWPOS_UNDEFINED, // initial x position SDL_WINDOWPOS_UNDEFINED, // initial y position width, // width, in pixels height, // height, in pixels SDL_WINDOW_OPENGL //SDL_WINDOW_BORDERLESS ); // if(m_pWindow == NULL) { // In the event that the window could not be made... std::cerr << "Could not create window: " << SDL_GetError() << std::endl; return -2; } //Initialize PNG loading int imgFlags = IMG_INIT_PNG; if(!(IMG_Init(imgFlags) & imgFlags)) { std::cerr << "SDL_image could not initialize: ", IMG_GetError(); return -4; } m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_PRESENTVSYNC); if(m_pWindow == NULL) { std::cerr << "Could not create renderer: " << SDL_GetError() << std::endl; return -2; } SDL_GetWindowSize(m_pWindow, &m_w, &m_h); return 0; }
// origin for our coordinates is the bottom left corner void SpringApp::GetDisplayGeometry() { #ifdef HEADLESS globalRendering->screenSizeX = 8; globalRendering->screenSizeY = 8; globalRendering->winSizeX = 8; globalRendering->winSizeY = 8; globalRendering->winPosX = 0; globalRendering->winPosY = 0; globalRendering->UpdateViewPortGeometry(); return; #else SDL_Rect screenSize; SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(window), &screenSize); globalRendering->screenSizeX = screenSize.w; globalRendering->screenSizeY = screenSize.h; SDL_GetWindowSize(window, &globalRendering->winSizeX, &globalRendering->winSizeY); SDL_GetWindowPosition(window, &globalRendering->winPosX, &globalRendering->winPosY); globalRendering->UpdateViewPortGeometry(); //XXX SDL2 is crap ... // Reading window state fails if it is changed via the window manager, like clicking on the titlebar (2013) // https://bugzilla.libsdl.org/show_bug.cgi?id=1508 & https://bugzilla.libsdl.org/show_bug.cgi?id=2282 // happens on linux too! const int state = WindowManagerHelper::GetWindowState(window); globalRendering->winState = CGlobalRendering::WINSTATE_DEFAULT; if (state & SDL_WINDOW_MAXIMIZED) { globalRendering->winState = CGlobalRendering::WINSTATE_MAXIMIZED; } else if (state & SDL_WINDOW_MINIMIZED) { globalRendering->winState = CGlobalRendering::WINSTATE_MINIMIZED; } #endif }
/****** Initialize LCD with SDL ******/ bool AGB_LCD::init() { //Initialize with SDL rendering software or hardware if(config::sdl_render) { //Initialize all of SDL if(SDL_Init(SDL_INIT_VIDEO) == -1) { std::cout<<"LCD::Error - Could not initialize SDL video\n"; return false; } //Setup OpenGL rendering if(config::use_opengl) {opengl_init(); } //Set up software rendering else { window = SDL_CreateWindow("GBE+", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, config::sys_width, config::sys_height, config::flags); SDL_GetWindowSize(window, &config::win_width, &config::win_height); final_screen = SDL_GetWindowSurface(window); original_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, config::sys_width, config::sys_height, 32, 0, 0, 0, 0); config::scaling_factor = 1; } if(final_screen == NULL) { return false; } } //Initialize with only a buffer for OpenGL (for external rendering) else if((!config::sdl_render) && (config::use_opengl)) { final_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, config::sys_width, config::sys_height, 32, 0, 0, 0, 0); } std::cout<<"LCD::Initialized\n"; return true; }
void graphicssdlrender_startFrame(void) { #ifdef USE_SDL_GRAPHICS_OPENGL_EFFECTS if (maincontext) { GLenum err; if ((err = glGetError()) != GL_NO_ERROR) { printwarning("graphicsrender_startFrame: earlier error " "around: %s", glGetErrorString(err)); } renderts = time_getMilliseconds(); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); if ((err = glGetError()) != GL_NO_ERROR) { printwarning("graphicsrender_startFrame: " "glClear error: %s", glGetErrorString(err)); } int actualwidth, actualheight; SDL_GetWindowSize(mainwindow, &actualwidth, &actualheight); renderwindow_width = actualwidth; renderwindow_height = actualheight; glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, actualwidth, actualheight, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return; } #endif SDL_SetRenderDrawColor(mainrenderer, 0, 0, 0, 1); SDL_RenderClear(mainrenderer); }
void RenderManagerSDL::refresh() { SDL_SetRenderTarget(mRenderer, NULL); // We have a resizeable window // Resize renderer if needed // TODO: We should catch the resize event SDL_Rect renderRect; int windowX; int windowY; SDL_RenderGetViewport(mRenderer, &renderRect); SDL_GetWindowSize(mWindow, &windowX, &windowY); if (renderRect.w != windowX || renderRect.h != windowY) { renderRect.w = windowX; renderRect.h = windowY; SDL_RenderSetViewport(mRenderer, &renderRect); } SDL_RenderCopy(mRenderer, mRenderTarget, NULL, NULL); SDL_RenderPresent(mRenderer); SDL_SetRenderTarget(mRenderer, mRenderTarget); }
static void process_events() { SDL_Event event; SDL_LockMutex(event_lock); while (SDL_PollEvent(&event)) { if (event.type == SDL_WINDOWEVENT) { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { SDL_GetWindowSize(screen,&win_w,&win_h); SDL_Rect newsize = boxify(); SDL_RenderSetViewport(renderer, &newsize); } } else if (event.type == SDL_QUIT) { end_emulation(); pending_sdl_thread_exit = true; #ifdef RUN_TESTS end_testing = true; #endif } } SDL_UnlockMutex(event_lock); }
/** * @brief Handles SDL events * @param e The SDL_Event. * @param delta Change in time between last update. */ void GameWindow::event ( SDL_Event e , const double& delta ) { switch ( e.type ) { case ( SDL_QUIT ): this->quit = true; break; case ( SDL_KEYDOWN ): if ( this->content->Input()->checkInput( "core", "qquit" ) ) { this->quit = true; } if ( this->content->Input()->checkInput( "core", "screenshot" ) ) { this->screenshot(); } break; case ( SDL_WINDOWEVENT ): int w = 0, h = 0; SDL_GetWindowSize( this->window, &w, &h ); this->camera.setBounds( w, h ); } }
static void VL_SDL2_ResizeWindow() { int realWinH, realWinW, curW, curH; SDL_GetWindowSize(vl_sdl2_window, &realWinW, &realWinH); VL_CalculateRenderRegions(realWinW, realWinH); if (vl_sdl2_scaledTarget) { // Check if the scaled render target is the correct size. SDL_QueryTexture(vl_sdl2_scaledTarget, 0, 0, &curW, &curH); if (curW == vl_integerWidth && curH == vl_integerHeight) { return; } // We need to recreate the scaled render target. Do so. SDL_DestroyTexture(vl_sdl2_scaledTarget); vl_sdl2_scaledTarget = 0; } SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); vl_sdl2_scaledTarget = SDL_CreateTexture(vl_sdl2_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, vl_integerWidth, vl_integerHeight); }
int draw(void) { int status = -1; int w, h; int i; SDL_GetWindowSize(window, &w, &h); SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_RENDERCLEAR(renderer); SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF); SDL_RENDERFILLRECT(renderer, &(SDL_Rect){w/4, h/4, w/2, h/2}); SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF); SDL_RENDERDRAWRECT(renderer, &(SDL_Rect){w/6, h/6, w*2/3, h*2/3}); SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF); SDL_RENDERDRAWLINE(renderer, 0, a%h, w, h-a%h); SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF); for (i=0; i<h; i+=4) { SDL_RENDERDRAWPOINT(renderer, a%w, i); } SDL_RenderPresent(renderer); status = 0; error: return status; }
void CWinsys::SetupVideoMode (const TScreenRes& resolution_) { Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP; window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, resolution.width, resolution.height, window_flags); if (NULL == window) { Message ("couldn't initialize video", SDL_GetError()); Message ("set to 800 x 600"); window = SDL_CreateWindow (WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, window_flags); param.res_type = 1; SaveConfigFile (); } int w, h; SDL_GetWindowSize(window, &w, &h); if (resolution.width == 0 && resolution.height == 0) { resolution.width = w; resolution.height = h; auto_resolution = resolution; } scale = CalcScreenScale (); if (param.use_quad_scale) scale = sqrt (scale); }
/** * Resize the game while keeping the original aspect ratio * @param max_zoom Maximum zoom value or a value equal to or less than 0 *for any zoom * @param update_window Whether the windows dimensions should be updated * @return The new zoom level or 0 on failure */ double GFraMe_screen_set_keep_ratio(int max_zoom, int update_window) { int w = 0, h = 0; double zoom, hratio, vratio; // Get the actual window's dimension if (update_window) { SDL_GetWindowSize(GFraMe_window, &w, &h); GFraMe_window_w = w; GFraMe_window_h = h; } else { w = GFraMe_window_w; h = GFraMe_window_h; } // Check each possible ratio hratio = (double)w / (double)GFraMe_screen_w; vratio = (double)h / (double)GFraMe_screen_h; if (vratio * GFraMe_screen_w <= w) zoom = vratio; else if (hratio * GFraMe_screen_h <= h) zoom = hratio; else zoom = 0; if (max_zoom > 0 && zoom > max_zoom) zoom = max_zoom; GFraMe_assertRet(zoom, "Invalid aspect ratio", _ret); // Letterbox the game GFraMe_buffer_x = (w - GFraMe_screen_w * zoom) / 2; GFraMe_buffer_y = (h - GFraMe_screen_h * zoom) / 2; GFraMe_buffer_w = GFraMe_screen_w * zoom; GFraMe_buffer_h = GFraMe_screen_h * zoom; GFraMe_screen_ratio_h = zoom; GFraMe_screen_ratio_v= zoom; GFraMe_screen_log_dimensions(zoom); GFraMe_screen_cache_dimensions(); _ret: return zoom; }
static int SDL_RendererEventWatch(void *userdata, SDL_Event *event) { SDL_Renderer *renderer = (SDL_Renderer *)userdata; if (event->type == SDL_WINDOWEVENT) { SDL_Window *window = SDL_GetWindowFromID(event->window.windowID); if (window == renderer->window) { if (renderer->WindowEvent) { renderer->WindowEvent(renderer, &event->window); } if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { /* Try to keep the previous viewport centered */ int w, h; SDL_Rect viewport; SDL_GetWindowSize(window, &w, &h); if (renderer->target) { renderer->viewport_backup.x = (w - renderer->viewport_backup.w) / 2; renderer->viewport_backup.y = (h - renderer->viewport_backup.h) / 2; } else { viewport.x = (w - renderer->viewport.w) / 2; viewport.y = (h - renderer->viewport.h) / 2; viewport.w = renderer->viewport.w; viewport.h = renderer->viewport.h; SDL_RenderSetViewport(renderer, &viewport); } } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) { renderer->minimized = SDL_TRUE; } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) { renderer->minimized = SDL_FALSE; } } } return 0; }