]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
mingw compilation fix
[wxWidgets.git] / samples / joytest / joytest.cpp
CommitLineData
bbf1f0e5
KB
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
367e57ec
RL
23#include <wx/wave.h>
24#include <wx/joystick.h>
bbf1f0e5
KB
25
26#include "joytest.h"
27
28MyFrame *frame = NULL;
29
30IMPLEMENT_APP(MyApp)
31
32// For drawing lines in a canvas
33long xpos = -1;
34long ypos = -1;
35
36int winNumber = 1;
37
38// Initialise this in OnInit, not statically
39bool MyApp::OnInit(void)
40{
41 wxJoystick stick(wxJOYSTICK1);
42 if (!stick.IsOk())
43 {
44 wxMessageBox("No joystick detected!");
45 return FALSE;
46 }
4c05b807 47#if wxUSE_WAVE
bbf1f0e5 48 m_fire.Create("gun.wav");
4c05b807 49#endif // wxUSE_WAVE
bbf1f0e5
KB
50
51 m_maxX = stick.GetXMax();
52 m_maxY = stick.GetYMax();
53
54 // Create the main frame window
55
56 frame = new MyFrame(NULL, "Joystick Demo", wxPoint(0, 0), wxSize(500, 400),
5f4987fe 57 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5
KB
58
59 // Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 60#ifdef __WXMSW__
bbf1f0e5
KB
61 frame->SetIcon(wxIcon("joyicon"));
62#endif
63#ifdef __X__
64 frame->SetIcon(wxIcon("joyicon.xbm"));
65#endif
66
67 // Make a menubar
68 wxMenu *file_menu = new wxMenu;
69
70 file_menu->Append(JOYTEST_QUIT, "&Exit");
71
72 wxMenu *help_menu = new wxMenu;
73 help_menu->Append(JOYTEST_ABOUT, "&About");
74
75 wxMenuBar *menu_bar = new wxMenuBar;
76
77 menu_bar->Append(file_menu, "&File");
78 menu_bar->Append(help_menu, "&Help");
79
80 // Associate the menu bar with the frame
81 frame->SetMenuBar(menu_bar);
82
83 frame->CreateStatusBar();
84
85 frame->Show(TRUE);
86
87 SetTopWindow(frame);
88
89 return TRUE;
90}
91
92BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
93 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
94END_EVENT_TABLE()
95
96// Define a constructor for my canvas
97MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
98 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
99{
100 wxJoystick joystick(wxJOYSTICK1);
101 joystick.SetCapture(this);
102}
103
104MyCanvas::~MyCanvas(void)
105{
106 wxJoystick joystick(wxJOYSTICK1);
107 joystick.ReleaseCapture();
108}
109
110void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
111{
112 wxClientDC dc(this);
113
114 wxPoint pt(event.GetPosition());
115
116 // Scale to canvas size
117 int cw, ch;
118 GetSize(&cw, &ch);
119
120 pt.x = (long) (((double)pt.x/(double)wxGetApp().m_maxX) * cw);
121 pt.y = (long) (((double)pt.y/(double)wxGetApp().m_maxY) * ch);
122
123 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
124 {
125 dc.SetPen(*wxBLACK_PEN);
126 dc.DrawLine(xpos, ypos, pt.x, pt.y);
127 }
128 xpos = pt.x;
129 ypos = pt.y;
130
131 char buf[100];
132 if (event.ButtonDown())
ee993c74 133 sprintf(buf, "Joystick (%d, %d) Fire!", pt.x, pt.y);
bbf1f0e5 134 else
ee993c74 135 sprintf(buf, "Joystick (%d, %d)", pt.x, pt.y);
bbf1f0e5
KB
136 frame->SetStatusText(buf);
137
4c05b807 138#if wxUSE_WAVE
bbf1f0e5
KB
139 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
140 {
141 wxGetApp().m_fire.Play();
142 }
4c05b807 143#endif // wxUSE_WAVE
bbf1f0e5
KB
144}
145
146BEGIN_EVENT_TABLE(MyFrame, wxFrame)
147 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
148END_EVENT_TABLE()
149
150MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
151const long style):
152 wxFrame(parent, -1, title, pos, size, style)
153{
154 canvas = new MyCanvas(this);
155}
156
157MyFrame::~MyFrame(void)
158{
159}
160
161void MyFrame::OnQuit(wxCommandEvent& event)
162{
163 Close(TRUE);
164}
165
166void MyFrame::OnActivate(wxActivateEvent& event)
167{
168 if (event.GetActive() && canvas)
169 canvas->SetFocus();
170}