Added loading from not zip files. Changed path spec.
[cantaveria.git] / edit.c
blobfc2463e28b9d3d29abeab80e58f6c37bc641bab0
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include <SDL/SDL.h>
8 #include <list.h>
9 #include <console.h>
10 #include <stage.h>
11 #include <loader.h>
12 #include <kernel.h>
13 #include <graphics.h>
14 #include <console.h>
16 struct edit {
17 int x;
18 int y;
19 char shape;
20 unsigned char fg;
21 unsigned char bg;
22 struct edit* next;
25 struct tile {
26 char shape;
27 unsigned char fg;
28 unsigned char bg;
31 struct undo_step {
32 struct tile* undo; /* writes to undo */
33 struct tile* redo; /* writes to redo */
34 struct edit* next;
35 struct edit* prev;
40 /* application state variables */
41 int toggle_background = 1;
42 int toggle_bgtiles = 1;
43 int toggle_fgtiles = 1;
44 int toggle_shapes = 0;
46 int origin_x = 0;
47 int origin_y = 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 = "";
55 int bgimage = 0;
56 int fgtiles = 0;
57 int bgtiles = 0;
59 int select_enable = 0;
60 int select_x = 0;
61 int select_y = 0;
62 int select_w = 0;
63 int select_h = 0;
65 struct tile* raw_tiles = NULL;
66 int raw_w = 20;
67 int raw_h = 15;
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};
72 int brush_tile = 1;
73 int brush_layer = 1;
74 int brush_enable = 0;
76 int panic_flag = 0;
78 int dialog_flag = 0;
79 int brush_dialog = 0;
80 int background_dialog = 0;
81 int tileset_dialog = 0;
82 int quit_dialog = 0;
83 int save_as_dialog = 0;
84 int open_dialog = 0;
85 /* *** */
88 /* base access methods */
89 struct tile* initialize_raw(int w, int h){
90 int i;
91 int j;
92 struct tile blank = {'0', 0, 0};
93 struct tile* ptr = malloc(w*h*sizeof(struct tile));
94 for(j=0; j<h; j++){
95 for(i=0; i<w; i++){
96 *(ptr + i + j*w) = blank;
100 return ptr;
103 void unload_raw(){
104 free(raw_tiles);
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){
110 return blank;
112 else{
113 return *(raw_tiles + x + raw_w*y);
117 void expand_raw(){
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);
121 struct tile* ptr;
122 struct tile t;
123 int i;
124 int j;
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);
130 *ptr = t;
134 origin_x += raw_w;
135 origin_y += raw_h;
136 raw_w *= 3;
137 raw_h *= 3;
138 free(raw_tiles);
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)
144 return 1;
145 else
146 return 0;
149 void detect_size(int* w, int* h){
150 //see the minimum size necessary for the area
151 //used for saving
154 void raw_write(int x, int y, int layer, int value){
155 while(out_of_bounds(x, y)){
156 printf("expanding\n");
157 expand_raw();
158 x += raw_w / 3;
159 y += raw_h / 3;
162 //expand if outside
163 //shift x y by expand shift
164 //shift x and y by origin
165 //do the write
166 struct tile* ptr = raw_tiles + x + raw_w*y;
167 if(layer == 1){
168 ptr->bg = value;
170 else if(layer == 2){
171 ptr->fg = value;
173 else if(layer == 3){
174 ptr->shape = value;
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);
184 void draw_raw(){
185 int x0 = camera_x + origin_x;
186 int y0 = camera_y + origin_y;
187 int i;
188 int j;
189 int x;
190 int y;
191 struct tile t;
192 int gx;
193 int gy;
195 if(toggle_background)
196 draw_background();
198 for(j=0; j<(15+5); j++){
199 y = y0 + j;
200 for(i=0; i<(20+8); i++){
201 x = x0 + i;
202 t = raw_read(x, y);
203 gy = 16*(t.bg / 16);
204 gx = 16*(t.bg % 16);
205 if(toggle_bgtiles)
206 draw_gfx_raw(bgtiles, i*16, j*16, gx, gy, 16, 16);
207 gy = 16*(t.fg / 16);
208 gx = 16*(t.fg % 16);
209 if(toggle_fgtiles)
210 draw_gfx_raw(fgtiles, i*16, j*16, gx, gy, 16, 16);
216 /* *** */
222 struct undo_step* undo_stack;
223 struct undo_step* undo_ptr;
225 /* undo operations */
226 void undo(){
227 //do the undo_ptr->undo operations
228 //move undo_ptr down one
231 void redo(){
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
245 //move undo_ptr
246 //store the undo operation in undo_ptr->undo
248 /* *** */
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){
259 //for each tiles
260 //write one tile
263 void edit_one_tile(int x, int y, int layer, int value){
264 //write_one_tile
265 //create a tile struct
266 //call edit on it
269 void edit_many_tiles(struct edit* edits){
270 //write many tiles
271 //edit(tiles)
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){
283 //make a tile struct
285 /* *** */
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){
302 void stop_box(){
306 void clear_box(){
310 void append_to_box(int x, int y){
314 struct tile* box_select(){
319 void move_paste(int x, int y){
323 void cancel_paste(){
327 void do_paste(){
332 void redraw_all(){
333 clear_video();
335 draw_raw();
337 //gui indicators
338 if(select_enable){
339 //draw green box
342 //dialogs
344 console_draw();
346 update_video();
349 char* onoff(int b){
350 if(b) return "on";
351 else return "off";
356 void save(){
357 printf("save code here\n");
359 /* *** */
374 void keydown(SDLKey key, SDLMod mod){
375 switch(key){
376 case SDLK_u:
377 undo();
378 console_printf("undo"); break;
379 case SDLK_r:
380 redo();
381 console_printf("redo"); break;
382 case SDLK_1:
383 toggle_background = !toggle_background;
384 console_printf("background %s", onoff(toggle_background));
385 break;
386 case SDLK_2:
387 toggle_bgtiles = !toggle_bgtiles;
388 console_printf("bg tiles %s", onoff(toggle_bgtiles));
389 break;
390 case SDLK_3:
391 toggle_fgtiles = !toggle_fgtiles;
392 console_printf("fg tiles %s", onoff(toggle_fgtiles));
393 break;
394 case SDLK_4:
395 toggle_shapes = !toggle_shapes;
396 console_printf("shapes %s", onoff(toggle_shapes));
397 break;
398 case SDLK_s:
399 if(mod & (KMOD_LCTRL|KMOD_RCTRL)){
400 save();
401 console_printf("saved %s", my_filename);
403 break;
404 case SDLK_w:
405 console_printf("save as...");
406 break;
407 case SDLK_o:
408 console_printf("open...");
409 break;
410 case SDLK_b:
411 console_printf("change background...");
412 break;
413 case SDLK_q:
414 if(dialog_flag == 0){
415 dialog_flag = 1;
416 quit_dialog = 1;
418 break;
419 case SDLK_ESCAPE:
420 panic_flag = 1;
421 if(dialog_flag == 0){
422 dialog_flag = 1;
423 quit_dialog = 1;
425 else{
426 dialog_flag = 0;
428 break;
429 case SDLK_RETURN:
430 console_printf("OK");
431 break;
432 case SDLK_y:
433 console_printf("yes");
434 break;
435 case SDLK_n:
436 console_printf("no");
437 break;
438 case SDLK_h:
439 case SDLK_F1:
440 case SDLK_SLASH:
441 console_printf("help...");
442 break;
443 case SDLK_F2:
444 console_printf("pick fg tileset...");
445 break;
446 case SDLK_F3:
447 console_printf("pick bg tileset...");
448 break;
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;
455 redraw_all();
457 console_clear();
459 U - undo
460 R - redo
461 1 - toggle layer 1
462 2 - toggle layer 2
463 3 - toggle layer 3
464 4 - toggle layer 4
465 ctrl S - save
466 W - save as
467 O - open stage
468 B - change background
469 Q - quit
470 ESC - quit / cancel
471 ENTER - yes / OK
472 Y - yes
473 N - no
474 H - help
475 ? - help
476 F1 - help
477 F2 - change fg tileset
478 F3 - change bg tileset
479 ARROW KEYS - scroll
483 void translate_pointer(int mx, int my, int *x, int *y){
484 int a, b;
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();
501 int x, y;
502 translate_pointer(mx, my, &x, &y);
504 if(brush_dialog){
505 //change brush, maybe
506 brush_dialog = 0;
507 return;
513 if(button == 1){
514 raw_write(x, y, brush_layer, brush_tile);
515 brush_enable = 1;
516 redraw_all();
518 else if(button == 3){
519 brush_dialog = 1;
520 redraw_all();
525 void mouseup(int x, int y, int button){
527 LMB - stop drawing
528 shift LMB - append box to selection
529 MMB - execute paste
532 if(button == 1){
533 brush_enable = 0;
538 void mousemove(int mx, int my, int xrel, int yrel){
540 redraw cursor
541 redraw box select
542 redraw paste box
544 int x, y;
545 translate_pointer(mx, my, &x, &y);
547 if(brush_enable){
548 raw_write(x, y, brush_layer, brush_tile);
549 redraw_all();
553 int check_events(){
554 SDL_Event e;
556 if(SDL_WaitEvent(&e) == 0){
557 printf("SDL_WaitEvent encountered an error (%s)\n", SDL_GetError());
558 return 1;
561 switch(e.type){
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);
566 return 0;
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;
569 default: return 0;
575 void terminate(){
576 loader_quit();
577 video_quit();
580 int main(int argc, char* argv[]){
581 video_init(argc, argv);
582 loader_init();
583 graphics_init();
585 raw_tiles = initialize_raw(raw_w, raw_h);
587 loader_data_mode(0);
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);
597 redraw_all();
599 SDL_ShowCursor(1);
600 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
602 atexit(terminate);
604 while(check_events() == 0 && panic_flag == 0);
606 return 0;