1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christian Meyer |
---|
13 | co-programmer: Patrick Boenzli |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "command_node.h" |
---|
18 | #include "keynames.h" |
---|
19 | #include "ini_parser.h" |
---|
20 | #include "world_entity.h" |
---|
21 | #include "game_loader.h" |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | #include <stdlib.h> |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | /** |
---|
30 | \brief constructs a CommandNode to handle remote input |
---|
31 | \param ID: unique denumerator to identify the node in the network |
---|
32 | */ |
---|
33 | CommandNode::CommandNode (int ID) |
---|
34 | { |
---|
35 | bound = new List(); |
---|
36 | aliases = NULL; |
---|
37 | netID = ID; |
---|
38 | bLocalInput = false; |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | \brief constructs a CommandNode to handle local input |
---|
44 | \param filename: The path and name of the file to load the key bindings from |
---|
45 | */ |
---|
46 | CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE) |
---|
47 | { |
---|
48 | aliases = NULL; |
---|
49 | bLocalInput = true; |
---|
50 | netID = 0; |
---|
51 | bound = new List(); |
---|
52 | load_bindings (filename); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | \brief removes the CommandNode from memory |
---|
57 | */ |
---|
58 | CommandNode::~CommandNode () |
---|
59 | { |
---|
60 | if( aliases != NULL) free (aliases); |
---|
61 | if( bound != NULL) delete bound; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | void CommandNode::reset() |
---|
66 | { |
---|
67 | this->bound->clear(); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | \brief loads new key bindings from a file |
---|
72 | \param filename: The path and name of the file to load the bindings from |
---|
73 | */ |
---|
74 | void CommandNode::load_bindings (char* filename) |
---|
75 | { |
---|
76 | FILE* stream; |
---|
77 | |
---|
78 | printf("Loading key bindings from %s\n", filename); |
---|
79 | |
---|
80 | if( filename == NULL) filename = DEFAULT_KEYBIND_FILE; |
---|
81 | |
---|
82 | // remove old bindings if present |
---|
83 | if( aliases != NULL) |
---|
84 | { |
---|
85 | free (aliases); |
---|
86 | aliases = NULL; |
---|
87 | } |
---|
88 | |
---|
89 | // create parser |
---|
90 | IniParser parser (filename); |
---|
91 | if( parser.get_section ("Bindings") == -1) |
---|
92 | { |
---|
93 | printf("Could not find key bindings in %s\n", filename); |
---|
94 | return; |
---|
95 | } |
---|
96 | // allocate empty lookup table |
---|
97 | aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
98 | |
---|
99 | char namebuf[256]; |
---|
100 | char valuebuf[256]; |
---|
101 | memset (namebuf, 0, 256); |
---|
102 | memset (valuebuf, 0, 256); |
---|
103 | int* index; |
---|
104 | |
---|
105 | while( parser.next_var (namebuf, valuebuf) != -1) |
---|
106 | { |
---|
107 | index = name_to_index (namebuf); |
---|
108 | switch( index[0]) |
---|
109 | { |
---|
110 | case 0: |
---|
111 | printf("Key binding %d(%s) set to %s\n", index[1], SDLK_to_keyname( index[1]), valuebuf); |
---|
112 | strcpy (aliases->keys[index[1]], valuebuf); |
---|
113 | break; |
---|
114 | case 1: |
---|
115 | printf("Button binding %d(%s) set to %s\n", index[1], SDLB_to_buttonname( index[1]), valuebuf); |
---|
116 | strcpy (aliases->buttons[index[1]], valuebuf); |
---|
117 | break; |
---|
118 | default: |
---|
119 | break; |
---|
120 | } |
---|
121 | memset (namebuf, 0, 256); |
---|
122 | memset (valuebuf, 0, 256); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | \brief binds a WorldEntity to the CommandNode |
---|
128 | \param entity: Pointer to the entity to bind |
---|
129 | */ |
---|
130 | void CommandNode::bind (WorldEntity* entity) |
---|
131 | { |
---|
132 | bound->add (entity); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | \brief removes an entity from the list of the CommandNode |
---|
137 | \param entity: Pointer to the entity to relese |
---|
138 | */ |
---|
139 | void CommandNode::unbind (WorldEntity* entity) |
---|
140 | { |
---|
141 | bound->remove (entity); |
---|
142 | } |
---|
143 | |
---|
144 | int* CommandNode::name_to_index (char* name) |
---|
145 | { |
---|
146 | coord[0] = -1; |
---|
147 | coord[1] = -1; |
---|
148 | int c; |
---|
149 | if( (c = keyname_to_SDLK (name)) != -1) |
---|
150 | { |
---|
151 | coord[1] = c; |
---|
152 | coord[0] = 0; |
---|
153 | } |
---|
154 | if( (c = buttonname_to_SDLB (name)) != -1) |
---|
155 | { |
---|
156 | coord[1] = c; |
---|
157 | coord[0] = 1; |
---|
158 | } |
---|
159 | return coord; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | \brief tells the CommandNode to run through all pending events and relay them accordingly |
---|
164 | */ |
---|
165 | void CommandNode::process () |
---|
166 | { |
---|
167 | if( bLocalInput) process_local (); |
---|
168 | else process_network (); |
---|
169 | } |
---|
170 | |
---|
171 | void CommandNode::process_local () |
---|
172 | { |
---|
173 | SDL_Event event; |
---|
174 | Command cmd; |
---|
175 | |
---|
176 | |
---|
177 | while( SDL_PollEvent (&event)) |
---|
178 | { |
---|
179 | memset (cmd.cmd, 0, CMD_LENGHT); |
---|
180 | switch( event.type) |
---|
181 | { |
---|
182 | case SDL_KEYDOWN: |
---|
183 | strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
184 | cmd.bUp = false; |
---|
185 | |
---|
186 | |
---|
187 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
188 | break; |
---|
189 | case SDL_KEYUP: |
---|
190 | strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
191 | cmd.bUp = true; |
---|
192 | |
---|
193 | |
---|
194 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
195 | break; |
---|
196 | case SDL_MOUSEMOTION: |
---|
197 | strcpy( cmd.cmd, "cursor"); |
---|
198 | cmd.x = event.motion.x; |
---|
199 | cmd.y = event.motion.y; |
---|
200 | cmd.xrel = event.motion.xrel; |
---|
201 | cmd.yrel = event.motion.yrel; |
---|
202 | break; |
---|
203 | case SDL_MOUSEBUTTONUP: |
---|
204 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
205 | cmd.bUp = true; |
---|
206 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
207 | break; |
---|
208 | case SDL_MOUSEBUTTONDOWN: |
---|
209 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
210 | cmd.bUp = false; |
---|
211 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
212 | break; |
---|
213 | case SDL_JOYAXISMOTION: |
---|
214 | case SDL_JOYBALLMOTION: |
---|
215 | case SDL_JOYHATMOTION: |
---|
216 | case SDL_JOYBUTTONDOWN: |
---|
217 | case SDL_JOYBUTTONUP: |
---|
218 | break; |
---|
219 | default: |
---|
220 | Orxonox *orx = Orxonox::getInstance(); |
---|
221 | orx->event_handler (&event); |
---|
222 | |
---|
223 | break; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | void CommandNode::process_network () |
---|
230 | { |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | void CommandNode::relay (Command* cmd) |
---|
236 | { |
---|
237 | |
---|
238 | Orxonox *orx = Orxonox::getInstance(); |
---|
239 | if( orx->system_command (cmd)) return; |
---|
240 | GameLoader* gl = GameLoader::getInstance(); |
---|
241 | if(gl->worldCommand(cmd)) return; |
---|
242 | |
---|
243 | if( bLocalInput) send_over_network (cmd); |
---|
244 | |
---|
245 | WorldEntity* entity = bound->enumerate(); |
---|
246 | while( entity != NULL) |
---|
247 | { |
---|
248 | entity->command (cmd); |
---|
249 | entity = bound->nextElement(); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | /** |
---|
255 | \brief sets the network identifier of the CommandNode |
---|
256 | \param ID: the new ID to use |
---|
257 | */ |
---|
258 | void CommandNode::set_netID (int ID) |
---|
259 | { |
---|
260 | netID = ID; |
---|
261 | } |
---|
262 | |
---|
263 | void CommandNode::send_over_network (Command* cmd) |
---|
264 | { |
---|
265 | } |
---|