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_filename
= "";
52 char* bgimage_file
= "";
53 char* fgtiles_file
= "";
54 char* bgtiles_file
= "";
59 int select_enable
= 0;
65 struct tile
* raw_tiles
= NULL
;
69 int show_favorites
= 0;
70 int bg_favorites
[7] = {0,0,0,0,0,0,0};
71 int fg_favorites
[7] = {0,0,0,0,0,0,0};
80 int background_dialog
= 0;
81 int tileset_dialog
= 0;
83 int save_as_dialog
= 0;
88 /* base access methods */
89 struct tile
* initialize_raw(int w
, int h
){
92 struct tile blank
= {'0', 0, 0};
93 struct tile
* ptr
= malloc(w
*h
*sizeof(struct tile
));
96 *(ptr
+ i
+ j
*w
) = blank
;
107 struct tile
raw_read(int x
, int y
){
108 struct tile blank
= {'0', 0, 0};
109 if(x
< 0 || y
< 0 || x
>= raw_w
|| y
>= raw_h
){
113 return *(raw_tiles
+ x
+ raw_w
*y
);
118 int new_w
= raw_w
* 3;
119 int new_h
= raw_h
* 3;
120 struct tile
* new_tiles
= initialize_raw(new_w
, new_h
);
126 for(j
=raw_h
; j
<2*raw_h
; j
++){
127 for(i
=raw_w
; i
<2*raw_w
; i
++){
128 ptr
= new_tiles
+ i
+ j
*(3*raw_w
);
129 t
= raw_read(i
-raw_w
, j
-raw_h
);
139 raw_tiles
= new_tiles
;
142 int out_of_bounds(int x
, int y
){
143 if(x
< 0 || y
< 0 || x
>= raw_w
|| y
>= raw_h
)
149 void detect_size(int* w
, int* h
){
150 //see the minimum size necessary for the area
154 void raw_write(int x
, int y
, int layer
, int value
){
155 while(out_of_bounds(x
, y
)){
156 printf("expanding\n");
163 //shift x y by expand shift
164 //shift x and y by origin
166 struct tile
* ptr
= raw_tiles
+ x
+ raw_w
*y
;
178 void draw_background(){
179 int W
= gfx_width(bgimage
);
180 int H
= gfx_height(bgimage
);
181 draw_gfx_raw(bgimage
, 0, 0, 0, 0, W
, H
);
185 int x0
= camera_x
+ origin_x
;
186 int y0
= camera_y
+ origin_y
;
195 if(toggle_background
)
198 for(j
=0; j
<(15+5); j
++){
200 for(i
=0; i
<(20+8); i
++){
206 draw_gfx_raw(bgtiles
, i
*16, j
*16, gx
, gy
, 16, 16);
210 draw_gfx_raw(fgtiles
, i
*16, j
*16, gx
, gy
, 16, 16);
222 struct undo_step
* undo_stack
;
223 struct undo_step
* undo_ptr
;
225 /* undo operations */
227 //do the undo_ptr->undo operations
228 //move undo_ptr down one
232 //if at top of stack, do nothing
234 //do the undo_ptr->redo operations
235 //move undo_ptr up one
238 void undo_record(struct edit
* edits
){
239 //eliminate undo_ptr->redo and all previous edit structs
240 //change the undo_stack
242 //store the edits in undo_ptr->redo
243 //calculate the undo operation XXX
244 //push a new edit struct
246 //store the undo operation in undo_ptr->undo
253 /* medium level editting commands */
254 void write_one_tile(int x
, int y
, int layer
, int value
){
255 //write x y layer value
258 void write_many_tiles(struct edit
* edits
){
263 void edit_one_tile(int x
, int y
, int layer
, int value
){
265 //create a tile struct
269 void edit_many_tiles(struct edit
* edits
){
274 void add_to_clipboard(struct edit
* edits
){
275 //makes a tile struct and appends to clipboard
278 void clear_clipboard(){
279 //clear the clipboard
282 struct tile
* read_tile(int x
, int y
){
289 /* high level gui commands */
290 void select_brush(int layer
, int value
){
294 void start_box(int x
, int y
){
298 void move_box(int x
, int y
){
310 void append_to_box(int x
, int y
){
314 struct tile
* box_select(){
319 void move_paste(int x
, int y
){
357 printf("save code here\n");
374 void keydown(SDLKey key
, SDLMod mod
){
378 console_printf("undo"); break;
381 console_printf("redo"); break;
383 toggle_background
= !toggle_background
;
384 console_printf("background %s", onoff(toggle_background
));
387 toggle_bgtiles
= !toggle_bgtiles
;
388 console_printf("bg tiles %s", onoff(toggle_bgtiles
));
391 toggle_fgtiles
= !toggle_fgtiles
;
392 console_printf("fg tiles %s", onoff(toggle_fgtiles
));
395 toggle_shapes
= !toggle_shapes
;
396 console_printf("shapes %s", onoff(toggle_shapes
));
399 if(mod
& (KMOD_LCTRL
|KMOD_RCTRL
)){
401 console_printf("saved %s", my_filename
);
405 console_printf("save as...");
408 console_printf("open...");
411 console_printf("change background...");
414 if(dialog_flag
== 0){
421 if(dialog_flag
== 0){
430 console_printf("OK");
433 console_printf("yes");
436 console_printf("no");
441 console_printf("help...");
444 console_printf("pick fg tileset...");
447 console_printf("pick bg tileset...");
449 case SDLK_LEFT
: camera_x
--; break;
450 case SDLK_RIGHT
: camera_x
++; break;
451 case SDLK_UP
: camera_y
--; break;
452 case SDLK_DOWN
: camera_y
++; break;
468 B - change background
477 F2 - change fg tileset
478 F3 - change bg tileset
483 void translate_pointer(int mx
, int my
, int *x
, int *y
){
485 map_pixel(mx
, my
, &a
, &b
);
486 *x
= a
/16 + camera_x
+ origin_x
;
487 *y
= b
/16 + camera_y
+ origin_y
;
491 void mousedown(int mx
, int my
, int button
){
493 hold LMB - draw single tiles / deselect
494 shift LMB - start box select
495 ctrl LMB - append single tiles to selection
496 RMB - display tilesets
497 hold MMB - choose where to paste (release to execute, esc to cancel)
499 SDLMod mod
= SDL_GetModState();
502 translate_pointer(mx
, my
, &x
, &y
);
505 //change brush, maybe
514 raw_write(x
, y
, brush_layer
, brush_tile
);
518 else if(button
== 3){
525 void mouseup(int x
, int y
, int button
){
528 shift LMB - append box to selection
538 void mousemove(int mx
, int my
, int xrel
, int yrel
){
545 translate_pointer(mx
, my
, &x
, &y
);
548 raw_write(x
, y
, brush_layer
, brush_tile
);
556 if(SDL_WaitEvent(&e
) == 0){
557 printf("SDL_WaitEvent encountered an error (%s)\n", SDL_GetError());
562 case SDL_QUIT
: return 1;
563 case SDL_KEYDOWN
: keydown(e
.key
.keysym
.sym
, e
.key
.keysym
.mod
); return 0;
564 case SDL_MOUSEMOTION
:
565 mousemove(e
.motion
.x
, e
.motion
.y
, e
.motion
.xrel
, e
.motion
.yrel
);
567 case SDL_MOUSEBUTTONDOWN
: mousedown(e
.button
.x
, e
.button
.y
, e
.button
.button
); return 0;
568 case SDL_MOUSEBUTTONUP
: mouseup(e
.button
.x
, e
.button
.y
, e
.button
.button
); return 0;
580 int main(int argc
, char* argv
[]){
581 video_init(argc
, argv
);
585 raw_tiles
= initialize_raw(raw_w
, raw_h
);
588 bgimage
= load_bitmap("azone/gfx/background.tga");
589 // loader_data_mode(0);
590 // fgtiles = load_bitmap("barf.tga");
591 // loader_data_mode(1);
592 fgtiles
= load_bitmap("azone/gfx/test.tga");
593 bgtiles
= load_bitmap("azone/gfx/test.tga");
595 raw_write(2, 2, 1, 3);
600 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
604 while(check_events() == 0 && panic_flag
== 0);