Added Drawer and Controller classes for game graphics and logic, refactoring
This commit is contained in:
parent
6b03071119
commit
4d0c579ea7
@ -7,7 +7,6 @@ class AboutDlg : public wxDialog
|
||||
{
|
||||
public:
|
||||
AboutDlg(wxWindow *parent, wxWindowID id);
|
||||
~AboutDlg() {};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
7
App.cpp
7
App.cpp
@ -1,12 +1,15 @@
|
||||
#include "./App.h"
|
||||
#include "./GameFrame.h"
|
||||
#include "./MainFrame.h"
|
||||
|
||||
wxIMPLEMENT_APP(MyApp);
|
||||
|
||||
bool MyApp::OnInit()
|
||||
{
|
||||
GameFrame *frame = new GameFrame();
|
||||
wxImage::AddHandler(new wxPNGHandler());
|
||||
|
||||
MainFrame *frame = new MainFrame();
|
||||
frame->Show(true);
|
||||
SetTopWindow(frame);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
2
App.h
2
App.h
@ -6,7 +6,7 @@
|
||||
class MyApp : public wxApp
|
||||
{
|
||||
public:
|
||||
virtual bool OnInit();
|
||||
virtual bool OnInit() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
3
Controller.cpp
Normal file
3
Controller.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "./Controller.h"
|
||||
|
||||
Controller::Controller() {};
|
15
Controller.h
Normal file
15
Controller.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H
|
||||
|
||||
#include "./wxw.h"
|
||||
|
||||
class Controller {
|
||||
public:
|
||||
Controller();
|
||||
|
||||
int stopwatch;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
45
Drawer.cpp
Normal file
45
Drawer.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "./Drawer.h"
|
||||
|
||||
static const char* tileImageNames[] = { "Back", "Blank", "Chun", "Front", "Haku", "Hatsu", "Man1", "Man2", "Man3", "Man4", "Man5", "Man5-Dora", "Man6", "Man7", "Man8", "Man9", "Nan", "Pei", "Pin1", "Pin2", "Pin3", "Pin4", "Pin5", "Pin5-Dora", "Pin6", "Pin7", "Pin8", "Pin9", "Shaa", "Sou1", "Sou2", "Sou3", "Sou4", "Sou5", "Sou5-Dora", "Sou6", "Sou7", "Sou8", "Sou9", "Ton" };
|
||||
|
||||
Drawer::Drawer() {
|
||||
for (int i = 0; i < 40; i++) {
|
||||
tileImages[i].LoadFile(_("./resources/tiles/") + _(tileImageNames[i]) + _(".png"), wxBITMAP_TYPE_PNG);
|
||||
tileImages[i].Rescale(60, 80);
|
||||
}
|
||||
}
|
||||
|
||||
void Drawer::drawTable(wxDC& dc) {
|
||||
drawBG(dc);
|
||||
drawTiles(dc);
|
||||
}
|
||||
|
||||
void Drawer::drawBG(wxDC& dc) {
|
||||
dc.GradientFillConcentric(wxRect(wxPoint(0, 0), tableSize), wxColor(7, 85, 45), wxColor(1, 45, 22));
|
||||
}
|
||||
|
||||
void Drawer::drawTiles(wxDC& dc) {
|
||||
dc.DrawBitmap(tileImages[3], wxPoint(10, 10));
|
||||
dc.DrawBitmap(tileImages[10], wxPoint(10, 10));
|
||||
|
||||
dc.DrawBitmap(tileImages[3], wxPoint(80, 10));
|
||||
dc.DrawBitmap(tileImages[10], wxPoint(80, 10));
|
||||
}
|
||||
|
||||
wxString LTimeToStr(int time) {
|
||||
return wxString::Format(_("%d:%02d:%02d"), time / 3600, (time / 60) % 60, time % 60);
|
||||
}
|
||||
|
||||
void Drawer::drawTimer(wxStaticText* timerText, int time) {
|
||||
wxString timeStr = LTimeToStr(time);
|
||||
timerText->SetPosition(wxPoint(tableSize.x / 2, 10));
|
||||
timerText->SetLabel(timeStr);
|
||||
}
|
||||
|
||||
wxStaticText* Drawer::initTimer(wxWindow* parent) {
|
||||
auto timerText = new wxStaticText(parent, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
|
||||
timerText->SetForegroundColour(wxColor(255, 255, 255));
|
||||
timerText->SetFont(wxFont(wxFontInfo(20)));
|
||||
|
||||
return timerText;
|
||||
}
|
23
Drawer.h
Normal file
23
Drawer.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef DRAWER_H
|
||||
#define DRAWER_H
|
||||
|
||||
#include "./wxw.h"
|
||||
|
||||
class Drawer {
|
||||
public:
|
||||
Drawer();
|
||||
|
||||
void drawTable(wxDC& dc);
|
||||
void drawTimer(wxStaticText* timerText, int time);
|
||||
wxStaticText* initTimer(wxWindow* parent);
|
||||
|
||||
wxSize tableSize;
|
||||
|
||||
private:
|
||||
void drawBG(wxDC& dc);
|
||||
void drawTiles(wxDC& dc);
|
||||
|
||||
wxImage tileImages[40];
|
||||
};
|
||||
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
#include "./GameFrame.h"
|
||||
|
||||
#include "./HelpDlg.h"
|
||||
#include "./RulesDlg.h"
|
||||
#include "./AboutDlg.h"
|
||||
|
||||
BEGIN_EVENT_TABLE(GameFrame, wxFrame)
|
||||
EVT_MENU(IDM_Exit, GameFrame::OnExit)
|
||||
EVT_MENU(IDM_Help, GameFrame::OnHelp)
|
||||
EVT_MENU(IDM_Rules, GameFrame::OnRules)
|
||||
EVT_MENU(IDM_About, GameFrame::OnHelp)
|
||||
EVT_PAINT(GameFrame::OnPaint)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
GameFrame::GameFrame()
|
||||
: wxFrame(nullptr, wxID_ANY, _("Маджонг (пасьянс)"))
|
||||
{
|
||||
InitMenu();
|
||||
}
|
||||
|
||||
void GameFrame::InitMenu() {
|
||||
wxMenu *menuGame = new wxMenu;
|
||||
menuGame->Append(IDM_New_Game, _("Начать сначала"));
|
||||
menuGame->AppendSeparator();
|
||||
menuGame->Append(IDM_Exit, _("Выход"));
|
||||
|
||||
wxMenu *menuHelp = new wxMenu;
|
||||
menuHelp->Append(IDM_Help, _("Инструкция"));
|
||||
menuHelp->Append(IDM_Rules, _("Правила игры"));
|
||||
menuHelp->Append(IDM_About, _("О программе"));
|
||||
|
||||
wxMenuBar *menuBar = new wxMenuBar;
|
||||
menuBar->Append(menuGame, _("Игра"));
|
||||
menuBar->Append(menuHelp, _("Помощь"));
|
||||
|
||||
SetMenuBar(menuBar);
|
||||
}
|
||||
|
||||
void GameFrame::OnExit(wxCommandEvent &event) {
|
||||
Close();
|
||||
}
|
||||
|
||||
void GameFrame::OnHelp(wxCommandEvent &event) {
|
||||
HelpDlg *dlg = new HelpDlg(this, -1);
|
||||
|
||||
dlg->Show();
|
||||
}
|
||||
|
||||
void GameFrame::OnRules(wxCommandEvent &event) {
|
||||
RulesDlg *dlg = new RulesDlg(this, -1);
|
||||
|
||||
dlg->Show();
|
||||
}
|
||||
|
||||
void GameFrame::OnAbout(wxCommandEvent &event) {
|
||||
AboutDlg *dlg = new AboutDlg(this, -1);
|
||||
|
||||
dlg->Show();
|
||||
}
|
||||
|
||||
void GameFrame::OnPaint(wxPaintEvent& event) {
|
||||
wxPaintDC dc(this);
|
||||
|
||||
paint.drawTable(dc, GetSize());
|
||||
}
|
38
GameFrame.h
38
GameFrame.h
@ -1,38 +0,0 @@
|
||||
#ifndef GAMEFRAME_H_
|
||||
#define GAMEFRAME_H_
|
||||
|
||||
#include "./wxw.h"
|
||||
|
||||
#include "./Graphics.h"
|
||||
|
||||
/// @uml{style[#line.dotted:blue]}
|
||||
class GameFrame : public wxFrame
|
||||
{
|
||||
public:
|
||||
GameFrame();
|
||||
|
||||
private:
|
||||
Graphics paint;
|
||||
|
||||
void InitMenu();
|
||||
|
||||
void OnExit(wxCommandEvent &event);
|
||||
void OnHelp(wxCommandEvent &event);
|
||||
void OnRules(wxCommandEvent &event);
|
||||
void OnAbout(wxCommandEvent &event);
|
||||
|
||||
void OnPaint(wxPaintEvent& event);
|
||||
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
IDM_Exit = wxID_EXIT,
|
||||
IDM_Help = wxID_HELP,
|
||||
IDM_About = wxID_ABOUT,
|
||||
IDM_New_Game = 1,
|
||||
IDM_Rules
|
||||
};
|
||||
|
||||
#endif
|
38
GamePanel.cpp
Normal file
38
GamePanel.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "./GamePanel.h"
|
||||
|
||||
#include <wx/dcbuffer.h>
|
||||
|
||||
GamePanel::GamePanel(wxFrame* parent) : wxPanel(parent) {
|
||||
SetBackgroundStyle(wxBG_STYLE_PAINT);
|
||||
|
||||
Bind(wxEVT_PAINT, &GamePanel::OnPaint, this);
|
||||
Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) -> void {
|
||||
this->drawer.tableSize = evt.GetSize();
|
||||
});
|
||||
|
||||
timer = new wxTimer(this);
|
||||
timerText = drawer.initTimer(this);
|
||||
|
||||
Bind(wxEVT_TIMER, [this](wxTimerEvent& _) -> void {
|
||||
controller.stopwatch += 1;
|
||||
drawer.drawTimer(timerText, controller.stopwatch);
|
||||
}, timer->GetId());
|
||||
}
|
||||
|
||||
void GamePanel::Start() {
|
||||
wxLogDebug(_("Started game"));
|
||||
|
||||
controller.stopwatch = 0;
|
||||
timer->Start(1000, wxTIMER_CONTINUOUS);
|
||||
|
||||
Refresh();
|
||||
}
|
||||
|
||||
void GamePanel::OnPaint(wxPaintEvent& _) {
|
||||
wxAutoBufferedPaintDC dc(this);
|
||||
|
||||
drawer.drawTable(dc);
|
||||
drawer.drawTimer(timerText, controller.stopwatch);
|
||||
|
||||
wxLogDebug("Rerender");
|
||||
}
|
27
GamePanel.h
Normal file
27
GamePanel.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef GRAPHICS_H
|
||||
#define GRAPHICS_H
|
||||
|
||||
#include "./wxw.h"
|
||||
|
||||
#include <wx/stopwatch.h>
|
||||
|
||||
#include "./Drawer.h"
|
||||
#include "./Controller.h"
|
||||
|
||||
class GamePanel : public wxPanel {
|
||||
public:
|
||||
GamePanel(wxFrame* parent);
|
||||
|
||||
void Start();
|
||||
|
||||
private:
|
||||
Drawer drawer;
|
||||
Controller controller;
|
||||
|
||||
void OnPaint(wxPaintEvent& _);
|
||||
|
||||
wxStaticText* timerText;
|
||||
wxTimer* timer;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,9 +0,0 @@
|
||||
#include "./Graphics.h"
|
||||
|
||||
void Graphics::drawTable(wxDC& dc, wxSize wndSize) {
|
||||
drawBG(dc, wndSize);
|
||||
}
|
||||
|
||||
void Graphics::drawBG(wxDC& dc, wxSize wndSize) {
|
||||
dc.GradientFillConcentric(wxRect(wxPoint(0, 0), wndSize), wxColor(7, 85, 45), wxColor(1, 45, 22));
|
||||
}
|
15
Graphics.h
15
Graphics.h
@ -1,15 +0,0 @@
|
||||
#ifndef GRAPHICS_H
|
||||
#define GRAPHICS_H
|
||||
|
||||
#include "./wxw.h"
|
||||
|
||||
class Graphics {
|
||||
public:
|
||||
Graphics() {};
|
||||
~Graphics() {};
|
||||
|
||||
void drawTable(wxDC& dc, wxSize wndSize);
|
||||
void drawBG(wxDC& dc, wxSize wndSize);
|
||||
};
|
||||
|
||||
#endif
|
@ -7,7 +7,6 @@ class HelpDlg : public wxDialog
|
||||
{
|
||||
public:
|
||||
HelpDlg(wxWindow *parent, wxWindowID id);
|
||||
~HelpDlg() {};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
56
MainFrame.cpp
Normal file
56
MainFrame.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include "./MainFrame.h"
|
||||
|
||||
#include "./HelpDlg.h"
|
||||
#include "./RulesDlg.h"
|
||||
#include "./AboutDlg.h"
|
||||
|
||||
MainFrame::MainFrame()
|
||||
: wxFrame(nullptr, wxID_ANY, _("Маджонг (пасьянс)"))
|
||||
{
|
||||
InitMenu();
|
||||
BindMenu();
|
||||
|
||||
panel = new GamePanel(this);
|
||||
panel->SetFocus();
|
||||
panel->Start();
|
||||
}
|
||||
|
||||
void MainFrame::InitMenu() {
|
||||
wxMenu *menuGame = new wxMenu;
|
||||
menuGame->Append(IDM_New_Game, _("Начать сначала"));
|
||||
menuGame->AppendSeparator();
|
||||
menuGame->Append(IDM_Exit, _("Выход"));
|
||||
|
||||
wxMenu *menuHelp = new wxMenu;
|
||||
menuHelp->Append(IDM_Help, _("Инструкция"));
|
||||
menuHelp->Append(IDM_Rules, _("Правила игры"));
|
||||
menuHelp->Append(IDM_About, _("О программе"));
|
||||
|
||||
wxMenuBar *menuBar = new wxMenuBar;
|
||||
menuBar->Append(menuGame, _("Игра"));
|
||||
menuBar->Append(menuHelp, _("Помощь"));
|
||||
|
||||
SetMenuBar(menuBar);
|
||||
}
|
||||
|
||||
void MainFrame::BindMenu() {
|
||||
Bind(wxEVT_MENU, [this](wxCommandEvent& _) -> void {
|
||||
Close();
|
||||
}, IDM_Exit);
|
||||
|
||||
Bind(wxEVT_MENU, [this](wxCommandEvent& _) -> void {
|
||||
(new HelpDlg(this, -1))->Show();
|
||||
}, IDM_Help);
|
||||
|
||||
Bind(wxEVT_MENU, [this](wxCommandEvent& _) -> void {
|
||||
(new AboutDlg(this, -1))->Show();
|
||||
}, IDM_About);
|
||||
|
||||
Bind(wxEVT_MENU, [this](wxCommandEvent& _) -> void {
|
||||
(new RulesDlg(this, -1))->Show();
|
||||
}, IDM_Rules);
|
||||
|
||||
Bind(wxEVT_MENU, [this](wxCommandEvent& _) -> void {
|
||||
panel->Start();
|
||||
}, IDM_New_Game);
|
||||
}
|
30
MainFrame.h
Normal file
30
MainFrame.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef MainFrame_H_
|
||||
#define MainFrame_H_
|
||||
|
||||
#include "./wxw.h"
|
||||
|
||||
#include "./GamePanel.h"
|
||||
|
||||
/// @uml{style[#line.dotted:blue]}
|
||||
class MainFrame : public wxFrame
|
||||
{
|
||||
public:
|
||||
MainFrame();
|
||||
|
||||
private:
|
||||
void InitMenu();
|
||||
void BindMenu();
|
||||
|
||||
GamePanel *panel;
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
IDM_Exit = wxID_EXIT,
|
||||
IDM_Help = wxID_HELP,
|
||||
IDM_About = wxID_ABOUT,
|
||||
IDM_Rules = wxID_HIGHEST + 1,
|
||||
IDM_New_Game
|
||||
};
|
||||
|
||||
#endif
|
@ -7,7 +7,6 @@ class RulesDlg : public wxDialog
|
||||
{
|
||||
public:
|
||||
RulesDlg(wxWindow *parent, wxWindowID id);
|
||||
~RulesDlg() {};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user