开发者

Problem handling keyboard with openGL and SDL in OCaml

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 beca开发者_JS百科use 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


First things first: You're mixing GLUT with SDL, both will battle for dealing with windowing and input event handling. Settle for one of those. GLUT is not part of OpenGL

I presume you have this from some tutorial. The key line is this one, which uses GLUT:

Glut.keyboardFunc (fun ~key ~x ~y -> if key=27 then exit 0);

Here a lambda (=anonymous) function is created in situ and passed to Glut.keyboadFunc as handler. This lambda will call exit 0 if ESC is passed. To fix this you first need to get rid of GLUT (all statements beginning with Glut.) and replace it with SDL counterparts.

Furthermore you placed the SDL event polling in the render function. This is just WRONG! Event polling should happen, where at the moment Glut.mainLoop() is.

Also your render function lacks proper viewport, projection and modelview setup, this needs fixing as well.


It looks to me like both SDL and Glut think they are supposed to be reading the keys. So maybe Glut is winning and SDL is losing. Does the program exit if you hit ESC (which has a code of 27)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