-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
132 lines (104 loc) · 2.84 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "wx/wx.h"
#include "wx/sizer.h"
#include "Ball.h"
#include "Pong.h"
enum
{
MY_TIMER_ID = 100500,
};
class Board : public wxPanel
{
private:
public:
wxTimer m_timer;
Ball ball;
wxSize windowSize;
wxPoint paddlePos;
Pong m_pong;
Board(wxFrame* parent);
void OnTimerTimeout(wxTimerEvent& event);
void mouseMoved(wxMouseEvent& event);
void StartGame();
void OnPause(wxCommandEvent &event);
void OnPlay(wxCommandEvent &event);
DECLARE_EVENT_TABLE()
};
Board::Board(wxFrame* parent) : wxPanel(parent)
{
windowSize = parent->GetClientSize();
m_timer.SetOwner(this,MY_TIMER_ID);
}
class MyApp: public wxApp
{
bool OnInit();
wxFrame *frame;
Board * drawPane;
wxMenuBar *menubar;
wxMenu *file;
wxMenu *game;
public:
void OnQuit(wxCommandEvent& event);
};
IMPLEMENT_APP(MyApp)
#define PAUSE_ID 10000
#define PLAY_ID 10001
bool MyApp::OnInit()
{
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
frame = new wxFrame((wxFrame *)NULL, -1, wxT("Hello wxDC"), wxPoint(50,50), wxSize(800,600));
frame->SetBackgroundColour(wxColour(* wxBLACK));
drawPane = new Board( (wxFrame*) frame );
drawPane->SetBackgroundColour(wxColour(* wxBLACK));
sizer->Add(drawPane, 1, wxEXPAND);
frame->SetSizer(sizer);
frame->SetAutoLayout(true);
menubar = new wxMenuBar;
file = new wxMenu;
file->Append(wxID_EXIT, wxT("&Quit"));
game = new wxMenu;
game->Append(PAUSE_ID ,wxT("&Pause"));
game->Append(PLAY_ID ,wxT("&Play"));
menubar->Append(file, wxT("&File"));
menubar->Append(game, wxT("&Game"));
frame->SetMenuBar(menubar);
Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyApp::OnQuit));
Connect(PAUSE_ID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Board::OnPause));
Connect(PLAY_ID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Board::OnPlay));
frame->Show();
drawPane->StartGame();
return true;
}
void MyApp::OnQuit(wxCommandEvent &event) {
frame->Close();
}
void Board::OnPause(wxCommandEvent &event)
{
this->Disconnect(MY_TIMER_ID, wxEVT_TIMER, wxTimerEventHandler(Board::OnTimerTimeout), NULL, this);
m_timer.Stop();
}
void Board::OnPlay(wxCommandEvent &event)
{
this->Connect(MY_TIMER_ID, wxEVT_TIMER, wxTimerEventHandler(Board::OnTimerTimeout),NULL, this);
m_timer.Start(10);
}
void Board::OnTimerTimeout(wxTimerEvent& event)
{
m_pong.ComputeNextStep();
wxPaintDC paintDC(this);
m_pong.Paint(paintDC);
};
void Board::StartGame()
{
m_pong.Init(windowSize);
//m_timer = wxTimer(this,TIMER_ID);
this->Connect(MY_TIMER_ID, wxEVT_TIMER, wxTimerEventHandler(Board::OnTimerTimeout),NULL, this);
m_timer.Start(10);
}
void Board::mouseMoved(wxMouseEvent& event)
{
m_pong.OnHumanPlayerMove(event.GetPosition().y);
}
BEGIN_EVENT_TABLE(Board, wxPanel)
EVT_MOTION(Board::mouseMoved)
// EVT_TIMER(TIMER_ID, Board::OnTimerTimeout)
END_EVENT_TABLE()