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