Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » load_bitmap trouble

Credits go to CGamesPlay, Evert, and Kitty Cat for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
load_bitmap trouble
Raf Vermeulen
Member #7,166
April 2006

I also tried leaving it out of a subdirectory and just plain in the same directory as the executable (with putting this in the code and putting the image there, obviously). Same results.

I'll comment out everything except the most necessary and'll try to load and blit it without it being a part of a class.

The thing that still confuses me, is how it works fine for one project, but the same code doesn't work in another.

Anyways, on to the testing

CGamesPlay
Member #2,559
July 2002
avatar

The proper test case should look something like this:

1#define ALLEGRO_NO_MAGIC_MAIN
2#include <allegro.h>
3#include <iostream>
4 
5using namespace std;
6 
7int main(int argc, char* argv[])
8{
9 install_allegro(SYSTEM_NONE, &errno, atexit);
10 BITMAP* b = load_bitmap("test.bmp", NULL);
11 if(!b)
12 {
13 cout << "Bitmap failed to load" << endl;
14 return 0;
15 }
16 cout << "Bitmap loaded" << endl;
17 destroy_bitmap(b);
18 return 1;
19}

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Raf Vermeulen
Member #7,166
April 2006

That one says: "Bitmap loaded"

It crashes with this one:

1int main(int argc, char* argv[])
2{
3 install_allegro(SYSTEM_NONE, &errno, atexit);
4 set_color_depth(16);
5 if(set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0)) {
6 cout << "Problem with setting gfx mode\n";
7 system("pause");
8 exit(1);
9 }
10 BITMAP* b = load_bitmap("Gfx/Tile.bmp", NULL);
11 if(!b)
12 {
13 cout << "Bitmap failed to load" << endl;
14 return 0;
15 }
16 cout << "Bitmap loaded" << endl;
17 blit(b, screen, 0,0,0,0,800,600);
18 destroy_bitmap(b);
19 system("pause");
20 return 1;
21}

Result is "Problem with setting gfx mode".

CGamesPlay
Member #2,559
July 2002
avatar

It says "Bitmap loaded" when you use the Gfx/Tile.bmp from the attachment in the first post?

[append]
If you use SYSTEM_NONE, you can't set a graphics mode, or use a keyboard, or anything like that. Change the install_allegro line to a standard allegro_init line for that :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Raf Vermeulen
Member #7,166
April 2006

It loads and shows fine now (with the allegro_init()). The bitmap's the one from the attachment (the 10 testing floor-tiles and the crappy stickman).

I'll add the code one by one again and'll see where it goes wrong.

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

Result is "Problem with setting gfx mode".

What graphics mode is your desktop set to?

No, wait.

Why do you install the SYSTEM_NONE driver and expect it to give you any useful graphics driver?

Try this one (fixing some errors along the way):

1#include <allegro.h>
2#include <iostream> // may be required on some platforms for cout / cerr
3 
4int main(int argc, char* argv[])
5{
6 allegro_init(); // will pick the system driver for you; the preferred way of installing allegro unless you want to do platform-specific hacking
7 set_color_depth(16);
8 if(set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0)) {
9 cerr << "Problem with setting gfx mode" << endl; // error messages go into the error stream, and use endl over '\n'
10 // system("pause"); // no need for this, better exit right away
11 exit(1);
12 }
13 // it's probably a good idea to try other color depths if 16bpp fails, but I leave that as an exercise for the reader. Hint: the code is in the allegro manual.
14 install_keyboard(); // we need this for readkey() later on
15 BITMAP* b = load_bitmap("./Gfx/Tile.bmp", NULL); // makes sure the path is regarded as relative to the current directory
16 if(!b)
17 {
18 set_gfx_mode(GFX_TEXT, 80, 25, 0, 0); // set a text mode so that text output is visible
19 cerr << "Bitmap failed to load" << endl; // Error to cerr
20 return -1; // return nonzero on error
21 }
22 cout << "Bitmap loaded" << endl;
23 blit(b, screen, 0,0,0,0,800,600);
24 destroy_bitmap(b);
25 // system("pause");
26 // better:
27 readkey(); // waits for keyboard input
28 return 0; // return zero on successful termination
29}
30END_OF_MAIN() // needed by allegro's magic main()

--- EDIT ---
Beaten. Twice.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Raf Vermeulen
Member #7,166
April 2006

Ok... this's getting a bit odd. I'm at the point where it says spritesheet not loaded again. That's when I placed

  if(m_tile == 0) load_tileset("Gfx/Tile.bmp"); 
  if (!m_tile) cout << "Spritesheet not loaded\n";

in

Tile::Tile() : /* deze methode is sneller dan wanneer ze in de constructor staan */
size(32),
type(0) {
  if(m_tile == 0) load_tileset("Gfx/Tile.bmp"); 
  if (!m_tile) cout << "Spritesheet not loaded\n";     
}

This code (in the main loop, which I left in there as double-checking) still works fine:

    BITMAP* b = load_bitmap("Gfx/Tile.bmp", NULL);
    if(!b) {
        cout << "Bitmap failed to load" << endl;
        system("pause");
        return 0;
    }
    masked_blit(b, buffer, 0,0,0,0,32,32);
    cout << "Bitmap loaded" << endl;
    blit(b, screen, 0,0,0,0,800,600);

CGamesPlay
Member #2,559
July 2002
avatar

You must have a pointer error somewhere... I guess. I'm at a loss, really. Check valgrind or gdb :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Kitty Cat
Member #2,815
October 2002
avatar

The constructor isn't called globally, is it? It's called after allegro_init and set_gfx_mode?

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

Raf Vermeulen
Member #7,166
April 2006

I get the spritesheet not loaded comment before any other comment, and the global variables're declared above the main-loop, like this:

1#include "crops.h"
2 
3BITMAP *buffer;
4Tile tile;
5Tile *field[HEIGHT][WIDTH];
6BITMAP *Tile::m_tile;
7int player_x;
8int player_y;
9 
10 
11int main(int argc, char* argv[]) {
12 srand(time(0));
13 initiaze();
14 set_game();
15 BITMAP* b = load_bitmap("Gfx/Tile.bmp", NULL);
16 if(!b) {
17 cout << "Bitmap failed to load" << endl;
18 system("pause");
19 return 0;
20 }
21 masked_blit(b, buffer, 0,0,0,0,32,32);
22 cout << "Bitmap loaded" << endl;
23 blit(b, screen, 0,0,0,0,800,600);
24 destroy_bitmap(b);
25 destroy_bitmap(buffer);
26 system("pause");
27 return 1;
28}
29END_OF_MAIN()

Evert
Member #794
November 2000
avatar

If the constructor is called before allegro_init() is called, then you're calling an Allegro function before Allegro is initialised - which doesn't work.

Raf Vermeulen
Member #7,166
April 2006

Well I'll be damned. That seems to be the difference between the first project and this one:o

Changing the global var declaration part to this did the trick:

BITMAP *buffer;
BITMAP *Tile::m_tile;
Tile *field[HEIGHT][WIDTH];
int player_x;
int player_y;

It still doesn't draw properly, but I'll check all the rest too to make sure things're going as they should.

WOO! Fixed a few more flaws and it works like a charm now;D Time to do the actual stuff now... the fun part;D

 1   2 


Go to: