Skip to content
Snippets Groups Projects
Commit 1b9931b0 authored by joey.martig's avatar joey.martig
Browse files

Gfx

parent bf121f31
Branches
No related tags found
1 merge request!1Gfx
CC := gcc CC:=gcc
CFLAGS := -std=c11 -Wall -Wextra -fsanitize=address -fsanitize=leak -pedantic -Ofast -g CFLAGS:=-g -O3 -Wall -Wextra -fsanitize=address -fsanitize=leak -std=gnu11
LDFLAGS := ${CFLAGS} -lm -lSDL2 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 main: main.o $(OBJFILES)
OUTDIR := build $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lSDL2
TARGET := ${OUTDIR}/main tests: $(TESTS)
SRC := $(wildcard ${SRCDIR}/*.c) $(TESTS): %: %.o $(OBJFILES)
HDR := $(wildcard ${SRCDIR}/*.h) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lSDL2
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
clean: clean:
rm -rf ${OUTDIR} rm -f *.o main $(TESTS) tests
make -C doc clean
make -C utils clean
exec: ${TARGET}
./${TARGET}
#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++;
}
}
}
#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
#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;
}
#include "physics.h" #include "field.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
......
#include <stdlib.h>
#include <stdio.h>
int main() {
printf("Field_tests\n");
return EXIT_SUCCESS;
}
#include <stdlib.h> #include <stdlib.h>
#include "draw.h"
int main() { int main() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
/// Uses the SDL2 library. /// Uses the SDL2 library.
#include "gfx.h" #include "gfx.h"
#include <assert.h> #include <assert.h>
/// Create a fullscreen graphic window. /// Create a fullscreen graphic window.
...@@ -12,8 +13,7 @@ ...@@ -12,8 +13,7 @@
/// @param width Width of the window in pixels. /// @param width Width of the window in pixels.
/// @param height Height 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. /// @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) if (SDL_Init(SDL_INIT_VIDEO) != 0)
goto error; goto error;
SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
...@@ -47,8 +47,7 @@ error: ...@@ -47,8 +47,7 @@ error:
/// @param column X coordinate of the pixel. /// @param column X coordinate of the pixel.
/// @param row Y coordinate of the pixel. /// @param row Y coordinate of the pixel.
/// @param color Color 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) if (column < ctxt->width && row < ctxt->height)
ctxt->pixels[ctxt->width * row + column] = color; 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 ...@@ -56,8 +55,7 @@ void gfx_putpixel(struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uin
/// Clear the specified graphic context. /// Clear the specified graphic context.
/// @param ctxt Graphic context to clear. /// @param ctxt Graphic context to clear.
/// @param color Color to use. /// @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; int n = ctxt->width * ctxt->height;
while (n) while (n)
ctxt->pixels[--n] = color; ctxt->pixels[--n] = color;
...@@ -65,8 +63,7 @@ void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) ...@@ -65,8 +63,7 @@ void gfx_clear(struct gfx_context_t *ctxt, uint32_t color)
/// Display the graphic context. /// Display the graphic context.
/// @param ctxt Graphic context to clear. /// @param ctxt Graphic context to clear.
void gfx_present(struct gfx_context_t *ctxt) void gfx_present(struct gfx_context_t *ctxt) {
{
SDL_UpdateTexture( SDL_UpdateTexture(
ctxt->texture, NULL, ctxt->pixels, ctxt->width * sizeof(uint32_t)); ctxt->texture, NULL, ctxt->pixels, ctxt->width * sizeof(uint32_t));
SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL); SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL);
...@@ -75,8 +72,7 @@ void gfx_present(struct gfx_context_t *ctxt) ...@@ -75,8 +72,7 @@ void gfx_present(struct gfx_context_t *ctxt)
/// Destroy a graphic window. /// Destroy a graphic window.
/// @param ctxt Graphic context of the window to close. /// @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_ShowCursor(SDL_ENABLE);
SDL_DestroyTexture(ctxt->texture); SDL_DestroyTexture(ctxt->texture);
SDL_DestroyRenderer(ctxt->renderer); SDL_DestroyRenderer(ctxt->renderer);
...@@ -93,11 +89,9 @@ void gfx_destroy(struct gfx_context_t *ctxt) ...@@ -93,11 +89,9 @@ void gfx_destroy(struct gfx_context_t *ctxt)
/// If a key was pressed, returns its key code (non blocking call). /// If a key was pressed, returns its key code (non blocking call).
/// List of key codes: https://wiki.libsdl.org/SDL_Keycode /// List of key codes: https://wiki.libsdl.org/SDL_Keycode
/// @return the key that was pressed or 0 if none was pressed. /// @return the key that was pressed or 0 if none was pressed.
SDL_Keycode gfx_keypressed() SDL_Keycode gfx_keypressed() {
{
SDL_Event event; SDL_Event event;
if (SDL_PollEvent(&event)) if (SDL_PollEvent(&event)) {
{
if (event.type == SDL_KEYDOWN) if (event.type == SDL_KEYDOWN)
return event.key.keysym.sym; return event.key.keysym.sym;
} }
......
...@@ -29,6 +29,7 @@ struct gfx_context_t ...@@ -29,6 +29,7 @@ struct gfx_context_t
uint32_t height; uint32_t height;
}; };
extern void gfx_putpixel( extern void gfx_putpixel(
struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color); 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); extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color);
......
#ifndef _UTILS_H_ #ifndef _UTILS_H_
#define _UTILS_H_ #define _UTILS_H_
#include <stdint.h> #include <stdint.h>
const double K = 8.988e9; static const double K = 8.988e9;
const double E = 1.602e-19; static const double E = 1.602e-19;
typedef struct typedef struct
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment