Systematic Gaming

Code: OCaml base game

(************************************************************************************)

external find_window : string -> int = “stub_find_window””stub_find_window”
external init_gl’ : string -> unit = “stub_init_gl” “stub_init_gl”
external swap_buffers : unit -> unit = “stub_swap_buffers” “stub_swap_buffers”

let init_opengl () =
let _ = Random.self_init () in
let title = Printf.sprintf “%d.%d” (Random.int 1000000) (Random.int 1000000) in
Graphics.set_window_title title;
init_gl’ title;
Graphics.set_window_title “”;;

(************************************************************************************)

open Sdl
open Video
open Window
open Timer
open Event
open SDLGL
open Glcaml

let perspective fov aspect zNear zFar =
let pi = 4.0 *. (atan 1.0) in
let fH = (tan (fov /. 360.0 *. pi )) *. zNear in
let fW = fH *. aspect in
glFrustum (-.fW) fW (-.fH) fH zNear zFar

let ortho () =
let left = 0.0 and right = 640.0 and bottom = 0.0 and top = 480.0 in
glOrtho left right bottom top 0.0 1000.0

(* A general OpenGL initialization function. Sets all of the initial parameters. *)
let init_gl width height =
glViewport 0 0 width height;
glClearColor 0.0 0.0 0.0 0.0;
glClearDepth 1.0;
glDepthFunc GL_LESS;
glEnable GL_DEPTH_TEST;
glShadeModel GL_SMOOTH;
glMatrixMode GL_PROJECTION;
glLoadIdentity ();
(*
let aspect = (float_of_int width) /. (float_of_int height) in
perspective 45.0 aspect 1.0 100.0;
*)
ortho ();
glMatrixMode GL_MODELVIEW

(* The main drawing function. *)
let draw_gl_scene () =
glClear [GL_COLOR_BUFFER_BIT; GL_DEPTH_BUFFER_BIT];
glLoadIdentity ();

type tower =
{
x : float;
y : float;
};;

type gameState =
{
towers : tower list;
};;

let draw_tower tower =
glLoadIdentity ();
glTranslatef tower.x tower.y 0.0;
(* draw a square *)
glBegin GL_QUADS;
glVertex3f (-.10.0) 10.0 0.0;
glVertex3f 10.0 10.0 0.0;
glVertex3f 10.0 (-.10.0) 0.0;
glVertex3f (-.10.0)(-.10.0) 0.0;
glEnd ()

let draw_game state =
List.iter draw_tower state.towers

(* return true if point (px, py) is inside the box *)
let insideBox px py x y w h =
(* Printf.printf “%f %f: %f %f %f %f\n” px py x y (x +. w) (y +. h); *)
px >= x && px < x +. w && py >= y && py < y +. h

let tower_clicked bx by tower =
let clicked = insideBox bx by (tower.x -. 10.0) (tower.y -. 10.0) 20.0 20.0 in
(* Printf.printf “tower %f %f: %B\n” tower.x tower.y clicked; *)
flush stdout;
clicked

let remove_tower state bx by =
let newState : gameState = { towers = List.filter (fun x -> false = tower_clicked bx by x) state.towers } in
newState

(* Wait until a key is pressed or the window is closed *)
let rec handle_events state quit =
draw_gl_scene ();
draw_game state;
swap_buffers ();
if not quit then begin
match poll_event () with
| Key k -> handle_events state false;
| Button b ->

(* left click – create new tower, right click remove tower *)
if ( b.buttonstate = RELEASED ) then
if ( b.mousebutton = LEFT ) then
let t : tower = { x = float b.bx; y = 480.0 -. float b.by } in
let newState : gameState = { towers = state.towers @ [t] } in
handle_events newState false;
else if ( b.mousebutton = RIGHT ) then
let newState = remove_tower state (float b.bx) (480.0 -. float b.by) in
handle_events newState false;
else begin
handle_events state false;
end
else begin
handle_events state false;
end

| Quit ->
handle_events state true;
| _ ->
handle_events state false;
end

let main () =
init [VIDEO];
let w = 640 and h = 480 and bpp = 32 in
let _ = set_video_mode w h bpp [OPENGL] in
set_caption “Game” “Game”;
init_gl w h;
swap_buffers () ;

let initialState : gameState = { towers = [{ x = 100.0; y = 100.0}] } in
handle_events initialState false;
quit ()

(* Program entry point *)
let _ =
try
main ()
with
SDL_failure m -> failwith m

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: