-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainView.cpp
More file actions
370 lines (291 loc) · 9.8 KB
/
mainView.cpp
File metadata and controls
370 lines (291 loc) · 9.8 KB
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
#include "mainView.h"
MainView::MainView(SDL_Renderer *renderer, std::string map_file, int w_fenetre, int h_fenetre)
:m_wfenetre(w_fenetre) , m_hfenetre(h_fenetre) , scrolling_number_right(0) ,scrolling_number_up(0),
m_tiles_position(nullptr), m_colision_position(nullptr), m_tiles_image(nullptr),
m_nb_wmap(0), m_nb_hmap(0) , m_tiles_image_name("")
{
m_renderer = renderer;
std::string tiles_file_name = load_map(map_file);
load_tiles_image(tiles_file_name);
// Map will be rendered by show() in the main loop
}
MainView::~MainView()
{
m_renderer = nullptr;
if(m_tiles_position != nullptr) {
for (int i = 0; i < m_nb_hmap; i++)
{
if(m_tiles_position[i] != nullptr) {
delete[] m_tiles_position[i];
}
}
delete[] m_tiles_position;
m_tiles_position = nullptr;
}
if(m_colision_position != nullptr) {
for (int i = 0; i < m_nb_hmap; i++)
{
if(m_colision_position[i] != nullptr) {
delete[] m_colision_position[i];
}
}
delete[] m_colision_position;
m_colision_position = nullptr;
}
// Destroy the texture if it exists
if(m_tiles_image != nullptr) {
SDL_DestroyTexture(m_tiles_image);
m_tiles_image = nullptr;
}
}
void MainView::show()
{
blitMap();
}
void MainView::blitMap()
{
SDL_Rect position_screen;
position_screen.h = m_hfenetre / taille_map;
position_screen.w = m_wfenetre / taille_map;
SDL_Rect position_tiles;
position_tiles.y = 0;
position_tiles.h = h_tiles;
position_tiles.w = w_tiles;
float nb_case_nombre(0.00);
int nb_case_ligne(m_tiles_image_rect.w / w_tiles);
int n(0);
int start_j, end_j, start_i, end_i;
if (m_nb_hmap > taille_map) {
start_j = m_nb_hmap - taille_map + scrolling_number_up;
end_j = m_nb_hmap + scrolling_number_up;
} else {
start_j = 0;
end_j = m_nb_hmap;
}
// For width
if (m_nb_wmap > taille_map) {
start_i = scrolling_number_right;
end_i = taille_map + scrolling_number_right;
} else {
start_i = 0;
end_i = m_nb_wmap;
}
if(start_j < 0) start_j = 0;
if(end_j > m_nb_hmap) end_j = m_nb_hmap;
if(start_i < 0) start_i = 0;
if(end_i > m_nb_wmap) end_i = m_nb_wmap;
for (int j = start_j; j < end_j; j++)
{
for (int i = start_i; i < end_i; i++)
{
position_tiles.y = 0;
n = m_tiles_position[j][i];
if((m_tiles_image_rect.w / w_tiles) < n)
{
nb_case_nombre = (float)n / (float)nb_case_ligne;
if(nb_case_nombre == (int)nb_case_nombre)
nb_case_nombre -= 1;
position_tiles.x = (n - (nb_case_ligne * (int)nb_case_nombre) - 1) * w_tiles;
position_tiles.y = (int)nb_case_nombre * h_tiles;
}
else
{
position_tiles.x = (n - 1) * w_tiles;
}
position_screen.x = (i - start_i) * position_screen.w;
position_screen.y = (j - start_j) * position_screen.h;
SDL_RenderCopy(m_renderer, m_tiles_image, &position_tiles, &position_screen);
}
}
return;
}
std::string MainView::load_map(std::string filename)
{
std::ifstream file(filename.c_str());
if(!file) {
std::cerr << "Error: Cannot open map file: " << filename << std::endl;
Error("erreur ouverture du fichier");
}
std::getline(file, m_tiles_image_name ,'\n');
if(!(file >> w_tiles >> h_tiles >> m_nb_wmap >> m_nb_hmap)) {
std::cerr << "Error: Invalid map file format" << std::endl;
Error("erreur format du fichier");
}
if(m_nb_wmap <= 0 || m_nb_hmap <= 0 ) {
std::cerr << "Error: Invalid map dimensions: " << m_nb_wmap << "x" << m_nb_hmap << std::endl;
Error("erreur dimensions invalides");
}
mapTableAllocation(m_nb_wmap , m_nb_hmap);
// Read tile positions
for (int j = 0; j < m_nb_hmap; j++)
{
for(int i = 0; i < m_nb_wmap; i++)
{
if(!(file >> m_tiles_position[j][i])) {
std::cerr << "Error: Invalid map file format" << std::endl;
Error("erreur format du fichier");
}
}
}
for (int j = 0; j < m_nb_hmap; j++)
{
for(int i = 0; i < m_nb_wmap; i++)
{
if(!(file >> m_colision_position[j][i])) {
std::cerr << "Error: Failed to read collision at position [" << j << "][" << i << "]" << std::endl;
// If collision data is missing, set to 0 (no collision) instead of crashing
m_colision_position[j][i] = 0;
}
// Skip commas if present
if(file.peek() == ',') {
file.ignore();
}
}
}
return m_tiles_image_name;
}
void MainView::load_tiles_image(std::string image_name)
{
SDL_Surface *image;
image = IMG_Load(image_name.c_str());
if(image == NULL ){
std::cerr << "Error: Cannot load image: " << image_name << std::endl;
Error("erreur chargement de l'image");
}
m_tiles_image = SDL_CreateTextureFromSurface(m_renderer, image);
SDL_FreeSurface(image);
if(m_tiles_image == NULL){
std::cerr << "Error: Cannot create texture from image" << std::endl;
Error("erreur creation de texture");
}
if(SDL_QueryTexture(m_tiles_image, NULL, NULL, &m_tiles_image_rect.w, &m_tiles_image_rect.h) != 0) {
std::cerr << "Error: Cannot query texture properties" << std::endl;
Error("erreur Query texture");
}
}
void MainView::mapTableAllocation(int wmax, int hmax)
{
m_tiles_position = new int *[hmax] ;
m_colision_position = new int *[hmax] ;
for(int i = 0; i < hmax; i++)
{
m_tiles_position[i] = new int[wmax] ;
for(int j = 0; j < wmax; j++) {
m_tiles_position[i][j] = 1;
}
}
for(int i = 0; i < hmax; i++)
{
m_colision_position[i] = new int[wmax];
// Initialize to 0 (no collision)
for(int j = 0; j < wmax; j++) {
m_colision_position[i][j] = 0;
}
}
}
void MainView::scrolling_right()
{
if( (m_nb_wmap >taille_map || m_nb_hmap > taille_map) && scrolling_number_right < m_nb_wmap - taille_map)
{
scrolling_number_right += 1;
show();
}
return;
}
//----------------------------------------------------------------------------------//
void MainView::scrolling_left()
{
if( (m_nb_wmap >taille_map || m_nb_hmap > taille_map) && scrolling_number_right > 0)
{
scrolling_number_right -= 1;
show();
}
return;
}
//------------------------------------------------------------------------//
void MainView::scrolling_up()
{
if( (m_nb_wmap >taille_map || m_nb_hmap > taille_map) && scrolling_number_up > -(m_nb_hmap - taille_map))
{
scrolling_number_up -= 1;
show();
}
return;
}
void MainView::scrolling_down()
{
if( (m_nb_wmap >taille_map || m_nb_hmap > taille_map) && scrolling_number_up < 0)
{
scrolling_number_up += 1;
show();
}
return;
}
void MainView::set_tile_at_screen(int x, int y, int tile_id)
{
int tile_size_w = m_wfenetre / taille_map;
int tile_size_h = m_hfenetre / taille_map;
int tile_x = x / tile_size_w + scrolling_number_right;
int tile_y = y / tile_size_h + (m_nb_hmap - taille_map) + scrolling_number_up;
if(tile_x >= 0 && tile_x < m_nb_wmap && tile_y >= 0 && tile_y < m_nb_hmap) {
m_tiles_position[tile_y][tile_x] = tile_id;
}
}
void MainView::draw_palette(int x, int y)
{
SDL_Rect position_tiles;
position_tiles.h = h_tiles;
position_tiles.w = w_tiles;
SDL_Rect position_screen;
position_screen.w = 24;
position_screen.h = 16;
int nb_case_ligne = m_tiles_image_rect.w / w_tiles;
for(int i = 0; i < 9; i++) {
int n = i + 1;
position_tiles.y = 0;
if(n > nb_case_ligne) {
float nb_case_nombre = (float)n / (float)nb_case_ligne;
if(nb_case_nombre == (int)nb_case_nombre)
nb_case_nombre -= 1;
position_tiles.x = (n - (nb_case_ligne * (int)nb_case_nombre) - 1) * w_tiles;
position_tiles.y = (int)nb_case_nombre * h_tiles;
} else {
position_tiles.x = (n - 1) * w_tiles;
}
position_screen.x = x + (i % 6) * 24;
position_screen.y = y + (i / 6) * 16;
SDL_RenderCopy(m_renderer, m_tiles_image, &position_tiles, &position_screen);
}
}
void MainView::save_map(std::string filename)
{
std::ofstream file(filename.c_str());
if(!file) {
std::cerr << "ERROR: Cannot save map to file: " << filename << std::endl;
std::cerr << "Make sure you have write permissions in this directory." << std::endl;
return;
}
std::cout << "Saving map to: " << filename << std::endl;
file << m_tiles_image_name << "\n";
file << w_tiles << " " << h_tiles << "\n";
file << m_nb_wmap << " " << m_nb_hmap << "\n";
for(int j = 0; j < m_nb_hmap; j++) {
for(int i = 0; i < m_nb_wmap; i++) {
file << m_tiles_position[j][i] << " ";
}
file << "\n";
}
for(int j = 0; j < m_nb_hmap; j++) {
for(int i = 0; i < m_nb_wmap; i++) {
file << m_colision_position[j][i];
if(i < m_nb_wmap - 1) file << ",";
}
file << "\n";
}
file.close();
if(file.good() || !file.bad()) {
std::cout << "SUCCESS: Map saved successfully to " << filename << std::endl;
} else {
std::cerr << "ERROR: Failed to save map properly to " << filename << std::endl;
}
}