Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/images/src/command_node.cc @ 3097

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

orxonox/trunk/src: new list implemented

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          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;
218        }
219    }
220}
221
222
223void CommandNode::process_network ()
224{
225
226}
227
228
229void CommandNode::relay (Command* cmd)
230{
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 
239  WorldEntity* entity = bound->enumerate();
240  while(  entity != NULL)
241    {
242      entity->command (cmd);
243      entity = bound->nextElement();
244    }
245}
246
247
248/**
249   \brief sets the network identifier of the CommandNode
250   \param ID: the new ID to use
251*/
252void CommandNode::set_netID (int ID)
253{
254  netID = ID;
255}
256
257void CommandNode::send_over_network (Command* cmd)
258{
259}
Note: See TracBrowser for help on using the repository browser.