added music

This commit is contained in:
Alexandre 2024-10-23 11:21:48 +02:00
parent 659ca44dcf
commit 2f6fa9cd87
11 changed files with 107 additions and 17 deletions

7
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"files.associations": {
"sdl.h": "c",
"time.h": "c",
"unistd.h": "c"
}
}

BIN
1sec.wav Normal file

Binary file not shown.

BIN
a.out

Binary file not shown.

View File

@ -1 +1,2 @@
ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics main.ml
ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics -thread -package threads -linkpkg main.ml -o pinball
gcc playsound.c -lSDL2 -lSDL2_image -o sound

BIN
main.cmi

Binary file not shown.

BIN
main.cmx

Binary file not shown.

91
main.ml
View File

@ -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)

BIN
main.o

Binary file not shown.

BIN
pinball Executable file

Binary file not shown.

23
playsound.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <SDL2/SDL.h>
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

BIN
sound Executable file

Binary file not shown.