Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/movie_player/src/command_node.cc @ 5029

Last change on this file since 5029 was 4217, checked in by bensch, 20 years ago

orxonox/branches/movie_player: merged the trunk back into the movie_player
merged with command:
svn merge -r 4014:HEAD ../trunk/ movie_player/
no conflicts

File size: 7.4 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 (CONFIG_SECTION_PLAYER "1") == -1)
129    {
130      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 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  // PARSE MISC SECTION
164  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
165    {
166      PRINTF(1)("Could not find key bindings in %s\n", filename);
167      return;
168    }
169
170  while( parser.nextVar (namebuf, valuebuf) != -1)
171    {
172      index = nameToIndex (valuebuf);
173      switch( index[0])
174        {
175        case 0:
176          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
177          strcpy (aliases->keys[index[1]], namebuf);
178          break;
179        case 1:
180          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
181          strcpy (aliases->buttons[index[1]], namebuf);
182          break;
183        default:
184          break;
185        }
186      memset (namebuf, 0, 256);
187      memset (valuebuf, 0, 256);
188    }
189
190}
191
192/**
193   \brief binds a WorldEntity to the CommandNode
194   \param entity: Pointer to the entity to bind
195*/
196void CommandNode::bind (WorldEntity* entity)
197{
198  bound->add (entity);
199}
200
201/**
202   \brief removes an entity from the list of the CommandNode
203   \param entity: Pointer to the entity to relese
204*/
205void CommandNode::unbind (WorldEntity* entity)
206{
207  bound->remove (entity);
208}
209
210int* CommandNode::nameToIndex (char* name)
211{
212  coord[0] = -1;
213  coord[1] = -1;
214  int c;
215  if( (c = keynameToSDLK (name)) != -1)
216    {
217      coord[1] = c;
218      coord[0] = 0;
219    }
220  if( (c = buttonnameToSDLB (name)) != -1)
221    {
222      coord[1] = c;
223      coord[0] = 1;
224    }
225  return coord;
226}
227
228/**
229   \brief tells the CommandNode to run through all pending events and relay them accordingly
230*/
231void CommandNode::process ()
232{
233  if( this->bEnabled) 
234    {
235      if( bLocalInput) processLocal ();
236      else processNetwork ();
237    }
238}
239
240void CommandNode::processLocal ()
241{
242  SDL_Event event;
243  Command cmd;
244  while( SDL_PollEvent (&event))
245    {
246      PRINTF(3)("CommandNode::processLocal() =========================got Event\n");
247      memset (cmd.cmd, 0, CMD_LENGHT); 
248      switch( event.type)
249        {
250        case SDL_KEYDOWN:
251          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
252          cmd.bUp = false;
253          if( strlen (cmd.cmd) > 0) relay(&cmd);
254          break;
255        case SDL_KEYUP:
256          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
257          cmd.bUp = true;
258          if( strlen (cmd.cmd) > 0) relay(&cmd);
259          break;
260        case SDL_MOUSEMOTION:
261          strcpy( cmd.cmd, "cursor");
262          cmd.x = event.motion.x;
263          cmd.y = event.motion.y;
264          cmd.xrel = event.motion.xrel;
265          cmd.yrel = event.motion.yrel;
266          break;
267        case SDL_MOUSEBUTTONUP:
268          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
269          cmd.bUp = true;
270          if( strlen (cmd.cmd) > 0) relay(&cmd);
271          break;
272        case SDL_MOUSEBUTTONDOWN:
273          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
274          cmd.bUp = false;
275          if( strlen (cmd.cmd) > 0) relay(&cmd);
276          break;
277        case SDL_JOYAXISMOTION:
278        case SDL_JOYBALLMOTION:
279        case SDL_JOYHATMOTION:
280        case SDL_JOYBUTTONDOWN:
281        case SDL_JOYBUTTONUP:
282          break;
283        default:
284          Orxonox *orx = Orxonox::getInstance();
285          orx->eventHandler(&event);
286          break;
287        }
288    }
289}
290
291
292void CommandNode::processNetwork ()
293{
294
295}
296
297
298void CommandNode::relay (Command* cmd)
299{
300  Orxonox *orx = Orxonox::getInstance();
301  if( orx->systemCommand (cmd)) return;
302
303  GameLoader* gl = GameLoader::getInstance();
304  if( gl->worldCommand(cmd)) return;
305
306  if( bLocalInput) sendOverNetwork (cmd);
307 
308  if( this->world->command(cmd)) return;
309
310  tIterator<WorldEntity>* iterator = bound->getIterator();
311  WorldEntity* entity = iterator->nextElement();
312  while( entity != NULL)
313    {
314      entity->command (cmd); /*no absorbtion of command! strange*/
315      entity = iterator->nextElement();
316    }
317  delete iterator;
318}
319
320
321/**
322   \brief sets the network identifier of the CommandNode
323   \param ID: the new ID to use
324*/
325void CommandNode::setNetID (int ID)
326{
327  netID = ID;
328}
329
330void CommandNode::sendOverNetwork (Command* cmd)
331{
332}
Note: See TracBrowser for help on using the repository browser.