I've a problem handling keyboards events using SDL (only for keyboard events) and openGL (to display graphics, I'm planning to display 3d graphics). When I use Sdlevent.poll nothing happen and I can't use wait_event because I've to display in real time. I've tried almost everyting I thinks and NOTHING has been successful, you are my only hope ! ^^
Here is my source code (it's just a spinning square ...)
let rotate = ref 1.0
let _ = Sdl.init [`VIDEO] ; at_exit Sdl.quit
let key_handler k = match k with
| {Sdlevent.keysym=Sdlkey.KEY_ESCAPE} ->
print_endline "I'll miss you so much..." ;
| {Sdlevent.keysym=Sdlkey.KEY_SPACE} ->
print_endline "UP" ;
| {Sdlevent.keysym=Sdlkey.KEY_DOWN} ->
print_endline "DOWN";
| _ -> ()
let renderScene () =
let current_time = ref (Sdltimer.get_ticks()) in
GlClear.clear[`color;`depth];
GlMat.rotate3 (!rotate) (0.0,0.0,1.0);
GlDraw.begins `quad_strip;
GlDraw.color (1.0, 0.0, 0.0);
GlDraw.vertex3 (-0.5, 0.5, 0.0);
GlDraw.vertex3 (-0.5, -0.5, 0.0);
GlDraw.color (0.0, 0.0, 1.0);
GlDraw.vertex3 (0.5, 0.5, 0.0);
GlDraw.vertex3 (0.5,-0.5, 0.0);
GlDraw.ends ();
current_time := Sdltimer.get_ticks() - !current_time;
if(!current_time < 10) then
Sdltimer.delay (10 - !current_time);
Glut.swapBuffers();
Glut.postRedisplay();
begin
match Sdlevent.poll () with
| Some (Sdlevent.KEYDOWN k) ->
print_endline "nope"; key_handler k
| None | _ -> print_endline "nopeee";
end;
Gl.flush();;
let () =
ignore(Glut.init Sys.argv);
Glut.initDisplayMode ~alpha:true ~double_buffer:true ~depth:true ();
Glut.initWindowPosition ~x:100 ~y:100;
Glut.initWindowSize ~w:500 ~h:500;
ignore(Glut.createWindow ~title:"3DMapPro - Edition Expert Business 2012");
GlClear.color(1.,1.,1.);
Glut.displayFunc ~cb:(renderScene);
print_string "oui";
Glut.keyboardFunc (fun ~key ~x ~y -> if key=27 then exit 0);
Glut.mainLoop();;
Since I don't understand how to post code properly on stackoverflow, you can also find my source code well indented and highlighted here : Pastebin