Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Code for landscape or portrait — Gideros Forum

Code for landscape or portrait

SinisterSoftSinisterSoft Maintainer
edited February 2018 in Code snippets
Here is some code to help you make your games landscape or portrait.

First make a new player with the project auto rotate turned on and also the mode to enable both landscape and portrait in the 'require' plugin. This code is for when the scaling has been set to 'letterbox', 'landscapeLeft'.

Then include this in at the beginning of your code (a lot of this isn't needed, but you may find it useful):
device={application:getDeviceInfo()}
print(device[1])
print(device[2])
print(device[3])
print(device[4])
print(device[5])
if device[5]=="TV" then tv=true end
if device[1]=="Windows" or device[1]=="Mac OS" then
	--application:set("cursor","blank")
	pc=true
	premium=true
	--tv=true
elseif device[1]=="Web" then web=true premium=true
elseif device[1]=="Win32" then win32=true application:setFullScreen(true) tv=true premium=true
elseif device[1]=="WinRT" then winrt=true premium=true
elseif device[1]=="Windows Phone" then winrt=true slow=true premium=true
elseif device[1]=="Android" then android=true
	if device[3]=="Amazon" then kindle=true
		if sub(device[4],1,3)=="AFT" then tv=true if device[4]=="AFTM" then slow=true end end
	elseif device[3]=="OUYA" then ouya=true tv=true slow=true
	elseif device[3]=="Geniatech" then tv=true
		if sub(device[4],1,9)=="GameStick" or device[4]=="TotoTV Stick" then gamestick=true slow=true end
	elseif device[3]=="HardKernel Co., Ltd." then tv=true hardkernel=true
		if device[4]=="ODROIDC" then odroidc=true slow=true end
	elseif device[4]=="Nexus Player" then tv=true nexusplayer=true
	elseif device[3]=="razer" then tv=true razer=true
		if device[4]=="Forge" then forgetv=true end
	elseif device[3]=="IRIVER" then slow=true end
elseif device[1]=="iOS" then ios=true
	if device[3]=="AppleTV" then tv=true appletv=true end
end
if application:isPlayerMode() then
	secret=true
end
if web then
	application:setOrientation("landscapeLeft")
end
 
local orientation=application:getDeviceOrientation()
local oldOrientation=orientation
local width=application:getLogicalHeight()
local height=application:getLogicalWidth()
local minX,minY,maxX,maxY=application:getDeviceSafeArea(true)
local maxWidth,maxHeight=maxX-minX,maxY-minY
local width2,height2=width/2,height/2
local landscape=true
Then later in your code add this:
function resize(e)
	print("resize")
	width=application:getLogicalHeight()
	height=application:getLogicalWidth()
	minX,minY,maxX,maxY=application:getDeviceSafeArea(true)
	maxWidth,maxHeight=maxX-minX,maxY-minY
	landscape=true
	if pc then
		if maxHeight>maxWidth then orientation="portraitLeft" landscape=false else orientation="landscapeLeft" end
	else
		orientation=application:getDeviceOrientation()
		local portrait=false
		if sub(orientation,1,8)=="portrait" then portrait=true end
		if (not e and portrait) or orientation~=oldOrientation then
			oldOrientation=orientation
			application:setOrientation(orientation)
		end
		if portrait then
			landscape=false
			width,height=height,width
			minX,maxX,minY,maxY=minY,maxY,minX,maxX
			maxWidth,maxHeight=maxHeight,maxWidth
		end
	end
	print(orientation)
 
	width2,height2=width/2,height/2
	-- do anything here you need to do only once on a resize
end
The code in the resize routine may seem a little complicated, but it copes with bugs on some hardware (amazon!) starting upside down if you set the orientation after getting the orientation and it hasn't been changed automatically. It also allows you to test in the normal Windows/OSX player and use the auto-sizing to stretch your screen from landscape to portrait aspect ratios and it figure it out.

If your positioning code you can use width2 & height2 to have the middle of the display, maxWidth & maxHeight to have the real screen sizes, etc...
Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+6 / -0 )Share on Facebook
Sign In or Register to comment.