-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.cpp
More file actions
102 lines (91 loc) · 3.46 KB
/
tile.cpp
File metadata and controls
102 lines (91 loc) · 3.46 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
97
98
99
100
101
102
#include "tile.h"
tile::tile() {} // This guy is just used to create an empty object
tile::tile(SDL_Renderer *renderer, TTF_Font *font, int x, int y, uint8_t val,
int boardSize) {
this->renderer = renderer;
this->font = font;
this->x = x;
this->y = y;
set_value(val);
this->boardSize = boardSize;
// Tile size = (screen size - outer padding) / #tiles - tile padding
width = (options::screenWidth - 50) / boardSize - 10;
height = (options::screenHeight - 50) / boardSize - 10;
// Left outer padding + #x * tile width + #tile * tile padding + offset
xPos = 25 + x * width + x * 10 + 5;
yPos = 25 + y * height + y * 10 + 25;
innerRect = {xPos + 5, yPos + 5, width - 10, height - 10};
outerRect = {xPos, yPos, width, height};
}
void tile::set_value(uint8_t value) {
this->value = value;
set_color(value);
}
void tile::render() {
if (value > 0) {
SDL_Surface *textSurface = TTF_RenderText_Shaded(
font, std::to_string((int)std::pow(2, value)).c_str(), textColor,
innerColor);
if (textSurface == NULL) {
cout << "Unable to create surface from text! TTF Error: "
<< TTF_GetError() << endl;
} else {
SDL_Texture *textTexture =
SDL_CreateTextureFromSurface(renderer, textSurface);
if (textTexture == NULL) {
cout << "Unable to create text texture! TTF Error: " << TTF_GetError()
<< endl;
} else {
if (textSurface->w > textSurface->h) {
textRect = {xPos + 5, yPos + 5, width - 10,
(int)(((float)(width - 10) / (float)textSurface->w) *
(float)textSurface->h)};
} else {
int wid = (int)(((float)(height - 10) / (float)textSurface->h) *
(float)textSurface->w);
textRect = {xPos + ((width - 10) - wid) / 2 + 5, yPos + 5, wid,
(height - 10)};
}
SDL_FreeSurface(textSurface);
SDL_SetRenderDrawColor(renderer, outerColor.r, outerColor.g,
outerColor.b, outerColor.a);
SDL_RenderFillRect(renderer, &outerRect);
SDL_SetRenderDrawColor(renderer, innerColor.r, innerColor.g,
innerColor.b, innerColor.a);
SDL_RenderFillRect(renderer, &innerRect);
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
}
}
} else {
SDL_SetRenderDrawColor(renderer, colors::black.r, colors::black.g,
colors::black.b, colors::black.a);
SDL_RenderFillRect(renderer, &outerRect);
}
}
void tile::set_color(uint8_t value) {
int row = value % 8;
int col = 0;
if (value >= 8) {
col = (value - row) / 8;
}
innerColor = colors::colorArray[row];
textColor = colors::textColorArray[row];
outerColor = colors::colorArray[col];
}
void tile::set_size(int screenWidth, int screenHeight) {
if (screenWidth > screenHeight) {
width = (screenHeight - 50) / boardSize - 10;
height = (screenHeight - 50) / boardSize - 10;
offset = (screenWidth - screenHeight) / 2;
xPos = 25 + x * width + x * 10 + 5 + offset;
yPos = 25 + y * height + y * 10 + 25;
} else {
width = (screenWidth - 50) / boardSize - 10;
height = (screenWidth - 50) / boardSize - 10;
offset = (screenHeight - screenWidth) / 2;
xPos = 25 + x * width + x * 10 + 5;
yPos = 25 + y * height + y * 10 + 25 + offset;
}
innerRect = {xPos + 5, yPos + 5, width - 10, height - 10};
outerRect = {xPos, yPos, width, height};
}