From fcb2997e69f066e2a6f7863da82f22eed3af7d79 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Fri, 1 Mar 2024 15:06:58 +0800 Subject: [PATCH] More updates to the mingw environment --- .clangd | 4 ++-- Main.cpp | 7 +++++++ Makefile.flags | 2 +- Types.c | 26 +++++++++++++++++++++++--- Types.h | 15 +++++++++------ 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/.clangd b/.clangd index 5fb9f17..35c56ef 100644 --- a/.clangd +++ b/.clangd @@ -1,9 +1,9 @@ If: PathMatch: [.*\.h, .*\.c] CompileFlags: - Add: [-xc, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu] + Add: [-xc, -DWIN32_LEAN_AND_MEAN, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu] --- If: PathMatch: [.*\.hpp, .*\.cpp] CompileFlags: - Add: [-xc++, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu] + Add: [-xc++, -DWIN32_LEAN_AND_MEAN, -I/home/edgar/Codes/JacksEscape/easyx/include, -target, x86_64-pc-windows-gnu] diff --git a/Main.cpp b/Main.cpp index aed442d..466d3e3 100644 --- a/Main.cpp +++ b/Main.cpp @@ -1,10 +1,17 @@ #include +#include + +#include "Types.h" int main() { + TimePoint startup = time_Now(); + initgraph(640, 480); circle(200, 200, 100); Sleep(5000); closegraph(); + + printf("%.6lf seconds has elapsed\n", duration_Seconds(time_Since(startup))); return 0; } diff --git a/Makefile.flags b/Makefile.flags index 8d85462..85700e7 100644 --- a/Makefile.flags +++ b/Makefile.flags @@ -37,7 +37,7 @@ endif export INCLUDEFLAGS := $(Platform_INCLUDEFLAGS) -export CPPFLAGS = +export CPPFLAGS = -DWIN32_LEAN_AND_MEAN export CFLAGS = $(INCLUDEFLAGS) $(BUILD_OPTIONS) -O2 -Wno-attributes -flto export CXXFLAGS = $(INCLUDEFLAGS) -DHELOS $(BUILD_OPTIONS) $(Platform_CXXFLAGS) -O2 -Wno-unused-result -flto -std=c++17 export LDFLAGS = $(Platform_LDFLAGS) -s -O2 -flto diff --git a/Types.c b/Types.c index 0bd9f86..abfa066 100644 --- a/Types.c +++ b/Types.c @@ -1,5 +1,6 @@ #include "Types.h" +#include static inline double dmin(double x, double y) { return x < y ? x : y; @@ -70,9 +71,28 @@ Box2 box2_OffsetY(Box2 box, double offsetY) { } -Duration duration_Now() { - // TODO Use Windows.h +static double freqInverse = 0.0; + +TimePoint time_Now() { // Reference: https://github.com/SFML/SFML/blob/2.6.x/src/SFML/System/Win32/ClockImpl.cpp - Duration d = {.microseconds = 0}; + + if (freqInverse == 0.0) { + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + freqInverse = 1000000.0 / ((double)freq.QuadPart); + } + + LARGE_INTEGER time; + QueryPerformanceCounter(&time); + + TimePoint t = {.microseconds = (int64_t)(((double)time.QuadPart) * freqInverse)}; + return t; +} + +Duration time_Since(TimePoint prev) { + return time_Difference(time_Now(), prev); +} +Duration time_Difference(TimePoint now, TimePoint prev) { + Duration d = {.microseconds = now.microseconds - prev.microseconds}; return d; } diff --git a/Types.h b/Types.h index 8b4f7ab..122db42 100644 --- a/Types.h +++ b/Types.h @@ -40,17 +40,20 @@ Box2 box2_OffsetY(Box2 box, double offsetY); // Time duration. typedef struct { - uint64_t microseconds; + int64_t microseconds; } Duration; static inline double duration_Seconds(const Duration t) { return ((double)t.microseconds) / 1000.0 / 1000.0; } static inline double duration_Milliseconds(const Duration t) { return ((double)t.microseconds) / 1000.0; } -// Returns a relative duration since -// a static time point in the past. -// -// Its absolute value has no meaning. -Duration duration_Now(); +// A Time point since the start of the program. +typedef struct { + int64_t microseconds; +} TimePoint; + +TimePoint time_Now(); +Duration time_Since(TimePoint prev); +Duration time_Difference(TimePoint now, TimePoint prev); #ifdef __cplusplus