Public Group

# API for roguelike

This topic is 2826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was making a roguelike with SDL and got as far as making a random map and discovered that the API has no way to recolor a sprite.

I'd rather not work with the console window, but I still want the game to be mac and lunix compatible, what API would you suggest that I switch to?

##### Share on other sites
libtcod is a very popular one. Its used in many many roguelikes. http://doryen.eptalys.net/libtcod/

##### Share on other sites
Does not have a single function that does what you want is not the same as unable to do what you want. Simple sprite recoloring can be done by creating a compatible bitmap, filling it with the desired color and blitting one on top of the other with some version of alpha blending. Exact details would depend on the precise format of your sprites.

Why do you need to recolor a sprite for a roguelike anyways? Why not just render the text in the desired color?

##### Share on other sites
It's real easy to do per-pixel manipulation once you get used to it. Write your own functions. SDL provides fast per-pixel level access perfect for that kind of thing. (Maybe skim this tutorial if you don't know how)

However, if you are sure you want to switch, I recommend SFML.

##### Share on other sites

It's real easy to do per-pixel manipulation once you get used to it. Write your own functions. SDL provides fast per-pixel level access perfect for that kind of thing. (Maybe skim this tutorial if you don't know how)

However, if you are sure you want to switch, I recommend SFML.

Thanks that's what i was looking for, I'll read through it.

Why do you need to recolor a sprite for a roguelike anyways? Why not just render the text in the desired color?

Because I want to have support for tilesets the game is not going to use the console, instead i'm using a bitmaped font. The reason I want to recolor instead of just having differient sets of each color is because I want to be able to have characters of any color instead of just what colors i filled the sprite with, and that would be a lot of recoloring.

##### Share on other sites

Because I want to have support for tilesets the game is not going to use the console, instead i'm using a bitmaped font. The reason I want to recolor instead of just having differient sets of each color is because I want to be able to have characters of any color instead of just what colors i filled the sprite with, and that would be a lot of recoloring.

In that case:
Step 1) Create an image filled with the color you want.
Step 2) "Draw" the bitmap font over it, inverted, so the letter part doesn't get draw, but the black font background gets drawn over the blank filled image. Use the black (or whatever) background as the colorkey for the resulting image.

Doable in SDL, definitely.

My SDL is a bit rusty, but something like: [color=#ff0000][size=2](Warning! Untested code)
 SDL_Surface *CreateColoredSymbol(SDL_Surface *bitmapFont, SDL_Rect symbolRect, SDL_Color colorToMakeSymbol, SDL_Color bitmapFontBackgroundColor) { SDL_Surface *screen = SDL_GetVideoSurface(); //Forgot how to create a new surface, so used this code: http://lists.libsdl.org/pipermail/sdl-libsdl.org/2002-January/022521.html SDL_Surface *symbolImage = SDL_CreateRGBSurface(screen->flags, symbolRect.width, symbolRect.height, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); if(!emptyImage) { //Error! Use SDL_GetError() for the reason why. } //Fill the new image with the color passed in. if(SDL_FillRect (symbolImage, NULL, Map_RGB(symbolImage->format, colorToMakeSymbol.r, colorToMakeSymbol.g, colorToMakeSymbol.b)) == -1) { //Error! Use SDL_GetError() for the reason why. } //Draw the symbol over the new image. ( http://sdl.beuc.net/sdl.wiki/SDL_BlitSurface ) if(SDL_BlitSurface(bitmapFont, &symbolRect, symbolImage, NULL) == -1) { //Error! Use SDL_GetError() for the reason why. } //Set the color key for the new image. if(SDL_SetColorKey(symbolImage, SDL_SRCCOLORKEY, Map_RGB(symbolImage->format, bitmapFontBackgroundColor.r, bitmapFontBackgroundColor.g, bitmapFontBackgroundColor.b)) == -1) { //Error! Use SDL_GetError() for the reason why. } return symbolImage; }

You can then cache that image and hold onto it until you no longer need it, so you don't need to recreate it every frame.

Note: bitmapFont should have two colors in it. One is the color of the symbols themselves (we'll call this the Symbol Color), and the other is the color of the background the symbols are on (we'll call this the Background Color).

After initially loading 'bitmapFont', it should be SDL_SetColorKey() to the Symbol Color... making the symbols themselves invisible. The background should _not_ be color keyed, for this to work. This function assumes you already loaded bitmapFont and called SDL_SetColorKey() with the Symbol Color on the bitmapFont.

[Edit:] Oops: Ofcourse, the smarter thing would be to color the entire bitmap font, instead of each individual symbol, but the function can be easily adapted to do that (post if you need help). Then you can just have:

SDL_Surface *BlueSymbolSheet = CreateColoredBitmapFont(originalBitmapFont, colorToMakeFont, backgroundColorOfOriginalFont);
SDL_Surface *RedSymbolSheet = CreateColoredBitmapFont(originalBitmapFont, colorToMakeFont, backgroundColorOfOriginalFont);
SDL_Surface *WhiteSymbolSheet = CreateColoredBitmapFont(originalBitmapFont, colorToMakeFont, backgroundColorOfOriginalFont);
SDL_Surface *GreenSymbolSheet = CreateColoredBitmapFont(originalBitmapFont, colorToMakeFont, backgroundColorOfOriginalFont);

##### Share on other sites
It worked after I tweeked the code (it was SDL_MapRGB instead of Map_RGB)! Thanks for the help, you probably saved me a few days of work.

 SDL_Surface *CreateColoredSymbol(SDL_Surface *bitmapFont, SDL_Color colorToMakeSymbol, SDL_Color bitmapFontBackgroundColor) { SDL_Surface *screen = SDL_GetVideoSurface(); //Forgot how to create a new surface, so used this code: http://lists.libsdl.org/pipermail/sdl-libsdl.org/2002-January/022521.html SDL_Surface *symbolImage = SDL_CreateRGBSurface(screen->flags, 256, 256, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); if(!symbolImage) { //Error! Use SDL_GetError() for the reason why. } //Fill the new image with the color passed in. if(SDL_FillRect (symbolImage, NULL, SDL_MapRGB(symbolImage->format, colorToMakeSymbol.r, colorToMakeSymbol.g, colorToMakeSymbol.b)) == -1) { //Error! Use SDL_GetError() for the reason why. } //Draw the symbol over the new image. ( http://sdl.beuc.net/sdl.wiki/SDL_BlitSurface ) if(SDL_BlitSurface(bitmapFont, NULL , symbolImage, NULL) == -1) { //Error! Use SDL_GetError() for the reason why. } //Set the color key for the new image. if(SDL_SetColorKey(symbolImage, SDL_SRCCOLORKEY, SDL_MapRGB(symbolImage->format, bitmapFontBackgroundColor.r, bitmapFontBackgroundColor.g, bitmapFontBackgroundColor.b)) == -1) { //Error! Use SDL_GetError() for the reason why. } return symbolImage; } 

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 18
• 13
• 9
• 9
• 25