Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/buerli/src/command_node.cc @ 2854

Last change on this file since 2854 was 2707, checked in by bensch, 20 years ago

orxonox/branches/buerli: merged back from trunk, with new configure makefile and so forth.

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