Bonsoir, pour apprendre n peu plus sfml et me familiariser avec Processing (j'en ai fait l'année dernière au lycée) et ayant un niveau respectable en languages web, j'ai essayé de porter un digicode que j'avais fait en processing (donc du java modifié) en c++ avec sfml. Tout vas bien jusqu'à ce que je m'aperçois que les différents rectangles des touches sont crées dynamiquement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
//Boutons 1 à 9
  int nbBouton = 1;
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
      fill(255);
      rect((25 + j * 75), (125 + i * 75), 50, 50);
      fill(0);
      text(nbBouton, (25 + j * 75) + 20, (175 + i * 75) - 20);
      nbBouton++;
    }
  }
Nom : 594dbde43896e1c52c368b9bf16d5d61.jpg
Affichages : 1052
Taille : 59,2 Ko

Or en c++ nous avons besoin d'instancier ces objets pour pouvoir les passer à une instance RenderWindow (méthode draw) donc j'ai essayé avec un tableau puis modifier pour le faire avec un vecteur mais je en vosi pas très bien la meilleure solution


Code c++:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include <cstdlib>
#include <vector>
#include <SFML/Graphics.hpp>
 
 
int main(int argc, char* argv[]) {
 
 
    sf::RenderWindow window(sf::VideoMode(250, 400), "Digicode");
 
 
    int nb = 0;
 
 
    sf::Font arial;
    if (!arial.loadFromFile("arial.ttf")) {
        //
    }
 
 
    sf::RectangleShape rects[9];
    sf::RectangleShape rectInput;
    sf::Text numbers[9];
 
 
    std::vector<sf::RectangleShape> rects;
 
 
    sf::Text entree;
    sf::Text validText;
 
 
    rectInput.setPosition(sf::Vector2f());
 
 
    entree.setFont(arial);
    entree.setFillColor(sf::Color::Black);
    entree.setCharacterSize(23);
 
 
    validText.setFont(arial);
    validText.setFillColor(sf::Color::Black);
    validText.setCharacterSize(23);
 
 
    entree.setString("Entrez un code à 4 chiffres");
    validText.setString("Valider");
 
 
    for (int h = 0; h < 9; h++) {
        rects[h].setSize(sf::Vector2f(50.0f, 50.0f));
        rects[h].setFillColor(sf::Color::White);
 
 
        numbers[h].setFont(arial);
        numbers[h].setFillColor(sf::Color::Black);
        numbers[h].setCharacterSize(23);
    }
 
 
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
 
                        //!\ INSTANCIER ici les différents rectangles
 
 
            nb++;
        }
    }
 
 
    for (int i = i; i < 9; i++) {
        //
    }
 
 
    while (window.isOpen()) {
        sf::Event e;
 
 
        while (window.pollEvent(e)) {
            if (e.type == sf::Event::Closed) {
                window.close();
            }
            if (e.type == sf::Event::MouseButtonPressed) {
                handleMouseInput(window);
            }
        }
 
 
        window.clear(sf::Color::Black);
 
 
        window.draw(entree);
        window.draw(validText);
 
 
        for (int i = i; i < 9; i++) {
                        //!\ Les dessiner ici 
            window.draw(rects[i]);
            window.draw(numbers[i]);
        }
 
 
        //end drawing
 
 
        window.display();
    }
 
 
    return EXIT_SUCCESS;
}
 
 
void handleMouseInput(sf::RenderWindow &win) {
    sf::Vector2i mousePos = sf::Mouse::getPosition(win);
        //...
}
Quelqu'un aurait une solution ? Merci

