33 struct tile
* undo
; /* writes to undo */
34 struct tile
* redo
; /* writes to redo */
41 /* application state variables */
42 int toggle_background
= 1;
43 int toggle_bgtiles
= 1;
44 int toggle_fgtiles
= 1;
45 int toggle_shapes
= 0;
49 static int camera_x
= 0;
50 static int camera_y
= 0;
52 char my_file
[256] = "";
53 char my_file_old
[256] = "";
54 char bgimage_file
[256] = "";
55 char fgtiles_file
[256] = "";
56 char bgtiles_file
[256] = "";
61 int select_enable
= 0;
67 struct tile
* raw_tiles
= NULL
;
71 int show_favorites
= 0;
72 int bg_favorites
[7] = {0,0,0,0,0,0,0};
73 int fg_favorites
[7] = {0,0,0,0,0,0,0};
82 int background_dialog
= 0;
83 int tileset_dialog
= 0;
85 int save_as_dialog
= 0;
87 int confirm_save_dialog
= 0;
91 char save_as_buf
[256] = "";
96 /* base access methods */
97 struct tile
* initialize_raw(int w
, int h
){
100 struct tile blank
= {'0', 0, 0};
101 struct tile
* ptr
= malloc(w
*h
*sizeof(struct tile
));
104 *(ptr
+ i
+ j
*w
) = blank
;
115 struct tile
raw_read(int x
, int y
){
116 struct tile blank
= {'0', 0, 0};
117 if(x
< 0 || y
< 0 || x
>= raw_w
|| y
>= raw_h
){
121 return *(raw_tiles
+ x
+ raw_w
*y
);
126 int new_w
= raw_w
* 3;
127 int new_h
= raw_h
* 3;
128 struct tile
* new_tiles
= initialize_raw(new_w
, new_h
);
134 for(j
=raw_h
; j
<2*raw_h
; j
++){
135 for(i
=raw_w
; i
<2*raw_w
; i
++){
136 ptr
= new_tiles
+ i
+ j
*(3*raw_w
);
137 t
= raw_read(i
-raw_w
, j
-raw_h
);
147 raw_tiles
= new_tiles
;
150 int out_of_bounds(int x
, int y
){
151 if(x
< 0 || y
< 0 || x
>= raw_w
|| y
>= raw_h
)
157 void detect_size(int* w
, int* h
){
158 //see the minimum size necessary for the area
162 void raw_write(int x
, int y
, int layer
, int value
){
163 while(out_of_bounds(x
, y
)){
164 printf("expanding\n");
171 //shift x y by expand shift
172 //shift x and y by origin
174 struct tile
* ptr
= raw_tiles
+ x
+ raw_w
*y
;
186 void draw_background(){
187 int W
= gfx_width(bgimage
);
188 int H
= gfx_height(bgimage
);
189 draw_gfx_raw(bgimage
, 0, 0, 0, 0, W
, H
);
193 int x0
= camera_x
+ origin_x
;
194 int y0
= camera_y
+ origin_y
;
203 if(toggle_background
)
206 for(j
=0; j
<(15+5); j
++){
208 for(i
=0; i
<(20+8); i
++){
214 draw_gfx_raw(bgtiles
, i
*16, j
*16, gx
, gy
, 16, 16);
218 draw_gfx_raw(fgtiles
, i
*16, j
*16, gx
, gy
, 16, 16);
224 /* determine an optimal size for the stage */
225 void raw_optimize(int* ox
, int* oy
, int* ow
, int* oh
){
234 for(i
=0; i
<(raw_w
*raw_h
); i
++){
237 fg
= raw_tiles
[i
].fg
;
238 bg
= raw_tiles
[i
].bg
;
239 shape
= raw_tiles
[i
].shape
;
240 if((fg
!= 0 || bg
!= 0 || shape
!= '0')){
241 if(x
> xmax
) xmax
= x
;
242 if(x
< xmin
) xmin
= x
;
243 if(y
> ymax
) ymax
= y
;
244 if(y
< ymin
) ymin
= y
;
248 if(ymax
- ymin
< 15) *oh
= 15;
249 else *oh
= (ymax
- ymin
);
251 if(xmax
- xmin
< 20) *ow
= 20;
252 else *ow
= (xmax
- xmin
);
258 void raw_save(char* path
){
259 /* save current stage to a stage file */
260 /* overwrites if already exists, no confirmation */
264 struct tile
* ptr
= raw_tiles
;
266 int opt_x
, opt_y
, opt_w
, opt_h
;
269 FILE* f
= fopen(path
, "w");
271 console_printf("error saving file");
275 raw_optimize(&opt_x
, &opt_y
, &opt_w
, &opt_h
);
277 fprintf(f
, "%d %d %d %d\n", opt_w
, opt_h
, origin_x
-opt_x
, origin_y
-opt_y
);
278 fprintf(f
, "%s\n", bgimage_file
);
279 fprintf(f
, "%s\n", fgtiles_file
);
280 fprintf(f
, "%s\n", bgtiles_file
);
282 for(i
=0; i
<(raw_w
*raw_h
); i
++){
283 x
= (i
% raw_w
) - origin_x
;
284 y
= (i
/ raw_w
) - origin_y
;
287 shape
= ptr
[i
].shape
;
289 if(fg
!= 0 || bg
!= 0 || shape
!= '0'){
290 fprintf(f
, "%d %d %d %d %c\n", x
, y
, fg
, bg
, shape
);
303 void update_window_name(){
305 SDL_WM_SetCaption("unnamed", NULL
);
308 SDL_WM_SetCaption(my_file
, NULL
);
314 struct undo_step
* undo_stack
;
315 struct undo_step
* undo_ptr
;
317 /* undo operations */
319 //do the undo_ptr->undo operations
320 //move undo_ptr down one
324 //if at top of stack, do nothing
326 //do the undo_ptr->redo operations
327 //move undo_ptr up one
330 void undo_record(struct edit
* edits
){
331 //eliminate undo_ptr->redo and all previous edit structs
332 //change the undo_stack
334 //store the edits in undo_ptr->redo
335 //calculate the undo operation XXX
336 //push a new edit struct
338 //store the undo operation in undo_ptr->undo
345 /* medium level editting commands */
346 void write_one_tile(int x
, int y
, int layer
, int value
){
347 //write x y layer value
350 void write_many_tiles(struct edit
* edits
){
355 void edit_one_tile(int x
, int y
, int layer
, int value
){
357 //create a tile struct
361 void edit_many_tiles(struct edit
* edits
){
366 void add_to_clipboard(struct edit
* edits
){
367 //makes a tile struct and appends to clipboard
370 void clear_clipboard(){
371 //clear the clipboard
374 struct tile
* read_tile(int x
, int y
){
381 /* high level gui commands */
382 void select_brush(int layer
, int value
){
386 void start_box(int x
, int y
){
390 void move_box(int x
, int y
){
402 void append_to_box(int x
, int y
){
406 struct tile
* box_select(){
411 void move_paste(int x
, int y
){
436 console_printf("save as: %s", save_as_buf
);
456 void select_bgfile(char* path
){
457 strcpy(bgimage_file
, path
);
466 /* dialog input handlers */
467 void confirm_save_press(SDLKey key
, Uint16 c
){
468 if(c
== 'y' || c
== 'Y'){
470 update_window_name();
471 console_printf("You're the boss. %s was overwritten", my_file
);
474 strcpy(my_file
, my_file_old
); /* ! */
475 console_printf("Operation cancelled");
478 confirm_save_dialog
= 0;
481 void save_as_press(SDLKey key
, Uint16 c
){
486 if(save_as_buf
[0] == 0){
487 console_printf("No name? Nevermind then.");
490 strcpy(my_file_old
, my_file
); /* ! */
491 strcpy(my_file
, save_as_buf
); /* ! */
493 /* see if file exists */
494 f
= fopen(my_file
, "r");
496 console_printf("ALERT: really overwrite %s? (Y/N)", my_file
);
497 confirm_save_dialog
= 1;
501 update_window_name();
502 console_printf("%s saved", my_file
);
518 save_as_buf
[save_as_ptr
] = 0;
522 if(save_as_ptr
< 255){
523 save_as_buf
[save_as_ptr
] = c
;
525 save_as_buf
[save_as_ptr
] = 0;
535 void keydown(SDLKey key
, SDLMod mod
, Uint16 c
){
538 save_as_press(key
, c
);
543 if(confirm_save_dialog
){
544 confirm_save_press(key
, c
);
552 console_printf("undo"); break;
555 console_printf("redo"); break;
557 toggle_background
= !toggle_background
;
558 console_printf("background %s", onoff(toggle_background
));
561 toggle_bgtiles
= !toggle_bgtiles
;
562 console_printf("bg tiles %s", onoff(toggle_bgtiles
));
565 toggle_fgtiles
= !toggle_fgtiles
;
566 console_printf("fg tiles %s", onoff(toggle_fgtiles
));
569 toggle_shapes
= !toggle_shapes
;
570 console_printf("shapes %s", onoff(toggle_shapes
));
573 if(mod
& (KMOD_LCTRL
|KMOD_RCTRL
)){
579 console_printf("saved %s", my_file
);
587 console_printf("open...");
590 console_printf("change background...");
593 if(dialog_flag
== 0){
600 if(dialog_flag
== 0){
609 console_printf("OK");
612 console_printf("yes");
615 console_printf("no");
620 console_printf("help...");
623 console_printf("pick fg tileset...");
626 console_printf("pick bg tileset...");
628 case SDLK_LEFT
: camera_x
--; break;
629 case SDLK_RIGHT
: camera_x
++; break;
630 case SDLK_UP
: camera_y
--; break;
631 case SDLK_DOWN
: camera_y
++; break;
633 /* temporary controls */
634 case SDLK_9
: brush_tile
--; brush_tile
%= 256; break;
635 case SDLK_0
: brush_tile
++; brush_tile
%= 256; break;
636 case SDLK_8
: brush_layer
= 2; break;
637 case SDLK_7
: brush_layer
= 1; break;
652 B - change background
661 F2 - change fg tileset
662 F3 - change bg tileset
667 void translate_pointer(int mx
, int my
, int *x
, int *y
){
669 map_pixel(mx
, my
, &a
, &b
);
670 *x
= a
/16 + camera_x
+ origin_x
;
671 *y
= b
/16 + camera_y
+ origin_y
;
675 void mousedown(int mx
, int my
, int button
){
677 hold LMB - draw single tiles / deselect
678 shift LMB - start box select
679 ctrl LMB - append single tiles to selection
680 RMB - display tilesets
681 hold MMB - choose where to paste (release to execute, esc to cancel)
683 SDLMod mod
= SDL_GetModState();
686 translate_pointer(mx
, my
, &x
, &y
);
689 //change brush, maybe
698 raw_write(x
, y
, brush_layer
, brush_tile
);
702 else if(button
== 3){
709 void mouseup(int x
, int y
, int button
){
712 shift LMB - append box to selection
722 void mousemove(int mx
, int my
, int xrel
, int yrel
){
729 translate_pointer(mx
, my
, &x
, &y
);
732 raw_write(x
, y
, brush_layer
, brush_tile
);
740 if(SDL_WaitEvent(&e
) == 0){
741 printf("SDL_WaitEvent encountered an error (%s)\n", SDL_GetError());
746 case SDL_QUIT
: return 1;
747 case SDL_KEYDOWN
: keydown(e
.key
.keysym
.sym
, e
.key
.keysym
.mod
, e
.key
.keysym
.unicode
); return 0;
748 case SDL_MOUSEMOTION
:
749 mousemove(e
.motion
.x
, e
.motion
.y
, e
.motion
.xrel
, e
.motion
.yrel
);
751 case SDL_MOUSEBUTTONDOWN
: mousedown(e
.button
.x
, e
.button
.y
, e
.button
.button
); return 0;
752 case SDL_MOUSEBUTTONUP
: mouseup(e
.button
.x
, e
.button
.y
, e
.button
.button
); return 0;
764 int main(int argc
, char* argv
[]){
765 video_init(argc
, argv
);
769 raw_tiles
= initialize_raw(raw_w
, raw_h
);
771 update_window_name();
774 bgimage
= load_bitmap("azone/gfx/background.tga");
775 // loader_data_mode(0);
776 // fgtiles = load_bitmap("barf.tga");
777 // loader_data_mode(1);
778 fgtiles
= load_bitmap("azone/gfx/barf.tga");
779 bgtiles
= load_bitmap("azone/gfx/test.tga");
781 raw_write(2, 2, 1, 3);
786 SDL_EnableUNICODE(1);
787 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
791 while(check_events() == 0 && panic_flag
== 0);