Code: Select all
if ((time/frame) < (1000/FPS))
{
SDL_Delay((1000/FPS) - (time/frame));
}
It compiles and runs but it runs at 2 FPS even though I have the const int FPS = 1;
Moderator: Coders of Rage
Code: Select all
if ((time/frame) < (1000/FPS))
{
SDL_Delay((1000/FPS) - (time/frame));
}
Let's plug in some values.RandomDever wrote:What's wrong with this code?Code: Select all
if ((time/frame) < (1000/FPS)) { SDL_Delay((1000/FPS) - (time/frame)); }
It compiles and runs but it runs at 2 FPS even though I have the const int FPS = 1;
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Why are you dividing things by the FPS then... 1000/1 is 1000...RandomDever wrote:What's wrong with this code?Code: Select all
if ((time/frame) < (1000/FPS)) { SDL_Delay((1000/FPS) - (time/frame)); }
It compiles and runs but it runs at 2 FPS even though I have the const int FPS = 1;
...Pickzell wrote:Why are you dividing things by the FPS then... 1000/1 is 1000...
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Thisshort wrote:46 posts here. You have to try harder then this. /palmface
Code: Select all
#include "MSDL_image.h"
#include "MSDL_render.h"
#include "MSDL_ttf.h"
#include "MSDL_key.h"
#include "MSDL_timer.h"
#include "player.h"
SDL_Event event;
int count = 0;
Uint32 frame = 0;
bool quit = false;
Uint8 *keystates;
Uint32 time = 1;
char buffer[600];
char buffer2[600];
const int Width = 640;
const int Height = 480;
const int BPP = 32;
const Uint32 FPS = 1;
SDL_Surface *screen = NULL;
SDL_Surface *color = NULL;
SDL_Surface *background = NULL;
SDL_Surface *ground = NULL;
SDL_Surface *timer = NULL;
SDL_Surface *keys = NULL;
SDL_Surface *frames =NULL;
Player player;
void Init()
{
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( Width, Height, BPP, SDL_SWSURFACE );
SDL_WM_SetCaption( "SMB Clone v0.0.1", NULL );
TTF_Init();
}
void Exit()
{
SDL_Quit();
}
int main( int argc, char* args[] )
{
Init();
background = LoadKeyImage( "background.png", 0, 0, 255 );
color = LoadImage( "color.png" );
ground = LoadImage( "ground.png" );
//, "arblanca.ttf", 140, 0, 0, 255
BlitSurface( 0, 0, color, screen );
BlitSurface( 0, 0, background, screen );
//BlitSurface( (Width / 2) - (timer->w / 2), (Height / 2) - (timer->h / 2), timer, screen );
while (count < 640)
{
BlitSurface( count, 448, ground, screen );
count += 32;
}
RenderTextCenter( "Hello!", "comic.ttf", 140, 255, 0, 0, screen, Height, Width );
SDL_Flip( screen );
while( quit == false )
{
frame++;
count = 0;
time = SDL_GetTicks();
BlitSurface( 400, 0, keys, screen );
SDL_Flip( screen );
keystates = Keystates();
BlitSurface( 0, 0, color, screen );
BlitSurface( 0, 0, background, screen );
while (count < 640)
{
BlitSurface( count, 448, ground, screen );
count += 32;
}
RenderTextCenter( "Hello!", "comic.ttf", 140, 255, 0, 0, screen, Height, Width );
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}
sprintf_s(buffer,"Time: %d", time);
timer = RenderText( buffer, "arberkley.ttf", 40, 0, 0, 255 );
BlitSurface ( 0, 0, timer, screen );
if( keystates[ SDLK_UP ] )
{
keys = RenderText( "UP", "comic.ttf", 40, 0, 0, 255 );
}
if ((time/frame) < (1000/FPS))
{
SDL_Delay((1000/FPS) - (time/frame));
}
sprintf_s(buffer2,"frame: %d", frame);
frames = RenderText( buffer2, "arberkley.ttf", 40, 0, 0, 255 );
BlitSurface ( 0, 60, frames, screen );
SDL_Flip( screen );
//if ( frame == 1 )
//{
// SDL_Delay( 5000 );
//}
}
Exit();
return 0;
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
i'm just using SDL_Delay()MarauderIIC wrote:Nothing jumps out as a problem. How are you telling how many frames happen per second? Maybe that's where the problem is.
Code: Select all
if ((time/frame) < (1000/FPS))
{
SDL_Delay((1000/FPS) - (time/frame));
}
Code: Select all
Uint32 elapsed = SDL_GetTicks() - time;
if (elapsed < (1000/FPS))
{
SDL_Delay((1000/FPS) - elapsed);
}
Scoody wrote:The framelimiter calculates how much time each frame has to its disposal, if it doesn't utilize all that time the rest is put in a delay.
Here you're just dividing the start time with the amount of frames that's been drawn, which makes no sense. You need to take the current ticks and subtract the ticks at the start; then you'll know how much time the current frame used to draw.So (time/frame) should be (SDL_GetTicks() - time)Code: Select all
if ((time/frame) < (1000/FPS)) { SDL_Delay((1000/FPS) - (time/frame)); }
Code: Select all
Uint32 elapsed = SDL_GetTicks() - time; if (elapsed < (1000/FPS)) { SDL_Delay((1000/FPS) - elapsed); }
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.