SpaceCadetPinball/SpaceCadetPinball/winmain.h

117 lines
3.1 KiB
C
Raw Permalink Normal View History

2020-11-05 16:44:34 +01:00
#pragma once
#include "gdrv.h"
2020-11-05 16:44:34 +01:00
enum class GameBindings;
struct SdlTickClock
{
using duration = std::chrono::milliseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<SdlTickClock>;
static constexpr bool is_steady = true;
static time_point now() noexcept
{
return time_point{duration{SDL_GetTicks()}};
}
};
struct SdlPerformanceClock
{
using duration = std::chrono::duration<uint64_t, std::nano>;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<SdlPerformanceClock>;
static constexpr bool is_steady = true;
static time_point now() noexcept
{
const auto freq = SDL_GetPerformanceFrequency();
const auto ctr = SDL_GetPerformanceCounter();
const auto whole = (ctr / freq) * period::den;
const auto part = (ctr % freq) * period::den / freq;
return time_point(duration(whole + part));
}
};
2021-11-18 15:58:53 +01:00
struct WelfordState
{
double mean;
double M2;
int64_t count;
WelfordState() : mean(0.005), M2(0), count(1)
{
}
void Advance(double newValue)
{
++count;
auto delta = newValue - mean;
mean += delta / count;
M2 += delta * (newValue - mean); //M2n = M2n-1 + (Xn - AvgXn-1) * (Xn - AvgXn)
}
double GetStdDev() const
{
return std::sqrt(M2 / (count - 1)); // Sn^2 = M2n / (n - 1)
}
};
2020-11-07 16:41:14 +01:00
class winmain
{
using Clock = SdlPerformanceClock; // Or std::chrono::steady_clock.
using DurationMs = std::chrono::duration<double, std::milli>;
using TimePoint = std::chrono::time_point<Clock>;
2020-11-07 16:41:14 +01:00
public:
static constexpr const char* Version = "2.1.1 DEV";
static bool single_step;
static SDL_Window* MainWindow;
static SDL_Renderer* Renderer;
static ImGuiIO* ImIO;
static bool LaunchBallEnabled;
static bool HighScoresEnabled;
static bool DemoActive;
static int MainMenuHeight;
2020-12-02 18:12:34 +01:00
static int WinMain(LPCSTR lpCmdLine);
static int event_handler(const SDL_Event* event);
[[ noreturn ]] static void memalloc_failure();
2020-11-07 16:41:14 +01:00
static int ProcessWindowMessages();
static void a_dialog();
2020-12-02 18:12:34 +01:00
static void end_pause();
static void new_game();
2022-01-05 09:38:50 +01:00
static void pause(bool toggle = true);
static void Restart();
static void UpdateFrameRate();
static void HandleGameBinding(GameBindings binding, bool shortcut);
2020-11-07 16:41:14 +01:00
private:
static int return_value;
static int mouse_down, last_mouse_x, last_mouse_y;
static bool no_time_loss, activated, bQuit, has_focus, DispGRhistory, DispFrameRate;
2022-12-13 13:06:27 +01:00
static std::vector<float> gfrDisplay;
static std::string FpsDetails, PrevSdlError;
static bool restart;
static bool ShowAboutDialog;
static bool ShowImGuiDemo;
static bool ShowSpriteViewer;
static bool ShowExitPopup;
static double UpdateToFrameRatio;
static DurationMs TargetFrameTime;
static struct optionsStruct& Options;
2021-11-18 15:58:53 +01:00
static DurationMs SpinThreshold;
static WelfordState SleepState;
static unsigned PrevSdlErrorCount;
2022-12-13 13:06:27 +01:00
static unsigned gfrOffset;
static float gfrWindow;
static int CursorIdleCounter;
static void RenderUi();
static void RenderFrameTimeDialog();
2021-11-18 15:58:53 +01:00
static void HybridSleep(DurationMs seconds);
static void MainLoop();
static void ImGuiMenuItemWShortcut(GameBindings binding, bool selected = false);
2020-11-07 16:41:14 +01:00
};