xdraw

Simple drawing app
git clone https://git.sinitax.com/sinitax/xdraw
Log | Files | Refs | sfeed.txt

commit 052020210ea946aaf9cb819d2d2b76503ae415e4
parent 5629be88954d57fa54305c2ac1f7e77ecb04046b
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 11 Aug 2024 18:01:52 +0200

Rename to xdraw

Diffstat:
M.gitignore | 2+-
MMakefile | 10+++++-----
Dsdraw.c | 182-------------------------------------------------------------------------------
Axdraw.c | 182+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 188 insertions(+), 188 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1 @@ -sdraw +xdraw diff --git a/Makefile b/Makefile @@ -3,17 +3,17 @@ BINDIR ?= /bin LDLIBS = -lglfw -lGL -all: sdraw +all: xdraw -sdraw: sdraw.c +xdraw: xdraw.c clean: - rm -f sdraw + rm -f xdraw install: - install -m755 sdraw -t "$(DESTDIR)$(PREFIX)$(BINDIR)" + install -m755 xdraw -t "$(DESTDIR)$(PREFIX)$(BINDIR)" uninstall: - rm "$(DESTDIR)$(PREFIX)$(BINDIR)/sdraw" + rm "$(DESTDIR)$(PREFIX)$(BINDIR)/xdraw" .PHONY: all clean install uninstall diff --git a/sdraw.c b/sdraw.c @@ -1,182 +0,0 @@ -#include <GL/gl.h> -#include <GLFW/glfw3.h> - -#include <string.h> -#include <stdlib.h> -#include <stdio.h> -#include <stdarg.h> -#include <stdbool.h> - -struct point { - double x, y; -}; - -struct line { - struct point *points; - size_t count, cap; -}; - -static struct line *lines = NULL; -static size_t line_count = 0; -static size_t line_cap = 0; - -static double cursor_x = 0; -static double cursor_y = 0; - -static int window_width = 0; -static int window_height = 0; - -static bool drawing = false; - -static void -die(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - fputs("sdraw: ", stderr); - vfprintf(stderr, fmt, ap); - if (*fmt && fmt[strlen(fmt)] == ':') - perror(NULL); - else - putc('\n', stderr); - va_end(ap); - - exit(1); -} - -static void * -append(void *_array, size_t *count, size_t *cap, size_t size) -{ - uint8_t **array = _array; - if (*count == *cap) { - if (*cap == 0) *cap = 1; - *cap *= 2; - *array = realloc(*array, *cap * size); - if (!*array) die("realloc:"); - } - return (void *) (*array + (*count)++ * size); -} - -static void -glfwerror(int rc, const char *error) -{ - die("glfw error (%i): %s", rc, error); -} - -static void -onresize(GLFWwindow *window, int width, int height) -{ - window_width = width; - window_height = height; - glViewport(0, 0, width, height); - glClear(GL_COLOR_BUFFER_BIT); - glLoadIdentity(); - glOrtho(0, width, height, 0, -1, 1); -} - -static void -onmove(GLFWwindow *window, double x, double y) -{ - cursor_x = x; - cursor_y = y; - if (drawing && line_count) { - struct line *line = &lines[line_count-1]; - if (line->count > 0) { - struct point *last_point = &line->points[line->count-1]; - if (cursor_x == last_point->x && cursor_y == last_point->y) - return; - } - struct point *point = append(&line->points, - &line->count, &line->cap, sizeof(struct point)); - point->x = cursor_x; - point->y = cursor_y; - } -} - -static void -onclick(GLFWwindow *window, int button, int action, int mods) -{ - if (button != GLFW_MOUSE_BUTTON_LEFT) return; - - if (action == GLFW_PRESS) { - struct line *line = append(&lines, - &line_count, &line_cap, sizeof(struct line)); - line->count = line->cap = 0; - line->points = NULL; - struct point *point = append(&line->points, - &line->count, &line->cap, sizeof(struct point)); - point->x = cursor_x; - point->y = cursor_y; - drawing = true; - } else if (action == GLFW_RELEASE) { - drawing = false; - } -} - -static void -onpress(GLFWwindow *window, int key, int scancode, int action, int mods) -{ - if (action != GLFW_PRESS) return; - - if ((mods & GLFW_MOD_CONTROL) && !strcmp(glfwGetKeyName(key, scancode), "z")) { - if (line_count) { - line_count -= 1; - free(lines[line_count].points); - } - } else if ((mods & GLFW_MOD_CONTROL) && !strcmp(glfwGetKeyName(key, scancode), "c")) { - for (size_t i = 0; i < line_count; i++) - free(lines[i].points); - line_count = 0; - } -} - -int -main(void) -{ - glfwSetErrorCallback(glfwerror); - - if (!glfwInit()) die("glfwInit"); - - GLFWwindow *window = glfwCreateWindow(640, 480, "sdraw", NULL, NULL); - if (!window) die("glfwCreateWindow"); - - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - glfwSetWindowSizeCallback(window, onresize); - glfwSetCursorPosCallback(window, onmove); - glfwSetMouseButtonCallback(window, onclick); - glfwSetKeyCallback(window, onpress); - glfwMakeContextCurrent(window); - - glClearColor(0, 0, 0, 0); - glColor3f(1, 1, 1); - glLineWidth(2); - glPointSize(2); - - glfwGetCursorPos(window, &cursor_x, &cursor_y); - glfwGetWindowSize(window, &window_width, &window_height); - - onresize(window, window_width, window_height); - - while (!glfwWindowShouldClose(window)) { - glClear(GL_COLOR_BUFFER_BIT); - - for (size_t i = 0; i < line_count; i++) { - struct line *line = &lines[i]; - glBegin(GL_LINE_STRIP); - for (size_t k = 0; k < line->count; k++) - glVertex2f(line->points[k].x, line->points[k].y); - glEnd(); - } - - glBegin(GL_POINTS); - glVertex2f(cursor_x, cursor_y); - glEnd(); - - glfwSwapBuffers(window); - - glfwPollEvents(); - } - - glfwTerminate(); -} diff --git a/xdraw.c b/xdraw.c @@ -0,0 +1,182 @@ +#include <GL/gl.h> +#include <GLFW/glfw3.h> + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <stdbool.h> + +struct point { + double x, y; +}; + +struct line { + struct point *points; + size_t count, cap; +}; + +static struct line *lines = NULL; +static size_t line_count = 0; +static size_t line_cap = 0; + +static double cursor_x = 0; +static double cursor_y = 0; + +static int window_width = 0; +static int window_height = 0; + +static bool drawing = false; + +static void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fputs("xdraw: ", stderr); + vfprintf(stderr, fmt, ap); + if (*fmt && fmt[strlen(fmt)] == ':') + perror(NULL); + else + putc('\n', stderr); + va_end(ap); + + exit(1); +} + +static void * +append(void *_array, size_t *count, size_t *cap, size_t size) +{ + uint8_t **array = _array; + if (*count == *cap) { + if (*cap == 0) *cap = 1; + *cap *= 2; + *array = realloc(*array, *cap * size); + if (!*array) die("realloc:"); + } + return (void *) (*array + (*count)++ * size); +} + +static void +glfwerror(int rc, const char *error) +{ + die("glfw error (%i): %s", rc, error); +} + +static void +onresize(GLFWwindow *window, int width, int height) +{ + window_width = width; + window_height = height; + glViewport(0, 0, width, height); + glClear(GL_COLOR_BUFFER_BIT); + glLoadIdentity(); + glOrtho(0, width, height, 0, -1, 1); +} + +static void +onmove(GLFWwindow *window, double x, double y) +{ + cursor_x = x; + cursor_y = y; + if (drawing && line_count) { + struct line *line = &lines[line_count-1]; + if (line->count > 0) { + struct point *last_point = &line->points[line->count-1]; + if (cursor_x == last_point->x && cursor_y == last_point->y) + return; + } + struct point *point = append(&line->points, + &line->count, &line->cap, sizeof(struct point)); + point->x = cursor_x; + point->y = cursor_y; + } +} + +static void +onclick(GLFWwindow *window, int button, int action, int mods) +{ + if (button != GLFW_MOUSE_BUTTON_LEFT) return; + + if (action == GLFW_PRESS) { + struct line *line = append(&lines, + &line_count, &line_cap, sizeof(struct line)); + line->count = line->cap = 0; + line->points = NULL; + struct point *point = append(&line->points, + &line->count, &line->cap, sizeof(struct point)); + point->x = cursor_x; + point->y = cursor_y; + drawing = true; + } else if (action == GLFW_RELEASE) { + drawing = false; + } +} + +static void +onpress(GLFWwindow *window, int key, int scancode, int action, int mods) +{ + if (action != GLFW_PRESS) return; + + if ((mods & GLFW_MOD_CONTROL) && !strcmp(glfwGetKeyName(key, scancode), "z")) { + if (line_count) { + line_count -= 1; + free(lines[line_count].points); + } + } else if ((mods & GLFW_MOD_CONTROL) && !strcmp(glfwGetKeyName(key, scancode), "c")) { + for (size_t i = 0; i < line_count; i++) + free(lines[i].points); + line_count = 0; + } +} + +int +main(void) +{ + glfwSetErrorCallback(glfwerror); + + if (!glfwInit()) die("glfwInit"); + + GLFWwindow *window = glfwCreateWindow(640, 480, "xdraw", NULL, NULL); + if (!window) die("glfwCreateWindow"); + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + glfwSetWindowSizeCallback(window, onresize); + glfwSetCursorPosCallback(window, onmove); + glfwSetMouseButtonCallback(window, onclick); + glfwSetKeyCallback(window, onpress); + glfwMakeContextCurrent(window); + + glClearColor(0, 0, 0, 0); + glColor3f(1, 1, 1); + glLineWidth(2); + glPointSize(2); + + glfwGetCursorPos(window, &cursor_x, &cursor_y); + glfwGetWindowSize(window, &window_width, &window_height); + + onresize(window, window_width, window_height); + + while (!glfwWindowShouldClose(window)) { + glClear(GL_COLOR_BUFFER_BIT); + + for (size_t i = 0; i < line_count; i++) { + struct line *line = &lines[i]; + glBegin(GL_LINE_STRIP); + for (size_t k = 0; k < line->count; k++) + glVertex2f(line->points[k].x, line->points[k].y); + glEnd(); + } + + glBegin(GL_POINTS); + glVertex2f(cursor_x, cursor_y); + glEnd(); + + glfwSwapBuffers(window); + + glfwPollEvents(); + } + + glfwTerminate(); +}