delta time usage

This commit is contained in:
Masashi 2025-12-17 16:54:18 +05:30
commit 1b26754f8f
16 changed files with 667 additions and 348 deletions

View file

@ -1,4 +1,4 @@
#include "../libsuicmez/libsuicmez.h"
#include "libsuicmez/libsuicmez.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

View file

@ -1,4 +1,4 @@
#include "../libsuicmez/libsuicmez.h"
#include "libsuicmez/libsuicmez.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
@ -30,8 +30,8 @@ int suic_main(void);
int suic_main(void) {
suic_enable_msaa_4x();
suic_init_window(1280, 720, suic_alloc_array(NULL, sizeof(char), 75, "Sui FPS Game - WASD to move, Mouse to look, SPACE to jump, SHIFT to sprint"));
suic_set_target_fps(60);
suic_init_window(1280, 720, suic_alloc_array(NULL, sizeof(char), 112, "Sui FPS Game - WASD to move, Mouse to look, SPACE to jump, SHIFT to sprint, F1 cursor, F2 hide FPS, F3 show FPS"));
suic_show_fps_meter((suic_vec2){ .x = 10.000000, .y = 10.000000 });
suic_disable_cursor();
suic_ode_init();
void* world = suic_ode_world_create();
@ -75,6 +75,7 @@ int suic_main(void) {
float yaw = 0.000000;
float pitch = 0.000000;
float mouse_sensitivity = 0.003000;
int fps_meter_visible = 1;
float cube1_x = 0.000000;
float cube1_y = 0.000000;
float cube1_z = 0.000000;
@ -88,7 +89,27 @@ int suic_main(void) {
float player_y = 2.000000;
float player_z = 0.000000;
while ((suic_window_should_close() == false)) {
float delta_time = 0.016660;
float delta_time = suic_get_frame_time();
float mouse_delta_x = suic_get_mouse_delta_x();
float mouse_delta_y = suic_get_mouse_delta_y();
yaw = (yaw + (mouse_delta_x * mouse_sensitivity));
pitch = (pitch - (mouse_delta_y * mouse_sensitivity));
if ((pitch > 1.500000)) {
pitch = 1.500000;
}
if ((pitch < -1.500000)) {
pitch = -1.500000;
}
float cos_yaw = cos(yaw);
float sin_yaw = sin(yaw);
float cos_pitch = cos(pitch);
float sin_pitch = sin(pitch);
forward_x = sin_yaw;
forward_y = sin_pitch;
forward_z = -cos_yaw;
right_x = cos_yaw;
right_y = 0.000000;
right_z = sin_yaw;
float move_speed = 5.000000;
float target_vx = 0.000000;
float target_vz = 0.000000;
@ -122,6 +143,11 @@ int suic_main(void) {
}
}
suic_ode_body_set_linear_vel(player_body, target_vx, current_vy, target_vz);
suic_ode_space_collide(world, space, contactgroup);
if ((delta_time > 0.000000)) {
suic_ode_world_step(world, delta_time);
}
suic_ode_joint_group_empty(contactgroup);
suic_ode_body_get_position(player_body, &player_x, &player_y, &player_z);
if (((suic_is_key_pressed(KEY_EQUAL) != 0) || (suic_is_key_pressed(KEY_KP_ADD) != 0))) {
mouse_sensitivity = (mouse_sensitivity + 0.001000);
@ -135,29 +161,14 @@ int suic_main(void) {
if ((suic_is_key_pressed(KEY_F1) != 0)) {
suic_enable_cursor();
}
float mouse_delta_x = suic_get_mouse_delta_x();
float mouse_delta_y = suic_get_mouse_delta_y();
yaw = (yaw + (mouse_delta_x * mouse_sensitivity));
pitch = (pitch - (mouse_delta_y * mouse_sensitivity));
if ((pitch > 1.500000)) {
pitch = 1.500000;
if ((suic_is_key_pressed(KEY_F2) != 0)) {
suic_hide_fps_meter();
fps_meter_visible = 0;
}
if ((pitch < -1.500000)) {
pitch = -1.500000;
if ((suic_is_key_pressed(KEY_F3) != 0)) {
suic_show_fps_meter((suic_vec2){ .x = 10.000000, .y = 10.000000 });
fps_meter_visible = 1;
}
suic_ode_space_collide(world, space, contactgroup);
suic_ode_world_step(world, delta_time);
suic_ode_joint_group_empty(contactgroup);
float cos_yaw = cos(yaw);
float sin_yaw = sin(yaw);
float cos_pitch = cos(pitch);
float sin_pitch = sin(pitch);
forward_x = sin_yaw;
forward_y = sin_pitch;
forward_z = -cos_yaw;
right_x = cos_yaw;
right_y = 0.000000;
right_z = sin_yaw;
camera_x = (player_x + (forward_x * 0.500000));
camera_y = (player_y + 0.900000);
camera_z = (player_z + (forward_z * 0.500000));

Binary file not shown.

View file

@ -6,12 +6,15 @@ fn main() -> int do
enable_msaa_4x()
# Initialize graphics
init_window(1280, 720, "Sui FPS Game - WASD to move, Mouse to look, SPACE to jump, SHIFT to sprint")
defer close_window()
set_target_fps(60)
init_window(1280, 720, "Sui FPS Game - WASD to move, Mouse to look, SPACE to jump, SHIFT to sprint, F1 cursor, F2 hide FPS, F3 show FPS")
defer close_window()
# set_target_fps(60)
# Hide cursor and capture mouse for FPS controls
disable_cursor()
# Show FPS meter
show_fps_meter(Vec2 { x: 10.0, y: 10.0 })
# Hide cursor and capture mouse for FPS controls
disable_cursor()
# Initialize physics
ode_init()
@ -93,6 +96,9 @@ fn main() -> int do
let mut yaw = 0.0
let mut pitch = 0.0
let mut mouse_sensitivity = 0.003
# FPS meter toggle
let mut fps_meter_visible = 1
# Cube position variables
let mut cube1_x = 0.0
@ -105,115 +111,128 @@ fn main() -> int do
let mut cube3_y = 0.0
let mut cube3_z = 0.0
# Player position (initial values)
let mut player_x = 0.0
let mut player_y = 2.0
let mut player_z = 0.0
# Player position (initial values)
let mut player_x = 0.0
let mut player_y = 2.0
let mut player_z = 0.0
# Main game loop
while window_should_close() == false do
# Input handling
let delta_time = 0.01666 # ~60 FPS
# Main game loop
while window_should_close() == false do
# Get actual frame delta time
let delta_time = get_frame_time()
# WASD movement - set velocities on physics body
let move_speed = 5.0 # Velocity applied to body
let mut target_vx = 0.0
let mut target_vz = 0.0
# Mouse look
let mouse_delta_x = get_mouse_delta_x()
let mouse_delta_y = get_mouse_delta_y()
if is_key_down(KEY_W) != 0 do
target_vx = target_vx + forward_x * move_speed
target_vz = target_vz + forward_z * move_speed
end
yaw = yaw + (mouse_delta_x * mouse_sensitivity)
pitch = pitch - (mouse_delta_y * mouse_sensitivity) # Negate Y for natural FPS controls
if is_key_down(KEY_S) != 0 do
target_vx = target_vx - forward_x * move_speed
target_vz = target_vz - forward_z * move_speed
end
# Clamp pitch to prevent flipping
if pitch > 1.5 do
pitch = 1.5
end
if pitch < -1.5 do
pitch = -1.5
end
if is_key_down(KEY_D) != 0 do
target_vx = target_vx + right_x * move_speed
target_vz = target_vz + right_z * move_speed
end
# Calculate camera direction from yaw and pitch
let cos_yaw = cos(yaw)
let sin_yaw = sin(yaw)
let cos_pitch = cos(pitch)
let sin_pitch = sin(pitch)
if is_key_down(KEY_A) != 0 do
target_vx = target_vx - right_x * move_speed
target_vz = target_vz - right_z * move_speed
end
# Update forward and right vectors FIRST (before movement)
forward_x = sin_yaw
forward_y = sin_pitch
forward_z = -cos_yaw
# Get current velocity for Y (preserve gravity)
let mut current_vx = 0.0
let mut current_vy = 0.0
let mut current_vz = 0.0
ode_body_get_linear_vel(player_body, &current_vx, &current_vy, &current_vz)
right_x = cos_yaw
right_y = 0.0
right_z = sin_yaw
# Jump - set upward velocity
if is_jump_pressed() != 0 do
# Get current position to check if on ground
let mut dummy_x = 0.0
let mut current_y = 0.0
let mut dummy_z = 0.0
ode_body_get_position(player_body, &dummy_x, &current_y, &dummy_z)
if current_y <= 1.3 do # On ground (box bottom is at y - 0.8 from center)
current_vy = 8.0 # Jump velocity
end
end
# WASD movement - set velocities on physics body
let move_speed = 5.0 # Units per second
let mut target_vx = 0.0
let mut target_vz = 0.0
# Set new velocity (keep Y velocity for gravity/jumping)
ode_body_set_linear_vel(player_body, target_vx, current_vy, target_vz)
if is_key_down(KEY_W) != 0 do
target_vx = target_vx + forward_x * move_speed
target_vz = target_vz + forward_z * move_speed
end
# Get player position from physics body
ode_body_get_position(player_body, &player_x, &player_y, &player_z)
if is_key_down(KEY_S) != 0 do
target_vx = target_vx - forward_x * move_speed
target_vz = target_vz - forward_z * move_speed
end
# Adjust mouse sensitivity
if is_key_pressed(KEY_EQUAL) != 0 or is_key_pressed(KEY_KP_ADD) != 0 do # + key
mouse_sensitivity = mouse_sensitivity + 0.001
end
if is_key_pressed(KEY_MINUS) != 0 or is_key_pressed(KEY_KP_SUBTRACT) != 0 do # - key
mouse_sensitivity = mouse_sensitivity - 0.001
if mouse_sensitivity < 0.001 do
mouse_sensitivity = 0.001 # Minimum sensitivity
end
end
if is_key_down(KEY_D) != 0 do
target_vx = target_vx + right_x * move_speed
target_vz = target_vz + right_z * move_speed
end
# Show cursor with F1 (useful for debugging)
if is_key_pressed(KEY_F1) != 0 do
enable_cursor()
end
if is_key_down(KEY_A) != 0 do
target_vx = target_vx - right_x * move_speed
target_vz = target_vz - right_z * move_speed
end
# Mouse look
let mouse_delta_x = get_mouse_delta_x()
let mouse_delta_y = get_mouse_delta_y()
# Get current velocity for Y (preserve gravity)
let mut current_vx = 0.0
let mut current_vy = 0.0
let mut current_vz = 0.0
ode_body_get_linear_vel(player_body, &current_vx, &current_vy, &current_vz)
yaw = yaw + (mouse_delta_x * mouse_sensitivity)
pitch = pitch - (mouse_delta_y * mouse_sensitivity) # Negate Y for natural FPS controls
# Jump - set upward velocity
if is_jump_pressed() != 0 do
# Get current position to check if on ground
let mut dummy_x = 0.0
let mut current_y = 0.0
let mut dummy_z = 0.0
ode_body_get_position(player_body, &dummy_x, &current_y, &dummy_z)
if current_y <= 1.3 do # On ground (box bottom is at y - 0.8 from center)
current_vy = 8.0 # Jump velocity
end
end
# Clamp pitch to prevent flipping
if pitch > 1.5 do
pitch = 1.5
end
if pitch < -1.5 do
pitch = -1.5
end
# Set new velocity (keep Y velocity for gravity/jumping)
ode_body_set_linear_vel(player_body, target_vx, current_vy, target_vz)
# Update physics (just for cubes)
ode_space_collide(world, space, contactgroup)
ode_world_step(world, delta_time)
ode_joint_group_empty(contactgroup)
# Update physics (just for cubes)
ode_space_collide(world, space, contactgroup)
# Only step physics if delta_time is valid (guard against first frame which is 0)
if delta_time > 0.0 do
ode_world_step(world, delta_time)
end
ode_joint_group_empty(contactgroup)
# Calculate camera direction from yaw and pitch
let cos_yaw = cos(yaw)
let sin_yaw = sin(yaw)
let cos_pitch = cos(pitch)
let sin_pitch = sin(pitch)
# Get player position from physics body
ode_body_get_position(player_body, &player_x, &player_y, &player_z)
# Update forward and right vectors
forward_x = sin_yaw
forward_y = sin_pitch
forward_z = -cos_yaw
# Adjust mouse sensitivity
if is_key_pressed(KEY_EQUAL) != 0 or is_key_pressed(KEY_KP_ADD) != 0 do # + key
mouse_sensitivity = mouse_sensitivity + 0.001
end
if is_key_pressed(KEY_MINUS) != 0 or is_key_pressed(KEY_KP_SUBTRACT) != 0 do # - key
mouse_sensitivity = mouse_sensitivity - 0.001
if mouse_sensitivity < 0.001 do
mouse_sensitivity = 0.001 # Minimum sensitivity
end
end
right_x = cos_yaw
right_y = 0.0
right_z = sin_yaw
# Show cursor with F1 (useful for debugging)
if is_key_pressed(KEY_F1) != 0 do
enable_cursor()
end
# Toggle FPS meter with F2 (hide) and F3 (show)
if is_key_pressed(KEY_F2) != 0 do
hide_fps_meter()
fps_meter_visible = 0
end
if is_key_pressed(KEY_F3) != 0 do
show_fps_meter(Vec2 { x: 10.0, y: 10.0 })
fps_meter_visible = 1
end
# Direction vectors are updated above

View file

@ -1,4 +1,4 @@
#include "../libsuicmez/libsuicmez.h"
#include "libsuicmez/libsuicmez.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>