Archived

This topic is now archived and is closed to further replies.

C++ and SDL

This topic is 5584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well whats there to say everything works to the point when a key is pressed and it go's thru the if statement but just doesn't add 1 to xpos and ypos... The problems are in the fKeyPressed function about half way down Code: uhh ya that was a little much so click here to download the ziped file >> Click here to download (Winrar>>Get it free Here) (Winzip>>Get it free Here) oh and thanks in advance to... well who ever lol --------------------signature-------------------- Looking for usefull, interesting resources WELL DONT LOOK HERE>>http://www.dragonruins.com/ but its still fun to have a look around [edited by - Smacker_626 on August 27, 2002 3:00:54 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ElCrazon
Link doesnt work, try posting all you souce inside [ source ] tags.


Sorry

I just put it up though and the serv is gonna be up (or so i hope im paying for it)

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
I was gonna do this but didnt know how but even though i got it on my site this is for all the lazy peeps out there

EDIT ---it took out alot of the returns so i would get the file from my site its more... readable---


    

#include <iostream.h>
#include <stdlib.h>
#include "SDL.h"

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *button;
SDL_Event event;

char cIamBlank[1];
int mxpos = 0, mypos = 0, xpos = 0, ypos = 0, buttonPressed = 0;
Uint8 *keys;

void fMessagePump();
void fPrintMessages();
void fKeyPressed(int km);
void fCheckMousePos();
unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
void fLoadImages();
void fDrawScreen();

int main(int argc, char *argv[])
{

if ((SDL_Init(SDL_INIT_VIDEO)) != 0)
{

cout << "Could not Init Video!";

exit (0);

}
else
{

cout << "Init Video Complete!\n";

}

atexit(SDL_Quit);

SDL_WM_SetCaption("Button Test!", NULL);
SDL_WM_SetIcon(SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\Mainicon.bmp"), NULL);

cout << "Trying to create Surface: \"screen\" at 600x400x32 using Hardware with Double Buffering...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

if (screen == NULL)
{

cout << "Failed! Trying without Double Buffering...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_HWSURFACE);

if (screen == NULL)
{

cout << "Failed! Trying Software Surface...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);

if (screen == NULL)
{

cout << "Failed! shuting down all systems\n\n";

exit (0);

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

fLoadImages();

fDrawScreen();

fMessagePump();

return 0;

}

void fMessagePump()
{

for ( ; ; )
{

if (SDL_PollEvent(&event))
{

switch (event.type)
{

case SDL_QUIT:

fPrintMessages();

break;

case SDL_KEYDOWN:

fKeyPressed(0);

break;

case SDL_MOUSEBUTTONDOWN:

buttonPressed = 1;

fKeyPressed(1);

break;

case SDL_MOUSEBUTTONUP:

buttonPressed = 0;

fKeyPressed(1);

break;

default:

break;

}

}

}

}

void fPrintMessages()
{

cout << "\nMessages Printed type a charactor and hit enter to quit\n\n";

cin >> cIamBlank;

cout << "\n\n";

exit (0);

}

void fKeyPressed(int km)
{

if (km == 0)
{

keys = SDL_GetKeyState(NULL);

if (keys[SDLK_UP] == 1)
{

ypos -= 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_DOWN] == 1)
{

ypos += 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_LEFT] == 1)
{

xpos -= 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_RIGHT] == 1)
{

xpos += 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_ESCAPE] == 1)
{

fPrintMessages();

}

}
else if (km == 1)
{

if(event.button.button == SDL_BUTTON_LEFT);
{

fCheckMousePos();

}

}

return;

}

void fCheckMousePos()
{

int tempx, tempy;

tempx = ((xpos) + (button->w));
tempy = ((ypos) + (button->h));

SDL_GetMouseState(&mxpos, &mypos);

mxpos = event.button.x;
mypos = event.button.y;

if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 1)
{

fLoadImages();

fDrawScreen();

cout << "Button Pressed!\n";

}
else if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 0)
{

fLoadImages();

fDrawScreen();

cout << "Button Released!\n";

}

