aboutsummaryrefslogtreecommitdiff
path: root/src/cdraw.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cdraw.c')
-rw-r--r--src/cdraw.c194
1 files changed, 194 insertions, 0 deletions
diff --git a/src/cdraw.c b/src/cdraw.c
new file mode 100644
index 0000000..5f3a1b5
--- /dev/null
+++ b/src/cdraw.c
@@ -0,0 +1,194 @@
+#include <stdio.h>
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+
+#include "cdraw.h"
+#include "types.h"
+#include "canvas.h"
+#include "debug.h"
+#include "palette.h"
+#include "ui.h"
+
+/* macros */
+#define LENGTH(X) (sizeof X / sizeof X[0])
+
+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, 0);
+ if (ren == NULL) {
+ printf("SDL_CreateRenderer failed: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ SDL_SetRenderDrawColor(ren, 18, 18, 18, 255);
+ SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND);
+ SDL_RenderPresent(ren);
+
+ def_palette = palette_open_gpl(def_palette_path);
+ if (def_palette == NULL)
+ return 1;
+
+ ui_create("assets/ui.png");
+ 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
+}