-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
116 lines (95 loc) · 2.81 KB
/
renderer.cpp
File metadata and controls
116 lines (95 loc) · 2.81 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "renderer.h"
Renderer::Renderer(Board &board) : numRows(7), boxSize(200)
{
InitSprites();
InitGrid();
UpdateRenderer(board);
if (!font.loadFromFile("assets/JETBRAINSMONONERDFONT-REGULAR.ttf"))
{
std::cout << "Couldn't load font!" << "\n";
}
}
void Renderer::InitSprites()
{
if (!whiteCoinTexture.loadFromFile("assets/whiteCoin.png"))
{
cout << "Couldnt load whiteCoin texture!" << endl;
}
if (!blackCoinTexture.loadFromFile("assets/blackCoin.png"))
{
cout << "Couldnt load blackCoin texture!" << endl;
}
if (!transparentCoinTexture.loadFromFile("assets/transparent.jpg"))
{
cout << "Couldnt load transparentCoin texture!" << endl;
}
whiteCoinSprite.setTexture(whiteCoinTexture);
blackCoinSprite.setTexture(blackCoinTexture);
transparentCoinSprite.setTexture(transparentCoinTexture);
// transparentCoinSprite.setColor(sf::Color::White);
}
// initialize grid once
void Renderer::InitGrid()
{
grid.resize(numRows);
for (int x = 0; x < numRows; x++)
{
grid[x] = GridCell(x, boxSize, font);
}
}
void Renderer::Render(sf::RenderWindow &window)
{
for (int x = 0; x < numRows; x++)
{
window.draw(grid[x].shape);
window.draw(spriteList[x]);
window.draw(grid[x].cellNum);
}
}
void Renderer::UpdateRenderer(Board& board)
{
// store the isSelected states in a vector
selectedStates.clear();
for (const auto &cell : grid)
{
selectedStates.push_back(cell.isSelected);
}
// reinitialize the coin sprite lists and grid vector
spriteList.resize(numRows);
// grid.resize(numRows);
// set the sprites and grid properties
for (int x = 0; x < numRows; x++)
{
grid[x].UpdateGridCell(x, boxSize, font);
// if (x < selectedStates.size())
// {
// grid[x].isSelected = selectedStates[x]; // for safety
// }
// set grid color based on the cell color
Color coinColor = board.GetCoinFromIndex(x).GetEnumColor();
switch (coinColor)
{
case (White):
spriteList[x] = (whiteCoinSprite);
break;
case (Black):
spriteList[x] = (blackCoinSprite);
break;
default:
spriteList[x] = (transparentCoinSprite);
break;
}
sf::Vector2u size = blackCoinTexture.getSize();
spriteList[x].setPosition(sf::Vector2f(x * boxSize, 0));
spriteList[x].setScale(sf::Vector2f(static_cast<float>( boxSize ) / size.x, (static_cast<float>( boxSize ) / size.y)));
//updating the cellNumColors
// grid[x].UpdateCellNumColor();
}
}
void Renderer::UnselectAll()
{
for (int i = 0; i < grid.size(); i++)
{
grid[i].isSelected = false;
}
}