Switching between landscape and portait mode

I’d like to get 90 degrees rotation to use screen in portrait mode on
framebuffer device (that will be the final environment for my applcation)
and usual landscape orientation on X (I use it while developing the
application).
I know there are some graphics libraries that allowing arbitrary surface
rotation, but I don’t want to waste my CPU time more than necessary.

Which is the best option to do that?

I think also about a modified SDL_BlitSurface that blits an (Width x Height)
surface on a (Height x Width) surface. Which is the best staring point to do
this?

Thank you for any suggestion,

Alessandro Staltari

Alessandro Staltari wrote:

I’d like to get 90 degrees rotation to use screen in portrait mode on
framebuffer device (that will be the final environment for my applcation)
and usual landscape orientation on X (I use it while developing the
application).
I know there are some graphics libraries that allowing arbitrary surface
rotation, but I don’t want to waste my CPU time more than necessary.

Which is the best option to do that?

I think also about a modified SDL_BlitSurface that blits an (Width x Height)
surface on a (Height x Width) surface. Which is the best staring point to do
this?

Thank you for any suggestion,

Alessandro Staltari


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hm.

Working on Compaq iPaq, I’ve built a library for my own use that autorotates all
images on load-time, and ‘overloads’ the SDL functions and the normal
SDL_Surface with #undefs and #define 's.

Pretty slick in some ways, if I’m allowed to say such of my own code :wink: - you
can SetVideoMode to 320 * 240 and get 240 * 320 and in general your source won’t
know the difference.

Some problems:

  1. As of current, the rotation is determined at compile-time. Not real-time.
    All the functions are heavily inlined etc. and I’d have to ditch that to get it
    to switch realtime. Email me if you

  2. SDL_LockSurface/SDL_UnlockSurface are currently unsupported.

  3. Bugz. Every once in awhile I stumble across and fix a new one, but there are
    surely still afew lurking.

  4. C++ codebase. Currently requires C++ because of -one- bit of code.
    Hopefully I can fix that eventually.

Email me if you want it in it’s current state.

“Corona688”:

Hm.

Working on Compaq iPaq, I’ve built a library for my own use that
autorotates all
images on load-time, and ‘overloads’ the SDL functions and the normal
SDL_Surface with #undefs and #define 's.

[…]

Email me if you want it in it’s current state.

Thank you very much, but I already decided to solve the problem in another
way,
using a scratch rotated surface for drawing and then blitting those surface
rotated back to screen.
It may seem a waste of memory, but it enough for me :slight_smile:

Here is my code, it may be useful for others, or maybe, somebody may suggest
improvement. :slight_smile:
I tested it only in 16 bpp. Sorry for the bad coding style :-/

/* Crate a new rotated surface for drawing */
SDL_Surface *CreateRotatedSurface(SDL_Surface *s)
{
return(SDL_CreateRGBSurface(s->flags, s->h, s->w,
s->format->BitsPerPixel,
s->format->Rmask,
s->format->Gmask,
s->format->Bmask,
s->format->Amask));
}

/* Used to copy the rotated scratch surface to the screen */
void BlitRotatedSurface(SDL_Surface *from, SDL_Surface *to)
{

int bpp = from->format->BytesPerPixel;
int w=from->w, h=from->h, pitch=to->pitch;
int i,j;
Uint8 *pfrom, *pto, *to0;

SDL_LockSurface(from);
SDL_LockSurface(to);
pfrom=(Uint8 *)from->pixels;
to0=(Uint8 *) to->pixels+pitch*(w-1);
for (i=0; i<h; i++)
{
    to0+=bpp;
    pto=to0;
    for (j=0; j<w; j++)
    {
        if (bpp==1) *pto=*pfrom;
        else if (bpp==2) *(Uint16 *)pto=*(Uint16 *)pfrom;
        else if (bpp==4) *(Uint32 *)pto=*(Uint32 *)pfrom;
        else if (bpp==3)
            {
                pto[0]=pfrom[0];
                pto[1]=pfrom[1];
                pto[2]=pfrom[2];
            }
        pfrom+=bpp;
        pto-=pitch;
    }
}
SDL_UnlockSurface(from);
SDL_UnlockSurface(to);

}

Usage example-------------

scratch=CreateRotatedSurface(screen);

/* draw operations on scratch surface */

BlitRotatedSurface(scratch, screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);