-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.cpp
111 lines (94 loc) · 3.43 KB
/
Counter.cpp
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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <string>
#pragma once
class Counter {
private:
int count;
std::vector<sf::Sprite> digitSprites;
sf::Texture digitsTexture;
int posY;
// Function to update the digit sprites based on the count
void updateDigitSprites() {
int tempCount = count;
// Clear existing digit sprites
digitSprites.clear();
// Handle negative count
bool isNegative = false;
if (tempCount < 0) {
isNegative = true;
tempCount = -tempCount;
}
// Create digit sprites
do {
int digit = tempCount % 10; // Extract last digit
sf::Sprite digitSprite(digitsTexture);
digitSprite.setTextureRect(sf::IntRect(digit * 21, 0, 21, 32)); // Set texture rect for current digit
digitSprites.push_back(digitSprite);
tempCount /= 10; // Remove last digit
} while (tempCount > 0);
// Add '-' sprite if count is negative
if (isNegative) {
sf::Sprite minusSprite(digitsTexture);
minusSprite.setTextureRect(sf::IntRect(10 * 21, 0, 21, 32)); // '-' sprite
digitSprites.push_back(minusSprite);
}
// Reverse the digit sprites vector to position them correctly
std::reverse(digitSprites.begin(), digitSprites.end());
// Set positions for digit sprites
int numDigits = digitSprites.size();
int digitWidth = 21; // Width of each digit sprite
int totalWidth = numDigits * digitWidth; // Total width occupied by digits
int posX = 12; // Starting position for digits
// Adjust starting position for negative single-digit numbers
if (isNegative && numDigits == 1) {
posX = 33; // Move the starting position to the right
}
// Position digits correctly
for (int i = 0; i < numDigits; ++i) {
digitSprites[i].setPosition(posX, (posY + 0.5) * 32 + 16); // Adjust position for each digit
posX += digitWidth; // Move to the right for the next digit
}
}
public:
// Constructor
Counter(int count, int y) : count(count), posY(y) {
// Load digits texture
if (!digitsTexture.loadFromFile("./images/digits.png")) {
std::cerr << "Failed to load digits texture!" << std::endl;
return;
}
// Update digit sprites initially
updateDigitSprites();
}
void setText(sf::Text &text, float x, float y){
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width/2.0f,textRect.top + textRect.height/2.0f);
text.setPosition(sf::Vector2f(x, y));
}
// Function to increase the counter
void increase() {
count++;
updateDigitSprites();
}
// Function to decrease the counter
void decrease() {
count--;
updateDigitSprites();
}
// Function to get the current count
int getCount() const {
return count;
}
void setCount(int m) {
this->count = m;
updateDigitSprites();
}
// Function to draw the counter digits
void draw(sf::RenderWindow& window) {
for (int i = 0; i < digitSprites.size(); ++i) {
window.draw(digitSprites[i]);
}
}
};