Neblinus

WindowSetup

Apr 25th, 2026 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.86 KB | Source Code | 0 0
  1. -- Set the game window resolutions and setup them
  2.  
  3. -- For easier OOP structuring
  4. local class = require("Libraries.middleclass")
  5. -- For logging handling
  6. local logger = require("Libraries.Logilog")
  7. -- For session handling
  8. local sessionHandler = require("Source.Session.SessionHandler")
  9.  
  10. -- Defines a class for handling the window
  11. local WindowHandler = class("WindowHandler")
  12.  
  13. ---@type number : define the minimum supported resolution
  14. WindowHandler.static.MIN_WIDTH = 640
  15. WindowHandler.static.MIN_HEIGHT = 480
  16.  
  17. -- Define supported common resolutions with strings as keys for faster lookup
  18. function WindowHandler:presetDimensions() ---@return table
  19.     local presetDimensions =
  20.     {
  21.       [tostring(WindowHandler.MIN_WIDTH) .. "x" .. tostring(WindowHandler.MIN_HEIGHT)] = true,
  22.       ["960x540"] = true, ["960x720"] = true, ["1024x768"] = true, ["1280x800"] = true,
  23.       ["1280x960"] = true, ["1366x768"] = true, ["1440x900"] = true, ["1440x1080"] = true,
  24.       ["1600x900"] = true, ["1600x1200"] = true, ["1680x1050"] = true, ["1920x1080"] = true,
  25.       ["1920x1200"] = true, ["2560x1080"] = true, ["2560x1440"] = true, ["2560x1600"] = true,
  26.       ["2880x1800"] = true, ["3440x1440"] = true, ["3840x1080"] = true, ["3840x1600"] = true,
  27.       ["3840x2160"] = true, ["5120x1440"] = true, ["5120x2160"] = true, ["7680x2160"] = true,
  28.       ["10240x4320"] = true
  29.   }
  30.   return presetDimensions
  31. end
  32.  
  33. -- Get and store essential information about the screen size
  34. function WindowHandler:initialize()
  35.     -- Ensure game window creation is possible
  36.     if not love.window then logger:logMessage("FATAL", "Couldn't create game window.")
  37.     sessionHandler:safeSessionExit("Bye bye :[") end
  38.     -- Get the screen dimensions, and when unsupported, abort the game
  39.     self.screenWidth, self.screenHeight = self:getScreenSize() ---@type number
  40.     self:ensureSupportedScreen()
  41.     -- Allows the window to be fullscreen when supported, or a smaller-than-full window when not
  42.     self.fullscreenCompatible = self:isDefinedResolution()
  43. end
  44.  
  45. -- Check whether the game window creation is supported by this screen or device
  46. function WindowHandler:ensureSupportedScreen()
  47.   -- When the size is smaller than the minimum resolution, it is invalid
  48.     if self.screenWidth < WindowHandler.MIN_WIDTH or self.screenHeight < WindowHandler.MIN_HEIGHT
  49.     then
  50.         logger:logMessage("FATAL", "Your screen resolution is smaller than 640 x 480, thus,\z
  51.         unsupported.")
  52.         sessionHandler:safeSessionExit("Bye bye :[")
  53.     end
  54. end
  55.  
  56. -- Get the screen size or an error code when smaller than the minimum resolution
  57. function WindowHandler:getScreenSize() ---@return number
  58.     -- Store the original size for restoring and get the screen size through setMode(0, 0)
  59.     local originalWidth, originalHeight = love.graphics.getDimensions() ---@type number
  60.     love.window.setMode(0, 0, {fullscreen=false})
  61.     -- Store the screen size and restore the original size
  62.     local monitorWidth, monitorHeight = love.graphics.getDimensions() ---@type number
  63.     love.window.setMode(originalWidth, originalHeight, {fullscreen=false})
  64.     return monitorWidth, monitorHeight
  65. end
  66.  
  67. -- Set the window mode and configurations
  68. function WindowHandler:configure(inputTitle)
  69.   -- Window and graphics setup
  70.     love.window.setTitle(inputTitle)
  71.     love.graphics.setDefaultFilter("nearest", "nearest")
  72.     -- If fullscreen compatible, make the window fullscreen; rendering is always pixel perfect
  73.     local windowAdjustments = {resizable=false, fullscreen=false, vsync=0} ---@type table
  74.     if self.fullscreenCompatible then windowAdjustments["fullscreen"] = true end
  75.     -- If not, set to a smaller-than-full window
  76.     local targetWidth, targetHeight = self:getOptimalWindow() ---@type number
  77.     love.window.setMode(targetWidth, targetHeight, windowAdjustments)
  78. end
  79.  
  80. -- Checks whether the screen size is one of the defined resolutions
  81. function WindowHandler:isDefinedResolution() ---@return boolean
  82.   ---@type string : screen size following the resolutions table key-naming pattern
  83.   local screenSize = tostring(self.screenWidth) .. "x" .. tostring(self.screenHeight)
  84.   if not self:presetDimensions()[screenSize] then return false else return true end
  85. end
  86.  
  87. -- Select and returns a given resolution that fits better the user screen
  88. function WindowHandler:getOptimalWindow() ---@return number
  89.     ---@type number : always start the offset against the minimum valid dimensions
  90.     local widthOffset = self.screenWidth - WindowHandler.MIN_WIDTH
  91.     local heightOffset = self.screenHeight - WindowHandler.MIN_HEIGHT
  92.     -- Iterate through the adequate aspect ratio resolutions to get the optimal one
  93.     for dimensions, garbage in pairs(self:presetDimensions()) do
  94.         local separatorIndex = string.find(dimensions, "x")
  95.         local newWidthOffset = self.screenWidth - tonumber(dimensions:sub(1, separatorIndex - 1))
  96.         local newHeightOffset = self.screenHeight - tonumber(dimensions:sub(separatorIndex + 1))
  97.         -- Avoid invalid dimensions for this screen size, getting the greatest smaller resolution
  98.         if self:isValidDimension(widthOffset, heightOffset, newWidthOffset, newHeightOffset) then
  99.           widthOffset, heightOffset = newWidthOffset, newHeightOffset
  100.         end
  101.     end
  102.     -- Subtracting the offsets from the screen dimensions results on the desired window size
  103.     return self.screenWidth - widthOffset, self.screenHeight - heightOffset
  104. end
  105.  
  106. -- Ensure the current in-analysis resolution is the nearest-but-not-equal to screen ones
  107. function WindowHandler:isValidDimension(baseWidDiff, baseHeiDiff, newWidDiff, newHeiDiff)
  108.     ---@type boolean : offset should always be smaller than the previous to be considered
  109.     local hasSmallerOffset = (newWidDiff + newHeiDiff) < (baseWidDiff + baseHeiDiff)
  110.     ---@type boolean : greater resolutions or same as screen size shouldn't be considered
  111.     local hasValidOffset = newWidDiff > (self.screenWidth * 0.1) and
  112.     newHeiDiff > (self.screenHeight * 0.2)
  113.     if hasSmallerOffset and hasValidOffset then return true else return false end
  114. end
  115.  
  116. return WindowHandler
Tags: lua
Advertisement
Add Comment
Please, Sign In to add comment