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 MOUSE_LEFT_MARGIN 80
46 #define MOUSE_RIGHT_MARGIN (560-32)
47 /* right_margin should noticed that the cursor is 32 pixels,
48 so it should subtract that value */
49 #define MOUSE_POS_SPEED 20
51 /* gameloop funcs declerations */
53 void loadshared(void);
54 void unloadshared(void);
56 /* own declerations */
57 /* crutial ones (main loop) */
61 void le_checkevents();
62 void le_change(float x, float y, unsigned char c);
65 void le_set_defaults(void);
66 void le_activate_bad_guys(void);
68 /* leveleditor internals */
69 static int le_level_changed; /* if changes, ask for saving, when quiting*/
70 static int pos_x, cursor_x, cursor_y, cursor_tile, fire;
72 static st_level le_current_level;
73 static st_subset le_level_subset;
74 static int le_show_grid;
76 static texture_type le_selection;
78 static char le_current_tile;
79 static int le_mouse_pressed;
80 static button_type le_test_level_bt;
81 static button_type le_next_level_bt;
82 static button_type le_previous_level_bt;
83 static button_type le_rubber_bt;
85 static SDL_Event event;
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 /* making events results to be in order */
136 if(pos_x > (le_current_level.width * 32) - screen->w)
137 pos_x = (le_current_level.width * 32) - screen->w;
144 menu_process_current();
145 if(current_menu == &leveleditor_menu)
147 switch (menu_check(&leveleditor_menu))
153 done = DONE_LEVELEDITOR;
165 if(done == DONE_QUIT)
172 now_time = SDL_GetTicks();
173 if (now_time < last_time + FPS)
174 SDL_Delay(last_time + FPS - now_time); /* delay some time */
184 subset_load(&le_level_subset,"default");
187 // level_changed = NO;
191 menu_set_current(&leveleditor_menu);
192 le_frame = 0; /* support for frames in some tiles, like waves and bad guys */
198 le_level_changed = NO;
199 if(level_load(&le_current_level, le_level_subset.name, le_level) != 0)
204 if(le_current_level.time_left == 0)
205 le_current_level.time_left = 255;
207 level_load_gfx(&le_current_level);
209 le_current_tile = '.';
210 le_mouse_pressed = NO;
211 le_activate_bad_guys();
213 texture_load(&le_selection,DATA_PREFIX "/images/leveleditor/select.png", USE_ALPHA);
214 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);
215 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);
216 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);
217 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);
219 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
226 /*if(level_changed == YES)
227 if(askforsaving() == CANCEL)
230 SDL_EnableKeyRepeat(0, 0); // disables key repeating
232 button_free(&le_test_level_bt);
234 level_free(&le_current_level);
237 texture_free(&le_selection);
243 static char str[LEVEL_NAME_MAX];
245 /* Draw the real background */
246 if(le_current_level.bkgd_image[0] != '\0')
249 texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s - 32, img_bkgd.h, NO_UPDATE);
250 texture_draw_part(&img_bkgd,0,0,screen->w - s - 32 ,0,s,img_bkgd.h, NO_UPDATE);
254 clearscreen(le_current_level.bkgd_red, le_current_level.bkgd_green, le_current_level.bkgd_blue);
257 /* clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue); */
259 for (y = 0; y < 15; ++y)
260 for (x = 0; x < 20; ++x)
262 drawshape(x * 32 - ((int)pos_x % 32), y * 32, le_current_level.tiles[y][x + (int)(pos_x / 32)]);
264 /* draw whats inside stuff when cursor is selecting those */
265 /* (draw them all the time - is this the right behaviour?) */
266 switch(le_current_level.tiles[y][x + (int)(pos_x/32)])
269 texture_draw(&img_mints, x * 32 - ((int)pos_x % 32), y*32, NO_UPDATE);
272 texture_draw(&img_golden_herring, x * 32 - ((int)pos_x % 32), y*32, NO_UPDATE);
277 texture_draw(&img_distro[(frame / 5) % 4], x * 32 - ((int)pos_x % 32), y*32, NO_UPDATE);
284 /* Draw the Bad guys: */
285 for (i = 0; i < num_bad_guys; ++i)
287 if(bad_guys[i].base.alive == NO)
289 /* to support frames: img_bsod_left[(frame / 5) % 4] */
290 if(bad_guys[i].kind == BAD_BSOD)
291 texture_draw(&img_bsod_left[(le_frame / 5) % 4], bad_guys[i].base.x - pos_x, bad_guys[i].base.y, NO_UPDATE);
292 else if(bad_guys[i].kind == BAD_LAPTOP)
293 texture_draw(&img_laptop_left[(le_frame / 5) % 3], bad_guys[i].base.x - pos_x, bad_guys[i].base.y, NO_UPDATE);
294 else if (bad_guys[i].kind == BAD_MONEY)
295 texture_draw(&img_money_left[(le_frame / 5) % 2], bad_guys[i].base.x - pos_x, bad_guys[i].base.y, NO_UPDATE);
298 /* Draw the player: */
299 // for now, the position is fixed at (0, 240)
300 texture_draw(&tux_right[(frame / 5) % 3], 0 - pos_x, 240, NO_UPDATE);
302 /* draw a grid (if selected) */
305 for(x = 0; x < 20; x++)
306 fillrect(x*32 - ((int)pos_x % 32), 0, 1, screen->h, 225, 225, 225,255);
307 for(y = 0; y < 15; y++)
308 fillrect(0, y*32, screen->w - 32, 1, 225, 225, 225,255);
311 texture_draw(&le_selection, cursor_x - pos_x, cursor_y, NO_UPDATE);
313 /* draw button bar */
314 fillrect(screen->w - 32, 0, 32, screen->h, 50, 50, 50,255);
315 drawshape(19 * 32, 14 * 32, le_current_tile);
317 button_draw(&le_test_level_bt);
318 button_draw(&le_next_level_bt);
319 button_draw(&le_previous_level_bt);
320 button_draw(&le_rubber_bt);
322 sprintf(str, "%d", le_current_level.time_left);
323 text_draw(&white_text, "TIME", 324, 0, 1, NO_UPDATE);
324 text_draw(&gold_text, str, 404, 0, 1, NO_UPDATE);
326 text_draw(&white_text, "NAME", 0, 0, 1, NO_UPDATE);
327 text_draw(&gold_text, le_current_level.name, 80, 0, 1, NO_UPDATE);
329 sprintf(str, "%d/%d", le_level,le_level_subset.levels);
330 text_draw(&white_text, "NUMB", 0, 20, 1, NO_UPDATE);
331 text_draw(&gold_text, str, 80, 20, 1, NO_UPDATE);
333 text_draw(&white_small_text, "F1 for Help", 10, 430, 1, NO_UPDATE);
334 text_draw(&white_small_text, "F2 for Testing", 150, 430, 1, NO_UPDATE);
337 void le_checkevents()
343 keymod = SDL_GetModState();
345 while(SDL_PollEvent(&event))
347 /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
350 case SDL_KEYDOWN: // key pressed
351 key = event.key.keysym.sym;
354 menu_event(&event.key.keysym);
367 cursor_x -= KEY_CURSOR_SPEED;
369 cursor_x -= KEY_CURSOR_FASTSPEED;
371 if(cursor_x < pos_x + MOUSE_LEFT_MARGIN)
372 pos_x = cursor_x - MOUSE_LEFT_MARGIN;
377 cursor_x += KEY_CURSOR_SPEED;
379 cursor_x += KEY_CURSOR_FASTSPEED;
381 if(cursor_x > pos_x + MOUSE_RIGHT_MARGIN-32)
382 pos_x = cursor_x - MOUSE_RIGHT_MARGIN+32;
387 cursor_y -= KEY_CURSOR_SPEED;
389 cursor_y -= KEY_CURSOR_FASTSPEED;
396 cursor_y += KEY_CURSOR_SPEED;
398 cursor_y += KEY_CURSOR_FASTSPEED;
400 if(cursor_y > screen->h-32)
401 cursor_y = screen->h-32;
410 level_save(&le_current_level,"test",le_level);
411 gameloop("test",le_level, ST_GL_TEST);
412 menu_set_current(&leveleditor_menu);
414 level_load_gfx(&le_current_level);
416 le_activate_bad_guys();
423 cursor_x = (le_current_level.width * 32) - 32;
427 cursor_x -= PAGE_CURSOR_SPEED;
429 if(cursor_x < pos_x + MOUSE_LEFT_MARGIN)
430 pos_x = cursor_x - MOUSE_LEFT_MARGIN;
434 cursor_x += PAGE_CURSOR_SPEED;
436 if(cursor_x > pos_x + MOUSE_RIGHT_MARGIN-32)
437 pos_x = cursor_x - MOUSE_RIGHT_MARGIN+32;
441 le_show_grid = !le_show_grid;
444 le_change(cursor_x, cursor_y, '.');
447 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
448 le_change(cursor_x, cursor_y, 'A');
450 le_change(cursor_x, cursor_y, 'a');
453 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
454 le_change(cursor_x, cursor_y, 'B');
457 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
458 le_change(cursor_x, cursor_y, 'C');
460 le_change(cursor_x, cursor_y, 'c');
463 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
464 le_change(cursor_x, cursor_y, 'D');
466 le_change(cursor_x, cursor_y, 'd');
469 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
470 le_change(cursor_x, cursor_y, 'E');
472 le_change(cursor_x, cursor_y, 'e');
475 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
476 le_change(cursor_x, cursor_y, 'F');
478 le_change(cursor_x, cursor_y, 'f');
481 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
482 le_change(cursor_x, cursor_y, 'G');
484 le_change(cursor_x, cursor_y, 'g');
487 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
488 le_change(cursor_x, cursor_y, 'H');
490 le_change(cursor_x, cursor_y, 'h');
493 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
494 le_change(cursor_x, cursor_y, 'I');
496 le_change(cursor_x, cursor_y, 'i');
499 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
500 le_change(cursor_x, cursor_y, 'J');
502 le_change(cursor_x, cursor_y, 'j');
505 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
506 le_change(cursor_x, cursor_y, 'X');
508 le_change(cursor_x, cursor_y, 'x');
511 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
512 le_change(cursor_x, cursor_y, 'Y');
514 le_change(cursor_x, cursor_y, 'y');
516 case SDLK_LEFTBRACKET:
517 le_change(cursor_x, cursor_y, '[');
519 case SDLK_RIGHTBRACKET:
520 le_change(cursor_x, cursor_y, ']');
524 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
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, '$');
533 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
534 le_change(cursor_x, cursor_y, '|');
536 le_change(cursor_x, cursor_y, '\\');
539 le_change(cursor_x, cursor_y, '^');
543 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
544 le_change(cursor_x, cursor_y, '&');
548 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
549 le_change(cursor_x, cursor_y, '=');
550 else /* let's add a bad guy */
551 le_change(cursor_x, cursor_y, '0');
553 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_BSOD);
556 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
557 le_change(cursor_x, cursor_y, '!');
558 else /* let's add a bad guy */
559 le_change(cursor_x, cursor_y, '1');
561 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_LAPTOP);
564 le_change(cursor_x, cursor_y, '2');
566 add_bad_guy((((int)cursor_x/32)*32), (((int)cursor_y/32)*32), BAD_MONEY);
569 if(keymod == KMOD_LSHIFT || keymod == KMOD_RSHIFT || keymod == KMOD_CAPS)
570 le_change(cursor_x, cursor_y, '*');
576 case SDL_KEYUP: // key released
577 switch(event.key.keysym.sym)
586 case SDL_MOUSEBUTTONDOWN:
587 if(event.button.button == SDL_BUTTON_LEFT)
589 le_mouse_pressed = YES;
592 case SDL_MOUSEBUTTONUP:
593 if(event.button.button == SDL_BUTTON_LEFT)
595 le_mouse_pressed = NO;
598 case SDL_MOUSEMOTION:
604 cursor_x = ((int)(pos_x + x) / 32) * 32;
605 cursor_y = ((int) y / 32) * 32;
608 case SDL_QUIT: // window closed
618 le_change(cursor_x, cursor_y, le_current_tile);
619 if(button_pressed(&le_test_level_bt,x,y))
623 if(!show_menu) /* scroll screen when mouse is in a margin */
625 if(event.motion.x > MOUSE_RIGHT_MARGIN && event.motion.x < screen->w-32)
626 pos_x += MOUSE_POS_SPEED;
627 else if(event.motion.x > 0 && event.motion.x < MOUSE_LEFT_MARGIN)
628 pos_x -= MOUSE_POS_SPEED;
632 void le_change(float x, float y, unsigned char c)
637 level_change(&le_current_level,x,y,c);
642 /* if there is a bad guy over there, remove it */
643 for(i = 0; i < num_bad_guys; ++i)
644 if (bad_guys[i].base.alive)
645 if(xx == bad_guys[i].base.x/32 && yy == bad_guys[i].base.y/32)
646 bad_guys[i].base.alive = NO;
651 level_save(&le_current_level,"test",le_level);
652 gameloop("test",le_level, ST_GL_TEST);
653 menu_set_current(&leveleditor_menu);
655 level_load_gfx(&le_current_level);
657 le_activate_bad_guys();
684 "./Del - Remove tile",
685 "F9 - Show/Hide Grid",
689 text_drawf(&blue_text, "- Help -", 0, 30, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
690 text_draw(&gold_text, "Keys:", 80, 60, 1, NO_UPDATE);
692 for(i = 0; i < sizeof(text)/sizeof(char *); i++)
693 text_draw(&white_text, text[i], 40, 90+(i*16), 1, NO_UPDATE);
695 text_drawf(&gold_text, "Press Any Key to Continue", 0, 460, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
702 while(SDL_PollEvent(&event))
705 case SDL_MOUSEBUTTONDOWN: // mouse pressed
706 case SDL_KEYDOWN: // key pressed