aboutsummaryrefslogtreecommitdiff
path: root/src/cdraw.c
blob: 2e0097ac91cfef2aa13be58eecb262012c54601d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#define SBDF_IMPLEMENTATION
#define SBDF_USESDL
#include "sbdf.h"
#include "cdraw.h"
#include "types.h"
#include "debug.h"
#include "action.h"
#include "canvas.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"

enum CDRAW_STATES {
	CDRAW_STATE_NORMAL,
	CDRAW_STATE_LNAME,   /* Editing layer name */
} cdraw_state;

SDL_Window *win;
void *ren;
void *font;
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();
	SBDF_FontDestroy(font);
	font = NULL;
	return 1;
}

uint
main_window_init(const char *s)
{
	/* TODO: defer*/
	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;

	font = SBDF_FontLoad(def_font);
	if (font == 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);
	}

	cdraw_state = CDRAW_STATE_NORMAL;
	SDL_StopTextInput();
    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;
	char *text;
	q = r = 0;
	while (SDL_PollEvent(&event)) {
		switch (cdraw_state) {
		case CDRAW_STATE_NORMAL:
			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:
						if (ui_mouser_down(event.button.x, event.button.y)) {
							cdraw_state = CDRAW_STATE_LNAME;
							SDL_StartTextInput();
						}
						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;
			}
			break;

		/* Input while editing layer name */
		case CDRAW_STATE_LNAME:
			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:
				cdraw_state = CDRAW_STATE_NORMAL;
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym) {
				case SDLK_BACKSPACE:
					if (cur_canvas == NULL) break;
					text = cur_canvas->layers[cur_canvas->a]->name;
					if (strlen(text))
						text[strlen(text)-1] = '\0';
					ui_redraw_panel(UI_PANELTYPE_TIMELINE);
					break;
				case SDLK_ESCAPE:
				case SDLK_RETURN:
					cdraw_state = CDRAW_STATE_NORMAL;
					break;
				}
				break;
			case SDL_TEXTINPUT:
				if (cur_canvas == NULL) break;
				text = cur_canvas->layers[cur_canvas->a]->name;
				if (strlen(text) >= LAYERNAMELEN) break;
				strncat(text, event.text.text, LAYERNAMELEN - strlen(text));
				ui_redraw_panel(UI_PANELTYPE_TIMELINE);
				break;
			}
			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);
    }
}