Форум программистов, компьютерный форум, киберфорум
SFML
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.77/13: Рейтинг темы: голосов - 13, средняя оценка - 4.77
0 / 0 / 0
Регистрация: 16.02.2023
Сообщений: 4
1

Проблема с выводом изображения

04.10.2023, 20:11. Показов 2540. Ответов 2

Author24 — интернет-сервис помощи студентам
при нажатии на кнопку "To create" должно выводиться изображение с машиной. Кучу способов перепробовал никак не выводится


C++
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include<SFML/Graphics.hpp>
#include <iostream>
#include<vector>
const int WINDOW_W = 1366;
const int WINDOW_H = 768;
class Car {
public:
    
    bool turnSignal;
    bool disks;
    bool frontAndBack;
    bool bodyFlag;
    bool flag ;
    std::string path;
    sf::RenderWindow* mainWindow;
    Car() : turnSignal(false), disks(false), frontAndBack(false), bodyFlag(false), flag(false), path("") {}
    std::string getPath(bool body);
private:
    std::vector<Car> save;
};
 
std::string Car::getPath(bool body)
{
    std::string path;
    if(body == false) { //Hatchback
        if (turnSignal == true && disks == true && frontAndBack == true) {
            path = "Hatchback\\ttt.psd";
        }
        else if (turnSignal == false && disks == false && frontAndBack == false) {
            path = "Hatchback\\fff.psd";
 
        }
        else if (turnSignal == false && disks == false && frontAndBack == true) {
            path = "Hatchback\\fft.psd";
        }
        else if (turnSignal == false && disks == true && frontAndBack == true) {
            path = "Hatchback\\ftt.psd";
        }
        else if (turnSignal == true && disks == false && frontAndBack == true) {
            path = "Hatchback\\tft.psd";
        }
        else if (turnSignal == true && disks == false && frontAndBack == false) {
            path = "Hatchback\\tff.psd";
        }
        else if (turnSignal == true && disks == true && frontAndBack == false) {
            path = "Hatchback\\ttf.psd";
        }
        else if (turnSignal == false && disks == true && frontAndBack == false) {
            path = "Hatchback\\ftf.psd";
        }
    }
    else if (body == true) {// Sedan
        if (turnSignal == true && disks == true && frontAndBack == true) {
            path = "Sedan\\ttt.psd";
        }
        else if (turnSignal == false && disks == false && frontAndBack == false) {
            path = "Sedan\\fff.psd";
        }
        else if (turnSignal == false && disks == false && frontAndBack == true) {
            path = "Sedan\\fft.psd";
        }
        else if (turnSignal == false && disks == true && frontAndBack == true) {
            path = "Sedan\\ftt.psd";
        }
        else if (turnSignal == true && disks == false && frontAndBack == true) {
            path = "Sedan\\tft.psd";
        }
        else if (turnSignal == true && disks == false && frontAndBack == false) {
            path = "Sedan\\tff.psd";
        }
        else if (turnSignal == true && disks == true && frontAndBack == false) {
            path = "Sedan\\ttf.psd";
        }
        else if (turnSignal == false && disks == true && frontAndBack == false) {
            path = "Sedan\\ftf.psd";
        }
    }
    return path;
}
 
class Button {
private:
    sf::RectangleShape rectangle;
    sf::Text text;
    sf::Color normalColor;
    sf::Color pressedColor;
    sf::Texture buttonTexture;
    sf::Vector2f position;
    sf::Font font;
    sf::Vector2f size;
    bool isPressed;
    bool isPlus;
 
public:
    Button(sf::Vector2f position, sf::Vector2f size, const std::string& text);
    void setPressed(bool pressed);
    sf::RectangleShape& getRectangle();
    void render(sf::RenderWindow& window);
    void setText(const std::string& newText);
};
Button::Button(sf::Vector2f position, sf::Vector2f size, const std::string& text) :
    rectangle(size),
    size(size),
    normalColor(sf::Color::White),
    pressedColor(sf::Color::Black),
    isPressed(false),
    isPlus(true)
{
    font.loadFromFile("comic.ttf");
 
    rectangle.setSize(size);
    rectangle.setPosition(position);
    rectangle.setFillColor(sf::Color::White);
    rectangle.setOutlineColor(sf::Color::Black);
    rectangle.setOutlineThickness(2);
    this->text.setFont(font);
    this->text.setString(text);
    this->text.setCharacterSize(24);
    this->text.setFillColor(sf::Color::Black);
    this->text.setPosition(position);
 
}
void Button::setText(const std::string& newText) {
    isPlus = !isPlus;
    if (isPlus) {
        text.setString(newText + " -");
    }
    else {
        text.setString(newText + " +");
    }
}
 
void  Button::setPressed(bool pressed)
{
    isPressed = pressed;
    if (isPressed) {
        rectangle.setFillColor(pressedColor);
    }
    else {
        rectangle.setFillColor(normalColor);
    }
 
}
 
void Button::render(sf::RenderWindow& window)
{
    window.draw(rectangle);
    text.setFont(font);
    window.draw(text);
}
 
sf::RectangleShape& Button::getRectangle()
{
    return rectangle;
}
 
class App {
private:
    sf::RenderWindow window;
    sf::Texture backgroundTexture;
    sf::Sprite backgroundSprite;
    std::vector<Button> buttons;
    std::vector<Button> bodyButtons;
    std::vector<Button> additionButtons;
    std::vector<Button> menuButtons;
    Car car;
 
public:
    App();
    void run();
 
private:
    void render();
    void processEvent();
    void update();
    void createBodyButtons();
    void createAdditionButtons();
    void showSavedCars();
    void createMenuButtons();
    void createCarImage(bool body);
    void createVarImage(bool body, bool turnSignal, bool disks, bool frontAndBack);
};
App::App() :
    window(sf::VideoMode(WINDOW_W, WINDOW_H), "Car Craft")
{
    backgroundTexture.loadFromFile("background.jpg");
    backgroundSprite.setTexture(backgroundTexture);
    buttons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 100, WINDOW_H / 2 - 225), sf::Vector2f(270, 50), "Create a new machine"));
    buttons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 100, WINDOW_H / 2 - 150), sf::Vector2f(270, 50), "Saved machines"));
    buttons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 100, WINDOW_H / 2 - 75), sf::Vector2f(270, 50), "Exit"));
}
 
void App::showSavedCars() {
 
}
 
void App::run()
{
    while (window.isOpen()) {
        processEvent();
        update(); 
        render();
    }
}
 
void App::render()
{
    window.clear();
    window.draw(backgroundSprite);
    for (auto& button : bodyButtons) {
        button.render(window);
    }
    for (auto& button : buttons) {
        button.render(window);
    }
    for (auto& button : menuButtons) {
        button.render(window);
    }
    for (auto& button : additionButtons) {
        button.render(window);
    }
    
    window.display();
}
 
void App::createVarImage(bool body, bool turnSignal, bool disks, bool frontAndBack) {
    std::cout << "Зашел" << std::endl;
    std::cout << body << std::endl;
    std::cout << turnSignal << std::endl;
    std::cout << disks << std::endl;
    std::cout << frontAndBack << std::endl;
    std::string path = car.getPath(body);
    sf::Image heroimage; 
    heroimage.loadFromFile(path);
    sf::Texture carTexture;
    carTexture.loadFromImage(heroimage);
    sf::Sprite herosprite;
    herosprite.setTexture(carTexture);
    herosprite.setPosition(50, 25);
    window.draw(herosprite);
    std::cout << "Вышел" << std::endl;
}
void App::processEvent()
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
        if (event.type == sf::Event::MouseButtonPressed) {
            sf::Vector2f localPosition(event.mouseButton.x, event.mouseButton.y);
            
            for (int i = 0; i < buttons.size(); i++) {
                if (buttons[i].getRectangle().getGlobalBounds().contains(localPosition)) {
                    std::cout << "Нажата кнопка " << i << std::endl;
                    buttons[i].setPressed(true);
                    if (i == 0) {
                        createBodyButtons();
                    }
                    else if (i == 1) {
                        if(car.flag==false)
                        showSavedCars();
                    }
                    else if (i == 2) {
                        window.close();
                    }
                }
            } 
 
            for (int i = 0; i < bodyButtons.size(); i++) {
                if (bodyButtons[i].getRectangle().getGlobalBounds().contains(localPosition)) {
                    std::cout << "Нажата bodyButton " << i << std::endl;
                    bodyButtons[i].setPressed(true);
                    if (i == 0) {
                        car.bodyFlag = false;
                    }
                    else if (i == 1) {
                        car.bodyFlag = true;
                    }
                    createAdditionButtons();
                }
            }
            for (int i = 0; i < additionButtons.size(); i++) {
                if (additionButtons[i].getRectangle().getGlobalBounds().contains(localPosition)) {
                    additionButtons[i].setPressed(true);
                    if (i == 0) {
                        additionButtons[i].setText("Turn signal");
                        car.turnSignal = !car.turnSignal;
                    }
                    else if (i == 1) {
                        additionButtons[i].setText("Disks");
                        car.disks = !car.disks;
                    }
                    else if (i == 2) {
                        additionButtons[i].setText("Front and back");
                        car.frontAndBack = !car.frontAndBack;
                    }
                    else if (i == 3) {
                        std::cout << "bodyFlag: " << car.bodyFlag << ", turnSignal: " << car.turnSignal << ", disks: " << car.disks << ", frontAndBack: " << car.frontAndBack << std::endl;
                        car.path = car.getPath(car.bodyFlag);
                        createVarImage(car.bodyFlag,car.turnSignal,car.disks,car.frontAndBack);
                        additionButtons.clear();
                        //createMenuButtons();
                    }
                }
            }
 
           
          
           
        }
        else if (event.type == sf::Event::MouseButtonReleased) {
            for (int i = 0; i < buttons.size(); i++) {
                buttons[i].setPressed(false);
            }
            for (int i = 0; i < bodyButtons.size(); i++) {
                bodyButtons[i].setPressed(false);
            }
            for (int i = 0; i < menuButtons.size(); i++) {
                menuButtons[i].setPressed(false);
            }
            for (int i = 0; i < additionButtons.size(); i++) {
                additionButtons[i].setPressed(false);
            }
        }
    }
}
 
