diff options
author | Arjun Satarkar <me@arjunsatarkar.net> | 2023-06-01 13:31:24 +0000 |
---|---|---|
committer | Arjun Satarkar <me@arjunsatarkar.net> | 2023-06-01 13:33:56 +0000 |
commit | 3a2fef94230f35d948634ffef83660afdffa0bad (patch) | |
tree | e4fb261e6fcf6d0b4b3d4b688aa94757700f45ab /lib/hitomezashi.c | |
parent | 316382d74ed4a705c43e2380e7af20a500c24fa9 (diff) | |
download | hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.tar hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.tar.gz hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.zip |
Complete libhitomezashi, reset project
Diffstat (limited to 'lib/hitomezashi.c')
-rw-r--r-- | lib/hitomezashi.c | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/lib/hitomezashi.c b/lib/hitomezashi.c deleted file mode 100644 index 5a3002d..0000000 --- a/lib/hitomezashi.c +++ /dev/null @@ -1,83 +0,0 @@ -#include "hitomezashi.h" -#include "SDL2/SDL.h" -#include <assert.h> - -SDL_Color HITOMEZASHI_FG_COLOUR = {.r = 0, .g = 0, .b = 0}; -SDL_Color HITOMEZASHI_BG_COLOUR = {.r = 255, .g = 255, .b = 255}; - -enum Hitomezashi_State_Init_Result -hitomezashi_state_init(struct Hitomezashi_State *state, int x_pattern_len, - int y_pattern_len, const char *x_pattern, - const char *y_pattern, int gap, int line_thickness, - Uint32 fg_colour, Uint32 bg_colour) { - assert(x_pattern_len >= 0); - assert(y_pattern_len >= 0); - assert(gap >= 0); - assert(line_thickness > 0); - - state->y_pattern_len = y_pattern_len; - state->x_pattern_len = x_pattern_len; - state->x_pattern = x_pattern; - state->y_pattern = y_pattern; - state->gap = gap; - state->line_thickness = line_thickness; - state->output_width = x_pattern_len * gap; - state->output_height = y_pattern_len * gap; - state->fg_colour = fg_colour; - state->bg_colour = bg_colour; - - state->surface = SDL_CreateRGBSurface(0, state->output_width, - state->output_height, 32, 0, 0, 0, 0); - if (!state->surface) { - return Hitomezashi_State_Init_Result_Err_Create_Surface; - } - return Hitomezashi_State_Init_Result_Success; -} - -enum Hitomezashi_Draw_Result hitomezashi_draw(struct Hitomezashi_State *state) { - if (SDL_LockSurface(state->surface) != 0) { - return Hitomezashi_Draw_Result_Err_Lock_Surface; - } - - SDL_FillRect(state->surface, NULL, state->bg_colour); - - SDL_Rect rect; - // Draw y pattern (horizontal) lines - for (int i = 0; i < state->y_pattern_len; ++i) { - for (int j = 0; j < state->x_pattern_len; ++j) { - if (j % 2 == state->y_pattern[i]) { - rect.x = j * state->gap; - rect.y = i * state->gap; - rect.w = state->gap; - rect.h = state->line_thickness; - SDL_FillRect(state->surface, &rect, state->fg_colour); - } - } - } - // Draw x pattern (vertical) lines - for (int i = 0; i < state->x_pattern_len; ++i) { - for (int j = 0; j < state->y_pattern_len; ++j) { - if (j % 2 == state->x_pattern[i]) { - rect.x = i * state->gap; - rect.y = j * state->gap; - rect.w = state->line_thickness; - rect.h = state->gap; - SDL_FillRect(state->surface, &rect, state->fg_colour); - } - } - } - // Join up the lines to avoid leaving holes at the intersections - // To understand how this works, try commenting it out and see the output - for (int x = state->gap; x < state->output_width; x += state->gap) { - for (int y = state->gap; y < state->output_height; y += state->gap) { - rect.x = x; - rect.y = y; - rect.w = state->line_thickness; - rect.h = state->line_thickness; - SDL_FillRect(state->surface, &rect, state->fg_colour); - } - } - - SDL_UnlockSurface(state->surface); - return Hitomezashi_Draw_Result_Success; -} |