-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
96 lines (83 loc) · 2.87 KB
/
Copy pathTable.cpp
File metadata and controls
96 lines (83 loc) · 2.87 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
96
#include "Table.h"
#include <iostream>
#include <SDL_image.h>
Table::Table(float width, float height, const char* texturePath, SDL_Renderer* renderer)
: width(width), height(height), texture(nullptr), renderer(renderer) { // Èíèöèàëèçàöèÿ renderer
// Çàãðóçêà òåêñòóðû ñòîëà
SDL_Surface* surface = IMG_Load(texturePath);
if (!surface) {
std::cerr << "Unable to load texture: " << IMG_GetError() << std::endl;
}
else {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (!texture) {
std::cerr << "Unable to create texture: " << SDL_GetError() << std::endl;
}
}
}
Table::~Table() {
if (texture) {
SDL_DestroyTexture(texture);
texture = nullptr;
}
}
void Table::init() {
// Èíèöèàëèçàöèÿ ëóç
addPocket(Pocket(80, 80, 30));
addPocket(Pocket(width - 80, 80, 30));
addPocket(Pocket(80, height - 80, 30));
addPocket(Pocket(width - 80, height - 80, 30));
addPocket(Pocket(width / 2, 80, 30));
addPocket(Pocket(width / 2, height - 80, 30));
// Èíèöèàëèçàöèÿ ñòåíîê
addWall(Wall(Vector2D(50, 50), Vector2D(width - 50, 50))); // Âåðõíÿÿ ñòåíêà
addWall(Wall(Vector2D(50, height - 50), Vector2D(width - 50, height - 50))); // Íèæíÿÿ ñòåíêà
addWall(Wall(Vector2D(50, 50), Vector2D(50, height - 50))); // Ëåâàÿ ñòåíêà
addWall(Wall(Vector2D(width - 50, 50), Vector2D(width - 50, height - 50))); // Ïðàâàÿ ñòåíêà
}
void Table::draw(SDL_Renderer* renderer) {
// Îòðèñîâêà ñòîëà
if (texture) {
SDL_Rect destRect;
destRect.x = 0;
destRect.y = 0;
destRect.w = static_cast<int>(width);
destRect.h = static_cast<int>(height);
SDL_RenderCopy(renderer, texture, NULL, &destRect);
}
else {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_Rect tableRect = { 0, 0, static_cast<int>(width), static_cast<int>(height) };
SDL_RenderFillRect(renderer, &tableRect);
}
// Îòðèñîâêà ëóç
for (auto& pocket : pockets) {
pocket.draw(renderer);
}
// Îòðèñîâêà ñòåíîê
SDL_SetRenderDrawColor(renderer, 139, 69, 19, 255);
for (auto& wall : walls) {
SDL_RenderDrawLine(renderer,
static_cast<int>(wall.getStart().getX()), static_cast<int>(wall.getStart().getY()),
static_cast<int>(wall.getEnd().getX()), static_cast<int>(wall.getEnd().getY()));
}
}
void Table::addPocket(const Pocket& pocket) {
pockets.push_back(pocket);
}
void Table::addWall(const Wall& wall) {
walls.push_back(wall);
}
std::vector<Pocket>& Table::getPockets() {
return pockets;
}
std::vector<Wall>& Table::getWalls() {
return walls;
}
float Table::getWidth() {
return width;
}
float Table::getHeight() {
return height;
}