Allegro bitmap is null even when initialized
Matias Persson

Hello folks I've come across this problem where my bitmap will not become initialized.
I've doubled checked my code and I can't see anything wrong, but it might be because I'm pretty tired atm.

Here is the relevant code;
Main.cpp - This isn't really relevant code, just another question here, is this a proper way of approaching what I am trying to accomplish? A small 2D game:

#include "initialize.h"

int main()
{
  const float FPS = 60.0;
  const float frameFPS = 10.0;

  initialize *init = new initialize();
  init->initSystem(FPS, frameFPS);

  return 0;
}

Inside initialize.cpp:

al_install_keyboard();
  al_init_image_addon();

  c = new Character("player.png", "Hero", 40, 40, 3);
  std::cout << c->getName() << ": " << c->getHealth() << " HP." << std::endl;

Character.cpp:

Character::Character(std::string filePath, std::string n, int var, int var2, int speed)
{
  image = al_load_bitmap(filePath.c_str());
  if(image == NULL)
  {
    std::cout << filePath.c_str() << " could not be loaded." << std::endl;
    al_destroy_bitmap(image);
  }

  name = n;
  maxHealth = 10, health = maxHealth;
  x = var, y = var2, this->speed = speed;
}

Edgar Reynaldo

Post the full code for the initialize constructor and for initSystem.

The loaded png may be null because it was not found.

See https://wiki.allegro.cc/index.php?title=Loading_Resources_%28Troubleshooting%29 for more help.

Matias Persson

No, the image is certainly there.. I will post the rest of the code in 2-3 minutes.

initialize::initialize()
{
  display = NULL;
  event_queue = NULL;
  timer = NULL;
  frameTimer = NULL;
  
  loop = true, redraw = false;
}

#SelectExpand
1int initialize::initSystem(const float FPS, const float frameFPS) 2{ 3 if(!al_init()) 4 { 5 return -1; 6 } 7 8 display = al_create_display(640, 480); 9 if(!display) 10 { 11 al_destroy_display(display); 12 return -1; 13 } 14 15 event_queue = al_create_event_queue(); 16 if(!event_queue) 17 { 18 al_destroy_event_queue(event_queue); 19 al_destroy_display(display); 20 return -1; 21 } 22 23 timer = al_create_timer(1.0 / FPS); 24 if(!timer) 25 { 26 al_destroy_timer(timer); 27 al_destroy_event_queue(event_queue); 28 al_destroy_display(display); 29 return -1; 30 } 31 32 frameTimer = al_create_timer(1.0 / frameFPS); 33 if(!frameTimer) 34 { 35 al_destroy_timer(frameTimer); 36 al_destroy_timer(timer); 37 al_destroy_event_queue(event_queue); 38 al_destroy_display(display); 39 return -1; 40 } 41 42 al_install_keyboard(); 43 al_init_image_addon(); 44 45 c = new Character("player.png", "Hero", 40, 40, 3); 46 std::cout << c->getName() << ": " << c->getHealth() << " HP." << std::endl; 47 48 al_register_event_source(event_queue, al_get_display_event_source(display)); 49 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 50 al_register_event_source(event_queue, al_get_keyboard_event_source()); 51 52 gameLoop(); 53 54 return 0; 55}

SiegeLord

No, the image is certainly there.

Stick a al_filename_exists("player.png") somewhere and make sure it returns true. The only other real reason it wouldn't work is if the image addon was compiled without png support (unlikely unless you compiled it yourself) or the png file itself is corrupted.

Matias Persson

I am pretty sure that the image file is NOT corrupted, that being said I've both jpg and bmp too which I just saved in paint which usually works.. I'm using pre-compiled Allegro 5.0.10 bins.

Edit: I tried al_filename_exists("player.png") and it returns true.

Edit edit: The program works now ALL OF A SUDDEN!?!? I haven't changed a thing.
I still do appreciate everyone's help, thanks :)

SiegeLord

That's the Allegro way.

Thomas Fjellstrom

Hm, random things happing tend to mean you have a memory corruption issue :o change a line of code, even adding a printf can "hide" them temporarily.

Thread #615086. Printed from Allegro.cc