- move Timer into sb namespace

- debug statement for FPS
- string conversion in Switch class
This commit is contained in:
ohsqueezy 2023-05-30 16:20:28 -04:00
parent 7140e8a3eb
commit 9f33ac872f
6 changed files with 75 additions and 42 deletions

View File

@ -2,7 +2,7 @@
Animation::Animation()
{
timer.toggle(Timer::OFF);
timer.toggle(sb::Timer::OFF);
}
void Animation::play(float delay, bool play_once)

View File

@ -29,7 +29,7 @@ private:
float delay = 0, overflow = 0, frame_length = 0;
callback step = nullptr;
Node* containing_object = nullptr;
Timer timer = Timer();
sb::Timer timer = sb::Timer();
public:
@ -44,7 +44,7 @@ public:
Animation(void(T::*member_function)(), T* object, float frame_length = 0) : frame_length(frame_length)
{
bind(member_function, object);
timer.toggle(Timer::OFF);
timer.toggle(sb::Timer::OFF);
}
/* Set the animation function by supplying an object and one of its member functions */

View File

@ -564,12 +564,14 @@ void Game::frame(float ticks)
frame_count_this_second++;
if (ticks - last_frame_count_timestamp >= 1000)
{
std::ostringstream message;
message << "Counted " << frame_count_this_second << " frames last second";
sb::Log::log(message, sb::Log::DEBUG);
framerate_indicator.refresh();
last_frame_count_timestamp = ticks;
frame_count_this_second = 0;
}
}
// std::cout << std::endl;
}
#if defined(__EMSCRIPTEN__)

View File

@ -73,6 +73,7 @@ The expected output is
#pragma once
#include <functional>
#include <ostream>
namespace sb
{
@ -144,5 +145,20 @@ namespace sb
return state;
}
operator std::string() const
{
return state ? "true" : "false";
}
};
}
namespace std
{
template<typename return_type, typename... arguments>
std::ostream& operator<<(std::ostream& stream, const sb::Switch<return_type, arguments...>& sw)
{
stream << std::string(sw);
return stream;
}
}

View File

@ -5,32 +5,32 @@
* may change in the future). The ticks and previous_ticks parameters are initially synchronized with the SDL
* ticks function, which returns the number of milliseconds since the program began, and the SDL ticks function
* is used to keep the time updated. */
Timer::Timer()
sb::Timer::Timer()
{
ticks = SDL_GetTicks();
ticks_previous = ticks;
}
/* Check whether Timer is keeping time or not */
bool Timer::state()
bool sb::Timer::state()
{
return timing;
}
/* Toggle timing on/off */
void Timer::toggle()
void sb::Timer::toggle()
{
toggle(!state());
}
/* Set state explicitly to Timer::ON (true) or Timer::OFF (false) */
void Timer::toggle(bool state)
/* Set state explicitly to sb::Timer::ON (true) or sb::Timer::OFF (false) */
void sb::Timer::toggle(bool state)
{
timing = state;
}
/* Reset time elapsed to zero */
void Timer::reset()
void sb::Timer::reset()
{
ticks_elapsed = 0;
ticks = SDL_GetTicks();
@ -39,33 +39,33 @@ void Timer::reset()
}
/* Return milliseconds elapsed on timer */
float Timer::ms_elapsed()
float sb::Timer::ms_elapsed()
{
return ticks_elapsed;
}
/* Return seconds elapsed on timer */
float Timer::seconds_elapsed()
float sb::Timer::seconds_elapsed()
{
return ms_elapsed() / 1000.0f;
}
/* Return the length of the previous frame in milliseconds (the time between the last two calls to
* the timer update function) */
float Timer::ms_last_frame()
float sb::Timer::ms_last_frame()
{
return frame_duration;
}
/* Return the length of the previous frame in seconds (the time between the last two calls to the
* timer update function) */
float Timer::seconds_last_frame()
float sb::Timer::seconds_last_frame()
{
return ms_last_frame() / 1000.0f;
}
/* Add time to the timer by measuring the time between this call and the previous call to update */
void Timer::update()
void sb::Timer::update()
{
ticks = SDL_GetTicks();
frame_duration = ticks - ticks_previous;

View File

@ -12,33 +12,48 @@
#include "SDL.h"
class Timer
namespace sb
{
public:
enum State : bool
class Timer
{
OFF,
ON
private:
bool timing = Timer::ON;
int ticks = 0, ticks_previous = 0, frame_duration = 0, ticks_elapsed = 0;
public:
enum State : bool
{
OFF,
ON
};
Timer();
bool state();
void toggle();
void toggle(bool);
void reset();
float ms_elapsed();
float seconds_elapsed();
float ms_last_frame();
float seconds_last_frame();
void update();
/*!
* Applies delta timing to a value: returns the value as weighted by the amount of time passed since the
* last frame update, allowing for values to change the same amount over time independent of the frame rate.
* The amount passed is how much the value should change per second.
*
* @param amount any scalar value to be weighted
* @return weighted value
*/
template<typename Type>
Type weigh(Type amount)
{
return seconds_last_frame() * amount;
}
};
private:
bool timing = Timer::ON;
int ticks = 0, ticks_previous = 0, frame_duration = 0, ticks_elapsed = 0;
public:
Timer();
bool state();
void toggle();
void toggle(bool);
void reset();
float ms_elapsed();
float seconds_elapsed();
float ms_last_frame();
float seconds_last_frame();
void update();
};
}