Skip to main content
gamedev.net
🔒 Locked 🎲 GameMaker

Weird Bug

Started by flashingchicken Apr 17, 2006 at 9:26 PM 4 replies 850+ views
Original Post
flashingchicken
flashingchicken
I am making a space launching game, and am using SDL, but i have come across a weird bug that i cant resolve. When i run the debugger, it says that player->thrust_percent=50, which is correct, but, the result of str is "h=0.00, thrust=0.000000". I am wondering if i am not referencing the class correctly. Thanks, John void Draw_Scene(SDL_Surface* screen, Spaceship *player) {char * str; str=new char[1023]; sprintf(str, "h=%4.2f, thrust=%f",player->height-RADIUSEARTH, player->thrust_percent ); SDL_FillRect(screen, NULL, 0x000000); Draw_Text(screen, str, screen->w/2, screen->h/2, 255,255,255); SDL_Flip(screen); free(str);} class Spaceship{ //theta = angle from verticle to axis of ship //phi = angle frme verticle to angle of V //gamma = angle from top of world to space ship public: double theta, phi, gamma; long double height; double mass, drag,velocity; float thrust_percent; Spaceship() { theta=0; phi=0; gamma=0; height=0; mass=0; drag=0;velocity=0; thrust_percent=0; }
jonahrowley
jonahrowley
How do you call Draw_Scene? Are you sure %f works with double and long doubles?

There's a slightly easier way to handle string output than sprintf. Well, first of all, don't use sprintf, use snprintf. Second, don't allocate the character array with new, that will only tax the malloc code further, just allocate it on the stack.

If you use an iostream (more specifically, a stringstream), you can avoid snprintf. snprintf is OK, if you pass the parameters 100% correctly. %f expects a float, what happens if you pass a double or long double? Using an iostream, you don't lose type information, and everything tends to work itself out without the need for error-prone format strings. I think this might be your problem.
flashingchicken
flashingchicken
Changed Draw_Scene to this, works great. Thanks. Now, if i can just keep from deleting my work, i should be set!

std::stringstream stream1;
stream1<<"h="<height-RADIUSEARTH<<" thrust="<thrust_percent;
SDL_FillRect(screen, NULL, 0x000000);
Draw_Text(screen, stream1.str().c_str(), screen->w/2, screen->h/2, 255,255,255);
SDL_Flip(screen);
TravisWells
TravisWells
I know that you fixed it, but this:
str=new char[1023];
free(str);}

was probably your problem.

If you use new[], you need to use delete [].
free goes with malloc(), not new/new[]
jonahrowley
jonahrowley
No, that wasn't the problem. free() might also successfully deallocate a new[], but certainly won't call destructors. A memory leak maybe, but it wasn't the problem he was asking about.
flashingchicken
flashingchicken
I think my primary problem was the format flags. The free vs delete is just bad practice on my part.
But, now that i can see everything, i can see that my physics is messed up, wanna help with my math?.... heheh

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.