Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/dave/src/command_node.cc @ 3155

Last change on this file since 3155 was 3155, checked in by dave, 19 years ago

Bin dran die Bewegung smoother zu machen-first try

File size: 5.4 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
64void CommandNode::reset()
65{
66  this->bound->clear();
67}
68
69/**
70   \brief loads new key bindings from a file
71   \param filename: The path and name of the file to load the bindings from
72*/
73void CommandNode::load_bindings (char* filename)
74{
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])
108        {
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;
119        }
120      memset (namebuf, 0, 256);
121      memset (valuebuf, 0, 256);
122    }
123}
124
125/**
126   \brief binds a WorldEntity to the CommandNode
127   \param entity: Pointer to the entity to bind
128*/
129void CommandNode::bind (WorldEntity* entity)
130{
131  bound->add (entity);
132}
133
134/**
135   \brief removes an entity from the list of the CommandNode
136   \param entity: Pointer to the entity to relese
137*/
138void CommandNode::unbind (WorldEntity* entity)
139{
140  bound->remove (entity);
141}
142
143int* CommandNode::name_to_index (char* name)
144{
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;
159}
160
161/**
162   \brief tells the CommandNode to run through all pending events and relay them accordingly
163*/
164void CommandNode::process ()
165{
166  if( bLocalInput) process_local ();
167  else process_network ();
168}
169
170void CommandNode::process_local ()
171{
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)
179        {
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          cmd.tottime=0.0f;
189         
190          if( strlen (cmd.cmd) > 0) relay (&cmd);
191          break;
192        case SDL_MOUSEMOTION:
193          strcpy( cmd.cmd, "cursor");
194          cmd.x = event.motion.x;
195          cmd.y = event.motion.y;
196          cmd.xrel = event.motion.xrel;
197          cmd.yrel = event.motion.yrel;
198          break;
199        case SDL_MOUSEBUTTONUP:
200          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
201          cmd.bUp = true;
202          if( strlen (cmd.cmd) > 0) relay (&cmd);
203          break;
204        case SDL_MOUSEBUTTONDOWN:
205          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
206          cmd.bUp = false;
207          if( strlen (cmd.cmd) > 0) relay (&cmd);
208          break;
209        case SDL_JOYAXISMOTION:
210        case SDL_JOYBALLMOTION:
211        case SDL_JOYHATMOTION:
212        case SDL_JOYBUTTONDOWN:
213        case SDL_JOYBUTTONUP:
214          break;
215        default:
216          Orxonox *orx = Orxonox::getInstance();
217          orx->event_handler (&event);
218         
219          break;
220        }
221    }
222}
223
224
225void CommandNode::process_network ()
226{
227
228}
229
230
231void CommandNode::relay (Command* cmd)
232{
233 
234  Orxonox *orx = Orxonox::getInstance();
235  if( orx->system_command (cmd)) return;
236  GameLoader* gl = GameLoader::getInstance();
237  if(gl->worldCommand(cmd)) return;
238 
239  if( bLocalInput) send_over_network (cmd);
240 
241  WorldEntity* entity = bound->enumerate();
242  while(  entity != NULL)
243    {
244      entity->command (cmd);
245      entity = bound->nextElement();
246    }
247}
248
249
250/**
251   \brief sets the network identifier of the CommandNode
252   \param ID: the new ID to use
253*/
254void CommandNode::set_netID (int ID)
255{
256  netID = ID;
257}
258
259void CommandNode::send_over_network (Command* cmd)
260{
261}
Note: See TracBrowser for help on using the repository browser.