#include #include #include #include "cdraw.h" #include "types.h" #include "action.h" #include "canvas.h" #include "debug.h" #include "palette.h" #include "ui.h" typedef struct { SDL_Keymod mod; SDL_Keycode sym; void (*func)(const Arg *); const Arg arg; } Key; #include "config.h" SDL_Window *win; void *ren; void *def_palette; struct Canvas* cur_canvas; /* TODO: window resize change mouseX and mouse Y */ int mouseX, mouseY; void keypress(SDL_Keysym *); void keypress(SDL_Keysym *k) { static Key *p, *end; /* printf("Key pressed: %d %d %d\n", k->sym, k->mod, KMOD_SHIFT); */ for (p = keys, end = &p[LENGTH(keys)]; p != end; ++p) { if (k->sym == p->sym && k->mod == p->mod) p->func(&(p->arg)); } } uint main_quit() { /* TODO: checks */ canvas_destroy(cur_canvas); ui_destroy(); SDL_DestroyWindow(win); IMG_Quit(); SDL_Quit(); return 1; } uint main_window_init(const char *s) { int ss; if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL_Init failed: %s\n", SDL_GetError()); return 1; } if (IMG_Init(IMG_INIT_PNG) == 0) return 1; win = SDL_CreateWindow("cdraw", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 512, 256, SDL_WINDOW_RESIZABLE); if (win == NULL) { printf("SDL_CreateWindow failed: %s\n", SDL_GetError()); return 1; } ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC); if (ren == NULL) { printf("SDL_CreateRenderer failed: %s\n", SDL_GetError()); return 1; } SDL_SetRenderDrawColor(ren, INTTOCOLA(0x121212ff)); SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_NONE); SDL_RenderPresent(ren); def_palette = palette_open_gpl(def_palette_path); if (def_palette == NULL) return 1; ui_create(def_theme_path); if (s == NULL) cur_canvas = canvas_init(64, 64, ren); else { ss = strlen(s); if (ss > 4 && strcmp(&s[ss-4], ".cdr") == 0) cur_canvas = canvas_open(s, ren); else cur_canvas = canvas_import_png(s, ren); } ui_redraw_panel(UI_PANELTYPE_CANVAS); if (cur_canvas == NULL) return 1; return 0; } uint main_event_handle() { static uint8 q, r; static SDL_Event event; q = r = 0; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: q = 1; break; case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: r = 1; /* FALLTROUGH */ case SDL_WINDOWEVENT_MOVED: break; } break; case SDL_MOUSEBUTTONDOWN: switch (event.button.button) { case SDL_BUTTON_LEFT: ui_mousel_down(event.button.x, event.button.y); break; case SDL_BUTTON_RIGHT: break; } break; case SDL_MOUSEBUTTONUP: switch (event.button.button) { case SDL_BUTTON_LEFT: ui_mousel_up(event.button.x, event.button.y); is_drawing = 0; break; } break; case SDL_MOUSEMOTION: mouseX = event.motion.x; mouseY = event.motion.y; ui_mouse_move(mouseX, mouseY); break; case SDL_KEYDOWN: keypress(&event.key.keysym); break; } } if (r) ui_resize(); SDL_SetRenderDrawColor(ren, INTTOCOL(back_c), 255); SDL_RenderClear(ren); ui_present(); SDL_RenderPresent(ren); if (q) return main_quit(); return 0; } int main(int argc, char *argv[]) { Uint32 ltick, fdif; if (main_window_init(argc > 1 ? argv[1] : NULL)) return 1; fdif = 1000 / fps; while (1) { ltick = SDL_GetTicks(); if (main_event_handle()) break; ltick = SDL_GetTicks() - ltick; if (ltick < fdif) SDL_Delay(fdif-ltick); } #ifdef F_MEMORY_DEBUG f_debug_mem_show(); #endif }