GLBasic forum

Main forum => FAQ => Topic started by: Millerszone on 2010-Nov-02

Title: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Millerszone on 2010-Nov-02
If you have an iPad and you are a developer then you already know that an iPad app must be able to work in
all 4 orientations or at least 2, either in landscape or portrait modes. GLBasic does not do this automatically.

I'm currently using this procedure with my iPad games "Hit the Deck Baseball" and "Just Pong", and works flawlessly

I've included the code below to make your iPad/iPhone app orientate in either landscape or portrait modes.
I'm using OpenGL commands directly, so it is fast. I notice no slow downs in FPS.

To know what orientation the iPad/iPhone is in, use this code:
http://www.glbasic.com/forum/index.php?topic=4650.msg35088#msg35088 (http://www.glbasic.com/forum/index.php?topic=4650.msg35088#msg35088)

NOTE: besides the code below, you must also reverse BOTH X and Y mouse inputs.

Add this code 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


EDIT: Found a solution and updated the code.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-02
Draw on a (createscreen) sprite and rotosprite draw it according to the joyx.  :zzz:
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Slydog on 2010-Nov-02
QuoteDraw on a (createscreen) sprite and rotosprite draw it according to the joyx.

But wouldn't all the touch input will be at the default orientation?
I guess you could handle that in code, but may get a little messy.

Is rotating that difficult for GLBasic? (on iPhones)
It would be so much easier if GLBasic handled the rotation for us (and rotated the input accordingly).

Like the links above (I never checked them), is there not some last minute camera matrix rotation that can be done by Gernot, based on a (new) rotation/orientation command?

It would be nice even if it simply used matchy's suggestion and called ROTOSPRITE for us.
It may not be the fastest solution, but it would at least work and keep our code consistent, and ready for a better solution if Gernot finds one (after an update).

Just wondering, as I have to tackle this myself eventually!
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-02
Quote from: Slydog on 2010-Nov-02
It would be so much easier if GLBasic handled the rotation for us (and rotated the input accordingly).
That would be nice!
I purchased Gernot Dr. Shiver HD game for the iPad, it rotates in all orientations, and very smooth.

I'm trying the CREATESCREEN and ROTOSPRITE that "matchy" suggested, but I'm not having much luck.
I got the screen to do a 180, but nothing moves. ( warning: I'm still learning. )
Here is a example of what I'm doing...

CREATESCREEN 1,10,1024,768
USESCREEN 1

// main loop
   WHILE TRUE

// *** MY GAME CODE HERE *** at 1024x768

   USESCREEN -1
   ROTOSPRITE 10, 0, 0, 180
   SHOWSCREEN
WEND

Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-02
Almost got it and there's more examples on the forum is you care to search for it.  ;)

This sample shows how to do it easily, not efficiently although the extra steps to draw to a createscreen sprite isn't much to worry about is it?  :sick:
Code (glbasic) Select

CREATESCREEN 1,10,1024,1024 // <-----rotating a square might help/be easier
// main loop
   WHILE TRUE
    USESCREEN 1 // <---------must be in loop

    /// *** invert mouse according to angle 0,90,180,270
    /// example: http://www.glbasic.com/forum/index.php?topic=4806.msg36640#msg36640
    /// more: http://www.glbasic.com/forum/index.php?topic=4650.msg35088#msg35088


// *** MY GAME CODE HERE *** at 1024x768

   USESCREEN -1
   ROTOSPRITE 10, 0, 0, 180
   SHOWSCREEN
WEND
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-02
O.K by moving the USESCREEN 1 inside the main loop, my objects are moving, everything
looks good but the objects aren't erasing each frame, they are leaving streaks behind them.
I'm using LOADBMP for my background.

Yes, I already had these in my code:
http://www.glbasic.com/forum/index.php?topic=4806.msg36640#msg36640 (http://www.glbasic.com/forum/index.php?topic=4806.msg36640#msg36640)
http://www.glbasic.com/forum/index.php?topic=4650.msg35088#msg35088 (http://www.glbasic.com/forum/index.php?topic=4650.msg35088#msg35088)

NOTE: I read this in the forum.
http://www.glbasic.com/forum/index.php?topic=4459 (http://www.glbasic.com/forum/index.php?topic=4459)
On iPhone once we set the project screen it can't be changed, so for changing the screen
between portrait and landscape in the same application we can only do currently a CREATE/USESCREEN.
This effectively draws what we want, but it can't be used really on iPhone, the simple sentence:
USESCREEN 0
USESCREEN -1
drops automatically game FPS from 60 to 40 doing nothing inside, and drawing into one screen
other than -1 is much worse.

Thanks for the help, I'm getting closer!

Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Slydog on 2010-Nov-02
I'm guessing that you can't use 'LOADBMP' if you also want to implement orientation.
The background sprite is baked into 'USESCREEN -1' (back buffer), and 'CREATESCREEN 1' / 'USESCREEN 1' can't access the background.  You will have to create a normal sprite for the background instead, and draw that sprite first each frame (on screen '1').

And I was worried there may be a drop in FPS, and that's why I was wondering if the screen can be rotated at the OpenGL level, using a Matrix (and hopefully automatic!  =D)
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-02
Quote from: Slydog link=topic=5298.msg41263#msg41263 date=
I'm guessing that you can't use 'LOADBMP' if you also want to implement orientation.
The background sprite is baked into 'USESCREEN -1' (back buffer), and 'CREATESCREEN 1' / 'USESCREEN 1' can't access the background.  You will have to create a normal sprite for the background instead, and draw that sprite first each frame (on screen '1').
That's what I thought I was going to have to do. If I have to use a DRAWSPRITE for my background pic at 1024x768 for each frame then my FPS drops from 60 to 35 on the iPad.

Quote
And I was worried there may be a drop in FPS, and that's why I was wondering if the screen can be rotated at the OpenGL level, using a Matrix (and hopefully automatic!  =D)
It seems like these commands ( glPushMatrix, glTranslatef, glRotatef, glPopMatrix ) from those links I posted shouldn't be very hard to make work with GLBasic.




Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-03
You'll have to clearscreen for <>0 screens. Also, if the device angle is zero, then there is no need to rotosprite..thus getting original fps.  :P

Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: ampos on 2010-Nov-03
Just create a rotated background in PS and do the following:

Code (glbasic) Select
If angle=0
   loadbmp "back1.png"
else
   loadbmp "back2.png"
endif


Or more flexible, on the point you check if the screen is rotated or not:
Code (glbasic) Select

if angle=0
  bk$="back1.png"
else
  bk$="back2.png"
endif
...
loadbmp bk$


Another option (if angle=0 or 180)
Code (glbasic) Select

load "back"+angle+".png"


and name your backgrounds as "back0.png" & "back180.png"
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-03
Changing the background doesn't deal with reversing the mouse inputs and draw positions.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: ampos on 2010-Nov-03
I was a answer to the "last" problem (rotated loadbmp), rotating the background, as the other rotating problems "seems" resolved with rotosprite.

BTW, I liked more your first answer  :P

Quoteampos, that's such a simplistic yet perfect solution without losing fps.  :good:
So I should point out that the screen doesn't need to literately animate the rotation.

Of course, the nice way is:

Code (glbasic) Select
if hasrotated=true
   grabsprite 1,0,0,1024,768
   backscreen
   for a=0 to 180 step 5
      rotosprite 1,somewherex,somewherey,a
      showscreen
   next
endif
...continue yuor program


To make a rotating screen effect
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-03
 :D :D :D This issue has me thinking and changing my mind.

When animated rotating, I use a square 1024x1024/480x480 because it hides the background (near the corners). Anyhow, what I have found in the overall process is how the application design. For example, I have an accepted app that rotates a square like menu only because there's no orientation for the actually app interface which stay still. Not sure about the design of menus that are on a 45' angle in the corner.

Most important, there is also a button structure that shifts the portrait/landscape (all four orientations) corner buttons to there appropriate place and I've had to include all these new features to an existing app.

I think I will still to landscape only games. ;)
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: erico on 2010-Nov-03
what if...

let'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

that way the app works on all orientation right?
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: 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!).
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-03
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.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-03
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
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: 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.

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.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Slydog on 2010-Nov-03
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)
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Slydog on 2010-Nov-03
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 (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.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-03
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.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-04
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:
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: erico on 2010-Nov-04
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.
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-04
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
Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: matchy on 2010-Nov-04
USEASBMP from the GLB help file:Note : On the iPhone, this command only works in Portrait mode.   :rtfm:  ::)

Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: Millerszone on 2010-Nov-04
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.

Title: Re: Changing the orientation of the screen on the iPhone or iPad
Post by: XanthorXIII on 2010-Nov-04
What if you remove the limit fps to 60? I thought there were some odd issues with setting that. I could be wrong though.

