Sunday, December 11, 2022
HomeGame DevelopmentEnter lag in 2D SDL2 recreation developed in OCaml utilizing an ECS...

Enter lag in 2D SDL2 recreation developed in OCaml utilizing an ECS method


Presently making an attempt to develop a 2D RPG much like early Closing Fantasy titles for the NES, and have adopted (what’s presumably) an entity-component-system method, outlined under:

Properties are the bottom-level constructing blocks. These are containers for client-defined information varieties.

  • Examples: the Place part is an integer tuple, for instance,
    whereas the Renderable part is a tuple of an SDL texture and a
    SDL renderer, each issues an entity that’s Renderable would wish
    to be rendered onscreen).

Elements encompass properties, and may rely on different parts.

  • Instance: The Sprite part is a Rectangle which has an updatable Place that’s Renderable. It subsequently bears these three properties.

Entities are principally simply IDs, as within the Artemis ECS mannequin (if I am understanding it appropriately), to which a set of parts (and subsequently properties) are related.

  • Instance: The participant is represented by an integer ID 0, and is a Sprite, which suggests it has the three properties talked about above.

There’s then an overarching System that retains observe of which properties are “certain” to which parts, and which parts are in flip “certain” to which entities.

Up till now I have been studying C++ tutorials on SDL2 and doing my finest to transform the ideas into OCaml. Nonetheless, I am unsure how I ought to deal with enter in a means in keeping with this ECS method to my recreation structure.

Proper now I am simply studying keyboard enter in my principal recreation loop as such:

let handle_events recreation =
  let e = Sdl.Occasion.create () in
  let _ = Sdl.poll_event (Some e) in
  match Sdl.Occasion.(enum (get e typ)) with
  | `Give up -> recreation.working <- false
  | `Key_down -> print_endline "key was pressed"
  | _ -> ()

(For these unfamiliar with practical sample matching, a match assertion right here sort of behaves like a switch-case assertion) This janky “occasion handler” is then known as each body in my principal, as such:

  whereas recreation.working do
    frame_start := Int32.to_int (Sdl.get_ticks ());
    Game_loop.handle_events recreation;
    Game_loop.replace recreation;
    Game_loop.render recreation;
    frame_time := Int32.to_int (Sdl.get_ticks ()) - !frame_start;
    if frame_delay > !frame_time then
      Sdl.delay (Int32.of_int (frame_delay - !frame_time))
  achieved;

The issue is, this method appears to not make superb use of the ECS framework I’ve painstakingly tried to ascertain, and nor does it appear to be well-performing — typically once I press a key repeatedly or maintain a key down, the sport takes seconds earlier than "key was pressed" reveals up within the console. That, and typically I can not even stop the sport.

I am have a sense I am going about this the improper means, however how?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments