Android screen sizes

Discussion about using Moai SDK - post questions, issues, and ideas here.

Moderators: naturally, seebs, ezraanderson

Android screen sizes

Postby lancew » Wed Aug 22, 2012 12:26 pm

Every example/tutorial I can find is setup for a specific screen size, ie:

Code: Select all
  1. SCREEN_UNITS_X = 960

  2. SCREEN_UNITS_Y = 540

  3. SCREEN_WIDTH = SCREEN_UNITS_X

  4. SCREEN_HEIGHT = SCREEN_UNITS_Y

  5.  

  6. MOAISim.openWindow ( "name here", SCREEN_WIDTH, SCREEN_HEIGHT )

  7.  

  8. viewport = MOAIViewport.new ()

  9. viewport:setScale ( SCREEN_UNITS_X, SCREEN_UNITS_Y )

  10. viewport:setSize ( SCREEN_WIDTH, SCREEN_HEIGHT )



Is there a way to query the size of the display so that I can autofit the game to the current device?
Check out my first game released with Moai: https://play.google.com/store/apps/details?id=com.fazed.bugs. Feedback is very welcome =P
lancew
 
Posts: 80
Joined: Wed Aug 22, 2012 12:23 pm

Re: Android screen sizes

Postby Behemoth » Wed Aug 22, 2012 4:10 pm

viewportWidth, viewportHeight = MOAIGfxDevice.getViewSize()

and you can call a function when the viewport resizes if you need to with:

MOAIGfxDevice.setListener(MOAIGfxDevice.EVENT_RESIZE, callbackOnViewportResize)
User avatar
Behemoth
 
Posts: 4
Joined: Fri Jul 13, 2012 7:22 am

Re: Android screen sizes

Postby sgeos » Wed Aug 22, 2012 4:37 pm

I'm using something like this:

Code: Select all
  1. -- Global Settings

  2. programName  = "Name of Program"

  3. screenWidth  = MOAIEnvironment.horizontalResolution or 960

  4. screenHeight = MOAIEnvironment.verticalResolution   or 9 / 16 * result.screenWidth

  5.  

  6. -- Simulator Window

  7. MOAISim.openWindow( programName, screenWidth, screenHeight )

  8.  

  9. -- Viewport

  10. viewport = MOAIViewport.new ( )

  11. unitsX  = 2048

  12. unitsY  = unitsX * screenHeight / screenWidth

  13. viewport:setSize  ( screenWidth, screenHeight )

  14. viewport:setScale ( unitsX,      unitsY       )


It locks X units at 2048 for landscape, and scales the Y units and viewport appropriately.
Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: Android screen sizes

Postby lancew » Thu Aug 23, 2012 7:46 am

Thanks guys, thats exactly what I needed.
Check out my first game released with Moai: https://play.google.com/store/apps/details?id=com.fazed.bugs. Feedback is very welcome =P
lancew
 
Posts: 80
Joined: Wed Aug 22, 2012 12:23 pm

Re: Android screen sizes

Postby alvion » Thu Dec 13, 2012 10:22 pm

Unfortunately I'm still having problems with this. All I'm doing is creating the window and then drawing a sprite at 0,0. No matter what I do, the character is drawn exactly in the center of the screen when run on the PC but on my Galaxy Nexus he is drawn *horizontally* centered but vertically he is near the bottom of the screen. It's worth noting that he seems to be in the same position regardless of how big I set the world size (ie viewport scale).

Anyone have an idea what I'm doing wrong?

Here's my code:

Code: Select all
  1.  

  2. SCREEN_WIDTH = MOAIEnvironment.verticalResolution or 640

  3. SCREEN_HEIGHT = MOAIEnvironment.horizontalResolution or (4/3) * SCREEN_WIDTH

  4.  

  5. MOAISim.openWindow ( "Kid Hero", SCREEN_WIDTH, SCREEN_HEIGHT )

  6.  

  7. WORLD_WIDTH = 320

  8. WORLD_HEIGHT = WORLD_WIDTH * (SCREEN_HEIGHT / SCREEN_WIDTH)

  9.  

  10. viewport = MOAIViewport.new ()

  11. viewport:setSize(SCREEN_WIDTH, SCREEN_HEIGHT)

  12. viewport:setScale(WORLD_WIDTH, WORLD_HEIGHT)

  13. viewport:setOffset(0, 0)

  14.  

  15. print ("Coordinates: " .. SCREEN_WIDTH .. " / " .. SCREEN_HEIGHT .. " -- " .. WORLD_WIDTH .. " / " .. WORLD_HEIGHT)

  16.  



Debug output from my PC build: Coordinates: 640 / 853.33333333333 -- 320 / 426.66666666667
Debug output from my Android build: Coordinates: 720 / 1184 -- 320 / 526.22222222222

Help!
H
alvion
 
Posts: 3
Joined: Thu Dec 13, 2012 10:08 pm

Re: Android screen sizes

Postby alvion » Wed Dec 19, 2012 1:31 pm

