source: abuse/trunk/src/config.cpp @ 129

Last change on this file since 129 was 129, checked in by Sam Hocevar, 15 years ago
  • Get rid of jmalloc and replace it with standard malloc. Modern operating systems certainly perform a lot better than this custom implementation, and we have superior tools (eg. valgrind) to debug and profile memory usage without interfering with the code itself.
File size: 6.1 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <ctype.h>
13
14#include "sdlport/joy.hpp"
15#include "game.hpp"
16
17#include "keys.hpp"
18#include "lisp.hpp"
19#include "jwindow.hpp"
20#include "config.hpp"
21
22extern int get_key_binding(char const *dir, int i);
23
24int key_players = 0;
25int morph_detail = MEDIUM_DETAIL;
26
27struct player_keys
28{
29    int joy, left, right, up, down, b1, b2, b3, b4;
30};
31
32static player_keys *key_map = NULL;
33
34static int binding_for_player( int player )
35{
36    char tmp[40];
37    sprintf( tmp, "player%d", player );
38    Cell *f = find_symbol( tmp );
39    if( !NILP( f ) && DEFINEDP( symbol_value( f ) ) )
40    {
41        void *what = symbol_value( f );
42        if( what == make_find_symbol( "keyboard" ) )
43            return 1;
44        else if( what == make_find_symbol( "joystick" ) )
45            return 2;
46    }
47    return 0;
48}
49
50/*
51int get_key_binding(char const *dir, int i)
52{
53    char tmp[100], kn[50];
54    sprintf( tmp, "player%d-%s", i, dir );
55    Cell *f = find_symbol( tmp );
56    if( NILP(f) || !DEFINEDP( symbol_value( f ) ) )
57        return 0;
58    void *k = symbol_value( f );
59
60    if( item_type( k ) != L_SYMBOL )
61        return 0;
62
63#ifdef SCADALISP
64    strcpy( tmp, symbol_name( k ) );
65#else
66    strcpy( tmp, lstring_value( symbol_name( k ) ) );
67#endif
68
69    for( char *c = tmp; *c; c++ )
70    {
71        *c = tolower( *c );
72        if( *c == '_' )
73            *c = ' ';
74    }
75
76    for( int j = 0; j < JK_MAX_KEY; j++ )
77    {
78        key_name( j, kn );
79        for( char *c = kn; *c; c++ )
80        {
81            *c = tolower(*c);
82        }
83        if( !strcmp( kn, tmp ) )
84            return j;
85    }
86    return 0;
87}
88*/
89
90/*
91void get_key_bindings()
92{
93    if( key_map )
94    {
95        free( key_map );
96    }
97    key_map = NULL;
98
99    for( key_players = 0; binding_for_player( key_players + 1); key_players++ );
100    if( key_players )
101    {
102        key_map = ( player_keys *)malloc(sizeof(player_keys)*key_players);
103        for( int i = 0; i < key_players; i++ )
104        {
105            key_map[i].joy = ( binding_for_player( i + 1 ) == 2 );
106            if( !key_map[i].joy )
107            {
108                key_map[i].left = get_key_binding( "left", i + 1 );
109                key_map[i].right = get_key_binding( "right", i + 1 );
110                key_map[i].up = get_key_binding( "up", i + 1 );
111                key_map[i].down = get_key_binding( "down", i + 1 );
112                key_map[i].b4 = get_key_binding( "b4", i + 1 );
113                key_map[i].b3 = get_key_binding( "b3", i + 1 );
114                key_map[i].b2 = get_key_binding( "b2", i + 1 );
115                key_map[i].b1 = get_key_binding( "b1", i + 1 );
116            }
117        }
118    }
119    else
120    {
121        key_map = NULL;
122    }
123}*/
124
125// AK
126void get_key_bindings()
127{
128    if( key_map )
129    {
130        free( key_map );
131    }
132    key_map = NULL;
133
134    key_players = 1;
135    key_map = (player_keys *)malloc( sizeof( player_keys ) * key_players );
136    for( int i = 0; i < key_players; i++ )
137    {
138        key_map[i].joy = 0;
139        key_map[i].left = get_key_binding( "left", i + 1 );
140        key_map[i].right = get_key_binding( "right", i + 1 );
141        key_map[i].up = get_key_binding( "up", i + 1 );
142        key_map[i].down = get_key_binding( "down", i + 1 );
143        key_map[i].b4 = get_key_binding( "b4", i + 1 );
144        key_map[i].b3 = get_key_binding( "b3", i + 1 );
145        key_map[i].b2 = get_key_binding( "b2", i + 1 );
146        key_map[i].b1 = get_key_binding( "b1", i + 1 );
147    }
148}
149
150
151#define is_pressed(x) the_game->key_down(x)
152
153void get_movement(int player, int &x, int &y, int &b1, int &b2, int &b3, int &b4)
154{
155    if( player < key_players )
156    {
157/*        if( key_map[player].joy )
158        {
159            joy_status( b1,b2,b3,x,y );
160            b3 = ( b1 && b2 );
161            b4 = 0;
162        }
163        else if( !wm )
164        {
165            x = y = b1 = b2 = b3 = b4 = 0;
166        }
167        else*/
168        {
169            if( is_pressed( key_map[player].left ) )
170                x = -1;
171            else if( is_pressed( key_map[player].right ) )
172                x=1;
173            else
174                x = 0;
175
176            if( is_pressed( key_map[player].up ) )
177                y = -1;
178            else if( is_pressed( key_map[player].down ) )
179                y = 1;
180            else y = 0;
181
182            if( is_pressed( key_map[player].b1 ) )
183                b1 = 1;
184            else
185                b1 = 0;
186
187            if( is_pressed( key_map[player].b2 ) )
188                b2 = 1;
189            else
190                b2 = 0;
191
192            if( is_pressed( key_map[player].b3 ) )
193                b3 = 1;
194            else
195                b3 = 0;
196
197            if( is_pressed( key_map[player].b4 ) )
198                b4 = 1;
199            else
200                b4 = 0;
201        }
202    }
203    else
204    {
205        x = y = b1 = b2 = b3 = 0;
206    }
207}
208
209void key_bindings(int player, int &left, int &right, int &up, int &down, int &b1, int &b2, int &b3, int &b4)
210{
211    left = key_map[player].left;
212    right = key_map[player].right;
213    up = key_map[player].up;
214    down = key_map[player].down;
215    b1 = key_map[player].b1;
216    b2 = key_map[player].b2;
217    b3 = key_map[player].b3;
218    b3 = key_map[player].b4;
219}
220
221
222void config_cleanup()
223{
224    if(key_map)
225    {
226        free(key_map);
227        key_map = NULL;
228    }
229}
230
231//
232// Get the keycode for the string 'str'
233// Returns -1 for an invalid key code
234//
235int get_keycode(char const *str)
236{
237    if( !str[0] )
238    {
239        return -1;
240    }
241    else if( !str[1] )
242    {
243        return str[0];
244    }
245    else
246    {
247        int j;
248        char buf[20];
249        for( j = 256; j < JK_MAX_KEY; j++ )
250        {
251            key_name( j, buf );
252            char *c = buf;
253            for( ; *c; c++ )
254            {
255                if( *c == ' ' )
256                {
257                    *c = '_';
258                }
259                else
260                {
261                    *c = tolower( *c );
262                }
263            }
264            if( strcmp( str, buf ) == 0 )
265            {
266                return j;
267            }
268        }
269    }
270    return -1;
271}
Note: See TracBrowser for help on using the repository browser.