return;

}

void fLoadImages()
{

back = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\back.bmp");

if (buttonPressed == 1)
{
button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonP.bmp");

}
else if (buttonPressed == 0)
{

button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonR.bmp");

}
/*
if (back == NULL)
{

cout << "Could not load background image!\n";

}
else
{

cout << "Loaded background image!\n";

}
if (button == NULL)
{

cout << "Could not load button image!\n";

}
else
{

cout << "Loaded button image!\n";

}
*/

return;

}

unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

xpos = x2;
ypos = y2;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

//SDL_FreeSurface(srcSurf);


return tempval;

}

unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

//SDL_FreeSurface(srcSurf);


return tempval;

}

void fDrawScreen()
{

unsigned int tempback, tempbutton;

tempback = fBlitImg(back, screen, back->w, back->h, 0, 0, 0, 0);

if (xpos == 0 || ypos == 0)
{

tempbutton = fBlitImg(button, screen, button->w, button->h, 0, 0, (((screen->w) - 128) /2), (((screen->h) - 128) /2));

}
else if (xpos != 0 || ypos != 0)
{

tempbutton = fBlitImg2(button, screen, button->w, button->h, 0, 0, xpos, ypos);

}

if (tempback != 0)
{

cout << "Could not Blit background image onto Surface: \"screen\" from Surface: \"back\"!\n";

}

if (tempbutton != 0)
{

cout << "Could not Blit button image onto Surface: \"screen\" from Surface: \"button\"!\n";

}

SDL_UpdateRect(screen, 0, 0, 0, 0);

return;

}



--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 27, 2002 3:24:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ElCrazon
Ok the link work now... i''ll read through it after i finish my lunch..


Sounds good to me *munch.munch.munch*

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
Ok, it compiles fine. What exactyly is your problem? Also could I get the images it loads?

[edit] oh wait, theres a Segmentation Fault, could I get all the files for this project? [/edit]

[edited by - ElCrazon on August 27, 2002 4:03:41 PM]

Share this post


Link to post
Share on other sites
The problem was that it wouldnt add 1 to ypos and xpos it always compiled fine.

(BTW how did it compile fine on ur comp do u have the SDL includes and libs?)

oh and also i got it fixed, I forgot to change one of the funtion calls so it kept setting y and xpos back to there starting values

and incase u wanted to see the files i updated them on the serv and im putting the source in here (also the zip on my serv has everything but the resource file and the icon so when u load the project it will tell u it couldnt open all the windows just click ok and go on with life)


    

#include <iostream.h>
#include <stdlib.h>
#include "SDL.h"

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *button;
SDL_Event event;

char cIamBlank[1];
int mxpos = 0, mypos = 0, xpos = 0, ypos = 0, buttonPressed = 0;
Uint8 *keys;

void fMessagePump();
void fPrintMessages();
void fKeyPressed(int km);
void fCheckMousePos();
unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
void fLoadImages();
void fDrawScreen();