It turns out my issues have to do with the fact that I was using portrait mode by simply removing the landscape orientation attribute from the main activity but there appears to be more to it than that. The problem wasn't helped by the fact that I had a typo mixing up vertical and horizontal resolution in the code snippet above, but fixing that doesn't fix the issue either. I suspect there is more than one needs to do to handle running in portrait mode on an Android device but as I now plan to force landscape anyway, I won't worry about that for now.
alvion
 
Posts: 3
Joined: Thu Dec 13, 2012 10:08 pm

Re: Android screen sizes

Postby ibisum » Thu Dec 20, 2012 9:44 pm

Just a thought .. are you responding to OnResize?

Code: Select all
  1.  

  2.  

  3. function onResize ( width, height )

  4.     mainViewport:setSize ( width, height )

  5.     mainViewport:setScale ( width, height )

  6. end

  7.  

  8. MOAIGfxDevice.setListener ( MOAIGfxDevice.EVENT_RESIZE, onResize )

  9.  

  10.  

;
--
Email: ibisum@gmail.com
IRC: torpor on FreeNode, see you in the #moai channel
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info/
User avatar
ibisum
 
Posts: 1504
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Android screen sizes

Postby satsabadell » Mon Dec 24, 2012 4:29 pm

I am creating an app for iPad resolution, and scaling it to the max resolution of each device (android or iOS):
Code: Select all
  1. --== Init ==--

  2. local TEST_X = 1196

  3. local TEST_Y = 720

  4. local T = {}

  5. T.GAME_X = 2048

  6. T.GAME_Y = 1536

  7. T.DEVICE_WIDTH = MOAIEnvironment.verticalResolution or TEST_X

  8. T.DEVICE_HEIGHT = MOAIEnvironment.horizontalResolution or TEST_Y

  9. T.SCREEN_WIDTH = 0

  10. T.SCREEN_HEIGHT = 0

  11. T.SCREEN_X_OFFSET = 0

  12. T.SCREEN_Y_OFFSET = 0

  13.  

  14. T.init = function()

  15.   --== Fix android weirds with screen ==--

  16.   if (T.DEVICE_WIDTH < T.DEVICE_HEIGHT) then

  17.     T.DEVICE_WIDTH, T.DEVICE_HEIGHT = T.DEVICE_HEIGHT, T.DEVICE_WIDTH

  18.   end

  19.  

  20.   --== Get screen to use and offset ==--

  21.   local gameAspect = T.GAME_X / T.GAME_Y

  22.   local realAspect = T.DEVICE_WIDTH / T.DEVICE_HEIGHT

  23.  

  24.   if realAspect == gameAspect then

  25.     T.SCREEN_WIDTH = T.DEVICE_WIDTH

  26.     T.SCREEN_HEIGHT = T.DEVICE_HEIGHT

  27.   elseif realAspect < gameAspect then        

  28.     T.SCREEN_WIDTH = T.DEVICE_WIDTH        

  29.     T.SCREEN_HEIGHT = T.DEVICE_WIDTH / gameAspect  

  30.   else        

  31.     T.SCREEN_WIDTH = T.DEVICE_HEIGHT * gameAspect        

  32.     T.SCREEN_HEIGHT = T.DEVICE_HEIGHT    

  33.   end      

  34.   if T.SCREEN_WIDTH < T.DEVICE_WIDTH then        

  35.     T.SCREEN_X_OFFSET = ( T.DEVICE_WIDTH - T.SCREEN_WIDTH ) * 0.5    

  36.   end      

  37.   if T.SCREEN_HEIGHT < T.DEVICE_HEIGHT then        

  38.     T.SCREEN_Y_OFFSET = ( T.DEVICE_HEIGHT - T.SCREEN_HEIGHT ) * 0.5    

  39.   end

  40.  

  41.   --== Load a window ==--

  42.   MOAISim.openWindow('MATHS I', T.DEVICE_WIDTH, T.DEVICE_HEIGHT)

  43.  

  44.   --== Load a new global viewport and render table ==--

  45.   myViewport = MOAIViewport.new()

  46.   myViewport:setSize(T.SCREEN_X_OFFSET, T.SCREEN_Y_OFFSET, T.SCREEN_X_OFFSET + T.SCREEN_WIDTH, T.SCREEN_Y_OFFSET + T.SCREEN_HEIGHT)

  47.   myViewport:setScale(T.GAME_X, T.GAME_Y)

  48.  

  49.   myRenderTable = {}

  50.   MOAIRenderMgr.setRenderTable(myRenderTable)

  51.  

  52. end

  53.  

  54. return T


BTW, I am working in landscape mode only, so I setted to this in the AndroidManifest file:
Code: Select all
  1. <uses-feature android:name="android.hardware.screen.landscape" android:required="false" />


It works perfectly for me in all the devices that I have tested.
www.imathings.com
iPhone & iPad apps
satsabadell
 
Posts: 31
Joined: Sun Dec 16, 2012 8:36 am


Return to Moai SDK

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x