-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas.cpp
More file actions
95 lines (76 loc) · 2.18 KB
/
Copy pathCanvas.cpp
File metadata and controls
95 lines (76 loc) · 2.18 KB
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
#include "Canvas.h"
#include <stdexcept>
#include <algorithm>
#include "CanvasImpl.h"
#include "Graphics.h"
#include "Command.h"
#include "CommandListener.h"
Canvas::Canvas()
: impl(std::make_unique<CanvasImpl>(this)),
graphics(std::make_unique<Graphics>(impl->getRenderer())),
commandListener(nullptr) {}
Canvas::~Canvas() = default;
int Canvas::getWidth() {
return impl->getWidth();
}
int Canvas::getHeight() {
return impl->getHeight();
}
void Canvas::setWindowTitle(const std::string& title) {
impl->setWindowTitle(title);
}
bool Canvas::isShown() {
return true;
}
CanvasImpl* Canvas::getCanvasImpl() {
return impl.get();
}
void Canvas::repaint() {
// Подготовка кадра: вызываем пользовательский метод paint
paint(graphics.get());
}
void Canvas::handleEventsAndPresent() {
// 1. Обработка событий SDL (клавиатура, мышь, окно)
impl->processEvents();
// 2. Отображение кадра (SDL_RenderPresent)
impl->repaint();
}
void Canvas::serviceRepaints() {
// Заглушка
}
int Canvas::getGameAction(int keyCode) {
switch (keyCode) {
case Keys::UP:
case Keys::DOWN:
case Keys::LEFT:
case Keys::RIGHT:
case Keys::FIRE:
return keyCode;
default:
throw std::runtime_error("getGameAction(" + std::to_string(keyCode) + ") не реализован!");
}
}
void Canvas::removeCommand(Command* command) {
currentCommands.erase(command);
}
void Canvas::addCommand(Command* command) {
currentCommands.insert(command);
}
void Canvas::setCommandListener(CommandListener* listener) {
commandListener = listener;
}
void Canvas::publicKeyPressed(int keyCode) {
keyPressed(keyCode);
}
void Canvas::publicKeyReleased(int keyCode) {
keyReleased(keyCode);
}
void Canvas::pressedEsc() {
// Обработка кнопки "Назад" (ESC) для меню и команд
for (const auto& command : currentCommands) {
if (command->type == Command::Type::BACK || currentCommands.size() == 1) {
commandListener->commandAction(command, this);
return;
}
}
}