Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2707 in orxonox.OLD for orxonox/branches/buerli/src/command_node.cc


Ignore:
Timestamp:
Nov 3, 2004, 12:29:03 AM (21 years ago)
Author:
bensch
Message:

orxonox/branches/buerli: merged back from trunk, with new configure makefile and so forth.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/buerli/src/command_node.cc

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