/src/game-event.c

https://bitbucket.org/ekolis/jackband · C · 153 lines · 94 code · 33 blank · 26 comment · 10 complexity · 6019eb53ca69ca5e57e0527c37e5b884 MD5 · raw file

  1. /*
  2. * File: ui-event.c
  3. * Purpose: Allows the registering of handlers to be told about ui "events",
  4. * and the game to signal these events to the UI.
  5. *
  6. * Copyright (c) 2007 Antony Sidwell
  7. *
  8. * This work is free software; you can redistribute it and/or modify it
  9. * under the terms of either:
  10. *
  11. * a) the GNU General Public License as published by the Free Software
  12. * Foundation, version 2, or
  13. *
  14. * b) the "Angband licence":
  15. * This software may be copied and distributed for educational, research,
  16. * and not for profit purposes provided that this copyright and statement
  17. * are included in all such copies. Other copyrights may also apply.
  18. */
  19. #include <assert.h>
  20. #include "z-virt.h"
  21. #include "game-event.h"
  22. struct event_handler_entry
  23. {
  24. struct event_handler_entry *next;
  25. game_event_handler *fn;
  26. void *user;
  27. };
  28. struct event_handler_entry *event_handlers[N_GAME_EVENTS];
  29. static void game_event_dispatch(game_event_type type, game_event_data *data)
  30. {
  31. struct event_handler_entry *this = event_handlers[type];
  32. /*
  33. * Send the word out to all interested event handlers.
  34. */
  35. while (this)
  36. {
  37. /* Call the handler with the relevant data */
  38. this->fn(type, data, this->user);
  39. this = this->next;
  40. }
  41. }
  42. void event_add_handler(game_event_type type, game_event_handler *fn, void *user)
  43. {
  44. struct event_handler_entry *new;
  45. assert(fn != NULL);
  46. /* Make a new entry */
  47. new = mem_alloc(sizeof *new);
  48. new->fn = fn;
  49. new->user = user;
  50. /* Add it to the head of the appropriate list */
  51. new->next = event_handlers[type];
  52. event_handlers[type] = new;
  53. }
  54. void event_remove_handler(game_event_type type, game_event_handler *fn, void *user)
  55. {
  56. struct event_handler_entry *prev = NULL;
  57. struct event_handler_entry *this = event_handlers[type];
  58. /* Look for the entry in the list */
  59. while (this)
  60. {
  61. /* Check if this is the entry we want to remove */
  62. if (this->fn == fn && this->user == user)
  63. {
  64. if (!prev)
  65. {
  66. event_handlers[type] = this->next;
  67. }
  68. else
  69. {
  70. prev->next = this->next;
  71. }
  72. mem_free(this);
  73. return;
  74. }
  75. prev = this;
  76. this = this->next;
  77. }
  78. }
  79. void event_add_handler_set(game_event_type *type, size_t n_types, game_event_handler *fn, void *user)
  80. {
  81. size_t i;
  82. for (i = 0; i < n_types; i++)
  83. event_add_handler(type[i], fn, user);
  84. }
  85. void event_remove_handler_set(game_event_type *type, size_t n_types, game_event_handler *fn, void *user)
  86. {
  87. size_t i;
  88. for (i = 0; i < n_types; i++)
  89. event_remove_handler(type[i], fn, user);
  90. }
  91. void event_signal(game_event_type type)
  92. {
  93. game_event_dispatch(type, NULL);
  94. }
  95. void event_signal_flag(game_event_type type, bool flag)
  96. {
  97. game_event_data data;
  98. data.flag = flag;
  99. game_event_dispatch(type, &data);
  100. }
  101. void event_signal_point(game_event_type type, int x, int y)
  102. {
  103. game_event_data data;
  104. data.point.x = x;
  105. data.point.y = y;
  106. game_event_dispatch(type, &data);
  107. }
  108. void event_signal_string(game_event_type type, const char *s)
  109. {
  110. game_event_data data;
  111. data.string = s;
  112. game_event_dispatch(type, &data);
  113. }
  114. void event_signal_birthpoints(int stats[6], int remaining)
  115. {
  116. game_event_data data;
  117. data.birthstats.stats = stats;
  118. data.birthstats.remaining = remaining;
  119. game_event_dispatch(EVENT_BIRTHPOINTS, &data);
  120. }