~artemis/svg

453fec2fd6964d6ad2b8bf3d9fa6d77ddd708830 — Artemis 1 year, 3 months ago 0eeb510
added borders and border toggle shortcut
1 files changed, 69 insertions(+), 45 deletions(-)

M svg.c
M svg.c => svg.c +69 -45
@@ 72,9 72,9 @@ bool load_image(const char *filename, load_image_res *res)
	nsvgRasterize(res->raster, res->image, 0, 0, 1, res->img, res->image->width, res->image->height, res->image->width * 4);

	res->surface = SDL_CreateRGBSurfaceFrom(
			(void *)res->img,
			res->image->width, res->image->height, 32, 4 * res->image->width,
			0xff, 0xff00, 0xff0000, 0xff000000);
		(void *)res->img,
		res->image->width, res->image->height, 32, 4 * res->image->width,
		0xff, 0xff00, 0xff0000, 0xff000000);
	if (!res->surface)
	{
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface: %s", SDL_GetError());


@@ 108,12 108,12 @@ int main(int argc, char **argv)
	{
		switch (opt)
		{
			case 'w':
				watch = false;
				break;
			default:
				fprintf(stderr, "Usage: %s [-w] file.svg\n\n\t-w\t disable watch mode (if you only want to display)\n", argv[0]);
				return EXIT_FAILURE;
		case 'w':
			watch = false;
			break;
		default:
			fprintf(stderr, "Usage: %s [-w] file.svg\n\n\t-w\t disable watch mode (if you only want to display)\n", argv[0]);
			return EXIT_FAILURE;
		}
	}
	if (optind >= argc)


@@ 126,19 126,20 @@ int main(int argc, char **argv)

	int inotify_fd, inotify_wd;
	char eventbuf[BUFSIZE];
	if (watch) {
	inotify_fd = inotify_init1(IN_NONBLOCK);
	if (inotify_fd == -1)
	{
		perror("inotify_init1");
		return EXIT_FAILURE;
	}
	inotify_wd = inotify_add_watch(inotify_fd, fname, IN_MODIFY);
	if (inotify_wd == -1)
	if (watch)
	{
		fprintf(stderr, "cannot watch %s: %s\n", fname, strerror(errno));
		goto die;
	}
		inotify_fd = inotify_init1(IN_NONBLOCK);
		if (inotify_fd == -1)
		{
			perror("inotify_init1");
			return EXIT_FAILURE;
		}
		inotify_wd = inotify_add_watch(inotify_fd, fname, IN_MODIFY);
		if (inotify_wd == -1)
		{
			fprintf(stderr, "cannot watch %s: %s\n", fname, strerror(errno));
			goto die;
		}
	}

	if (SDL_Init(SDL_INIT_VIDEO) < 0)


@@ 174,6 175,8 @@ int main(int argc, char **argv)
	SDL_Event event;
	bool needs_file_reload = false, needs_position = false, needs_redraw = true;
	bool looping = true;
	bool display_borders = true;
	SDL_Rect border_position = {.x = 0, .y = 0, .h = 1, .w = 1};
	while (looping)
	{
		// UI Events


@@ 186,31 189,39 @@ int main(int argc, char **argv)
		else
			switch (event.type)
			{
				case SDL_QUIT:
					looping = false;
					needs_file_reload = false;
					needs_position = false;
					needs_redraw = false;
					break;
				case SDL_KEYDOWN:
				case SDL_MOUSEBUTTONDOWN:
					{
						if (event.key.keysym.sym == SDLK_r || event.button.button == SDL_BUTTON_LEFT)
						{
							needs_file_reload = true;
						}
					}
					break;
				case SDL_WINDOWEVENT:
					switch (event.window.event)
			case SDL_QUIT:
				looping = false;
				needs_file_reload = false;
				needs_position = false;
				needs_redraw = false;
				break;
			case SDL_KEYDOWN:
			case SDL_MOUSEBUTTONDOWN:
			{
				if (event.key.keysym.sym == SDLK_r || event.button.button == SDL_BUTTON_LEFT)
				{
					needs_file_reload = true;
				}
				else
					switch (event.key.keysym.sym)
					{
						case SDL_WINDOWEVENT_RESIZED:
							{
								needs_position = true;
							}
					case SDLK_b:
						display_borders = !display_borders;
						printf(display_borders ? "Enabled borders\n" : "Disabled borders\n");
						needs_position = true;
					}
					needs_redraw = true;
					break;
			}
			break;
			case SDL_WINDOWEVENT:
				switch (event.window.event)
				{
				case SDL_WINDOWEVENT_RESIZED:
				{
					needs_position = true;
				}
				}
				needs_redraw = true;
				break;
			}

		if (needs_file_reload)


@@ 236,11 247,16 @@ int main(int argc, char **argv)
				.h = h,
			};
			recompute_image_position(&image.box, win_coords);
			border_position.x = image.box.x - 1;
			border_position.y = image.box.y - 1;
			border_position.w = image.box.w + 2;
			border_position.h = image.box.h + 2;
			needs_redraw = true;
		}

		// FS Events
		if (watch) {
		if (watch)
		{
			int event_len = read(inotify_fd, eventbuf, BUFSIZE);
			if (event_len > 0)
			{


@@ 262,7 278,15 @@ int main(int argc, char **argv)
			needs_redraw = false;
			SDL_SetRenderDrawColor(renderer, 0xDD, 0xDD, 0xDD, 0xFF);
			SDL_RenderClear(renderer);

			SDL_RenderCopy(renderer, texture, NULL, &image.box);

			if (display_borders)
			{
				SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
				SDL_RenderDrawRect(renderer, &border_position);
			}

			SDL_RenderPresent(renderer);
		}
	}