-/***************************************************************************
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-
-/* December 28, 2003 - March 15, 2004 */
-
-/* leveleditor.c - A built-in level editor for SuperTux
- Ricardo Cruz <rick2@aeiou.pt>
- Tobias Glaesser <tobi.web@gmx.de> */
+// $Id$
+//
+// SuperTux
+// Copyright (C) 2003 Ricardo Cruz <rick2@aeiou.pt>
+// Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <map>
#include <stdio.h>
#include <SDL_image.h>
#include "leveleditor.h"
-#include "world.h"
#include "screen.h"
#include "defines.h"
#include "globals.h"
#include "resources.h"
/* definitions to aid development */
-#define DONE_LEVELEDITOR 1
-#define DONE_QUIT 2
-#define DONE_CHANGELEVEL 3
/* definitions that affect gameplay */
#define KEY_CURSOR_SPEED 32
void update_subset_settings_menu();
void save_subset_settings_menu();
+static Level* le_current_level;
+
+struct LevelEditorWorld
+{
+ std::vector<BadGuy> bad_guys;
+ void arrays_free(void)
+ {
+ bad_guys.clear();
+ }
+
+ void add_bad_guy(float x, float y, BadGuyKind kind)
+ {
+ bad_guys.push_back(BadGuy());
+ BadGuy& new_bad_guy = bad_guys.back();
+
+ new_bad_guy.init(x,y,kind, false /* stay_on_platform */);
+ }
+
+ void activate_bad_guys()
+ {
+ for (std::vector<BadGuyData>::iterator i = le_current_level->badguy_data.begin();
+ i != le_current_level->badguy_data.end();
+ ++i)
+ {
+ add_bad_guy(i->x, i->y, i->kind);
+ }
+ }
+};
+
/* leveleditor internals */
static string_list_type level_subsets;
static bool le_level_changed; /* if changes, ask for saving, when quiting*/
static int pos_x, cursor_x, cursor_y, fire;
static int le_level;
-static Level* le_current_level;
-static World le_world;
+static LevelEditorWorld le_world;
static st_subset le_level_subset;
static int le_show_grid;
static int le_frame;
updatescreen();
while (SDL_PollEvent(&event))
- {}
+ {}
while(true)
{
le_checkevents();
- if(current_menu == select_tilegroup_menu)
+ if(Menu::current() == select_tilegroup_menu)
{
if(select_tilegroup_menu_effect.check())
{
/* draw editor interface */
le_drawinterface();
- if(show_menu)
+ Menu* menu = Menu::current();
+ if(menu)
{
- menu_process_current();
- if(current_menu == leveleditor_menu)
+ menu->draw();
+ menu->action();
+
+ if(menu == leveleditor_menu)
{
switch (leveleditor_menu->check())
{
case 2:
- show_menu = false;
+ Menu::set_current(0);
break;
case 3:
update_subset_settings_menu();
break;
case 7:
- done = DONE_LEVELEDITOR;
+ done = 1;
break;
}
}
- else if(current_menu == level_settings_menu)
+ else if(menu == level_settings_menu)
{
switch (level_settings_menu->check())
{
apply_level_settings_menu();
Menu::set_current(leveleditor_menu);
break;
+
default:
- show_menu = true;
+ //show_menu = true;
break;
}
}
- else if(current_menu == select_tilegroup_menu)
+ else if(menu == select_tilegroup_menu)
{
- int it = -1;
+ int it = -1;
switch (it = select_tilegroup_menu->check())
{
default:
if(it != -1)
- {
- if(select_tilegroup_menu->item[it].kind == MN_ACTION)
- cur_tilegroup = select_tilegroup_menu->item[it].text;
+ {
+ if(select_tilegroup_menu->item[it].kind == MN_ACTION)
+ cur_tilegroup = select_tilegroup_menu->item[it].text;
- show_menu = false;
- }
+ Menu::set_current(0);
+ }
break;
}
}
- else if(current_menu == subset_load_menu)
+ else if(menu == subset_load_menu)
{
switch (i = subset_load_menu->check())
{
le_set_defaults();
le_current_level->load_gfx();
le_world.activate_bad_guys();
- show_menu = true;
+
+ // FIXME:?
+ Menu::set_current(leveleditor_menu);
}
break;
}
}
- else if(current_menu == subset_new_menu)
+ else if(menu == subset_new_menu)
{
if(subset_new_menu->item[2].input[0] == '\0')
subset_new_menu->item[3].kind = MN_DEACTIVE;
le_current_level->load_gfx();
le_world.activate_bad_guys();
subset_new_menu->item[2].change_input("");
- show_menu = true;
+ // FIXME:? show_menu = true;
+ Menu::set_current(leveleditor_menu);
break;
}
}
}
- else if(current_menu == subset_settings_menu)
+ else if(menu == subset_settings_menu)
{
if(le_level_subset.title.compare(subset_settings_menu->item[2].input) == 0 && le_level_subset.description.compare(subset_settings_menu->item[3].input) == 0 )
subset_settings_menu->item[5].kind = MN_DEACTIVE;
{
case 5:
save_subset_settings_menu();
- show_menu = true;
+ //FIXME:show_menu = true;
+ Menu::set_current(leveleditor_menu);
break;
}
}
return 0;
}
- if(done == DONE_QUIT)
- {
- le_quit();
- return 1;
- }
-
++global_frame_counter;
SDL_Delay(25);
leveleditor_menu->additem(MN_HL,"",0,0);
leveleditor_menu->additem(MN_ACTION,"Quit Level Editor",0,0);
- menu_reset();
Menu::set_current(leveleditor_menu);
- show_menu = true;
-
+
subset_load_menu->additem(MN_LABEL, "Load Level Subset", 0, 0);
subset_load_menu->additem(MN_HL, "", 0, 0);
}
else
{
- if(show_menu == false)
+ if(!Menu::current())
white_small_text->draw("No Level Subset loaded - Press ESC and choose one in the menu", 10, 430, 1);
else
white_small_text->draw("No Level Subset loaded", 10, 430, 1);
while(SDL_PollEvent(&event))
{
- if(show_menu)
- current_menu->event(event);
+ if (Menu::current())
+ {
+ Menu::current()->event(event);
+ }
else
- mouse_cursor->set_state(MC_NORMAL);
-
- /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
- if(event.type == SDL_KEYDOWN || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION) && (event.motion.x > 0 && event.motion.x < screen->w - 64 &&
- event.motion.y > 0 && event.motion.y < screen->h)))
{
- switch(event.type)
+ mouse_cursor->set_state(MC_NORMAL);
+
+ /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
+ if(event.type == SDL_KEYDOWN
+ || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION)
+ && (event.motion.x > 0
+ && event.motion.x < screen->w - 64 &&
+ event.motion.y > 0 && event.motion.y < screen->h)))
{
- case SDL_KEYDOWN: // key pressed
- key = event.key.keysym.sym;
- if(show_menu)
+ switch(event.type)
{
- if(key == SDLK_ESCAPE)
+ case SDL_KEYDOWN: // key pressed
+ key = event.key.keysym.sym;
+ switch(key)
{
- show_menu = false;
+ case SDLK_ESCAPE:
Menu::set_current(leveleditor_menu);
- }
- break;
- }
- switch(key)
- {
- case SDLK_ESCAPE:
- if(!show_menu)
- show_menu = true;
- else
- show_menu = false;
- break;
- case SDLK_LEFT:
- if(fire == DOWN)
- cursor_x -= KEY_CURSOR_SPEED;
- else
- cursor_x -= KEY_CURSOR_FASTSPEED;
+ case SDLK_LEFT:
+ if(fire == DOWN)
+ cursor_x -= KEY_CURSOR_SPEED;
+ else
+ cursor_x -= KEY_CURSOR_FASTSPEED;
- if(cursor_x < pos_x + MOUSE_LEFT_MARGIN)
- pos_x = cursor_x - MOUSE_LEFT_MARGIN;
+ if(cursor_x < pos_x + MOUSE_LEFT_MARGIN)
+ pos_x = cursor_x - MOUSE_LEFT_MARGIN;
- break;
- case SDLK_RIGHT:
- if(fire == DOWN)
- cursor_x += KEY_CURSOR_SPEED;
- else
- cursor_x += KEY_CURSOR_FASTSPEED;
+ break;
+ case SDLK_RIGHT:
+ if(fire == DOWN)
+ cursor_x += KEY_CURSOR_SPEED;
+ else
+ cursor_x += KEY_CURSOR_FASTSPEED;
- if(cursor_x > pos_x + MOUSE_RIGHT_MARGIN-32)
- pos_x = cursor_x - MOUSE_RIGHT_MARGIN+32;
+ if(cursor_x > pos_x + MOUSE_RIGHT_MARGIN-32)
+ pos_x = cursor_x - MOUSE_RIGHT_MARGIN+32;
+ break;
+ case SDLK_UP:
+ if(fire == DOWN)
+ cursor_y -= KEY_CURSOR_SPEED;
+ else
+ cursor_y -= KEY_CURSOR_FASTSPEED;
+
+ if(cursor_y < 0)
+ cursor_y = 0;
+ break;
+ case SDLK_DOWN:
+ if(fire == DOWN)
+ cursor_y += KEY_CURSOR_SPEED;
+ else
+ cursor_y += KEY_CURSOR_FASTSPEED;
+
+ if(cursor_y > screen->h-32)
+ cursor_y = screen->h-32;
+ break;
+ case SDLK_LCTRL:
+ fire =UP;
+ break;
+ case SDLK_F1:
+ le_showhelp();
+ break;
+ case SDLK_HOME:
+ cursor_x = 0;
+ pos_x = cursor_x;
+ break;
+ case SDLK_END:
+ cursor_x = (le_current_level->width * 32) - 32;
+ pos_x = cursor_x;
+ break;
+ case SDLK_F9:
+ le_show_grid = !le_show_grid;
+ break;
+ default:
+ break;
+ }
break;
- case SDLK_UP:
- if(fire == DOWN)
- cursor_y -= KEY_CURSOR_SPEED;
- else
- cursor_y -= KEY_CURSOR_FASTSPEED;
-
- if(cursor_y < 0)
- cursor_y = 0;
- break;
- case SDLK_DOWN:
- if(fire == DOWN)
- cursor_y += KEY_CURSOR_SPEED;
- else
- cursor_y += KEY_CURSOR_FASTSPEED;
-
- if(cursor_y > screen->h-32)
- cursor_y = screen->h-32;
- break;
- case SDLK_LCTRL:
- fire =UP;
- break;
- case SDLK_F1:
- le_showhelp();
- break;
- case SDLK_HOME:
- cursor_x = 0;
- pos_x = cursor_x;
- break;
- case SDLK_END:
- cursor_x = (le_current_level->width * 32) - 32;
- pos_x = cursor_x;
- break;
- case SDLK_F9:
- le_show_grid = !le_show_grid;
- break;
- default:
+ case SDL_KEYUP: /* key released */
+ switch(event.key.keysym.sym)
+ {
+ case SDLK_LCTRL:
+ fire = DOWN;
+ break;
+ default:
+ break;
+ }
break;
- }
- break;
- case SDL_KEYUP: /* key released */
- switch(event.key.keysym.sym)
- {
- case SDLK_LCTRL:
- fire = DOWN;
+ case SDL_MOUSEBUTTONDOWN:
+ if(event.button.button == SDL_BUTTON_LEFT)
+ {
+ le_mouse_pressed[LEFT] = true;
+
+ selection.x1 = event.motion.x + pos_x;
+ selection.y1 = event.motion.y;
+ selection.x2 = event.motion.x + pos_x;
+ selection.y2 = event.motion.y;
+ }
+ else if(event.button.button == SDL_BUTTON_RIGHT)
+ {
+ le_mouse_pressed[RIGHT] = true;
+ }
break;
- default:
+ case SDL_MOUSEBUTTONUP:
+ if(event.button.button == SDL_BUTTON_LEFT)
+ le_mouse_pressed[LEFT] = false;
+ else if(event.button.button == SDL_BUTTON_RIGHT)
+ le_mouse_pressed[RIGHT] = false;
break;
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- if(event.button.button == SDL_BUTTON_LEFT)
- {
- le_mouse_pressed[LEFT] = true;
+ case SDL_MOUSEMOTION:
- selection.x1 = event.motion.x + pos_x;
- selection.y1 = event.motion.y;
- selection.x2 = event.motion.x + pos_x;
- selection.y2 = event.motion.y;
- }
- else if(event.button.button == SDL_BUTTON_RIGHT)
- {
- le_mouse_pressed[RIGHT] = true;
- }
- break;
- case SDL_MOUSEBUTTONUP:
- if(event.button.button == SDL_BUTTON_LEFT)
- le_mouse_pressed[LEFT] = false;
- else if(event.button.button == SDL_BUTTON_RIGHT)
- le_mouse_pressed[RIGHT] = false;
- break;
- case SDL_MOUSEMOTION:
- if(!show_menu)
- {
- x = event.motion.x;
- y = event.motion.y;
+ if(!Menu::current())
+ {
+ x = event.motion.x;
+ y = event.motion.y;
- cursor_x = ((int)(pos_x + x) / 32) * 32;
- cursor_y = ((int) y / 32) * 32;
+ cursor_x = ((int)(pos_x + x) / 32) * 32;
+ cursor_y = ((int) y / 32) * 32;
- if(le_mouse_pressed[LEFT])
- {
- selection.x2 = x + pos_x;
- selection.y2 = y;
- }
+ if(le_mouse_pressed[LEFT])
+ {
+ selection.x2 = x + pos_x;
+ selection.y2 = y;
+ }
- if(le_mouse_pressed[RIGHT])
- {
- pos_x += -1 * event.motion.xrel;
+ if(le_mouse_pressed[RIGHT])
+ {
+ pos_x += -1 * event.motion.xrel;
+ }
}
+ break;
+ case SDL_QUIT: // window closed
+ done = 1;
+ break;
+ default:
+ break;
}
- break;
- case SDL_QUIT: // window closed
- done = DONE_QUIT;
- break;
- default:
- break;
}
}
if(le_current_level != NULL)
{
if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION) && (event.motion.x > screen->w-64 && event.motion.x < screen->w &&
- event.motion.y > 0 && event.motion.y < screen->h)))
+ event.motion.y > 0 && event.motion.y < screen->h)))
{
le_mouse_pressed[LEFT] = false;
le_mouse_pressed[RIGHT] = false;
- if(show_menu == false)
+ if(!Menu::current())
{
/* Check for button events */
le_test_level_bt->event(event);
le_current_level->save(le_level_subset.name.c_str(),le_level);
le_exit_bt->event(event);
if(le_exit_bt->get_state() == BUTTON_CLICKED)
- {
- Menu::set_current(leveleditor_menu);
- show_menu = true;
- }
+ {
+ Menu::set_current(leveleditor_menu);
+ }
le_next_level_bt->event(event);
if(le_next_level_bt->get_state() == BUTTON_CLICKED)
{
Menu::set_current(select_tilegroup_menu);
select_tilegroup_menu_effect.start(200);
select_tilegroup_menu->set_pos(screen->w - 64,100,-0.5,0.5);
- show_menu = true;
}
le_settings_bt->event(event);
{
update_level_settings_menu();
Menu::set_current(level_settings_menu);
- show_menu = true;
}
if(!cur_tilegroup.empty())
- if((pbutton = tilegroups_map[cur_tilegroup]->event(event)) != NULL)
- {
- if(pbutton->get_state() == BUTTON_CLICKED)
- {
- le_current_tile = pbutton->get_tag();
- }
- }
+ if((pbutton = tilegroups_map[cur_tilegroup]->event(event)) != NULL)
+ {
+ if(pbutton->get_state() == BUTTON_CLICKED)
+ {
+ le_current_tile = pbutton->get_tag();
+ }
+ }
if((pbutton = le_tilemap_panel->event(event)) != NULL)
- {
- if(pbutton->get_state() == BUTTON_CLICKED)
- {
- active_tm = static_cast<TileMapType>(pbutton->get_tag());
- }
- }
+ {
+ if(pbutton->get_state() == BUTTON_CLICKED)
+ {
+ active_tm = static_cast<TileMapType>(pbutton->get_tag());
+ }
+ }
}
else
{
le_settings_bt->event(event);
if(le_settings_bt->get_state() == BUTTON_CLICKED)
{
- Menu::set_current(leveleditor_menu);
- show_menu = false;
+ Menu::set_current(0);
}
le_tilegroup_bt->event(event);
if(le_tilegroup_bt->get_state() == BUTTON_CLICKED)
{
- Menu::set_current(leveleditor_menu);
- show_menu = false;
+ Menu::set_current(0);
}
}
}
- if(show_menu == false)
+
+ if(!Menu::current())
{
le_move_left_bt->event(event);
le_move_right_bt->event(event);
}
}
}
- if(show_menu == false)
+ if(!Menu::current())
{
if(le_move_left_bt->get_state() == BUTTON_PRESSED)
{
/* if there is a bad guy over there, remove it */
for(i = 0; i < le_world.bad_guys.size(); ++i)
if(xx == le_world.bad_guys[i].base.x/32 && yy == le_world.bad_guys[i].base.y/32)
- le_world.bad_guys.erase(static_cast<std::vector<BadGuy>::iterator>(&le_world.bad_guys[i]));
+ le_world.bad_guys.erase(le_world.bad_guys.begin() + i);
if(c == '0') /* if it's a bad guy */
le_world.add_bad_guy(xx*32, yy*32, BAD_BSOD);
void le_showhelp()
{
SDL_Event event;
- unsigned int i, done;
+ unsigned int i, done_;
char *text[] = {
" - This is SuperTux's built-in level editor -",
"It has been designed to be light and easy to use from the start.",
blue_text->drawf("- Help -", 0, 30, A_HMIDDLE, A_TOP, 2);
for(i = 0; i < sizeof(text)/sizeof(char *); i++)
- white_text->draw(text[i], 5, 80+(i*18), 1);
+ white_small_text->draw(text[i], 5, 80+(i*white_small_text->h), 1);
gold_text->drawf("Press Any Key to Continue", 0, 440, A_HMIDDLE, A_TOP, 1);
flipscreen();
- done = 0;
+ done_ = 0;
- while(done == 0)
+ while(done_ == 0)
{
- done = wait_for_event(event);
+ done_ = wait_for_event(event);
SDL_Delay(50);
}
}