diff --git a/Makefile b/Makefile index c2fa0b1d39295e8a14abd93702c231b9c3c755aa..dfb9627faa32ad11d7542fccf530da0016874a95 100644 --- a/Makefile +++ b/Makefile @@ -1,45 +1,17 @@ -CC := gcc -CFLAGS := -std=c11 -Wall -Wextra -fsanitize=address -fsanitize=leak -pedantic -Ofast -g -LDFLAGS := ${CFLAGS} -lm -lSDL2 +CC:=gcc +CFLAGS:=-g -O3 -Wall -Wextra -fsanitize=address -fsanitize=leak -std=gnu11 +LDFLAGS:=-lm +VPATH:=utils utils/vec2 utils/gfx src +OBJFILES:= vec2.o utils.o field.o draw.o gfx.o +TESTS:= draw_tests field_tests -SRCDIR := src -OUTDIR := build +main: main.o $(OBJFILES) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lSDL2 -TARGET := ${OUTDIR}/main +tests: $(TESTS) -SRC := $(wildcard ${SRCDIR}/*.c) -HDR := $(wildcard ${SRCDIR}/*.h) -OBJ := $(patsubst %.c,${OUTDIR}/%.o,${SRC}) - - -all: ${TARGET} ${DOC} - - -${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 +$(TESTS): %: %.o $(OBJFILES) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lSDL2 clean: - rm -rf ${OUTDIR} - make -C doc clean - make -C utils clean - -exec: ${TARGET} - ./${TARGET} + rm -f *.o main $(TESTS) tests diff --git a/src/draw.c b/src/draw.c new file mode 100644 index 0000000000000000000000000000000000000000..923a8e1827965fca443b42870a67a7e07baf1df6 --- /dev/null +++ b/src/draw.c @@ -0,0 +1,66 @@ +#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_t p0, coordinates_t p1, uint32_t color) { + 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) { + 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) { + int x = 0; + int y = r; + int d = r - 1; + while(y >= x){ + gfx_putpixel(ctxt, c.row + x, c.column + y, color); + gfx_putpixel(ctxt, c.row + y, c.column + x, color); + gfx_putpixel(ctxt, c.row - x, c.column + y, color); + gfx_putpixel(ctxt, c.row - y, c.column + x, color); + gfx_putpixel(ctxt, c.row + x, c.column - y, color); + gfx_putpixel(ctxt, c.row + y, c.column - x, color); + gfx_putpixel(ctxt, c.row - x, c.column - y, color); + gfx_putpixel(ctxt, c.row - y, c.column - x, color); + if(d >= 2*x){ + d = d-2*x-1; + x++; + }else if(d < 2 * (r-y)){ + d = d + 2*y-1; + y--; + }else { + d = d + 2 * (y-x-1); + y--; + x++; + } + } +} diff --git a/src/draw.h b/src/draw.h new file mode 100644 index 0000000000000000000000000000000000000000..5ac6b731f6bc9a958959c2cb23ecfe8d352f8f4d --- /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_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 \ No newline at end of file diff --git a/src/draw_tests.c b/src/draw_tests.c new file mode 100644 index 0000000000000000000000000000000000000000..4eba0e1356e801c294ba359993e4c4c4777e40bd --- /dev/null +++ b/src/draw_tests.c @@ -0,0 +1,56 @@ +#include <stdlib.h> +#include "draw.h" + +int main() { + struct gfx_context_t* ctxt = gfx_create("main", 100, 100); + + coordinates_t src = coordinates_create(50, 50); + coordinates_t dst = coordinates_create(75, 50); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(72, 62); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(62, 72); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(50, 75); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(38, 72); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(28, 62); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(25, 50); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(28, 38); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(37, 28); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(50, 25); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(62, 28); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + dst = coordinates_create(72, 37); + gfx_draw_line(ctxt, src, dst, COLOR_WHITE); + + coordinates_t c = coordinates_create(50, 50); + int r = 25; + gfx_draw_circle(ctxt, c, r, COLOR_RED); + + while (true) { + if (gfx_keypressed() == SDLK_ESCAPE) + break; + + gfx_present(ctxt); + } + + return EXIT_SUCCESS; +} diff --git a/src/field.c b/src/field.c index 8bff07808808b201785e108cd9001895c5c6342a..18a18c2b7d7adccc68129336ea17e4879798085c 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/field_tests.c b/src/field_tests.c new file mode 100644 index 0000000000000000000000000000000000000000..8f26ef5e8dbc89f8f100f0b6cffb7c40d185b354 --- /dev/null +++ b/src/field_tests.c @@ -0,0 +1,7 @@ +#include <stdlib.h> +#include <stdio.h> + +int main() { + printf("Field_tests\n"); + return EXIT_SUCCESS; +} diff --git a/src/main.c b/src/main.c index d77a0e89a55256c4f47c97817c00f6d7c5eb4b9b..069ed50b9ec26ab97eeb7fb90b630608085a8f0e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include <stdlib.h> +#include "draw.h" int main() { return EXIT_SUCCESS; diff --git a/utils/gfx/gfx.c b/utils/gfx/gfx.c index cd6662f178c2bc3eb8326efae92ca7fa7436c1c6..27aeb3085eb6c9165ed6fcbb5f6df69bbe6c40ae 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,11 +89,9 @@ 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; } diff --git a/utils/gfx/gfx.h b/utils/gfx/gfx.h index 4d9d13a965b264265a47243d650b7a5965a92b81..6e256562a7a2cee62a0c16358d36364d50ee4a03 100644 --- a/utils/gfx/gfx.h +++ b/utils/gfx/gfx.h @@ -29,6 +29,7 @@ struct gfx_context_t uint32_t height; }; + extern void gfx_putpixel( struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color); extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color); diff --git a/utils/utils.h b/utils/utils.h index 7c45fca5bb1636c98b460562eac3c89b215953aa..43c02948c138c7460a471032be4ddfd05626729a 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -1,9 +1,10 @@ #ifndef _UTILS_H_ #define _UTILS_H_ + #include <stdint.h> -const double K = 8.988e9; -const double E = 1.602e-19; +static const double K = 8.988e9; +static const double E = 1.602e-19; typedef struct {