Sign in to follow this  
Possumdude0

Strange error

Recommended Posts

I'm getting a weird error in my program. I tried making a map that loads via a bitmap image, but a problem arose. Here's the code: #include <allegro.h> BITMAP* buffer; BITMAP* World_Map; BITMAP* Grass; BITMAP* Water; BITMAP* Tree; BITMAP* Rocks; BITMAP* Gold; BITMAP* Player_Hall; BITMAP* Knights_Hall; BITMAP* Woods_Hall; BITMAP* Plains_Hall; int Map[100][100]; int Color_Value; int r; int b; int g; void Draw_Map(){ clear_to_color(buffer, makecol(0, 0, 0)); for (int y = 0; y < 20; y++){ for (int x = 0; x < 15; x++){ if (Map[y][x] == 1) draw_sprite(buffer, Grass, x * 32, y * 32); if (Map[y][x] == 2){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Tree, x * 32, y * 32); }else if (Map[y][x] == 3){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Rocks, x * 32, y * 32); }else if (Map[y][x] == 4){ draw_sprite(buffer, Water, x * 32, y * 32); }else if (Map[y][x] == 5){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Gold, x * 32, y * 32); }else if (Map[y][x] == 6){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Player_Hall, x * 32, y * 32); }else if (Map[y][x] == 7){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Knights_Hall, x * 32, y * 32); }else if (Map[y][x] == 8){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Woods_Hall, x * 32, y * 32); }else if (Map[y][x] == 9){ draw_sprite(buffer, Grass, x * 32, y * 32); draw_sprite(buffer, Plains_Hall, x * 32, y * 32); } } } draw_sprite(screen, buffer, 0, 0); } int main(){ allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); World_Map = load_bitmap( "World Map.bmp", NULL); buffer = create_bitmap(640, 480); Grass = load_bitmap( "Grass.bmp", NULL); Tree = load_bitmap( "Tree.bmp", NULL); Rocks = load_bitmap( "Rocks.bmp", NULL); Water = load_bitmap( "Water.bmp", NULL); Gold = load_bitmap( "Gold.bmp", NULL); Player_Hall = load_bitmap( "Player Hall.bmp", NULL); Knights_Hall = load_bitmap( "Knights Hall.bmp", NULL); Woods_Hall = load_bitmap( "Woodsman Hall.bmp", NULL); Plains_Hall = load_bitmap( "Plainsman Hall.bmp", NULL); // Error seems to be in this section, // which uses the World_Map bitmap to // fill the Map array. Only the Grass value (1) // seems to be registering for (int y = 0; y < 100; y++){ for (int x = 0; x < 100; x++){ Color_Value = getpixel(World_Map, x, y); r = getr(Color_Value); g = getg(Color_Value); b = getb(Color_Value); if (r == 0 && g == 255 && b == 0) Map[y][x] = 1; else if (r == 0 && g == 128 && b == 0) Map[y][x] = 2; else if (r == 192 && g == 192 && b == 192) Map[y][x] = 3; else if (r == 0 && g == 0 && b == 255) Map[y][x] = 4; else if (r == 255 && g == 255 && b == 0) Map[y][x] = 5; else if (r == 128 && g == 0 && b == 0) Map[y][x] = 6; else if (r == 0 && g == 0 && b == 0) Map[y][x] = 7; else if (r == 0 && g == 128 && b == 255) Map[y][x] = 8; else if (r == 128 && g == 64 && b == 0) Map[y][x] = 9; // 1 = grass // 2 = tree // 3 = rocks // 4 = water // 5 = gold // 6 = playerhall // 7 = knighthall // 8 = woodhall // 9 = plainhall } } Draw_Map(); readkey(); return 0; } END_OF_MAIN() Okay, so the Map array isn't filled right. Only the Grass (and maybe the Water) is stored into the array. I've checked and rechecked the color values, using Paint, andthey're right. If I change the code so that an rgb of 0, 255, 0 = 2 then the trees display right. So it seems that the code isn't reading the rgb values of the colors right. P.S. I just thought of something. Paint saves in 24 bit color mode, and my program is 16 bit, is that the problem?

Share this post


Link to post
Share on other sites
It doesn't matter that your program is 16 bit.

Are you able to step through the map loop? Maybe try printing out all the rgb values as you loop through them. Maybe there's a bug in load_bitmap, getpixel, or one of the getr/g/b functions?

sam

Share this post


Link to post
Share on other sites
Ow, my eyes! Please use [ source ] tags, it makes your code readable. [smile]

It looks like you're loading a bitmap file and using exact comparisons on its pixels to fill your map tile array. In this case, color depth certainly does matter. When Allegro loads a bitmap, it will convert it to the current color depth; if you have a 24bpp image that you're loading in Allegro with the a color depth of 16bpp, it will reduce the bitmap data to 16bpp and there will definitely be some loss of color information. This would likely cause your exact comparisons to fail.

Since you're not actually displaying this bitmap, probably the easiest thing to do would be to call set_color_conversion(COLORCONV_NONE) before loading your tilemap bitmap. This will tell Allegro not to perform any color conversions on any future calls to image-loading functions. This should ensure that your bitmap's pixels will have precisely the values you want, so your comparisons should work.

Just make sure to call set_color_conversion again with COLORCONV_TOTAL after loading your tilemap, so that any future bitmaps loaded will actually be displayable.

Share this post


Link to post
Share on other sites
[EDIT - Mr Accident got there first, and seems to know more about allegro than me [smile]]

I'm not sure I agree with izzo that it doesn't matter that your program is 16 bit. If allegro is loading a 24 bit image into a 16 bit surface, surely it is inevitable that some of the colour information will be "lost" in that process? Based on what you say about changing some of the colours and finding some work and some don't, that would seem a likely explanation to me.

Have you tried just (perhaps temporarily) setting your application to work with 24 or 32 bit images?

[opinion]
I'd also point out that I don't think images are a good way to represent maps like this and you'd be far better off getting hold of one of the excellent tile map editors available (google TileStudio or Mappy) and reading your map info from a dedicated file.
[/opinion]

Share this post


Link to post
Share on other sites
Oh, and as long as EasilyConfused brought it up, I just have to agree; using a bitmap to store your tilemap data does seem kinda awkward. It's fairly simple, I suppose, but I would think editing your tilemaps in paint would be a bit of a pain. (For one, you don't get to see what your map looks like while you're editing it...) I guess it's not a big deal for a tileset as simple as the one you're using, but if you plan to expand, I would definitely look into one of the options EasilyConfused recommended, or maybe you could have a shot at writing a more advanced tiling system of your own.

But that's just my opinion, of course. [grin]

Share this post


Link to post
Share on other sites
MrAccident - I'm not sure how to use source tags, sorry for that.

I did try redoing the map in 16bpp, but it still doesn't work, and yes, I rechecked the rgb values.

Yes, I am aware that using a bitmap to hold my map information is crude, sloppy, counter-productive, and a lot of other things, but I only had a few tiles and didn't plan to keep it in my game. Now it's just a matter of pride to make it work.

Share this post


Link to post
Share on other sites
Sorry, I don't know anything about Allegro. I assumed that calling load_bitmap would load the image from disk into a BITMAP structure without any colour conversion. Perhaps it is doing a conversion on load (sounds a bit dodgy and underhanded to me!).

Have you tried MrAccident's suggestion about calling set_color_conversion?

sam

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this