source: golgotha/src/i4/video/win32/dx_cursor.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 14 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: 7.7 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//#define INITGUID
10#include "video/win32/dx_cursor.hh"
11#include "video/win32/win32_input.hh"
12#include "image/image.hh"
13#include "image/color.hh"
14#include "area/rectlist.hh"
15#include "main/win_main.hh"
16#include <process.h>
17#include "threads/threads.hh"
18
19extern void dx_error(sw32 result);
20
21
22void dx_threaded_mouse_class::thread()
23{
24  DIDEVICEOBJECTDATA od;
25
26  //set_active(i4_T);
27 
28  SetCursorPos(0,0);
29
30  while (!stop)
31  {   
32    DWORD dwElements = 1;
33   
34    WaitForSingleObject(direct_input_mouse_event, INFINITE);
35
36    acquire_lock.lock();
37   
38    sw32    temp_mouse_x = current_mouse_x;
39    sw32    temp_mouse_y = current_mouse_y;
40   
41    i4_bool asdf = i4_F;
42
43    while (dwElements)
44    {
45      direct_input_mouse_device->Acquire();
46
47      HRESULT hr = direct_input_mouse_device->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),
48                                                            &od,
49                                                            &dwElements, 0);     
50      if (active && dwElements)
51      {
52        switch (od.dwOfs)
53        {         
54          case DIMOFS_X:
55          {           
56            sw32 new_x = temp_mouse_x + od.dwData;
57 
58            if (new_x < clip_x1)
59            {
60              new_x = clip_x1;
61            }
62            else
63            if (new_x >= clip_x2)
64            {
65              new_x = clip_x2-1;
66            }
67
68            temp_mouse_x = new_x;
69          } break;
70
71          case DIMOFS_Y:
72          {
73            sw32 new_y = temp_mouse_y + od.dwData;
74
75            if (new_y < clip_y1)
76            {
77              new_y = clip_y1;
78            }
79            else
80            if (new_y >= clip_y2)
81            {
82              new_y = clip_y2-1;
83            }
84
85            temp_mouse_y = new_y;
86          } break;
87         
88          case DIMOFS_BUTTON0:
89          {
90            if (input)
91            {
92              if (od.dwData & 0x80)             
93              {
94                asdf = i4_T;
95                input->update_mouse_buttons(i4_mouse_button_event_class::LEFT,
96                                            win32_input_class::DOWN);
97              }
98              else
99                input->update_mouse_buttons(i4_mouse_button_event_class::LEFT,
100                                            win32_input_class::UP);
101            }
102          } break;
103
104          case DIMOFS_BUTTON1:
105          {
106            if (input)
107            {
108              if (od.dwData & 0x80)             
109                input->update_mouse_buttons(i4_mouse_button_event_class::RIGHT,
110                                            win32_input_class::DOWN);
111              else
112                input->update_mouse_buttons(i4_mouse_button_event_class::RIGHT,
113                                            win32_input_class::UP);
114            }
115          } break;
116         
117          case DIMOFS_BUTTON2:
118          {
119            if (input)
120            {
121              if (od.dwData & 0x80)             
122                input->update_mouse_buttons(i4_mouse_button_event_class::CENTER,
123                                            win32_input_class::DOWN);
124              else
125                input->update_mouse_buttons(i4_mouse_button_event_class::CENTER,
126                                            win32_input_class::UP);
127            }
128          } break;
129        }
130      }
131    }
132   
133    if (active)
134    {
135      if (temp_mouse_x != current_mouse_x || temp_mouse_y != current_mouse_y)
136      {
137        if (input)
138          input->update_mouse_movement(temp_mouse_x,temp_mouse_y);
139     
140        if (!position_locked)
141        {
142          current_mouse_x = temp_mouse_x;
143          current_mouse_y = temp_mouse_y;
144        }
145         
146        draw_lock.lock();
147        remove();
148        display();
149        draw_lock.unlock();
150      }     
151    }
152    else
153      direct_input_mouse_device->Unacquire();
154
155    if (asdf)
156    {
157      i4_warning("hi");
158    }
159    acquire_lock.unlock();
160  }
161
162  set_active(i4_F);
163
164  stop = i4_F;
165}
166
167
168void directx_mouse_thread_start(void *arg)
169{
170  ((dx_threaded_mouse_class *)arg)->thread();
171  _endthread();
172}
173
174
175
176void dx_threaded_mouse_class::uninit_mouse()
177{
178  if (direct_input_driver)
179  {
180    direct_input_driver->Release();
181    direct_input_driver = 0;
182  }
183
184  if (direct_input_mouse_device)
185  {
186    direct_input_mouse_device->Release();
187    direct_input_mouse_device = 0;
188  }
189
190  if (direct_input_mouse_event)
191  {
192    CloseHandle(direct_input_mouse_event);
193    direct_input_mouse_event = 0;
194  }
195}
196
197
198void dx_threaded_mouse_class::init_mouse(HWND hwnd)
199{
200  HRESULT hr;
201
202  hr = DirectInputCreate(i4_win32_instance, DIRECTINPUT_VERSION, &direct_input_driver, NULL);
203  if (hr !=DI_OK)
204    i4_error("DirectInputCreate");
205
206  hr = direct_input_driver->CreateDevice(GUID_SysMouse, &direct_input_mouse_device, NULL);
207  if (hr !=DI_OK)
208    i4_error("CreateDevice(SysMouse)");
209
210  hr = direct_input_mouse_device->SetDataFormat(&c_dfDIMouse);
211  if (hr !=DI_OK)
212    i4_error("SetDataFormat(SysMouse, dfDIMouse)");
213
214
215  hr = direct_input_mouse_device->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
216  if (hr !=DI_OK)
217    i4_error("SetCooperativeLevel(SysMouse)");
218
219  direct_input_mouse_event = CreateEvent(0, 0, 0, 0);
220  if (direct_input_mouse_event == NULL)
221    i4_error("CreateEvent");
222
223
224  hr = direct_input_mouse_device->SetEventNotification(direct_input_mouse_event);
225  if (hr !=DI_OK)
226    i4_error("SetEventNotification(SysMouse)");
227
228 
229  /*
230   *  Set the buffer size to DINPUT_BUFFERSIZE elements.
231   *  The buffer size is a DWORD property associated with the device.
232   */
233
234  DIPROPDWORD dipdw =
235  {
236    {
237      sizeof(DIPROPDWORD),        // diph.dwSize
238      sizeof(DIPROPHEADER),       // diph.dwHeaderSize
239      0,                          // diph.dwObj
240      DIPH_DEVICE,                // diph.dwHow
241    },
242    16                            // dwData
243  };
244     
245  hr = direct_input_mouse_device->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph);     
246  if (hr !=DI_OK)
247    i4_error("Set buffer size(SysMouse)");
248 
249}
250
251dx_threaded_mouse_class::dx_threaded_mouse_class(const i4_pal *screen_pal,
252                                                 HWND   window_handle,
253                                                 sw32 clip_x1, sw32 clip_y1,
254                                                 sw32 clip_x2, sw32 clip_y2)
255    : window_handle(window_handle),
256      clip_x1(clip_x1), clip_y1(clip_y1),
257      clip_x2(clip_x2), clip_y2(clip_y2)     
258{
259 
260  init_mouse(window_handle);
261 
262  cursor.pict=0;
263  position_locked = i4_F;
264
265  visible=i4_F;
266  stop=i4_F;
267
268  current_mouse_x=0;
269  current_mouse_y=0;
270
271  _beginthread(directx_mouse_thread_start, thread_stack_size, this);
272}
273
274void dx_threaded_mouse_class::set_active(i4_bool act)
275{
276  acquire_lock.lock();
277
278  active = act;
279
280  if (active)
281    direct_input_mouse_device->Acquire();
282  else
283    direct_input_mouse_device->Unacquire();
284 
285  acquire_lock.unlock();
286}
287
288dx_threaded_mouse_class::~dx_threaded_mouse_class()
289{
290  stop = i4_T;
291  while (stop)
292    Sleep(0);
293
294  if (cursor.pict)
295  {
296    delete cursor.pict;
297    cursor.pict = 0;
298  }
299 
300  uninit_mouse();
301}
302
303
Note: See TracBrowser for help on using the repository browser.