32 struct tile
* undo
; /* writes to undo */
33 struct tile
* redo
; /* writes to redo */
40 /* application state variables */
41 int toggle_background
= 1;
42 int toggle_bgtiles
= 1;
43 int toggle_fgtiles
= 1;
44 int toggle_shapes
= 0;
48 static int camera_x
= 0;
49 static int camera_y
= 0;
51 char my_file
[256] = "";
52 char my_file_old
[256] = "";
53 char bgimage_file
[256] = "";
54 char fgtiles_file
[256] = "";
55 char bgtiles_file
[256] = "";
60 int select_enable
= 0;
66 struct tile
* raw_tiles
= NULL
;
70 int show_favorites
= 0;
71 int bg_favorites
[7] = {0,0,0,0,0,0,0};
72 int fg_favorites
[7] = {0,0,0,0,0,0,0};
81 int background_dialog
= 0;
82 int tileset_dialog
= 0;
84 int save_as_dialog
= 0;
86 int confirm_save_dialog
= 0;
90 char save_as_buf
[256] = "";
95 /* base access methods */
96 struct tile
* initialize_raw(int w
, int h
){
99 struct tile blank
= {'0', 0, 0};
100 struct tile
* ptr
= malloc(w
*h
*sizeof(struct tile
));
103 *(ptr
+ i
+ j
*w
) = blank
;
114 struct tile
raw_read(int x
, int y
){
115 struct tile blank
= {'0', 0, 0};
116 if(x
< 0 || y
< 0 || x
>= raw_w
|| y
>= raw_h
){
120 return *(raw_tiles
+ x
+ raw_w
*y
);
125 int new_w
= raw_w
* 3;
126 int new_h
= raw_h
* 3;
127 struct tile
* new_tiles
= initialize_raw(new_w
, new_h
);
133 for(j
=raw_h
; j
<2*raw_h
; j
++){
134 for(i
=raw_w
; i
<2*raw_w
; i
++){
135 ptr
= new_tiles
+ i
+ j
*(3*raw_w
);
136 t
= raw_read(i
-raw_w
, j
-raw_h
);
146 raw_tiles
= new_tiles
;
149 int out_of_bounds(int x
, int y
){
150 if(x
< 0 || y
< 0 || x
>= raw_w
|| y
>= raw_h
)
156 void detect_size(int* w
, int* h
){
157 //see the minimum size necessary for the area
161 void raw_write(int x
, int y
, int layer
, int value
){
162 while(out_of_bounds(x
, y
)){
163 printf("expanding\n");
170 //shift x y by expand shift
171 //shift x and y by origin
173 struct tile
* ptr
= raw_tiles
+ x
+ raw_w
*y
;
185 void draw_background(){
186 int W
= gfx_width(bgimage
);
187 int H
= gfx_height(bgimage
);
188 draw_gfx_raw(bgimage
, 0, 0, 0, 0, W
, H
);
192 int x0
= camera_x
+ origin_x
;
193 int y0
= camera_y
+ origin_y
;
202 if(toggle_background
)
205 for(j
=0; j
<(15+5); j
++){
207 for(i
=0; i
<(20+8); i
++){
213 draw_gfx_raw(bgtiles
, i
*16, j
*16, gx
, gy
, 16, 16);
217 draw_gfx_raw(fgtiles
, i
*16, j
*16, gx
, gy
, 16, 16);
227 void update_window_name(){
229 SDL_WM_SetCaption("unnamed", NULL
);
232 SDL_WM_SetCaption(my_file
, NULL
);
238 struct undo_step
* undo_stack
;
239 struct undo_step
* undo_ptr
;
241 /* undo operations */
243 //do the undo_ptr->undo operations
244 //move undo_ptr down one
248 //if at top of stack, do nothing
250 //do the undo_ptr->redo operations
251 //move undo_ptr up one
254 void undo_record(struct edit
* edits
){
255 //eliminate undo_ptr->redo and all previous edit structs
256 //change the undo_stack
258 //store the edits in undo_ptr->redo
259 //calculate the undo operation XXX
260 //push a new edit struct
262 //store the undo operation in undo_ptr->undo
269 /* medium level editting commands */
270 void write_one_tile(int x
, int y
, int layer
, int value
){
271 //write x y layer value
274 void write_many_tiles(struct edit
* edits
){
279 void edit_one_tile(int x
, int y
, int layer
, int value
){
281 //create a tile struct
285 void edit_many_tiles(struct edit
* edits
){
290 void add_to_clipboard(struct edit
* edits
){
291 //makes a tile struct and appends to clipboard
294 void clear_clipboard(){
295 //clear the clipboard
298 struct tile
* read_tile(int x
, int y
){
305 /* high level gui commands */
306 void select_brush(int layer
, int value
){
310 void start_box(int x
, int y
){
314 void move_box(int x
, int y
){
326 void append_to_box(int x
, int y
){
330 struct tile
* box_select(){
335 void move_paste(int x
, int y
){
360 console_printf("save as: %s", save_as_buf
);
377 void save(char* path
){
378 /* save current stage to a stage file */
379 /* overwrites if already exists, no confirmation */
380 FILE* f
= fopen(path
, "w");
382 console_printf("error saving file");
386 fprintf(f
, "HELLO WORLD\n");
396 /* dialog input handlers */
397 void confirm_save_press(SDLKey key
, Uint16 c
){
398 if(c
== 'y' || c
== 'Y'){
400 update_window_name();
401 console_printf("You're the boss. %s was overwritten", my_file
);
404 strcpy(my_file
, my_file_old
); /* ! */
405 console_printf("Operation cancelled");
408 confirm_save_dialog
= 0;
411 void save_as_press(SDLKey key
, Uint16 c
){
416 if(save_as_buf
[0] == 0){
417 console_printf("No name? Nevermind then.");
420 strcpy(my_file_old
, my_file
); /* ! */
421 strcpy(my_file
, save_as_buf
); /* ! */
423 /* see if file exists */
424 f
= fopen(my_file
, "r");
426 console_printf("ALERT: really overwrite %s? (Y/N)", my_file
);
427 confirm_save_dialog
= 1;
431 update_window_name();
432 console_printf("%s saved", my_file
);
448 save_as_buf
[save_as_ptr
] = 0;
452 if(save_as_ptr
< 255){
453 save_as_buf
[save_as_ptr
] = c
;
455 save_as_buf
[save_as_ptr
] = 0;
465 void keydown(SDLKey key
, SDLMod mod
, Uint16 c
){
468 save_as_press(key
, c
);
473 if(confirm_save_dialog
){
474 confirm_save_press(key
, c
);
482 console_printf("undo"); break;
485 console_printf("redo"); break;
487 toggle_background
= !toggle_background
;
488 console_printf("background %s", onoff(toggle_background
));
491 toggle_bgtiles
= !toggle_bgtiles
;
492 console_printf("bg tiles %s", onoff(toggle_bgtiles
));
495 toggle_fgtiles
= !toggle_fgtiles
;
496 console_printf("fg tiles %s", onoff(toggle_fgtiles
));
499 toggle_shapes
= !toggle_shapes
;
500 console_printf("shapes %s", onoff(toggle_shapes
));
503 if(mod
& (KMOD_LCTRL
|KMOD_RCTRL
)){
509 console_printf("saved %s", my_file
);
517 console_printf("open...");
520 console_printf("change background...");
523 if(dialog_flag
== 0){
530 if(dialog_flag
== 0){
539 console_printf("OK");
542 console_printf("yes");
545 console_printf("no");
550 console_printf("help...");
553 console_printf("pick fg tileset...");
556 console_printf("pick bg tileset...");
558 case SDLK_LEFT
: camera_x
--; break;
559 case SDLK_RIGHT
: camera_x
++; break;
560 case SDLK_UP
: camera_y
--; break;
561 case SDLK_DOWN
: camera_y
++; break;
563 /* temporary controls */
564 case SDLK_9
: brush_tile
--; brush_tile
%= 256; break;
565 case SDLK_0
: brush_tile
++; brush_tile
%= 256; break;
566 case SDLK_8
: brush_layer
= 2; break;
567 case SDLK_7
: brush_layer
= 1; break;
582 B - change background
591 F2 - change fg tileset
592 F3 - change bg tileset
597 void translate_pointer(int mx
, int my
, int *x
, int *y
){
599 map_pixel(mx
, my
, &a
, &b
);
600 *x
= a
/16 + camera_x
+ origin_x
;
601 *y
= b
/16 + camera_y
+ origin_y
;
605 void mousedown(int mx
, int my
, int button
){
607 hold LMB - draw single tiles / deselect
608 shift LMB - start box select
609 ctrl LMB - append single tiles to selection
610 RMB - display tilesets
611 hold MMB - choose where to paste (release to execute, esc to cancel)
613 SDLMod mod
= SDL_GetModState();
616 translate_pointer(mx
, my
, &x
, &y
);
619 //change brush, maybe
628 raw_write(x
, y
, brush_layer
, brush_tile
);
632 else if(button
== 3){
639 void mouseup(int x
, int y
, int button
){
642 shift LMB - append box to selection
652 void mousemove(int mx
, int my
, int xrel
, int yrel
){
659 translate_pointer(mx
, my
, &x
, &y
);
662 raw_write(x
, y
, brush_layer
, brush_tile
);
670 if(SDL_WaitEvent(&e
) == 0){
671 printf("SDL_WaitEvent encountered an error (%s)\n", SDL_GetError());
676 case SDL_QUIT
: return 1;
677 case SDL_KEYDOWN
: keydown(e
.key
.keysym
.sym
, e
.key
.keysym
.mod
, e
.key
.keysym
.unicode
); return 0;
678 case SDL_MOUSEMOTION
:
679 mousemove(e
.motion
.x
, e
.motion
.y
, e
.motion
.xrel
, e
.motion
.yrel
);
681 case SDL_MOUSEBUTTONDOWN
: mousedown(e
.button
.x
, e
.button
.y
, e
.button
.button
); return 0;
682 case SDL_MOUSEBUTTONUP
: mouseup(e
.button
.x
, e
.button
.y
, e
.button
.button
); return 0;
694 int main(int argc
, char* argv
[]){
695 video_init(argc
, argv
);
699 raw_tiles
= initialize_raw(raw_w
, raw_h
);
701 update_window_name();
704 bgimage
= load_bitmap("azone/gfx/background.tga");
705 // loader_data_mode(0);
706 // fgtiles = load_bitmap("barf.tga");
707 // loader_data_mode(1);
708 fgtiles
= load_bitmap("azone/gfx/barf.tga");
709 bgtiles
= load_bitmap("azone/gfx/test.tga");
711 raw_write(2, 2, 1, 3);
716 SDL_EnableUNICODE(1);
717 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
721 while(check_events() == 0 && panic_flag
== 0);