Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Why is this not drawing?

This thread is locked; no one can reply to it. rss feed Print
Why is this not drawing?
EternalGames
Member #14,603
October 2012

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
Member #7,719
August 2006
avatar

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.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Kris Asick
Member #1,424
July 2001

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.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

weapon_S
Member #7,859
October 2006
avatar

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
Member #14,603
October 2012

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
Member #12,491
January 2011
avatar

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);
}

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

weapon_S
Member #7,859
October 2006
avatar

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
Member #14,603
October 2012

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

EternalGames
Member #14,603
October 2012

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
Member #1,424
July 2001

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

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Go to: