source: golgotha/src/golg/input.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.5 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "input.hh"
10#include "device/kernel.hh"
11#include "resources.hh"
12#include "human.hh"
13#include "g1_speed.hh"
14#include "error/alert.hh"
15#include "controller.hh"
16#include "device/key_man.hh"
17#include "statistics.hh"
18#include "app/app.hh"
19#include "window/win_evt.hh"
20#include "app/app.hh"
21#include "lisp/lisp.hh"
22
23
24enum
25{
26  G1_MAIN_MENU=g1_input_class::NUM_STANK_CONTROLS,
27  G1_TOGGLE_SHADOWS,
28  G1_TOGGLE_STATS,
29  G1_TOGGLE_TEXTURE_LOADING,
30  G1_SHOW_HELP,
31  G1_NUM_COMMANDS
32};
33
34g1_input_class g1_input;
35
36i4_bool g1_disable_all_drawing = i4_F;
37
38//(OLI)
39void (*g1_reload_dll)(void) = NULL;
40
41void g1_input_class::acknowledge()
42{
43  for (w32 i=0; i<NUM_STANK_CONTROLS; i++)
44    if (in[i]==PRESSED_AND_RELEASED)
45      in[i]=NOT_PRESSED;
46}
47
48void g1_input_class::init()
49{
50  // default to invalid keys (negative)
51  memset(keys, 0xff, sizeof(keys));
52  mouse_scroll_flags=0;
53
54  i4_key_man.load(i4gets("keys_res_file"));
55
56  matchup=new i4_key_matchup_class;
57
58  char *cmds[]={"Move Left",            // LEFT =0,
59                "Move Right",           // RIGHT,
60                "Move Forward",         // UP,
61                "Move Backward",        // DOWN,
62                "Main Gun",             // BUTTON_1,
63                "Missiles",             // BUTTON_2,
64                "Chain Gun",            // BUTTON_3,
65                "Strafe Left",          // STRAFE_LEFT,
66                "Strafe Right",         // STRAFE_RIGHT,
67                "Strafe Mode",          // STRAFE_MODIFIER,
68                "Zoom In",
69                "Zoom Out",
70                "Zoom Up",
71                "Zoom Down",
72                "Zoom Left",
73                "Zoom Right",
74                "Main Menu",
75                "Toggle Shadows",
76                "Toggle Stats",
77                "Toggle Texture Loading",
78                "Help",
79
80                0};
81
82               
83  for (int i=0; cmds[i]; i++)
84    matchup->add(cmds[i], i);
85
86
87  memset(in,0,sizeof(in));
88  memset(time_qued, 0, sizeof(time_qued));
89  memset(cheat_code_buffer,0,CHEATCODE_BUFFERSIZE);
90
91  esc=0;
92  cont=i4_F;
93}
94
95void g1_input_class::uninit()
96{
97  i4_key_man.uninit();
98  delete matchup;
99  matchup=0;
100}
101
102
103void g1_input_class::que_keys(i4_time_class cur_time)
104{
105  for (int i=0; i<NUM_STANK_CONTROLS; i++)
106  {
107    if (in[i]==PRESSED)
108    {
109      sw32 add=cur_time.milli_diff(time_down[i]);
110      if (add<0)
111        // haven't reached the keypress yet
112        add=0;
113      if (add>1000/G1_HZ)
114        add=1000/G1_HZ;   
115
116      time_qued[i]+=add;
117      time_down[i].add_milli(add);
118    }
119  }
120}
121
122w32 g1_input_class::deque_time(in_type t)
123{
124  w32 ret = time_qued[t];
125   if (ret>1000/G1_HZ)
126    ret=1000/G1_HZ;   
127  time_qued[t]-=ret;
128  return ret;
129}
130
131void g1_input_class::receive_event(i4_event *ev)
132{
133  if (ev->type()==i4_event::DO_COMMAND)
134  {
135    CAST_PTR(kev, i4_do_command_event_class, ev);
136
137    int com=matchup->remap(kev->command_id);
138   
139    if (com>=0 && com<NUM_STANK_CONTROLS)
140    {
141      if (in[com]!= PRESSED)
142      {
143        time_down[com]=kev->time;   
144        in[com]=PRESSED;
145      }
146    }
147    else if (memcmp(kev->command, "Build ", 6)==0)
148      g1_human->build_unit(g1_get_object_type(kev->command+6));         // build an object
149    else
150    {
151      switch (com)
152      {
153        case G1_MAIN_MENU : esc=i4_T; break;
154        case G1_TOGGLE_STATS :  g1_stat_counter.show();   break;
155        case G1_SHOW_HELP : i4_kernel.send_event(i4_current_app, ev); break;
156      }
157    } 
158  }
159  else if (ev->type()==i4_event::END_COMMAND)
160  {
161    CAST_PTR(kev,i4_end_command_event_class,ev);
162
163    int com=matchup->remap(kev->command_id);
164
165    if (com>=0 && com<NUM_STANK_CONTROLS)
166    {
167      sw32 add=kev->time.milli_diff(time_down[com]);
168      if (add>0)
169      {
170        time_qued[com]+=add;
171        time_down[com].add_milli(add);
172      }
173     
174      if (in[com]==PRESSED)
175        in[com]=PRESSED_AND_RELEASED;
176      else
177        in[com]=NOT_PRESSED;
178    }
179  }
180  else if (ev->type()==i4_event::KEY_PRESS)
181  {
182    key_pressed=i4_T;   // any key sets continue to true
183
184
185    CAST_PTR(kev,i4_key_press_event_class,ev);
186    memmove(cheat_code_buffer+1,cheat_code_buffer,CHEATCODE_BUFFERSIZE-1);
187    cheat_code_buffer[0] = kev->key_code;
188  }
189  else if (ev->type()==i4_event::MOUSE_MOVE)
190  {
191    CAST_PTR(mev,i4_mouse_move_event_class,ev);
192   
193    mouse_scroll_flags=0;
194
195#if 0
196    if (mev->x<g1_resources.mouse_scroll.x1)
197      mouse_scroll_flags|=MOUSE_SCROLL_LEFT;
198    if (mev->x>i4_current_app->get_root_window()->width()-g1_resources.mouse_scroll.x2)
199      mouse_scroll_flags|=MOUSE_SCROLL_RIGHT;
200
201    if (mev->y>i4_current_app->get_root_window()->height()-g1_resources.mouse_scroll.y2)
202      mouse_scroll_flags|=MOUSE_SCROLL_DOWN;
203
204    if (mev->y<g1_resources.mouse_scroll.y1)
205      mouse_scroll_flags|=MOUSE_SCROLL_UP;
206#endif
207  } else if (ev->type()==i4_event::WINDOW_MESSAGE)
208  {
209    CAST_PTR(wev, i4_window_message_class, ev);
210    if (wev->sub_type==i4_window_message_class::LOST_MOUSE_FOCUS)
211      mouse_scroll_flags=0;
212  }
213
214}
215
Note: See TracBrowser for help on using the repository browser.