@@ -228,6 +228,16 @@ static void _draw_text(IDirect3DDevice9 *device, ID3DXFont *font, uint32_t font_
228228 ID3DXFont_DrawText (font , NULL , text , -1 , & rect , DT_LEFT | DT_TOP , D3DCOLOR_XRGB (255 , 255 , 255 ));
229229}
230230
231+ static bool _is_esc_key_pressed ()
232+ {
233+ return GetAsyncKeyState (VK_ESCAPE ) & 0x8000 ;
234+ }
235+
236+ static bool _is_esc_key_released ()
237+ {
238+ return !(GetAsyncKeyState (VK_ESCAPE ) & 0x8000 );
239+ }
240+
231241static bool _adapter ()
232242{
233243 IDirect3D9 * d3d ;
@@ -433,7 +443,12 @@ static bool _run(uint32_t width, uint32_t height, uint32_t refresh_rate, uint32_
433443 break ;
434444 }
435445
436- if (GetAsyncKeyState (VK_ESCAPE ) & 0x8000 ) {
446+ if (_is_esc_key_pressed ()) {
447+ // Avoid multi triggering with further key evaluations
448+ while (!_is_esc_key_released ()) {
449+ Sleep (10 );
450+ }
451+
437452 exit_loop = true;
438453 break ;
439454 }
@@ -532,31 +547,62 @@ static bool _run(uint32_t width, uint32_t height, uint32_t refresh_rate, uint32_
532547 } else {
533548 _draw_text (device , font , font_height , text_offset_x , text_offset_y * 6 , "Status: Finished" );
534549 }
535-
550+
536551 _draw_text (device , font , font_height , text_offset_x , text_offset_y * 7 ,
537552 "Total warm-up frame count: %d" , warm_up_frame_count );
538553 _draw_text (device , font , font_height , text_offset_x , text_offset_y * 8 ,
539554 "Total sample frame count: %d" , frame_count );
540555 _draw_text (device , font , font_height , text_offset_x , text_offset_y * 9 ,
541- "Avg frame time: %.3f ms" , total_elapsed_us / frame_count / 1000.0f );
556+ "Avg frame time: %.3f ms" , total_elapsed_us > 0 && frame_count > 0 ? total_elapsed_us / frame_count / 1000.0f : 0 );
542557 _draw_text (device , font , font_height , text_offset_x , text_offset_y * 10 ,
543- "Avg refresh rate: %.3f Hz" , 1000.0f / (total_elapsed_us / frame_count / 1000.0f ));
558+ "Avg refresh rate: %.3f Hz" , total_elapsed_us > 0 && frame_count > 0 ? 1000.0f / (total_elapsed_us / frame_count / 1000.0f ) : 0 );
544559
545560 _draw_text (device , font , font_height , text_offset_x , text_offset_y * 12 , "Exiting in 5 seconds ..." );
561+ _draw_text (device , font , font_height , text_offset_x , text_offset_y * 13 , "Press ESC to exit immediately" );
546562
547563 IDirect3DDevice9_EndScene (device );
548564 IDirect3DDevice9_Present (device , NULL , NULL , NULL , NULL );
549565
550- Sleep (5000 );
566+ exit_loop = false;
567+
568+ for (uint32_t i = 0 ; i < 5000 / 10 ; i ++ ) {
569+ // Required to not make windows think we are stuck and not responding
570+ while (PeekMessage (& msg , NULL , 0 , 0 , PM_REMOVE )) {
571+ if (msg .message == WM_QUIT ) {
572+ exit_loop = true;
573+ break ;
574+ }
575+
576+ TranslateMessage (& msg );
577+ DispatchMessage (& msg );
578+ }
579+
580+ if (exit_loop ) {
581+ break ;
582+ }
583+
584+ // Allow quick exit
585+ if (_is_esc_key_pressed ()) {
586+ // Avoid multi triggering with further key evaluations
587+ while (!_is_esc_key_released ()) {
588+ Sleep (10 );
589+ }
590+
591+ exit_loop = true;
592+ break ;
593+ }
594+
595+ Sleep (10 );
596+ }
551597
552598 // ---------------------------------------------------------------------------------------------
553599
554600 printfln_err ("Final results" );
555601 printfln_out ("GPU: %s" , identifier .Description );
556602 printfln_out ("Spec: %d x %d @ %d hz, %s, vsync %s" , width , height , refresh_rate ,
557603 windowed ? "windowed" : "fullscreen" , vsync_off ? "off" : "on" );
558- printfln_out ("Avg frame time (ms): %.3f" , total_elapsed_us / frame_count / 1000.0f );
559- printfln_out ("Avg refresh rate (hz): %.3f" , 1000.0f / (total_elapsed_us / frame_count / 1000.0f ));
604+ printfln_out ("Avg frame time (ms): %.3f" , total_elapsed_us > 0 && frame_count > 0 ? total_elapsed_us / frame_count / 1000.0f : 0 );
605+ printfln_out ("Avg refresh rate (hz): %.3f" , total_elapsed_us > 0 && frame_count > 0 ? 1000.0f / (total_elapsed_us / frame_count / 1000.0f ) : 0 );
560606
561607 ID3DXFont_Release (font );
562608 IDirect3DDevice9_Release (device );
0 commit comments