Sdl_gfx

Trying to set it up so an image rotate based on a key command with
SDL_gfx. I have the bitmap image on a surface and rotate it based on the
left or right key. Should I be using SDL_Rect with the rotated image? How
should I have it set up to draw this rotated image within the window with
sdl_gfx?

-Jamie

code:
#include <SDL/SDL.h>
#include <SDL/SDL_rotozoom.h>
#include

SDL_Surface *turret;

int InitImages()
{
turret = SDL_LoadBMP(“images/turret.bmp”);
SDL_SetColorKey(turret, SDL_SRCCOLORKEY, SDL_MapRGB(turret->format, 255,
255, 255));

return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawScene()
{
DrawIMG(turret, 205, 339);

SDL_Flip(screen);
}

int main(int argc, char argv[])
{
Uint8
keys;

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(630,394,32,SDL_HWSURFACE|SDL_DOUBLEBUF);

InitImages();

int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
  if ( event.type == SDL_QUIT )  {  done = 1;  }

  if ( event.type == SDL_KEYDOWN )
  {
    if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
  }
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_LEFT] )
{
turret = rotozoomSurface(turret, 5.0, 0.0, 0);
}
if ( keys[SDLK_RIGHT] )
{
turret = rotozoomSurface(turret, 0.0, 0.0, 0);
}
DrawScene();

return 0;
}

Jamie,

the rotozoom routine creates a new surface. You usually use it like this
(pseudocode):

source_surface=loadimage(image.png)
steps=10
for step=0 to (steps-1) do
angle=step*360/steps
rotated_surfaces[step] = rotozoom(source_surface, angle, …)

This pre-rotates your images and caches them for later use. This works
well for smaller images since caching uses memory. To get good quality
your input should be 32bit (i.e. PNG with transparency) and
interpolation should be enabled in the rotozoom routine. A smarter
approach would be to cache only a few images relevant to the current
game state (i.e. angle-20, angle-10, angle, angle+10, angle+20) and
re-render to update the cache as needed when the game state is changed -
that way you can optimize to save a rotozoom for every single state change.

Once you have the new surface, you can determine its center from its
dimensions and blit as usual using clipping to limit its visibility to a
rectangular area.

Hope that helps
Andreas>Trying to set it up so an image rotate based on a key command with

SDL_gfx. I have the bitmap image on a surface and rotate it based on the
left or right key. Should I be using SDL_Rect with the rotated image? How
should I have it set up to draw this rotated image within the window with
sdl_gfx?

-Jamie

code:
#include <SDL/SDL.h>
#include <SDL/SDL_rotozoom.h>
#include

SDL_Surface *turret;

int InitImages()
{
turret = SDL_LoadBMP(“images/turret.bmp”);
SDL_SetColorKey(turret, SDL_SRCCOLORKEY, SDL_MapRGB(turret->format, 255,
255, 255));

return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawScene()
{
DrawIMG(turret, 205, 339);

SDL_Flip(screen);
}

int main(int argc, char argv[])
{
Uint8
keys;

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(630,394,32,SDL_HWSURFACE|SDL_DOUBLEBUF);

InitImages();

int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

 if ( event.type == SDL_KEYDOWN )
 {
   if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
 }

}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_LEFT] )
{
turret = rotozoomSurface(turret, 5.0, 0.0, 0);
}
if ( keys[SDLK_RIGHT] )
{
turret = rotozoomSurface(turret, 0.0, 0.0, 0);
}
DrawScene();

return 0;
}


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