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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
|
/***************************************************************************
editor.c - description
-------------------
begin : Fri Oct 12 2001
copyright : (C) 2001 by Michael Speck
email : kulkanie@gmx.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "lbreakout.h"
#include "../game/game.h"
#include "game.h"
#include "config.h"
#include "bricks.h"
#include "frame.h"
#include "editor.h"
SDL_Surface *sel_frame = 0, *buttons = 0;
SDL_Surface *editor_bkgnd = 0; /* background (black with frame) of editor */
char edit_file_name[512]; /* full path of edited file */
Level *edit_levels[MAX_LEVELS]; /* editor levels */
char edit_version[16]; /* version of edited set */
int edit_level_count; /* how many levels currently used? */
int edit_cur_level_id;
Level *edit_cur_level; /* current level modified */
enum { EDITOR_BRICK, EDITOR_EXTRA };
int edit_sel_type; /* type of selected tile */
int edit_sel_id; /* brick or extra id */
int edit_sel_x, edit_sel_y; /* position in map of selected tile */
int edit_tile_x = 1, edit_tile_y = 20, edit_tile_w = MAP_WIDTH - 2, edit_tile_h = 3; /* part where either bricks ro bonuses
are displayed */
int extra_vis; /* extras currently shown? blinks. */
int first_swap_level = -1; /* if not -1 this and the current level will be swapped
next time the button is pressed */
enum {
/* tiles */
BUTTON_NONE = 0,
BUTTON_FIRST_BRICK,
BUTTON_LAST_BRICK = BUTTON_FIRST_BRICK + ( BRICK_COUNT ),
BUTTON_FIRST_EXTRA,
BUTTON_LAST_EXTRA = BUTTON_FIRST_EXTRA + ( EX_NUMBER -1 ),
BUTTON_EDIT,
BUTTON_EDIT_AUTHOR,
BUTTON_EDIT_NAME,
/* buttons */
BUTTON_FIRST,
BUTTON_NEXT,
BUTTON_PREV,
BUTTON_CLEAR,
BUTTON_SWAP,
BUTTON_ADD,
BUTTON_INSERT,
BUTTON_DELETE,
BUTTON_VERSION,
BUTTON_LOAD,
BUTTON_SAVE,
BUTTON_PLAY
};
int edit_buttons[MAP_WIDTH][MAP_HEIGHT]; /* an action is assigned to each map tile */
/* externals */
extern SDL_Surface *stk_display;
extern SDL_Surface *frame;
extern SDL_Surface *extra_pic;
extern SDL_Surface *brick_pic;
extern StkFont *mfont; /* use menu's font to draw status */
extern StkFont *font; /* use game's font to confirm */
extern Config config;
extern int stk_quit_request;
/* extra conversion table may be found in bricks.c */
extern Extra_Conv extra_conv_table[EX_NUMBER];
extern Brick_Conv brick_conv_table[BRICK_COUNT];
/*
====================================================================
Locals
====================================================================
*/
/*
====================================================================
We had a right click into the tile region so check and change the
bricks displayed there. We select the first new tile and set
edit_sel_id, edit_sel_x, edit_sel_y. The full update is initated
by editor_handle_click().
====================================================================
*/
void editor_switch_tiles()
{
int x_off = 2, y_off = 20, len = MAP_WIDTH - x_off * 2; /* offset in map of tiles, len is the number of tiles in one line */
int i, j;
/* clear edit buttons */
for ( i = edit_tile_x; i < edit_tile_x + edit_tile_w; i++ )
for ( j = edit_tile_y; j < edit_tile_y + edit_tile_h; j++ )
edit_buttons[i][j] = BUTTON_NONE;
/* clear this part of the editor bkjgnd */
stk_surface_fill( editor_bkgnd,
edit_tile_x * BRICK_WIDTH, edit_tile_y * BRICK_HEIGHT,
edit_tile_w * BRICK_WIDTH, edit_tile_h * BRICK_HEIGHT, 0x0 );
/* switch */
if ( edit_sel_type == EDITOR_BRICK ) {
/* flag */
edit_sel_type = EDITOR_EXTRA;
/* button map & background */
i = 0; j = 0;
while ( i + j * len < EX_NUMBER ) {
edit_buttons[x_off + i][y_off + j] = BUTTON_FIRST_EXTRA + i + j * len;
stk_surface_blit( extra_pic, ( i + j * len ) * BRICK_WIDTH, 0,
BRICK_WIDTH, BRICK_HEIGHT,
editor_bkgnd, ( i + x_off ) * BRICK_WIDTH, ( j + y_off ) * BRICK_HEIGHT );
i++;
if ( i == len ) {
i = 0;
j++;
}
}
/* select first tile */
edit_sel_id = 0;
edit_sel_x = x_off;
edit_sel_y = y_off;
}
else {
/* flag */
edit_sel_type = EDITOR_BRICK;
/* button map & background */
i = 0; j = 0;
while ( i + j * len < BRICK_COUNT ) {
edit_buttons[x_off + i][y_off + j] = BUTTON_FIRST_BRICK + i + j * len;
stk_surface_blit( brick_pic, ( i + j * len ) * BRICK_WIDTH, 0,
BRICK_WIDTH, BRICK_HEIGHT,
editor_bkgnd, ( i + x_off ) * BRICK_WIDTH, ( j + y_off ) * BRICK_HEIGHT );
i++;
if ( i == len ) {
i = 0;
j++;
}
}
/* select first tile */
edit_sel_id = 0;
edit_sel_x = x_off;
edit_sel_y = y_off;
}
}
/*
====================================================================
Draw a helping grid.
====================================================================
*/
void editor_draw_grid()
{
SDL_Surface *buffer;
int i, alpha;
buffer = stk_surface_create( SDL_SWSURFACE, EDIT_WIDTH * BRICK_WIDTH, 1 );
stk_surface_fill( buffer, 0,0,-1,-1, 0xffffff );
stk_surface_blit( buffer, 0,0,-1,-1, editor_bkgnd, BRICK_WIDTH, BRICK_HEIGHT );
stk_surface_blit( buffer, 0,0,-1,-1, editor_bkgnd, BRICK_WIDTH,
( EDIT_HEIGHT + 1 ) * BRICK_HEIGHT - 1 );
for ( i = 0; i < EDIT_HEIGHT - 1; i++ ) {
if ( i == EDIT_HEIGHT / 2 - 1 )
alpha = 192;
else
if ( i % (EDIT_HEIGHT/4) == 0 )
alpha = 128;
else
alpha = 64;
stk_surface_alpha_blit( buffer, 0,0,-1,-1, editor_bkgnd,
BRICK_WIDTH, ( i + 1 ) * BRICK_HEIGHT + BRICK_HEIGHT - 1,
alpha );
}
SDL_FreeSurface( buffer );
buffer = stk_surface_create( SDL_SWSURFACE, 1, EDIT_HEIGHT * BRICK_HEIGHT );
stk_surface_fill( buffer, 0,0,-1,-1, 0xffffff );
stk_surface_blit( buffer, 0,0,-1,-1, editor_bkgnd, BRICK_WIDTH, BRICK_HEIGHT );
stk_surface_blit( buffer, 0,0,-1,-1, editor_bkgnd,
( EDIT_WIDTH + 1 ) * BRICK_WIDTH - 1, BRICK_HEIGHT );
for ( i = 0; i < EDIT_WIDTH - 1; i++ ) {
if ( i == EDIT_WIDTH / 2 - 1 )
alpha = 192;
else
if ( i % (EDIT_WIDTH/4) == 0 )
alpha = 128;
else
alpha = 64;
stk_surface_alpha_blit( buffer, 0,0,-1,-1, editor_bkgnd,
( i + 1 ) * BRICK_WIDTH + BRICK_WIDTH- 1, BRICK_HEIGHT,
alpha );
}
SDL_FreeSurface( buffer );
}
/*
====================================================================
Translate the saved character strings into extra and brick indices
for the editor.
====================================================================
*/
enum { INDICES_2_CHAR, CHAR_2_INDICES };
void editor_translate_level( Level *level, int type )
{
int i, j, k;
if ( type == CHAR_2_INDICES ) {
for ( i = 0; i < EDIT_WIDTH; i++ )
for ( j = 0; j < EDIT_HEIGHT; j++ ) {
/* bricks */
for ( k = 0; k < BRICK_COUNT; k++ )
if ( brick_conv_table[k].c == level->bricks[i][j] ) {
level->bricks[i][j] = brick_conv_table[k].id;
break;
}
if ( k == BRICK_COUNT ) level->bricks[i][j] = -1;
/* extras */
for ( k = 0; k < EX_NUMBER; k++ )
if ( extra_conv_table[k].c == level->extras[i][j] ) {
level->extras[i][j] = extra_conv_table[k].type;
break;
}
if ( k == EX_NUMBER ) level->extras[i][j] = EX_NONE;
}
}
else {
/* indices to characters */
for ( i = 0; i < EDIT_WIDTH; i++ )
for ( j = 0; j < EDIT_HEIGHT; j++ ) {
/* bricks */
if ( level->bricks[i][j] == -1 )
level->bricks[i][j] = '.';
else
for ( k = 0; k < BRICK_COUNT; k++ )
if ( level->bricks[i][j] == brick_conv_table[k].id ) {
level->bricks[i][j] = brick_conv_table[k].c;
break;
}
/* extras */
if ( level->extras[i][j] == EX_NONE )
level->extras[i][j] = '.';
else
for ( k = 0; k < EX_NUMBER; k++ )
if ( level->extras[i][j] == extra_conv_table[k].type ) {
level->extras[i][j] = extra_conv_table[k].c;
break;
}
}
}
}
/*
====================================================================
Draw name of set file, current level, current count, remaining
levels, and other info stuff.
====================================================================
*/
void editor_draw_status()
{
char str[512];
int x = BRICK_WIDTH, y = ( MAP_HEIGHT - 1 ) * BRICK_HEIGHT - 2;
int height = 10;
/* locartion */
sprintf( str, "Location: %s", edit_file_name );
mfont->align = STK_FONT_ALIGN_LEFT | STK_FONT_ALIGN_TOP;
stk_font_write( mfont, stk_display, x, y, STK_OPAQUE, str );
/* current level */
sprintf( str, "Current Level: %i/%i (Free: %i)", edit_cur_level_id + 1, edit_level_count, MAX_LEVELS - edit_level_count );
mfont->align = STK_FONT_ALIGN_LEFT | STK_FONT_ALIGN_TOP;
stk_font_write( mfont, stk_display, x, y + height, STK_OPAQUE, str );
/* swap */
mfont->align = STK_FONT_ALIGN_RIGHT | STK_FONT_ALIGN_TOP;
if ( first_swap_level != -1 ) {
sprintf( str, "*** Level %i Marked For Swap ***", first_swap_level + 1 );
stk_font_write( mfont, stk_display, stk_display->w - BRICK_WIDTH, y + height, STK_OPAQUE, str );
}
else {
/* version */
sprintf( str, "Version: %s ", edit_version );
stk_font_write( mfont, stk_display, stk_display->w - BRICK_WIDTH, y + height, STK_OPAQUE, str );
}
/* name and author */
mfont->align = STK_FONT_ALIGN_LEFT | STK_FONT_ALIGN_TOP;
sprintf( str, "Title: %s", edit_cur_level->name );
stk_font_write( mfont, stk_display, BRICK_WIDTH + 2, ( MAP_HEIGHT - 5 ) * BRICK_HEIGHT + 5, STK_OPAQUE, str );
mfont->align = STK_FONT_ALIGN_RIGHT | STK_FONT_ALIGN_TOP;
sprintf( str, "Author: %s", edit_cur_level->author );
stk_font_write( mfont, stk_display, stk_display->w - BRICK_WIDTH - 2, ( MAP_HEIGHT - 5 ) * BRICK_HEIGHT + 5, STK_OPAQUE, str );
}
/*
====================================================================
Draw brick and extra (if any) from screen map pos
so it fits the editable field (no frame, no bottom).
====================================================================
*/
void editor_draw_brick( int edit_map_x, int edit_map_y )
{
/* brick */
if ( edit_cur_level->bricks[edit_map_x][edit_map_y] != -1 ) {
if ( edit_cur_level->bricks[edit_map_x][edit_map_y] != INVIS_BRICK_ID )
stk_surface_blit( brick_pic,
edit_cur_level->bricks[edit_map_x][edit_map_y] * BRICK_WIDTH, 0,
BRICK_WIDTH, BRICK_HEIGHT,
stk_display, (edit_map_x + 1) * BRICK_WIDTH, (edit_map_y + 1) * BRICK_HEIGHT );
else
stk_surface_fill( stk_display,
(edit_map_x + 1) * BRICK_WIDTH, (edit_map_y + 1) * BRICK_HEIGHT,
BRICK_WIDTH, BRICK_HEIGHT, 0x777777 );
}
else {
stk_surface_blit( editor_bkgnd,
(edit_map_x + 1) * BRICK_WIDTH, (edit_map_y + 1) * BRICK_HEIGHT,
BRICK_WIDTH, BRICK_HEIGHT, stk_display,
(edit_map_x + 1) * BRICK_WIDTH, (edit_map_y + 1) * BRICK_HEIGHT );
}
/* extra */
if ( ( extra_vis || edit_sel_type == EDITOR_EXTRA ) && edit_cur_level->extras[edit_map_x][edit_map_y] != EX_NONE) {
stk_surface_blit( extra_pic,
( edit_cur_level->extras[edit_map_x][edit_map_y] ) * BRICK_WIDTH, 0,
BRICK_WIDTH, BRICK_HEIGHT,
stk_display, (edit_map_x + 1) * BRICK_WIDTH, (edit_map_y + 1) * BRICK_HEIGHT );
}
stk_display_store_drect();
}
/*
====================================================================
Redraw and refresh full screen
====================================================================
*/
void editor_full_update()
{
int i, j;
/* background */
stk_surface_blit( editor_bkgnd, 0,0,-1,-1, stk_display, 0,0 );
/* bricks&extras */
for ( i = 0; i < EDIT_WIDTH; i++ )
for ( j = 0; j < EDIT_HEIGHT; j++ )
editor_draw_brick( i, j );
/* selection frame */
stk_surface_blit( sel_frame, 0,0,-1,-1,
stk_display, edit_sel_x * BRICK_WIDTH, edit_sel_y * BRICK_HEIGHT );
/* status */
editor_draw_status();
/* refresh */
stk_display_update( STK_UPDATE_ALL );
}
/*
====================================================================
Do only redraw and refresh those bricks with an extra
====================================================================
*/
void editor_update_extra_bricks()
{
int i, j;
for ( i = 0; i < EDIT_WIDTH; i++ )
for ( j = 0; j < EDIT_HEIGHT; j++ )
if ( edit_cur_level->extras[i][j] != EX_NONE )
editor_draw_brick( i, j );
stk_display_update( STK_UPDATE_RECTS );
}
/*
====================================================================
Free all editor levels
====================================================================
*/
void editor_clear_levels()
{
int i;
for ( i = 0; i < MAX_LEVELS; i++ )
if ( edit_levels[i] ) level_delete( edit_levels[i] );
memset( edit_levels, 0, sizeof( Level* ) * MAX_LEVELS );
edit_level_count = 0;
}
/*
====================================================================
Save/load levels to/from editor file.
====================================================================
*/
void editor_load_levels()
{
int i, version, update;
Level *level;
FILE *file = 0;
/* clear levels first */
editor_clear_levels();
/* read levels while there are some in it */
edit_level_count = 0;
if ( ( file = fopen( edit_file_name, "rb" ) ) != 0 ) {
levelset_get_version( file, &version, &update );
sprintf( edit_version, "%i.%02i", version, update );
while ( ( level = level_load( file ) ) != 0 )
edit_levels[edit_level_count++] = level;
fclose( file );
}
/* if we got no level at all create an empty one */
if ( edit_level_count == 0 ) {
edit_level_count = 1;
edit_levels[0] = level_create_empty( "noname", "untitled" );
}
/* translate the character strings to editor info */
for ( i = 0; i < edit_level_count; i++ )
editor_translate_level( edit_levels[i], CHAR_2_INDICES );
}
void editor_save_levels()
{
FILE *file = 0;
int i, j, k;
Level *level = 0;
/* convert */
for ( i = 0; i < edit_level_count; i++ )
editor_translate_level( edit_levels[i], INDICES_2_CHAR );
/* save */
if ( ( file = fopen( edit_file_name, "w" ) ) != 0 ) {
fprintf( file, "Version: %s\n", edit_version );
for ( i = 0; i < edit_level_count; i++ ) {
level = edit_levels[i];
/* save level */
fprintf( file, "Level:\n%s\n%s\nBricks:\n", level->author, level->name );
for ( j = 0; j < EDIT_HEIGHT; j++ ) {
for ( k = 0; k < EDIT_WIDTH; k++ )
fprintf( file, "%c", level->bricks[k][j] );
fprintf( file, "\n" );
}
fprintf( file, "Bonus:\n" );
for ( j = 0; j < EDIT_HEIGHT; j++ ) {
for ( k = 0; k < EDIT_WIDTH; k++ )
fprintf( file, "%c", level->extras[k][j] );
fprintf( file, "\n" );
}
}
fclose( file );
}
/* convert back */
for ( i = 0; i < edit_level_count; i++ )
editor_translate_level( edit_levels[i], CHAR_2_INDICES );
}
/*
====================================================================
Handle button action
====================================================================
*/
void editor_handle_button( int type, int *full_update ) {
Level *dummy_ptr;
int old_pos;
int version, update;
char *name, *author;
int i;
switch ( type ) {
case BUTTON_FIRST:
edit_cur_level_id = 0;
edit_cur_level = edit_levels[0];
*full_update = 1;
break;
case BUTTON_NEXT:
edit_cur_level_id++;
if ( edit_cur_level_id == edit_level_count ) edit_cur_level_id = 0;
edit_cur_level = edit_levels[edit_cur_level_id];
*full_update = 1;
break;
case BUTTON_PREV:
edit_cur_level_id--;
if ( edit_cur_level_id == -1 ) edit_cur_level_id = edit_level_count - 1;
edit_cur_level = edit_levels[edit_cur_level_id];
*full_update = 1;
break;
case BUTTON_SWAP:
if ( first_swap_level == -1 ) {
first_swap_level = edit_cur_level_id;
*full_update = 1;
}
else {
/* swap current and marked level */
dummy_ptr = edit_levels[first_swap_level];
edit_levels[first_swap_level] = edit_levels[edit_cur_level_id];
edit_levels[edit_cur_level_id] = dummy_ptr;
edit_cur_level = edit_levels[edit_cur_level_id];
first_swap_level = -1;
*full_update = 1;
}
break;
case BUTTON_LOAD:
if ( !confirm( font, "Discard All Changes? y/n", CONFIRM_YES_NO ) ) break;
/* load levels and reset position if level doesn't exist */
old_pos = edit_cur_level_id;
editor_load_levels();
if ( old_pos >= edit_level_count ) edit_cur_level_id = 0;
edit_cur_level = edit_levels[edit_cur_level_id];
first_swap_level = -1;
*full_update = 1;
break;
case BUTTON_SAVE:
if ( !confirm( font, "Save Changes? y/n", CONFIRM_YES_NO ) ) break;
editor_save_levels();
break;
case BUTTON_CLEAR:
if ( !confirm( font, "Clear Level? y/n", CONFIRM_YES_NO ) ) break;
author = strdup( edit_cur_level->author );
name = strdup( edit_cur_level->name );
level_delete( edit_levels[edit_cur_level_id] );
edit_levels[edit_cur_level_id] = level_create_empty( author, name );
edit_cur_level = edit_levels[edit_cur_level_id];
free( name ); free( author );
*full_update = 1;
break;
case BUTTON_ADD:
if ( edit_level_count == MAX_LEVELS ) break;
if ( !confirm( font, "Add Level? y/n", CONFIRM_YES_NO ) ) break;
edit_levels[edit_level_count] = level_create_empty( edit_levels[edit_level_count - 1]->author, edit_levels[edit_level_count - 1]->name );
edit_level_count++;
*full_update = 1;
break;
case BUTTON_INSERT:
if ( edit_level_count == MAX_LEVELS ) break;
if ( !confirm( font, "Insert Level? y/n", CONFIRM_YES_NO ) ) break;
for ( i = edit_level_count; i > edit_cur_level_id; i-- )
edit_levels[i] = edit_levels[i - 1];
edit_level_count++;
edit_levels[edit_cur_level_id] = level_create_empty( edit_cur_level->author, edit_cur_level->name );
edit_cur_level = edit_levels[edit_cur_level_id];
*full_update = 1;
break;
case BUTTON_DELETE:
if ( edit_level_count == 1 ) break; /* last level may not be removed */
if ( !confirm( font, "Delete Level? y/n", CONFIRM_YES_NO ) ) break;
level_delete( edit_levels[edit_cur_level_id] );
for ( i = edit_cur_level_id; i < edit_level_count - 1; i++ )
edit_levels[i] = edit_levels[i + 1];
edit_levels[i] = 0;
edit_level_count--;
if ( edit_cur_level_id >= edit_level_count )
edit_cur_level_id = edit_level_count - 1;
edit_cur_level = edit_levels[edit_cur_level_id];
*full_update = 1;
break;
case BUTTON_PLAY:
stk_display_fade( STK_FADE_OUT, STK_FADE_DEFAULT_TIME );
/* translate */
editor_translate_level( edit_cur_level, INDICES_2_CHAR );
/* run */
client_game_test_level( edit_cur_level );
/* translate back */
editor_translate_level( edit_cur_level, CHAR_2_INDICES );
*full_update = 1;
break;
case BUTTON_VERSION:
if ( enter_string( font, "Levelset Version:", edit_version, 8 ) ) {
parse_version( edit_version, &version, &update );
sprintf( edit_version, "%i.%02i", version, update );
*full_update = 1;
}
break;
}
}
/*
====================================================================
Handle a click on a map tile.
If set is False a remove action was requested (only for
editing)
====================================================================
*/
int near_grow_brick( int x, int y ) {
int i, j;
for ( i = x - 1; i <= x + 1; i++ )
for ( j = y - 1; j <= y + 1; j++ )
if ( i != x || j != y )
if ( i >= 0 && j >= 0 && i < EDIT_WIDTH && j < EDIT_HEIGHT )
if ( edit_cur_level->bricks[i][j] == GROW_BRICK_ID )
return 1;
return 0;
}
void editor_handle_click( int x, int y, int set, int *full_update )
{
int sel = 0;
int edit_x, edit_y;
char str[32];
/* if !set and within the tile field we perform a switch */
if ( !set ) {
if ( x >= edit_tile_x && y >= edit_tile_y && x < edit_tile_x + edit_tile_w && y < edit_tile_y + edit_tile_h ) {
editor_switch_tiles();
*full_update = 1;
return;
}
}
/* the remaining stuff requires a tile at the position */
if ( edit_buttons[x][y] == BUTTON_NONE ) return; /* no action */
/* bricks */
if ( edit_buttons[x][y] >= BUTTON_FIRST_BRICK && edit_buttons[x][y] <= BUTTON_LAST_BRICK ) {
sel = 1;
edit_sel_type = EDITOR_BRICK;
edit_sel_id = edit_buttons[x][y] - BUTTON_FIRST_BRICK;
}
/* extras */
if ( edit_buttons[x][y] >= BUTTON_FIRST_EXTRA && edit_buttons[x][y] <= BUTTON_LAST_EXTRA ) {
sel = 1;
edit_sel_type = EDITOR_EXTRA;
edit_sel_id = edit_buttons[x][y] - BUTTON_FIRST_EXTRA;
}
/* edit field */
if ( edit_buttons[x][y] == BUTTON_EDIT ) {
edit_x = x - 1;
edit_y = y - 1;
if ( edit_sel_type == EDITOR_BRICK ) {
if ( set ) {
/* set brick */
edit_cur_level->bricks[edit_x][edit_y] = edit_sel_id;
*full_update = 1;
}
else {
/* remove brick and extra if any */
if ( edit_cur_level->bricks[edit_x][edit_y] != -1 ) {
edit_cur_level->bricks[edit_x][edit_y] = -1;
edit_cur_level->extras[edit_x][edit_y] = EX_NONE;
*full_update = 1;
}
}
}
else {
if ( set ) {
/* set extra - must be on a brick or beside a grow brick */
if ( edit_cur_level->bricks[edit_x][edit_y] != -1 || near_grow_brick( edit_x, edit_y ) ) {
edit_cur_level->extras[edit_x][edit_y] = edit_sel_id;
*full_update = 1;
}
}
else {
/* remove extra */
if ( edit_cur_level->extras[edit_x][edit_y] != EX_NONE ) {
edit_cur_level->extras[edit_x][edit_y] = EX_NONE;
*full_update = 1;
}
}
}
}
/* buttons */
editor_handle_button( edit_buttons[x][y], full_update );
/* name&author */
strcpy( str, "" );
if ( edit_buttons[x][y] == BUTTON_EDIT_AUTHOR )
if ( enter_string( font, "Author's Name:", str, 24 ) ) {
snprintf( edit_cur_level->author, 31, "%s", str );
*full_update = 1;
}
if ( edit_buttons[x][y] == BUTTON_EDIT_NAME )
if ( enter_string( font, "Title:", str, 24 ) ) {
snprintf( edit_cur_level->name, 31, "%s", str );
*full_update = 1;
}
/* sel frame tile position */
if ( sel ) {
edit_sel_x = x;
edit_sel_y = y;
*full_update = 1;
}
}
/*
====================================================================
Publics
====================================================================
*/
/*
====================================================================
Create/delete editor resources
====================================================================
*/
void editor_create()
{
int i, j;
/* clear all level pointers */
memset( edit_levels, 0, sizeof( Level* ) * MAX_LEVELS );
/* load sel frame */
sel_frame = stk_surface_load( SDL_SWSURFACE, "sel_frame.png" );
/* load buttons */
buttons = stk_surface_load( SDL_SWSURFACE, "buttons.png" );
/* background is black + frame */
editor_bkgnd = stk_surface_create( SDL_SWSURFACE, stk_display->w, stk_display->h );
SDL_SetColorKey( editor_bkgnd, 0, 0 );
stk_surface_fill( editor_bkgnd, 0,0,-1,-1, 0x0 );
/* add helping grid */
editor_draw_grid();
/* set actions */
/* editable part */
for ( i = 0; i < EDIT_WIDTH; i++ )
for ( j = 0; j < EDIT_HEIGHT; j++ )
edit_buttons[i + 1][j + 1] = BUTTON_EDIT;
/* buttons */
for ( i = 0; i < 11; i++ )
edit_buttons[0][MAP_HEIGHT - 11 + i] = BUTTON_FIRST + i;
edit_buttons[MAP_WIDTH - 1][MAP_HEIGHT - 1] = BUTTON_PLAY;
/* name&author */
for ( i = 1; i < MAP_WIDTH / 2; i++ )
edit_buttons[i][MAP_HEIGHT - 5] = BUTTON_EDIT_NAME;
for ( i = MAP_WIDTH / 2; i < MAP_WIDTH - 1; i++ )
edit_buttons[i][MAP_HEIGHT - 5] = BUTTON_EDIT_AUTHOR;
/* draw buttons */
for ( i = 0; i < 11; i++ ) {
stk_surface_blit( buttons, i * BRICK_WIDTH, 0,
BRICK_WIDTH, BRICK_HEIGHT,
editor_bkgnd, 0, ( MAP_HEIGHT - 11 + i ) * BRICK_HEIGHT );
}
stk_surface_blit( buttons, 11 * BRICK_WIDTH, 0,
BRICK_WIDTH, BRICK_HEIGHT,
editor_bkgnd,
( MAP_WIDTH - 1 ) * BRICK_WIDTH,
( MAP_HEIGHT - 1 ) * BRICK_HEIGHT );
}
void editor_delete()
{
stk_surface_free( &editor_bkgnd );
stk_surface_free( &sel_frame );
stk_surface_free( &buttons );
}
/*
====================================================================
Initiate and clear stuff for each editor call.
file_name is the name of the edited file in home directory.
====================================================================
*/
int editor_init( char *file_name )
{
FILE *file = 0;
/* set full file name */
snprintf( edit_file_name, sizeof(edit_file_name)-1, "%s/%s/lbreakout2-levels/%s", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME, file_name );
/* test this file for write access. use append to keep contents */
if ( ( file = fopen( edit_file_name, "a" ) ) == 0 ) {
fprintf( stderr, "Permission to write to file '%s' denied.\n", edit_file_name );
return 0;
}
else
fclose( file );
/* load levels */
editor_load_levels();
/* select first level */
edit_cur_level_id = 0;
edit_cur_level = edit_levels[0];
/* select first brick */
edit_sel_type = EDITOR_EXTRA;
editor_switch_tiles();
/* clear other flags */
extra_vis = 0;
first_swap_level = -1;
return 1;
}
void editor_clear()
{
/* free all levels */
editor_clear_levels();
}
/*
====================================================================
Run the editor
====================================================================
*/
void editor_run()
{
SDL_Event event;
int leave = 0;
int ms;
int last_switch_time = 0;
int full_update = 0, set;
int x, y, xoff,yoff;
Uint8 buttonstate;
/* reset any alpha keys */
SDL_SetAlpha( extra_pic, 0,0 );
/* draw first time */
editor_full_update();
/* main loop */
stk_timer_reset();
while ( !leave && !stk_quit_request ) {
if ( SDL_PollEvent( &event ) ) {
switch ( event.type ) {
case SDL_QUIT: leave = 1; stk_quit_request = 1; break;
case SDL_MOUSEBUTTONDOWN:
editor_handle_click( event.button.x / BRICK_WIDTH,
event.button.y / BRICK_HEIGHT,
(event.button.button == STK_BUTTON_LEFT),
&full_update );
break;
case SDL_KEYDOWN:
switch ( event.key.keysym.sym ) {
case SDLK_ESCAPE:
if ( confirm( font, "Quit Editor? y/n", CONFIRM_YES_NO ) ) leave = 1;
break;
case SDLK_LEFT: editor_handle_button( BUTTON_PREV, &full_update ); break;
case SDLK_RIGHT: editor_handle_button( BUTTON_NEXT, &full_update ); break;
case SDLK_UP: editor_handle_button( BUTTON_FIRST, &full_update ); break;
case SDLK_f:
config.fullscreen = !config.fullscreen;
stk_display_apply_fullscreen( config.fullscreen );
full_update = 1;
break;
default: break;
}
break;
default: break;
}
}
/* mouse motion is handled directly */
buttonstate = SDL_GetRelativeMouseState( &xoff, &yoff );
if ( (xoff || yoff) && buttonstate ) {
buttonstate = SDL_GetMouseState( &x, &y );
set = 0; if ( buttonstate & SDL_BUTTON(1) ) set = 1;
editor_handle_click( x / BRICK_WIDTH, y / BRICK_HEIGHT, set, &full_update );
}
ms = stk_timer_get_time();
if ( ( last_switch_time -= ms ) <= 0 ) {
extra_vis = !extra_vis;
last_switch_time = 500;
editor_update_extra_bricks();
}
/* full update? */
if ( full_update ) {
editor_full_update();
full_update = 0;
}
/* don't consume all CPU time */
SDL_Delay( 5 );
}
}
|