Skip to content

Instantly share code, notes, and snippets.

@marvhus
Last active January 27, 2025 21:40
Show Gist options
  • Save marvhus/2441e1dda7fef3451e5f919b737a32ce to your computer and use it in GitHub Desktop.
Save marvhus/2441e1dda7fef3451e5f919b737a32ce to your computer and use it in GitHub Desktop.
Jai example of how to have a moving camera using the Simp library
#import "Math";
#import "Simp";
#import "Basic";
#import "Input";
#import "Window_Creation";
window_width : s32 = 1280;
window_height : s32 = 720;
quit : bool = false;
the_window : Window_Type;
projection_matrix : Matrix4;
camera_position : Vector2;
camera_scale : float = 20;
camera_speed : float = 0.25;
main :: () {
the_window = create_window(window_width, window_height, "Camera Example");
set_render_target(the_window);
while !quit {
handle_events();
render_frame();
}
}
handle_events :: () {
update_window_events();
recalculate_projection_matrix := false;
// Window resize events.
for get_window_resizes() {
update_window(it.window);
if it.window != the_window continue;
window_width = it.width;
window_height = it.height;
recalculate_projection_matrix = true;
}
// Events
for events_this_frame {
if it.type == .QUIT then quit = true;
if it.type == .KEYBOARD {
if it.key_pressed if it.key_code == {
case .ESCAPE; quit = true;
}
}
}
// Keyboard input.
camera_position_delta: Vector2;
if input_button_states[#char "W"] & .DOWN {
camera_position_delta.y -= 1;
recalculate_projection_matrix = true;
}
if input_button_states[#char "S"] & .DOWN {
camera_position_delta.y += 1;
recalculate_projection_matrix = true;
}
if input_button_states[#char "A"] & .DOWN {
camera_position_delta.x += 1;
recalculate_projection_matrix = true;
}
if input_button_states[#char "D"] & .DOWN {
camera_position_delta.x -= 1;
recalculate_projection_matrix = true;
}
normalize(*camera_position_delta);
camera_position += camera_position_delta * camera_speed;
// Recalculate projection matrix.
if recalculate_projection_matrix then update_projection_matrix();
}
render_frame :: () {
RED :: Vector4.{.70, .17, .17, 1};
clear_render_target(0.94, 0.94, 0.94, 1);
{
immediate_begin();
set_shader_for_color();
set_projecton_matrix();
immediate_quad(0, 0, 10, 10, Vector4.{1, 0, 0, 1});
immediate_flush();
}
swap_buffers(the_window);
}
update_projection_matrix :: () {
aspect_ratio := cast(float) window_width / cast(float) window_height;
left := -camera_scale * aspect_ratio / 2;
right := camera_scale * aspect_ratio / 2;
bottom := -camera_scale / 2;
top := camera_scale / 2;
near := -1.0;
far := 1.0;
mat := orthographic_projection_matrix(left, right, bottom, top, near, far);
mat *= make_translation_matrix4(Vector3.{x = camera_position.x, y = camera_position.y, z = 0});
state := context.simp;
assert(state != null);
// NOTE(mvh): Taken from backend_tweak_projection_matrix
if state.texture_render_target {
mat._21 *= -1;
mat._22 *= -1;
mat._23 *= -1;
mat._24 *= -1;
}
projection_matrix = mat;
}
set_projecton_matrix :: () {
state := context.simp;
assert(state != null);
shader := state.current_shader;
assert(shader != null);
set_parameter(shader, "projection", projection_matrix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment