Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » masked_blit(); problem

This thread is locked; no one can reply to it. rss feed Print
 1   2 
masked_blit(); problem
Jon Ray
Member #5,340
December 2004
avatar

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"}591204
{"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"}591205

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).

kazzmir
Member #1,786
December 2001
avatar

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 ?

Jon Ray
Member #5,340
December 2004
avatar

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()

kazzmir
Member #1,786
December 2001
avatar

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?

Jon Ray
Member #5,340
December 2004
avatar

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

Dennis
Member #1,090
July 2003
avatar

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.

Kitty Cat
Member #2,815
October 2002
avatar

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).

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Dustin Dettmer
Member #3,935
October 2003
avatar

Whys your project called Week 5?

Jon Ray
Member #5,340
December 2004
avatar

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.

Evert
Member #794
November 2000
avatar

Post the smallest code sample the duplicates the problem and we'll tell you what's wrong with it.

Jon Ray
Member #5,340
December 2004
avatar

Here is my game setup function.

1void 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.

Evert
Member #794
November 2000
avatar

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.

Kris Asick
Member #1,424
July 2001

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

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Jon Ray
Member #5,340
December 2004
avatar

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?

Kris Asick
Member #1,424
July 2001

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

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Thomas Fjellstrom
Member #476
June 2000
avatar

Nope. draw_sprite doesn't do any magical conversions of the source bitmap.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Jon Ray
Member #5,340
December 2004
avatar

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.

Evert
Member #794
November 2000
avatar

How's that small test program coming along that we can run to duplicate the problem?

Jon Ray
Member #5,340
December 2004
avatar

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"}591228

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"}591229 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
16BITMAP *buffer;
17 
18// Sprite Maps
19BITMAP *background_bmp;
20BITMAP *player_bmp;
21 
22void 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}
35void setupPlayer()
36{
37 player_bmp = load_bitmap(TANK, NULL);
38}
39void drawPlayer()
40{
41 masked_blit(player_bmp, buffer, 0, 0, 320, 240, PLAYER_SIZE, PLAYER_SIZE);
42}
43void clearScreen()
44{
45 blit(background_bmp, buffer, 0, 0, 0, 0, WIDTH - 1, HEIGHT - 1);
46}
47void cleanUp()
48{
49 void destroy_bitmap(BITMAP *background_bmp);
50 void destroy_bitmap(BITMAP *player_bmp);
51 void destroy_bitmap(BITMAP *buffer);
52}
53int 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}
80END_OF_MAIN()

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

buffer = create_bitmap(WIDTH - 1, HEIGHT - 1);
// game setup
setupGame();

I said:

Make sure you load/create them all after setting the gfx mode

:P

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Evert
Member #794
November 2000
avatar

There, see? Didn't take long, did it?

Quote:

Ok Mr. Evert,

Watch it.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Watch it.

Low self esteem, or do you just not like being called Mr.?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

kentl
Member #2,905
November 2002

Is Mr an insult these days? ;D

William Labbett
Member #4,486
March 2004
avatar

#define BLACK makecol(0,0,0)

Is it okay to call allegro functions before allegro_init() ?

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 1   2 


Go to: