Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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