Skip to content

input.h

Devon edited this page Feb 12, 2021 · 1 revision

If you find any errors, please open an issue or submit a pull request!

Preface

The input system is reliant on the rendering system due to both using GLFW's features. Due to this reason you'll want to make sure you link the two together for usage. The specific order of initialization doesn't make too much of a difference, but for runtime usage you want to make sure to call r_ctx_set_i_ctx with the pointer to both your render and input contexts. This just enables the callbacks to make sure the input system get's the information it needs.

Function Ordering

  1. i_ctx_create
  2. r_ctx_set_i_ctx (for callbacks)
  3. RUNTIME (i_ctx_update & i_poll_events)
  4. i_ctx_destroy

Basic Input

Every frame you'll want to call i_ctx_update and i_poll_events in order to update the states for your system. After this you can call the input check functions you want. There are 3 base types of input's you can check:

- key
- mouse
- joy

On top of those you can use the i_binding type to include any of the 3 base types into an easily checked type. All 4 of these types have the following functions:

- i_xxx_clicked - if was clicked this frame (not last)
- i_xxx_released - if was released this frame (not last)
- i_xxx_down - if is pressed this frame
- i_xxx_up - if is not pressed this frame

As well you can check a joystick's axes with i_joy_axis which will return the floating point value of that specific joy axis.If your joystick isn't automatically working, you should call i_joy_create with the ID of the joystick created.

Character Callback

If you want to get input that a user is typing in the form of characters, you can do so by setting i_set_char_tracking and then getting the characters with: i_get_chars

Mouse Movement

Within the input system, both the mouse & the mouse scroll are tracked in similar ways. You can interact with the buttons using the standard checks like i_mouse_clicked and whatnot (similar to keys / joy buttons). However, in order to check for the mouse position, delta, scroll position, or scroll delta, you have to use one of the following functions:

void   i_mouse_get_pos(i_ctx* ctx, double* x, double* y);
double i_mouse_get_x(i_ctx* ctx);
double i_mouse_get_y(i_ctx* ctx);

void   i_mouse_get_delta(i_ctx* ctx, double* x, double* y);
double i_mouse_get_dx(i_ctx* ctx);
double i_mouse_get_dy(i_ctx* ctx);

Basic Example

This is meant to be an outline of the functions you should call in order to get input working in your program.

i_ctx* input_ctx = i_ctx_create(4, 32, 4, 16, 16, 32);

... 

// Link the input ctx to render ctx for callbacks
r_ctx_set_i_ctx(render_ctx, input_ctx);

while(1){
  i_ctx_update(input_ctx);
  i_poll_events();

  if(i_key_clicked(input_ctx, KEY_SPACE)){
    printf("Hello world!\n");
  }
}

REFERENCE

This is reference section is meant to be a programmer's companion when using astera!

NOTE: This is still under construction, feedback is welcome!

Bindings

All of the types (key, mouse button, joy button / axis) can be used within an i_binding type. Note, an i_binding's value (event to trigger on) and alt (alternate event to trigger on) can both be different types, which are set with their respective type values.

In order to create a binding, you can do so by calling:

void i_binding_add(i_ctx* ctx, const char* name, int value, int type);
void i_binding_add_alt(i_ctx* ctx, const char* name, int value, int type);

If you want to modify a binding in real time, you can set i_enable_binding_track which will then set the next input to the respective value:

NOTE: You can set the binding types as ASTERA_BINDING_KEY, ASTERA_BINDING_MB, ASTERA_BINDING_JOYA, or ASTERA_BINDING_JOYB.

// Set a key binding's value (alt = 1, value = 0) to the next input type
void i_enable_binding_track(i_ctx* ctx, const char* key_binding, uint8_t alt);

Otherwise, the i_binding type has all of the usual up, down, clicked, released functions.

Scroll Input

Scroll input acts similar to mouse movement in the sense that it has an X & Y value, as well as a delta-X and delta-Y (dx / dy) value. This value can be reset with the i_scroll_reset in order to allow for window / scroll position tracking with the values.

Macros

Many of the button macro values are modified based on the target system being compiled for. This is due to different operating system drivers remapping different buttons / axes to different numbers.

  • ASTERA_KB_NAMELEN - The max length of a key binding's name
  • ASTERA_BINDING_KEY
  • ASTERA_BINDING_MB
  • ASTERA_BINDING_JOYA
  • ASTERA_BINDING_JOYB
  • KEY_A - KEY_Z
  • KEY_0 - KEY_9
  • XBOX_A
  • XBOX_B
  • XBOX_X
  • XBOX_Y
  • XBOX_L1
  • XBOX_R1
  • XBOX_SELECT
  • XBOX_START
  • XBOX_LEFT_STICK
  • XBOX_RIGHT_STICK
  • MOUSE_LEFT
  • MOUSE_RIGHT
  • MOUSE_MIDDLE
  • KEY_SPACE
  • KEY_BACKSPACE
  • KEY_DELETE
  • KEY_UP
  • KEY_DOWN
  • KEY_RIGHT
  • KEY_LEFT
  • KEY_HOME
  • KEY_TAB
  • KEY_ESC
  • KEY_ESCAPE
  • KEY_LEFT_SHIFT
  • KEY_RIGHT_SHIFT
  • KEY_ENTER
  • KEY_LEFT_CTRL
  • KEY_RIGHT_CTRL
  • KEY_LEFT_ALT
  • KEY_RIGHT_ALT
  • KEY_LEFT_SUPER
  • KEY_RIGHT_SUPER