refactoring of render functions

This commit is contained in:
Alexandre 2024-07-10 11:06:23 +02:00
parent 60ae801f1b
commit def4253da9
5 changed files with 30 additions and 19 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -15,7 +15,10 @@ and depth = 4 ;;
let render_distance = 7 ;;
let chunk_dist = 2 ;;
let chunk_size = 4 ;;
let cube_size = 3 ;;
(* has to be a multiple of cube_size *)
let chunk_size = 6 ;;
let chunk_size_f = float_of_int chunk_size ;;
let density = 50 ;;
@ -946,32 +949,38 @@ let initialize_chunk hash ch_x ch_y ch_z =
done;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let initialize_flat hash ch_x ch_y ch_z =
let init_chunk_all hash mem ch_x ch_y ch_z =
let n_cubes = chunk_size / 3 in
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
for i = 0 to 3 do
for j = 0 to 3 do
for k = 0 to 3 do
dyn_append dyna {cube = create_cube (4*ch_x +i) (4*ch_y +k) (4*ch_z +j) 1; red = 32; green = 222; blue = 32};
for i = 0 to n_cubes -1 do
for j = 0 to n_cubes -1 do
for k = 0 to n_cubes -1 do
if (Random.int 101) < density then
dyn_append dyna {cube = create_cube (chunk_size*ch_x + cube_size*i) (chunk_size*ch_y + cube_size*j) (chunk_size*ch_z + cube_size*k) cube_size; red = 250; green = 250; blue = 250}
done
done
done;
Hashtbl.add mem (ch_x, ch_y, ch_z) 1;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let init_random hash ch_x ch_y ch_z =
let init_full_all hash mem ch_x ch_y ch_z =
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
if (Random.int 101) < density then
dyn_append dyna {cube = create_cube (4*ch_x) (4*ch_y) (4*ch_z) 4; red = 250 ; green = 250 ; blue = 250};
dyn_append dyna {cube = create_cube (chunk_size*ch_x) (chunk_size*ch_y) (chunk_size*ch_z) chunk_size; red = 250; green = 32; blue = 32};
Hashtbl.add mem (ch_x, ch_y, ch_z) 1;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let rec manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov =
try
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) hash screen_wd screen_ht fov
with
| Not_found when ch_y = (-64)-> initialize_flat hash ch_x ch_y ch_z ; manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov
| Not_found when ch_y = 64-> initialize_flat hash ch_x ch_y ch_z ; manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov
| Not_found -> init_random hash ch_x ch_y ch_z ; manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov ;;
let redirect_generation hash mem ch_x ch_y ch_z =
if abs (ch_y * chunk_size) >= 128 then
init_full_all hash mem ch_x ch_y ch_z
else
init_chunk_all hash mem ch_x ch_y ch_z ;;
let render_chunks hash camx camy camz ch_distance screen_wd screen_ht fov =
let rec manage_unexisting_chunk hash mem ch_x ch_y ch_z screen_wd screen_ht fov =
if Hashtbl.find_opt mem (ch_x, ch_y, ch_z) = None then
redirect_generation hash mem ch_x ch_y ch_z ;
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) hash screen_wd screen_ht fov ;;
let render_chunks hash mem camx camy camz ch_distance screen_wd screen_ht fov =
let arr = Array.make ((2*ch_distance + 1)*(2*ch_distance + 1)*(2*ch_distance + 1)) ((0, 0), (0, 99)) in
let id = ref 0 in
for i = -ch_distance to ch_distance do
@ -991,7 +1000,7 @@ let render_chunks hash camx camy camz ch_distance screen_wd screen_ht fov =
for i = 0 to (Array.length arr -1) do
(*Printf.printf "[%d, %d, %d] (%d)" (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) (snd (snd arr.(i)));
Stdlib.print_endline " ";*)
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 mem (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) screen_wd screen_ht fov ;
done ;;
let get1char_plus () =
@ -1008,9 +1017,11 @@ let play_dos laby =
Stdlib.print_endline "Converting terrain...";
(*let hash = chunkify_2 laby 2 in*)
let hash = Hashtbl.create 100 in
let memory = Hashtbl.create 100 in
let dyna = dyn_create {cube = create_cube 0 0 0 1 ; red = 30 ; green = 30 ; blue = 30} in
Hashtbl.add hash (0, 0, 0) dyna ;
Hashtbl.add memory (0, 0, 0) 1;
(*print_cubes cs ;*)
@ -1034,7 +1045,7 @@ let play_dos laby =
let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in
ch_x := cx ; ch_y := cy ; ch_z := cz;
render_chunks hash !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ;
render_chunks hash memory !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ;
set_color white;
draw_integer_alignedleft 10 (__height__ - 30) (-int_of_float camera_xyz.x) 25;

BIN
display.o

Binary file not shown.