aboutsummaryrefslogtreecommitdiffstats
path: root/examples/spacewars.cpp
diff options
context:
space:
mode:
authorSvenson Han Göthel <[email protected]>2024-03-18 19:11:46 +0100
committerSvenson Han Göthel <[email protected]>2024-03-18 19:11:46 +0100
commitaf8f3c9c667cc7b18799605eec90fa1e1c496c52 (patch)
tree7413bba2a7574508430b8923a1de57e42c8b7c43 /examples/spacewars.cpp
parentd833c44996e51ef2b6228d43c1d81b1b5938c08a (diff)
pixel::swap_gpu_buffer: Add optional forced fps value, allowing to reduce rendering load
Diffstat (limited to 'examples/spacewars.cpp')
-rw-r--r--examples/spacewars.cpp28
1 files changed, 2 insertions, 26 deletions
diff --git a/examples/spacewars.cpp b/examples/spacewars.cpp
index cecf8f6..84acb0e 100644
--- a/examples/spacewars.cpp
+++ b/examples/spacewars.cpp
@@ -290,7 +290,7 @@ int main(int argc, char *argv[])
int win_width = 1920, win_height = 1080;
std::string record_bmpseq_basename;
bool enable_vsync = true;
- int forced_fps = -1;
+ int forced_fps = 30;
bool one_player = true;
pixel::log_printf("X1\n");
{
@@ -371,7 +371,6 @@ int main(int argc, char *argv[])
uint64_t frame_count_total = 0;
uint64_t t_last = pixel::getElapsedMillisecond(); // [ms]
- uint64_t t_fps_last = pixel::getCurrentMilliseconds();
pixel::input_event_t event;
while( !event.pressed_and_clr( pixel::input_event_type_t::WINDOW_CLOSE_REQ ) ) {
@@ -385,8 +384,6 @@ int main(int argc, char *argv[])
const uint64_t t1 = pixel::getElapsedMillisecond(); // [ms]
const float dt = (float)( t1 - t_last ) / 1000.0f; // [s]
- const float dt_exp = 1.0f / (float)pixel::frames_per_sec; // [s]
- const float dt_diff = (float)( dt_exp - dt ) * 1000.0f; // [ms]
t_last = t1;
if(nullptr != ship_r){
@@ -457,7 +454,7 @@ int main(int argc, char *argv[])
const float sy = (float)text_height / (float)hud_text->height;
hud_text->draw(pixel::fb_width/2.0f-(hud_text->width*sy/2.0f), thickness_pixel, sy, sy);
}
- pixel::swap_gpu_buffer();
+ pixel::swap_gpu_buffer(forced_fps);
if( record_bmpseq_basename.size() > 0 ) {
std::string snap_fname(128, '\0');
const int written = std::snprintf(&snap_fname[0], snap_fname.size(), "%s-%7.7" PRIu64 ".bmp", record_bmpseq_basename.c_str(), frame_count_total);
@@ -465,27 +462,6 @@ int main(int argc, char *argv[])
pixel::save_snapshot(snap_fname);
}
++frame_count_total;
- if( 0 < forced_fps ) {
- const int64_t fudge_ns = pixel::NanoPerMilli / 4;
- const uint64_t ms_per_frame = (uint64_t)std::round(1000.0 / forced_fps);
- const uint64_t ms_last_frame = pixel::getCurrentMilliseconds() - t_fps_last;
- int64_t td_ns = int64_t( ms_per_frame - ms_last_frame ) * pixel::NanoPerMilli;
- if( td_ns > fudge_ns )
- {
- if( true ) {
- const int64_t td_ns_0 = td_ns%pixel::NanoPerOne;
- struct timespec ts { td_ns/pixel::NanoPerOne, td_ns_0 - fudge_ns };
- nanosleep( &ts, NULL );
- // pixel::log_printf("soft-sync [exp %zd > has %zd]ms, delay %" PRIi64 "ms (%lds, %ldns)\n", ms_per_frame, ms_last_frame, td_ns/pixel::NanoPerMilli, ts.tv_sec, ts.tv_nsec);
- } else {
- pixel::milli_sleep( td_ns / pixel::NanoPerMilli );
- // pixel::log_printf("soft-sync [exp %zd > has %zd]ms, delay %" PRIi64 "ms\n", ms_per_frame, ms_last_frame, td_ns/pixel::NanoPerMilli);
- }
- }
- } else if( dt_diff > 1.0f ) {
- pixel::milli_sleep( (uint64_t)dt_diff );
- }
- t_fps_last = pixel::getCurrentMilliseconds();
}
printf("Exit");
exit(0);