-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrolling_moon.ml
executable file
·1124 lines (887 loc) · 25.6 KB
/
rolling_moon.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ocaml
(* {{{ COPYING *(
This file is a simple arcade game that uses the Chipmunk library.
Copyright (C) 2008 Florent Monnier <[email protected]>
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
)* }}} *)
(* {{{ Modules *)
#directory "/tmp/glMLite/SRC"
#load "GL.cma"
#load "Glu.cma"
#load "Glut.cma"
open GL
open Glu
open Glut
#directory "/tmp/ocaml-chipmunk-0.04.2"
#load "chipmunk.cma"
open Chipmunk ;;
open Low_level ;;
open OO ;;
(* }}} *)
(* {{{ load level *)
type level =
| Path of (float * float) array
| Target of (float * float * float)
| Avatar of (float * float * float)
;;
let input_custom_int ic =
let b0 = input_byte ic in
let b1 = input_byte ic in
(b0 lor (b1 lsl 8)) - 32768
;;
let read_path ic =
let n = input_binary_int ic in
let arr =
Array.init n (fun _ ->
let x = input_custom_int ic in
let y = input_custom_int ic in
let x = (float x) /. 1.0
and y = (float y) /. 1.0 in
(x,y))
in
Path(arr)
;;
let read_target ic =
let x = input_custom_int ic in
let y = input_custom_int ic in
let r = input_custom_int ic in
let x = (float x) /. 1.0
and y = (float y) /. 1.0
and r = (float r) /. 1.0 in
Target(x,y,r)
;;
let read_avatar ic =
let x = input_custom_int ic in
let y = input_custom_int ic in
let r = input_custom_int ic in
let x = (float x) /. 1.0
and y = (float y) /. 1.0
and r = (float r) /. 1.0 in
Avatar(x,y,r)
;;
let read_datas ic =
let rec aux acc =
try
let kind =
try input_byte ic
with End_of_file -> raise Exit
in
try match kind with
| 1 -> aux (read_path ic :: acc)
| 2 -> aux (read_target ic :: acc)
| 3 -> aux (read_avatar ic :: acc)
| x -> invalid_arg(Printf.sprintf "Unknown data type: %d" x)
with
End_of_file -> failwith("uncomplete data")
with
Exit -> acc
in
aux []
;;
let load_datas file =
let ic = open_in_bin file in
let datas = read_datas ic in
close_in ic;
(datas)
;;
let load_level file =
let datas = load_datas file in
let rec filter paths targets s = function
| Path p :: tl -> filter (p::paths) targets s tl
| Target i :: tl -> filter paths (i::targets) s tl
| Avatar a::tl -> filter paths targets (Some a) tl
| [] ->
let x, y, r =
match s with Some s -> s
| None -> (150.0, 0.0, 18.0) (* default start position, and radius *)
in
(paths, targets, x, y, r)
in
filter [] [] None datas
;;
(* }}} *)
(* {{{ global vars *)
let do_tess = ref true ;;
let field = ref 400. ;;
let sleep_ticks = 16 ;;
let texd = ref false ;;
let do_bg = ref false ;;
let pi = 4.0 *. atan 1.0 ;;
let init_global() = ref None ;;
let get_global g = match !g with Some v -> v | None -> raise Not_found ;;
let _space = init_global()
let _staticBody = init_global()
let _level_paths = init_global()
let _ball = init_global()
let active_shapes_li = ref [] ;;
let active_bodies_li = ref [] ;;
let targets_shapes_li = ref [] ;;
let _ball_id = init_global()
let targets_id_arr = ref [| |] ;;
let _stop = ref false ;;
let _finished = ref false ;;
let cpv x y = {cp_x=x; cp_y=y}
let cpvzero = cpvzero()
let gravity = ref 600. ;;
let key_left_down = ref false ;;
let key_right_down = ref false ;;
let spent_time = ref 0 ;;
let angle_z = ref (0.)
let angle_step = ref 2.0 ;;
let rad_to_deg =
let f = (pi /. 180.) in
(function v -> v *. f)
;;
let level_files = ref [] ;;
let texture_file = ref "" ;;
(* default window size *)
(*
let win_width, win_height = ref 800, ref 600 ;;
let win_width, win_height = ref 700, ref 525 ;;
let win_width, win_height = ref 640, ref 480 ;;
let win_width, win_height = ref 600, ref 450 ;;
let win_width, win_height = ref 500, ref 375 ;;
let win_width, win_height = ref 400, ref 300 ;;
let win_width, win_height = ref 300, ref 225 ;;
let win_width, win_height = ref 280, ref 210 ;;
*)
let win_width, win_height = ref 640, ref 480 ;;
let factor = ref 0. ;;
let tess_id = ref None ;;
let outline_id = ref None ;;
let bg_id = ref None ;;
(* }}} *)
(* {{{ restart the level *)
let reset_avatar, avatar_start_pos =
let x = ref 0.0 and y = ref 0.0 in
(fun () ->
let ball = get_global _ball in
ball#reset_forces;
ball#set_torque ~t:0.0;
ball#set_force ~f:cpvzero;
ball#set_a_vel ~w:0.0;
ball#set_angle ~a:0.0;
ball#set_vel ~v:cpvzero;
ball#set_rot ~rot:cpvzero;
ball#set_pos ~p:(cpv !x !y);
),
(fun _x _y ->
x := _x;
y := _y)
;;
(* }}} *)
(* {{{ init the physic *)
let init_rolling_ball level =
let level_paths, level_targets, start_x, start_y, ball_radius =
load_level level
in
_level_paths := Some level_paths;
let space = new cp_space in
_space := Some space;
space#set_gravity (cpv 0.0 (-. !gravity));
space#set_elastic_iterations 10;
space#resize_static_hash 30.0 4000;
space#resize_active_hash 30.0 20;
(* {{{ static body *)
let staticBody = new cp_body infinity infinity in
_staticBody := Some staticBody;
List.iter (fun path ->
let n = Array.length path in
let rec terrain_loop i a =
if i >= n then () else begin
let x, y = path.(i) in
let b = cpv x y in
let seg = new cp_shape staticBody (SEGMENT_SHAPE(a, b, 0.0)) in
seg#set_layers 0b1111111_11111111_11111111_11111111;
seg#set_friction 1.0;
seg#set_elasticity 0.6;
space#add_static_shape seg;
terrain_loop (succ i) b
end
in
let x, y = path.(0) in
let a = cpv x y in
terrain_loop 1 a;
) level_paths;
(* }}} *)
(* {{{ avatar ball *)
let ball_mass = 2.0 in
let ball_moment = moment_for_circle ball_mass ball_radius 0.0 cpvzero in
let ball = new cp_body ball_mass ball_moment in
_ball := Some ball;
active_bodies_li := ball :: !active_bodies_li;
avatar_start_pos start_x start_y;
ball#set_pos (cpv start_x start_y);
space#add_body ball;
let shape = new cp_shape ball (CIRCLE_SHAPE(ball_radius, cpvzero)) in
shape#set_friction 0.2;
shape#set_elasticity 0.8;
space#add_shape shape;
active_shapes_li := shape :: !active_shapes_li;
_ball_id := Some(shape#get_hashid);
shape#set_layers 0b0000000_00000000_11111111_00000000;
(* Twin mode:
let ball_mass = 2.0 in
let ball_radius = 18.0 in
let ball_moment = moment_for_circle ball_mass ball_radius 0.0 cpvzero in
let ball = new cp_body ball_mass ball_moment in
_ball := Some ball;
active_bodies_li := ball :: !active_bodies_li;
ball#set_pos (cpv 200.0 0.0);
space#add_body ball;
let shape = new cp_shape ball (CIRCLE_SHAPE(ball_radius, cpvzero)) in
shape#set_friction 0.2;
shape#set_elasticity 0.8;
space#add_shape shape;
active_shapes_li := shape :: !active_shapes_li;
shape#set_layers 0b0000000_00000000_00000000_11111111;
*)
(* }}} *)
(* {{{ game targets *)
let add_target (x, y, r) =
let target = new cp_body infinity infinity in
target#set_pos (cpv x y);
let shape = new cp_shape target (CIRCLE_SHAPE(r, cpvzero)) in
shape#set_friction 0.6;
shape#set_elasticity 0.6;
space#add_static_shape shape;
targets_shapes_li := shape :: !targets_shapes_li;
(shape#get_hashid, false)
in
targets_id_arr := Array.of_list (List.map add_target level_targets);
(* }}} *)
;;
(* }}} *)
(* {{{ free level *)
let free_display_list should_be dl =
match !dl with
| None ->
if should_be then
Printf.eprintf "Warning: empty display list\n%!"
| Some gl_list ->
glDeleteLists ~gl_list ~range:1;
dl := None;
;;
let globals_init_vals() =
_stop := false;
_finished := false;
angle_z := 0.;
spent_time := 0;
factor := 0.;
key_left_down := false;
key_right_down := false;
;;
let re_init_globals() =
active_shapes_li := [];
active_bodies_li := [];
targets_shapes_li := [];
globals_init_vals();
free_display_list (!do_tess) tess_id;
free_display_list (true) outline_id;
free_display_list (!do_bg) bg_id;
;;
let free_level() =
let space = get_global _space in
space#free_children;
space#free;
let staticBody = get_global _staticBody in
staticBody#free;
;;
(* }}} *)
(* {{{ next level *)
(*
let pop_level() =
try
let hd = List.hd !level_files in
level_files := List.tl !level_files;
(hd)
with
| Failure "hd" -> failwith "no level"
| Failure "tl" -> failwith "bug"
;;
*)
let pop_level() =
match !level_files with
| hd::tl -> level_files := tl; (hd)
| [] -> failwith "no level"
;;
let next_level() =
re_init_globals();
free_level();
let level =
try pop_level()
with Failure _ ->
print_endline "goodbye"; exit 0
in
init_rolling_ball level;
;;
let is_last_level() =
match !level_files with
| [] -> true
| _ -> false
;;
(* timer *)
let goto_next = ref false ;;
let goto_next_level ~value = if !goto_next then next_level() ;;
(* }}} *)
(* {{{ handle collisions *)
let prop_targets() =
let len = Array.length !targets_id_arr in
let n = Array.fold_left (fun n (_,b) -> if b then succ n else n) 0 !targets_id_arr in
Printf.sprintf "%d/%d" (succ n) len;
;;
let proc_collisions ~arb =
let a = cpArbiterGetShapePA arb
and b = cpArbiterGetShapePB arb in
let a_id = cpShapeGetHashID a
and b_id = cpShapeGetHashID b in
let ball_id = get_global _ball_id in
let check_target idx (it_id, collided) =
if it_id = a_id || it_id = b_id then
if ball_id = a_id || ball_id = b_id then begin
if not(!_stop) && not(snd !targets_id_arr.(idx)) then
Printf.printf " %s : collided at %g sec.\n%!" (prop_targets()) (float !spent_time /. 10.);
!targets_id_arr.(idx) <- (it_id, true);
end
in
Array.iteri check_target !targets_id_arr;
let done_ =
Array.fold_left (fun x (_,b) -> x && b) true !targets_id_arr
in
if done_ && not(!_stop) then begin
Printf.printf " Completed in %g sec.\n%!" (float !spent_time /. 10.);
_stop := true;
_finished := true;
if not(is_last_level()) then
(goto_next := true;
glutTimerFunc ~msecs:5000 ~timer:goto_next_level ~value:0);
if not(!key_left_down) && not(!key_right_down) then
if (!spent_time mod 2) = 0
then key_left_down := true
else key_right_down := true;
end;
;;
(* }}} *)
(* {{{ update physics steps *)
let rolling_ball_update() =
if not(!_stop) then begin
let ball = get_global _ball
and space = get_global _space in
let y = !gravity *. cos (rad_to_deg !angle_z)
and x = !gravity *. sin (rad_to_deg !angle_z)
in
space#set_gravity (cpv x (-. y));
let substeps = 4 in
let dt = (1.0 /. 60.0) /. (float substeps) in
for i=0 to pred substeps do
let arbiters = space#get_arbiters in
Array.iter (fun arb -> proc_collisions ~arb) arbiters;
ball#reset_forces;
space#step ~dt;
done;
end;
;;
(* }}} *)
(* {{{ draw Objects *)
let target_circle ~x ~y ~r ~a =
let segs = 16 in
let coef = 2.0 *. pi /. (float segs) in
glBegin GL_LINE_LOOP;
for n=0 to pred segs do
let rads = (float n) *. coef in
glVertex2 (r *. cos(rads +. a) +. x)
(r *. sin(rads +. a) +. y);
done;
glEnd();
;;
let was_touched id arr =
let len = Array.length arr in
let rec aux i =
if i >= len then false else
let _id, b = arr.(i) in
if _id = id then b
else aux(succ i)
in
aux 0
;;
let drawTargetShape ~shape =
let body = shape#body
and circle = shape#get_circle_shape
and id = shape#get_hashid
in
let c = cpvadd body#get_pos (cpvrotate circle#get_center body#get_rot) in
if was_touched id !targets_id_arr
then glColor3 0.0 0.5 0.0
else glColor3 0.2 1.0 0.7;
target_circle c.cp_x c.cp_y circle#get_radius body#get_angle;
;;
(* circle of the avatar ball *)
let drawCircle ~x ~y ~r ~a =
let segs = 16 in
let coef = 2.0 *. pi /. (float segs) in
glColor3 0.8 0.5 0.0;
glBegin GL_LINE_LOOP;
for n=0 to pred segs do
let rads = (float n) *. coef in
glVertex2 (r *. cos(rads +. a) +. x)
(r *. sin(rads +. a) +. y);
done;
glEnd();
glColor3 0.9 0.6 0.0;
glBegin GL_LINES;
glVertex2 x y;
glVertex2 (r *. cos(a) +. x)
(r *. sin(a) +. y);
glVertex2 x y;
glVertex2 (r *. cos(a +. (pi *. 0.666666)) +. x)
(r *. sin(a +. (pi *. 0.666666)) +. y);
glVertex2 x y;
glVertex2 (r *. cos(a +. (pi *. 1.333333)) +. x)
(r *. sin(a +. (pi *. 1.333333)) +. y);
glEnd();
;;
let drawCircleShape ~shape =
let body = shape#body
and circle = shape#get_circle_shape
in
let c = cpvadd body#get_pos (cpvrotate circle#get_center body#get_rot) in
drawCircle c.cp_x c.cp_y circle#get_radius body#get_angle;
;;
let drawSegmentShape ~shape =
let body = shape#body
and seg = shape#get_segment_shape
in
let a = cpvadd body#get_pos (cpvrotate seg#get_a body#get_rot)
and b = cpvadd body#get_pos (cpvrotate seg#get_b body#get_rot)
in
glBegin GL_LINES;
glVertex2 a.cp_x a.cp_y;
glVertex2 b.cp_x b.cp_y;
glEnd();
;;
let drawPolyShape ~shape =
let body = shape#body
and poly = shape#get_poly_shape in
let num = poly#get_num_verts
and verts = poly#get_verts in
glBegin GL_LINE_LOOP;
for i=0 to pred num do
let v = cpvadd body#get_pos (cpvrotate verts.(i) body#get_rot) in
glVertex2 v.cp_x v.cp_y;
done;
glEnd();
;;
let drawObject ~shape =
match shape#kind with
| CP_CIRCLE_SHAPE -> drawCircleShape ~shape;
| CP_SEGMENT_SHAPE -> drawSegmentShape ~shape;
| CP_POLY_SHAPE -> drawPolyShape ~shape;
| _ ->
Printf.printf "Bad enumeration in drawObject().\n";
;;
let drawBodies ~bodies =
glBegin GL_POINTS;
glColor3 0.0 0.8 1.0;
List.iter
(fun body ->
let p = body#get_pos in
glVertex2 p.cp_x p.cp_y;
) bodies;
glEnd();
;;
let draw_collisions ~arb:arbiter =
let cont_arr = (get_arbiter_contacts ~arbiter) in
glColor3 1.0 0.5 0.0;
glBegin GL_POINTS;
Array.iter (fun contact ->
let v = cpContactGetP contact in
glVertex2 v.cp_x v.cp_y;
) cont_arr;
glEnd();
;;
(* }}} *)
(* {{{ display *)
let display =
function () ->
let ball = get_global _ball in
let pos = ball#get_pos in
let x = -. pos.cp_x
and y = -. pos.cp_y in
glClear [GL_COLOR_BUFFER_BIT];
glLoadIdentity();
if !_finished then begin
let f = cos !factor in
glScale f f 1.0;
factor := !factor +. 0.008;
end;
glRotate ~angle:(!angle_z) ~x:0.0 ~y:0.0 ~z:(-1.0);
glTranslate x y 0.0;
(* {{{ draw background *)
if !texd && !do_bg then
begin
match !bg_id with
| None ->
let id = glGenLists 1 in
glNewList id GL_COMPILE_AND_EXECUTE;
glEnable GL_TEXTURE_2D;
glColor3 0.8 0.8 0.8;
glBegin GL_POLYGON;
glTexCoord2 ( 2000.0) ( 2000.0); glVertex3 ( 2000.) ( 2000.) (-1.0);
glTexCoord2 ( 2000.0) (-2000.0); glVertex3 ( 2000.) (-2000.) (-1.0);
glTexCoord2 (-2000.0) (-2000.0); glVertex3 (-2000.) (-2000.) (-1.0);
glTexCoord2 (-2000.0) ( 2000.0); glVertex3 (-2000.) ( 2000.) (-1.0);
glEnd();
glDisable GL_TEXTURE_2D;
glEndList();
bg_id := Some id;
| Some id ->
glPushMatrix();
glTranslate (-.(x/.2.)) (-.(y/.2.)) 0.0;
glCallList id;
glPopMatrix();
end;
(* }}} *)
(* {{{ draw the level *)
begin
let level_paths = get_global _level_paths in
(* {{{ fill the shape *)
if !do_tess then
begin
let tess = gluNewTess() in
gluTessDefaultCallback tess GLU_TESS_VERTEX;
gluTessDefaultCallback tess GLU_TESS_BEGIN;
gluTessDefaultCallback tess GLU_TESS_END;
(*
gluCallbackTessVertex tess (fun ~x ~y ~z -> glTexCoord2 x y; glVertex3 x y z;);
gluCallbackTessBegin tess (fun ~prim -> glBegin prim;);
gluCallbackTessEnd tess (fun _ -> glEnd(););
*)
gluTessDefaultCallback tess GLU_TESS_COMBINE;
gluTessDefaultCallback tess GLU_TESS_ERROR;
gluGetTessWindingRule tess GLU_TESS_WINDING_POSITIVE;
let filling() =
if !texd then begin
glEnable GL_TEXTURE_2D;
glMatrixMode GL_TEXTURE;
let f = 1.0 /. 180. in
glLoadIdentity();
glScale f f 1.0;
(*
glTranslate (x/.5.) (y/.5.) 0.0;
*)
glMatrixMode GL_MODELVIEW;
end;
if !texd
then glColor3 1.0 1.0 1.0
else glColor3 0.1 0.0 0.3;
in
(*
(* applying here allows to modify the texture mapping,
while keeping the tesselation in a display list *)
filling();
*)
begin
match !tess_id with
| None ->
let tess_pnts =
List.map (Array.map (fun (x,y) -> (x,y,0.0))) level_paths
in
let id = glGenLists 1 in
glNewList id GL_COMPILE_AND_EXECUTE;
filling();
gluTesselateIter tess tess_pnts;
(* {{{ equivalent:
gluTessBeginPolygon ~tess;
List.iter (fun points ->
gluTessBeginContour ~tess;
Array.iter (fun (x,y,z) -> gluTessVertex ~tess ~x ~y ~z) points;
gluTessEndContour ~tess;
) tess_pnts;
gluTessEndPolygon ~tess;
}}} *)
glEndList();
tess_id := Some id;
| Some id -> glCallList id;
end;
if !texd then
glDisable GL_TEXTURE_2D;
gluDeleteTess ~tess;
end;
(* }}} *)
(* {{{ draw outlines *)
begin
match !outline_id with
| None ->
let id = glGenLists 1 in
glNewList id GL_COMPILE_AND_EXECUTE;
glColor3 0.0 0.5 1.0;
List.iter (fun a ->
glBegin GL_LINE_LOOP;
Array.iter (fun (x,y) -> glVertex2 x y;) a;
glEnd();
) level_paths;
glEndList();
outline_id := Some id;
| Some id -> glCallList id;
end;
(* }}} *)
end;
(* }}} *)
List.iter (fun shape -> drawObject ~shape) !active_shapes_li;
List.iter (fun shape -> drawTargetShape ~shape) !targets_shapes_li;
(*
let bodies = !active_bodies_li in
drawBodies ~bodies;
*)
(*
let space = get_global _space in
let arbiters = space#get_arbiters in
Array.iter (fun arb -> draw_collisions ~arb) arbiters;
*)
glutSwapBuffers();
rolling_ball_update();
;;
(* }}} *)
(* {{{ reshape *)
let reshape ~width:w ~height:h =
glViewport 0 0 w h;
glMatrixMode GL_PROJECTION;
glLoadIdentity();
if w <= h then
let p = (float h) /. (float w) in
glOrtho (-. !field) (!field) (-. !field *. p) (!field *. p) (-10.0) (10.0);
else
let p = (float w) /. (float h) in
glOrtho (-. !field *. p) (!field *. p) (-. !field) (!field) (-10.0) (10.0);
glMatrixMode GL_MODELVIEW;
;;
(* }}} *)
(* {{{ user entries *)
let reset_targets() =
Array.iteri (fun i (id,_) ->
!targets_id_arr.(i) <- (id, false);
) !targets_id_arr;
;;
let restart_level() =
reset_avatar();
reset_targets();
globals_init_vals();
;;
let toggle_pause() = _stop := not(!_stop) ;;
let keyboard ~key ~x ~y =
match key with
| '\027' | 'q' | 'Q' -> free_level(); exit 0;
| 'z' -> restart_level();
| 'd' ->
let ball = get_global _ball in
let x = (ball#get_pos).cp_x
and y = (ball#get_pos).cp_y in
Printf.printf " %g %g\n%!" x y;
| 'n' -> goto_next := false; next_level();
| 'b' -> do_bg := not(!do_bg);
| 't' ->
Printf.printf " spent time %g sec.\n%!" (float !spent_time /. 10.);
| 'p' -> toggle_pause();
| _ -> ()
;;
let special ~key ~x ~y =
if not(!_stop) then
match key with
| GLUT_KEY_LEFT -> key_left_down := true
| GLUT_KEY_RIGHT -> key_right_down := true
| GLUT_KEY_UP -> angle_step := !angle_step +. 0.1
| GLUT_KEY_DOWN -> angle_step := !angle_step -. 0.1
| GLUT_KEY_PAGE_UP -> gravity := !gravity +. 20.0
| GLUT_KEY_PAGE_DOWN -> gravity := !gravity -. 20.0
| _ -> ()
;;
let special_up ~key ~x ~y =
if not(!_stop) then
match key with
| GLUT_KEY_LEFT -> key_left_down := false
| GLUT_KEY_RIGHT -> key_right_down := false
| _ -> ()
;;
(* }}} *)
(* {{{ menu *)
let rb_menu ~value =
match value with
| 1 -> toggle_pause()
| 2 -> restart_level();
| 3 -> texd := not(!texd);
| 4 -> do_tess := not(!do_tess);
| 5 -> do_bg := not(!do_bg);
| 6 -> glutFullScreen();
| 7 -> field := !field *. 0.8;
reshape
(glutGet GLUT_WINDOW_WIDTH)
(glutGet GLUT_WINDOW_HEIGHT);
glutPostRedisplay();
| 8 -> field := !field /. 0.8;
reshape
(glutGet GLUT_WINDOW_WIDTH)
(glutGet GLUT_WINDOW_HEIGHT);
glutPostRedisplay();
| 9 -> next_level();
| 0 -> exit 0;
| _ -> ()
;;
let resol_menu ~value =
match value with
| 800_600 -> glutReshapeWindow 800 600;
| 700_525 -> glutReshapeWindow 700 525;
| 640_480 -> glutReshapeWindow 640 480;
| 600_450 -> glutReshapeWindow 600 450;
| 500_375 -> glutReshapeWindow 500 375;
| 400_300 -> glutReshapeWindow 400 300;
| 300_225 -> glutReshapeWindow 300 225;
| 280_210 -> glutReshapeWindow 280 210;
| _ -> ()
;;
(* }}} *)
(* {{{ parse cmd arg *)
let parse_resolution str =
Scanf.sscanf str "%dx%d" (fun x y -> x, y)
;;
let parse_args() =
let argc = Array.length Sys.argv in
for i=0 to pred argc do
match Sys.argv.(i) with
| "-l" | "--level" ->
level_files := !level_files @ [Sys.argv.(i+1)];
| "-t" | "--texture" ->
texture_file := Sys.argv.(i+1);
texd := true;
do_bg := true;
| "-b" | "--no-bg" ->
do_bg := false;
| "-r" | "--resolution" ->
let w, h = parse_resolution Sys.argv.(i+1) in
win_width := w;
win_height := h;
| "-w" | "--wireframe"
| "--not-tess" ->
do_tess := false;
| _ -> ()
done;
;;
(* }}} *)
(* {{{ timer *)
let rec timer ~value =
if !key_left_down then angle_z := !angle_z -. (!angle_step);
if !key_right_down then angle_z := !angle_z +. (!angle_step);
glutTimerFunc ~msecs:sleep_ticks ~timer ~value:0;
glutPostRedisplay();
;;
let rec time_count ~value =
if not(!_stop) then incr spent_time;
glutTimerFunc ~msecs:100 ~timer:time_count ~value:0;
;;