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