Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set the game window resolutions and setup them
- -- For easier OOP structuring
- local class = require("Libraries.middleclass")
- -- For logging handling
- local logger = require("Libraries.Logilog")
- -- For session handling
- local sessionHandler = require("Source.Session.SessionHandler")
- -- Defines a class for handling the window
- local WindowHandler = class("WindowHandler")
- ---@type number : define the minimum supported resolution
- WindowHandler.static.MIN_WIDTH = 640
- WindowHandler.static.MIN_HEIGHT = 480
- -- Define supported common resolutions with strings as keys for faster lookup
- function WindowHandler:presetDimensions() ---@return table
- local presetDimensions =
- {
- [tostring(WindowHandler.MIN_WIDTH) .. "x" .. tostring(WindowHandler.MIN_HEIGHT)] = true,
- ["960x540"] = true, ["960x720"] = true, ["1024x768"] = true, ["1280x800"] = true,
- ["1280x960"] = true, ["1366x768"] = true, ["1440x900"] = true, ["1440x1080"] = true,
- ["1600x900"] = true, ["1600x1200"] = true, ["1680x1050"] = true, ["1920x1080"] = true,
- ["1920x1200"] = true, ["2560x1080"] = true, ["2560x1440"] = true, ["2560x1600"] = true,
- ["2880x1800"] = true, ["3440x1440"] = true, ["3840x1080"] = true, ["3840x1600"] = true,
- ["3840x2160"] = true, ["5120x1440"] = true, ["5120x2160"] = true, ["7680x2160"] = true,
- ["10240x4320"] = true
- }
- return presetDimensions
- end
- -- Get and store essential information about the screen size
- function WindowHandler:initialize()
- -- Ensure game window creation is possible
- if not love.window then logger:logMessage("FATAL", "Couldn't create game window.")
- sessionHandler:safeSessionExit("Bye bye :[") end
- -- Get the screen dimensions, and when unsupported, abort the game
- self.screenWidth, self.screenHeight = self:getScreenSize() ---@type number
- self:ensureSupportedScreen()
- -- Allows the window to be fullscreen when supported, or a smaller-than-full window when not
- self.fullscreenCompatible = self:isDefinedResolution()
- end
- -- Check whether the game window creation is supported by this screen or device
- function WindowHandler:ensureSupportedScreen()
- -- When the size is smaller than the minimum resolution, it is invalid
- if self.screenWidth < WindowHandler.MIN_WIDTH or self.screenHeight < WindowHandler.MIN_HEIGHT
- then
- logger:logMessage("FATAL", "Your screen resolution is smaller than 640 x 480, thus,\z
- unsupported.")
- sessionHandler:safeSessionExit("Bye bye :[")
- end
- end
- -- Get the screen size or an error code when smaller than the minimum resolution
- function WindowHandler:getScreenSize() ---@return number
- -- Store the original size for restoring and get the screen size through setMode(0, 0)
- local originalWidth, originalHeight = love.graphics.getDimensions() ---@type number
- love.window.setMode(0, 0, {fullscreen=false})
- -- Store the screen size and restore the original size
- local monitorWidth, monitorHeight = love.graphics.getDimensions() ---@type number
- love.window.setMode(originalWidth, originalHeight, {fullscreen=false})
- return monitorWidth, monitorHeight
- end
- -- Set the window mode and configurations
- function WindowHandler:configure(inputTitle)
- -- Window and graphics setup
- love.window.setTitle(inputTitle)
- love.graphics.setDefaultFilter("nearest", "nearest")
- -- If fullscreen compatible, make the window fullscreen; rendering is always pixel perfect
- local windowAdjustments = {resizable=false, fullscreen=false, vsync=0} ---@type table
- if self.fullscreenCompatible then windowAdjustments["fullscreen"] = true end
- -- If not, set to a smaller-than-full window
- local targetWidth, targetHeight = self:getOptimalWindow() ---@type number
- love.window.setMode(targetWidth, targetHeight, windowAdjustments)
- end
- -- Checks whether the screen size is one of the defined resolutions
- function WindowHandler:isDefinedResolution() ---@return boolean
- ---@type string : screen size following the resolutions table key-naming pattern
- local screenSize = tostring(self.screenWidth) .. "x" .. tostring(self.screenHeight)
- if not self:presetDimensions()[screenSize] then return false else return true end
- end
- -- Select and returns a given resolution that fits better the user screen
- function WindowHandler:getOptimalWindow() ---@return number
- ---@type number : always start the offset against the minimum valid dimensions
- local widthOffset = self.screenWidth - WindowHandler.MIN_WIDTH
- local heightOffset = self.screenHeight - WindowHandler.MIN_HEIGHT
- -- Iterate through the adequate aspect ratio resolutions to get the optimal one
- for dimensions, garbage in pairs(self:presetDimensions()) do
- local separatorIndex = string.find(dimensions, "x")
- local newWidthOffset = self.screenWidth - tonumber(dimensions:sub(1, separatorIndex - 1))
- local newHeightOffset = self.screenHeight - tonumber(dimensions:sub(separatorIndex + 1))
- -- Avoid invalid dimensions for this screen size, getting the greatest smaller resolution
- if self:isValidDimension(widthOffset, heightOffset, newWidthOffset, newHeightOffset) then
- widthOffset, heightOffset = newWidthOffset, newHeightOffset
- end
- end
- -- Subtracting the offsets from the screen dimensions results on the desired window size
- return self.screenWidth - widthOffset, self.screenHeight - heightOffset
- end
- -- Ensure the current in-analysis resolution is the nearest-but-not-equal to screen ones
- function WindowHandler:isValidDimension(baseWidDiff, baseHeiDiff, newWidDiff, newHeiDiff)
- ---@type boolean : offset should always be smaller than the previous to be considered
- local hasSmallerOffset = (newWidDiff + newHeiDiff) < (baseWidDiff + baseHeiDiff)
- ---@type boolean : greater resolutions or same as screen size shouldn't be considered
- local hasValidOffset = newWidDiff > (self.screenWidth * 0.1) and
- newHeiDiff > (self.screenHeight * 0.2)
- if hasSmallerOffset and hasValidOffset then return true else return false end
- end
- return WindowHandler
Advertisement
Add Comment
Please, Sign In to add comment