Skip to content

Commit

Permalink
added FPS_LIMIT; made fps counters more accurate; fixed some weirdnes…
Browse files Browse the repository at this point in the history
…s with the window
  • Loading branch information
farzher committed Mar 6, 2023
1 parent e460c4b commit dacab28
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion first.jai
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ build :: () {
// seperate build options for debug vs release
build_options((options: *Build_Options) {
options.output_executable_name = "bunnymark";
options.emit_debug_info = .NONE;

#if !RELEASE then options.backend = .X64;
#if RELEASE then #run Windows_Resources.disable_runtime_console();
#if RELEASE then options.emit_debug_info = .NONE;
});

// wait for the exe to finish compiling, so we can set its icon
Expand Down
2 changes: 1 addition & 1 deletion src/functions.jai
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fps_man: struct {
if elapsed < .5 return;

last_time = get_time();
fps = cast(int)( (frame_count - last_frame_count) / elapsed );
fps = xx floor(.5+ (frame_count - last_frame_count) / elapsed );
last_frame_count = frame_count;
ready = true;
}
Expand Down
27 changes: 20 additions & 7 deletions src/main.jai
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ window_width :: 1920/2;
window_height :: 1080/2;

VSYNC :: false;
FPS_LIMIT :: 0; // 0 to disable FPS_LIMIT
DEBUG :: #run cmdline_flag("DEBUG", or=false); // disables shader optimization
DISABLE_MOUSE_EVENTS :: true; // this prevents fps drops when moving the mouse. but this also causes some weirdness

Expand Down Expand Up @@ -43,6 +44,15 @@ main :: () {

while true {

#if FPS_LIMIT {
Global(#code fps_limit_last_time: float64);
if fps_limit_last_time == 0 fps_limit_last_time = 1.0/FPS_LIMIT *-1;

now := get_time();
if (now - fps_limit_last_time) < 1.0/FPS_LIMIT continue;
fps_limit_last_time = now;
}

inputs();
update();
render();
Expand Down Expand Up @@ -103,7 +113,8 @@ Bunny :: struct {
bind_buffer(0, six_vertex_for_a_square_buffer);
bind_buffer(1, instance_buffer);

// Instanced Draw call
// Instanced Draw call of trianglelist
ID3D11DeviceContext_IASetPrimitiveTopology(d3d_context, .TRIANGLELIST);
ID3D11DeviceContext_DrawInstanced(d3d_context, 6, xx bunnies.count, 0, 0);
}
}
Expand All @@ -125,16 +136,14 @@ inputs :: () {
if event.key_code == .MOUSE_BUTTON_LEFT ismousedown = xx event.key_pressed;
}

// this prevents fps drops when moving the mouse. but this also causes some weirdness
#if DISABLE_MOUSE_EVENTS {
ismousedown = xx (GetAsyncKeyState(VK_LBUTTON) & 0x8000);
mousepos := Mouse.pos(hwnd);

is_mouse_over_window_title_bar := mousepos.x >= -10 && mousepos.x <= window_width + 10
is_mouse_over_window_title_bar := mousepos.x >= -10 /*&& mousepos.x <= window_width + 10*/
&& mousepos.y >= -30 && mousepos.y <= 0;
if is_mouse_over_window_title_bar enable_mouse_events(); else disable_mouse_events();
// is_mouse_inside_window := mousepos.x >= 0 && mousepos.x <= window_width
// && mousepos.y >= 0 && mousepos.y <= window_height;
// if is_mouse_inside_window disable_mouse_events(); else enable_mouse_events();
}
}

Expand All @@ -160,10 +169,10 @@ update :: () {


// update window title with stats every once in a while
Global(#code max_dt: float);
Global(#code max_dt: float64);
if dt > max_dt max_dt = dt;
if fps_man.ready { fps_man.ready = false;
worst_fps : int = xx (1000 / (max_dt*1000));
worst_fps : int = xx floor(.5+ (1.0 / max_dt));
set_window_title(_("Bunnymark Jai D3D11 | FPS: #{fps_man.fps} | 0% Low: #{worst_fps} | Buns: #{bunnies.count}"));
max_dt = 0;
}
Expand Down Expand Up @@ -278,3 +287,7 @@ init_bunny_shader :: () {
}
HLSL);
}

// note: alt+f4 doesn't close the app because modules/input/windows.jai needs this code in case WM_KEYDOWN;
// if wParam == VK_F4 return DefWindowProcW(hwnd, message, wParam, lParam); // need this here to handle alt+f4 !
// this is currently a problem with all jai applications

0 comments on commit dacab28

Please sign in to comment.