Change screen orientation on the iPhone/iPad [SOLVED]

Previous topic - Next topic

matchy

Monkey Island rocks.  :coke:

There's also the issue of when the user locks the orientation, via hardware or task-bar, which would probably require an external class import.

Millerszone

I tried this simple code on the iPad, only got 13 FPS with the background and only 20 FPS without the background.
Am I missing something?
Code (glbasic) Select

SETSCREEN 1024,768,1
LIMITFPS 60

LOADSPRITE "Media/background.png", 0
LOADSPRITE "Media/ball.png" , 1

CREATESCREEN 1,10,1024,768

WHILE TRUE

ballX = ballX + 1

USESCREEN 1

CLEARSCREEN

DRAWSPRITE 0, 0, 0      <--- 13 FPS with and only 20 FPS without
DRAWSPRITE 1, ballX, 325

USESCREEN -1
ROTOSPRITE 10, 0, 0, 180 // or 270 for other orientation

FPS (TRUE)
SHOWSCREEN

WEND
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

ampos

Although your solution is the easiest one, to rotate such big sprite takes time... You are not missing anything. It is the way it should work.

Im sorry for you, but you will have to take the hard-long way.

Try this and check fps:

Code (glbasic) Select
SETSCREEN 1024,768,1
LIMITFPS 60

LOADbmp "Media/background.png"
LOADSPRITE "Media/ball.png" , 1

WHILE TRUE

ballX = ballX + 1
DRAWSPRITE 1, ballX, 325
FPS (TRUE)
SHOWSCREEN

WEND


This is the max fps you will get. Saddly, you will have to handle all screen rotation by yourself.

It would be REALLY COOL if GLB incorporates some hardware-openGL command to rotate screen.
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Slydog

Just wondering if a perfect square rotates faster?

So, instead of:
Code (glbasic) Select
CREATESCREEN 1,10,1024,768

Try:
Code (glbasic) Select
CREATESCREEN 1,10,1024,1024

If not, maybe Gernot could add a special 'rotate sprite' command that only allows rotations of 90 degree increments.  I'm sure it could be done *much* faster this way.  Maybe even ensure the sprite is a perfect square with 2^n dimensions.
[Edit] If not Gernot, then one of us could create a function, if we have access to the sprite data (and a good algorithm)

And/or, instead of rotating a 1024x1024 sprite, break it down into '32x32' (or 256x256) sprites and rotate each sub-sprite separately then move them into the proper 'slot' when done.  That is assuming the large size is the problem.  (A speed test between rotating one 1024x1024 versus 16 '256x256' sprites would tell us)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Slydog

Here's a quick function to rotate a 4x4 array 90 degrees.
I found it at:
http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array

Code (glbasic) Select
int a[4][4];
int n=4;
int tmp;
for (int i=0; i<n/2; i++){
        for (int j=i; j<n-i-1; j++){
                tmp=a[i][j];
                a[i][j]=a[j][n-i-1];
                a[j][n-i-1]=a[n-i-1][n-j-1];
                a[n-i-1][n-j-1]=a[n-j-1][i];
                a[n-j-1][i]=tmp;
        }
}


It's only one direction, and I'm not sure how fast it is.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Millerszone

I don't have a problem with rotating. Check out "Angry Birds", when you change the
orientation, it changes the screen instant, so I imagine they are using their own
code to rotate the screen. Thanks for the code Slydog!
Anyway here is a sample of the code I will be using to do with my orientation.
I just through this together, still needs work. :help:
Code (glbasic) Select

SETSCREEN 1024,768,1

LOADSPRITE "Media/background.png", 0

DRAWSPRITE 0 , 0, 0
USEASBMP

// main loop
WHILE TRUE

IF currentOrient <> iPadOrient(orientation) THEN orientChanged()

//  *** MAIN GAME CODE HERE ***

// FPS (TRUE)
SHOWSCREEN
WEND


FUNCTION orientChanged:
        IF orientation = 0
  // change everything back to normal
  currentOrient = 0
ELSE
// background
ROTOSPRITE 0, 0, 0, 180
USEASBMP
// *** reverse input movement here ***
// *** will GRABSPRITE AND ROTOSPRITE all sprites here  ***
  currentOrient = 2
ENDIF
ENDFUNCTION


// this code courtesy of Gernot
// my_drawangle - angle you can pass to ROTOSPRITE for correct orientation
//
// return:
// 0 = home button is on the right
// 1 = home button is on the bottom
// 2 = home button is on the left
// 3 = home button is on the top
FUNCTION iPadOrient%: BYREF my_drawangle
LOCAL fx, fy
STATIC last%, drawangle
   fx = GETJOYX(0)
   fy = GETJOYY(0)

   LOCAL limit = .75
   IF fy < -limit THEN last = 0
   IF fx >  limit THEN last = 1
   IF fy >  limit THEN last = 2
   IF fx < -limit THEN last = 3

   LOCAL angle = last * 90

   IF ABS(angle - drawangle)>180.0
      IF angle < drawangle
         INC angle, 360.0
      ELSE
         DEC angle, 360.0
      ENDIF
   ENDIF

   my_drawangle = drawangle * 0.9 + angle*0.1; drawangle=my_drawangle

   RETURN last
ENDFUNCTION


EDIT: changed code.
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

Millerszone

