You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.6 KiB

  1. simpleScale = {}
  2. --Your Game's Aspect Ratio
  3. local gAspectRatio
  4. --The Window's Aspect Ratio
  5. local wAspectRatio
  6. --The scale between the game and the window's aspect ratio
  7. simpleScale.scale = 1
  8. local xt, yt = 0, 0, 1
  9. local gameW, gameH, windowW, windowH = 800, 600, 800, 600
  10. -- Declares your game's width and height, and sets the window size/settings
  11. -- To be used instead of love.window.setMode
  12. -- [gw] and [gh] are the width and height of the initial game
  13. -- [sw] and [sh] (optional) are the width and height of the final window
  14. -- [settings] (optional) are settings for love.window.setMode
  15. function simpleScale.setWindow(gw, gh, sw, sh, settings)
  16. sw = sw or gw
  17. sh = sh or gh
  18. gAspectRatio = gw/gh
  19. gameW = gw
  20. gameH = gh
  21. simpleScale.updateWindow(sw, sh, settings)
  22. end
  23. -- Updates the Window size/settings
  24. -- To be used instead of love.window.setMode
  25. -- [sw] and [sh] are the width and height of the new Window
  26. -- [settings] (optional) are settings for love.window.setMode
  27. function simpleScale.updateWindow(sw, sh, settings)
  28. love.window.setMode(sw, sh, settings)
  29. windowW, windowH = love.graphics.getWidth(), love.graphics.getHeight()
  30. wAspectRatio = windowW/windowH
  31. --Window aspect ratio is TALLER than game
  32. if gAspectRatio > wAspectRatio then
  33. scale = windowW/gameW
  34. xt = 0
  35. yt = windowH/2 - (scale*gameH)/2
  36. --Window aspect ratio is WIDER than game
  37. elseif gAspectRatio < wAspectRatio then
  38. scale = windowH/gameH
  39. xt = windowW/2 - (scale*gameW)/2
  40. yt = 0
  41. --Window and game aspect ratios are EQUAL
  42. else
  43. scale = windowW/gameW
  44. xt = 0
  45. yt = 0
  46. end
  47. simpleScale.scale = scale
  48. end
  49. -- If you screen is resizable on drag, you'll need to call this to make sure
  50. -- the appropriate screen values stay updated
  51. -- You can call it on love.update() with no trouble
  52. function simpleScale.resizeUpdate()
  53. windowW, windowH = love.graphics.getWidth(), love.graphics.getHeight()
  54. wAspectRatio = windowW/windowH
  55. --Window aspect ratio is TALLER than game
  56. if gAspectRatio > wAspectRatio then
  57. scale = windowW/gameW
  58. xt = 0
  59. yt = windowH/2 - (scale*gameH)/2
  60. --Window aspect ratio is WIDER than game
  61. elseif gAspectRatio < wAspectRatio then
  62. scale = windowH/gameH
  63. xt = windowW/2 - (scale*gameW)/2
  64. yt = 0
  65. --Window and game aspect ratios are EQUAL
  66. else
  67. scale = windowW/gameW
  68. xt = 0
  69. yt = 0
  70. end
  71. simpleScale.scale = scale
  72. end
  73. -- Transforms the game's window relative to the entire window
  74. -- Call this at the beginning of love.draw()
  75. function simpleScale.set()
  76. love.graphics.push()
  77. love.graphics.translate(xt, yt)
  78. love.graphics.scale(scale, scale)
  79. end
  80. -- Untransforms the game's window
  81. -- Call this at the end of love.draw
  82. -- You can optionally make the letterboxes a specific color by passing
  83. -- [color] (optional) a table of color values
  84. function simpleScale.unSet(color)
  85. love.graphics.scale(1/scale, 1/scale)
  86. love.graphics.translate(-xt, -yt)
  87. love.graphics.pop()
  88. --Draw the Letterboxes
  89. local r,g,b,a = love.graphics.getColor()
  90. local originalColor = love.graphics.getColor()
  91. local boxColor
  92. if color == nil then
  93. boxColor = {0,0,0}
  94. else
  95. boxColor = color
  96. end
  97. love.graphics.setColor(boxColor)
  98. --Horizontal bars
  99. if gAspectRatio > wAspectRatio then
  100. love.graphics.rectangle("fill", 0, 0, windowW, math.abs((gameH*scale - (windowH))/2))
  101. love.graphics.rectangle("fill", 0, windowH, windowW, -math.abs((gameH*scale - (windowH))/2))
  102. --Vertical bars
  103. elseif gAspectRatio < wAspectRatio then
  104. love.graphics.rectangle("fill", 0, 0, math.abs((gameW*scale - (windowW))/2),windowH)
  105. love.graphics.rectangle("fill", windowW, 0, -math.abs((gameW*scale - (windowW))/2),windowH)
  106. end
  107. love.graphics.setColor(r,g,b,a)
  108. end