Sign in to follow this  
PhilipNguyen

Please Delete this post, my problem was solved

Recommended Posts

PhilipNguyen    122
Im adding a heathmeter to this fps im working on. I know for sure that this is the only part that the compiler doesnt like. float health = m_playerManager->GetLocalPlayer()->GetHealth(); char healthcasted = (char) health; m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700); I know render takes char, so i casted it and it says error C2664: 'Font::Render' : cannot convert parameter l from 'char' to 'char *' I'm a noob so im sorry. Thankyou for any responses. [Edited by - PhilipNguyen on August 18, 2006 7:13:25 PM]

Share this post


Link to post
Share on other sites
Morpheus011    309
not really a rendering problem, the function you're calling is expecting the first parameter to be a char *, and you're passing it a char. you can either pass the address of the variable using the address of operator (&) or you can pass it a true character pointer.

Share this post


Link to post
Share on other sites
Limitz    342
Quote:
Original post by PhilipNguyen
Im adding a heathmeter to this fps im working on. I know for sure that this is the only part that the compiler doesnt like.

float health = m_playerManager->GetLocalPlayer()->GetHealth();
char healthcasted = (char) health;

m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700);

I know render takes char, so i casted it and it says
error C2664: 'Font::Render' : cannot convert parameter l from 'char' to 'char *'

I'm a noob so im sorry. Thankyou for any responses.


Even then, casting a float to a char doesn't really give you any information. If healthcasted needs to be a string, ie. char* you better use sprintf.

untested:

float health = m_playerManager->GetLocalPlayer()->GetHealth();
char healthcasted[6] // expecting 100.0 at most

//now put the float into the string using format 3.1 => 0.0 to 100.0
sprintf(healthcasted, "%3.1f", health);

m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700);


hope that works for you.

Share this post


Link to post
Share on other sites
PhilipNguyen    122
Morpheus, I changed this line of code
m_scoreBoardFont->Render(&healthcasted, 30, 0, 0xFFFF7700);
The screen displayed a bunch of characters, like BQBS3

Limitz, I changed the program like you said and it compiles but crashes when i get into the game state. It says the heathcasted is corrupt

Thankyou for responding

Share this post


Link to post
Share on other sites
dmail    116
&healthcasted is a char**


#include <sstream>

std::stringstream health<<m_playerManager->GetLocalPlayer()->GetHealth();
m_scoreBoardFont->Render(health.str(), 30, 0, 0xFFFF7700);


[edit]
it may require
std::stringstream health;
health<<m_playerManager->GetLocalPlayer()->GetHealth();

Share this post


Link to post
Share on other sites
Limitz    342
Quote:
Original post by PhilipNguyen
Morpheus, I changed this line of code
m_scoreBoardFont->Render(&healthcasted, 30, 0, 0xFFFF7700);
The screen displayed a bunch of characters, like BQBS3

Limitz, I changed the program like you said and it compiles but crashes when i get into the game state. It says the heathcasted is corrupt

Thankyou for responding


are you sure you removed the & before healthcasted in the Render function using my method? It should do the same as dmail's code...

Greetings

EDIT: ofcourse you did, you said it compiled... silly me :)
EDIT: is that your own renderfunction? if it sais it is corrupt because it gets its input in the format 100.0 and you need 100 you may replace %3.1f by %3.0f.

Share this post


Link to post
Share on other sites
hplus0603    11347
Render() seems to take a null-terminated string of characters, and draws those. If you don't know what a null-terminated string is, you should probably go learn (check out a basic C tutorial, or the function strcpy(), for example).

The reason you get lots of characters when passing the pointer to the character, is that there is no null after the character to terminate the string. Further, the character itself is a single ASCII character with the value you're setting it to -- not the characters that make up the number you want to render. For that, you need sprintf().

Thus, your code should look like this:


float health = m_playerManager->GetLocalPlayer()->GetHealth();
char buf[30];
sprintf(buf, "%.1f", health);
m_scoreBoardFont->Render(buf, 30, 0, 0xFFFF7700);


This will print health with one decimal of precision.

Share this post


Link to post
Share on other sites
Morpheus011    309
Quote:
Original post by dmail
&healthcasted is a char**

*** Source Snippet Removed ***


He's creating a char called healthcasted. &healthcasted is therefore the address of the variable healthcasted, i.e. a char *, not a double pointer. If healthcasted was a char *, then you'd be right.

Share this post


Link to post
Share on other sites
t0Xic    138


#include <sstream>

std::stringstream health;
health << m_playerManager->GetLocalPlayer()->GetHealth();
m_scoreBoardFont->Render(health.str().c_str(), 30, 0, 0xFFFF7700);

Share this post


Link to post
Share on other sites
dmail    116
Quote:
Original post by Morpheus011
Quote:
Original post by dmail
&healthcasted is a char**

*** Source Snippet Removed ***


He's creating a char called healthcasted. &healthcasted is therefore the address of the variable healthcasted, i.e. a char *, not a double pointer. If healthcasted was a char *, then you'd be right.


Pardon!
float health = m_playerManager->GetLocalPlayer()->GetHealth();
char healthcasted[6] // expecting 100.0 at most

//now put the float into the string using format 3.1 => 0.0 to 100.0
sprintf(healthcasted, "%3.1f", health);

m_scoreBoardFont->Render(healthcasted, 30, 0, 0xFFFF7700);



Quote:
m_scoreBoardFont->Render(&healthcasted, 30, 0, 0xFFFF7700);

The name of an array is a pointer to the first element, therefore taking the address is a pointer to pointer.
If you looked... "then you'd be right"

Share this post


Link to post
Share on other sites
Limitz    342
Quote:
Original post by Morpheus011
Quote:
Original post by dmail
&healthcasted is a char**

*** Source Snippet Removed ***


He's creating a char called healthcasted. &healthcasted is therefore the address of the variable healthcasted, i.e. a char *, not a double pointer. If healthcasted was a char *, then you'd be right.


Either way, if you cast a float to a char you'd only interpret the first byte of the 4 byte float as a char which has no meaning whatsoever. If the render function asks for a char* in order to draw the health value you may assume that it needs a string containing the value in a textual representation, not a pointer to the first byte of a float containing the value.

Looks great by the way. Glad it works :)

Greetings.

Share this post


Link to post
Share on other sites
Morpheus011    309
Quote:
Original post by dmail
Quote:
Original post by Morpheus011
Quote:
Original post by dmail
&healthcasted is a char**

*** Source Snippet Removed ***


He's creating a char called healthcasted. &healthcasted is therefore the address of the variable healthcasted, i.e. a char *, not a double pointer. If healthcasted was a char *, then you'd be right.


Pardon!
*** Source Snippet Removed ***
Quote:
m_scoreBoardFont->Render(&healthcasted, 30, 0, 0xFFFF7700);

The name of an array is a pointer to the first element, therefore taking the address is a pointer to pointer.
If you looked... "then you'd be right"


You quoted the wrong snippet of code. The OP's code most definitely was not creating an array called healthcasted, he was creating a single character called healthcasted. Taking the address of a character gives you a pointer. The address of the name of an array is a double pointer for sure, but you weren't referencing the OP's code when you made that statement

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