#include "SDL.h"
// this timer class I've taken from Enginuity by Superpig
class CGlobalTimer
{
public:
float dT;
unsigned long lastFrameIndex;
unsigned long thisFrameIndex;
bool Start();
void Update();
};
bool CGlobalTimer::Start()
{
thisFrameIndex=SDL_GetTicks();
lastFrameIndex=thisFrameIndex;
dT=0;
return true;
}
void CGlobalTimer::Update()
{
lastFrameIndex=thisFrameIndex;
thisFrameIndex=SDL_GetTicks();
dT=((float)(thisFrameIndex-lastFrameIndex))/1000.0f;
}
#define abs(x) ((x>0) ? (x) : (-x))
CGlobalTimer timer;
int main(int argc, char ** argv)
{
// initialization...
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface * screen = SDL_SetVideoMode (640 , 480, 0, SDL_SWSURFACE ) ;
SDL_Event event ;
float x = 450;
int vx = -1;
SDL_Rect rect = { int(x), 150, 60, 60 };
SDL_ShowCursor(0);
timer.Start();
// endless loop
for ( ; ; )
{
if ( SDL_PollEvent ( &event ) )
{
if ( event.type == SDL_KEYDOWN )
{
// if space was pressed, change the direction of movement
if (event.key.keysym.sym == SDLK_SPACE) vx = -vx;
else break;
}
if ( event.type == SDL_QUIT ) break ;
}
// now the update phase
timer.Update();
// for clearing ugly border
if (vx > 0) rect.x-=abs(vx);
else rect.x+=abs(vx);
SDL_FillRect ( screen , &rect , SDL_MapRGB ( screen->format , 0, 0 , 0) ) ;
if (vx > 0) rect.x+=abs(vx);
else rect.x-=abs(vx);
if (x+rect.w > 638) vx = -vx;
if (x < 0) vx = -vx;
x += vx * (timer.dT); // !!! without the multiplication
rect.x = x; // rectangle is moving normally
// now draw the rectangle
SDL_FillRect ( screen , &rect , SDL_MapRGB ( screen->format , 0, 0 , 255 ) ) ;
SDL_Flip(screen);
}
SDL_Quit();
return 0;
};
Yet another independent framerate movement in SDL...
Yes yes yes, I know, everybody is tired of this type of topics (me too). Hovewer, the code I've written according to some of very good sources I've found on net (for example: Enginuity #3, one code snippet from GameDev etc.) should give me a perfect, time independent object movement (these sources tell that). Well... it doesn't :-/
I started simple, by coding rectangle that moved on screen and bumped off the walls. And it worked. Then, I've added timer code and slightly altered rectangle moving. And it stoped working. Rectangle was moving, but very slowly and jerky. I've tried many different combinations but none of them worked. Could you please take a look at my code and tell me what I'm doing wrong?
try changing vx to a float, and give it a much larger value.
for my breakout game, I had to change the velocity to be about 50 in order for it to be chalenging.
changing vx to a float instead of using an int might solve your problem.
for my breakout game, I had to change the velocity to be about 50 in order for it to be chalenging.
changing vx to a float instead of using an int might solve your problem.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
Man, that was one *really* stupid bug.
Yes, you are right. I had to increase vx to 50 to see the properly moved rectangle. Changing vx to float didn't change anything...
Anyway, big thx for the help PnP Bios! :-)
Quote: try changing vx to a float, and give it a much larger value.
for my breakout game, I had to change the velocity to be about 50 in order for it to be chalenging.
Yes, you are right. I had to increase vx to 50 to see the properly moved rectangle. Changing vx to float didn't change anything...
Anyway, big thx for the help PnP Bios! :-)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement