Hey all, I used to be pretty good with Allegro, but this was about 4 or 5 years ago. A few days ago I decided to get back into it, but I'm having some issues. Basically what I'm tryin to do right now is display a basic tiled map and my character. However, when I display the character to be on top of the map, transparency does not work, and the pink surrounding box is still there. here's some code:
This is my main function where things are initialized and the character is loaded (it'll be broken down into more functions at a later time):
| 1 | int main(int argc, int *argv[]) |
| 2 | { |
| 3 | allegro_init(); |
| 4 | install_keyboard(); |
| 5 | set_color_depth(24); |
| 6 | if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0) //Defaulted to 800x600, may make scalable later |
| 7 | abort_on_error("Couldn't set graphic mode!"); |
| 8 | |
| 9 | BITMAP *bmp = create_bitmap(800,600); //main screen |
| 10 | DATAFILE *character_data = load_datafile("PCs.dat"); |
| 11 | BITMAP *character_buffer; |
| 12 | string versionNumber = "v0.01-alpha1"; |
| 13 | clear_bitmap(bmp); |
| 14 | if (!character_data) { |
| 15 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
| 16 | allegro_message("Error loading Character Datafile"); |
| 17 | return 1; |
| 18 | } |
| 19 | |
| 20 | acquire_screen(); |
| 21 | draw_map(); |
| 22 | character_buffer = create_bitmap(32,32); |
| 23 | clear_bitmap(character_buffer); |
| 24 | //draw_sprite(bmp, (BITMAP *)character_data[0].dat, 0, 0); |
| 25 | /* Blit to screen */ |
| 26 | |
| 27 | blit((BITMAP *)character_data[0].dat, screen, 0, 0,0,0,32,32); |
| 28 | |
| 29 | int size = text_length(font, versionNumber.c_str()); //display version number |
| 30 | textout_right_ex(screen, font, versionNumber.c_str(), SCREEN_W-10, SCREEN_H-10, makecol(255,255,255), -1); |
| 31 | release_screen(); |
| 32 | /* wait for a keypress */ |
| 33 | readkey(); |
| 34 | return 0; |
| 35 | } |
and here's the code for loading the map:
| 1 | void draw_map() |
| 2 | { |
| 3 | BITMAP *land = create_bitmap(32,32); |
| 4 | DATAFILE *landFile; |
| 5 | landFile = load_datafile("map.dat"); |
| 6 | clear_bitmap(land); |
| 7 | if (!landFile) { |
| 8 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
| 9 | allegro_message("Error loading Map\n"); |
| 10 | return; |
| 11 | } |
| 12 | int mapXtotal = 20 * 32; |
| 13 | int mapYtotal = 18 * 32; |
| 14 | for (int x = 0; x < mapXtotal; x = x+32){ |
| 15 | for (int y = 0; y <= mapYtotal; y = y+32){ |
| 16 | draw_sprite(land,(BITMAP *)landFile[0].dat,x,y); |
| 17 | blit(land,screen,0,0,x,y,32,32); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | } |
I've double checked grabber, and all images are listed being 24 bit, and according to photoshop the pink is (255,0,255). So does anyone know why the program displays this still:
{"name":"screen.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/2\/5263c0d496c025ef6133c2b1003fab27.jpg","w":808,"h":631,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/2\/5263c0d496c025ef6133c2b1003fab27"}
any help would be greatly appreciated!
blit((BITMAP *)character_data[0].dat, screen, 0, 0,0,0,32,32);
Is this the line that is drawing the guy? It needs to be a masked blit or draw sprite.
i originally had that, but i had some variables wrong. Now im getting closer, but instead of displaying Pink now, it displays black!
this is what i changed that snippet to:
draw_map(); character_buffer = create_bitmap(32,32); clear_bitmap(character_buffer); draw_sprite(character_buffer, (BITMAP *)character_data[0].dat, 0, 0); /* Blit to screen */ blit(character_buffer, screen, 0, 0,0,0,32,32);
any idea why all the pink area is black now? am I deleting that tile somehow?
I think clear_bitmap is making the background of the character_buffer black... try clear_to_color(character_buffer, makecol(255, 0, 255)) and draw_sprite'ing that to the screen. I suggest just having one buffer instead of a buffer for the character only though...
EDIT: Code:
draw_map(); character_buffer = create_bitmap(32,32); clear_to_color(character_buffer, makecol(255, 0, 255)); draw_sprite(character_buffer, (BITMAP *)character_data[0].dat, 0, 0); /* Blit to screen */ draw_sprite(screen, character_buffer, 0, 0);
EDIT2: Or, for your case, why not just draw_sprite directly to the screen..? Why're you using a buffer for the character?
Thanks Mokkan! You're fix worked. I changed my code to
draw_sprite(screen,(BITMAP *)character_data[0].dat,0,0);
and it worked perfectly!
What is so special about draw_sprite over masked_blit? I've always wondered that myself.
There is no longer such a thing as a SPRITE structure. The sprite drawing code now draws bitmaps onto other bitmaps, which is a much more flexible way of doing things. The datafile reader will convert all your old sprites to bitmaps as it loads them in, so the only practical difference this is likely to make is that the sprite drawing routines can't draw opaque sprites any more (but you can use a blit instead).
What is so special about draw_sprite over masked_blit?
Less variables to pass that you don't normally care about (the values I typically pass to masked_blit() are those that make it behave the same as draw_sprite()) and being able to do 8bit->any depth blits (masked_blit() cannot do colour conversions at all).