~ubuntu-branches/ubuntu/maverick/stormbaancoureur/maverick

« back to all changes in this revision

Viewing changes to src-stormbaancoureur/controllerkey.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2007-08-22 07:53:07 UTC
  • Revision ID: james.westby@ubuntu.com-20070822075307-60hr9y5jyeeob6as
Tags: upstream-1.5.2
ImportĀ upstreamĀ versionĀ 1.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * stormbaancoureur
 
3
 * (c) 2006 by Bram Stolk
 
4
 * bram at gmail.com
 
5
 * LICENSED ACCORDING TO THE GPL
 
6
 */
 
7
 
 
8
#include <assert.h>
 
9
#include <math.h>
 
10
#include <errno.h>
 
11
#include <fcntl.h>
 
12
 
 
13
#include "controller.h"
 
14
 
 
15
 
 
16
ControllerKey::ControllerKey(const std::string &map_fname) :
 
17
Controller(),
 
18
key_steerl(false),
 
19
key_steerr(false),
 
20
key_accel(false),
 
21
key_brake(false),
 
22
key_action(false),
 
23
key_ebrake(false)
 
24
{
 
25
  name = "keyboard controller";
 
26
  axes[0]=axes[1]=axes[2]=0.0;
 
27
 
 
28
  FILE *f = fopen(map_fname.c_str(), "r");
 
29
 
 
30
  if (f)
 
31
  {
 
32
    char line[256], *s;
 
33
    do
 
34
    {
 
35
      s = fgets(line, sizeof(line), f);
 
36
      if (s && s[0]!='#')
 
37
      {
 
38
        int ascii;
 
39
        char func[128];
 
40
        int rv=sscanf(line, "%d %s", &ascii, func);
 
41
        if (rv==2)
 
42
          mapping[ascii] = func;
 
43
      }
 
44
    } while(s);
 
45
    fprintf(stderr, "Custom key mapping in %s contains %d entries\n", map_fname.c_str(), (int) mapping.size());
 
46
    assert(mapping.size()>=6);
 
47
  }
 
48
  else
 
49
  {
 
50
    fprintf(stderr, "Cannot open %s\n", map_fname.c_str());
 
51
    mapping['a'] = "ACCEL";
 
52
    mapping['z'] = "BRAKE";
 
53
    mapping['y'] = "BRAKE";
 
54
    mapping[','] = "LEFT";
 
55
    mapping['.'] = "RIGHT";
 
56
    mapping[' '] = "ACTION";
 
57
    mapping['\t']= "EBRAKE";
 
58
    mapping['b'] = "EBRAKE";
 
59
  }
 
60
}
 
61
 
 
62
 
 
63
ControllerKey::~ControllerKey()
 
64
{
 
65
}
 
66
 
 
67
 
 
68
void ControllerKey::Sustain(float dt)
 
69
{
 
70
  axes[0] = 0.0;
 
71
  if (key_steerl) axes[0] -= 0.7;
 
72
  if (key_steerr) axes[0] += 0.7;
 
73
  axes[1] = (key_accel)?0.9:0.0;
 
74
  axes[2] = (key_brake)?0.9:0.0;
 
75
}
 
76
 
 
77
 
 
78
float ControllerKey::GetAxisValue(int nr) const
 
79
{
 
80
  assert(nr<3);
 
81
  return axes[nr];
 
82
}
 
83
 
 
84
 
 
85
bool ControllerKey::GetButtonValue(int nr) const
 
86
{
 
87
  if (nr == 0)
 
88
    return key_action;
 
89
  if (nr == 1)
 
90
    return key_ebrake;
 
91
  return false;
 
92
}
 
93
 
 
94
 
 
95
void ControllerKey::FeedKey(unsigned char k, bool down)
 
96
{
 
97
  std::map<unsigned char, std::string>::const_iterator ip = mapping.find(k);
 
98
  if (ip != mapping.end())
 
99
    FeedAction((*ip).second, down);
 
100
}
 
101
 
 
102
 
 
103
void ControllerKey::FeedAction(const std::string &action, bool down)
 
104
{
 
105
  if (action=="ACCEL") { key_accel  = down; return; }
 
106
  if (action=="BRAKE") { key_brake  = down; return; }
 
107
  if (action=="LEFT")  { key_steerl = down; return; }
 
108
  if (action=="RIGHT") { key_steerr = down; return; }
 
109
  if (action=="ACTION"){ key_action = down; return; }
 
110
  if (action=="EBRAKE"){ key_ebrake = down; return; }
 
111
}
 
112