revolver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

platform_gfx.c (2819B)


      1 //////////////////////////////////////////////////////////////////
      2 // platform_gfx.c
      3 
      4 RV_GLOBAL bool32 rv_key_pressed(rv_keycode_t keycode)
      5 {
      6     return rv_keyboard_context.current_state[keycode] && !rv_keyboard_context.last_state[keycode];
      7 }
      8 
      9 RV_GLOBAL bool32 rv_key_down(rv_keycode_t keycode)
     10 {
     11     return rv_keyboard_context.current_state[keycode];
     12 }
     13 
     14 RV_GLOBAL bool32 rv_key_released(rv_keycode_t keycode)
     15 {
     16     return !rv_keyboard_context.current_state[keycode] && rv_keyboard_context.last_state[keycode];
     17 }
     18 
     19 RV_GLOBAL bool32 rv_key_changed(rv_keycode_t keycode)
     20 {
     21     return rv_keyboard_context.current_state[keycode] != rv_keyboard_context.last_state[keycode];
     22 }
     23 
     24 RV_GLOBAL bool32 rv_key_mod(u8 mask)
     25 {
     26     return (rv_keyboard_context.mods & mask) != 0;
     27 }
     28 
     29 RV_GLOBAL bool32 rv_mouse_pressed(u32 mask)
     30 {
     31     for (u32 i = 0; i < 32; i++) {
     32         if (((rv_keyboard_context.current_mouse_state >> i) & 1) == ((mask >> i) & 1)) {
     33             if (((rv_keyboard_context.last_mouse_state >> i) & 1) != ((mask >> i) & 1)) {
     34                 return true;
     35             }
     36         }
     37     }
     38     return false;
     39 }
     40 
     41 RV_GLOBAL bool32 rv_mouse_down(u32 mask)
     42 {
     43     return (rv_keyboard_context.current_mouse_state & mask) != 0;
     44 }
     45 
     46 RV_GLOBAL bool32 rv_mouse_released(u32 mask)
     47 {
     48     for (u32 i = 0; i < 32; i++) {
     49         if (((rv_keyboard_context.current_mouse_state >> i) & 1) != ((mask >> i) & 1)) {
     50             if (((rv_keyboard_context.last_mouse_state >> i) & 1) == ((mask >> i) & 1)) {
     51                 return true;
     52             }
     53         }
     54     }
     55     return false;
     56 }
     57 
     58 RV_GLOBAL bool32 rv_mouse_changed(u32 mask)
     59 {
     60     return ((rv_keyboard_context.current_mouse_state & mask) ^ (rv_keyboard_context.last_mouse_state & mask)) != 0;
     61 }
     62 
     63 RV_GLOBAL void rv_keyboard_context_update(rv_keyboard_context_t* kbd_ctx, rv_event_t* events)
     64 {
     65     kbd_ctx->last_mouse_state = kbd_ctx->current_mouse_state;
     66     rv_mem_copy(kbd_ctx->last_state, kbd_ctx->current_state, sizeof(kbd_ctx->current_state));
     67 
     68     for (rv_event_t* e = events; e; e = e->next) {
     69         if (e->key_press.keycode == RV_KEYCODE_INVALID) continue;
     70         switch(e->type) {
     71         case RV_EVENT_KEY_PRESS: {
     72                 kbd_ctx->current_state[e->key_press.keycode] = true;
     73                 kbd_ctx->mods = e->key_press.mods;
     74                 kbd_ctx->group = e->key_press.group;
     75         } break;
     76         case RV_EVENT_KEY_RELEASE: {
     77             kbd_ctx->current_state[e->key_release.keycode] = false;
     78             kbd_ctx->group = e->key_press.group;
     79         } break;
     80         case RV_EVENT_BUTTON_PRESS: {
     81             kbd_ctx->current_mouse_state |= 1 << e->button_press.button;
     82         } break;
     83         case RV_EVENT_BUTTON_RELEASE: {
     84             kbd_ctx->current_mouse_state &= ~(1 << e->button_release.button);
     85         } break;
     86         default: break;
     87         }
     88     }
     89 }