Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3212 was 3194, checked in by patrick, 20 years ago

orxonox/trunk/src: state-freeze - redefinig destroy functions, some comments

File size: 5.6 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
16
17#include "command_node.h"
18#include "keynames.h"
19#include "ini_parser.h"
[2100]20#include "world_entity.h"
[2636]21#include "game_loader.h"
[2066]22
23#include <stdio.h>
[2166]24#include <string.h>
[2105]25#include <stdlib.h>
[2066]26
27using namespace std;
28
[2141]29/**
[2636]30   \brief constructs a CommandNode to handle remote input
31   \param ID: unique denumerator to identify the node in the network
[2141]32*/
[2066]33CommandNode::CommandNode (int ID)
34{
[2816]35  bound = new List();
[2636]36  aliases = NULL;
37  netID = ID;
38  bLocalInput = false;
[2066]39}
40
[2141]41/**
[2636]42   \brief constructs a CommandNode to handle local input
43   \param filename: The path and name of the file to load the key bindings from
[2141]44*/
[2066]45CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
46{
[2636]47  aliases = NULL;
48  bLocalInput = true;
49  netID = 0;
[2816]50  bound = new List();
[2636]51  load_bindings (filename);
[2066]52}
53
[2141]54/**
[2636]55   \brief removes the CommandNode from memory
[2141]56*/
[2066]57CommandNode::~CommandNode ()
58{
[2636]59  if( aliases != NULL) free (aliases);
60  if( bound != NULL) delete bound;
[2066]61}
62
[2816]63
[3194]64/**
65  \brief this resets the command node
66
67   deleting all data contained in the command node to fill it up again
68
69  \todo coppling to different game-entities
70  \todo reset/destroy has to be redesigned
71*/
72
[2816]73void CommandNode::reset()
74{
[3194]75  this->bound->destroy();
[2816]76}
77
[2141]78/**
[2636]79   \brief loads new key bindings from a file
80   \param filename: The path and name of the file to load the bindings from
[2141]81*/
[2066]82void CommandNode::load_bindings (char* filename)
83{
[2636]84  FILE* stream;
85 
86  printf("Loading key bindings from %s\n", filename);
87 
88  if( filename == NULL) filename = DEFAULT_KEYBIND_FILE;
89 
90  // remove old bindings if present
91  if( aliases != NULL)
92    {
93      free (aliases);
94      aliases = NULL;
95    }
96 
97  // create parser
98  IniParser parser (filename);
99  if( parser.get_section ("Bindings") == -1)
100    {
101      printf("Could not find key bindings in %s\n", filename);
102      return;
103    }
104  // allocate empty lookup table
105  aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
106 
107  char namebuf[256];
108  char valuebuf[256];
109  memset (namebuf, 0, 256);
110  memset (valuebuf, 0, 256);
111  int* index;
112 
113  while( parser.next_var (namebuf, valuebuf) != -1)
114    {
115      index = name_to_index (namebuf);
116      switch( index[0])
[2066]117        {
[2636]118        case 0:
119          printf("Key binding %d(%s) set to %s\n", index[1], SDLK_to_keyname( index[1]), valuebuf);
120          strcpy (aliases->keys[index[1]], valuebuf);
121          break;
122        case 1:
123          printf("Button binding %d(%s) set to %s\n", index[1], SDLB_to_buttonname( index[1]), valuebuf);
124          strcpy (aliases->buttons[index[1]], valuebuf);
125          break;
126        default:
127          break;
[2066]128        }
[2636]129      memset (namebuf, 0, 256);
130      memset (valuebuf, 0, 256);
131    }
[2066]132}
133
[2141]134/**
[2636]135   \brief binds a WorldEntity to the CommandNode
136   \param entity: Pointer to the entity to bind
[2141]137*/
[2066]138void CommandNode::bind (WorldEntity* entity)
139{
[2816]140  bound->add (entity);
[2066]141}
142
[2141]143/**
[2636]144   \brief removes an entity from the list of the CommandNode
145   \param entity: Pointer to the entity to relese
[2141]146*/
[2066]147void CommandNode::unbind (WorldEntity* entity)
148{
[2816]149  bound->remove (entity);
[2066]150}
151
152int* CommandNode::name_to_index (char* name)
153{
[2636]154  coord[0] = -1;
155  coord[1] = -1;
156  int c;
157  if( (c = keyname_to_SDLK (name)) != -1)
158    {
159      coord[1] = c;
160      coord[0] = 0;
161    }
162  if( (c = buttonname_to_SDLB (name)) != -1)
163    {
164      coord[1] = c;
165      coord[0] = 1;
166    }
167  return coord;
[2066]168}
169
[2141]170/**
[2636]171   \brief tells the CommandNode to run through all pending events and relay them accordingly
[2141]172*/
[2066]173void CommandNode::process ()
174{
[2636]175  if( bLocalInput) process_local ();
176  else process_network ();
[2066]177}
178
179void CommandNode::process_local ()
180{
[2636]181  SDL_Event event;
182  Command cmd;
183 
184  while( SDL_PollEvent (&event))
185    {
186      memset (cmd.cmd, 0, CMD_LENGHT); 
187      switch( event.type)
[2066]188        {
[2636]189        case SDL_KEYDOWN:
190          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
191          cmd.bUp = false;
192          if( strlen (cmd.cmd) > 0) relay (&cmd);
193          break;
194        case SDL_KEYUP:
195          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
196          cmd.bUp = true;
197          if( strlen (cmd.cmd) > 0) relay (&cmd);
198          break;
199        case SDL_MOUSEMOTION:
200          strcpy( cmd.cmd, "cursor");
201          cmd.x = event.motion.x;
202          cmd.y = event.motion.y;
203          cmd.xrel = event.motion.xrel;
204          cmd.yrel = event.motion.yrel;
205          break;
206        case SDL_MOUSEBUTTONUP:
207          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
208          cmd.bUp = true;
209          if( strlen (cmd.cmd) > 0) relay (&cmd);
210          break;
211        case SDL_MOUSEBUTTONDOWN:
212          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
213          cmd.bUp = false;
214          if( strlen (cmd.cmd) > 0) relay (&cmd);
215          break;
216        case SDL_JOYAXISMOTION:
217        case SDL_JOYBALLMOTION:
218        case SDL_JOYHATMOTION:
219        case SDL_JOYBUTTONDOWN:
220        case SDL_JOYBUTTONUP:
221          break;
222        default:
223          Orxonox *orx = Orxonox::getInstance();
224          orx->event_handler (&event);
225         
226          break;
[2066]227        }
[2636]228    }
[2066]229}
230
[2816]231
[2066]232void CommandNode::process_network ()
233{
234
235}
236
[2816]237
[2066]238void CommandNode::relay (Command* cmd)
239{
[2636]240 
241  Orxonox *orx = Orxonox::getInstance();
242  if( orx->system_command (cmd)) return;
243  GameLoader* gl = GameLoader::getInstance();
244  if(gl->worldCommand(cmd)) return;
245 
246  if( bLocalInput) send_over_network (cmd);
247 
[2816]248  WorldEntity* entity = bound->enumerate();
249  while(  entity != NULL)
[2636]250    {
[2816]251      entity->command (cmd);
252      entity = bound->nextElement();
[2636]253    }
[2066]254}
[2096]255
[2816]256
[2141]257/**
[2636]258   \brief sets the network identifier of the CommandNode
259   \param ID: the new ID to use
[2141]260*/
[2096]261void CommandNode::set_netID (int ID)
262{
[2636]263  netID = ID;
[2096]264}
265
266void CommandNode::send_over_network (Command* cmd)
267{
268}
Note: See TracBrowser for help on using the repository browser.