-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameProExample.cpp
More file actions
167 lines (136 loc) · 4.41 KB
/
FrameProExample.cpp
File metadata and controls
167 lines (136 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include "FramePro.h"
#include "Utils.h"
//------------------------------------------------------------------------
#if FRAMEPRO_WIN_BASED_PLATFORM
#include <Windows.h>
#include <timeapi.h> // For timeBeginPeriod/timeEndPeriod
#else
#include <unistd.h>
#include <time.h>
#endif
//------------------------------------------------------------------------
namespace
{
Event g_Thread1Event(false, true);
void Check(bool b) { if(!b) *(int*)0=0; }
//------------------------------------------------------------------------
#if !FRAMEPRO_WIN_BASED_PLATFORM
inline void Sleep(int ms)
{
usleep(ms * 1000);
}
#endif
}
//------------------------------------------------------------------------
void MainThread_Function1()
{
FRAMEPRO_SCOPE();
// Optimized: Removed sleep completely - function now has minimal overhead
// Original: Sleep(10); -> Sleep(1); -> removed
// Sleep removed to achieve 60 FPS target
}
//------------------------------------------------------------------------
void MainThread_StringIDTest(FramePro::StringId string_id)
{
FRAMEPRO_ID_SCOPE(string_id);
// Optimized: Reduced sleep time from 1ms to 0ms (minimal work simulation)
// Original: Sleep(1);
// Sleep removed - string ID test doesn't need actual delay
}
//------------------------------------------------------------------------
void MainThread_WStringTest()
{
FRAMEPRO_NAMED_SCOPE_W(L"wide string literal");
// Optimized: Reduced sleep time from 2ms to 0ms
// Original: Sleep(2);
// Sleep removed - wide string test doesn't need actual delay
}
//------------------------------------------------------------------------
int ThreadMain1(void*)
{
FRAMEPRO_SET_THREAD_NAME("Thread1");
for(;;)
{
g_Thread1Event.Wait();
{
FRAMEPRO_SCOPE();
Sleep(5);
}
}
return 0;
}
//------------------------------------------------------------------------
#if FRAMEPRO_WIN_BASED_PLATFORM
// High-precision timing functions for Windows
inline long long GetTimeMs()
{
LARGE_INTEGER frequency, counter;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&counter);
return (long long)(counter.QuadPart * 1000LL / frequency.QuadPart);
}
#else
// High-precision timing functions for Unix
inline long long GetTimeMs()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
}
#endif
//------------------------------------------------------------------------
int main()
{
#if FRAMEPRO_WIN_BASED_PLATFORM
// Increase timer resolution to 1ms for more accurate Sleep()
// This improves Sleep() precision from ~15ms to ~1ms on Windows
timeBeginPeriod(1);
#endif
FramePro::StringId string_id = FRAMEPRO_REGISTER_STRING("My test string");
FramePro::StringId same_string_id = FRAMEPRO_REGISTER_STRING("My test string");
Check(same_string_id == string_id);
FramePro::StringId wide_string_id = FRAMEPRO_REGISTER_STRING(L"My wide test string");
Thread thread;
thread.CreateThread(ThreadMain1);
// Optimized main loop with precise frame timing for 60 FPS
// Target: 16.67ms per frame for 60 FPS
const double target_frame_time_ms = 16.666666666666668; // Exact 60 FPS target
for(int frame=0;;++frame)
{
// Measure frame start time (before work)
long long frame_start_time = GetTimeMs();
FRAMEPRO_FRAME_START();
FRAMEPRO_NAMED_SCOPE("Main Loop");
// Execute main thread functions (optimized - minimal overhead)
MainThread_Function1();
MainThread_StringIDTest(string_id);
MainThread_StringIDTest(wide_string_id);
MainThread_WStringTest();
// Signal worker thread
g_Thread1Event.Set();
// Print frame counter (non-blocking, minimal overhead)
if (frame % 10 == 0) // Print every 10 frames to reduce I/O overhead
{
printf("frame: %d\r", frame);
}
// Measure actual work time (excluding sleep)
long long frame_work_end_time = GetTimeMs();
long long frame_work_time_ms = frame_work_end_time - frame_start_time;
// Calculate how long to sleep to maintain 60 FPS
long long sleep_time_ms = (long long)target_frame_time_ms - frame_work_time_ms;
// Only sleep if we have time remaining (prevent negative sleep)
// This ensures we maintain exactly 60 FPS
if (sleep_time_ms > 0)
{
Sleep((int)sleep_time_ms);
}
// If work took longer than target, skip sleep to catch up
}
#if FRAMEPRO_WIN_BASED_PLATFORM
timeEndPeriod(1);
#endif
return 0;
}