Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3210 was 3194, checked in by patrick, 19 years ago

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

File size: 5.6 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
17#include "command_node.h"
18#include "keynames.h"
19#include "ini_parser.h"
20#include "world_entity.h"
21#include "game_loader.h"
22
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26
27using namespace std;
28
29/**
30   \brief constructs a CommandNode to handle remote input
31   \param ID: unique denumerator to identify the node in the network
32*/
33CommandNode::CommandNode (int ID)
34{
35  bound = new List();
36  aliases = NULL;
37  netID = ID;
38  bLocalInput = false;
39}
40
41/**
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
44*/
45CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
46{
47  aliases = NULL;
48  bLocalInput = true;
49  netID = 0;
50  bound = new List();
51  load_bindings (filename);
52}
53
54/**
55   \brief removes the CommandNode from memory
56*/
57CommandNode::~CommandNode ()
58{
59  if( aliases != NULL) free (aliases);
60  if( bound != NULL) delete bound;
61}
62
63
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
73void CommandNode::reset()
74{
75  this->bound->destroy();
76}
77
78/**
79   \brief loads new key bindings from a file
80   \param filename: The path and name of the file to load the bindings from
81*/
82void CommandNode::load_bindings (char* filename)
83{
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])
117        {
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;
128        }
129      memset (namebuf, 0, 256);
130      memset (valuebuf, 0, 256);
131    }
132}
133
134/**
135   \brief binds a WorldEntity to the CommandNode
136   \param entity: Pointer to the entity to bind
137*/
138void CommandNode::bind (WorldEntity* entity)
139{
140  bound->add (entity);
141}
142
143/**
144   \brief removes an entity from the list of the CommandNode
145   \param entity: Pointer to the entity to relese
146*/
147void CommandNode::unbind (WorldEntity* entity)
148{
149  bound->remove (entity);
150}
151
152int* CommandNode::name_to_index (char* name)
153{
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;
168}
169
170/**
171   \brief tells the CommandNode to run through all pending events and relay them accordingly
172*/
173void CommandNode::process ()
174{
175  if( bLocalInput) process_local ();
176  else process_network ();
177}
178
179void CommandNode::process_local ()
180{
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)
188        {
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;
227        }
228    }
229}
230
231
232void CommandNode::process_network ()
233{
234
235}
236
237
238void CommandNode::relay (Command* cmd)
239{
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 
248  WorldEntity* entity = bound->enumerate();
249  while(  entity != NULL)
250    {
251      entity->command (cmd);
252      entity = bound->nextElement();
253    }
254}
255
256
257/**
258   \brief sets the network identifier of the CommandNode
259   \param ID: the new ID to use
260*/
261void CommandNode::set_netID (int ID)
262{
263  netID = ID;
264}
265
266void CommandNode::send_over_network (Command* cmd)
267{
268}
Note: See TracBrowser for help on using the repository browser.