Device type detection – ShiVa Engine

Device type detection

This is done immediately at the initialization (onInit) of your program. Here are the steps:
1. Create an ENVIRONMENT VARIABLE (Game Editor->Envoironment Tab->Add) called sOSType set to empty string
NOTE: The code currently will detect the OS and turn the screen orientation to a certain direction based on the OS type. If its an android or iOS the screen will be in landscape setting (setting 3). If its a Windows OS then screen will be setting 0 or Portrait mode. These settings can easily be adjusted to suit your needs.
2. Place the following code into the onInit function of your program (You may also elaborate on the code to account for other OS types and screen sizes, but for now this code will detect iOS devices, Windows and Android):

    -- 1. DETECT OS Type
    if system.getOSType ( ) == system.kOSTypeIPhone then
        -- 2. DETECT SCREEN SIZE
        local nWidth, nHeight = system.getCurrentScreenResolution ( )
        -- IF item is 1024x768 more than likely an iPad or Android 3.0 tablet so screen should be rotated to 3
        if ((nWidth == 1024 and nHeight == 768) or (nWidth == 768 and nHeight == 1024)) then
            application.setCurrentUserEnvironmentVariable ( "sOSType", "IPAD" )
        elseif ((nWidth == 960 and nHeight == 640) or (nWidth == 640 and nHeight == 960)) then
            -- IF item is 960x640 more than likely an iPhone 4
            application.setCurrentUserEnvironmentVariable ( "sOSType", "IPHONE4" )
        elseif ((nWidth == 480 and nHeight == 320) or (nWidth == 320 and nHeight == 480)) then
            -- IF item is 480x320 more than likely an iPod touch or iPhone 3G/iPhone3GS/2G
            application.setCurrentUserEnvironmentVariable ( "sOSType", "IPHONE3" )
        else
            application.setCurrentUserEnvironmentVariable ( "sOSType", "IPHONE_unknown" )
        end
        application.setOption ( application.kOptionViewportRotation, 3 )
    elseif (system.getOSType ( ) == system.kOSTypeAndroid) then
        -- IF item is 1024x768 more than likely an iPad or Android 3.0 tablet so screen should be rotated to 3
        if (system.getOSVersionString ( ) == "3.0") then
            application.setCurrentUserEnvironmentVariable ( "sOSType", "ANDROID3" )
        elseif (system.getOSVersionString ( ) == "2.2") then
            application.setCurrentUserEnvironmentVariable ( "sOSType", "ANDROID2.2" )
        elseif (system.getOSVersionString ( ) == "2.1") then
            application.setCurrentUserEnvironmentVariable ( "sOSType", "ANDROID2.1" )
        else
            application.setCurrentUserEnvironmentVariable ( "sOSType", "ANDROID_unknown" )
        end
        application.setOption ( application.kOptionViewportRotation, 3 )
    elseif (system.getOSType ( ) == system.kOSTypeWindows) then
        application.setCurrentUserEnvironmentVariable ( "sOSType", "WINDOWS" )
        application.setOption ( application.kOptionViewportRotation, 0 )
    else
        application.setCurrentUserEnvironmentVariable ( "sOSType", "unknown" )
        application.setOption ( application.kOptionViewportRotation, 0 )
    end



Need more answers?

  • slackBanner