Skip to content

Commit

Permalink
Handle multiple windows at 60fps
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 17, 2024
1 parent 95fb101 commit eb860ea
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ add_subdirectory(sdl)
add_subdirectory(util)
add_subdirectory(emulator)

target_sources(8ChocChip PUBLIC main.cpp)
target_sources(8ChocChip PUBLIC main.cpp Timer.cpp Timer.h)
103 changes: 103 additions & 0 deletions src/Timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "Timer.h"

Timer::Timer()
{
//Initialize the variables
mStartTicks = 0;
mPausedTicks = 0;

mPaused = false;
mStarted = false;
}

void Timer::start()
{
//Start the timer
mStarted = true;

//Unpause the timer
mPaused = false;

//Get the current clock time
mStartTicks = SDL_GetTicks();
mPausedTicks = 0;
}

void Timer::stop()
{
//Stop the timer
mStarted = false;

//Unpause the timer
mPaused = false;

//Clear tick variables
mStartTicks = 0;
mPausedTicks = 0;
}

void Timer::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 Timer::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;
}
}

Uint64 Timer::getTicks()
{
//The actual timer time
Uint64 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 Timer::isStarted()
{
//Timer is running and paused or unpaused
return mStarted;
}

bool Timer::isPaused()
{
//Timer is running and paused
return mPaused && mStarted;
}
36 changes: 36 additions & 0 deletions src/Timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef TIMER_H
#define TIMER_H

#include <SDL3/SDL.h>

class Timer {
public:
//Initializes variables
Timer();

//The various clock actions
void start();
void stop();
void pause();
void unpause();

//Gets the timer's time
Uint64 getTicks();

//Checks the status of the timer
bool isStarted();
bool isPaused();

private:
//The clock time when the timer started
Uint64 mStartTicks;

//The ticks stored when the timer was paused
Uint64 mPausedTicks;

//The timer status
bool mPaused;
bool mStarted;
};

#endif
25 changes: 25 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <SDL3_ttf/SDL_ttf.h>

#include "Timer.h"

int main(int argc, char **argv) {
std::string rom;

Expand Down Expand Up @@ -114,7 +116,14 @@ int main(int argc, char **argv) {
bool quit = false;
SDL_Event event;

Timer fpsTimer;
Timer capTimer;

int countedFrames = 0;
fpsTimer.start();

while (!quit) {
capTimer.start();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
quit = true;
Expand All @@ -125,6 +134,13 @@ int main(int argc, char **argv) {
}
}

float avgFPS = countedFrames / ( fpsTimer.getTicks() / 1000.f );
if( avgFPS > 2000000 ) {
avgFPS = 0;
}

std::cout << "FPS Average " << avgFPS << std::endl;;

// Do not change, this makes multiple windows not crash
for (size_t i = 0; i < windows.size(); ++i) {
windows[i]->update();
Expand All @@ -142,6 +158,15 @@ int main(int argc, char **argv) {
if (allWindowsClosed) {
quit = true;
}

++countedFrames;

int frameTicks = capTimer.getTicks();
if( frameTicks < 1000 / 60 )
{
//Wait remaining time
SDL_Delay( 1000 / 60 - frameTicks );
}
}

TTF_CloseFont(font);
Expand Down
2 changes: 1 addition & 1 deletion src/sdl/Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Emulator::render() {
this->renderWrapper.render(this->renderer);
SDL_RenderPresent(this->renderer);

SDL_Delay(16);
// SDL_Delay(16);
}

void Emulator::resize(SDL_Event &event) {
Expand Down

0 comments on commit eb860ea

Please sign in to comment.