Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/command_node.cc @ 4087

Last change on this file since 4087 was 4085, checked in by bensch, 20 years ago

orxonox/trunk: key-definition, and the keys get read as they should, eg. name == Usage, value == KeyName

File size: 6.6 KB
RevLine 
[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
[2816]13   co-programmer: Patrick Boenzli
[2066]14*/
15
[3591]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COMMAND_NODE
[2066]17
18#include "command_node.h"
19#include "keynames.h"
20#include "ini_parser.h"
[2100]21#include "world_entity.h"
[2636]22#include "game_loader.h"
[3216]23#include "world.h"
[3608]24#include "list.h"
25#include "orxonox.h"
[2066]26
27#include <stdio.h>
[2166]28#include <string.h>
[2105]29#include <stdlib.h>
[2066]30
31using namespace std;
32
[2141]33/**
[2636]34   \brief constructs a CommandNode to handle remote input
35   \param ID: unique denumerator to identify the node in the network
[2141]36*/
[2066]37CommandNode::CommandNode (int ID)
38{
[3214]39  this->bound = new tList<WorldEntity>();
[3213]40  this->aliases = NULL;
41  this->netID = ID;
42  this->bLocalInput = false;
43  this->bEnabled = true;
[3216]44  this->world = NULL;
[2066]45}
46
[2141]47/**
[2636]48   \brief constructs a CommandNode to handle local input
49   \param filename: The path and name of the file to load the key bindings from
[2141]50*/
[2066]51CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
52{
[3213]53  this->aliases = NULL;
54  this->bLocalInput = true;
55  this->netID = 0;
[3214]56  this->bound = new tList<WorldEntity>();
[3213]57  this->bEnabled = true;
[3216]58  this->world = NULL;
[4084]59  this->loadBindings(filename);
[2066]60}
61
[2141]62/**
[2636]63   \brief removes the CommandNode from memory
[2141]64*/
[2066]65CommandNode::~CommandNode ()
66{
[2636]67  if( aliases != NULL) free (aliases);
[3213]68  if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */
[2066]69}
70
[2816]71
[3194]72/**
73  \brief this resets the command node
74
75   deleting all data contained in the command node to fill it up again
76
77  \todo coppling to different game-entities
78  \todo reset/destroy has to be redesigned
79*/
80
[2816]81void CommandNode::reset()
82{
[3194]83  this->bound->destroy();
[3213]84  //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */
85  this->bEnabled = false;
[3216]86  this->world = NULL;
[2816]87}
88
[3213]89void CommandNode::enable(bool bEnabled)
90{
91  this->bEnabled = bEnabled;
92}
93
[3216]94
[2141]95/**
[3216]96  \brief adds Node to a GameWorld
97
98   this is usefull, if you want to catch events in a world class. usualy
99   this is done automaticaly via GameLoader. Reset it via
100   CommandNode::reset()
101
102*/
103void CommandNode::addToWorld(World* world)
104{
105  this->world = world;
106}
107
108
109/**
[2636]110   \brief loads new key bindings from a file
111   \param filename: The path and name of the file to load the bindings from
[2141]112*/
[4084]113void CommandNode::loadBindings (char* filename = DEFAULT_KEYBIND_FILE)
[2066]114{
[2636]115  FILE* stream;
116 
[3591]117  PRINTF(4)("Loading key bindings from %s\n", filename);
[2636]118 
119  // remove old bindings if present
120  if( aliases != NULL)
121    {
122      free (aliases);
123      aliases = NULL;
124    }
125 
126  // create parser
127  IniParser parser (filename);
[4084]128  if( parser.getSection ("player1") == -1)
[2636]129    {
[3591]130      PRINTF(1)("Could not find key bindings in %s\n", filename);
[2636]131      return;
132    }
133  // allocate empty lookup table
134  aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
135 
136  char namebuf[256];
137  char valuebuf[256];
138  memset (namebuf, 0, 256);
139  memset (valuebuf, 0, 256);
140  int* index;
141 
[3236]142  while( parser.nextVar (namebuf, valuebuf) != -1)
[2636]143    {
[4085]144      index = nameToIndex (valuebuf);
[2636]145      switch( index[0])
[2066]146        {
[2636]147        case 0:
[4085]148          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
149          strcpy (aliases->keys[index[1]], namebuf);
[2636]150          break;
151        case 1:
[4085]152          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
153          strcpy (aliases->buttons[index[1]], namebuf);
[2636]154          break;
155        default:
156          break;
[2066]157        }
[2636]158      memset (namebuf, 0, 256);
159      memset (valuebuf, 0, 256);
160    }
[2066]161}
162
[2141]163/**
[2636]164   \brief binds a WorldEntity to the CommandNode
165   \param entity: Pointer to the entity to bind
[2141]166*/
[2066]167void CommandNode::bind (WorldEntity* entity)
168{
[3225]169  bound->add (entity);
[2066]170}
171
[2141]172/**
[2636]173   \brief removes an entity from the list of the CommandNode
174   \param entity: Pointer to the entity to relese
[2141]175*/
[2066]176void CommandNode::unbind (WorldEntity* entity)
177{
[3225]178  bound->remove (entity);
[2066]179}
180
[3225]181int* CommandNode::nameToIndex (char* name)
[2066]182{
[2636]183  coord[0] = -1;
184  coord[1] = -1;
185  int c;
[3225]186  if( (c = keynameToSDLK (name)) != -1)
[2636]187    {
188      coord[1] = c;
189      coord[0] = 0;
190    }
[3225]191  if( (c = buttonnameToSDLB (name)) != -1)
[2636]192    {
193      coord[1] = c;
194      coord[0] = 1;
195    }
196  return coord;
[2066]197}
198
[2141]199/**
[2636]200   \brief tells the CommandNode to run through all pending events and relay them accordingly
[2141]201*/
[2066]202void CommandNode::process ()
203{
[3213]204  if( this->bEnabled) 
205    {
[3225]206      if( bLocalInput) processLocal ();
207      else processNetwork ();
[3213]208    }
[2066]209}
210
[3236]211void CommandNode::processLocal ()
[2066]212{
[2636]213  SDL_Event event;
214  Command cmd;
215  while( SDL_PollEvent (&event))
216    {
[3591]217      PRINTF(3)("CommandNode::processLocal() =========================got Event\n");
[2636]218      memset (cmd.cmd, 0, CMD_LENGHT); 
219      switch( event.type)
[2066]220        {
[2636]221        case SDL_KEYDOWN:
222          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
223          cmd.bUp = false;
[3225]224          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]225          break;
226        case SDL_KEYUP:
227          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
228          cmd.bUp = true;
[3225]229          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]230          break;
231        case SDL_MOUSEMOTION:
232          strcpy( cmd.cmd, "cursor");
233          cmd.x = event.motion.x;
234          cmd.y = event.motion.y;
235          cmd.xrel = event.motion.xrel;
236          cmd.yrel = event.motion.yrel;
237          break;
238        case SDL_MOUSEBUTTONUP:
239          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
240          cmd.bUp = true;
[3225]241          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]242          break;
243        case SDL_MOUSEBUTTONDOWN:
244          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
245          cmd.bUp = false;
[3225]246          if( strlen (cmd.cmd) > 0) relay(&cmd);
[2636]247          break;
248        case SDL_JOYAXISMOTION:
249        case SDL_JOYBALLMOTION:
250        case SDL_JOYHATMOTION:
251        case SDL_JOYBUTTONDOWN:
252        case SDL_JOYBUTTONUP:
253          break;
254        default:
255          Orxonox *orx = Orxonox::getInstance();
[3225]256          orx->eventHandler(&event);
[2636]257          break;
[2066]258        }
[2636]259    }
[2066]260}
261
[2816]262
[3225]263void CommandNode::processNetwork ()
[2066]264{
265
266}
267
[2816]268
[2066]269void CommandNode::relay (Command* cmd)
270{
[2636]271  Orxonox *orx = Orxonox::getInstance();
[3225]272  if( orx->systemCommand (cmd)) return;
[3216]273
[2636]274  GameLoader* gl = GameLoader::getInstance();
[3216]275  if( gl->worldCommand(cmd)) return;
276
[3225]277  if( bLocalInput) sendOverNetwork (cmd);
[2636]278 
[3216]279  if( this->world->command(cmd)) return;
280
[3654]281  tIterator<WorldEntity>* iterator = bound->getIterator();
282  WorldEntity* entity = iterator->nextElement();
[3216]283  while( entity != NULL)
[2636]284    {
[3586]285      entity->command (cmd); /*no absorbtion of command! strange*/
[3654]286      entity = iterator->nextElement();
[2636]287    }
[3654]288  delete iterator;
[2066]289}
[2096]290
[2816]291
[2141]292/**
[2636]293   \brief sets the network identifier of the CommandNode
294   \param ID: the new ID to use
[2141]295*/
[3225]296void CommandNode::setNetID (int ID)
[2096]297{
[2636]298  netID = ID;
[2096]299}
300
[3225]301void CommandNode::sendOverNetwork (Command* cmd)
[2096]302{
303}
Note: See TracBrowser for help on using the repository browser.