Jump to content
  • Advertisement
Sign in to follow this  

Basic Line Drawing

This topic is 2609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm starting small with Win32 and doing a basic battle type system to get used to displaying things.

void draw_opp_health(HDC hdc)
HPEN hPenOld;

HPEN hLinePen;
COLORREF bar_colour;
if (opponent.current_hp / opponent.max_hp > 0.5) // Green > 50% health
bar_colour = RGB(55, 220, 30);
else if (opponent.current_hp / opponent.max_hp > 0.1) // Yellow > 10% health
bar_colour = RGB(225, 225, 0);
else // Red < 10% health
bar_colour = RGB(225, 0, 0);
hLinePen = CreatePen(PS_SOLID, 7, bar_colour);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);

health_bar = ((opponent.current_hp / opponent.max_hp) * 150) + 110;
// int // int starts at 100 // const int 100 //positioning the health bar

// Opponents health
MoveToEx(hdc, 110, 113, NULL);
LineTo(hdc, health_bar, 113);

SelectObject(hdc, hPenOld);

I'm trying to redraw the health bar to a percentage of its maximum amount (changing colours as health decreases).
So (as of this testing phase) when I click the left mouse button, opponent.current_hp decreases by 20 and i redraw the window.
Initially, when it's all drawn, current_hp is 100 and max_hp is 100 so health_bar evaluates to 260 and the line is drawn properly. Then on my first click the line is redrawn red and short (essentially a circle... I'm pretty sure it's a line of length 0 but the computer can't do that... Either way, not important right now).
Can anyone tell me why the line isn't being redrawn 20% shorter each time?

Share this post

Link to post
Share on other sites
Are opponent[color="#666600"].current_hp and opponent[color="#666600"].max_hp integers?
In this case (opponent[color="#666600"].current_hp [color="#666600"]/ opponent[color="#666600"].max_hp) is always 0 when current_hp is smaller than max_hp.

Share this post

Link to post
Share on other sites
Like Texus said.
int / int = not a good idea because it returns an int.
what you want is a cast, and I think that just doing a cast on the bottom one should suffice
as in int / (float)int (this should return a float)
(or maybe it was the other way around, it doesn't hurt to (cast) both of them though

but then you have a float that you want to put inside an int, so you can just cast back to int

myint = (int)((float)int1/(float)int2)
although not as accurate as holding the result in a float, will at least be more accurate than just myint = int1/int2;

but someone correct me if I'm wrong.

And sorry if you already knew about all this and it was just a mind-slip

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!