From c41ff6df4dfac0feba88f0f359b7347a36301bba Mon Sep 17 00:00:00 2001 From: JM <crewgan@pop-os.localdomain> Date: Mon, 2 May 2022 11:38:34 +0200 Subject: [PATCH] struct + drawline + makefile --- Makefile | 53 +++++++++++++++---------------------------------- src/draw.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/draw.h | 9 +++++++++ src/field.c | 2 +- src/main.c | 8 ++++++++ utils/gfx/gfx.c | 31 ++++++++--------------------- utils/gfx/gfx.h | 7 ------- 7 files changed, 91 insertions(+), 68 deletions(-) create mode 100644 src/draw.c create mode 100644 src/draw.h diff --git a/Makefile b/Makefile index c2fa0b1..bfd0005 100644 --- a/Makefile +++ b/Makefile @@ -1,45 +1,24 @@ -CC := gcc -CFLAGS := -std=c11 -Wall -Wextra -fsanitize=address -fsanitize=leak -pedantic -Ofast -g -LDFLAGS := ${CFLAGS} -lm -lSDL2 +#The compiler +CC:=gcc +#The flags passed to the compiler +CFLAGS:=-g -O3 -Wall -Wextra -fsanitize=address -fsanitize=leak -std=gnu11 +#The flags passed to the linker +LDFLAGS:=-lm +VPATH:=utils utils/vec2 utils/gfx src -SRCDIR := src -OUTDIR := build +main: main.o vec2.o utils.o field.o gfx.o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lSDL2 -TARGET := ${OUTDIR}/main +tests: vec_tests.o vec2.o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -SRC := $(wildcard ${SRCDIR}/*.c) -HDR := $(wildcard ${SRCDIR}/*.h) -OBJ := $(patsubst %.c,${OUTDIR}/%.o,${SRC}) +field.o: field.h +vec2.o: vec2.h -all: ${TARGET} ${DOC} +utils.o: utils.h - -${TARGET}: ${OBJ} utils ${OUTDIR} - ${CC} ${LDFLAGS} -o $@ ${OBJ} - - -${OBJ}: ${OUTDIR}/%.o: %.c ${HDR} ${OUTDIR} - ${CC} ${CFLAGS} -c -o $@ $< - - -${OUTDIR}: - mkdir -p ${OUTDIR} - - - -.PHONY: all clean doc exec utils - -doc: - make -C doc - -utils: - make -C utils +gfx.o: gfx.h clean: - rm -rf ${OUTDIR} - make -C doc clean - make -C utils clean - -exec: ${TARGET} - ./${TARGET} + rm -f *.o main tests diff --git a/src/draw.c b/src/draw.c new file mode 100644 index 0000000..0f1d9c2 --- /dev/null +++ b/src/draw.c @@ -0,0 +1,49 @@ +#include <utils/gfx/gfx.h> +#include <field.h> +/* +(50, 50) → (75, 50)1 , (50, 50) → (72, 62), (50, 50) → (62, 72) +(50, 50) → (50, 75), (50, 50) → (38, 72), (50, 50) → (28, 62) +(50, 50) → (25, 50), (50, 50) → (28, 38), (50, 50) → (37, 28) +(50, 50) → (50, 25), (50, 50) → (62, 28), (50, 50) → (72, 37) +*/ + +void gfx_draw_line(struct gfx_context_t *ctxt, coordinates p0, coordinates p1, uint32_t color) { + // x =col, y = row + // x0 p0.column + // x1 p1.column + // y0 p0.row + // y1 p1.row + int dx = abs(p1.column - p0.column); + int sx = p0.column < p1.column ? 1 : -1; + int dy = -abs(p1.row - p0.row); + int sy = p0.row < p1.row ? 1 : -1; + int error = dx + dy; + + while (true) { + //plot(p0.column, p0.row); + gfx_putpixel(ctxt, p0.column, p0.row, color); + + if (p0.column == p1.column && p0.row == p1.row) + break; + + int e2 = 2 * error; + + if (e2 >= dy) { + if (p0.column == p1.column) + break; + error = error + dy; + p0.column = p0.column + sx; + } + + if (e2 <= dx) { + if (p0.row == p1.row) + break; + error = error + dx; + p0.row = p0.row + sy; + } + } + +} + +void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates c, uint32_t r, uint32_t color) { +} diff --git a/src/draw.h b/src/draw.h new file mode 100644 index 0000000..dd02f59 --- /dev/null +++ b/src/draw.h @@ -0,0 +1,9 @@ +#ifndef DRAW_H +#define DRAW_H + +#include <field.h> + +void gfx_draw_line(struct gfx_context_t *ctxt, coordinates p0, coordinates p1, uint32_t color); +void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates c, uint32_t r, uint32_t color); + +#endif \ No newline at end of file diff --git a/src/field.c b/src/field.c index 8bff078..18a18c2 100644 --- a/src/field.c +++ b/src/field.c @@ -1,4 +1,4 @@ -#include "physics.h" +#include "field.h" #include <math.h> #include <stdlib.h> diff --git a/src/main.c b/src/main.c index d77a0e8..2fbb85c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,13 @@ #include <stdlib.h> +//#include <utils/gfx/gfx.h> int main() { + /* + (50, 50) → (75, 50)1 , (50, 50) → (72, 62), (50, 50) → (62, 72) + (50, 50) → (50, 75), (50, 50) → (38, 72), (50, 50) → (28, 62) + (50, 50) → (25, 50), (50, 50) → (28, 38), (50, 50) → (37, 28) + (50, 50) → (50, 25), (50, 50) → (62, 28), (50, 50) → (72, 37) + */ + //struct gfx_content_t* gfx = gfx_create("main", 100, 100); return EXIT_SUCCESS; } diff --git a/utils/gfx/gfx.c b/utils/gfx/gfx.c index a77eb85..27aeb30 100644 --- a/utils/gfx/gfx.c +++ b/utils/gfx/gfx.c @@ -5,6 +5,7 @@ /// Uses the SDL2 library. #include "gfx.h" + #include <assert.h> /// Create a fullscreen graphic window. @@ -12,8 +13,7 @@ /// @param width Width of the window in pixels. /// @param height Height of the window in pixels. /// @return a pointer to the graphic context or NULL if it failed. -struct gfx_context_t *gfx_create(char *title, uint32_t width, uint32_t height) -{ +struct gfx_context_t *gfx_create(char *title, uint32_t width, uint32_t height) { if (SDL_Init(SDL_INIT_VIDEO) != 0) goto error; SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, @@ -47,8 +47,7 @@ error: /// @param column X coordinate of the pixel. /// @param row Y coordinate of the pixel. /// @param color Color of the pixel. -void gfx_putpixel(struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color) -{ +void gfx_putpixel(struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color) { if (column < ctxt->width && row < ctxt->height) ctxt->pixels[ctxt->width * row + column] = color; } @@ -56,8 +55,7 @@ void gfx_putpixel(struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uin /// Clear the specified graphic context. /// @param ctxt Graphic context to clear. /// @param color Color to use. -void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) -{ +void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) { int n = ctxt->width * ctxt->height; while (n) ctxt->pixels[--n] = color; @@ -65,8 +63,7 @@ void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) /// Display the graphic context. /// @param ctxt Graphic context to clear. -void gfx_present(struct gfx_context_t *ctxt) -{ +void gfx_present(struct gfx_context_t *ctxt) { SDL_UpdateTexture( ctxt->texture, NULL, ctxt->pixels, ctxt->width * sizeof(uint32_t)); SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL); @@ -75,8 +72,7 @@ void gfx_present(struct gfx_context_t *ctxt) /// Destroy a graphic window. /// @param ctxt Graphic context of the window to close. -void gfx_destroy(struct gfx_context_t *ctxt) -{ +void gfx_destroy(struct gfx_context_t *ctxt) { SDL_ShowCursor(SDL_ENABLE); SDL_DestroyTexture(ctxt->texture); SDL_DestroyRenderer(ctxt->renderer); @@ -93,22 +89,11 @@ void gfx_destroy(struct gfx_context_t *ctxt) /// If a key was pressed, returns its key code (non blocking call). /// List of key codes: https://wiki.libsdl.org/SDL_Keycode /// @return the key that was pressed or 0 if none was pressed. -SDL_Keycode gfx_keypressed() -{ +SDL_Keycode gfx_keypressed() { SDL_Event event; - if (SDL_PollEvent(&event)) - { + if (SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN) return event.key.keysym.sym; } return 0; -} - - -void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color){ - -} - -void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, uint32_t r, uint32_t color){ - } \ No newline at end of file diff --git a/utils/gfx/gfx.h b/utils/gfx/gfx.h index 37423cc..6e25656 100644 --- a/utils/gfx/gfx.h +++ b/utils/gfx/gfx.h @@ -29,11 +29,6 @@ struct gfx_context_t uint32_t height; }; -typedef struct -{ - uint32_t row; - uint32_t column; -} coordinates_t; extern void gfx_putpixel( struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color); @@ -42,7 +37,5 @@ extern struct gfx_context_t *gfx_create(char *text, uint32_t width, uint32_t hei extern void gfx_destroy(struct gfx_context_t *ctxt); extern void gfx_present(struct gfx_context_t *ctxt); extern SDL_Keycode gfx_keypressed(); -void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color); -void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, uint32_t r, uint32_t color); #endif -- GitLab