Quote from: ampos on 2010-Nov-03
Although your solution is the easiest one, to rotate such big sprite takes time... You are not missing anything. It is the way it should work.

It would be REALLY COOL if GLB incorporates some hardware-openGL command to rotate screen.
Try this and check fps:

I get 60 FPS.
I'm sure someone will get OpenGL commands to rotate screen... Hint, Hint Gernot  :nw:
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

erico

Quote from: Ian Price on 2010-Nov-03
Quotelet's say you want your game to work one orientation only, portrait, and when you rotate to landscape, the game pauses and display game statistics and options? :S

Monkey Island does that - and it's fecking annoying. If you don't hold the device almost perfectly still the game pauses and the Load/Save screen pops up. Aaarggghhhh! (done in a pirate voice, obviously!).

What if a little delay is added, would it help the thing not scrambling? is this idea any good anyhow? I don't own an I-stuff but I worked out a friend of mine's one and the auto shift sometimes can get annoying I agree, sometimes out of nothing. Is a delay possible? if it would not break apple rules... I plan to do stuff for i-platforms so this interests me. If such rotating stuff is required, I wonder what's the fastest auto-rotate route...other than programing both views.

Millerszone

Quote from: erico
If such rotating stuff is required, I wonder what's the fastest auto-rotate route...other than programing both views.

HEY Erico!
I'm with Slydog, OpenGL level using a Matrix would be the fastest, but I wonder what kind of hit that
would take on FPS. I'm learning that you have to optimize your code for the i-platforms.
Games that run 500+ on a PC might only run 50 FPS on an iPad.

I'll talk to you later Erico, I'm flipping some code. :S
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

matchy

USEASBMP from the GLB help file:Note : On the iPhone, this command only works in Portrait mode.   :rtfm:  ::)


Millerszone

Quote from: matchy on 2010-Nov-04
USEASBMP from the GLB help file:Note : On the iPhone, this command only works in Portrait mode.   :rtfm:  ::)

Yep, I just found that out. Also GRABSPRITE does not work in landscape mode, this is not good.

I'm glad my other game is in portrait mode.

Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

XanthorXIII

What if you remove the limit fps to 60? I thought there were some odd issues with setting that. I could be wrong though.

Owlcat has wise

Millerszone

#27
This code flips the screen 180 degrees either in portrait or landscape using the accelerometer.
(use this code to get the orientation: http://www.glbasic.com/forum/index.php?topic=4650.msg35088#msg35088
You will also have to reverse BOTH X and Y mouse inputs. That's it.
I don't notice any drop in FPS with this procedure, compared to CREATESCREEN that
drops below 40 FPS with nothing in the loop on the iPad.
(I still need a easier way to change the draw and collision positions.)

Add the OpenGL commands to your main loop:
Code (glbasic) Select

SETSCREEN 1024, 768, TRUE
LOADBMP background$

FUNCTION upateGraphics:

    glPushMatrix()

        // use this code to flip 180 landscape (home button on left)
        glRotatef(180.0, 0.0, 0.0, 1.0)
        glTranslatef(-1024, -768, 0.0)  <-- change for iPhone

        //use this code to flip 180 portrait (home button on top)
        //glRotatef(90.0, 0.0, 0.0, 1.0)
        //glTranslatef(-768, -1024, 0.0)  <-- change for iPhone

        //      *** all your DRAW code goes here  ***

    glPopMatrix()

    SHOWSCREEN

ENDFUNCTION



Add this OpenGL wrapper code to a new file in your project.
Code (glbasic) Select

// INLINE functions to use OpenGL functions by Gernot
// directly inside GLBasic

INLINE
#define OGL                ::
typedef float           GLfloat;
ENDINLINE

INLINE
} extern "C" { void __stdcall glTranslatef( GLfloat x , GLfloat y , GLfloat z );; }; namespace __GLBASIC__ {
ENDINLINE
FUNCTION glTranslatef: x, y, z
    INLINE
        OGL glTranslatef(x, y, z);
    ENDINLINE
ENDFUNCTION


INLINE
} extern "C" { void __stdcall glRotatef( GLfloat angle , GLfloat x , GLfloat y , GLfloat z );; }; namespace __GLBASIC__ {
ENDINLINE
FUNCTION glRotatef: angle, x, y, z
    INLINE
        OGL glRotatef(angle, x, y, z);
    ENDINLINE
ENDFUNCTION


INLINE
} extern "C" { void __stdcall glPushMatrix( void );; }; namespace __GLBASIC__ {
ENDINLINE
FUNCTION glPushMatrix:
    INLINE
        OGL glPushMatrix();
    ENDINLINE
ENDFUNCTION


INLINE
} extern "C" { void __stdcall glPopMatrix( void );; }; namespace __GLBASIC__ {
ENDINLINE
FUNCTION glPopMatrix:
    INLINE
        OGL glPopMatrix();
    ENDINLINE
ENDFUNCTION
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

Kitty Hello

one would have to rotate the GL_PROJECTION matrix - that way all the drawsprite positions would stay safe.

Millerszone

#29
Quote from: Kitty Hello on 2010-Nov-08
one would have to rotate the GL_PROJECTION matrix - that way all the drawsprite positions would stay safe.

Actually the drawsprite positions where O.k, it was the collision positions I thought were
messed up, but I forget to reverse the mouse[0].x input, now everything is working great.

No need to change any positions on sprites or collisions, just reverse both mouse inputs.
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5