text
stringlengths
0
601k
let event_loop ( ) = let e = Sdl . Event . create ( ) in let rec loop ( ) = match Sdl . wait_event ( Some e ) with | Error ( ` Msg e ) -> log_err " Could not wait event : % s " e ; ( ) | Ok ( ) -> log " % a " Fmts . pp_event e ; match Sdl . Event . ( enum ( get e typ ) ) with | ` Quit -> ( ) | ` Drop_file -> Sdl . Even...
let main ( ) = let inits = Sdl . Init . ( video + joystick + gamecontroller + events ) in match Sdl . init inits with | Error ( ` Msg e ) -> log_err " SDL init : % s " e | Ok ( ) -> let flags = Sdl . Window . ( shown + mouse_focus + resizable ) in match Sdl . create_window ~ w : 640 ~ h : 480 " SDL events " flags with ...
let ( ) = main ( )
let pi = acos ( - 1 . )
let gear ~ inner_radius ~ outer_radius ~ width ~ teeth ~ tooth_depth = let r0 = inner_radius and r1 = outer_radius . - tooth_depth . / 2 . 0 and r2 = outer_radius . + tooth_depth . / 2 . 0 in let ta = 2 . 0 . * pi . / float teeth in let da = ta . / 4 . 0 in GlDraw . shade_model ` flat ; GlDraw . normal ~ z : 1 . 0 ( ) ...
let view_rotx = ref 20 . 0
let view_roty = ref 30 . 0
let view_rotz = ref 0 . 0
let angle = ref 0 . 0
let increase ( ? by = 5 . 0 ) r = r := ! r . + by
let decrease ( ? by = 5 . 0 ) r = r := ! r . - by
let t0 = ref 0
let frames = ref 0
let draw ( gear1 , gear2 , gear3 ) = GlClear . clear [ ` color ; ` depth ] ; GlMat . push ( ) ; GlMat . rotate ~ angle :! view_rotx ~ x : 1 . 0 ( ) ; GlMat . rotate ~ angle :! view_roty ~ y : 1 . 0 ( ) ; GlMat . rotate ~ angle :! view_rotz ~ z : 1 . 0 ( ) ; GlMat . push ( ) ; GlMat . translate ~ x ( :- 3 . 0 ) ~ y ( :-...
let idle delay_fun = increase angle ~ by : 2 . 0 ; delay_fun ( )
let reshape ~ w ~ h = ignore ( Sdlvideo . set_video_mode ~ w ~ h [ ` OPENGL ; ` RESIZABLE ] ) ; GlDraw . viewport ~ x : 0 ~ y : 0 ~ w ~ h ; GlMat . mode ` projection ; GlMat . load_identity ( ) ; begin if w > h then let r = float w . / float h in GlMat . frustum ~ x ( . :- r , r ) ~ y ( :- 1 . 0 , 1 . 0 ) ~ z ( : 5 . 0...
let init ( ) = let pos = 5 . 0 , 5 . 0 , 10 . 0 , 0 . 0 and red = 0 . 8 , 0 . 1 , 0 . 0 , 1 . 0 and green = 0 . 0 , 0 . 8 , 0 . 2 , 1 . 0 and blue = 0 . 2 , 0 . 2 , 1 . 0 , 1 . 0 in GlLight . light ~ num : 0 ( ` position pos ) ; List . iter Gl . enable [ ` cull_face ; ` lighting ; ` light0 ; ` depth_test ; ` normalize ...
let finished ( ) = Sdl . quit ( ) ; exit 0
let do_key = function | KEY_ESCAPE | KEY_q -> finished ( ) | KEY_UP -> increase view_rotx | KEY_DOWN -> decrease view_rotx | KEY_LEFT -> increase view_roty | KEY_RIGHT -> decrease view_roty | KEY_z -> if get_mod_state ( ) land kmod_shift != 0 then else | _ -> ( )
let check_events ( ) = while has_event ( ) do match wait_event ( ) with | VIDEORESIZE ( w , h ) -> reshape ~ w ~ h | KEYDOWN { keysym = key } -> do_key key | QUIT -> finished ( ) | _ -> ( ) done
let framerate target = let last_time = ref ( Sdltimer . get_ticks ( ) ) in let interv = int_of_float ( 1000 . . / target ) in fun ( ) -> let t = Sdltimer . get_ticks ( ) in let to_wait = ! last_time + interv - t in if to_wait <= 0 then last_time := t else begin last_time := ! last_time + interv ; Sdltimer . delay to_wa...
let target_framerate = ref 75 .
let _ = Arg . parse [ ] ( fun a -> try target_framerate := float_of_string a with Failure _ -> ( ) ) ( Printf . sprintf " usage : % s [ framerate ] " ( Filename . basename Sys . executable_name ) )
let main ( ) = Sdl . init [ ` VIDEO ] ; Sdlwm . set_caption ~ title : " OCamlSDL Gears " ~ icon : " gears " ; reshape ~ w : 300 ~ h : 300 ; let delay = framerate ! target_framerate in let gears = init ( ) in enable_key_repeat ( ) ; while true do idle delay ; check_events ( ) ; draw gears done
let _ = main ( )
type vect = int * int = " ml_roundedRectangleRGBA_bc " " ml_roundedRectangleRGBA " = " ml_roundedBoxRGBA_bc " " ml_roundedBoxRGBA " = " ml_thickLineRGBA_bc " " ml_thickLineRGBA " = " ml_arcColor_bc " " ml_arcColor " = " ml_arcRGBA_bc " " ml_arcRGBA "
type context = " caml_SDL_GL_CreateContext " win : Sdlwindow . t -> ctx : context -> int = " caml_SDL_GL_MakeCurrent " = " caml_SDL_GL_UnloadLibrary " = " caml_SDL_GL_ExtensionSupported " = " caml_SDL_GL_SetSwapInterval " = " caml_SDL_GL_GetSwapInterval " = " caml_SDL_GL_SwapWindow " = " caml_SDL_GL_DeleteContext "
type gl_attr = | GL_RED_SIZE | GL_GREEN_SIZE | GL_BLUE_SIZE | GL_ALPHA_SIZE | GL_BUFFER_SIZE | GL_DOUBLEBUFFER | GL_DEPTH_SIZE | GL_STENCIL_SIZE | GL_ACCUM_RED_SIZE | GL_ACCUM_GREEN_SIZE | GL_ACCUM_BLUE_SIZE | GL_ACCUM_ALPHA_SIZE | GL_STEREO | GL_MULTISAMPLEBUFFERS | GL_MULTISAMPLESAMPLES | GL_ACCELERATED_VISUAL | GL_R...
let to_string = function | AccelerometerAsJoystick -> " SDL_ACCELEROMETER_AS_JOYSTICK " | AndroidApkExpansionMainFileVersion -> " SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION " | AndroidApkExpansionPatchFileVersion -> " SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION " | AndroidSeparateMouseAndTouch -> " SDL_ANDROID_SEPARA...
let of_string s = match String . uppercase_ascii s with | " SDL_ACCELEROMETER_AS_JOYSTICK " -> AccelerometerAsJoystick | " SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION " -> AndroidApkExpansionMainFileVersion | " SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION " -> AndroidApkExpansionPatchFileVersion | " SDL_ANDROID_SEPARAT...
type priority = | Default | Normal | Override = " caml_SDL_SetHint " = " caml_SDL_SetHintWithPriority "
let set hint value = sdl_set_hint ( to_string hint ) hint value
let set_with_priority hint value prio = sdl_set_hint_with_priority ( to_string hint ) hint value prio
let _ = Callback . register_exception " SDLjoystick_exception " ( SDLjoystick_exception " " )
let hat_leftdown = 0x0C = " ml_SDL_JoystickGetAxis " = " ml_SDL_JoystickGetHat " = " ml_SDL_JoystickGetBall " = " ml_SDL_JoystickGetButton "
type t = | KEY_UNKNOWN | KEY_BACKSPACE | KEY_TAB | KEY_CLEAR | KEY_RETURN | KEY_PAUSE | KEY_ESCAPE | KEY_SPACE | KEY_EXCLAIM | KEY_QUOTEDBL | KEY_HASH | KEY_DOLLAR | KEY_AMPERSAND | KEY_QUOTE | KEY_LEFTPAREN | KEY_RIGHTPAREN | KEY_ASTERISK | KEY_PLUS | KEY_COMMA | KEY_MINUS | KEY_PERIOD | KEY_SLASH | KEY_0 | KEY_1 | KE...
let keycodes = [ ( 0 , 0 ) ; ( 8 , 9 ) ; ( 12 , 13 ) ; ( 19 , 19 ) ; ( 27 , 27 ) ; ( 32 , 36 ) ; ( 38 , 64 ) ; ( 91 , 122 ) ; ( 127 , 127 ) ; ( 160 , 296 ) ; ( 300 , 322 ) ; ]
let num_keys = ( Obj . magic KEY_UNDO ) + 1
let keycode_table = let table = Array . make num_keys 0 in let rec proc i = function | ( a , b ) :: l -> | [ ] -> ( ) in proc 0 keycodes ; Callback . register " keycode_table " table ; table
let max_code = keycode_table . ( num_keys - 1 )
let int_of_key ( key : t ) = keycode_table . ( Obj . magic key )
let char_of_key key = let code = int_of_key key in if code < 256 then Char . chr code else invalid_arg " Sdlkey . char_of_key "
let rev_keycode_table = let table = Array . make ( max_code + 1 ) ( - 1 ) in for i = 0 to pred num_keys do table . ( keycode_table . ( i ) ) <- i done ; Callback . register " rev_keycode_table " table ; table
let key_of_int n = if n < 0 || n > max_code then invalid_arg " Sdlkey . key_of_int " ; let k = rev_keycode_table . ( n ) in if k < 0 then invalid_arg " Sdlkey . key_of_int " else ( Obj . magic k : t ) = " ml_SDL_GetKeyName "
let name key = _name ( int_of_key key ) = " ml_SDL_EnableUNICODE " = " ml_SDL_QueryUNICODE " = " ml_SDL_DisableKeyRepeat " = " ml_SDL_EnableKeyRepeat " = " ml_SDL_GetKeyState " = " ml_sdl_key_pressed "
let is_key_pressed key = _is_key_pressed ( int_of_key key )
let kmod_meta = 0x0C00 = " ml_SDL_GetModState " = " ml_SDL_SetModState "
type t = | Unknown | Return | Escape | Backspace | Tab | Space | Exclaim | QuoteDBL | Hash | Percent | Dollar | Ampersand | Quote | LeftParen | RightParen | Asterisk | Plus | Comma | Minus | Period | Slash | Num0 | Num1 | Num2 | Num3 | Num4 | Num5 | Num6 | Num7 | Num8 | Num9 | Colon | SemiColon | Less | Equals | Greate...
let to_string = function | Unknown -> " Unknown " | Return -> " Return " | Escape -> " Escape " | Backspace -> " Backspace " | Tab -> " Tab " | Space -> " Space " | Exclaim -> " Exclaim " | QuoteDBL -> " QuoteDBL " | Hash -> " Hash " | Percent -> " Percent " | Dollar -> " Dollar " | Ampersand -> " Ampersand " | Quote -...
let of_string s = match String . lowercase_ascii s with | " unknown " -> Unknown | " return " -> Return | " escape " -> Escape | " backspace " -> Backspace | " tab " -> Tab | " space " -> Space | " exclaim " -> Exclaim | " quotedbl " -> QuoteDBL | " hash " -> Hash | " percent " -> Percent | " dollar " -> Dollar | " amp...
let _ = Callback . register_exception " SDLmixer_exception " ( SDLmixer_exception " " )
type format = | AUDIO_FORMAT_U8 | AUDIO_FORMAT_S8 | AUDIO_FORMAT_U16LSB | AUDIO_FORMAT_S16LSB | AUDIO_FORMAT_U16MSB | AUDIO_FORMAT_S16MSB | AUDIO_FORMAT_U16SYS | AUDIO_FORMAT_S16SYS
type fade_status = | NO_FADING | FADING_OUT | FADING_IN
type music_kind = | NONE | CMD | WAV | MOD | MID | OGG | MP3
type channels = MONO | STEREO
type specs = { frequency : int ; format : format ; channels : channels } ? freq : int -> ? format : format -> ? chunksize : int -> ? channels : channels -> unit -> unit = " sdlmixer_open_audio "
let loadWAV_from_mem buff = loadWAV_RW ( Sdl . rwops_from_mem buff )
let num_channels ( ) = allocate_channels ~- 1 " sdlmixer_fadein_music "
let play_sound chunk = ignore ( play_channel chunk )
type button = | BUTTON_LEFT | BUTTON_MIDDLE | BUTTON_RIGHT | BUTTON_WHEELUP | BUTTON_WHEELDOWN | BUTTON_X of int = " mlsdlevent_get_mouse_state " = " ml_SDL_WarpMouse "
type cursor_data = { data : ( int , int8_unsigned_elt , c_layout ) Array2 . t ; mask : ( int , int8_unsigned_elt , c_layout ) Array2 . t ; w : int ; h : int ; hot_x : int ; hot_y : int ; } data ( : int , int8_unsigned_elt , c_layout ) Array2 . t -> mask ( : int , int8_unsigned_elt , c_layout ) Array2 . t -> hot_x : int...
let string_of_bits x = let s = String . make 8 ' ' in for i = 0 to 7 do if x land ( 1 lsl i ) <> 0 then s . [ 7 - i ] <- ' ' @ done ; s
let pprint_cursor c = let { data = data ; mask = mask } = cursor_data c in let h = Array2 . dim1 data in let w = Array2 . dim2 data in print_endline " data " : ; for i = 0 to pred h do for j = 0 to pred w do print_string ( string_of_bits data . { i , j } ) done ; print_newline ( ) done ; print_newline ( ) ; print_endli...
let convert_to_cursor ~ data ~ mask ~ w ~ h ~ hot_x ~ hot_y = if Array . length data <> Array . length mask || w mod 8 <> 0 || Array . length data <> h * w / 8 then invalid_arg " Sdlmouse . convert_to_cursor " ; let w ' = w / 8 in let b_data = Array2 . create int8_unsigned c_layout h w ' in let b_mask = Array2 . create...
type t = | Unknown | Index1LSB | Index1MSB | Index4LSB | Index4MSB | Index8 | RGB332 | RGB444 | RGB555 | BGR555 | ARGB4444 | RGBA4444 | ABGR4444 | BGRA4444 | ARGB1555 | RGBA5551 | ABGR1555 | BGRA5551 | RGB565 | BGR565 | RGB24 | BGR24 | RGB888 | RGBX8888 | BGR888 | BGRX8888 | ARGB8888 | RGBA8888 | ABGR8888 | BGRA8888 | ...
let to_string = function | Unknown -> " SDL_PIXELFORMAT_UNKNOWN " | Index1LSB -> " SDL_PIXELFORMAT_INDEX1LSB " | Index1MSB -> " SDL_PIXELFORMAT_INDEX1MSB " | Index4LSB -> " SDL_PIXELFORMAT_INDEX4LSB " | Index4MSB -> " SDL_PIXELFORMAT_INDEX4MSB " | Index8 -> " SDL_PIXELFORMAT_INDEX8 " | RGB332 -> " SDL_PIXELFORMAT_RGB33...
let of_string s = match String . uppercase_ascii s with | " SDL_PIXELFORMAT_UNKNOWN " -> Unknown | " SDL_PIXELFORMAT_INDEX1LSB " -> Index1LSB | " SDL_PIXELFORMAT_INDEX1MSB " -> Index1MSB | " SDL_PIXELFORMAT_INDEX4LSB " -> Index4LSB | " SDL_PIXELFORMAT_INDEX4MSB " -> Index4MSB | " SDL_PIXELFORMAT_INDEX8 " -> Index8 | " ...
type t = Sdltype . renderer width : int -> height : int -> flags : Sdlwindow . window_flags list -> Sdlwindow . t * t = " caml_SDL_CreateWindowAndRenderer "
type renderer_flags = | Software | Accelerated | PresentVSync | TargetTexture
let string_of_renderer_flags = function | Software -> " Software " | Accelerated -> " Accelerated " | PresentVSync -> " PresentVSync " | TargetTexture -> " TargetTexture "
let renderer_flags_of_string s = match String . lowercase_ascii s with | " software " -> Software | " accelerated " -> Accelerated | " presentvsync " -> PresentVSync | " targettexture " -> TargetTexture | _ -> invalid_arg " Sdlrender . renderer_flags_of_string " win : Sdlwindow . t -> index : int -> flags : renderer_fl...
type renderer_flip = | Flip_None | Flip_Horizontal | Flip_Vertical texture : Sdltexture . t -> ? src_rect : Sdlrect . t -> ? dst_rect : Sdlrect . t -> ? angle : float -> ? center : int * int -> ? flip : renderer_flip -> unit -> unit = " caml_SDL_RenderCopyEx_bc " " caml_SDL_RenderCopyEx " = " caml_SDL_RenderSetScale " ...
type renderer_info = { name : string ; max_texture_width : int ; max_texture_height : int ; } = " caml_SDL_GetRenderDrivers " = " caml_SDL_RenderReadPixels " = " caml_SDL_SetRenderTarget "
type t = " caml_SDL_RWFromMem " = " caml_SDL_RWFromConstMem " = " caml_SDL_RWFromFile "
type input = [ | ` Filename of string | ` Buffer of bytes | ` String of string ]
let from_input = function | ` Filename ( Filenamefilename ) Filenamefilename -> from_file ~ filename ~ mode " : r " | ` Buffer ( Buffermem ) Buffermem -> from_mem mem | ` String ( Stringmem ) Stringmem -> from_const_mem mem
let from_input_opt = function | ` Filename ( Filenamefilename ) Filenamefilename -> Some ( Somefrom_file ~ filename ~ mode " : r ) " | ` Buffer ( Buffermem ) Buffermem -> Some ( Somefrom_mem mem ) mem | ` String ( Stringmem ) Stringmem -> Some ( Somefrom_const_mem mem ) mem | _ -> None
type seek = | SEEK_SET | SEEK_CUR | SEEK_END = " caml_SDL_RWseek " = " caml_SDL_RWtell "
module BigEndian = struct external read16 : t -> uint16 = " caml_SDL_ReadBE16 " external read32 : t -> uint32 = " caml_SDL_ReadBE32 " external read64 : t -> uint64 = " caml_SDL_ReadBE64 " external write16 : t -> uint16 -> unit = " caml_SDL_WriteBE16 " external write32 : t -> uint32 -> unit = " caml_SDL_WriteBE32 " exte...
module LittleEndian = struct external read16 : t -> uint16 = " caml_SDL_ReadLE16 " external read32 : t -> uint32 = " caml_SDL_ReadLE32 " external read64 : t -> uint64 = " caml_SDL_ReadLE64 " external write16 : t -> uint16 -> unit = " caml_SDL_WriteLE16 " external write32 : t -> uint32 -> unit = " caml_SDL_WriteLE32 " e...
type t = | UNKNOWN | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Num1 | Num2 | Num3 | Num4 | Num5 | Num6 | Num7 | Num8 | Num9 | Num0 | RETURN | ESCAPE | BACKSPACE | TAB | SPACE | MINUS | EQUALS | LEFTBRACKET | RIGHTBRACKET | BACKSLASH | NONUSHASH | SEMICOLON |...
let to_string = function | UNKNOWN -> " UNKNOWN " | A -> " A " | B -> " B " | C -> " C " | D -> " D " | E -> " E " | F -> " F " | G -> " G " | H -> " H " | I -> " I " | J -> " J " | K -> " K " | L -> " L " | M -> " M " | N -> " N " | O -> " O " | P -> " P " | Q -> " Q " | R -> " R " | S -> " S " | T -> " T " | U -> " U...
let of_string s = match String . uppercase_ascii s with | " UNKNOWN " -> UNKNOWN | " A " -> A | " B " -> B | " C " -> C | " D " -> D | " E " -> E | " F " -> F | " G " -> G | " H " -> H | " I " -> I | " J " -> J | " K " -> K | " L " -> L | " M " -> M | " N " -> N | " O " -> O | " P " -> P | " Q " -> Q | " R " -> R | " S...
type t width : int -> height : int -> depth : int -> t = " caml_SDL_CreateRGBSurface " = " caml_SDL_FreeSurface " = " caml_SDL_LoadBMP " = " caml_SDL_SaveBMP " dst : t -> rect : Sdlrect . t -> color : int32 -> unit = " caml_SDL_FillRect " src : t -> src_rect : Sdlrect . t -> dst : t -> dst_rect : Sdlrect . t -> Sdlrect...
let _ = Callback . register_exception " SDLttf_exception " ( SDLttf_exception " " )
type font_style = | NORMAL | BOLD | ITALIC | UNDERLINE fg : color -> surface = " sdlttf_render_text_solid " fg : color -> bg : color -> surface = " sdlttf_render_text_shaded " fg : color -> surface = " sdlttf_render_text_blended " fg : color -> surface = " sdlttf_render_utf8_solid " fg : color -> bg : color -> surface ...
type render_kind = | SOLID of color | SHADED of color * color | BLENDED of color
let render_text font kind txt = match kind with | SOLID fg -> render_text_solid font txt ~ fg | SHADED ( fg , bg ) -> render_text_shaded font txt ~ fg ~ bg | BLENDED fg -> render_text_blended font txt ~ fg fg : color -> surface = " sdlttf_render_glyph_solid " fg : color -> bg : color -> surface = " sdlttf_render_glyph_...
let render_glyph font kind c = match kind with | SOLID fg -> render_glyph_solid font c ~ fg | SHADED ( fg , bg ) -> render_glyph_shaded font c ~ fg ~ bg | BLENDED fg -> render_glyph_blended font c ~ fg = " sdlttf_glyph_metrics "
let _ = Callback . register_exception " SDLvideo2_exception " ( Video_exn " " )
type rect = { mutable r_x : int ; mutable r_y : int ; mutable r_w : int ; mutable r_h : int ; }
let rect ~ x ~ y ~ w ~ h = { r_x = x ; r_y = y ; r_w = w ; r_h = h }
let copy_rect r = { r with r_x = r . r_x }
type pixel_format_info = { palette : bool ; bits_pp : int ; bytes_pp : int ; rmask : int32 ; gmask : int32 ; bmask : int32 ; amask : int32 ; rshift : int ; gshift : int ; bshift : int ; ashift : int ; rloss : int ; gloss : int ; bloss : int ; aloss : int ; colorkey : int32 ; alpha : int ; }
type video_info = { hw_available : bool ; wm_available : bool ; blit_hw : bool ; blit_hw_color_key : bool ; blit_hw_alpha : bool ; blit_sw : bool ; blit_sw_color_key : bool ; blit_sw_alpha : bool ; blit_fill : bool ; video_mem : int ; current_w : int ; current_h : int ; }
type video_flag = [ | ` SWSURFACE | ` HWSURFACE | ` ASYNCBLIT | ` ANYFORMAT | ` HWPALETTE | ` DOUBLEBUF | ` FULLSCREEN | ` OPENGL | ` OPENGLBLIT | ` RESIZABLE | ` NOFRAME = " ml_SDL_GetVideoInfo " = " ml_SDL_GetVideoInfo_format " = " ml_SDL_VideoDriverName "
type modes = | NOMODE | ANY | DIM of ( int * int ) list = " ml_SDL_ListModes " = " ml_SDL_VideoModeOK "
type surface_flags = [ | video_flag | ` HWACCEL | ` SRCCOLORKEY | ` RLEACCEL | ` SRCALPHA | ` PREALLOC ]
type surface_info = { flags : surface_flags list ; w : int ; h : int ; pitch : int ; clip_rect : rect ; refcount : int ; } = " ml_sdl_surface_info "
let surface_dims s = let { w = w ; h = h ; pitch = pitch } = surface_info s in ( w , h , pitch ) = " ml_sdl_surface_info_format "