PDA

View Full Version : How to dynamically create sprites



rpwilson1
03-19-2008, 03:24 AM
I am in the process of creating a rougelike game. For those that have no idea what rougelike is, it is old style rpg games that use letters and symbols like *, |, and ~ to represent the characters, terrain, and buildings in the game. Think ASCII Art. :)

I've been looking at ClanLib since it has a very strong 2d slant and since it uses OpenGL by default, will run faster than SDL. I have run into the following issue: instead of creating hundreds of little image files, I was hoping to create the sprites dynamically and cache them to file. I've figured out that the following steps will be needed to create the sprite/s:

1. Create some form of canvas or buffer
2. Draw it's background as such-and-such color.
3. Render letter/symbol onto canvas with such-and-such color
4. Convert canvas into sprite.

I know it will end with a CL_Sprite, but the classes before that I am not so sure of. I think it will involve CL_SpriteDescription and CL_Pixelbuffer as well, but I am not so sure. I would appreciate it if someone could point me in the right direction. Thanks in advance!

Seth
03-19-2008, 10:03 AM
Sounds like a cool project.

I do some sort of similar tricks when I generate thumbnails. I don't know if this is the best way, but here is how I do it:

If you make a CL_Canvas, you can draw/render anything to it.

I make one like this:


m_pBackground = new CL_Surface(CL_PixelBuffer(x, y,x*4, CL_PixelFormat::abgr8888));
m_pBackgroundCanvas = new CL_Canvas(*m_pBackground);

Can draw lines:
m_pBackgroundCanvas->get_gc()->draw_line(0, 0, 100, 100, CL_Color::yellow);

Or text:
pMyFont->draw(0,0, "Hello world!", m_pBackgroundCanvas->get_gc());

When done, grab the part of the screen you want as a pixel buffer with m_pBackgroundCanvas->get_pixeldata(const CL_Rect &area).

You can use the pixel buffer to create surfaces or sprites or save out the data if you wanted to cache it to disk.

rpwilson1
03-19-2008, 05:49 PM
I played with the code you gave and it looks like it's exactly what I need. Your help is much appreciated! :D