Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3104 was 2816, checked in by patrick, 20 years ago

orxonox/trunk/src: new list implemented

File size: 5.4 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
64void CommandNode::reset()
65{
66  this->bound->clear();
67}
68
[2141]69/**
[2636]70   \brief loads new key bindings from a file
71   \param filename: The path and name of the file to load the bindings from
[2141]72*/
[2066]73void CommandNode::load_bindings (char* filename)
74{
[2636]75  FILE* stream;
76 
77  printf("Loading key bindings from %s\n", filename);
78 
79  if( filename == NULL) filename = DEFAULT_KEYBIND_FILE;
80 
81  // remove old bindings if present
82  if( aliases != NULL)
83    {
84      free (aliases);
85      aliases = NULL;
86    }
87 
88  // create parser
89  IniParser parser (filename);
90  if( parser.get_section ("Bindings") == -1)
91    {
92      printf("Could not find key bindings in %s\n", filename);
93      return;
94    }
95  // allocate empty lookup table
96  aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
97 
98  char namebuf[256];
99  char valuebuf[256];
100  memset (namebuf, 0, 256);
101  memset (valuebuf, 0, 256);
102  int* index;
103 
104  while( parser.next_var (namebuf, valuebuf) != -1)
105    {
106      index = name_to_index (namebuf);
107      switch( index[0])
[2066]108        {
[2636]109        case 0:
110          printf("Key binding %d(%s) set to %s\n", index[1], SDLK_to_keyname( index[1]), valuebuf);
111          strcpy (aliases->keys[index[1]], valuebuf);
112          break;
113        case 1:
114          printf("Button binding %d(%s) set to %s\n", index[1], SDLB_to_buttonname( index[1]), valuebuf);
115          strcpy (aliases->buttons[index[1]], valuebuf);
116          break;
117        default:
118          break;
[2066]119        }
[2636]120      memset (namebuf, 0, 256);
121      memset (valuebuf, 0, 256);
122    }
[2066]123}
124
[2141]125/**
[2636]126   \brief binds a WorldEntity to the CommandNode
127   \param entity: Pointer to the entity to bind
[2141]128*/
[2066]129void CommandNode::bind (WorldEntity* entity)
130{
[2816]131  bound->add (entity);
[2066]132}
133
[2141]134/**
[2636]135   \brief removes an entity from the list of the CommandNode
136   \param entity: Pointer to the entity to relese
[2141]137*/
[2066]138void CommandNode::unbind (WorldEntity* entity)
139{
[2816]140  bound->remove (entity);
[2066]141}
142
143int* CommandNode::name_to_index (char* name)
144{
[2636]145  coord[0] = -1;
146  coord[1] = -1;
147  int c;
148  if( (c = keyname_to_SDLK (name)) != -1)
149    {
150      coord[1] = c;
151      coord[0] = 0;
152    }
153  if( (c = buttonname_to_SDLB (name)) != -1)
154    {
155      coord[1] = c;
156      coord[0] = 1;
157    }
158  return coord;
[2066]159}
160
[2141]161/**
[2636]162   \brief tells the CommandNode to run through all pending events and relay them accordingly
[2141]163*/
[2066]164void CommandNode::process ()
165{
[2636]166  if( bLocalInput) process_local ();
167  else process_network ();
[2066]168}
169
170void CommandNode::process_local ()
171{
[2636]172  SDL_Event event;
173  Command cmd;
174 
175  while( SDL_PollEvent (&event))
176    {
177      memset (cmd.cmd, 0, CMD_LENGHT); 
178      switch( event.type)
[2066]179        {
[2636]180        case SDL_KEYDOWN:
181          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
182          cmd.bUp = false;
183          if( strlen (cmd.cmd) > 0) relay (&cmd);
184          break;
185        case SDL_KEYUP:
186          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
187          cmd.bUp = true;
188          if( strlen (cmd.cmd) > 0) relay (&cmd);
189          break;
190        case SDL_MOUSEMOTION:
191          strcpy( cmd.cmd, "cursor");
192          cmd.x = event.motion.x;
193          cmd.y = event.motion.y;
194          cmd.xrel = event.motion.xrel;
195          cmd.yrel = event.motion.yrel;
196          break;
197        case SDL_MOUSEBUTTONUP:
198          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
199          cmd.bUp = true;
200          if( strlen (cmd.cmd) > 0) relay (&cmd);
201          break;
202        case SDL_MOUSEBUTTONDOWN:
203          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
204          cmd.bUp = false;
205          if( strlen (cmd.cmd) > 0) relay (&cmd);
206          break;
207        case SDL_JOYAXISMOTION:
208        case SDL_JOYBALLMOTION:
209        case SDL_JOYHATMOTION:
210        case SDL_JOYBUTTONDOWN:
211        case SDL_JOYBUTTONUP:
212          break;
213        default:
214          Orxonox *orx = Orxonox::getInstance();
215          orx->event_handler (&event);
216         
217          break;
[2066]218        }
[2636]219    }
[2066]220}
221
[2816]222
[2066]223void CommandNode::process_network ()
224{
225
226}
227
[2816]228
[2066]229void CommandNode::relay (Command* cmd)
230{
[2636]231 
232  Orxonox *orx = Orxonox::getInstance();
233  if( orx->system_command (cmd)) return;
234  GameLoader* gl = GameLoader::getInstance();
235  if(gl->worldCommand(cmd)) return;
236 
237  if( bLocalInput) send_over_network (cmd);
238 
[2816]239  WorldEntity* entity = bound->enumerate();
240  while(  entity != NULL)
[2636]241    {
[2816]242      entity->command (cmd);
243      entity = bound->nextElement();
[2636]244    }
[2066]245}
[2096]246
[2816]247
[2141]248/**
[2636]249   \brief sets the network identifier of the CommandNode
250   \param ID: the new ID to use
[2141]251*/
[2096]252void CommandNode::set_netID (int ID)
253{
[2636]254  netID = ID;
[2096]255}
256
257void CommandNode::send_over_network (Command* cmd)
258{
259}
Note: See TracBrowser for help on using the repository browser.