Skip to content
Snippets Groups Projects
Commit c41ff6df authored by JM's avatar JM
Browse files

struct + drawline + makefile

parent a6186376
No related branches found
No related tags found
1 merge request!1Gfx
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
#The compiler
CC:=gcc
CFLAGS := -std=c11 -Wall -Wextra -fsanitize=address -fsanitize=leak -pedantic -Ofast -g
LDFLAGS := ${CFLAGS} -lm -lSDL2
#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
src/draw.c 0 → 100644
#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) {
}
src/draw.h 0 → 100644
#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
#include "physics.h"
#include "field.h"
#include <math.h>
#include <stdlib.h>
......
#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;
}
......@@ -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;
}
\ No newline at end of file
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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment