trouble loading bitmaps in allegro [Archive] - C Board

PDA

View Full Version : trouble loading bitmaps in allegro


bobish
02-15-2002, 01:26 PM
heres my code it currently allows you to move a circle around the screen but i want to replace the circle with a bitmap image

#define left 1
#define right 2
#define up 3
#define down 4
#define UpandLeft 5
#define DownandLeft 6
#define UpandRight 7
#define DownandRight 8

#include <stdio.h>
#include <allegro.h>

PALETTE *Pal;
BITMAP *ImageBuffer;

int main(void)
{
BITMAP *dblbuffer;

int x=100;
int y=100;
int lastkeypressed=0;
allegro_init();

install_keyboard();
install_timer(); /* needed for `rest' function */

// set_color_depth(24);
if (set_gfx_mode(GFX_SAFE, 800, 600, 0, 0) < 0)
{
printf("%s\n", allegro_error);
exit(1);
}

clear(screen);

// Pay attention!!! We HAVE to allocate memory for our memory bitmap
dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
clear(dblbuffer);

ImageBuffer = load_bmp("Eric.bmp", Pal);

while(1)// main loop
{
/////////////////// input
if (key[KEY_LEFT])
{
x--;
lastkeypressed=left;
}

if (key[KEY_RIGHT])
{
x++;
lastkeypressed=right;
}

if (key[KEY_UP])
{
y--;
lastkeypressed=up;
}

if (key[KEY_DOWN])
{
y++;
lastkeypressed=down;
}

if (key[KEY_LEFT] && key[KEY_UP])
{
lastkeypressed=UpandLeft;
}

if (key[KEY_LEFT] && key[KEY_DOWN])
{
lastkeypressed=DownandLeft;
}

if (key[KEY_RIGHT] && key[KEY_UP])
{
lastkeypressed=UpandRight;
}

if (key[KEY_RIGHT] && key[KEY_DOWN])
{
lastkeypressed=DownandRight;
}

//////////////////output


if (lastkeypressed=right)
{
circlefill(dblbuffer, x - 1 , y , 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=left)
{
circlefill(dblbuffer, x + 1 , y , 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=up)
{
circlefill(dblbuffer, x , y + 1, 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=down)
{
circlefill(dblbuffer, x , y - 1, 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=UpandLeft)
{
circlefill(dblbuffer, x + 1 , y + 1, 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=DownandLeft)
{
circlefill(dblbuffer, x + 1 , y - 1, 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=UpandRight)
{
circlefill(dblbuffer, x - 1 , y + 1, 50, makecol(000,000,000)); /* erase from last place */
}

if (lastkeypressed=DownandRight)
{
circlefill(dblbuffer, x - 1 , y - 1, 50, makecol(000,000,000)); /* erase from last place */
}

circlefill(dblbuffer, x , y, 50, makecol(255,255,255)); /* redraw at new place */

vsync();
blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);


if (key[KEY_ESC])//clean up and quit
{
// Now, remember to FREE the memory you previously allocated
destroy_bitmap(dblbuffer);
exit(1);
}
}
return 0;
}
END_OF_MAIN();

heres the warnings i got:41) : warning C4047: 'function' : 'struct RGB *' differs in levels of indirection from 'struct RGB (*)[256]'
and
41) : warning C4024: 'load_bmp' : different types for formal and actual parameter 2

also set color depth seems to make the program windowed is there anyway i can stop that and lastly is anyone have anyrecomendations for improving my code. im using msvc++6.0 windows ME and allegro 4.00

Justin W
02-16-2002, 02:25 PM
ImageBuffer = load_bmp("Eric.bmp", Pal);

Should be changed to:

ImageBuffer = load_bmp("Eric.bmp", &Pal);

If your video card does not support 24 bit graphics, it will change to 32 bit graphics with an emulated 24 bit graphics window. Just change set_color_depth(24); to set_color_depth(32); or set_color_depth(16); to ensure it is full screen (also you can use the GFX_AUTODETECT_FULLSCREEN parameter in set_gfx_mode(...) to make sure you get an error if it can't do it full screen - instead of using a windowed screen).

The lines:

dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
clear(dblbuffer);

is not necessary because you use load_bmp on dblbuffer, which allocates your memory for you anyway.

Well, that should be about it for now. Hope it helps. :)

-Justin

Bobish
02-17-2002, 01:08 PM
if thats the case then should i use 32 bit tga files instead of 24 bit bmp files or does it not really matter (the thing is converting files out of their normal format seems like a waste to me)

bobish
02-17-2002, 08:12 PM
oh and how do i make those annying warning messages go away (and i don't mean tell the compiler to ignore them)

Justin W
02-18-2002, 12:28 PM
Color depths are downward compatible. 32 will use 24 bit bitmaps just fine. 16 bit mode with 24 bit bitmap will attempt to convert right, but may look a bit strange depending upon the colors used.

What are the warning messages? I don't usually get any. (edit: PS, I'm going to be gone for the next week, so sorry if I don't respond for a while again.)

Bobish
02-22-2002, 07:26 PM
well still don't know whats wrong and id like to add that if I try to load two bitmaps the program won't even run

Justin W
02-27-2002, 06:26 AM
What are the errors (or warnings)? What exactly happens when you load a second picture? Is that where it is crashing, or when you use it? Please post a snippet of that code.

Unregistered
02-27-2002, 06:20 PM
heres the warnings

--------------------Configuration: allegrotest - Win32 Debug--------------------
Compiling...
test.c
c:\programming stuff\allegrotest\test.c(53) : warning C4047: 'function' : 'struct RGB *' differs in levels of indirection from 'struct RGB (** )[256]'
c:\programming stuff\allegrotest\test.c(53) : warning C4024: 'load_bmp' : different types for formal and actual parameter 2
c:\programming stuff\allegrotest\test.c(54) : warning C4047: 'function' : 'struct RGB *' differs in levels of indirection from 'struct RGB (** )[256]'
c:\programming stuff\allegrotest\test.c(54) : warning C4024: 'load_bmp' : different types for formal and actual parameter 2
Linking...

allegrotest.exe - 0 error(s), 4 warning(s)

note that since my code has changed the line numbers mentioned are differn't then in the posted code

ImageBuffer = load_bmp("Eric.bmp", &Pal); is where the first on is occuring though

and i solved the problem, with loading two bitmaps I hadent relized u need one pallet per bitmap.

Bobish
02-27-2002, 06:24 PM
oh sorry that last post was me

Justin W
02-27-2002, 09:17 PM
OK, here's a better way to do the Pal thing. Please forgive the somewhat erronious response I gave before.

Instead of:
PALETTE *Pal;
...
ImageBuffer = load_bmp("pic.bmp", &Pal);

It should be:
PALETTE Pal;
...
ImageBuffer = load_bmp("pic.bmp", Pal);


I believe that will clear up those warnings (if not we'll get to the bottom of it anyway). Also, you should definitely be able to reuse Pal for multiple load_bmp() calls.

Hope that helps.

-Justin

Bobish
02-28-2002, 06:07 PM
okay it works nowm thanks. We should probably tell this to hydro how seemed to be having the same problem as me. man is msvc++ 6 annoying at times