#ifdef __cplusplus
#include
#else
#include
#endif
#include "SDL.h"
#include "inc/cPlayer.h"
#include "inc/cEvent.h"
#include "inc/InfoDebug.h"
#include "inc/cTimer.h"
#include "inc/cFps.h"
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
cPlayer p1;
p1.GetScreenPointer(screen);
p1.m_sprite.load_image("img/Character Boy.png");
cEvent GameEvent;
InfoDebug text;
cTimer time;
cFps fps;
/////////////////////////////////////////////////
/////////////////////////////////////////////////
fps.SetFrameRate(60);
while ( GameEvent.IsQuit()!= true)
{
fps.Start();
GameEvent.Update();
p1.move(GameEvent.KeyState());
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 200, 200, 200));
// draw bitmap
p1.blit();
text.show_Var(fps.GetTicks(),"fps.GetTicks",screen,200,250);
text.show_Var(fps.GetFrameNumber(),"fps.GetFrameNumber",screen,200,280);
// Update FPS
fps.Update();
// finally, update the screen☺
SDL_Flip(screen);
} // end main loop
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
كود الـ Fps كلاس :#ifndef CFPS_H
#define CFPS_H
#include
#include "inc/cTimer.h"
class cFps
{
public:
cFps();
~cFps();
void SetFrameRate(int frame_rate);
void Start();
void Update();
inline int GetFrameRate(){return m_frame_rate;}
inline int GetFrameNumber(){return m_frame_number;}
inline int GetTicks(){ return m_fps.GetTicks(); }
protected:
cTimer m_fps;
int m_frame_rate;
int m_frame_number;
int m_last_ticks;
private:
};
#endif // CFPS_H
#include "inc/cFps.h"
cFps::cFps()
{
m_frame_rate =0;
m_frame_number=0;
}
cFps::~cFps()
{
//dtor
}
void cFps::SetFrameRate( int frame_rate){
m_frame_rate = frame_rate;
}
void cFps::Start(){
m_fps.Start();
}
void cFps::Update(){
m_frame_number++;
if( (m_fps.GetStartingTime()-m_last_ticks) < (1000/m_frame_rate) ){
SDL_Delay( (1000/m_frame_rate)- m_fps.GetTicks() );
}
m_last_ticks = m_fps.GetStartingTime();
}
#ifndef CTIMER_H
#define CTIMER_H
#include "SDL.h"
class cTimer
{
public:
cTimer();
virtual ~cTimer();
int GetTicks();
void Start();
void Pause();
void UnPause();
void Stop();
inline bool IsStarted(){return m_started;}
inline bool IsPaused(){return m_paused;}
inline int GetStartingTime(){return m_ticks_when_started;}
protected:
int m_ticks_when_paused;
int m_ticks_when_started;
bool m_started;
bool m_paused;
private:
};
#endif // CTIMER_H
#include "inc/cTimer.h"
cTimer::cTimer()
{
m_ticks_when_paused = 0;
m_started = false;
m_paused = false;
}
cTimer::~cTimer(){
//dtor
}
int cTimer::GetTicks(){
if(m_started == true){
if( m_paused == true){
return m_ticks_when_paused;
}
return ( SDL_GetTicks()-m_ticks_when_started );
}
// Timer isn't started yet !
return 0;
}
void cTimer::Start(){
m_started = true;
m_paused = false;
m_ticks_when_started = SDL_GetTicks();
}
void cTimer::Pause(){
if( (m_started==true)&&( m_paused==false ) ){
m_paused = true;
m_ticks_when_paused = (SDL_GetTicks()-m_ticks_when_started) ;
}
}
void cTimer::UnPause(){
if( ( m_paused == true) ) {
m_paused= false;
m_ticks_when_started = SDL_GetTicks()-m_ticks_when_paused;
m_ticks_when_paused = 0;
}
}
void cTimer::Stop(){
m_started = false;
m_paused = false;
}
بتاريخ 29/رمضان/1431 03:49 م، قطب انس حاجبيه بشدة وهو يقول:
هل من الممكن توضيح هذه المفاهيم :في 30/رمضان/1431 12:53 م، قال انس بهدوء وتؤدة:
-كيف نعرف الزمن اللازم الذي سنوقف به البرنامج (delay) ؟