void App::createBodyButtons() {
    buttons.clear();
 
    bodyButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 425), sf::Vector2f(170, 50), "Hatchback"));
    bodyButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 350), sf::Vector2f(170, 50), "Sedan"));
}
 
void App::createAdditionButtons()
{
    bodyButtons.clear();
    additionButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 630), sf::Vector2f(186, 50), "Turn signal"));
    additionButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 555), sf::Vector2f(186, 50), "Disks"));
    additionButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 480), sf::Vector2f(186, 50), "Front and back"));
    additionButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 405), sf::Vector2f(186, 50), "To create"));
}
void App::createMenuButtons() {
    menuButtons.push_back(Button(sf::Vector2f(WINDOW_W / 2 - 50, WINDOW_H - 630), sf::Vector2f(186, 50), "Menu"));
}
 
void App::update()
{
 
}
 
 
int main()
{
    setlocale(LC_ALL, "rus");
    App app;
    app.run();
    
}
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
04.10.2023, 20:11
Ответы с готовыми решениями:

Проблема с выводом изображения
Я написал программу которая добавляет изображение студенту в базе данных. И нужно чтобы изображение...

Проблема с выводом изображения
Недавно приобрел 3050 и заметил, что при запуске пк она не выводит изображение, при перезагрузке же...

Проблема с выводом изображения на экран
Пытаюсь вывести изображение формата jpg на экран, но не могу загрузить изображение. Закидываю...

Проблема с выводом изображения на монитор
Здравствуйте! Имеется такая проблема: позавчера включил ПК. Сперва все было хорошо. Я смотрел...

2
1071 / 677 / 182
Регистрация: 30.03.2021
Сообщений: 2,116
04.10.2023, 21:34 2
zunderz, не по теме, -вот этот метод можно упростить как-то так:
C++
1
2
3
4
5
6
7
8
9
10
11
12
std::string Car::getPath(const bool &body)
{
    auto getState=[this](const bool & state)->std::string{
        return state ? "t" : "f";
    };
    return (body ? std::string("Hatchback\\") : std::string("Sedan\\"))
                                                   + getState(turnSignal)
                                                   + getState(disks)
                                                   + getState(frontAndBack)
                                                   + std::string(".psd");
 
}
0
0 / 0 / 0
Регистрация: 16.02.2023
Сообщений: 4
07.10.2023, 12:59  [ТС] 3
Все равно не выводит
0
07.10.2023, 12:59
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
07.10.2023, 12:59
Помогаю со студенческими работами здесь

Проблема с выводом изображения и звуков
Всем привет ,столкнулся с такой проблемой что во время работы на компьютере появляется резко...

Rx580 проблема с выводом изображения
Заранее прошу прощения за длинный текст, постарался максимально подробно описать проблему. ...

Четыре быстрых сигнала от биоса и проблема с выводом изображения на экран
Железо: Серверная материнка Kllisre X79A(socket LGA1356) Проц - XEON E5-2420 ОЗУ - 8113 МБ (ECC...

Проблемы с выводом изображения
нет сейчас финансовой возможности проверить и изменить ситуацию, поэтому обращаюсь на форум. в...

Проблемы с выводом изображения на пк
Здраствуйте, подскажите по такому вопросу. При включение ПК бывает на сеунду появляется...

Велосипеды с ChoiceType и выводом изображения
Пересмотрел кучу мануалов по работе с формами в симфони, но не нашел ничего подобного. Задача...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
3
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru