1 /***************************************************************************
3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. *
8 ***************************************************************************/
10 /* December 28, 2003 - February 1st, 2004 */
12 /* leveleditor.c - A built-in level editor for SuperTux
13 by Ricardo Cruz <rick2@aeiou.pt> */
21 #include <SDL_image.h>
22 #include "leveleditor.h"
34 /* definitions to aid development */
35 #define DONE_LEVELEDITOR 1
37 #define DONE_CHANGELEVEL 3
39 /* definitions that affect gameplay */
40 #define KEY_CURSOR_SPEED 32
41 #define KEY_CURSOR_FASTSPEED 64
42 /* when pagedown/up pressed speed:*/
43 #define PAGE_CURSOR_SPEED 13*32
45 #define CURSOR_LEFT_MARGIN 96
46 #define CURSOR_RIGHT_MARGIN 600
47 /* right_margin should noticed that the cursor is 32 pixels,
48 so it should subtract that value */
50 #define MOUSE_LEFT_MARGIN 32
51 #define MOUSE_RIGHT_MARGIN 608
52 #define MOUSE_POS_SPEED 32
54 /* gameloop funcs declerations */
56 void loadshared(void);
57 void unloadshared(void);
59 /* own declerations */
60 /* crutial ones (main loop) */
64 void le_checkevents();
65 void le_change(float x, float y, unsigned char c);
67 void le_set_defaults(void);
68 void le_activate_bad_guys(void);
70 /* leveleditor internals */
71 static int le_level_changed; /* if changes, ask for saving, when quiting*/
72 static int pos_x, cursor_x, cursor_y, cursor_tile, fire;
74 static st_level le_current_level;
75 static st_subset le_level_subset;
76 static int le_show_grid;
78 static texture_type le_selection;
80 static char le_current_tile;
81 static int le_mouse_pressed;
82 static button_type le_test_level_bt;
83 static button_type le_next_level_bt;
84 static button_type le_previous_level_bt;
85 static button_type le_rubber_bt;
87 void le_activate_bad_guys(void)
91 /* Activate bad guys: */
93 /* as oposed to the gameloop.c func, this one doesn't remove
94 the badguys from tiles */
96 for (y = 0; y < 15; ++y)
97 for (x = 0; x < le_current_level.width; ++x)
98 if (le_current_level.tiles[y][x] >= '0' && le_current_level.tiles[y][x] <= '9')
99 add_bad_guy(x * 32, y * 32, le_current_level.tiles[y][x] - '0');
102 void le_set_defaults()
106 if(le_current_level.time_left == 0)
107 le_current_level.time_left = 255;
110 /* FIXME: Needs to be implemented. It should ask the user for the level(file)name and then let him create a new level based on this. */
114 /* FIXME: It should let select the user a level, which is in the leveldirectory and then load it. */
118 int leveleditor(int levelnb)
120 int last_time, now_time;
128 last_time = SDL_GetTicks();
133 if(cursor_x < pos_x + CURSOR_LEFT_MARGIN)
134 pos_x = cursor_x - CURSOR_LEFT_MARGIN;
136 if(cursor_x > pos_x + CURSOR_RIGHT_MARGIN)
137 pos_x = cursor_x - CURSOR_RIGHT_MARGIN;
139 /* make sure we respect the borders */
142 if(cursor_x > (le_current_level.width*32) - 32)
143 cursor_x = (le_current_level.width*32) - 32;
147 if(pos_x > (le_current_level.width * 32) - screen->w + 32)
148 pos_x = (le_current_level.width * 32) - screen->w + 32;
155 menu_process_current();
156 if(current_menu == &leveleditor_menu)
158 switch (menu_check(&leveleditor_menu))
164 done = DONE_LEVELEDITOR;
176 if(done == DONE_QUIT)
183 now_time = SDL_GetTicks();
184 if (now_time < last_time + FPS)
185 SDL_Delay(last_time + FPS - now_time); /* delay some time */
195 subset_load(&le_level_subset,"default");
200 menu_set_current(&leveleditor_menu);
201 le_frame = 0; /* support for frames in some tiles, like waves and bad guys */
207 le_level_changed = NO;
208 if(level_load(&le_current_level, le_level_subset.name, le_level) != 0)
213 if(le_current_level.time_left == 0)
214 le_current_level.time_left = 255;
216 level_load_gfx(&le_current_level);
218 le_current_tile = '.';
219 le_mouse_pressed = NO;
220 le_activate_bad_guys();
222 texture_load(&le_selection,DATA_PREFIX "/images/leveleditor/select.png", USE_ALPHA);
223 button_load(&le_test_level_bt,"/images/icons/test-level.png","Test Level","Press this button to test the level that is currently being edited.",150,screen->h - 64);
224 button_load(&le_next_level_bt,"/images/icons/up.png","Test Level","Press this button to test the level that is currently being edited.",screen->w-32,0);
225 button_load(&le_previous_level_bt,"/images/icons/down.png","Test Level","Press this button to test the level that is currently being edited.",screen->w-32,16);
226 button_load(&le_rubber_bt,"/images/icons/rubber.png","Test Level","Press this button to test the level that is currently being edited.",screen->w-32,32);
228 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
235 /*if(level_changed == YES)
236 if(askforsaving() == CANCEL)
240 button_free(&le_test_level_bt);
242 level_free(&le_current_level);
245 texture_free(&le_selection);
251 static char str[LEVEL_NAME_MAX];
253 /* Draw the real background */
254 if(le_current_level.bkgd_image[0] != '\0')
257 texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s - 32, img_bkgd.h, NO_UPDATE);
258 texture_draw_part(&img_bkgd,0,0,screen->w - s - 32 ,0,s,img_bkgd.h, NO_UPDATE);
262 clearscreen(le_current_level.bkgd_red, le_current_level.bkgd_green, le_current_level.bkgd_blue);
265 /* clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue); */
267 for (y = 0; y < 15; ++y)
268 for (x = 0; x < 19; ++x)
270 drawshape(x * 32, y * 32, le_current_level.tiles[y][x + (pos_x / 32)]);
273 /* draw whats inside stuff when cursor is selecting those */
274 cursor_tile = le_current_level.tiles[cursor_y/32][cursor_x/32];
278 texture_draw(&img_mints, cursor_x - pos_x, cursor_y, NO_UPDATE);
281 texture_draw(&img_golden_herring, cursor_x - pos_x, cursor_y, NO_UPDATE);
286 texture_draw(&img_distro[(le_frame / 5) % 4], cursor_x - pos_x, cursor_y, NO_UPDATE);
292 /* Draw the Bad guys: */
293 for (i = 0; i < num_bad_guys; ++i)
295 if(bad_guys[i].base.alive == NO)
297 /* to support frames: img_bsod_left[(frame / 5) % 4] */
298 if(bad_guys[i].kind == BAD_BSOD)
299 texture_draw(&img_bsod_left[(le_frame / 5) % 4], ((int)(bad_guys[i].base.x - pos_x)/32)*32, bad_guys[i].base.y, NO_UPDATE);
300 else if(bad_guys[i].kind == BAD_LAPTOP)
301 texture_draw(&img_laptop_left[(le_frame / 5) % 3], ((int)(bad_guys[i].base.x - pos_x)/32)*32, bad_guys[i].base.y, NO_UPDATE);
302 else if (bad_guys[i].kind == BAD_MONEY)
303 texture_draw(&img_money_left[(le_frame / 5) % 2], ((int)(bad_guys[i].base.x - pos_x)/32)*32, bad_guys[i].base.y, NO_UPDATE);
306 /* draw a grid (if selected) */
309 for(x = 0; x < 19; x++)
310 fillrect(x*32, 0, 1, screen->h, 225, 225, 225,255);
311 for(y = 0; y < 15; y++)
312 fillrect(0, y*32, screen->w - 32, 1, 225, 225, 225,255);
315 fillrect(screen->w - 32, 0, 32, screen->h, 50, 50, 50,255);
316 drawshape(19 * 32, 14 * 32, le_current_tile);
318 button_draw(&le_test_level_bt);
319 button_draw(&le_next_level_bt);
320 button_draw(&le_previous_level_bt);
321 button_draw(&le_rubber_bt);
323 texture_draw(&le_selection, ((int)(cursor_x - pos_x)/32)*32, cursor_y, NO_UPDATE);
325 sprintf(str, "%d", le_current_level.time_left);
326 text_draw(&white_text, "TIME", 324, 0, 1, NO_UPDATE);
327 text_draw(&gold_text, str, 404, 0, 1, NO_UPDATE);
329 text_draw(&white_text, "NAME", 0, 0, 1, NO_UPDATE);
330 text_draw(&gold_text, le_current_level.name, 80, 0, 1, NO_UPDATE);
332 sprintf(str, "%d/%d", le_level,le_level_subset.levels);
333 text_draw(&white_text, "NUMB", 0, 20, 1, NO_UPDATE);
334 text_draw(&gold_text, str, 80, 20, 1, NO_UPDATE);
336 text_draw(&white_small_text, "F1 for Help", 10, 430, 1, NO_UPDATE);
337 text_draw(&white_small_text, "F2 for Testing", 150, 430, 1, NO_UPDATE);
340 void le_checkevents()
347 keymod = SDL_GetModState();
349 while(SDL_PollEvent(&event))
351 /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
354 case SDL_KEYDOWN: // key pressed
355 key = event.key.keysym.sym;
358 menu_event(&event.key.keysym);
371 cursor_x -= KEY_CURSOR_SPEED;
373 cursor_x -= KEY_CURSOR_FASTSPEED;
377 cursor_x += KEY_CURSOR_SPEED;
379 cursor_x += KEY_CURSOR_FASTSPEED;
383 cursor_y -= KEY_CURSOR_SPEED;
385 cursor_y -= KEY_CURSOR_FASTSPEED;
392 cursor_y += KEY_CURSOR_SPEED;
394 cursor_y += KEY_CURSOR_FASTSPEED;
396 if(cursor_y > screen->h-32)
397 cursor_y = screen->h-32;
406 level_save(&le_current_level,"test",le_level);
407 gameloop("test",le_level, ST_GL_TEST);
408 menu_set_current(&leveleditor_menu);
410 level_load_gfx(&le_current_level);
412 le_activate_bad_guys();
418 cursor_x = (le_current_level.width * 32) - 32;
421 cursor_x -= PAGE_CURSOR_SPEED;
424 cursor_x += PAGE_CURSOR_SPEED;
427 le_show_grid = !le_show_grid;
430 le_change(cursor_x, cursor_y, '.');
433 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
434 le_change(cursor_x, cursor_y, 'A');
436 le_change(cursor_x, cursor_y, 'a');
439 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
440 le_change(cursor_x, cursor_y, 'B');
443 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
444 le_change(cursor_x, cursor_y, 'C');
446 le_change(cursor_x, cursor_y, 'c');
449 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
450 le_change(cursor_x, cursor_y, 'D');
452 le_change(cursor_x, cursor_y, 'd');
455 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
456 le_change(cursor_x, cursor_y, 'E');
458 le_change(cursor_x, cursor_y, 'e');
461 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
462 le_change(cursor_x, cursor_y, 'F');
464 le_change(cursor_x, cursor_y, 'f');
467 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
468 le_change(cursor_x, cursor_y, 'G');
470 le_change(cursor_x, cursor_y, 'g');
473 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
474 le_change(cursor_x, cursor_y, 'H');
476 le_change(cursor_x, cursor_y, 'h');
479 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
480 le_change(cursor_x, cursor_y, 'I');
482 le_change(cursor_x, cursor_y, 'i');
485 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
486 le_change(cursor_x, cursor_y, 'J');
488 le_change(cursor_x, cursor_y, 'j');
491 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
492 le_change(cursor_x, cursor_y, 'X');
494 le_change(cursor_x, cursor_y, 'x');
497 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
498 le_change(cursor_x, cursor_y, 'Y');
500 le_change(cursor_x, cursor_y, 'y');
502 case SDLK_LEFTBRACKET:
503 le_change(cursor_x, cursor_y, '[');
505 case SDLK_RIGHTBRACKET:
506 le_change(cursor_x, cursor_y, ']');
510 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
511 le_change(cursor_x, cursor_y, '#');
515 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
516 le_change(cursor_x, cursor_y, '$');
519 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
520 le_change(cursor_x, cursor_y, '|');
522 le_change(cursor_x, cursor_y, '\\');
525 le_change(cursor_x, cursor_y, '^');
529 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
530 le_change(cursor_x, cursor_y, '&');
534 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
535 le_change(cursor_x, cursor_y, '=');
536 else /* let's add a bad guy */
537 le_change(cursor_x, cursor_y, '0');
539 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_BSOD);
542 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
543 le_change(cursor_x, cursor_y, '!');
544 else /* let's add a bad guy */
545 le_change(cursor_x, cursor_y, '1');
547 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_LAPTOP);
550 le_change(cursor_x, cursor_y, '2');
552 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_MONEY);
555 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
556 le_change(cursor_x, cursor_y, '*');
562 case SDL_KEYUP: // key released
563 switch(event.key.keysym.sym)
572 case SDL_MOUSEBUTTONDOWN:
573 if(event.button.button == SDL_BUTTON_LEFT)
575 le_mouse_pressed = YES;
578 case SDL_MOUSEBUTTONUP:
579 if(event.button.button == SDL_BUTTON_LEFT)
581 le_mouse_pressed = NO;
584 case SDL_MOUSEMOTION:
590 cursor_x = ((int)(pos_x + x) / 32) * 32;
591 cursor_y = ((int) y / 32) * 32;
594 case SDL_QUIT: // window closed
604 le_change(cursor_x, cursor_y, le_current_tile);
605 if(button_pressed(&le_test_level_bt,x,y))
607 level_save(&le_current_level,"test",le_level);
608 gameloop("test",le_level, ST_GL_TEST);
609 menu_set_current(&leveleditor_menu);
611 level_load_gfx(&le_current_level);
613 le_activate_bad_guys();
619 void le_change(float x, float y, unsigned char c)
624 level_change(&le_current_level,x,y,c);
629 /* if there is a bad guy over there, remove it */
630 for(i = 0; i < num_bad_guys; ++i)
631 if (bad_guys[i].base.alive)
632 if(xx == bad_guys[i].base.x/32 && yy == bad_guys[i].base.y/32)
633 bad_guys[i].base.alive = NO;
660 "./Del - Remove tile",
661 "F9 - Show/Hide Grid",
665 text_drawf(&blue_text, "- Help -", 0, 30, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
666 text_draw(&gold_text, "Keys:", 80, 60, 1, NO_UPDATE);
668 for(i = 0; i < sizeof(text)/sizeof(char *); i++)
669 text_draw(&white_text, text[i], 40, 90+(i*16), 1, NO_UPDATE);
671 text_drawf(&gold_text, "Press Any Key to Continue", 0, 460, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
678 while(SDL_PollEvent(&event))
681 case SDL_MOUSEBUTTONDOWN: // mouse pressed
682 case SDL_KEYDOWN: // key pressed