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 | cmd.NumPressed+=1; |
---|
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 | cmd.NumPressed-=1; |
---|
193 | |
---|
194 | |
---|
195 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
196 | break; |
---|
197 | case SDL_MOUSEMOTION: |
---|
198 | strcpy( cmd.cmd, "cursor"); |
---|
199 | cmd.x = event.motion.x; |
---|
200 | cmd.y = event.motion.y; |
---|
201 | cmd.xrel = event.motion.xrel; |
---|
202 | cmd.yrel = event.motion.yrel; |
---|
203 | break; |
---|
204 | case SDL_MOUSEBUTTONUP: |
---|
205 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
206 | cmd.bUp = true; |
---|
207 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
208 | break; |
---|
209 | case SDL_MOUSEBUTTONDOWN: |
---|
210 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
211 | cmd.bUp = false; |
---|
212 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
213 | break; |
---|
214 | case SDL_JOYAXISMOTION: |
---|
215 | case SDL_JOYBALLMOTION: |
---|
216 | case SDL_JOYHATMOTION: |
---|
217 | case SDL_JOYBUTTONDOWN: |
---|
218 | case SDL_JOYBUTTONUP: |
---|
219 | break; |
---|
220 | default: |
---|
221 | Orxonox *orx = Orxonox::getInstance(); |
---|
222 | orx->event_handler (&event); |
---|
223 | |
---|
224 | break; |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | void CommandNode::process_network () |
---|
231 | { |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | void CommandNode::relay (Command* cmd) |
---|
237 | { |
---|
238 | |
---|
239 | Orxonox *orx = Orxonox::getInstance(); |
---|
240 | if( orx->system_command (cmd)) return; |
---|
241 | GameLoader* gl = GameLoader::getInstance(); |
---|
242 | if(gl->worldCommand(cmd)) return; |
---|
243 | |
---|
244 | if( bLocalInput) send_over_network (cmd); |
---|
245 | |
---|
246 | WorldEntity* entity = bound->enumerate(); |
---|
247 | while( entity != NULL) |
---|
248 | { |
---|
249 | entity->command (cmd); |
---|
250 | entity = bound->nextElement(); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | /** |
---|
256 | \brief sets the network identifier of the CommandNode |
---|
257 | \param ID: the new ID to use |
---|
258 | */ |
---|
259 | void CommandNode::set_netID (int ID) |
---|
260 | { |
---|
261 | netID = ID; |
---|
262 | } |
---|
263 | |
---|
264 | void CommandNode::send_over_network (Command* cmd) |
---|
265 | { |
---|
266 | } |
---|