diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-10 16:48:37 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-10 16:48:37 +0200 |
| commit | f23764a0ec0b946d9d0c0dd1a2537c0d22849086 (patch) | |
| tree | 81d0b4dc68e4f34d6e950d0f28f58ca5d3d1e07f /src | |
| parent | 6d9c284a24555a7df0b37661ff3c5491d6d0449a (diff) | |
| download | cardioid-f23764a0ec0b946d9d0c0dd1a2537c0d22849086.tar.gz cardioid-f23764a0ec0b946d9d0c0dd1a2537c0d22849086.tar.bz2 cardioid-f23764a0ec0b946d9d0c0dd1a2537c0d22849086.zip | |
Dynamic factor, everything to double, removed cardioid struct, window resize
Diffstat (limited to 'src')
| -rw-r--r-- | src/cardioid.c | 45 | ||||
| -rw-r--r-- | src/graphics.c | 104 | ||||
| -rw-r--r-- | src/main.c | 26 |
3 files changed, 0 insertions, 175 deletions
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; -} |
