Sign in to follow this  

Basic Line Drawing

Recommended Posts

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

[code]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"].[/color]current_hp and opponent[color="#666600"].[/color]max_hp integers?
In this case (opponent[color="#666600"].[/color]current_hp [color="#666600"]/[/color] opponent[color="#666600"].[/color]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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this