From 23f101b022c3742653ead4313428d34f36d9368f Mon Sep 17 00:00:00 2001 From: EvanR Date: Sat, 15 May 2010 17:11:49 -0500 Subject: [PATCH] Editor saves a correct, optimized stage. --- edit.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- stage.c | 4 +-- 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/edit.c b/edit.c index 3521490..8bd7db3 100644 --- a/edit.c +++ b/edit.c @@ -1,5 +1,6 @@ #include #include +#include #include @@ -220,6 +221,81 @@ void draw_raw(){ } +/* determine an optimal size for the stage */ +void raw_optimize(int* ox, int* oy, int* ow, int* oh){ + int i; + int xmax = 0; + int xmin = INT_MAX; + int ymax = 0; + int ymin = INT_MAX; + int x, y, fg, bg; + char shape; + + for(i=0; i<(raw_w*raw_h); i++){ + x = i % raw_w; + y = i / raw_h; + fg = raw_tiles[i].fg; + bg = raw_tiles[i].bg; + shape = raw_tiles[i].shape; + if((fg != 0 || bg != 0 || shape != '0')){ + if(x > xmax) xmax = x; + if(x < xmin) xmin = x; + if(y > ymax) ymax = y; + if(y < ymin) ymin = y; + } + } + + if(ymax - ymin < 15) *oh = 15; + else *oh = (ymax - ymin); + + if(xmax - xmin < 20) *ow = 20; + else *ow = (xmax - xmin); + + *ox = xmin; + *oy = ymin; +} + +void raw_save(char* path){ + /* save current stage to a stage file */ + /* overwrites if already exists, no confirmation */ + int x, y, bg, fg; + char shape; + int i; + struct tile* ptr = raw_tiles; + int opt_ox, opt_oy; + int opt_x, opt_y, opt_w, opt_h; + + + FILE* f = fopen(path, "w"); + if(f == NULL){ + console_printf("error saving file"); + return; + } + + raw_optimize(&opt_x, &opt_y, &opt_w, &opt_h); + + fprintf(f, "%d %d %d %d\n", opt_w, opt_h, origin_x-opt_x, origin_y-opt_y); + fprintf(f, "%s\n", bgimage_file); + fprintf(f, "%s\n", fgtiles_file); + fprintf(f, "%s\n", bgtiles_file); + + for(i=0; i<(raw_w*raw_h); i++){ + x = (i % raw_w) - origin_x; + y = (i / raw_w) - origin_y; + fg = ptr[i].fg; + bg = ptr[i].bg; + shape = ptr[i].shape; + + if(fg != 0 || bg != 0 || shape != '0'){ + fprintf(f, "%d %d %d %d %c\n", x, y, fg, bg, shape); + } + } + + + fclose(f); + +} + /* *** */ @@ -373,30 +449,24 @@ char* onoff(int b){ } +/* *** */ -void save(char* path){ - /* save current stage to a stage file */ - /* overwrites if already exists, no confirmation */ - FILE* f = fopen(path, "w"); - if(f == NULL){ - console_printf("error saving file"); - return; - } - - fprintf(f, "HELLO WORLD\n"); - - fclose(f); +/* utility */ +void select_bgfile(char* path){ + strcpy(bgimage_file, path); } + /* *** */ + /* dialog input handlers */ void confirm_save_press(SDLKey key, Uint16 c){ if(c == 'y' || c == 'Y'){ - save(my_file); + raw_save(my_file); update_window_name(); console_printf("You're the boss. %s was overwritten", my_file); } @@ -430,7 +500,7 @@ void save_as_press(SDLKey key, Uint16 c){ else{ update_window_name(); console_printf("%s saved", my_file); - save(my_file); + raw_save(my_file); } } save_as_buf[0] = 0; @@ -505,7 +575,7 @@ void keydown(SDLKey key, SDLMod mod, Uint16 c){ save_as_dialog = 1; } else{ - save(my_file); + raw_save(my_file); console_printf("saved %s", my_file); } } diff --git a/stage.c b/stage.c index 68bade8..d115280 100644 --- a/stage.c +++ b/stage.c @@ -318,8 +318,8 @@ struct stage { for(j=0; jh; j++){ for(i=0; iw; i++){ - c = (ptr->tiles + i + ptr->w*j)->shape; - printf("%c", c); + c = (ptr->tiles + i + ptr->w*j)->bg; + printf("%d", c); } printf("\n"); } -- 2.11.4.GIT