Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/src/command_node.cc @ 2097

Last change on this file since 2097 was 2096, checked in by chris, 21 years ago

orxonox/branches/chris: Messed with the Player class, added stuff here and there, debug world now creates a player and bind IO to it. Added some doxygen tags.

File size: 3.8 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
21#include <stdio.h>
22#include <strings.h>
23
24using namespace std;
25
26
27CommandNode::CommandNode (int ID)
28{
29        bound = new List<WorldEntity>();
30        aliases = NULL;
31        netID = ID;
32        bLocalInput = false;
33}
34
35CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
36{
37        bLocalInput = true;
38        netID = 0;
39        bound = new List<WorldEntity>();
40        load_bindings (filename);
41}
42
43CommandNode::~CommandNode ()
44{
45        if( aliases != NULL) free (aliases);
46        if( bound != NULL) delete bound;
47}
48
49void CommandNode::load_bindings (char* filename)
50{
51        FILE* stream;
52       
53        if( filename == NULL) filename = DEFAULT_KEYBIND_FILE;
54       
55                // remove old bindings if present
56        if( aliases != NULL)
57        {
58                free (aliases);
59                aliases = NULL;
60        }
61                       
62                // create parser
63        IniParser parser (filename);
64        if( parser.get_section ("Bindings") == -1)
65        {
66                printf("Could not find key bindings in %s\n", filename);
67                return;
68        }
69                        // allocate empty lookup table
70        aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
71       
72        char namebuf[256];
73        char valuebuf[256];
74        int* index;
75
76        while( parser.next_var (namebuf, valuebuf) != -1)
77        {
78                index = name_to_index (namebuf);
79                switch( index[0])
80                {
81                        case 0:
82                                strcpy (aliases->keys[index[1]], valuebuf);
83                                break;
84                        case 1:
85                                strcpy (aliases->buttons[index[1]], valuebuf);
86                                break;
87                        default:
88                                break;
89                }
90        }
91}
92
93void CommandNode::bind (WorldEntity* entity)
94{
95        bound->add (entity, LIST_ADD_NEXT, true);
96}
97
98void CommandNode::unbind (WorldEntity* entity)
99{
100        bound->remove (entity, LIST_FIND_FW);
101}
102
103int* CommandNode::name_to_index (char* name)
104{
105        coord[0] = -1;
106        if( (coord[1] = keyname_to_SDLK (name)) != -1) coord[0] = 0;
107        if( (coord[1] = buttonname_to_SDLB (name)) != -1) coord[0] = 1;
108        return coord;
109}
110
111void CommandNode::process ()
112{
113        if( bLocalInput) process_local ();
114        else process_network ();
115}
116
117void CommandNode::process_local ()
118{
119        SDL_Event event;
120        Command cmd;
121       
122        while( SDL_PollEvent (&event))
123        {
124                switch( event.type)
125                {
126                        case SDL_KEYDOWN:
127                                strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
128                                cmd.bUp = false;
129                                if( strlen (cmd.cmd) > 0) relay (&cmd);
130                                break;
131                        case SDL_KEYUP:
132                                strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
133                                cmd.bUp = true;
134                                if( strlen (cmd.cmd) > 0) relay (&cmd);
135                                break;
136                        case SDL_MOUSEMOTION:
137                                strcpy( cmd.cmd, "cursor");
138                                cmd.x = event.motion.x;
139                                cmd.y = event.motion.y;
140                                cmd.xrel = event.motion.xrel;
141                                cmd.yrel = event.motion.yrel;
142                                break;
143                        case SDL_MOUSEBUTTONUP:
144                                strcpy( cmd.cmd, aliases->buttons[event.button.button]);
145                                cmd.bUp = true;
146                                if( strlen (cmd.cmd) > 0) relay (&cmd);
147                                break;
148                        case SDL_MOUSEBUTTONDOWN:
149                                strcpy( cmd.cmd, aliases->buttons[event.button.button]);
150                                cmd.bUp = false;
151                                if( strlen (cmd.cmd) > 0) relay (&cmd);
152                                break;
153                        case SDL_JOYAXISMOTION:
154                        case SDL_JOYBALLMOTION:
155                        case SDL_JOYHATMOTION:
156                        case SDL_JOYBUTTONDOWN:
157                        case SDL_JOYBUTTONUP:
158                                break;
159                        default:
160                                Orxonox *orx = Orxonox::getInstance();
161                                orx->event_handler (&event);
162                                break;
163                }
164        }
165}
166
167void CommandNode::process_network ()
168{
169
170}
171
172void CommandNode::relay (Command* cmd)
173{
174        List<WorldEntity>* plist = bound;
175       
176        if( bLocalInput) send_over_network (cmd);
177       
178        while( (plist = plist->get_next()) != NULL)
179        {
180                (plist->get_object())->command (cmd);
181        }
182}
183
184void CommandNode::set_netID (int ID)
185{
186}
187
188void CommandNode::send_over_network (Command* cmd)
189{
190}
Note: See TracBrowser for help on using the repository browser.