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

Скроллинг определённой части сетки

27.07.2023, 18:55. Показов 597. Ответов 1

Author24 — интернет-сервис помощи студентам
Здравствуйте! Написал на SFML простой редактор пиксельной графики. Хочу сделать скроллинг сетки так, чтобы квадратики выбора цвета оставались на месте. Хотел сделать через View и setViewport, но ничего не получилось.
Как бы я смог решить эту проблему?
Вот код:

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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
 
class PixelColor
{
public:
    Texture texture;
    Sprite sprite;
 
    PixelColor(int x, int y, int w, int h, int sx, int sy)
    {
        texture.loadFromFile("https://www.cyberforum.ru/images/map.png");
        sprite.setTexture(texture);
        sprite.setTextureRect(IntRect(x, y, w, h));
        sprite.setPosition(sx, sy);
    }
};
 
int main()
{
    RenderWindow window(VideoMode(1024, 800), "Your_Pixels", Style::Fullscreen);
 
    RectangleShape rect(Vector2f(71, 71));
    rect.setOutlineThickness(5);
    rect.setOutlineColor(Color(60, 60, 60));
    rect.setPosition(0, 0);
    rect.setFillColor(Color(236, 56, 56));
 
    RectangleShape rectForWhite(Vector2f(67, 67));
    rectForWhite.setOutlineThickness(5);
    rectForWhite.setOutlineColor(Color::Black);
    rectForWhite.setPosition(0, 70);
 
    Texture map;
    map.loadFromFile("https://www.cyberforum.ru/images/map.png");
    Sprite s_map;
    s_map.setTexture(map);
 
    /*Sprite white;
    white.setTexture(map);
    white.setTextureRect(IntRect(0, 0, 70, 70));
    white.setPosition(1820, 0);*/
 
    PixelColor red(70, 0, 70, 70, 0, 0);
    PixelColor white(0, 0, 70, 70, 0, 70);
    PixelColor green(140, 0, 70, 70, 0, 140);
    PixelColor yellow(210, 0, 70, 70, 0, 210);
    PixelColor black(280, 0, 70, 70, 0, 280);
    PixelColor orange(350, 0, 70, 70, 0, 350);
    PixelColor blue(420, 0, 70, 70, 0, 420);
    PixelColor brown(490, 0, 70, 70, 0, 490);
    PixelColor pink(560, 0, 70, 70, 0, 560);
    PixelColor turquoise(630, 0, 70, 70, 0, 630);
    PixelColor darkGreen(700, 0, 70, 70, 0, 700);
    PixelColor lightBlue(770, 0, 70, 70, 0, 770);
    PixelColor lightGray(840, 0, 70, 70, 0, 840);
    PixelColor lightGreen(910, 0, 70, 70, 0, 910);
    PixelColor beige(980, 0, 70, 70, 0, 980);
 
 
    int w = 50;
    String sgrid[100][100];
 
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            sgrid[i][j] = '0';
        }
    }
 
    char n = 'r';
 
    Clock clock;
 
    while (window.isOpen())
    {
        float time = clock.getElapsedTime().asMicroseconds();
        clock.restart();
        time /= 800;
 
        Vector2i pos = Mouse::getPosition(window);
        Vector2f localPos = window.mapPixelToCoords(pos);
        std::cout << localPos.x << std::endl;
        int x = localPos.x / w;
        int y = localPos.y / w;
 
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
 
            if (event.type == Event::MouseButtonPressed)
            {
                if (event.key.code == Mouse::Left)
                {
                    if (sf::IntRect(red.sprite.getPosition().x, red.sprite.getPosition().y,
                        red.sprite.getLocalBounds().width, red.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(236, 56, 56));
                        rect.setPosition(0, 0);
                        n = 'r';
                    }
 
                    if (sf::IntRect(white.sprite.getPosition().x, white.sprite.getPosition().y,
                        white.sprite.getLocalBounds().width, white.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color::White);
                        rect.setPosition(0, 70);
                        n = '0';
                    }
 
                    if (sf::IntRect(green.sprite.getPosition().x, green.sprite.getPosition().y,
                        green.sprite.getLocalBounds().width, green.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(56, 209, 69));
                        rect.setPosition(0, 140);
                        n = 'g';
                    }
 
                    if (sf::IntRect(yellow.sprite.getPosition().x, yellow.sprite.getPosition().y,
                        yellow.sprite.getLocalBounds().width, yellow.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(255, 242, 56));
                        rect.setPosition(0, 210);
                        n = 'y';
                    }
 
                    if (sf::IntRect(black.sprite.getPosition().x, black.sprite.getPosition().y,
                        black.sprite.getLocalBounds().width, black.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(0, 0, 0));
                        rect.setPosition(0, 280);
                        n = 'b';
                    }
 
                    if (sf::IntRect(orange.sprite.getPosition().x, orange.sprite.getPosition().y,
                        orange.sprite.getLocalBounds().width, orange.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(255, 127, 56));
                        rect.setPosition(0, 350);
                        n = 'o';
                    }
 
                    if (sf::IntRect(blue.sprite.getPosition().x, blue.sprite.getPosition().y,
                        blue.sprite.getLocalBounds().width, blue.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(63, 72, 204));
                        rect.setPosition(0, 420);
                        n = 'B';
                    }
 
                    if (sf::IntRect(brown.sprite.getPosition().x, brown.sprite.getPosition().y,
                        brown.sprite.getLocalBounds().width, brown.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(185, 122, 86));
                        rect.setPosition(0, 490);
                        n = 'n';
                    }
 
                    if (sf::IntRect(pink.sprite.getPosition().x, pink.sprite.getPosition().y,
                        pink.sprite.getLocalBounds().width, pink.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(255, 174, 200));
                        rect.setPosition(0, 560);
                        n = 'p';
                    }
 
                    if (sf::IntRect(turquoise.sprite.getPosition().x, turquoise.sprite.getPosition().y,
                        turquoise.sprite.getLocalBounds().width, turquoise.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(140, 255, 251));
                        rect.setPosition(0, 630);
                        n = 't';
                    }
 
                    if (sf::IntRect(darkGreen.sprite.getPosition().x, darkGreen.sprite.getPosition().y,
                        darkGreen.sprite.getLocalBounds().width, darkGreen.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(11, 102, 35));
                        rect.setPosition(0, 700);
                        n = 'd';
                    }
 
                    if (sf::IntRect(lightBlue.sprite.getPosition().x, lightBlue.sprite.getPosition().y,
                        lightBlue.sprite.getLocalBounds().width, lightBlue.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(0, 168, 243));
                        rect.setPosition(0, 770);
                        n = 'l';
                    }
 
                    if (sf::IntRect(lightGray.sprite.getPosition().x, lightGray.sprite.getPosition().y,
                        lightGray.sprite.getLocalBounds().width, lightGray.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(195, 195, 195));
                        rect.setPosition(0, 840);
                        n = 'a';
                    }
 
                    if (sf::IntRect(lightGreen.sprite.getPosition().x, lightGreen.sprite.getPosition().y,
                        lightGreen.sprite.getLocalBounds().width, lightGreen.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(196, 255, 14));
                        rect.setPosition(0, 910);
                        n = 'L';
                    }
 
                    if (sf::IntRect(beige.sprite.getPosition().x, beige.sprite.getPosition().y,
                        beige.sprite.getLocalBounds().width, beige.sprite.getLocalBounds().height).contains(sf::Mouse::getPosition(window)))
                    {
                        rect.setFillColor(Color(253, 236, 166));
                        rect.setPosition(0, 980);
                        n = 'i';
                    }
 
                    else
                    {
                        sgrid[x][y] = n;
                    }
                }
                if (event.key.code == Mouse::Right)
                {
                    sgrid[x][y] = '0';
                }
            }
        }
 
        if (w != 20)
        {
            if (Keyboard::isKeyPressed(Keyboard::X))
            {
                w--;
            }
        }
        if (w != 70)
        {
            if (Keyboard::isKeyPressed(Keyboard::Z))
            {
                w++;
            }
        }
 
 
        /*if ((Keyboard::isKeyPressed(Keyboard::Right)) || (Keyboard::isKeyPressed(Keyboard::D)))
        {
            view.move(60.f, 0);
        }
        if ((Keyboard::isKeyPressed(Keyboard::Left)) || (Keyboard::isKeyPressed(Keyboard::A)))
        {
            view.move(-60.f, 0);
        }
        if ((Keyboard::isKeyPressed(Keyboard::Down)) || (Keyboard::isKeyPressed(Keyboard::S)))
        {
            view.move(0, 60.f);
        }
        if ((Keyboard::isKeyPressed(Keyboard::Up)) || (Keyboard::isKeyPressed(Keyboard::W)))
        {
            view.move(0, -60.f);
        }
 
        if (Keyboard::isKeyPressed(Keyboard::R))
        {
            view = window.getDefaultView();
        }*/
 
        window.clear(Color::White);
 
        for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 100; j++)
            {
                if (sgrid[i][j] == '0')
                {
                    s_map.setTextureRect(IntRect(0, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'r')
                {
                    s_map.setTextureRect(IntRect(70, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'g')
                {
                    s_map.setTextureRect(IntRect(140, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'y')
                {
                    s_map.setTextureRect(IntRect(210, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'b')
                {
                    s_map.setTextureRect(IntRect(280, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'o')
                {
                    s_map.setTextureRect(IntRect(350, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'B')
                {
                    s_map.setTextureRect(IntRect(420, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'n')
                {
                    s_map.setTextureRect(IntRect(490, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'p')
                {
                    s_map.setTextureRect(IntRect(560, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 't')
                {
                    s_map.setTextureRect(IntRect(630, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'd')
                {
                    s_map.setTextureRect(IntRect(700, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'l')
                {
                    s_map.setTextureRect(IntRect(770, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'a')
                {
                    s_map.setTextureRect(IntRect(840, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'L')
                {
                    s_map.setTextureRect(IntRect(910, 0, 70, 70));
                }
 
                if (sgrid[i][j] == 'i')
                {
                    s_map.setTextureRect(IntRect(980, 0, 70, 70));
                }
                
                s_map.setPosition(i * w, j * w);
 
                window.draw(s_map);
            }
        }
 
        window.draw(rectForWhite);
 
        window.draw(red.sprite);
        window.draw(white.sprite);
        window.draw(green.sprite);
        window.draw(yellow.sprite);
        window.draw(black.sprite);
        window.draw(orange.sprite);
        window.draw(blue.sprite);
        window.draw(brown.sprite);
        window.draw(pink.sprite);
        window.draw(turquoise.sprite);
        window.draw(darkGreen.sprite);
        window.draw(lightBlue.sprite);
        window.draw(lightGray.sprite);
        window.draw(lightGreen.sprite);
        window.draw(beige.sprite);
 
        window.draw(rect);
        
        window.display();
    }
 
    return 0;
}
Вот тайлсет:
Скроллинг определённой части сетки


Заранее огромное спасибо!!
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
27.07.2023, 18:55
Ответы с готовыми решениями:

TStringGrid: как изменить цвет линий сетки и отследить скроллинг?
1. Можно ли изменить цвет линий сетки, когда в ячейке находится редактор (опции: goEditing и...

FlexGrid.Как при скроллинге одной сетки передавать скроллинг к другой?
Одна сетка под другой. Подскажите, как при скроллинге одной сетки передавать скроллинг к другой.

Создание навигационной сетки на части террейна
Добрый день! Можно ли в Unity создать навигационную сетку на определённом участке террейна, без...

Iframe определённой части
в iframe надо отображатб не верхний левый край другого сайта а область немного в стороне вот...

1
Just Do It!
3841 / 2286 / 636
Регистрация: 23.09.2014
Сообщений: 7,074
Записей в блоге: 3
08.08.2023, 23:03 2
Цитата Сообщение от Egor_klchkff Посмотреть сообщение
texture.loadFromFile("https://www.cyberforum.ru/images/map.png");
как это работает?
0
08.08.2023, 23:03
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
08.08.2023, 23:03
Помогаю со студенческими работами здесь

Блокирование определенной части экрана
Мне стало интересно, а как можно сделать такое: запускается программа, которая блокирует все...

Найти сумму в определенной части
Здраствуйте. Можете помочь сделать программу в которой надо найти сумму элементов в в определенной...

скриншот определенной части страницы
мне нужно сделать скриншот не всей страницы, а только одной части. сам код у меня есть, только...

Split: выделение определённой части
Дано: два компьютера, .vdi размером 18Gb на одном из них, забитом почти под 100%, USB флешка на...

Screenshot определенной части экрана
Здравствуйте, уважаемые. Стоит такая задача: Превратить содержимое div-блока в картинку и...

Вывод определенной части строки
Здравствуйте. Нужна ваша помощь, уважаемые. С помощью ps сделал выгрузку из ldap. Выгруженные...

Удаление с теста определённой части
Есть некоторый текст. Вот пример {'content_type' 'sticker', 'message_id' 513, 'from_user' {'id'...


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

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