]> git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
corrections to dynamic loading for Darwin
[wxWidgets.git] / samples / joytest / joytest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: joytest.cpp
3 // Purpose: Joystick sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #if !wxUSE_JOYSTICK
24 #error You must set wxUSE_JOYSTICK to 1 in setup.h!
25 #endif
26
27 #include "wx/wave.h"
28 #include "wx/joystick.h"
29
30 #include "joytest.h"
31
32 MyFrame *frame = NULL;
33
34 IMPLEMENT_APP(MyApp)
35
36 // For drawing lines in a canvas
37 long xpos = -1;
38 long ypos = -1;
39
40 int winNumber = 1;
41
42 // Initialise this in OnInit, not statically
43 bool MyApp::OnInit(void)
44 {
45 wxJoystick stick(wxJOYSTICK1);
46 if (!stick.IsOk())
47 {
48 wxMessageBox("No joystick detected!");
49 return FALSE;
50 }
51 #if wxUSE_WAVE
52 m_fire.Create("gun.wav");
53 #endif // wxUSE_WAVE
54
55 m_maxX = stick.GetXMax();
56 m_maxY = stick.GetYMax();
57
58 // Create the main frame window
59
60 frame = new MyFrame(NULL, "Joystick Demo", wxPoint(0, 0), wxSize(500, 400),
61 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
62
63 // Give it an icon (this is ignored in MDI mode: uses resources)
64 #ifdef __WXMSW__
65 frame->SetIcon(wxIcon("joyicon"));
66 #endif
67 #ifdef __X__
68 frame->SetIcon(wxIcon("joyicon.xbm"));
69 #endif
70
71 // Make a menubar
72 wxMenu *file_menu = new wxMenu;
73
74 file_menu->Append(JOYTEST_QUIT, "&Exit");
75
76 wxMenu *help_menu = new wxMenu;
77 help_menu->Append(JOYTEST_ABOUT, "&About");
78
79 wxMenuBar *menu_bar = new wxMenuBar;
80
81 menu_bar->Append(file_menu, "&File");
82 menu_bar->Append(help_menu, "&Help");
83
84 // Associate the menu bar with the frame
85 frame->SetMenuBar(menu_bar);
86
87 frame->CreateStatusBar();
88
89 frame->Show(TRUE);
90
91 SetTopWindow(frame);
92
93 return TRUE;
94 }
95
96 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
97 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
98 END_EVENT_TABLE()
99
100 // Define a constructor for my canvas
101 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
102 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
103 {
104 wxJoystick joystick(wxJOYSTICK1);
105 joystick.SetCapture(this);
106 }
107
108 MyCanvas::~MyCanvas(void)
109 {
110 wxJoystick joystick(wxJOYSTICK1);
111 joystick.ReleaseCapture();
112 }
113
114 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
115 {
116 wxClientDC dc(this);
117
118 wxPoint pt(event.GetPosition());
119
120 // Scale to canvas size
121 int cw, ch;
122 GetSize(&cw, &ch);
123
124 pt.x = (long) (((double)pt.x/(double)wxGetApp().m_maxX) * cw);
125 pt.y = (long) (((double)pt.y/(double)wxGetApp().m_maxY) * ch);
126
127 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
128 {
129 dc.SetPen(*wxBLACK_PEN);
130 dc.DrawLine(xpos, ypos, pt.x, pt.y);
131 }
132 xpos = pt.x;
133 ypos = pt.y;
134
135 char buf[100];
136 if (event.ButtonDown())
137 sprintf(buf, "Joystick (%d, %d) Fire!", pt.x, pt.y);
138 else
139 sprintf(buf, "Joystick (%d, %d)", pt.x, pt.y);
140 frame->SetStatusText(buf);
141
142 #if wxUSE_WAVE
143 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
144 {
145 wxGetApp().m_fire.Play();
146 }
147 #endif // wxUSE_WAVE
148 }
149
150 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
151 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
152 END_EVENT_TABLE()
153
154 MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
155 const long style):
156 wxFrame(parent, -1, title, pos, size, style)
157 {
158 canvas = new MyCanvas(this);
159 }
160
161 MyFrame::~MyFrame(void)
162 {
163 }
164
165 void MyFrame::OnQuit(wxCommandEvent& event)
166 {
167 Close(TRUE);
168 }
169
170 void MyFrame::OnActivate(wxActivateEvent& event)
171 {
172 if (event.GetActive() && canvas)
173 canvas->SetFocus();
174 }