int main(int argc, char *argv[])
{

if ((SDL_Init(SDL_INIT_VIDEO)) != 0)
{

cout << "Could not Init Video!";

exit (0);

}
else
{

cout << "Init Video Complete!\n";

}

atexit(SDL_Quit);

SDL_WM_SetCaption("Button Test!", NULL);
SDL_WM_SetIcon(SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\Mainicon.bmp"), NULL);

cout << "Trying to create Surface: \"screen\" at 600x400x32 using Hardware with Double Buffering...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

if (screen == NULL)
{

cout << "Failed! Trying without Double Buffering...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_HWSURFACE);

if (screen == NULL)
{

cout << "Failed! Trying Software Surface...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);

if (screen == NULL)
{

cout << "Failed! shuting down all systems\n\n";

exit (0);

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

fLoadImages();

fDrawScreen();

fMessagePump();

return 0;

}

void fMessagePump()
{

for ( ; ; )
{

if (SDL_PollEvent(&event))
{

switch (event.type)
{

case SDL_QUIT:

fPrintMessages();

break;

case SDL_KEYDOWN:

fKeyPressed(0);

break;

case SDL_MOUSEBUTTONDOWN:

buttonPressed = 1;

fKeyPressed(1);

break;

case SDL_MOUSEBUTTONUP:

buttonPressed = 0;

fKeyPressed(1);

break;

default:

break;

}

}

SDL_Delay(1);

}

}

void fPrintMessages()
{

cout << "\nMessages Printed type a charactor and hit enter to quit\n\n";

cin >> cIamBlank;

cout << "\n\n";

exit (0);

}

void fKeyPressed(int km)
{

if (km == 0)
{

keys = SDL_GetKeyState(NULL);

if (keys[SDLK_UP] == 1)
{

ypos -= 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_DOWN] == 1)
{

ypos += 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_LEFT] == 1)
{

xpos -= 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_RIGHT] == 1)
{

xpos += 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_ESCAPE] == 1)
{

fPrintMessages();

}

}
else if (km == 1)
{

if(event.button.button == SDL_BUTTON_LEFT);
{

fCheckMousePos();

}

}

return;

}

void fCheckMousePos()
{

int tempx, tempy;

tempx = ((xpos) + (button->w));
tempy = ((ypos) + (button->h));

SDL_GetMouseState(&mxpos, &mypos);

mxpos = event.button.x;
mypos = event.button.y;

if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 1)
{

fLoadImages();

fDrawScreen();

cout << "Button Pressed!\n";

}
else if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 0)
{

fLoadImages();

fDrawScreen();

cout << "Button Released!\n";

}

return;

}

void fLoadImages()
{

back = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\back.bmp");

if (buttonPressed == 1)
{
button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonP.bmp");

}
else if (buttonPressed == 0)
{

button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonR.bmp");

}
/*
if (back == NULL)
{

cout << "Could not load background image!\n";

}
else
{

cout << "Loaded background image!\n";

}
if (button == NULL)
{

cout << "Could not load button image!\n";

}
else
{

cout << "Loaded button image!\n";

}
*/

return;

}

unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

xpos = x2;
ypos = y2;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

SDL_FreeSurface(srcSurf);

return tempval;

}

unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

SDL_FreeSurface(srcSurf);

return tempval;

}

void fDrawScreen()
{

unsigned int tempback, tempbutton;

tempback = fBlitImg2(back, screen, back->w, back->h, 0, 0, 0, 0);

if (xpos == 0 || ypos == 0)
{

tempbutton = fBlitImg(button, screen, button->w, button->h, 0, 0, (((screen->w) - 128) /2), (((screen->h) - 128) /2));

}
else if (xpos != 0 || ypos != 0)
{

tempbutton = fBlitImg2(button, screen, button->w, button->h, 0, 0, xpos, ypos);

}

if (tempback != 0)
{

cout << "Could not Blit background image onto Surface: \"screen\" from Surface: \"back\"!\n";

}

if (tempbutton != 0)
{

cout << "Could not Blit button image onto Surface: \"screen\" from Surface: \"button\"!\n";

}

SDL_UpdateRect(screen, 0, 0, 0, 0);

return;

}



--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 27, 2002 4:09:55 PM]

Share this post


Link to post
Share on other sites
Oh one more thing with all my SDL apps the cpu usage stays at 99 but i put an SDL_Delay(1); at the end of my loop and its whent down to 01 also in the file i gave u i didnt uncomment the SDL_FreeSurface(button); so it eat up RAM like there was no tommorow but its all fixed in these files

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
Yes, i do development with SDL so I have all the dlls in my system folder, any way I way getting an access violation in fBltImg because it didn't load any bitmaps, need some error checking there. That new zip file is missing one byte, so it can't be decompressed... might want to upload it again.

[edited by - ElCrazon on August 27, 2002 4:16:55 PM]

Share this post


Link to post
Share on other sites
Looking quickly, I looks like you draw the scene once, then go into the game loop, and keep processing key presses. You need to keep re-drawing the screen within the ''for'' loop in your message pump.

Share this post


Link to post
Share on other sites
Here it is i tested this one

and jakar thanks for helping but umm why would i want to do that?(i was going to do it in the first place also though this was just because i was bored)

and ELCrazon that was because i forgot to uncomment the fLoadImages(); line in the pump and fBlitImg and fBlitImg2 Frees the Surface each time (and i had the SDL_FreeSurface line commented out so it had a huge memory proplem) so it couldnt Blit it

I hope thats everything i wanted to say lol


--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
I just noticed where you are redrawing your screen (I can''t believe I missed it with so many calls). It is hard to follow, and very easy to lose control of the situation like that. In my opinion, drawing the screen should be called from one point. As for your problem, as long as it is getting to the ''if'' statement, I can''t see why it would have a problem adding 1. I am only viewing this code through the browser, and can''t try and compile it at the moment.

Share this post


Link to post
Share on other sites
i put even newer files on the serv so click the link up there^^^

also does either of you know how to make it not so CPU Intensive i had to take out the SDL_Delay(1); it wasnt running smooth but know it stays at 99% CPU Usage (like always)

and how would you get input from 2 different keys at the same time:

say it was like galaga how could i make the ship move back and up by pressing the up and left keys (its only getting input from 1 key at a time right now)

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 27, 2002 4:55:15 PM]

Share this post


Link to post
Share on other sites
Another problem I see is that you keep loading the images each pass. You also keep redirecting the surface pointer to a newly loaded image without getting rid of the old image (eating up memory). Try loading all the images once, and have an image for button1 and another for button2.

I saw that you are releasing the images once you draw them, but I can't easily tell if you are getting them all. From your description of your memory problem before, I would guess that you are missing the release of some. This is one reason you should allocate your resources in the beginning, then release them at the end. Another reason is that it takes up too much CPU time.

[edited by - jakar on August 27, 2002 5:11:24 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by jakar
Another problem I see is that you keep loading the images each pass. You also keep redirecting the surface pointer to a newly loaded image without getting rid of the old image (eating up memory). Try loading all the images once, and have an image for button1 and another for button2.


I think you have a point but about not getting rid of the old image your wrong if you look at fBlitImg (and fBlitImg2) right before return; i call SDL_FreeSurface(srcSurf); to free that surface

and umm besides me having to redo the loading problem could you help me with this: (it works fine accept when it comes to the if statements it runs both of them fine accept for the fact it lets you click anywhere could you help me with that?)


  

void fCheckMousePos()
{

int tempx, tempy;

tempx = ((xpos) + (128));
tempy = ((ypos) + (128));

SDL_GetMouseState(&mxpos, &mypos);

mxpos = event.button.x;
mypos = event.button.y;

if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 1)
{

fLoadImages();
fDrawScreen();

cout << "Button Pressed!\n";

}
else if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 0)
{

fLoadImages();
fDrawScreen();

cout << "Button Released!\n";

}

return;

}



--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
Well im gonna go eat ill be back in a couple minutes i guess

if u fix the problem just post it on here but most of all try to ansewer my post (3rd to last post)

Thanks for all the help

--------------------signature--------------------

Looking for usefull, interesting resources?

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites

ok, i found some...


  
int* keys;

...

keys = SDL_GetKeyState(NULL);


if ( keys[SDLK_PAGEUP] == SDL_PRESSED ) {
z-=0.02f;
}
if ( keys[SDLK_PAGEDOWN] == SDL_PRESSED ) {
z+=0.02f;
}
if ( keys[SDLK_UP] == SDL_PRESSED ) {
xspeed-=0.01f;
}
if ( keys[SDLK_DOWN] == SDL_PRESSED ) {
xspeed+=0.01f;
}
if ( keys[SDLK_LEFT] == SDL_PRESSED ) {
yspeed-=0.01f;
}
if ( keys[SDLK_RIGHT] == SDL_PRESSED ) {
yspeed+=0.01f;
}




piece of cake...

you use SDL_PumpEvents to update the key state array

read the sdl docs for more info

Share this post


Link to post
Share on other sites
this is a really quick very messy, not fully tested, class thingy that that i made real quick that makes an sdl program only use the CPU it needs to reach a specified number of frames per second.

just call Start() before the main loop, SetLimit(int) sometime after that but still before the main loop, and then call
RunFrame() at the end of every frame drawing loop...

btw, this is very messy code...



  
class FpsCalculator
{
public:
void Start()
{
lasttick=SDL_GetTicks();
fps=0.0f;
frames=0;
fpslimit=30;
tickdelta=1.0f;
currentticks=lasttick;
oldtick=lasttick;
};
void RunFrame()
{
float milliPerFrame;
float aimMilliPerFrame;
float shiftMilliPerFrame;

currentticks=SDL_GetTicks();
frames++;
if (oldtick+1000<currentticks)
{
frames=0;
oldtick=currentticks;
}

milliPerFrame=currentticks-lasttick;
tickdelta=(tickdelta+1.0f/((((float)fpslimit)/1000.0f)*milliPerFrame))/2.0f;
fps=tickdelta*(float)fpslimit;
aimMilliPerFrame=1000.0f/(float)fpslimit;
shiftMilliPerFrame=aimMilliPerFrame*tickdelta-aimMilliPerFrame;
if (shiftMilliPerFrame>0.0f)
{
SDL_Delay((Uint32)shiftMilliPerFrame);
}

lasttick=currentticks;
};
void Setlimit(int limit)
{
fpslimit=limit;
};
float Getfps()
{
tickdelta=1.0f;
return fps;
};
int GetFrames()
{
return frames;
};

Uint32 currentticks;
Uint32 lasttick;
Uint32 oldtick;
float tickdelta;
float fps;
int frames;
int fpslimit;
};


Share this post


Link to post
Share on other sites
yeah i knew about that warpexplorer but i made it so you could hold the button and keep the image moving (its the if statement at the end of the for loop) but i needed a second variable to find out the second key pressed and its in the new file on my server (incase anyone wants to see or if a newbie has trouble maybe...) thanks anyway.

And about your last post with the Fpscalculator class thanks and it looks cool but umm *scratchs head* could you maybe explain umm what it does... plz (im gonna go test it after i post the other version on my server)

Thanks to everyone for the help to

link to the all working (but cpu intensive code) Get it here

EDIT---Sorry i just got it up---

--------------------signature--------------------

Looking for usefull, interesting resources?

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 28, 2002 4:03:53 AM]

Share this post


Link to post
Share on other sites
Ok i tested your FPSCalc and it works great its whent from 99% to 37% to 47% thanks alot!

And i just updated the file on my server for whoever once again (thats the last time)

But warp could you still explain how it works?

--------------------signature--------------------

Looking for usefull, interesting resources?

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
umm how it works....oh gosh, Its too messy, I''ll rewrite after I get back from class.

basically how it works, is it finds the number of milliseconds it took to complete the last frame, then it calculates how many milliseconds perframe it needs in order to get the right number of frames per second, then it takes the difference of those two numbers, and if it is greater than 0 it waits that many before continueing....(ignore the tickdelta stuff, is useless and ugly )

this method doesnt really work because it drops out any fractional milliseconds in the delay. This makes you get a higher fps than what you set as the limit. ill write a better one after i get back...

Share this post


Link to post
Share on other sites
ok, this is a much better way to do it...


  
Uint32 timecallback(Uint32 interval, void* param)
{
//Draw Frame

};

int main( int argc, char* argv[] )
{
SDL_Event event;
bool done=false;
SDL_TimerID id;
int fps=30;

id=SDL_AddTimer(1000/fps,timecallback, &scenedata);
//1000/fps is the number of milliseconds per frame

// this will have to be adjusted to account for timer

// latency if you want to have an exact fps


while (!done)
{
//Main program loop

SDL_WaitEvent(&event);
//basically all we have to do is wait for events

// and then process them

}
SDL_RemoveTimer(id);
SDL_Quit();
return 0;
}

Share this post


Link to post
Share on other sites