/src/game-event.c
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 20#include <assert.h> 21#include "z-virt.h" 22#include "game-event.h" 23 24struct event_handler_entry 25{ 26 struct event_handler_entry *next; 27 game_event_handler *fn; 28 void *user; 29}; 30 31struct event_handler_entry *event_handlers[N_GAME_EVENTS]; 32 33static void game_event_dispatch(game_event_type type, game_event_data *data) 34{ 35 struct event_handler_entry *this = event_handlers[type]; 36 37 /* 38 * Send the word out to all interested event handlers. 39 */ 40 while (this) 41 { 42 /* Call the handler with the relevant data */ 43 this->fn(type, data, this->user); 44 this = this->next; 45 } 46} 47 48void event_add_handler(game_event_type type, game_event_handler *fn, void *user) 49{ 50 struct event_handler_entry *new; 51 52 assert(fn != NULL); 53 54 /* Make a new entry */ 55 new = mem_alloc(sizeof *new); 56 new->fn = fn; 57 new->user = user; 58 59 /* Add it to the head of the appropriate list */ 60 new->next = event_handlers[type]; 61 event_handlers[type] = new; 62} 63 64void event_remove_handler(game_event_type type, game_event_handler *fn, void *user) 65{ 66 struct event_handler_entry *prev = NULL; 67 struct event_handler_entry *this = event_handlers[type]; 68 69 /* Look for the entry in the list */ 70 while (this) 71 { 72 /* Check if this is the entry we want to remove */ 73 if (this->fn == fn && this->user == user) 74 { 75 if (!prev) 76 { 77 event_handlers[type] = this->next; 78 } 79 else 80 { 81 prev->next = this->next; 82 } 83 84 mem_free(this); 85 return; 86 } 87 88 prev = this; 89 this = this->next; 90 } 91} 92 93void event_add_handler_set(game_event_type *type, size_t n_types, game_event_handler *fn, void *user) 94{ 95 size_t i; 96 97 for (i = 0; i < n_types; i++) 98 event_add_handler(type[i], fn, user); 99} 100 101void event_remove_handler_set(game_event_type *type, size_t n_types, game_event_handler *fn, void *user) 102{ 103 size_t i; 104 105 for (i = 0; i < n_types; i++) 106 event_remove_handler(type[i], fn, user); 107} 108 109 110 111 112void event_signal(game_event_type type) 113{ 114 game_event_dispatch(type, NULL); 115} 116 117void event_signal_flag(game_event_type type, bool flag) 118{ 119 game_event_data data; 120 data.flag = flag; 121 122 game_event_dispatch(type, &data); 123} 124 125 126void event_signal_point(game_event_type type, int x, int y) 127{ 128 game_event_data data; 129 data.point.x = x; 130 data.point.y = y; 131 132 game_event_dispatch(type, &data); 133} 134 135 136void event_signal_string(game_event_type type, const char *s) 137{ 138 game_event_data data; 139 data.string = s; 140 141 game_event_dispatch(type, &data); 142} 143 144void event_signal_birthpoints(int stats[6], int remaining) 145{ 146 game_event_data data; 147 148 data.birthstats.stats = stats; 149 data.birthstats.remaining = remaining; 150 151 game_event_dispatch(EVENT_BIRTHPOINTS, &data); 152} 153