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)
239 SDL_EnableKeyRepeat(0, 0); // disables key repeating
241 button_free(&le_test_level_bt);
243 level_free(&le_current_level);
246 texture_free(&le_selection);
252 static char str[LEVEL_NAME_MAX];
254 /* Draw the real background */
255 if(le_current_level.bkgd_image[0] != '\0')
258 texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s - 32, img_bkgd.h, NO_UPDATE);
259 texture_draw_part(&img_bkgd,0,0,screen->w - s - 32 ,0,s,img_bkgd.h, NO_UPDATE);
263 clearscreen(le_current_level.bkgd_red, le_current_level.bkgd_green, le_current_level.bkgd_blue);
266 /* clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue); */
268 for (y = 0; y < 15; ++y)
269 for (x = 0; x < 19; ++x)
271 drawshape(x * 32, y * 32, le_current_level.tiles[y][x + (pos_x / 32)]);
274 /* draw whats inside stuff when cursor is selecting those */
275 cursor_tile = le_current_level.tiles[cursor_y/32][cursor_x/32];
279 texture_draw(&img_mints, cursor_x - pos_x, cursor_y, NO_UPDATE);
282 texture_draw(&img_golden_herring, cursor_x - pos_x, cursor_y, NO_UPDATE);
287 texture_draw(&img_distro[(le_frame / 5) % 4], cursor_x - pos_x, cursor_y, NO_UPDATE);
293 /* Draw the Bad guys: */
294 for (i = 0; i < num_bad_guys; ++i)
296 if(bad_guys[i].base.alive == NO)
298 /* to support frames: img_bsod_left[(frame / 5) % 4] */
299 if(bad_guys[i].kind == BAD_BSOD)
300 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);
301 else if(bad_guys[i].kind == BAD_LAPTOP)
302 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);
303 else if (bad_guys[i].kind == BAD_MONEY)
304 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);
307 /* draw a grid (if selected) */
310 for(x = 0; x < 19; x++)
311 fillrect(x*32, 0, 1, screen->h, 225, 225, 225,255);
312 for(y = 0; y < 15; y++)
313 fillrect(0, y*32, screen->w - 32, 1, 225, 225, 225,255);
316 fillrect(screen->w - 32, 0, 32, screen->h, 50, 50, 50,255);
317 drawshape(19 * 32, 14 * 32, le_current_tile);
319 button_draw(&le_test_level_bt);
320 button_draw(&le_next_level_bt);
321 button_draw(&le_previous_level_bt);
322 button_draw(&le_rubber_bt);
324 texture_draw(&le_selection, ((int)(cursor_x - pos_x)/32)*32, cursor_y, NO_UPDATE);
326 sprintf(str, "%d", le_current_level.time_left);
327 text_draw(&white_text, "TIME", 324, 0, 1, NO_UPDATE);
328 text_draw(&gold_text, str, 404, 0, 1, NO_UPDATE);
330 text_draw(&white_text, "NAME", 0, 0, 1, NO_UPDATE);
331 text_draw(&gold_text, le_current_level.name, 80, 0, 1, NO_UPDATE);
333 sprintf(str, "%d/%d", le_level,le_level_subset.levels);
334 text_draw(&white_text, "NUMB", 0, 20, 1, NO_UPDATE);
335 text_draw(&gold_text, str, 80, 20, 1, NO_UPDATE);
337 text_draw(&white_small_text, "F1 for Help", 10, 430, 1, NO_UPDATE);
338 text_draw(&white_small_text, "F2 for Testing", 150, 430, 1, NO_UPDATE);
341 void le_checkevents()
348 keymod = SDL_GetModState();
350 while(SDL_PollEvent(&event))
352 /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
355 case SDL_KEYDOWN: // key pressed
356 key = event.key.keysym.sym;
359 menu_event(&event.key.keysym);
372 cursor_x -= KEY_CURSOR_SPEED;
374 cursor_x -= KEY_CURSOR_FASTSPEED;
378 cursor_x += KEY_CURSOR_SPEED;
380 cursor_x += KEY_CURSOR_FASTSPEED;
384 cursor_y -= KEY_CURSOR_SPEED;
386 cursor_y -= KEY_CURSOR_FASTSPEED;
393 cursor_y += KEY_CURSOR_SPEED;
395 cursor_y += KEY_CURSOR_FASTSPEED;
397 if(cursor_y > screen->h-32)
398 cursor_y = screen->h-32;
407 level_save(&le_current_level,"test",le_level);
408 gameloop("test",le_level, ST_GL_TEST);
409 menu_set_current(&leveleditor_menu);
411 level_load_gfx(&le_current_level);
413 le_activate_bad_guys();
419 cursor_x = (le_current_level.width * 32) - 32;
422 cursor_x -= PAGE_CURSOR_SPEED;
425 cursor_x += PAGE_CURSOR_SPEED;
428 le_show_grid = !le_show_grid;
431 le_change(cursor_x, cursor_y, '.');
434 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
435 le_change(cursor_x, cursor_y, 'A');
437 le_change(cursor_x, cursor_y, 'a');
440 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
441 le_change(cursor_x, cursor_y, 'B');
444 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
445 le_change(cursor_x, cursor_y, 'C');
447 le_change(cursor_x, cursor_y, 'c');
450 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
451 le_change(cursor_x, cursor_y, 'D');
453 le_change(cursor_x, cursor_y, 'd');
456 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
457 le_change(cursor_x, cursor_y, 'E');
459 le_change(cursor_x, cursor_y, 'e');
462 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
463 le_change(cursor_x, cursor_y, 'F');
465 le_change(cursor_x, cursor_y, 'f');
468 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
469 le_change(cursor_x, cursor_y, 'G');
471 le_change(cursor_x, cursor_y, 'g');
474 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
475 le_change(cursor_x, cursor_y, 'H');
477 le_change(cursor_x, cursor_y, 'h');
480 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
481 le_change(cursor_x, cursor_y, 'I');
483 le_change(cursor_x, cursor_y, 'i');
486 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
487 le_change(cursor_x, cursor_y, 'J');
489 le_change(cursor_x, cursor_y, 'j');
492 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
493 le_change(cursor_x, cursor_y, 'X');
495 le_change(cursor_x, cursor_y, 'x');
498 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
499 le_change(cursor_x, cursor_y, 'Y');
501 le_change(cursor_x, cursor_y, 'y');
503 case SDLK_LEFTBRACKET:
504 le_change(cursor_x, cursor_y, '[');
506 case SDLK_RIGHTBRACKET:
507 le_change(cursor_x, cursor_y, ']');
511 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
512 le_change(cursor_x, cursor_y, '#');
516 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
517 le_change(cursor_x, cursor_y, '$');
520 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
521 le_change(cursor_x, cursor_y, '|');
523 le_change(cursor_x, cursor_y, '\\');
526 le_change(cursor_x, cursor_y, '^');
530 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
531 le_change(cursor_x, cursor_y, '&');
535 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
536 le_change(cursor_x, cursor_y, '=');
537 else /* let's add a bad guy */
538 le_change(cursor_x, cursor_y, '0');
540 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_BSOD);
543 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
544 le_change(cursor_x, cursor_y, '!');
545 else /* let's add a bad guy */
546 le_change(cursor_x, cursor_y, '1');
548 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_LAPTOP);
551 le_change(cursor_x, cursor_y, '2');
553 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_MONEY);
556 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
557 le_change(cursor_x, cursor_y, '*');
563 case SDL_KEYUP: // key released
564 switch(event.key.keysym.sym)
573 case SDL_MOUSEBUTTONDOWN:
574 if(event.button.button == SDL_BUTTON_LEFT)
576 le_mouse_pressed = YES;
579 case SDL_MOUSEBUTTONUP:
580 if(event.button.button == SDL_BUTTON_LEFT)
582 le_mouse_pressed = NO;
585 case SDL_MOUSEMOTION:
591 cursor_x = ((int)(pos_x + x) / 32) * 32;
592 cursor_y = ((int) y / 32) * 32;
595 case SDL_QUIT: // window closed
605 le_change(cursor_x, cursor_y, le_current_tile);
606 if(button_pressed(&le_test_level_bt,x,y))
608 level_save(&le_current_level,"test",le_level);
609 gameloop("test",le_level, ST_GL_TEST);
610 menu_set_current(&leveleditor_menu);
612 level_load_gfx(&le_current_level);
614 le_activate_bad_guys();
620 void le_change(float x, float y, unsigned char c)
625 level_change(&le_current_level,x,y,c);
630 /* if there is a bad guy over there, remove it */
631 for(i = 0; i < num_bad_guys; ++i)
632 if (bad_guys[i].base.alive)
633 if(xx == bad_guys[i].base.x/32 && yy == bad_guys[i].base.y/32)
634 bad_guys[i].base.alive = NO;
661 "./Del - Remove tile",
662 "F9 - Show/Hide Grid",
666 text_drawf(&blue_text, "- Help -", 0, 30, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
667 text_draw(&gold_text, "Keys:", 80, 60, 1, NO_UPDATE);
669 for(i = 0; i < sizeof(text)/sizeof(char *); i++)
670 text_draw(&white_text, text[i], 40, 90+(i*16), 1, NO_UPDATE);
672 text_drawf(&gold_text, "Press Any Key to Continue", 0, 460, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
679 while(SDL_PollEvent(&event))
682 case SDL_MOUSEBUTTONDOWN: // mouse pressed
683 case SDL_KEYDOWN: // key pressed