Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelloader/src/command_node.cc @ 4162

Last change on this file since 4162 was 3746, checked in by chris, 20 years ago

orxonox/branches/levelloader: Merged trunk into branch… still not working though…

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