SFML Button Class HELP!

I was wondering if there would be any way to help me make a class for all of my buttons. Every sprite is just a button. Is there any possible way to make it so that I have one class for every button? The code doesn't work because I had to cut alot of it out for it to fit. (NOTE: This code uses SFML.) Thanks!

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include<iostream>
#include<SFML/Audio.hpp>
#include<SFML/Graphics.hpp>

using namespace std;
using namespace sf;

int main(){

    string input;

  
    int frame = 0;
    int row = 0;
    int frame2 = 0;
    int row2 = 0;
    int frame3 = 0;
    int row3 = 0;
    int frame5 = 0;
    int row5 = 0;
    int frame6 = 0;
    int row6 = 0;
    int frame9 = 0;
    int row9 = 0;
    int frame10 = 0;
    int row10 = 0;
    int counter = 0;
    bool pressed = false;
    bool pressed2 = false;
    bool pressed3 = false;
    bool pressed5 = false;
    bool pressed6 = false;
    bool pressed9 = false;
    bool pressed10 = false;
    bool leftButtonDown = false;
    bool RightButtonDown = false;
    bool leftButtonDown2 = false;
    bool RightButtonDown2 = false;
    bool leftButtonDown3 = false;
    bool RightButtonDown3 = false;
    bool leftButtonDown5 = false;
    bool RightButtonDown5 = false;
    bool leftButtonDown6 = false;
    bool RightButtonDown6 = false;
    bool leftButtonDown9 = false;
    bool RightButtonDown9 = false;
    bool leftButtonDown10 = false;
    bool RightButtonDown10 = false;

   sf::RenderWindow window(sf::VideoMode(800,800), "My window");
    window.setFramerateLimit(120);

   Mouse theMouse;
   Texture spriteSheet;
   Texture spriteSheet2;
   Texture spriteSheet3;
   Texture spriteSheet5;
   Texture spriteSheet6;
   Texture spriteSheet9;
   Texture spriteSheet10;
   Sprite MySprite;
   Sprite MySprite2;
   Sprite MySprite3;
   Sprite MySprite5;
   Sprite MySprite6;
   Sprite MySprite9;
   Sprite MySprite10;

   if(!spriteSheet.loadFromFile("drumbutton.png"))
            cout << "Fail at spriting! Press F to pay respects." << endl;
            MySprite.setTexture(spriteSheet);

   if(!spriteSheet2.loadFromFile("drumbutton2.png"))
            cout << "Fail at spriting! Press F to pay respects." << endl;
            MySprite2.setTexture(spriteSheet2);

        Music music;
        Music music2;
        Music music3;
        Music music5;
        Music music6;
  

   if(!music.openFromFile("classic 1.wav"))
        return -1; // error

   if(!music2.openFromFile("classic 2.wav"))
        return -1; // error

   if(!music3.openFromFile("classic 3.wav"))
        return -1; // error

   while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

   MySprite.setTextureRect(IntRect(128 * frame, 162.5 * row, 128, 162.5));
   MySprite2.setTextureRect(IntRect(128 * frame2, 162.5 * row2, 128, 162.5));
   MySprite2.setPosition(0,150);
   MySprite3.setTextureRect(IntRect(128 * frame3, 162.5 * row3, 128, 162.5));
   MySprite3.setPosition(0,300);

 
   if(Mouse::isButtonPressed(Mouse::Left) and leftButtonDown == false){
        if(MySprite.getGlobalBounds().contains(theMouse.getPosition(window).x,theMouse.getPosition(window).y) == true){
            music.setVolume(50.f);
            leftButtonDown = true;
            pressed = true;
            RightButtonDown = true;

            if(pressed2 == true){
                    RightButtonDown2 = false;
                    music2.setVolume(0);
                    pressed2 = false;
                    leftButtonDown2 = false;

                    if(RightButtonDown2 == false){
                        row2 = (row2 + 1) % 2;
                        frameCounter = 0;
                    }

            }
            if(pressed3 == true){
                    RightButtonDown3 = false;
                    music3.setVolume(0);
                    pressed3 = false;
                    leftButtonDown3 = false;

                    if(RightButtonDown3 == false){
                        row3 = (row3 + 1) % 2;
                        frameCounter = 0;
                    }

            }


            if(leftButtonDown == true){
                row = (row + 1) % 2;
                frameCounter = 0;
            }
        }
   }
   if(Mouse::isButtonPressed(Mouse::Left) and leftButtonDown2 == false){
        if(MySprite2.getGlobalBounds().contains(theMouse.getPosition(window).x,theMouse.getPosition(window).y) == true){
            music2.setVolume(50.f);
            leftButtonDown2 = true;
            pressed2 = true;
            RightButtonDown2 = true;

            if(pressed == true){
                    RightButtonDown = false;
                    music.setVolume(0);
                    pressed = false;
                    leftButtonDown = false;

                    if(RightButtonDown == false){
                        row = (row + 1) % 2;
                        frameCounter = 0;
                    }

            }
            if(pressed3 == true){
                    RightButtonDown3 = false;
                    music3.setVolume(0);
                    pressed3 = false;
                    leftButtonDown3 = false;

                    if(RightButtonDown3 == false){
                        row3 = (row3 + 1) % 2;
                        frameCounter = 0;
                    }

            }

            if(leftButtonDown2 == true){
                row2 = (row2 + 1) % 2;
                frameCounter = 0;
            }
        }
   }
      if(Mouse::isButtonPressed(Mouse::Left) and leftButtonDown3 == false){
        if(MySprite3.getGlobalBounds().contains(theMouse.getPosition(window).x,theMouse.getPosition(window).y) == true){
            music3.setVolume(50.f);
            leftButtonDown3 = true;
            pressed3 = true;
            RightButtonDown3 = true;

            if(pressed == true){
                    RightButtonDown = false;
                    music.setVolume(0);
                    pressed = false;
                    leftButtonDown = false;

                    if(RightButtonDown == false){
                        row = (row + 1) % 2;
                        frameCounter = 0;
                    }

            }
            if(pressed2 == true){
                    RightButtonDown2 = false;
                    music2.setVolume(0);
                    pressed2 = false;
                    leftButtonDown2 = false;

                    if(RightButtonDown2 == false){
                        row2 = (row2 + 1) % 2;
                        frameCounter = 0;
                    }

            }

            if(leftButtonDown3 == true){
                row3 = (row3 + 1) % 2;
                frameCounter = 0;
            }
        }
   }
   if(Mouse::isButtonPressed(Mouse::Left) and leftButtonDown5 == false){

 
   
        frameCounter++;
        window.clear();
        window.draw(MySprite);
        window.draw(MySprite2);
        window.draw(MySprite3);
        window.draw(MySprite5);
        window.draw(MySprite6);
        window.draw(MySprite9);
        window.draw(MySprite10);
        window.display();
   }
    return 0;
}
Last edited on
There will likely be several posts in this conversation, back and forth.

Start with an empty class.

Examine this code for all of those things you have 10 of. They are the data members for your new class.

Create a constructor function. It likely must take several parameters, the name of the sprint graphic being one of them.

That's a start. As to the rest, it seems to me you're thinking in terms of one function. That makes it long and confusing. Once you have a class representing the button/sprite combination, you'll need to consider the overall operation of your program. This appears to run straight through a series of processes and then end. I doubt that is your actual plan. I can't tell from out here, but it is likely you need a plan where after all the initialization stuff completes (which should probably be an initialization function main calls), you'll need some kind of loop which lets the user continuously select options until they choose to exit (a button or some command I don't see represented here).

Now, I realize it is probably that your loop which starts at line 93 may well be that loop I speak of, but with such a long main function there's no sense of obvious design.

If I'm correct, this loop is typical of an animation loop, or any kind of design where the program repeatedly checks input/provides output until exit is commanded by the user.

For that to be clear it is good design, and quite customary, to create a loop that just does those few things, and can be understood in a short bit of code. In a typical animation loop we see things as simple as:

1
2
3
4
5
6
7
8
9

bool exit_requested = false;

while( windows.IsOpen() && !exit_requested )
  {
   exit_requested = runloop();
  }



This makes it clear how the program is animated. If there are other signals to stop this loop, it can be from the return of runloop. This is only a hint, not a design for you to follow.

The runloop function itself shouldn't just be all the code inside the loop dumped there. You need to consider structure here, too.

It appears that you poll for input at the top of this loop. If the window is closed, this code lets the rest of the material execute. It is not likely to be of any purpose (and might crash) because the window is already closed. This step should probably "return true" if you follow the notion I presented.

It appears that you check the various buttons. When you have a button class, you'll still create the 10 buttons, but now they are likely going to be stored in some kind of a container, perhaps a std::vector.

I'll stop here because I don't see evidence yet that a std::vector is familiar to you, and that would stop everything. Perhaps an array (if you're working old school)? How would you choose to store the 10 button classes? Certainly NOT as a set of values b1, b2, b3, etc....that's what arrays and vectors are for.

If that is foreign to you, it's time to learn it before you can finish this.


Thanks, I'm going to follow your steps and play with it a little bit.
As I thought about it, I was motivate to clarify one point based on how you thought through the code presented.

When I said look for the variables you have 10 of, that does indicate what should be in your class. I just want to underscore that you wont have 10 of them in the class, only 1. You'll instantiate that class 10 times.
Topic archived. No new replies allowed.