From ef6c59e30e5fca0528ca542403f7d3797efd6d3b Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sun, 17 Apr 2022 11:42:40 +0300 Subject: [PATCH] Moved stopwatch to status bar, separated bg and tiles to bitmaps and upgraded rendering system --- Controller.cpp | 30 +++++++++++++++++++++++-- Controller.h | 17 +++++++++++--- Drawer.cpp | 61 ++++++++++++++++++++++++++------------------------ Drawer.h | 19 +++++++++++----- GamePanel.cpp | 34 +++++++++++++++------------- GamePanel.h | 12 +++++----- 6 files changed, 113 insertions(+), 60 deletions(-) diff --git a/Controller.cpp b/Controller.cpp index 9a6b42d..e650197 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -1,3 +1,29 @@ -#include "./Controller.h" +#include "Controller.h" +#include "utils.h" +#include -Controller::Controller() {}; \ No newline at end of file +Controller::Controller(Drawer& drawer): drawer(drawer) {}; + +void Controller::resize(const wxSize& tableSize) { + drawer.setBG(tableSize); + drawer.initScreen(tableSize); + + resolution = tableSize; + + wxLogDebug("Resize"); +} + +wxPoint Controller::toGrid(const wxPoint& point) { + wxPoint out(-1, -1); + + if (point.x >= pixelTableRect.x && + point.x <= pixelTableRect.x + pixelTableRect.width && + point.y >= pixelTableRect.y && + point.y <= pixelTableRect.y + pixelTableRect.height) + { + out.x = upDiv(point.x - pixelTableRect.x, tileSize.x); + out.y = upDiv(point.y - pixelTableRect.y, tileSize.y); + } + + return out; +} \ No newline at end of file diff --git a/Controller.h b/Controller.h index dbeeff4..fc0d3d0 100644 --- a/Controller.h +++ b/Controller.h @@ -1,15 +1,26 @@ #ifndef CONTROLLER_H #define CONTROLLER_H -#include "./wxw.h" +#include "wxw.h" + +#include "Drawer.h" class Controller { public: - Controller(); + Controller(Drawer& drawer); - int stopwatch; + int stopwatch = 0; + void resize(const wxSize& tableSize); + + wxPoint toGrid(const wxPoint& point); private: + Drawer& drawer; + + wxSize tileSize; + wxSize resolution; + wxSize gridSize; + wxRect pixelTableRect; }; #endif diff --git a/Drawer.cpp b/Drawer.cpp index 440a5b0..df60765 100644 --- a/Drawer.cpp +++ b/Drawer.cpp @@ -1,45 +1,48 @@ -#include "./Drawer.h" +#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" }; +#define TILE_IMAGES_N 37 +static const char* tileImageNames[] = { "Back", "Blank", "Chun", "Front", "Haku", "Hatsu", "Man1", "Man2", "Man3", "Man4", "Man5", "Man6", "Man7", "Man8", "Man9", "Nan", "Pei", "Pin1", "Pin2", "Pin3", "Pin4", "Pin5", "Pin6", "Pin7", "Pin8", "Pin9", "Shaa", "Sou1", "Sou2", "Sou3", "Sou4", "Sou5", "Sou6", "Sou7", "Sou8", "Sou9", "Ton" }; Drawer::Drawer() { - for (int i = 0; i < 40; i++) { + for (int i = 0; i < 37; 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); + wxLogDebug("Redraw"); + + if (isScreenReady) + dc.DrawBitmap(screenBitmap, 0, 0, false); } -void Drawer::drawBG(wxDC& dc) { +wxBitmap copyBitmap(const wxBitmap& old) { + return old.GetSubBitmap(wxRect(0, 0, old.GetWidth(), old.GetHeight())); +} + +void Drawer::setBG(const wxSize& tableSize) { + wxLogDebug("Recreate background"); + + bgBitmap = wxBitmap(tableSize); + + wxMemoryDC dc; + dc.SelectObject(bgBitmap); + dc.GradientFillConcentric(wxRect(wxPoint(0, 0), tableSize), wxColor(7, 85, 45), wxColor(1, 45, 22)); + + isBgReady = true; } -void Drawer::drawTiles(wxDC& dc) { +void Drawer::initScreen(const wxSize& tableSize) { + wxLogDebug("Recreate screen"); + if (isBgReady) { + screenBitmap = copyBitmap(bgBitmap); + isScreenReady = true; + } +} + +void Drawer::drawTile(wxDC& dc, const char index) { 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; + dc.DrawBitmap(tileImages[index], wxPoint(10, 10)); } diff --git a/Drawer.h b/Drawer.h index e7849d4..a7cc207 100644 --- a/Drawer.h +++ b/Drawer.h @@ -1,23 +1,30 @@ #ifndef DRAWER_H #define DRAWER_H -#include "./wxw.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); + void setBG(const wxSize& tableSize); + void initScreen(const wxSize& tableSize); +private: + void drawScreen(wxDC& dc); + void drawTile(wxDC& dc, const char index); + wxImage tileImages[40]; + + wxBitmap bgBitmap; + wxBitmap screenBitmap; + + bool isBgReady = false; + bool isScreenReady = false; }; #endif diff --git a/GamePanel.cpp b/GamePanel.cpp index b6c5731..5ec827f 100644 --- a/GamePanel.cpp +++ b/GamePanel.cpp @@ -1,38 +1,42 @@ -#include "./GamePanel.h" +#include "GamePanel.h" #include -GamePanel::GamePanel(wxFrame* parent) : wxPanel(parent) { +#include "utils.h" + +GamePanel::GamePanel(wxFrame* parent) : wxPanel(parent), controller(drawer), sb(parent->GetStatusBar()) { SetBackgroundStyle(wxBG_STYLE_PAINT); Bind(wxEVT_PAINT, &GamePanel::OnPaint, this); Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) -> void { - this->drawer.tableSize = evt.GetSize(); + this->controller.resize(evt.GetSize()); }); timer = new wxTimer(this); - timerText = drawer.initTimer(this); + Bind(wxEVT_TIMER, &GamePanel::OnTimer, this, timer->GetId()); - Bind(wxEVT_TIMER, [this](wxTimerEvent& _) -> void { - controller.stopwatch += 1; - drawer.drawTimer(timerText, controller.stopwatch); - }, timer->GetId()); + Bind(wxEVT_LEFT_DOWN, &GamePanel::OnClick, this); } void GamePanel::Start() { - wxLogDebug(_("Started game")); - controller.stopwatch = 0; - timer->Start(1000, wxTIMER_CONTINUOUS); - Refresh(); + timer->Start(1000, wxTIMER_CONTINUOUS); + sb->SetStatusText(LTimeToStr(controller.stopwatch)); } void GamePanel::OnPaint(wxPaintEvent& _) { wxAutoBufferedPaintDC dc(this); drawer.drawTable(dc); - drawer.drawTimer(timerText, controller.stopwatch); - - wxLogDebug("Rerender"); } + +void GamePanel::OnTimer(wxTimerEvent& _) { + controller.stopwatch += 1; + sb->SetStatusText(LTimeToStr(controller.stopwatch)); +} + +void GamePanel::OnClick(wxMouseEvent& _) { + wxPoint res = controller.toGrid(wxGetMousePosition()); + sb->PushStatusText(itowxS(res.x) + _("x") + itowxS(res.y)); +} \ No newline at end of file diff --git a/GamePanel.h b/GamePanel.h index 8695c09..787582e 100644 --- a/GamePanel.h +++ b/GamePanel.h @@ -1,12 +1,12 @@ #ifndef GRAPHICS_H #define GRAPHICS_H -#include "./wxw.h" +#include "wxw.h" #include -#include "./Drawer.h" -#include "./Controller.h" +#include "Drawer.h" +#include "Controller.h" class GamePanel : public wxPanel { public: @@ -19,8 +19,10 @@ private: Controller controller; void OnPaint(wxPaintEvent& _); - - wxStaticText* timerText; + void OnTimer(wxTimerEvent& _); + void OnClick(wxMouseEvent& _); + + wxStatusBar* sb; wxTimer* timer; };