diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0966402 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "sdl.h": "c", + "time.h": "c", + "unistd.h": "c" + } +} \ No newline at end of file diff --git a/1sec.wav b/1sec.wav new file mode 100644 index 0000000..2f3bfdb Binary files /dev/null and b/1sec.wav differ diff --git a/a.out b/a.out deleted file mode 100755 index 0a9d772..0000000 Binary files a/a.out and /dev/null differ diff --git a/compilation.sh b/compilation.sh index 8286cfa..a44b2f3 100644 --- a/compilation.sh +++ b/compilation.sh @@ -1 +1,2 @@ -ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics main.ml \ No newline at end of file +ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics -thread -package threads -linkpkg main.ml -o pinball +gcc playsound.c -lSDL2 -lSDL2_image -o sound \ No newline at end of file diff --git a/main.cmi b/main.cmi index 8c810a3..764570c 100644 Binary files a/main.cmi and b/main.cmi differ diff --git a/main.cmx b/main.cmx index a47e173..42b02b3 100644 Binary files a/main.cmx and b/main.cmx differ diff --git a/main.ml b/main.ml index 23c0b9f..67c853d 100644 --- a/main.ml +++ b/main.ml @@ -40,6 +40,18 @@ type sphere = { score : int ; } ;; +type ball = { + radius : float ; + mass : float ; + rgb : int ; + xy : pt_2d ; + v : pt_2d ; + a : pt_2d ; + fres : pt_2d ; +} ;; + +(* --- *) + let default_polygon = { vertexes = [||] ; rgb = 0 ; @@ -63,25 +75,57 @@ let default_sphere = { score = 0 ; } ;; -type ball = { - radius : float ; - mass : float ; - rgb : int ; - xy : pt_2d ; - v : pt_2d ; - a : pt_2d ; - fres : pt_2d ; -} ;; - -let univ_dt = 0.05 ;; -let univ_friction = 0.8 ;; let univ_g = 800.0 ;; let pi = 3.14159265358979343 ;; +let winBL = { + x = 0. ; + y = 0. ; +} ;; + +let winTR = { + x = 1200. ; + y = 800. ; +} + +let winball = { + x = 750. ; + y = 500. ; +} + let gforce = {x = 0. ; y = -. univ_g} ;; let score = ref 0 ;; +(* ------------------------------------------------------------------------------------- *) +(* ------------------------------------------------------------------------------------- *) +(* WALUIGI_TIME Threads *) + +let n_threads = 8 ;; + +let beep_boop = Array.make n_threads false ;; +let beep_id = ref 0 ;; + +let playbeep id = + while true do + if beep_boop.(id) then begin + beep_boop.(id) <- false ; + ignore (Unix.system "./sound 1sec.wav") ; + end; + Unix.sleepf 0.005 ; + done;; + +let beep_list = Array.init n_threads (fun k -> Thread.create playbeep k) ;; + +(**) + +let play_music () = + while true do + ignore (Unix.system "./sound 1sec.wav") ; + done;; + +let theme_thr = Thread.create play_music () ;; + (* ------------------------------------------------------------------------------------- *) (* ------------------------------------------------------------------------------------- *) (* WALUIGI_TIME Arithmetical operations *) @@ -332,6 +376,9 @@ let update_ball_data (b : ball) (polys : polygon array) (spheres : sphere array) if is_collision_s b spheres.(s) dt then begin score := !score + spheres.(s).score ; + beep_boop.(!beep_id) <- true ; + beep_id := (!beep_id+1) mod n_threads ; + (* apply normal reaction force *) let proj_n = vect_normalize_2D (vect_diff_2D b.xy spheres.(s).center) in let reaction_force_2 = vect_mult_2D proj_n (univ_g *. b.mass *. (vect_dot_product_2D (vect_normalize_2D gforce) proj_n)) in @@ -363,6 +410,11 @@ let update_ball_data (b : ball) (polys : polygon array) (spheres : sphere array) b.xy.x <- b.xy.x +. b.v.x *. dt ; b.xy.y <- b.xy.y +. b.v.y *. dt ;; +let update_balls (bl : ball array) (polys : polygon array) (spheres : sphere array) (dt : float) = + for b = 0 to Array.length bl -1 do + update_ball_data bl.(b) polys spheres dt + done ;; + (* ------------------------------------------------------------------------------------- *) (* ------------------------------------------------------------------------------------- *) (* WALUIGI_TIME Graphics fcts *) @@ -462,6 +514,11 @@ let draw_ball (b : ball) = set_line_width 4 ; draw_circle (int_of_float b.xy.x) (int_of_float b.xy.y) (int_of_float b.radius) ;; +let draw_all_balls (bs : ball array) = + for k = 0 to Array.length bs -1 do + draw_ball bs.(k) + done ;; + (* ------------------------------------------------------------------------------------- *) (* ------------------------------------------------------------------------------------- *) (* WALUIGI_TIME Misc fcts *) @@ -509,6 +566,9 @@ let create_sphere (x00 : int) (y00 : int) (rd : float) (rest : float) (pts : int score = pts ; } ;; +let generate_pinballs (count : int) (r : float) (x0 : int) (y0 : int) (m : float) (red : int) (green : int) (blue : int) = + Array.init count (fun k -> create_ball r x0 y0 m red green blue) ;; + (* ------------------------------------------------------------------------------------- *) (* ------------------------------------------------------------------------------------- *) (* WALUIGI_TIME Edition functions *) @@ -576,7 +636,7 @@ let simulate (data : polygon dynamic) (dats : sphere dynamic) = open_graph " 1200x800" ; set_window_title "WAH" ; - let pinball = create_ball 25.0 600 800 0.15 169 169 169 in + let pinballs = generate_pinballs 3 20.0 600 700 0.15 255 255 0 in let stime = Unix.gettimeofday () in let ctime = ref (Unix.gettimeofday ()) in @@ -596,7 +656,7 @@ let simulate (data : polygon dynamic) (dats : sphere dynamic) = for d = 0 to dats.len -1 do draw_sphere dats.tab.(d) done; - draw_ball pinball ; + draw_all_balls pinballs ; set_color (rgb 128 128 32) ; draw_float 25 770 (round (!ctime -. stime) 3) 25 ; @@ -606,7 +666,7 @@ let simulate (data : polygon dynamic) (dats : sphere dynamic) = let __end = Unix.gettimeofday () in ctime := !ctime +. (__end -. __start) ; - update_ball_data pinball data.tab dats.tab (__end -. __start) ; + update_balls pinballs data.tab dats.tab (__end -. __start) ; done; close_graph () ;; @@ -630,7 +690,6 @@ dyn_add spheres (create_sphere 1000 400 20. 1. 5 220 32 220) ;; dyn_add spheres (create_sphere 50 200 15. 1. 20 255 255 32) ;; dyn_add spheres (create_sphere 1150 200 15. 1. 20 255 255 32) ;; - simulate polygons spheres ;; (* let create_polygon (arr : (int * int) array) (rest : float) (pts : int) (red : int) (green : int) (blue : int) diff --git a/main.o b/main.o index 59e6c58..8ae798e 100644 Binary files a/main.o and b/main.o differ diff --git a/pinball b/pinball new file mode 100755 index 0000000..c7df3b0 Binary files /dev/null and b/pinball differ diff --git a/playsound.c b/playsound.c new file mode 100644 index 0000000..d9701a0 --- /dev/null +++ b/playsound.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +int main(int argc, char** argv) { + assert(argc == 2); + if(SDL_Init(SDL_INIT_AUDIO)) {fprintf(stderr, "cannot initialize audio");exit(1);} + SDL_AudioSpec wavSpec; + Uint32 wavLength; + Uint8 *wavBuffer; + SDL_LoadWAV(argv[1], &wavSpec, &wavBuffer, &wavLength); + SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0); + int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); + SDL_PauseAudioDevice(deviceId, 0); + usleep((int)((wavLength)/88200.0*100000.0)) ; + SDL_CloseAudioDevice(deviceId); + SDL_FreeWAV(wavBuffer); + SDL_Quit(); + return 0; +} +// yummy \ No newline at end of file diff --git a/sound b/sound new file mode 100755 index 0000000..9734e11 Binary files /dev/null and b/sound differ