Tuesday, April 3, 2012

Rpg Client update: Rotate players

Welcome back!


Player's point of view is quite clear now...

Since I decided to continue the project I started to think again in all the things I could make to improve it, and the first thing was to  make it clear where players / foes were looking at, it's basic to make you know:
  1. Where are you going (the red dot did not helped a lot)
  2. Where are going all the other players / enemies
  3. Can I ambush somebody?, am I being ambushed?
To make client rotate images I just needed a function to rotate a tile certain angle, I didn't had to modify the server code because I already included angle in the players data, I thought how to rotate that and finally I decided to use a library from a good friend that was open source and based in the standard library SGE:

SPRIG: The SDL Primitive Generator

A great job from Jhonny D

To use the library you will need the subversion client, (in linux "sudo apt-get install subversion", in Windows you can use Tortoise SVN client, for example)

To download / install it under linux:
svn checkout http://sprig.googlecode.com/svn/trunk/ sprig
cd sprig
make
sudo make install

(Haven't tested it under windows yet)

I created a new function: RotateBlitTileset( x,y,image,angle), that functions draws the rotated tile (image) at x,y.
void Engine::RotateBlitTileset(int x, int y, int tile, float Angle) //Rotate and Blits a tile from our tileset to the screen
{
    SDL_Surface * tmpBlit = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY |SDL_RLEACCEL,TileSize,TileSize,32,0,0,0,0); 
    SDL_Surface * tmpBlit2 =  SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY |SDL_RLEACCEL,TileSize,TileSize,32,0,0,0,0);
    //1-fill temporary surface with pink
    SDL_FillRect(tmpBlit, 0, SDL_MapRGB(tmpBlit->format, 255, 0, 255));

    start.x= (tile%10)*TileSize; //Column = tile % 8 (0-7)
    start.y= (tile/10)*TileSize; //Row = tile / 8 (0-7)
    start.w=TileSize;
    start.h=TileSize;

    end.x =0; // X
    end.y =0; // Y
    end.w = TileSize;
    end.h = TileSize;
    //2-Blit the desired tile to the temporary surface
    SDL_BlitSurface(tileset, &start, tmpBlit, &end); 
    //3-I call sprig function SPG_Rotate() to rotate the temporary surface and get the resulting surface into a 2nd temporary surface
    tmpBlit2 = SPG_Rotate(tmpBlit, Angle -90,  SDL_MapRGB(tmpBlit-> format, 255, 0, 255)); 
    SDL_SetColorKey(tmpBlit2,SDL_SRCCOLORKEY |SDL_RLEACCEL,SDL_MapRGB(tmpBlit2-> format, 255, 0, 255));//set the color key
    start.x=0;
    start.y=0;
    start.w=TileSize;
    start.h=TileSize;

    //set the cliping for the destination surface (the screen)
    end.x =x; // X
    end.y =y; // Y
    end.w = TileSize;
    end.h = TileSize;

    SDL_BlitSurface(tmpBlit2, NULL, screen, &end); //Blits the rotated tile to the screen
    SDL_FreeSurface( tmpBlit);
    SDL_FreeSurface( tmpBlit2);
}

Notice I can't just call Sprig rotate function because I would get a rotated tile with pink background, so I have to use the tmp surface to set the color-key, that way is not optimized at all so I will think in a way to make it faster later, but at the moment is ok.

In the next post: Let's chat!