diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-19 16:46:13 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-19 18:19:31 +0200 |
| commit | b0998910dd974280b3c6f3f65e21bfd5859b117f (patch) | |
| tree | 5a7049ac6cf0d05370df3324b2bb4b591c2bac87 /src/state.c | |
| parent | 6a80b1b70ec069b051c0e31aafac6eb596e20261 (diff) | |
| download | mandelbrot-b0998910dd974280b3c6f3f65e21bfd5859b117f.tar.gz mandelbrot-b0998910dd974280b3c6f3f65e21bfd5859b117f.tar.bz2 mandelbrot-b0998910dd974280b3c6f3f65e21bfd5859b117f.zip | |
Basic explorer with window resize, iterations change, moving around
Diffstat (limited to 'src/state.c')
| -rw-r--r-- | src/state.c | 65 |
1 files changed, 61 insertions, 4 deletions
diff --git a/src/state.c b/src/state.c index ee68b77..395d590 100644 --- a/src/state.c +++ b/src/state.c @@ -10,10 +10,26 @@ bool state_init(State *state) SDL_WINDOWPOS_UNDEFINED, MANDEL_WINDOW_WIDTH, MANDEL_WINDOW_HEIGHT, - 0)); + SDL_WINDOW_RESIZABLE + )); SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0)); + SDL_CALL(state->texture = SDL_CreateTexture( + state->renderer, + SDL_PIXELFORMAT_RGB888, + SDL_TEXTUREACCESS_STREAMING, + MANDEL_WINDOW_WIDTH, + MANDEL_WINDOW_HEIGHT + )); + state->palette = color_palette_new(NULL, MANDEL_ITERATIONS); + if (state->palette == NULL) + return false; + state->iterations = MANDEL_ITERATIONS; + state->real_start = -2.0; + state->real_end = 2.0; + state->imag_start = -2.0; + state->imag_end = 2.0; state->running = true; - return state; + return true; } void state_run(State *state) @@ -21,13 +37,54 @@ void state_run(State *state) while (state->running) { event_handle(state); + + double real_step; + double imag_step; + double real; + double imag; + void *pixels; + int pitch; + int width; + int height; + + SDL_CALL(SDL_LockTexture(state->texture, NULL, &pixels, &pitch)); + SDL_CALL(SDL_QueryTexture(state->texture, NULL, NULL, &width, &height)); + + real_step = (state->real_end - state->real_start) / width; + imag_step = (state->imag_end - state->imag_start) / height; + imag = state->imag_start; + + uint32_t render_start_time = SDL_GetTicks(); + + for (int i = 0; i < height; i++) + { + real = state->real_start; + for (int j = 0; j < width; j++) + { + int n = mandelbrot(real, imag, state->iterations); + Color c = state->palette[n]; + ((Color*)pixels)[i * width + j] = c; + real += real_step; + } + imag += imag_step; + } + + uint32_t render_end_time = SDL_GetTicks(); + + printf("%ums\r", (render_end_time - render_start_time)); + fflush(stdout); + + SDL_UnlockTexture(state->texture); + SDL_CALL(SDL_RenderCopy(state->renderer, state->texture, NULL, NULL)); + SDL_RenderPresent(state->renderer); + SDL_Delay(3); } } void state_quit(State *state) { - /* free(state->palette); */ - /* SDL_FreeSurface(state->surface); */ + free(state->palette); + SDL_DestroyTexture(state->texture); SDL_DestroyRenderer(state->renderer); SDL_DestroyWindow(state->window); SDL_Quit(); |
