Moved stopwatch to status bar, separated bg and tiles to bitmaps and upgraded rendering system
This commit is contained in:
parent
34f08a4e0b
commit
ef6c59e30e
@ -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;
|
||||||
|
}
|
17
Controller.h
17
Controller.h
@ -1,15 +1,26 @@
|
|||||||
#ifndef CONTROLLER_H
|
#ifndef CONTROLLER_H
|
||||||
#define CONTROLLER_H
|
#define CONTROLLER_H
|
||||||
|
|
||||||
#include "./wxw.h"
|
#include "wxw.h"
|
||||||
|
|
||||||
|
#include "Drawer.h"
|
||||||
|
|
||||||
class Controller {
|
class Controller {
|
||||||
public:
|
public:
|
||||||
Controller();
|
Controller(Drawer& drawer);
|
||||||
|
|
||||||
int stopwatch;
|
int stopwatch = 0;
|
||||||
|
|
||||||
|
void resize(const wxSize& tableSize);
|
||||||
|
|
||||||
|
wxPoint toGrid(const wxPoint& point);
|
||||||
private:
|
private:
|
||||||
|
Drawer& drawer;
|
||||||
|
|
||||||
|
wxSize tileSize;
|
||||||
|
wxSize resolution;
|
||||||
|
wxSize gridSize;
|
||||||
|
wxRect pixelTableRect;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
61
Drawer.cpp
61
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() {
|
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].LoadFile(_("./resources/tiles/") + _(tileImageNames[i]) + _(".png"), wxBITMAP_TYPE_PNG);
|
||||||
tileImages[i].Rescale(60, 80);
|
tileImages[i].Rescale(60, 80);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Drawer::drawTable(wxDC& dc) {
|
void Drawer::drawTable(wxDC& dc) {
|
||||||
drawBG(dc);
|
wxLogDebug("Redraw");
|
||||||
drawTiles(dc);
|
|
||||||
|
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));
|
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[3], wxPoint(10, 10));
|
||||||
dc.DrawBitmap(tileImages[10], wxPoint(10, 10));
|
dc.DrawBitmap(tileImages[index], 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;
|
|
||||||
}
|
}
|
||||||
|
19
Drawer.h
19
Drawer.h
@ -1,23 +1,30 @@
|
|||||||
#ifndef DRAWER_H
|
#ifndef DRAWER_H
|
||||||
#define DRAWER_H
|
#define DRAWER_H
|
||||||
|
|
||||||
#include "./wxw.h"
|
#include "wxw.h"
|
||||||
|
|
||||||
class Drawer {
|
class Drawer {
|
||||||
public:
|
public:
|
||||||
Drawer();
|
Drawer();
|
||||||
|
|
||||||
void drawTable(wxDC& dc);
|
void drawTable(wxDC& dc);
|
||||||
void drawTimer(wxStaticText* timerText, int time);
|
|
||||||
wxStaticText* initTimer(wxWindow* parent);
|
|
||||||
|
|
||||||
wxSize tableSize;
|
wxSize tableSize;
|
||||||
|
|
||||||
private:
|
void setBG(const wxSize& tableSize);
|
||||||
void drawBG(wxDC& dc);
|
void initScreen(const wxSize& tableSize);
|
||||||
void drawTiles(wxDC& dc);
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
void drawScreen(wxDC& dc);
|
||||||
|
void drawTile(wxDC& dc, const char index);
|
||||||
|
|
||||||
wxImage tileImages[40];
|
wxImage tileImages[40];
|
||||||
|
|
||||||
|
wxBitmap bgBitmap;
|
||||||
|
wxBitmap screenBitmap;
|
||||||
|
|
||||||
|
bool isBgReady = false;
|
||||||
|
bool isScreenReady = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,38 +1,42 @@
|
|||||||
#include "./GamePanel.h"
|
#include "GamePanel.h"
|
||||||
|
|
||||||
#include <wx/dcbuffer.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);
|
SetBackgroundStyle(wxBG_STYLE_PAINT);
|
||||||
|
|
||||||
Bind(wxEVT_PAINT, &GamePanel::OnPaint, this);
|
Bind(wxEVT_PAINT, &GamePanel::OnPaint, this);
|
||||||
Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) -> void {
|
Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) -> void {
|
||||||
this->drawer.tableSize = evt.GetSize();
|
this->controller.resize(evt.GetSize());
|
||||||
});
|
});
|
||||||
|
|
||||||
timer = new wxTimer(this);
|
timer = new wxTimer(this);
|
||||||
timerText = drawer.initTimer(this);
|
Bind(wxEVT_TIMER, &GamePanel::OnTimer, this, timer->GetId());
|
||||||
|
|
||||||
Bind(wxEVT_TIMER, [this](wxTimerEvent& _) -> void {
|
Bind(wxEVT_LEFT_DOWN, &GamePanel::OnClick, this);
|
||||||
controller.stopwatch += 1;
|
|
||||||
drawer.drawTimer(timerText, controller.stopwatch);
|
|
||||||
}, timer->GetId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GamePanel::Start() {
|
void GamePanel::Start() {
|
||||||
wxLogDebug(_("Started game"));
|
|
||||||
|
|
||||||
controller.stopwatch = 0;
|
controller.stopwatch = 0;
|
||||||
timer->Start(1000, wxTIMER_CONTINUOUS);
|
|
||||||
|
|
||||||
Refresh();
|
timer->Start(1000, wxTIMER_CONTINUOUS);
|
||||||
|
sb->SetStatusText(LTimeToStr(controller.stopwatch));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GamePanel::OnPaint(wxPaintEvent& _) {
|
void GamePanel::OnPaint(wxPaintEvent& _) {
|
||||||
wxAutoBufferedPaintDC dc(this);
|
wxAutoBufferedPaintDC dc(this);
|
||||||
|
|
||||||
drawer.drawTable(dc);
|
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));
|
||||||
|
}
|
12
GamePanel.h
12
GamePanel.h
@ -1,12 +1,12 @@
|
|||||||
#ifndef GRAPHICS_H
|
#ifndef GRAPHICS_H
|
||||||
#define GRAPHICS_H
|
#define GRAPHICS_H
|
||||||
|
|
||||||
#include "./wxw.h"
|
#include "wxw.h"
|
||||||
|
|
||||||
#include <wx/stopwatch.h>
|
#include <wx/stopwatch.h>
|
||||||
|
|
||||||
#include "./Drawer.h"
|
#include "Drawer.h"
|
||||||
#include "./Controller.h"
|
#include "Controller.h"
|
||||||
|
|
||||||
class GamePanel : public wxPanel {
|
class GamePanel : public wxPanel {
|
||||||
public:
|
public:
|
||||||
@ -19,8 +19,10 @@ private:
|
|||||||
Controller controller;
|
Controller controller;
|
||||||
|
|
||||||
void OnPaint(wxPaintEvent& _);
|
void OnPaint(wxPaintEvent& _);
|
||||||
|
void OnTimer(wxTimerEvent& _);
|
||||||
wxStaticText* timerText;
|
void OnClick(wxMouseEvent& _);
|
||||||
|
|
||||||
|
wxStatusBar* sb;
|
||||||
wxTimer* timer;
|
wxTimer* timer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user