rework of user input system

This commit is contained in:
Alexandre 2024-07-04 10:35:39 +02:00
parent 6feef49545
commit 8a88f07366
5 changed files with 38 additions and 22 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -414,9 +414,12 @@ let is_visible_cube (cube : pt_3d array) (hash : (int * int * int, coloredCube d
let draw_texture (rect : (int * int) array) (text : texture) light = let draw_texture (rect : (int * int) array) (text : texture) light =
(*set_color white; (*set_color white;
fill_poly rect ;;*) fill_poly rect ;;*)
Stdlib.print_endline "{";
for i = 0 to text.width -1 do for i = 0 to text.width -1 do
for j = 0 to text.height -1 do for j = 0 to text.height -1 do
if true then begin if true then begin
Printf.printf " (((%f)))" light;
Stdlib.print_endline " ";
let face_R = int_of_float ((float_of_int text.arr_red.(i).(j)) *. light) let face_R = int_of_float ((float_of_int text.arr_red.(i).(j)) *. light)
and face_G = int_of_float ((float_of_int text.arr_green.(i).(j)) *. light) and face_G = int_of_float ((float_of_int text.arr_green.(i).(j)) *. light)
and face_B = int_of_float ((float_of_int text.arr_blue.(i).(j)) *. light) in and face_B = int_of_float ((float_of_int text.arr_blue.(i).(j)) *. light) in
@ -435,7 +438,8 @@ let draw_texture (rect : (int * int) array) (text : texture) light =
fill_poly [|bot_left; bot_right; top_right; top_left|] fill_poly [|bot_left; bot_right; top_right; top_left|]
end end
done done
done ;; done;
Stdlib.print_endline "}" ;;
let draw_cube_p (cube : pt_3d array) (hash : (int * int * int, coloredCube dynamic) Hashtbl.t) screen_wd screen_ht fov r g b = let draw_cube_p (cube : pt_3d array) (hash : (int * int * int, coloredCube dynamic) Hashtbl.t) screen_wd screen_ht fov r g b =
let adjusted = adjust_to_camera cube in let adjusted = adjust_to_camera cube in
@ -764,17 +768,17 @@ let chunkify_2 laby sz =
let (cw, ch, cd) = coords_to_chunk (w*sz) (h*sz) (d*sz) in let (cw, ch, cd) = coords_to_chunk (w*sz) (h*sz) (d*sz) in
match Hashtbl.find_opt cubes (cw, ch, cd) with match Hashtbl.find_opt cubes (cw, ch, cd) with
| None -> begin | None -> begin
Printf.printf "created cube (%d, %d, %d) (%d)\n" (w*sz) (h*sz) (d*sz) sz; (*Printf.printf "created cube (%d, %d, %d) (%d)\n" (w*sz) (h*sz) (d*sz) sz;
Printf.printf "in chunk (%d, %d, %d)\n" cw ch cd; Printf.printf "in chunk (%d, %d, %d)\n" cw ch cd;
Stdlib.print_endline " "; Stdlib.print_endline " ";*)
let dyna = dyn_create {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b} in let dyna = dyn_create {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b} in
dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b}; dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b};
Hashtbl.add cubes (cw, ch, cd) dyna Hashtbl.add cubes (cw, ch, cd) dyna
end end
| Some dyna -> begin | Some dyna -> begin
Printf.printf "created cube (%d, %d, %d) (%d)\n" (w*sz) (h*sz) (d*sz) sz; (*Printf.printf "created cube (%d, %d, %d) (%d)\n" (w*sz) (h*sz) (d*sz) sz;
Printf.printf "in chunk (%d, %d, %d)\n" cw ch cd; Printf.printf "in chunk (%d, %d, %d)\n" cw ch cd;
Stdlib.print_endline " "; Stdlib.print_endline " ";*)
Hashtbl.remove cubes (cw, ch, cd); Hashtbl.remove cubes (cw, ch, cd);
dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b}; dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b};
Hashtbl.add cubes (cw, ch, cd) dyna Hashtbl.add cubes (cw, ch, cd) dyna
@ -1015,6 +1019,12 @@ let render_chunks hash camx camy camz ch_distance screen_wd screen_ht fov =
manage_unexisting_chunk hash (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) screen_wd screen_ht fov ; manage_unexisting_chunk hash (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) screen_wd screen_ht fov ;
done ;; done ;;
let get1char_plus () =
if key_pressed () then
read_key ()
else
'@' ;;
let play_dos laby = let play_dos laby =
try try
Stdlib.print_endline "Building terrain..."; Stdlib.print_endline "Building terrain...";
@ -1029,30 +1039,36 @@ let play_dos laby =
(*print_cubes cs ;*) (*print_cubes cs ;*)
let redraw = ref true in
let ch_x = ref 0
and ch_y = ref 0
and ch_z = ref 0 in
while true do while true do
ignore (Sys.command "clear") ; if !redraw then begin
auto_synchronize false;
open_graph " 1500x1000";
set_color black;
fill_poly [|(0, 0); (__width__, 0); (__width__, __height__); (0, __height__); (0, 0)|];
Stdlib.print_endline "Rendering terrain..."; let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in
auto_synchronize false; ch_x := cx ; ch_y := cy ; ch_z := cz;
open_graph " 1500x1000";
set_color black;
fill_poly [|(0, 0); (__width__, 0); (__width__, __height__); (0, __height__); (0, 0)|];
let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in render_chunks hash !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ;
render_chunks hash ch_x ch_y ch_z chunk_dist __width__ __height__ fov ; auto_synchronize true;
end;
redraw := false;
auto_synchronize true; let usr_input = get1char_plus () in
if usr_input <> '@' then begin
Printf.printf "current pos : (%f, %f, %f)\n" (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z; move_cam_hash_2 hash !ch_x !ch_y !ch_z true usr_input;
Printf.printf "current chunk : (%d, %d, %d)" ch_x ch_y ch_z; redraw := true
Stdlib.print_endline " "; end
let usr_input = get1char () in
move_cam_hash_2 hash ch_x ch_y ch_z true usr_input
done ; done ;
() ()
with with
| Not_found -> Stdlib.print_endline "Looks like you tried to load an uninitialized chunk..." ;; | Not_found -> Stdlib.print_endline "Wait that's illegal\n" ;;
play_dos laby ;; play_dos laby ;;

BIN
display.o

Binary file not shown.