I've been trying to get masked_blit to work and having problem as shown in the screenshots.
{"name":"591204","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/9\/79cfdd4324691597e95b7cb2e6b14fec.jpg","w":646,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/9\/79cfdd4324691597e95b7cb2e6b14fec"}
{"name":"591205","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/5\/05980b83476c03fb74aebbd6bf06fe99.jpg","w":646,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/5\/05980b83476c03fb74aebbd6bf06fe99"}
The 1st image is all blit(...);
The 2nd image is using masked_blit(...); and blit(...) for the background image which shows up clear. Does anyone have an suggestions on how to get this to work?
My transparent color is (255, 0, 255).
Uh, I'm not sure whats going on. What color depth are the pictures? What color depth are you setting when you start allegro( set_color_depth )? Are you loading the pictures before you call set_gfx_mode ?
set_color_depth(32);
And I am setting it before I call
int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
And I load the graphics after this setup. In fact the icons that are loading are the same for those using blut() and those using masked_blit()
Can you attach the original sprites themselves? What version of Allegro are you using?
Can you paste the exact code you used when you call blit and masked_blit?
I've attached the 24bit tank.bmp icon created in MS Paint.
As for my blit / masked_blit code..(buffer is my backbuffer which writes to the screen, same buffer that is used for blit(), which works fine.)
void drawPlayer() { if(Player.alive) { int x = Player.x; int y = Player.y; if(MASKED) masked_blit(player_bmp, buffer, 0, 0, x, y, PLAYER_SIZE, PLAYER_SIZE); else blit(player_bmp, buffer, 0, 0, x, y, PLAYER_SIZE, PLAYER_SIZE); } }
EDIT: I'm using Allegro 4.2
You have (255, 0, 128)rgb on that tank bitmap. Also for masked_blit to work, the source and the target bitmap have to have the same color depth, though if you load the bitmap after setting the gfx mode it should be automatically converted to the set color depth. If it still doesn't work after changing the (not quite)bright pink to (255, 0, 255)rgb, try saving your sprites in 32bpp.
The loaded images have a different color depth than the buffer. Make sure you load/create them all after setting the gfx mode (which you do after setting the color depth).
Whys your project called Week 5?
The only image that didn't have a 255, 0, 255 background was the one I picked to post here. Glad I found that error. I changed the background for the tank image to 255, 0, 255 and still no go. I also have a background that loads when Masking is turned on. It appears something is forcing my color depth to go 256-pal mode from the look of the background. Also to note, each image is loaded from a 24-bit BMP that is not animated. Is it still required to use create the images after loading? If so, how would I create a static image from one that is been loaded and to ensure the 24 - 36 bit colors are retained?
I call it Week 5, because I'm making a game a week in class. This project has already been completed and turned in. But, I need to clear up my masking problem for future projects, which will heavily depend upon masked sprites.
Post the smallest code sample the duplicates the problem and we'll tell you what's wrong with it.
Here is my game setup function.
1 | void setupGame() |
2 | { |
3 | // set video mode |
4 | set_color_depth(24); |
5 | // init video mode to 640x480 |
6 | int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); |
7 | if(ret != 0) { |
8 | allegro_message(allegro_error); |
9 | return; |
10 | } |
11 | // install a digital sound driver |
12 | if(install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0) { |
13 | allegro_message("Error initializing sound system"); |
14 | return; |
15 | } |
16 | |
17 | // load background |
18 | if(MASKED) |
19 | background_bmp = load_bitmap("gfx/background.bmp", NULL); |
20 | |
21 | // load sounds |
22 | tank_snd = load_sample("sound/tank.wav"); |
23 | invader_snd = load_sample("sound/invader.wav"); |
24 | rocket_snd = load_sample("sound/rocket.wav"); |
25 | ufo_snd = load_sample("sound/ufo.wav"); |
26 | gameover_snd = load_sample("sound/gameover.wav"); |
27 | invadersMove_snd = load_sample("sound/invadersMove.wav"); |
28 | |
29 | // Grab High score |
30 | ifstream file("highscore.dat", ios_base::binary); |
31 | if(file.is_open()) |
32 | { |
33 | file >> highscore; |
34 | file.close(); |
35 | } |
36 | else |
37 | { |
38 | ofstream outfile("highscore.dat"); |
39 | outfile << (0); |
40 | highscore = 0; |
41 | outfile.close(); |
42 | } |
43 | } |
This is how I'm loading the player bitmap
player_bmp = load_bitmap("gfx/tank.bmp", NULL);
And this is the code that draws the player to the screen
void drawPlayer() { if(Player.alive) { int x = Player.x; int y = Player.y; if(MASKED) masked_blit(player_bmp, buffer, 0, 0, x, y, PLAYER_SIZE, PLAYER_SIZE); else blit(player_bmp, buffer, 0, 0, x, y, PLAYER_SIZE, PLAYER_SIZE); } }
Keep in mind that the MASKED is a #define that makes it easier to turn masking on/off.
That isn't the smallest code sample.
Let me rephrase my request. Post the smallest example program (ie, code that compiles on its own) along with the graphics that reproduces the problem.
When you boot up your graphics mode, add a textout or textprintf line to show you the results of a get_color_depth() call. It sounds to me that your computer doesn't like 24-Bit or 32-Bit graphics modes. (Which can sometimes be caused by incorrect video or monitor drivers.)
In 8-bit mode, the background colour needs to be palette entry 0, which would be one way to explain why the magic pink isn't disappearing.
Also, if it isn't liking 24-Bit or 32-Bit colour, try running the program in a window and you may have better luck.
EDIT: Oh wait... you are, aren't you? Maybe you should try full-screen then...
EDIT: Waitaminute... I think I recognize this problem. I believe masked_blit() requires that the bitmaps and the screen be the same colour depth... the images look as though you're trying to blit 32-Bit data onto an 8 or 16-bit surface. Is your desktop colour depth set to 8 or 16-Bit?
--- Kris Asick (Gemini)
--- http://www.pixelships.com
I've ensured all graphics I'm using are set to 24bit RGB color mode. The effects on the star background are not there originally as it should be blended better as a 24bit color image. This caused me to think even though I'm setting the color mode its not taking it.
I've tested the binary on 2 other computers and all of them come up with the same 2nd image when I use masked_blit();
I'll give you textout idea a try and see what I get.
I just added the textout and it comes back as 24 mode, however, still getting the same issue. 24bit mode is also what I'm setting it at. In addition, I noticed from reading around that there may be a problem with using backbuffer with masked_blit. Has anyone else heard of this issue?
Does draw_sprite() do the same thing as masked_blit() when you use it instead? (The two functions are almost identical in functionality.)
--- Kris Asick (Gemini)
--- http://www.pixelships.com
Nope. draw_sprite doesn't do any magical conversions of the source bitmap.
As an update, I was testing out various things and decided to set it to 8-bit 256-pal mode. I also took the non-animated sprite and converted it to an 8bit image with 0 for the masking. Surprisingly, the masking worked fine, however, the image itself was still distorted.
Someone suggested I try to set color to 32bit and use TGA instead of BMP. I'm going to give this a try later tonight.
Just wanted to mention the 8bit test in case this clues anyone in to what my problem may be. It really seems like something is force me into 8bit PAL mode, but I have set everything to 24bit / 32bit and pass NULL for the PAL info.
How's that small test program coming along that we can run to duplicate the problem?
Ok Mr. Evert,
I've gotten around to making a Test program and got it working, but the masking is not.
{"name":"591228","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/7\/a71b1e85f28e77601db624ee5a17ea47.jpg","w":641,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/7\/a71b1e85f28e77601db624ee5a17ea47"}
As you can see, the problem is still there. It should be the tank icon over a green field. Everything is grey.
{"name":"591229","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/7\/676c9e9f40766c305cc403d79f7c0f5e.png","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/7\/676c9e9f40766c305cc403d79f7c0f5e"} 591230
1 | // main.cpp |
2 | // Test project to determine why Masking is not working |
3 | // for my images. |
4 | |
5 | #include "allegro.h" |
6 | |
7 | #define MODE GFX_AUTODETECT_WINDOWED |
8 | #define WIDTH 640 |
9 | #define HEIGHT 480 |
10 | #define PLAYER_SIZE 32 |
11 | #define BLACK makecol(0,0,0) |
12 | #define BACKGROUND "gfx/background.bmp" |
13 | #define TANK "gfx/tank.bmp" |
14 | |
15 | // BackBuffer |
16 | BITMAP *buffer; |
17 | |
18 | // Sprite Maps |
19 | BITMAP *background_bmp; |
20 | BITMAP *player_bmp; |
21 | |
22 | void setupGame() |
23 | { |
24 | // set video mode |
25 | set_color_depth(32); |
26 | // init video mode to 640x480 |
27 | int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); |
28 | if(ret != 0) { |
29 | allegro_message(allegro_error); |
30 | return; |
31 | } |
32 | // load background |
33 | background_bmp = load_bitmap(BACKGROUND, NULL); |
34 | } |
35 | void setupPlayer() |
36 | { |
37 | player_bmp = load_bitmap(TANK, NULL); |
38 | } |
39 | void drawPlayer() |
40 | { |
41 | masked_blit(player_bmp, buffer, 0, 0, 320, 240, PLAYER_SIZE, PLAYER_SIZE); |
42 | } |
43 | void clearScreen() |
44 | { |
45 | blit(background_bmp, buffer, 0, 0, 0, 0, WIDTH - 1, HEIGHT - 1); |
46 | } |
47 | void cleanUp() |
48 | { |
49 | void destroy_bitmap(BITMAP *background_bmp); |
50 | void destroy_bitmap(BITMAP *player_bmp); |
51 | void destroy_bitmap(BITMAP *buffer); |
52 | } |
53 | int main(void) |
54 | { |
55 | // init Allegro |
56 | allegro_init(); |
57 | // init keyboard |
58 | install_keyboard(); |
59 | buffer = create_bitmap(WIDTH - 1, HEIGHT - 1); |
60 | // game setup |
61 | setupGame(); |
62 | setupPlayer(); |
63 | |
64 | // game loop |
65 | while(!key[KEY_ESC]) |
66 | { |
67 | // clear screen / update text |
68 | clearScreen(); |
69 | drawPlayer(); |
70 | // update screen |
71 | blit(buffer, screen, 0,0,0,0,WIDTH - 1,HEIGHT - 1); |
72 | |
73 | rest(5); |
74 | } |
75 | // end program |
76 | cleanUp(); |
77 | allegro_exit(); |
78 | return 0; |
79 | } |
80 | END_OF_MAIN() |
Make sure you load/create them all after setting the gfx mode
There, see? Didn't take long, did it?
Ok Mr. Evert,
Watch it.
Watch it.
Low self esteem, or do you just not like being called Mr.?
Is Mr an insult these days?
#define BLACK makecol(0,0,0)
Is it okay to call allegro functions before allegro_init() ?
Functions placed in macros aren't called until the macro is used.
Think of the preprocessor as a big search and replace engine. The compiler only sees what happens after the search and replace takes place.
So "BLACK" gets translated into makecol(0,0,0) where ever its used, before it hits the compiler, thus, the compiler only sees a bunch of makecol calls.
edit, but to answer your question, generally no, things wont work if allegro hasn't been initialized.
Low self esteem, or do you just not like being called Mr.?
On the conterary on both parts. However, I felt the response in this case to be unjustly sarcastic.
I didn't think there was enough context to tell if it was sarcastic at all.
And for the last goddamn time, BMP IS NOT A WEB-APPROPRIATE IMAGE FORMAT.
I didn't think there was enough context to tell if it was sarcastic at all.
I did. How would you take suddenly being called "Mr. Fjellstrom"?
Not that it matters much either way; the important things is that he posted the example program and got an answer to what was wrong with it.
How would you take suddenly being called "Mr. Fjellstrom"?
I dunno, I don't think I'd think it was sarcasm right away. the tone in the rest of the message would tell me either way.
Well, the rest of the message was fine. The opening ticked me off a little. Meh, never mind.
EDIT: I should perhaps mention, the change that Kitty Cat pointed out does actually fix the problem.
Kitty Cat's got it right. You can't call create_bitmap() until after you have selected a video mode, otherwise the colour depth of the bitmap might not match the colour depth of the screen.
If you want to, you can get around this using create_bitmap_ex() which allows you to specify the colour depth of any bitmap you create.
--- Kris Asick (Gemini)
--- http://www.pixelships.com
If you want to, you can get around this using create_bitmap_ex() which allows you to specify the colour depth of any bitmap you create.
That still might not do it. Allegro creates bitmaps in the same ordering (BGR RGB) as the card uses, and the default before a mode it set is BGR iirc, so if a card uses RGB, you'll have a BGR bitmap and a RGB screen.
Thanks Kitty Cat and Evert for the suggestion (so hard feelings mate)!!!
Something so easy too.. got it to work.
I mentioned the solution in the first post. Oh wel :-p
kazzmir,
Sorry.. I guess you can give the man an answer, but if he doesn't know the language, its pointless..
To build upon Thomas's reply regarding makecol before allegro_init(), it's also worth noting you may run into issues if you use makecol, load_bitmap, etc before setting the color depth and/or graphics mode.
Maybe it'd be worth mentioning that in the docs.
Initialises the Allegro library. You must call either this or allegro_init() before doing anything other than using the Unicode routines. If you want to use a text mode other than UTF-8, you can set it with set_uformat() before you call this. The other functions that can be called before this one will be marked explicitly in the documentation, like set_config_file().
http://www.allegro.cc/manual/api/using-allegro/install_allegro