White window when blitting an image

Hello there. I’ve been trying to make sdl works on my WSL in OCaml. I wrote a starting interface, and a program that could move an image, and had some success. But the next day, the moving screen becomes white screen. The starting interface works well. I uploaded my code to git and tried on other computers – it works for both of my friends, one on Windows and one on Mac, so I’m pretty sure the code should run.

I’m running SDL on an extension tsdl that translate C grammars to OCaml, and I’m using VcXsrv on windows to display videos from ubuntu. I have been trying delete and re-install almost everything, ubuntu, opam, tsdl, vcxsrv, and etc, but it still wouldn’t work on my computer only. Any suggestions on what could be the issue?

Here’s my code, just to make sure:

open Tsdl


(**[load_media] takes a path input, and it outputs a [sdl.surface] structure. *)
let load_media path = match Sdl.load_bmp path with
| Error (`Msg e) -> Sdl.log "Load bmp error: %s" e; exit 1
| Ok bmp -> bmp

(**[get_surface] returns the surface associated with the window [w].*)
let get_surface w = match Sdl.get_window_surface w with
| Error (`Msg e) -> Sdl.log "Create window error: %s" e; exit 1
| Ok surf -> surf

(**[update_window] copies the surface of window [w] to the screen*)
let update_window w = match Sdl.update_window_surface w with
| Error _ -> exit 1
| Ok () -> () 



(**[blit] use this function to perform 
a scaled surface copy to a destination surface.*)
let blit bmp bmp_op surf surf_op = match Sdl.blit_scaled ~src:bmp bmp_op ~dst:surf surf_op with
| Error (`Msg e) -> Sdl.log "Blit error: %s" e; exit 1
| Ok () -> ()

let w = match Sdl.init Sdl.Init.video with
| Error (`Msg e) -> Sdl.log "Init error: %s" e; exit 1
| Ok () ->
  match Sdl.create_window ~w:1600 ~h:900 "Game" Sdl.Window.opengl with
  | Error (`Msg e) -> Sdl.log "Create window error: %s" e; exit 1
  | Ok w -> w



module StartScreen = struct

(**[start] contains definition of a rectangle, with the origin at the 
upper left*)
let start = Sdl.Rect.create ~x:750 ~y:400 ~w:100 ~h:100


let is_pressed (rect : Sdl.rect) e =
  Sdl.Event.(get e mouse_button_x) > Sdl.Rect.x rect
  && Sdl.Event.(get e mouse_button_x) < (Sdl.Rect.x rect + Sdl.Rect.w rect)
  && Sdl.Event.(get e mouse_button_y) > Sdl.Rect.y rect
  && Sdl.Event.(get e mouse_button_y) < (Sdl.Rect.y rect + Sdl.Rect.h rect)


let event_loop () =
  let e = Sdl.Event.create () in
  let rec loop () = match Sdl.wait_event (Some e) with
  | Error (`Msg e) -> Sdl.log " Could not wait event: %s" e; ()
  | Ok () ->
      match Sdl.Event.(enum (get e typ)) with
      | `Quit -> exit 0
      | `Mouse_button_down ->
        if is_pressed start e
        then match Sdl.show_simple_message_box 
        Sdl.Message_box.information ~title:"Start" "Game Starting" None with
        | Ok () -> ()
        | Error _ -> exit 0
        else loop ()
      | `Drop_file -> Sdl.Event.drop_file_free e; loop ()
      | _ -> loop ()
  in
  Sdl.start_text_input ();
  loop ()

  let main () =
    let bmp = load_media "bmp/catface.bmp" in
    blit bmp None (get_surface w) (Some start);
    update_window w;
    event_loop ();

end

module MovingScreen = struct
  let map_bmp = load_media "bmp/maptest.bmp"
  let chara_bmp = load_media "bmp/catface.bmp"
  let pos = Sdl.Rect.create ~x:750 ~y:400 ~w:100 ~h:100
  let screen = Sdl.Rect.create ~x:0 ~y:0 ~w:1600 ~h:900

  let move_up w chara =
    Sdl.Rect.(set_y pos (y pos - 10));
    blit map_bmp None (get_surface w) (Some screen);
    blit chara None (get_surface w) (Some pos);
    update_window w 
  
  let move_left w chara =
    Sdl.Rect.(set_x pos (x pos - 10));
    blit map_bmp None (get_surface w) (Some screen);
    blit chara None (get_surface w) (Some pos);
    update_window w 

  let move_right w chara =
    Sdl.Rect.(set_x pos (x pos + 10));
    blit map_bmp None (get_surface w) (Some screen);
    blit chara None (get_surface w) (Some pos);
    update_window w 
  
  let move_down w chara = 
    Sdl.Rect.(set_y pos (y pos + 10));
    blit map_bmp None (get_surface w) (Some screen);
    blit chara None (get_surface w) (Some pos);
    update_window w

  let is_wasd_pressed e = 
    if Sdl.Event.(get e keyboard_keycode) = int_of_char 'w' then "up"
    else if Sdl.Event.(get e keyboard_keycode) = int_of_char 'a' then "left"
    else if Sdl.Event.(get e keyboard_keycode) = int_of_char 's' then "down"
    else "right"
      
  
  let is_a_pressed e =
    Sdl.Event.(get e keyboard_keycode) = int_of_char 'a'
  

  let event_loop () =
    let e = Sdl.Event.create () in
    let rec loop () = match Sdl.wait_event (Some e) with
    | Error (`Msg e) -> Sdl.log " Could not wait event: %s" e; ()
    | Ok () ->
      match Sdl.Event.(enum (get e typ)) with
      | `Quit -> exit 0
      | `Key_down -> 
        begin match is_wasd_pressed e with
        | "up" -> move_up w chara_bmp; loop ()
        | "left" -> move_left w chara_bmp; loop ()
        | "down" -> move_down w chara_bmp; loop ()
        | "right" -> move_right w chara_bmp; loop ()
        | _ -> loop ();
        end
      | _ -> loop ()
      in Sdl.start_text_input (); loop ()

  let main () =
    blit map_bmp None (get_surface w) (Some screen);
    blit chara_bmp None (get_surface w) (Some pos);
    update_window w;
    event_loop ();
    

end
let main () =
  StartScreen.main ();
  MovingScreen.main ();
  Sdl.quit ();
  exit 0

let () = main ()