Title: Use this procedure to change orientation of the screen on the iPhone/iPad
Post by: Millerszone on 2010-Nov-07
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 (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
Title: Re: Changing the orientation of the screen on the iPhone/iPad [SOLVED]
Post by: Kitty Hello on 2010-Nov-08
one would have to rotate the GL_PROJECTION matrix - that way all the drawsprite positions would stay safe.
Title: Re: Changing the orientation of the screen on the iPhone/iPad [SOLVED]
Post by: Millerszone on 2010-Nov-08
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.
Title: Re: Changing the orientation of the screen on the iPhone/iPad [SOLVED]
Post by: XanthorXIII on 2010-Nov-08
Excellent! I think this should be a must add update for GLBasic.
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Kitty Hello on 2010-Nov-09
You want me to auto-flip the screen?
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Hark0 on 2010-Nov-09
Quote from: Kitty Hello on 2010-Nov-09
You want me to auto-flip the screen?

A NEW function like???:

RotateScreen(orientation)

orientation maybe 0,1,2,3 (up, down, left right) 0,90,180,270

With AUTO correct mouse coordinates?  =D

:P
Title: Re: Use this procedure to change orientation of the screen on the iPhone/iPad
Post by: Hark0 on 2010-Nov-09
Quote from: millerszone (KidNovak) on 2010-Nov-07
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 (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


I made modifications in your source for ANIMATED rotation:

Change this:

glRotatef(180.0, 0.0, 0.0, 1.0)
glTranslatef(-1024, -384, 0.0)

with this for CENTER ROTATION:

glTranslatef(512, 384, 0)
glRotatef(Angulo, 0.0, 0.0, 1.0)
glTranslatef(-512, -384, 0)


and ADD this "trick" for correct mouse coordinates on inverted screen:

MOUSESTATE mx, my, ma, mb

IF Screen_Rotated=TRUE
   mx = (mx * COS(180) - my * SIN(180))+1024
   my = (mx * SIN(180) + my * COS(180))+768
ENDIF

Test my compiled sample...

Use cursors up/down for simulate rotation on win32.
Use cursors left/right for change angle speed rotation.

=D


NOTE: I tried with 1024x1024 png... but seems the app "cut the image" to screen size.  :blink:

[attachment deleted by admin]
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Hark0 on 2010-Nov-09
Look the sample here:  =D




Rael speed 60 FPS... Reduced with video capture screen to 26+- FPS  ;/

And NOW.... preparing for iPad test speed.....  :P
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: bigsofty on 2010-Nov-09
Now that's a cool effect, it's good to see how this thread evolved so quickly into something so slick!  :good:

Cheers,


Ian
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Millerszone on 2010-Nov-09
Quote from: Hark0 on 2010-Nov-09
Quote from: Kitty Hello on 2010-Nov-09
You want me to auto-flip the screen?
A NEW function like???:
RotateScreen(orientation)
orientation maybe 0,1,2,3 (up, down, left right) 0,90,180,270
With AUTO correct mouse coordinates?  =D
:P
That would be nice! :)

Quote from: Hark0 on 2010-Nov-09
Quote from: millerszone (KidNovak) on 2010-Nov-07
This code flips the screen 180 degrees either in portrait or landscape using the accelerometer.
...
...
I made modifications in your source for ANIMATED rotation:

Change this:

glRotatef(180.0, 0.0, 0.0, 1.0)
glTranslatef(-1024, -384, 0.0)

with this for CENTER ROTATION:

glTranslatef(512, 384, 0)
glRotatef(Angulo, 0.0, 0.0, 1.0)
glTranslatef(-512, -384, 0)

and ADD this "trick" for correct mouse coordinates on inverted screen:

MOUSESTATE mx, my, ma, mb

IF Screen_Rotated=TRUE
   mx = (mx * COS(180) - my * SIN(180))+1024
   my = (mx * SIN(180) + my * COS(180))+768
ENDIF

Test my compiled sample...

Use cursors up/down for simulate rotation on win32.
Use cursors left/right for change angle speed rotation.
=D
NOTE: I tried with 1024x1024 png... but seems the app "cut the image" to screen size.  :blink:
I figured someone would come up with the code for animated rotation.  :)
I submitted one of my apps to apple yesterday with my code, I'll be
using this animated rotation next app.

Thanks.
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Hark0 on 2010-Nov-09
Good Luck with Appstore!!!

:good:
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: matchy on 2010-Dec-08
I can't get this to work with X_MAKE3D although I do need to rotate and translate after every X_MAKE2D. It is only for 2D?  :blink:
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Kitty Hello on 2010-Dec-08
the x_rotate is only for 3D. Or what do you mean?
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: matchy on 2010-Dec-09
glRotatef only rotate 2D not 3D (camera).
Title: Re: Change screen orientation on the iPhone/iPad [SOLVED]
Post by: Kitty Hello on 2010-Dec-09
You should apply it to the GL_PROJECTION matrix, not modelview.