From 6d9c284a24555a7df0b37661ff3c5491d6d0449a Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 10 May 2020 14:08:07 +0200 Subject: First primitive cardioid drawing --- src/cardioid.c | 45 +++++++++++++++++++++++++ src/grahpics.c | 71 --------------------------------------- src/graphics.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 18 ++++++++-- 4 files changed, 165 insertions(+), 73 deletions(-) create mode 100644 src/cardioid.c delete mode 100644 src/grahpics.c create mode 100644 src/graphics.c (limited to 'src') diff --git a/src/cardioid.c b/src/cardioid.c new file mode 100644 index 0000000..891de8d --- /dev/null +++ b/src/cardioid.c @@ -0,0 +1,45 @@ +#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/grahpics.c b/src/grahpics.c deleted file mode 100644 index ae9409d..0000000 --- a/src/grahpics.c +++ /dev/null @@ -1,71 +0,0 @@ -#include "graphics.h" - -#define WINDOW_TITLE "Title" - -static const char *g_sdl_error_str; - -#define SDL_CALL(x) \ - SDL_ClearError(); \ - x; \ - g_sdl_error_str = SDL_GetError(); \ - if (*g_sdl_error_str != '\0') { \ - SDL_Log("ERROR SDL: %s", g_sdl_error_str); \ - exit(EXIT_FAILURE); \ - } - - -static void update(t_state *state); -static void event_handler(t_state *state); - -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, - 0 - )); - SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0)); - 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) -{ - // do stuff -} - -static -void event_handler(t_state *state) -{ - SDL_Event e; - while (SDL_PollEvent(&e)) - { - switch (e.type) - { - case SDL_QUIT: - state->running = false; - break; - } - } -} diff --git a/src/graphics.c b/src/graphics.c new file mode 100644 index 0000000..2c5ef7e --- /dev/null +++ b/src/graphics.c @@ -0,0 +1,104 @@ +#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 index 91c8dde..341bfc1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,26 @@ #include "graphics.h" +#define WINDOW_WIDTH 640 +#define WINDOW_HEIGHT 480 + int main() { - t_state state; + t_state state; + t_cardioid cardioid; - graphics_init(&state, 640, 480); + 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; } -- cgit