Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: .M.M. on Tue 26/05/2009 19:23:58

Title: Drawing HP does not work properly (using DrawingSurface) [SOLVED]
Post by: .M.M. on Tue 26/05/2009 19:23:58
Hi, I´ve got problem with HP bar in my game. It works fine when player loses HP, but it does nothing when HP are gained. I have tested, if HP are realy gained by some display functions and special events, and everything except the HP bar works fine. Here is the code for drawing HP bar from the repeteadly_execute part of script (it displays how many HP has player lost, not how many HP does he has, btn_player_health height and width is 70 pixels):  NumA=IntToFloat(player_maxhealth);
 NumB=IntToFloat(player_health);
 NumC=NumA /70.00;
 if (NumC==0.00) NumC=0.01;
 NumF=NumB/NumC;  
 NumD=FloatToInt(NumF, eRoundUp);
 
 HP_bar = DynamicSprite.CreateFromExistingSprite(btn_player_health.NormalGraphic);
 DrawingSurface*ds = HP_bar.GetDrawingSurface();
 ds.DrawImage(0, 0, 74);
 ds.DrawingColor = Game.GetColorFromRGB(255,0,0); // red
 ds.DrawRectangle(0, 70, 70, NumD); // draw
 ds.Release(); // end drawing
 btn_player_health.NormalGraphic = HP_bar.Graphic;
 btn_player_health.Text = String.Format("%d/%d", player_health, player_maxhealth);

I am sure I have done just some stupid fault, but I cannot find it!   :-\
Title: Re: Drawing HP does not work properly (using DrawingSurface)
Post by: Trent R on Tue 26/05/2009 19:48:57
Using Button clipping is usually the easiest way to do health....

[Edit]: I'm not great at Drawing code, but the line with ds.DrawRectangle is striking me as odd... lemme try to check the manual entry and see if I'm crazy or not.
[Edit2]: Why is HP_bar created from the existing sprite of the button? Why not create it from sprite 74 directly? (Assuming I understand correctly)

~Trent
Title: Re: Drawing HP does not work properly (using DrawingSurface)
Post by: Khris on Tue 26/05/2009 20:15:52
I've tried decrypting the first six lines of code ;)

  NumD = (player_health * 70) / player_maxhealth;
is all you need to do.

Now, since you're not drawing the part of the health bar that represents health but the "hole", loosing HP makes the hole bigger and thus visibly changes the image while gaining HP doesn't (because you're drawing over the old bar image).

  HP_bar = DynamicSprite.CreateFromExistingSprite(74);
  DrawingSurface*ds = HP_bar.GetDrawingSurface();
  ds.DrawingColor = Game.GetColorFromRGB(255,0,0); // red
  if (NumD < 70) ds.DrawRectangle(0, NumD, 70, 70); // draw
  ds.Release(); // end drawing
  btn_player_health.NormalGraphic = HP_bar.Graphic;

This should work.
Title: Re: Drawing HP does not work properly (using DrawingSurface)
Post by: .M.M. on Wed 27/05/2009 19:35:19
Oh, thanks, this works great!  :) It ends like that when I´m copying code from my older games with just a minor changes...  ;)
Title: Re: Drawing HP does not work properly (using DrawingSurface) [SOLVED]
Post by: NsMn on Wed 27/05/2009 19:45:12
It's from KhrisMUC, it's almost unnessecary to tell HIM that it's working great  :P
Title: Re: Drawing HP does not work properly (using DrawingSurface) [SOLVED]
Post by: .M.M. on Thu 28/05/2009 15:31:29
Yes, you´re right, just my habit...  ;D