Moved stopwatch to status bar, separated bg and tiles to bitmaps and upgraded rendering system

This commit is contained in:
Dmitriy Shishkov 2022-04-17 11:42:40 +03:00
parent 34f08a4e0b
commit ef6c59e30e
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
6 changed files with 113 additions and 60 deletions

View File

@ -1,3 +1,29 @@
#include "./Controller.h"
#include "Controller.h"
#include "utils.h"
#include <wx/xml/xml.h>
Controller::Controller() {};
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;
}

View File

@ -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

View File

@ -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));
}

View File

@ -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;
void setBG(const wxSize& tableSize);
void initScreen(const wxSize& tableSize);
private:
void drawBG(wxDC& dc);
void drawTiles(wxDC& dc);
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

View File

@ -1,38 +1,42 @@
#include "./GamePanel.h"
#include "GamePanel.h"
#include <wx/dcbuffer.h>
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));
}

View File

@ -1,12 +1,12 @@
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "./wxw.h"
#include "wxw.h"
#include <wx/stopwatch.h>
#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& _);
void OnTimer(wxTimerEvent& _);
void OnClick(wxMouseEvent& _);
wxStaticText* timerText;
wxStatusBar* sb;
wxTimer* timer;
};