Skip to content

Commit 8ab9463

Browse files
committed
Performance and misc changes
1 parent fca4658 commit 8ab9463

File tree

7 files changed

+74
-70
lines changed

7 files changed

+74
-70
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Blockudoku++
2-
This is a very basic copy of the Blockudoku mobile game ([Android](https://play.google.com/store/apps/details?id=com.easybrain.block.puzzle.games&hl=en&gl=US), [IOS](https://apps.apple.com/us/app/blockudoku-block-puzzle-game/id1452227871)) written in C++14 using SFML, originally made as the final project for the university's OOP class.
2+
This game is a copy of the Blockudoku mobile game ([Android](https://play.google.com/store/apps/details?id=com.easybrain.block.puzzle.games&hl=en&gl=US), [IOS](https://apps.apple.com/us/app/blockudoku-block-puzzle-game/id1452227871)) written in C++14 using SFML, originally made as the final project for the university's OOP class.
33

44
## Resources
55
The game uses audio effects from the original Blockudoku game.

include/pickupBoard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
#include "score.h"
88

99
#include <SFML/System/Vector2.hpp>
10+
#include <SFML/Graphics/VertexArray.hpp>
1011
#include <SFML/Graphics/Rect.hpp>
1112

1213
#include <array>
1314

1415
class PickupBoard : Drawable, Eventer
1516
{
1617
private:
18+
sf::VertexArray borders = sf::VertexArray(sf::Lines, 2 * 2 + 4 * 2);
19+
1720
std::array<Block*, 3> pickupableBlocks = { nullptr };
1821

1922
int pickedUpIndex = -1;

include/table.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "impl.h"
44
#include "block.h"
55
#include "score.h"
6+
#include "spacing.h"
7+
8+
#include <SFML/Graphics/VertexArray.hpp>
69

710
#include <memory>
811

@@ -13,6 +16,9 @@ class Table : Drawable
1316
enum class mark { square, vline, hline };
1417

1518
private:
19+
sf::VertexArray minorGrid = sf::VertexArray(sf::Lines, 2 * (TABLE_SIZE * 2 - 2));
20+
sf::VertexArray majorGrid = sf::VertexArray(sf::Lines, 4 * 2 * 2);
21+
1622
struct completetion {
1723
mark type;
1824
unsigned x = 0, y = 0; //vline uses only x, hline only y and square both

src/block.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ void Block::setFloating(bool isFloating) {
131131

132132
void Block::draw(sf::RenderWindow& window) {
133133
sf::VertexArray borders(sf::Lines, 4 * 2);
134+
sf::Vector2f cellPosition;
134135

135136
for (unsigned l = 0; l < structure.size(); l++) {
136137
for (unsigned c = 0; c < structure[l].size(); c++) {
137138
if (structure[l][c] == 1) {
138-
sf::Vector2f cellPosition;
139-
140139
if(floating)
141140
cellPosition = { position.x + (4 + CELL_SPACING) * l * scale, position.y + (4 + CELL_SPACING) * c * scale };
142141
else

src/pickupBoard.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@
33
#include "colors.h"
44
#include "audio.h"
55

6-
#include <SFML/Graphics/VertexArray.hpp>
76
#include <SFML/Window/Event.hpp>
87
#include <SFML/Window/Mouse.hpp>
98

109
PickupBoard::PickupBoard(Table& theTable, Score& theScore) : theTable(theTable), theScore(theScore)
1110
{
1211
generateBlocks();
12+
13+
for (unsigned i = 0; i < 2; i++) {
14+
float rowY = BoardHeight * i * 1.f;
15+
borders[i * 2].position = { startPosition.y, rowY + startPosition.x };
16+
borders[i * 2].color = COLOR_GRAY;
17+
borders[i * 2 + 1].position = { BoardLength + startPosition.y, rowY + startPosition.x };
18+
borders[i * 2 + 1].color = COLOR_GRAY;
19+
}
20+
21+
unsigned gridPositionOffset = 4;
22+
for (unsigned i = 0; i < 4; i++) {
23+
float rowX = BoardHeight * i * 1.f;
24+
borders[gridPositionOffset + i * 2].position = { rowX + startPosition.y, startPosition.x };
25+
borders[gridPositionOffset + i * 2].color = COLOR_GRAY;
26+
borders[gridPositionOffset + i * 2 + 1].position = { rowX + startPosition.y, BoardHeight + startPosition.x };
27+
borders[gridPositionOffset + i * 2 + 1].color = COLOR_GRAY;
28+
}
1329
}
1430

1531
PickupBoard::~PickupBoard()
@@ -63,25 +79,6 @@ bool PickupBoard::canAnyBlocksBePlaced() {
6379

6480
void PickupBoard::draw(sf::RenderWindow& window)
6581
{
66-
sf::VertexArray borders(sf::Lines, 2 * 2 + 4 * 2);
67-
68-
for (unsigned i = 0; i < 2; i++) {
69-
float rowY = BoardHeight * i * 1.f;
70-
borders[i * 2].position = { startPosition.y, rowY + startPosition.x };
71-
borders[i * 2].color = COLOR_GRAY;
72-
borders[i * 2 + 1].position = { BoardLength + startPosition.y, rowY + startPosition.x };
73-
borders[i * 2 + 1].color = COLOR_GRAY;
74-
}
75-
76-
unsigned gridPositionOffset = 4;
77-
for (unsigned i = 0; i < 4; i++) {
78-
float rowX = BoardHeight * i * 1.f;
79-
borders[gridPositionOffset + i * 2].position = { rowX + startPosition.y, startPosition.x };
80-
borders[gridPositionOffset + i * 2].color = COLOR_GRAY;
81-
borders[gridPositionOffset + i * 2 + 1].position = { rowX + startPosition.y, BoardHeight + startPosition.x };
82-
borders[gridPositionOffset + i * 2 + 1].color = COLOR_GRAY;
83-
}
84-
8582
window.draw(borders);
8683

8784
for (unsigned i = 0; i < 3; i++) {

src/score.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void Score::draw(sf::RenderWindow& window) {
5252
"\n Blocks used: " + std::to_string(placed) +
5353
"\n Time: " + std::to_string((unsigned)timePlayed) + " seconds" +
5454
"\n APM: " + std::to_string(timePlayed / placed) +
55-
"\n SPM: " + std::to_string(timePlayed / score) +
55+
"\n SPM: " + std::to_string(score > 0 ? timePlayed / score : 0) +
5656
"\nThe most popular block: ";
5757

5858
theText.setString(endGameStatsString);

src/table.cpp

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include <SFML/Graphics/RectangleShape.hpp>
88
#include <SFML/Graphics/Rect.hpp>
99

10-
#include <array>
11-
1210
Table::completetion::completetion(mark theType, unsigned a, unsigned b) {
1311
type = theType;
1412

@@ -28,7 +26,42 @@ Table::completetion::completetion(mark theType, unsigned a, unsigned b) {
2826

2927
Table::Table(Score& theScore) : theScore(theScore)
3028
{
29+
sf::Vector2f startPosition = { TABLE_POSITION_X, TABLE_POSITION_Y };
30+
float lineLength = CELL_SPACING * TABLE_SIZE;
31+
32+
for (unsigned i = 0; i < TABLE_SIZE - 1; i++) {
33+
float rowY = CELL_SPACING * (i + 1.f);
34+
minorGrid[i * 2].position = { startPosition.y, rowY + startPosition.y };
35+
minorGrid[i * 2].color = COLOR_LIGHT_BLUE;
36+
minorGrid[i * 2 + 1].position = { lineLength + startPosition.y, rowY + startPosition.y };
37+
minorGrid[i * 2 + 1].color = COLOR_LIGHT_BLUE;
38+
}
39+
40+
unsigned gridPositionOffset = (TABLE_SIZE - 1) * 2;
41+
for (unsigned i = 0; i < TABLE_SIZE - 1; i++) {
42+
float rowX = CELL_SPACING * (i + 1.f);
43+
minorGrid[gridPositionOffset + i * 2].position = { rowX + startPosition.x, startPosition.x };
44+
minorGrid[gridPositionOffset + i * 2].color = COLOR_LIGHT_BLUE;
45+
minorGrid[gridPositionOffset + i * 2 + 1].position = { rowX + startPosition.x, lineLength + startPosition.x };
46+
minorGrid[gridPositionOffset + i * 2 + 1].color = COLOR_LIGHT_BLUE;
47+
}
3148

49+
for (unsigned i = 0; i < 4; i++) {
50+
float rowY = (CELL_SPACING * 3.f) * i;
51+
majorGrid[i * 2].position = { startPosition.y, rowY + startPosition.y };
52+
majorGrid[i * 2].color = COLOR_BLACK;
53+
majorGrid[i * 2 + 1].position = { lineLength + startPosition.y, rowY + startPosition.y };
54+
majorGrid[i * 2 + 1].color = COLOR_BLACK;
55+
}
56+
57+
gridPositionOffset = 4 * 2;
58+
for (unsigned i = 0; i < 4; i++) {
59+
float rowX = (CELL_SPACING * 3.f) * i;
60+
majorGrid[gridPositionOffset + i * 2].position = { rowX + startPosition.x, startPosition.x };
61+
majorGrid[gridPositionOffset + i * 2].color = COLOR_BLACK;
62+
majorGrid[gridPositionOffset + i * 2 + 1].position = { rowX + startPosition.x, lineLength + startPosition.x };
63+
majorGrid[gridPositionOffset + i * 2 + 1].color = COLOR_BLACK;
64+
}
3265
}
3366

3467
sf::Vector2i Table::mousePositionToCellPosition(const sf::Vector2f& mousePosition)
@@ -159,6 +192,13 @@ void Table::applyBlock(Block& theBlock, const sf::Vector2i& tableCellCoords) {
159192
if(completedMarks->size() > 1) //give one point for getting more marks at one time
160193
theScore.addToCombo(1);
161194

195+
for (const auto& mark : *completedMarks) {
196+
if(mark.type == mark::square)
197+
theScore.addCompletionSquare();
198+
else
199+
theScore.addCompletionLine();
200+
}
201+
162202
executeCompletetionsWith(completedMarks, cell::empty);
163203
}
164204
}
@@ -239,14 +279,12 @@ bool Table::canBlockBePlaced(Block& theBlock)
239279

240280
void Table::draw(sf::RenderWindow& window)
241281
{
242-
sf::Vector2f startPosition = { TABLE_POSITION_X, TABLE_POSITION_Y };
243-
float lineLength = CELL_SPACING * TABLE_SIZE;
282+
sf::RectangleShape cell;
283+
cell.setSize({ CELL_SPACING - 2, CELL_SPACING - 2 });
244284

245285
for (unsigned l = 0; l < 9; l++) {
246286
for (unsigned c = 0; c < 9; c++) {
247-
sf::RectangleShape cell;
248-
cell.setPosition({ startPosition.x + CELL_SPACING * l + 1, startPosition.y + CELL_SPACING * c + 1 });
249-
cell.setSize({ CELL_SPACING - 2, CELL_SPACING - 2 });
287+
cell.setPosition({ TABLE_POSITION_X + CELL_SPACING * l + 1.f, TABLE_POSITION_Y + CELL_SPACING * c + 1.f });
250288

251289
switch (cellTable[l][c]) {
252290
case cell::empty:
@@ -270,45 +308,6 @@ void Table::draw(sf::RenderWindow& window)
270308
}
271309
} //DRAW CELLS END
272310

273-
sf::VertexArray minorGrid(sf::Lines, 2 * (TABLE_SIZE * 2 - 2));
274-
275-
for (unsigned i = 0; i < TABLE_SIZE - 1; i++) {
276-
float rowY = CELL_SPACING * (i + 1.f);
277-
minorGrid[i * 2].position = { startPosition.y, rowY + startPosition.y };
278-
minorGrid[i * 2].color = COLOR_LIGHT_BLUE;
279-
minorGrid[i * 2 + 1].position = { lineLength + startPosition.y, rowY + startPosition.y };
280-
minorGrid[i * 2 + 1].color = COLOR_LIGHT_BLUE;
281-
}
282-
283-
unsigned gridPositionOffset = (TABLE_SIZE - 1) * 2;
284-
for (unsigned i = 0; i < TABLE_SIZE - 1; i++) {
285-
float rowX = CELL_SPACING * (i + 1.f);
286-
minorGrid[gridPositionOffset + i * 2].position = { rowX + startPosition.x, startPosition.x };
287-
minorGrid[gridPositionOffset + i * 2].color = COLOR_LIGHT_BLUE;
288-
minorGrid[gridPositionOffset + i * 2 + 1].position = { rowX + startPosition.x, lineLength + startPosition.x };
289-
minorGrid[gridPositionOffset + i * 2 + 1].color = COLOR_LIGHT_BLUE;
290-
}
291-
292311
window.draw(minorGrid);
293-
294-
sf::VertexArray majorGrid(sf::Lines, 4 * 2 * 2);
295-
296-
for (unsigned i = 0; i < 4; i++) {
297-
float rowY = (CELL_SPACING * 3.f) * i;
298-
majorGrid[i * 2].position = { startPosition.y, rowY + startPosition.y };
299-
majorGrid[i * 2].color = COLOR_BLACK;
300-
majorGrid[i * 2 + 1].position = { lineLength + startPosition.y, rowY + startPosition.y };
301-
majorGrid[i * 2 + 1].color = COLOR_BLACK;
302-
}
303-
304-
gridPositionOffset = 4 * 2;
305-
for (unsigned i = 0; i < 4; i++) {
306-
float rowX = (CELL_SPACING * 3.f) * i;
307-
majorGrid[gridPositionOffset + i * 2].position = { rowX + startPosition.x, startPosition.x };
308-
majorGrid[gridPositionOffset + i * 2].color = COLOR_BLACK;
309-
majorGrid[gridPositionOffset + i * 2 + 1].position = { rowX + startPosition.x, lineLength + startPosition.x };
310-
majorGrid[gridPositionOffset + i * 2 + 1].color = COLOR_BLACK;
311-
}
312-
313312
window.draw(majorGrid); //DRAW GRID END
314313
}

0 commit comments

Comments
 (0)