aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile18
-rw-r--r--config.h16
-rw-r--r--graphics.c148
-rw-r--r--graphics.h (renamed from inc/graphics.h)14
-rw-r--r--inc/cardioid.h27
-rw-r--r--main.c25
-rw-r--r--src/cardioid.c45
-rw-r--r--src/graphics.c104
-rw-r--r--src/main.c26
9 files changed, 206 insertions, 217 deletions
diff --git a/Makefile b/Makefile
index 44c7a9c..dabbf60 100644
--- a/Makefile
+++ b/Makefile
@@ -3,27 +3,23 @@ MKDIR = mkdir -p
NAME = cardioid
-SRCDIR = src
-INCDIR = inc
-OBJDIR = obj
-
CC = gcc
CCFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags)
LDFLAGS = $(shell sdl2-config --libs) -lm
-SRC = $(shell find $(SRCDIR) -type f -name "*.c")
-INC = $(shell find $(INCDIR) -type f -name "*.h")
-OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
+SRC = $(shell find . -type f -name "*.c")
+INC = $(shell find . -type f -name "*.h")
+OBJ = $(SRC:.c=.o)
-all: prebuild $(NAME)
+all: $(NAME)
-prebuild:
- $(MKDIR) $(OBJDIR)
+release: CCFLAGS += -D CARDIOID_RELEASE -Ofast
+release: re
$(NAME): $(OBJ)
$(CC) -o $@ $^ $(LDFLAGS)
-$(OBJDIR)/%.o: $(SRCDIR)/%.c $(INC)
+%.o: %.c $(INC)
$(CC) $(CCFLAGS) -c -o $@ $<
clean:
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..1347faa
--- /dev/null
+++ b/config.h
@@ -0,0 +1,16 @@
+#ifndef CONFIG_H
+# define CONFIG_H
+
+// # define LINE_COLOR 0x151515ff
+// # define LINE_COLOR 0xb71c1cff
+# define LINE_COLOR 0x280000ff
+
+# define POINTS_STEP 1.0
+# define FACTOR_STEP 0.01
+# define POINTS_DEFAULT 300
+# define FACTOR_DEFAULT 2.0
+
+# define WINDOW_WIDTH 640
+# define WINDOW_HEIGHT 480
+
+#endif
diff --git a/graphics.c b/graphics.c
new file mode 100644
index 0000000..2d69c82
--- /dev/null
+++ b/graphics.c
@@ -0,0 +1,148 @@
+#include "graphics.h"
+
+#define WINDOW_TITLE "Cardioid"
+
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+
+#ifdef CARDIOID_RELEASE
+# define SDL_CALL(x) x
+#else
+# define SDL_CALL(x) \
+ SDL_ClearError(); \
+ x; \
+ if (*SDL_GetError() != '\0') { \
+ SDL_Log("[ERROR SDL] %s at %s:%d\n\t"#x, \
+ SDL_GetError(), __FILE__, __LINE__); \
+ exit(EXIT_FAILURE); \
+}
+#endif
+
+static void update(t_state *state);
+static void event_handler(t_state *state);
+static void get_point(t_state *state, SDL_Point* dst, double index);
+static void update_window_value(t_state *state, int width, int height);
+
+void graphics_init(t_state *state, int width, int height)
+{
+ SDL_CALL(SDL_Init(SDL_INIT_VIDEO));
+ SDL_CALL(state->window = SDL_CreateWindow(
+ WINDOW_TITLE,
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ width,
+ height,
+ SDL_WINDOW_RESIZABLE
+ ));
+ SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0));
+ // looks less blocky, add color to draw with previous color.
+ // The more lines are drawn to the same place the brighter it is.
+ SDL_CALL(SDL_SetRenderDrawBlendMode(state->renderer, SDL_BLENDMODE_ADD));
+ state->running = true;
+ state->points_num = POINTS_DEFAULT;
+ state->factor = FACTOR_DEFAULT;
+ update_window_value(state, width, height);
+}
+
+void graphics_quit(t_state *state)
+{
+ SDL_DestroyRenderer(state->renderer);
+ SDL_DestroyWindow(state->window);
+ SDL_Quit();
+}
+
+void graphics_run(t_state *state)
+{
+ while (state->running)
+ {
+ event_handler(state);
+ update(state);
+ SDL_Delay(2);
+ }
+}
+
+static void update(t_state *state)
+{
+ SDL_Point a;
+ SDL_Point b;
+
+ SDL_CALL(SDL_SetRenderDrawColor(state->renderer, 0, 0, 0, 255));
+ SDL_CALL(SDL_RenderClear(state->renderer));
+
+ SDL_CALL(SDL_SetRenderDrawColor(
+ state->renderer,
+ (LINE_COLOR >> 24) & 0xff,
+ (LINE_COLOR >> 16) & 0xff,
+ (LINE_COLOR >> 8 ) & 0xff,
+ (LINE_COLOR >> 0 ) & 0xff
+ ));
+ for (double i = 0.0; i < state->points_num; i += 1.0)
+ {
+ get_point(state, &a, i);
+ get_point(state, &b, i * state->factor);
+ SDL_CALL(SDL_RenderDrawLine(state->renderer, a.x, a.y, b.x, b.y));
+ }
+
+ SDL_CALL(SDL_RenderPresent(state->renderer));
+}
+
+static void get_point(t_state *state, SDL_Point* dst, double index)
+{
+ double index_rem = fmod(index, state->points_num);
+ double radian = M_PI_2 + 2.0 * M_PI * (index_rem / state->points_num);
+ dst->x = cos(radian) * state->radius + state->center.x;
+ dst->y = sin(radian) * state->radius + state->center.y;
+}
+
+static void update_window_value(t_state *state, int width, int height)
+{
+ state->center.x = width / 2.0;
+ state->center.y = height / 2.0;
+ state->radius = MIN(width, height) / 2.0 - 10.0;
+}
+
+static void event_handler(t_state *state)
+{
+ SDL_Event e;
+
+ while (SDL_PollEvent(&e))
+ {
+ switch (e.type)
+ {
+ case SDL_QUIT:
+ state->running = false;
+ break;
+
+ case SDL_KEYDOWN:
+ switch (e.key.keysym.sym)
+ {
+ case SDLK_j:
+ case SDLK_DOWN:
+ if (state->points_num < 3.0)
+ state->points_num = 0.0;
+ else
+ state->points_num -= 3.0;
+ break;
+ case SDLK_k:
+ case SDLK_UP:
+ state->points_num += 3.0;
+ break;
+ case SDLK_u:
+ case SDLK_LEFT:
+ if (state->factor < 0.05)
+ state->factor = 0.0;
+ else
+ state->factor -= 0.05;
+ break;
+ case SDLK_i:
+ case SDLK_RIGHT:
+ state->factor += 0.1;
+ break;
+ }
+ break;
+
+ case SDL_WINDOWEVENT:
+ if (e.window.event == SDL_WINDOWEVENT_RESIZED)
+ update_window_value(state, e.window.data1, e.window.data2);
+ }
+ }
+}
diff --git a/inc/graphics.h b/graphics.h
index bb7e491..ca21a98 100644
--- a/inc/graphics.h
+++ b/graphics.h
@@ -3,18 +3,24 @@
# include <stdbool.h>
# include <SDL2/SDL.h>
-
-# include "cardioid.h"
+# include "config.h"
typedef struct
{
SDL_Window *window;
SDL_Renderer *renderer;
- t_cardioid *cardioid;
+ double factor;
+ double points_num;
+ struct
+ {
+ double x;
+ double y;
+ } center;
+ double radius;
bool running;
} t_state;
-void graphics_init(t_state *state, int width, int height, t_cardioid *cardioid);
+void graphics_init(t_state *state, int width, int height);//, t_cardioid *cardioid);
void graphics_quit(t_state *state);
void graphics_run(t_state *state);
diff --git a/inc/cardioid.h b/inc/cardioid.h
deleted file mode 100644
index 23ba64a..0000000
--- a/inc/cardioid.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef CARDIOID_H
-# define CARDIOID_H
-
-# include <stddef.h>
-# include <SDL2/SDL.h>
-
-typedef struct
-{
- size_t points_num;
- SDL_Point *points;
- SDL_Point center;
- size_t radius;
- // double multiplication;
-} t_cardioid;
-
-# define MIN(x, y) ((x) < (y) ? (x) : (y))
-
-# define CARDIOID_ADD_POINTS(cardioid, n) cardioid_update_points((cardioid), (cardioid)->points_num + (n))
-# define CARDIOID_SUB_POINTS(cardioid, n) \
- cardioid_update_points((cardioid), (cardioid)->points_num < (n) ? 0 : (cardioid)->points_num - (n))
-
-int cardioid_init(t_cardioid *cardioid, size_t points_num, int width, int height);
-int cardioid_update_window(t_cardioid *cardioid, int width, int height);
-int cardioid_update_points(t_cardioid *cardioid, size_t points_num);
-void cardioid_quit(t_cardioid *cardioid);
-
-#endif
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..fd2da09
--- /dev/null
+++ b/main.c
@@ -0,0 +1,25 @@
+#include "graphics.h"
+#include "config.h"
+
+
+int main()
+{
+ t_state state;
+ /* t_cardioid cardioid; */
+
+ /* if (cardioid_init(&cardioid, 10, WINDOW_WIDTH, WINDOW_HEIGHT) == -1) */
+ /* { */
+ /* fprintf(stderr, "[ERROR] couldn't initialize caridoid\n"); */
+ /* return (1); */
+ /* } */
+ /* for (size_t i = 0; i < 10; i++) */
+ /* { */
+ /* printf("%d %d\n", cardioid.points[i].x, cardioid.points[i].y); */
+ /* } */
+ graphics_init(&state, WINDOW_WIDTH, WINDOW_HEIGHT);//, &cardioid);
+ graphics_run(&state);
+ graphics_quit(&state);
+ /* cardioid_quit(&cardioid); */
+
+ return 0;
+}
diff --git a/src/cardioid.c b/src/cardioid.c
deleted file mode 100644
index 891de8d..0000000
--- a/src/cardioid.c
+++ /dev/null
@@ -1,45 +0,0 @@
-#include "cardioid.h"
-
-int cardioid_init(t_cardioid *cardioid, size_t points_num, int width, int height)
-{
- cardioid->points = NULL;
- cardioid->points_num = points_num;
- return (cardioid_update_window(cardioid, width, height));
-}
-
-int cardioid_update_window(t_cardioid *cardioid, int width, int height)
-{
- cardioid->center.x = width / 2;
- cardioid->center.y = height / 2;
- cardioid->radius = MIN(width, height) / 2 - 10;
- printf("%d %d, %lu\n", cardioid->center.x,
- cardioid->center.y,
- cardioid->radius);
-
- return (cardioid_update_points(cardioid, cardioid->points_num));
-}
-
-int cardioid_update_points(t_cardioid *cardioid, size_t points_num)
-{
- double radian_step;
- double radian_pos;
-
- cardioid->points = realloc(cardioid->points, sizeof(SDL_Point) * points_num);
- if (cardioid->points == NULL)
- return (-1);
- cardioid->points_num = points_num;
- radian_pos = 0;
- radian_step = (2 * M_PI) / points_num;
- for (size_t i = 0; i < points_num; i++)
- {
- cardioid->points[i].x = cos(radian_pos) * (double)cardioid->radius + (double)cardioid->center.x;
- cardioid->points[i].y = sin(radian_pos) * (double)cardioid->radius + (double)cardioid->center.y;
- radian_pos += radian_step;
- }
- return (0);
-}
-
-void cardioid_quit(t_cardioid *cardioid)
-{
- free(cardioid->points);
-}
diff --git a/src/graphics.c b/src/graphics.c
deleted file mode 100644
index 2c5ef7e..0000000
--- a/src/graphics.c
+++ /dev/null
@@ -1,104 +0,0 @@
-#include "graphics.h"
-
-#define WINDOW_TITLE "Cardioid"
-
-static const char *g_sdl_error_str;
-
-#ifdef CARDIOID_RELEASE
-# define SDL_CALL(x) x
-#else
-# define SDL_CALL(x) \
- SDL_ClearError(); \
- x; \
- g_sdl_error_str = SDL_GetError(); \
- if (*g_sdl_error_str != '\0') { \
- SDL_Log("[ERROR SDL] %s at %s:%d\n\t"#x, \
- g_sdl_error_str, __FILE__, __LINE__); \
- exit(EXIT_FAILURE); \
-}
-#endif
-
-
-static void update(t_state *state);
-static void event_handler(t_state *state);
-static void draw_lines(t_state *state);
-
-void graphics_init(t_state *state, int width, int height, t_cardioid *cardioid)
-{
- SDL_CALL(SDL_Init(SDL_INIT_VIDEO));
- SDL_CALL(state->window = SDL_CreateWindow(
- WINDOW_TITLE,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- width,
- height,
- 0
- ));
- SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0));
- state->cardioid = cardioid;
- state->running = true;
-}
-
-void graphics_quit(t_state *state)
-{
- SDL_DestroyRenderer(state->renderer);
- SDL_DestroyWindow(state->window);
- SDL_Quit();
-}
-
-void graphics_run(t_state *state)
-{
- while (state->running)
- {
- event_handler(state);
- update(state);
- SDL_Delay(3);
- }
-}
-
- static
-void update(t_state *state)
-{
- SDL_CALL(SDL_SetRenderDrawColor(state->renderer, 0, 0, 0, 255));
- SDL_CALL(SDL_RenderClear(state->renderer));
- SDL_CALL(SDL_SetRenderDrawColor(state->renderer, 200, 150, 150, 255));
- SDL_CALL(SDL_RenderDrawPoints(state->renderer, state->cardioid->points, state->cardioid->points_num));
- draw_lines(state);
- SDL_CALL(SDL_RenderPresent(state->renderer));
-}
-
- static
-void draw_lines(t_state *state)
-{
- SDL_Point *points = state->cardioid->points;
- SDL_Point *paired;
-
- for (size_t i = 0; i < state->cardioid->points_num; i++)
- {
- paired = &points[i * 10 % state->cardioid->points_num];
- SDL_CALL(SDL_RenderDrawLine(state->renderer, points[i].x, points[i].y, paired->x, paired->y));
- }
-}
-
- static
-void event_handler(t_state *state)
-{
- SDL_Event e;
-
- while (SDL_PollEvent(&e))
- {
- switch (e.type)
- {
- case SDL_QUIT:
- state->running = false;
- break;
- case SDL_KEYDOWN:
- switch (e.key.keysym.sym)
- {
- case SDLK_j: CARDIOID_SUB_POINTS(state->cardioid, 3); break;
- case SDLK_k: CARDIOID_ADD_POINTS(state->cardioid, 3); break;
- }
- break;
- }
- }
-}
diff --git a/src/main.c b/src/main.c
deleted file mode 100644
index 341bfc1..0000000
--- a/src/main.c
+++ /dev/null
@@ -1,26 +0,0 @@
-#include "graphics.h"
-
-#define WINDOW_WIDTH 640
-#define WINDOW_HEIGHT 480
-
-int main()
-{
- t_state state;
- t_cardioid cardioid;
-
- if (cardioid_init(&cardioid, 10, WINDOW_WIDTH, WINDOW_HEIGHT) == -1)
- {
- fprintf(stderr, "[ERROR] couldn't initialize caridoid\n");
- return (1);
- }
- for (size_t i = 0; i < 10; i++)
- {
- printf("%d %d\n", cardioid.points[i].x, cardioid.points[i].y);
- }
- graphics_init(&state, WINDOW_WIDTH, WINDOW_HEIGHT, &cardioid);
- graphics_run(&state);
- graphics_quit(&state);
- cardioid_quit(&cardioid);
-
- return 0;
-}