place letterboxing code in file other than main.lua?

New to Moai? Get started here with our best tips and tutorials.

Moderators: ezraanderson, DanielWolf

place letterboxing code in file other than main.lua?

Postby rpallen » Fri Dec 01, 2017 6:27 am

I'm pretty new to Moai. I'm coming from Löve and Gideros. I was having problems with scaling in Löve, and Gideros isn't really available in Linux and I REALLY prefer Linux. I am using Moai with ZeroBrane on a Windows 10 machine. (I'll go through trying to get Moai working on my Linux if I'm successful in learning it on Windows.)

I'm playing around with scaling and how to do letterboxing, based on the blog post "Handling Multiple Device Resolutions and Aspect Ratios". That post says, "When we create our viewport, we use this offset to set the location of the viewport and thus the letterboxing. This is how our code looks throughout our config files:" I added the code to main.lua in a test project and modified it slightly to make it work since there is no actual device. The code I have works and opens a window when I run the project. It opens different sized windows if I change the DEVICE_WIDTH and DEVICE_HEIGHT variables. The working code is in the attached screenshot.

So what's my problem? This is probably something incredibly simple. The post says, "This is how our code looks throughout our config files:", so should some or all of this code be placed in a file other than main.lua, perhaps conf.lua or config.lua? If so,which part of the code and where?
Thanks.
Attachments
scaling_main.PNG
scaling_main.PNG (26.8 KiB) Viewed 436 times
rpallen
 
Posts: 3
Joined: Mon Oct 30, 2017 10:07 am

Re: place letterboxing code in file other than main.lua?

Postby naturally » Mon Dec 04, 2017 4:52 am

Just put that anywhere you want in Lua. There are no config files, so the tutorial must have meant a Lua file that you called "config.lua" and then required it somewhere.

Best bet is put that in a function and call it when you need to.
Image - Don't Be Patchman - a sneak-and-grow adventure!
User avatar
naturally
 
Posts: 716
Joined: Thu Aug 29, 2013 8:05 pm
Location: Canada

Re: place letterboxing code in file other than main.lua?

Postby naturally » Mon Dec 04, 2017 4:58 am

Here is what I do, with a 640x360 target resolution:

Code: Select all
  1.  

  2. Game.screenRatio = 1.9

  3. Game.screenHeight = 640

  4. Game.cursorVisible = false

  5. Game.screenWidth = math.floor(Game.screenHeight*Game.screenRatio)

  6.  

  7. -- ACTUAL SCREEN

  8. Game.screenHeight = MOAIEnvironment.verticalResolution or Game.screenHeight

  9. Game.screenWidth = MOAIEnvironment.horizontalResolution or Game.screenWidth

  10.  

  11. -- WORLD SIZE

  12. Game.viewport = MOAIViewport.new()

  13. Game.menu_viewport = MOAIViewport.new()

  14. Game.menu_crops = true

  15. function Game:ResizeViewports()

  16.         -- game world viewport

  17.         Game.screenRatio = Game.screenWidth / Game.screenHeight

  18.         Game.worldHeight = 360

  19.         Game.worldWidth = Game.worldHeight * Game.screenRatio

  20.         Game.worldWidth2 = Game.worldWidth/2

  21.         Game.worldHeight2 = Game.worldHeight/2

  22.         Game.viewport:setSize(Game.screenWidth, Game.screenHeight)

  23.         Game.viewport:setScale(Game.worldWidth, Game.worldHeight)

  24.         -- menu world viewport

  25.         Game.menuHeight = 360

  26.         Game.menuWidth = Game.menuHeight * Game.screenRatio

  27.         Game.menuCropped = false

  28.         if not Game.menu_crops then

  29.                 Game.menu_viewport:setSize(Game.screenWidth, Game.screenHeight)

  30.                 Game.menu_viewport:setScale(Game.worldWidth, Game.worldHeight)

  31.         else

  32.                 Game.menuCropped = true

  33.                 Game.menuHeight = 360

  34.                 Game.menuWidth = Game.menuHeight * Game.screenRatio

  35.                 if Game.menuWidth < 640 then

  36.                         local ratio = (Game.menuWidth/640)

  37.                         Game.menuWidth = 640

  38.                         local trimHeight = math.floor(Game.screenHeight*ratio)

  39.                         local offy = math.floor((Game.screenHeight - trimHeight)/2)

  40.                         Game.menu_viewport:setSize(0, offy, Game.screenWidth, trimHeight+offy)

  41.                         Game.menu_viewport:setScale(Game.menuWidth, Game.menuHeight)

  42.                 else

  43.                         Game.menu_viewport:setSize(Game.screenWidth, Game.screenHeight)

  44.                         Game.menu_viewport:setScale(Game.menuWidth, Game.menuHeight)

  45.                 end    

  46.         end

  47. end

  48. Game:ResizeViewports()

  49.  

  50. -- WINDOW

  51. MOAISim.openWindow(Game.gameName, Game.screenWidth, Game.screenHeight)

  52.  

  53. function OnScreenResize(width, height)

  54.         Game.screenWidth = width

  55.         Game.screenHeight = height

  56.         Game:ResizeViewports()

  57. end

  58. MOAIGfxDevice.setListener(MOAIGfxDevice.EVENT_RESIZE, OnScreenResize )

  59.  

  60.  



My game world gets the whole screen (Game.viewport) but my menus are letterboxed when screen goes to 4:3 etc (Game.menu_viewport)
Image - Don't Be Patchman - a sneak-and-grow adventure!
User avatar
naturally
 
Posts: 716
Joined: Thu Aug 29, 2013 8:05 pm
Location: Canada


Return to Questions and Tips

Who is online

Users browsing this forum: No registered users and 0 guests

cron

x