Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 6.6 KB
Line 
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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COMMAND_NODE
17
18#include "command_node.h"
19#include "keynames.h"
20#include "ini_parser.h"
21#include "world_entity.h"
22#include "game_loader.h"
23#include "world.h"
24#include "list.h"
25#include "orxonox.h"
26
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30
31using namespace std;
32
33/**
34   \brief constructs a CommandNode to handle remote input
35   \param ID: unique denumerator to identify the node in the network
36*/
37CommandNode::CommandNode (int ID)
38{
39  this->bound = new tList<WorldEntity>();
40  this->aliases = NULL;
41  this->netID = ID;
42  this->bLocalInput = false;
43  this->bEnabled = true;
44  this->world = NULL;
45}
46
47/**
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
50*/
51CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
52{
53  this->aliases = NULL;
54  this->bLocalInput = true;
55  this->netID = 0;
56  this->bound = new tList<WorldEntity>();
57  this->bEnabled = true;
58  this->world = NULL;
59  this->loadBindings(filename);
60}
61
62/**
63   \brief removes the CommandNode from memory
64*/
65CommandNode::~CommandNode ()
66{
67  if( aliases != NULL) free (aliases);
68  if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */
69}
70
71
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
81void CommandNode::reset()
82{
83  this->bound->destroy();
84  //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */
85  this->bEnabled = false;
86  this->world = NULL;
87}
88
89void CommandNode::enable(bool bEnabled)
90{
91  this->bEnabled = bEnabled;
92}
93
94
95/**
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/**
110   \brief loads new key bindings from a file
111   \param filename: The path and name of the file to load the bindings from
112*/
113void CommandNode::loadBindings (char* filename = DEFAULT_KEYBIND_FILE)
114{
115  FILE* stream;
116 
117  PRINTF(4)("Loading key bindings from %s\n", filename);
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);
128  if( parser.getSection ("player1") == -1)
129    {
130      PRINTF(1)("Could not find key bindings in %s\n", filename);
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 
142  while( parser.nextVar (namebuf, valuebuf) != -1)
143    {
144      index = nameToIndex (valuebuf);
145      switch( index[0])
146        {
147        case 0:
148          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
149          strcpy (aliases->keys[index[1]], namebuf);
150          break;
151        case 1:
152          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
153          strcpy (aliases->buttons[index[1]], namebuf);
154          break;
155        default:
156          break;
157        }
158      memset (namebuf, 0, 256);
159      memset (valuebuf, 0, 256);
160    }
161}
162
163/**
164   \brief binds a WorldEntity to the CommandNode
165   \param entity: Pointer to the entity to bind
166*/
167void CommandNode::bind (WorldEntity* entity)
168{
169  bound->add (entity);
170}
171
172/**
173   \brief removes an entity from the list of the CommandNode
174   \param entity: Pointer to the entity to relese
175*/
176void CommandNode::unbind (WorldEntity* entity)
177{
178  bound->remove (entity);
179}
180
181int* CommandNode::nameToIndex (char* name)
182{
183  coord[0] = -1;
184  coord[1] = -1;
185  int c;
186  if( (c = keynameToSDLK (name)) != -1)
187    {
188      coord[1] = c;
189      coord[0] = 0;
190    }
191  if( (c = buttonnameToSDLB (name)) != -1)
192    {
193      coord[1] = c;
194      coord[0] = 1;
195    }
196  return coord;
197}
198
199/**
200   \brief tells the CommandNode to run through all pending events and relay them accordingly
201*/
202void CommandNode::process ()
203{
204  if( this->bEnabled) 
205    {
206      if( bLocalInput) processLocal ();
207      else processNetwork ();
208    }
209}
210
211void CommandNode::processLocal ()
212{
213  SDL_Event event;
214  Command cmd;
215  while( SDL_PollEvent (&event))
216    {
217      PRINTF(3)("CommandNode::processLocal() =========================got Event\n");
218      memset (cmd.cmd, 0, CMD_LENGHT); 
219      switch( event.type)
220        {
221        case SDL_KEYDOWN:
222          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
223          cmd.bUp = false;
224          if( strlen (cmd.cmd) > 0) relay(&cmd);
225          break;
226        case SDL_KEYUP:
227          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
228          cmd.bUp = true;
229          if( strlen (cmd.cmd) > 0) relay(&cmd);
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;
241          if( strlen (cmd.cmd) > 0) relay(&cmd);
242          break;
243        case SDL_MOUSEBUTTONDOWN:
244          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
245          cmd.bUp = false;
246          if( strlen (cmd.cmd) > 0) relay(&cmd);
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();
256          orx->eventHandler(&event);
257          break;
258        }
259    }
260}
261
262
263void CommandNode::processNetwork ()
264{
265
266}
267
268
269void CommandNode::relay (Command* cmd)
270{
271  Orxonox *orx = Orxonox::getInstance();
272  if( orx->systemCommand (cmd)) return;
273
274  GameLoader* gl = GameLoader::getInstance();
275  if( gl->worldCommand(cmd)) return;
276
277  if( bLocalInput) sendOverNetwork (cmd);
278 
279  if( this->world->command(cmd)) return;
280
281  tIterator<WorldEntity>* iterator = bound->getIterator();
282  WorldEntity* entity = iterator->nextElement();
283  while( entity != NULL)
284    {
285      entity->command (cmd); /*no absorbtion of command! strange*/
286      entity = iterator->nextElement();
287    }
288  delete iterator;
289}
290
291
292/**
293   \brief sets the network identifier of the CommandNode
294   \param ID: the new ID to use
295*/
296void CommandNode::setNetID (int ID)
297{
298  netID = ID;
299}
300
301void CommandNode::sendOverNetwork (Command* cmd)
302{
303}
Note: See TracBrowser for help on using the repository browser.