Edit: code Processing
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
/***********************************************************/
// Nom du programme : Digicode
// Auteur(s) : 
// Date : 21/12/2017
// Version : 1
/***********************************************************/
// Description du programme :
//===========================
//
//
//
/***********************************************************/
// Licence : GPL v3
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
/***********************************************************/
 
 
 
 
/******************* Entête déclarative ********************/
/***********************************************************/
 
 
// --- inclusion des librairies utilisées ---
 
 
 
 
// --- déclaration objets ---
 
 
 
 
// --- déclaration variables globales ---
 
 
int nbPress = 0;
int clic = 0;
 
 
String Code = "";
String message = "";
 
 
boolean toucheValidApp = false;
 
 
final String CodeSecret = "1234";
 
 
 
 
 
 
/********************* Fonction SETUP **********************/
// fonction d'initialisation exécutée 1 fois au démarrage
/***********************************************************/
 
 
void setup() {
  // --- initialisation fenêtre de base ---
  size(250, 400); // ouvre une fenêtre xpixels  x ypixels
  background(0,0,0); // couleur fond fenetre
 
  // ---- initialisation paramètres graphiques utilisés ---
 
  PFont police;
  police = loadFont("AgencyFB-Reg-23.vlw");
  textFont(police, 23);
 
  fill(255);
  rect(25, 25, 200, 75);
 
  fill(0);
  text("Entrez un code à 4 chiffres", 30, 45);
 
  fill(255);
 
  //Bouton Valider
  rect(25, 350, 200, 25);
  fill(0);
  text("Valider", 100, 370);
  // 30, 365
 
  //Boutons 1 à 9
  int nbBouton = 1;
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
      fill(255);
      rect((25 + j * 75), (125 + i * 75), 50, 50);
      fill(0);
      text(nbBouton, (25 + j * 75) + 20, (175 + i * 75) - 20);
      nbBouton++;
    }
  }
 
 
  // --- initialisation des objets et fonctionnalités utilisées ---
 
 
}
 
 
 
 
 
 
 
 
 
 
/********************** Fonction DRAW **********************/
//             fonction exécutée en boucle
/***********************************************************/
 
 
void draw() { 
 
 
  if(clic > 0) {
    message = Code;
  }
  if(toucheValidApp) {
    if(Code.equals(CodeSecret)) { //Si le code appuyé est le code secret
      message = "Vous pouvez entrer";
    } else {
      message = "Essayez encore";
    }
    //Remise à zéro des différentes variables
    clic = 0;
    Code = "";
    toucheValidApp = false;
 
    fill(255);
    rect(25, 25, 200, 75);
    fill(0);
    text(message, 30, 45);
  }
 
}
 
 
 
 
 
 
 
 
/******************** Autres Fonctions *********************/
/***********************************************************/
 
 
void mousePressed() {
 
  /** Affiche le nombre de fois que la souris a ete presse **/
  nbPress++;
  fill(255);
  rect(25, 25, 200, 75);
  fill(0);
  text("Nombre de pression: " + nbPress, 30, 45);
  /** **/
 
  /* Vérification pression des touches */
 
  if(mouseX > 25 && mouseY > 125 && mouseX < 225 && mouseY < 350) {
 
    //1ere rangee
    if(mouseY < 175) {
      if(mouseX < 75) { //Touche 1
        Code += "1";
      }
      if(mouseX > 100 && mouseX < 150) { //Touche 2
        Code += "2";
      }
      if(mouseX > 175 && mouseX < 225) { //Touche 3
        Code += "3";
      }
    }
 
    //2eme rangee
    if(mouseY > 200 && mouseY < 250) {
      if(mouseX < 75) { //Touche 4
        Code += "4";
      }
      if(mouseX > 100 && mouseX < 150) { //Touche 5
        Code += "5";
      }
      if(mouseX > 175 && mouseX < 225) { //Touche 6
        Code += "6";
      }
    }
 
    //3eme rangee
    if(mouseY > 275) {
      if(mouseX < 75) { //Touche 7
        Code += "7";
      }
      if(mouseX > 100 && mouseX < 150) { //Touche 8
        Code += "8";
      }
      if(mouseX > 175) { //Touche 9
        Code += "9";
      }
    } 
 
    //On efface l'ecran
    fill(255);
    rect(25, 25, 200, 75);
    fill(0);
    text("Code: " + Code, 30, 45);
  }
 
  //vérification touche Valider pressée
  if(mouseX > 25 && mouseX < 225 && mouseY > 350 && mouseY < 375) {
    println("Touche valider pressée");
    toucheValidApp = true;
  }
}