Skip to content

Instantly share code, notes, and snippets.

@andresfelipemendez
Created July 19, 2018 01:15
Show Gist options
  • Save andresfelipemendez/c0d0304f9991f60bb3aa661abd9c3512 to your computer and use it in GitHub Desktop.
Save andresfelipemendez/c0d0304f9991f60bb3aa661abd9c3512 to your computer and use it in GitHub Desktop.
engine without architecture, frame rate independence
#include <SDL.h>
#undef main
#include <iostream>
class LTimer
{
public:
//Initializes variables
LTimer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
Uint32 getTicks();
//Checks the status of the timer
bool isStarted();
bool isPaused();
private:
//The clock time when the timer started
Uint32 mStartTicks;
//The ticks stored when the timer was paused
Uint32 mPausedTicks;
//The timer status
bool mPaused;
bool mStarted;
};
LTimer::LTimer()
{
//Initialize the variables
mStartTicks = 0;
mPausedTicks = 0;
mPaused = false;
mStarted = false;
}
void LTimer::start()
{
//Start the timer
mStarted = true;
//Unpause the timer
mPaused = false;
//Get the current clock time
mStartTicks = SDL_GetTicks();
mPausedTicks = 0;
}
void LTimer::stop()
{
//Stop the timer
mStarted = false;
//Unpause the timer
mPaused = false;
//Clear tick variables
mStartTicks = 0;
mPausedTicks = 0;
}
void LTimer::pause()
{
//If the timer is running and isn't already paused
if (mStarted && !mPaused)
{
//Pause the timer
mPaused = true;
//Calculate the paused ticks
mPausedTicks = SDL_GetTicks() - mStartTicks;
mStartTicks = 0;
}
}
void LTimer::unpause()
{
//If the timer is running and paused
if (mStarted && mPaused)
{
//Unpause the timer
mPaused = false;
//Reset the starting ticks
mStartTicks = SDL_GetTicks() - mPausedTicks;
//Reset the paused ticks
mPausedTicks = 0;
}
}
Uint32 LTimer::getTicks()
{
//The actual timer time
Uint32 time = 0;
//If the timer is running
if (mStarted)
{
//If the timer is paused
if (mPaused)
{
//Return the number of ticks when the timer was paused
time = mPausedTicks;
}
else
{
//Return the current time minus the start time
time = SDL_GetTicks() - mStartTicks;
}
}
return time;
}
bool LTimer::isStarted()
{
//Timer is running and paused or unpaused
return mStarted;
}
bool LTimer::isPaused()
{
//Timer is running and paused
return mPaused && mStarted;
}
int main(int argc, char const *argv[])
{
const int FPS = 60;
const int DELAY_TIME = 1000.0f / FPS;
SDL_Window* window;
SDL_Renderer* renderer;
Uint32 frameStart, frameTime = 0;
if (!SDL_Init(SDL_INIT_EVERYTHING) == 0) return false;
std::cout << "SDL init success\n";
window = SDL_CreateWindow("engine test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
if(window == 0) return false;
std::cout << "window creation success\n";
renderer = SDL_CreateRenderer(window, -1, 0);
if(renderer == 0) return false;
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
bool isRunning = true;
SDL_Rect* player = new SDL_Rect();
player->h = 10;
player->w = 10;
player->x = 10;
player->y = 10;
SDL_Event event;
int horizontal = 0;
int vertical = 0;
float x = 0;
float y = 0;
LTimer stepTimer;
while (isRunning)
{
frameStart = SDL_GetTicks();
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
isRunning = false;
}
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
horizontal = -1;
break;
case SDLK_RIGHT:
horizontal = 1;
break;
case SDLK_UP:
vertical = -1;
break;
case SDLK_DOWN:
vertical = 1;
break;
default:
break;
}
}
else if (event.type == SDL_KEYUP)
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
horizontal = 0;
break;
case SDLK_RIGHT:
horizontal = 0;
break;
case SDLK_UP:
vertical = 0;
break;
case SDLK_DOWN:
vertical = 0;
break;
default:
break;
}
}
}
float timeStep = stepTimer.getTicks() / 1000.f;
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_Rect rectangle;
x += horizontal * timeStep * 340;
y += vertical * timeStep * 340;
rectangle.x = (int)x;
rectangle.y = (int)y;
rectangle.w = 5;
rectangle.h = 5;
SDL_RenderFillRect(renderer, &rectangle);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
stepTimer.start();
// frameTime = SDL_GetTicks() - frameStart;
SDL_RenderPresent(renderer);
/*if (frameTime < DELAY_TIME)
{
SDL_Delay(static_cast<int>(DELAY_TIME - frameTime));
}*/
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment