Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro bitmap is null even when initialized

This thread is locked; no one can reply to it. rss feed Print
Allegro bitmap is null even when initialized
Matias Persson
Member #15,093
May 2013

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
Major Reynaldo
May 2007
avatar

Matias Persson
Member #15,093
May 2013

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
Member #7,827
October 2006
avatar

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Matias Persson
Member #15,093
May 2013

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
Member #7,827
October 2006
avatar

That's the Allegro way.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

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

Go to: