Why is this not drawing?
EternalGames

Hello, first time poster here.
I'm currently working on a game and I've stumpled upon some extremely weird behaviour in my program. Basically what happens is that what is basically two times the same function call (with different parameters) does something only on the second call. I'd like to know in which way these function calls differ and how I can fix this behaviour.
Here is the necessary code to understand:

#SelectExpand
1void State_Mainmenu::Render() 2{ 3 if(render){ 4 render = false; 5 al_draw_bitmap(bg,0,0,0); // This does nothing 6 7 8 for(int i = 0; i< buttons.size(); i++) 9 { 10 buttons.at(i)->Render(); //This works 11 } 12 al_flip_display(); 13 al_clear_to_color(al_map_rgb(0,0,0)); 14 } 15} 16 17void GUI_Button::Render() 18{ 19 if(!pressed){al_draw_bitmap(unclickedTexture,x_Pos,y_Pos,0);} 20 else{al_draw_bitmap(clickedTexture,x_Pos,y_Pos,0);} 21 if(text.size()!= 0) 22 {al_draw_text(font,al_map_rgb(0,0,0),x_Pos+width/2,y_Pos+height/2-al_get_font_line_height(font)/2,ALLEGRO_ALIGN_CENTRE,text.c_str());} 23}

Like I said. This is basically two times the same thing (calling al_draw_bitmap), not changing anything in between whatsoever.

I'd really appreciate it, if someone could help me out on this one. ::)

someone972

You are sure that the image loaded properly and that it actually has image data saved in it right? Other than that I don't see anything wrong with this code here, so it might be somewhere else.

Kris Asick

Make sure all of your calls to al_create_bitmap() and al_load_bitmap() aren't returning NULL. If they do, then they have failed, likely due to an incorrect filename, invalid bitmap sizes, or other such things.

weapon_S

1. What they said.
2. So when you comment out the button rendering, you see nothing?
I could imagine that the blending mode is set incorrectly and the whole screen is covered by the button bitmaps.

EternalGames

Make sure all of your calls to al_create_bitmap() and al_load_bitmap() aren't returning NULL. If they do, then they have failed, likely due to an incorrect filename, invalid bitmap sizes, or other such things.

I did the following:

if(al_load_bitmap("Stoffmalerei.png"))
  {
    bg = al_load_bitmap("Stoffmalerei.png");
  }
  else
  {
    al_show_native_message_box(al_get_current_display(),"Error","Function","al_load_bitmap failed","aye",0);
  }

it does not show the message box, so I guess I'm loading it right.

weapon_S said:

So when you comment out the button rendering, you see nothing?

Yes, I only see black, when I comment out the button code.:-X

J-Gamer

That's a memory leak right there...
You should try not to load it twice:

bg = al_load_bitmap("Stoffmalerei.png");
if(!bg) {
    al_show_native_message_box(al_get_current_display(),"Error","Function","al_load_bitmap failed","aye",0);
}

weapon_S

Technically you haven't checked bg yet ;) And you're leaking memory :P

bg = al_load_bitmap("Stoffmalerei.png");
if(! bg)
    al_show_native_message_box(al_get_current_display(), "Error", "Function", "al_load_bitmap failed", "aye" ,0);
}

:-/ Sorry, can't help you any further.

EternalGames

Well I thought that checking the return value of al_load_bitmap was supposed to work like that, because I can't do
if(!bg = al_load_bitmap("Stoffmalerei.png")){ errorstuff}

Thanks for reminding me of the memory leak, though. ;D

weapon_S

Yes, you can :-/ Add parentheses, if it feels too weird:
if(!(bg = al_load_bitmap("Stoffmalerei.png"))){ errorstuff}
And yes, it is weird.

EternalGames

Haha, well I found my mistake and it made me laugh hard.
When I did one of the other textures for the game, I accidentally overwrote my Stoffmalerei.png and the new texture was something consisting of black, so I couldn't see it drawn on the black background.

I'm facepalming so hard right now. ;D

Kris Asick

You still need to make sure you're not leaking memory. When you call al_load_bitmap(), you load a bitmap into video memory. So every single time you call this function, you absolutely MUST place the result into an empty pointer. If you don't:

1. The memory will be wasted and future calls to al_load_bitmap() may fail if memory is exhausted.

2. If the pointer you load to already has a bitmap in it, you'll lose access to that bitmap, again leaking memory and potentially running out if you keep doing this.

Remember, a "pointer" simply points to an object in memory, it isn't itself the actual memory being used. If you forget the contents of a pointer, or fail to store one that's created, then the memory it points to will be inaccessible by ANYTHING for the duration of your application, and if you do this enough times, you will eventually have no memory left. :P

Thread #611186. Printed from Allegro.cc