Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3593 was 3591, checked in by bensch, 20 years ago

orxonox/trunk: debug.h some cleanup

File size: 6.5 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
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
28
29using namespace std;
30
31/**
32   \brief constructs a CommandNode to handle remote input
33   \param ID: unique denumerator to identify the node in the network
34*/
35CommandNode::CommandNode (int ID)
36{
37  this->bound = new tList<WorldEntity>();
38  this->aliases = NULL;
39  this->netID = ID;
40  this->bLocalInput = false;
41  this->bEnabled = true;
42  this->world = NULL;
43}
44
45/**
46   \brief constructs a CommandNode to handle local input
47   \param filename: The path and name of the file to load the key bindings from
48*/
49CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
50{
51  this->aliases = NULL;
52  this->bLocalInput = true;
53  this->netID = 0;
54  this->bound = new tList<WorldEntity>();
55  this->bEnabled = true;
56  this->world = NULL;
57  this->loadBindings (filename);
58}
59
60/**
61   \brief removes the CommandNode from memory
62*/
63CommandNode::~CommandNode ()
64{
65  if( aliases != NULL) free (aliases);
66  if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */
67}
68
69
70/**
71  \brief this resets the command node
72
73   deleting all data contained in the command node to fill it up again
74
75  \todo coppling to different game-entities
76  \todo reset/destroy has to be redesigned
77*/
78
79void CommandNode::reset()
80{
81  this->bound->destroy();
82  //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */
83  this->bEnabled = false;
84  this->world = NULL;
85}
86
87void CommandNode::enable(bool bEnabled)
88{
89  this->bEnabled = bEnabled;
90}
91
92
93/**
94  \brief adds Node to a GameWorld
95
96   this is usefull, if you want to catch events in a world class. usualy
97   this is done automaticaly via GameLoader. Reset it via
98   CommandNode::reset()
99
100*/
101void CommandNode::addToWorld(World* world)
102{
103  this->world = world;
104}
105
106
107/**
108   \brief loads new key bindings from a file
109   \param filename: The path and name of the file to load the bindings from
110*/
111void CommandNode::loadBindings (char* filename)
112{
113  FILE* stream;
114 
115  PRINTF(4)("Loading key bindings from %s\n", filename);
116 
117  if( filename == NULL) filename = DEFAULT_KEYBIND_FILE;
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 ("Bindings") == -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 (namebuf);
145      switch( index[0])
146        {
147        case 0:
148          PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), valuebuf);
149          strcpy (aliases->keys[index[1]], valuebuf);
150          break;
151        case 1:
152          PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), valuebuf);
153          strcpy (aliases->buttons[index[1]], valuebuf);
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  WorldEntity* entity = bound->enumerate();
282  while( entity != NULL)
283    {
284      entity->command (cmd); /*no absorbtion of command! strange*/
285      entity = bound->nextElement();
286    }
287}
288
289
290/**
291   \brief sets the network identifier of the CommandNode
292   \param ID: the new ID to use
293*/
294void CommandNode::setNetID (int ID)
295{
296  netID = ID;
297}
298
299void CommandNode::sendOverNetwork (Command* cmd)
300{
301}
Note: See TracBrowser for help on using the repository browser.