From 6878d573e50cca6ad917b67d596e5bb38c93b42a Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 5 May 2020 16:47:42 +0200 Subject: Subsampling for faster image --- sdl.cpp | 238 ++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 133 insertions(+), 105 deletions(-) (limited to 'sdl.cpp') diff --git a/sdl.cpp b/sdl.cpp index dcc2bb9..ea2f0f9 100644 --- a/sdl.cpp +++ b/sdl.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "mandel.h" @@ -8,111 +9,119 @@ using namespace std; #if 1 -int main() { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - cerr << "SDL init error: " << SDL_GetError() << endl; - return 1; - } - - SDL_Window *window = SDL_CreateWindow("Mandel", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 960, 640, SDL_WINDOW_SHOWN); - if (!window) { - cerr << "SDL window creation error: " << SDL_GetError() << endl; - return 1; - } +struct State { + SDL_Window *window = nullptr; - int w, h; - SDL_GetWindowSize(window, &w, &h); - Mandel *ctx = mandel_init(w, h); + int w = -1, h = -1; + Mandel *ctx = nullptr; Params par = mandel_default_params(); - uint8_t *imgdata = new uint8_t[3 * w * h]; - mandel_render(imgdata, ctx, &par); + uint8_t *imgdata = nullptr; - SDL_Surface *mandelsurf = SDL_CreateRGBSurfaceFrom(imgdata, w, h, 24, 3 * w, 0x0000000ff, 0x0000ff00, 0x00ff0000, 0); + SDL_Surface *mandelsurf = nullptr; - SDL_Surface *winsurf = SDL_GetWindowSurface(window); + size_t current_resolution = 4; - SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr); + State() { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + cerr << "SDL init error: " << SDL_GetError() << endl; + exit(1); + } - SDL_UpdateWindowSurface(window); + window = SDL_CreateWindow("Mandel", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 960, 640, SDL_WINDOW_SHOWN); + if (!window) { + cerr << "SDL window creation error: " << SDL_GetError() << endl; + exit(1); + } - while (true) { - const double min_img_size = min(par.imgw, mandel_imgh(ctx, &par)); - const double scroll_dist = min_img_size * 0.1; + handle_resize(); + mandel_render(imgdata, ctx, &par, current_resolution); - bool need_redraw = false; - - { - stringstream ss; - ss << "Mandel " - << setprecision(17) - << " x=" << par.cx - << " y=" << par.cy - << " w=" << par.imgw - << " i=" << par.maxit; - SDL_SetWindowTitle(window, ss.str().data()); - } + blit(); + SDL_UpdateWindowSurface(window); + } + void blit() { + SDL_Surface *winsurf = SDL_GetWindowSurface(window); + if (SDL_MUSTLOCK(winsurf)) SDL_LockSurface(winsurf); + SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr); + if (SDL_MUSTLOCK(winsurf)) SDL_UnlockSurface(winsurf); + } + + void update_title() { + stringstream ss; + ss << "Mandel " + << setprecision(17) + << " x=" << par.cx + << " y=" << par.cy + << " w=" << par.imgw + << " i=" << par.maxit; + SDL_SetWindowTitle(window, ss.str().data()); + } + + SDL_Event wait_event() const { SDL_Event e; if (SDL_WaitEvent(&e) == 0) { cerr << "SDL event wait error: " << SDL_GetError() << endl; - break; + exit(1); } + return e; + } - winsurf = SDL_GetWindowSurface(window); + optional poll_event() const { + SDL_Event e; + if (SDL_PollEvent(&e) == 0) { + return nullopt; + } + return {e}; + } + + void handle_resize() { + if (ctx) mandel_free(ctx); + if (imgdata) delete[] imgdata; + if (mandelsurf) SDL_FreeSurface(mandelsurf); + + SDL_GetWindowSize(window, &w, &h); + cerr << "w=" << w << " h=" << h << endl; + imgdata = new uint8_t[3 * w * h]; + mandelsurf = SDL_CreateRGBSurfaceFrom(imgdata, w, h, 24, 3 * w, 255, 255 << 8, 255 << 16, 0); + ctx = mandel_init(w, h); + } + + enum class action { + quit, + redraw, + idle, + }; + + action handle_event(const SDL_Event &e) { + const double min_img_size = min(par.imgw, mandel_imgh(ctx, &par)); + const double scroll_dist = min_img_size * 0.1; switch (e.type) { case SDL_QUIT: - break; + return action::quit; case SDL_KEYDOWN: switch (e.key.keysym.sym) { - case SDLK_UP: - par.cy += scroll_dist; - need_redraw = true; - break; - - case SDLK_DOWN: - par.cy -= scroll_dist; - need_redraw = true; - break; + case SDLK_UP: par.cy += scroll_dist; return action::redraw; + case SDLK_DOWN: par.cy -= scroll_dist; return action::redraw; + case SDLK_RIGHT: par.cx += scroll_dist; return action::redraw; + case SDLK_LEFT: par.cx -= scroll_dist; return action::redraw; - case SDLK_RIGHT: - par.cx += scroll_dist; - need_redraw = true; - break; + case SDLK_KP_PLUS: case SDLK_PLUS: case SDLK_EQUALS: + par.imgw *= 0.8; return action::redraw; - case SDLK_LEFT: - par.cx -= scroll_dist; - need_redraw = true; - break; - - case SDLK_KP_PLUS: - case SDLK_PLUS: - case SDLK_EQUALS: - par.imgw *= 0.8; - need_redraw = true; - break; - - case SDLK_KP_MINUS: - case SDLK_MINUS: - par.imgw *= 1.25; - need_redraw = true; - break; + case SDLK_KP_MINUS: case SDLK_MINUS: + par.imgw *= 1.25; return action::redraw; case SDLK_PAGEUP: - par.maxit += 128; - need_redraw = true; - break; - + par.maxit += 128; return action::redraw; case SDLK_PAGEDOWN: - if (par.maxit > 256) { - par.maxit -= 128; - need_redraw = true; - } + if (par.maxit > 256) { par.maxit -= 128; return action::redraw; } break; case SDLK_q: - goto cleanup; + return action::quit; } break; @@ -120,43 +129,62 @@ int main() { switch (e.window.event) { case SDL_WINDOWEVENT_FOCUS_GAINED: SDL_UpdateWindowSurface(window); - break; + return action::idle; case SDL_WINDOWEVENT_SIZE_CHANGED: - mandel_free(ctx); - delete[] imgdata; - SDL_FreeSurface(mandelsurf); - - winsurf = SDL_GetWindowSurface(window); - SDL_GetWindowSize(window, &w, &h); - cerr << "w=" << w << " h=" << h << endl; - cerr << "surface: w=" << winsurf->w << " h=" << winsurf->h << endl; - imgdata = new uint8_t[3 * w * h]; - mandelsurf = SDL_CreateRGBSurfaceFrom(imgdata, w, h, 24, 3 * w, 0x0000000ff, 0x0000ff00, 0x00ff0000, 0); - ctx = mandel_init(w, h); - need_redraw = true; - break; + handle_resize(); + return action::redraw; } break; } - if (need_redraw) { - SDL_GetWindowSize(window, &w, &h); - cerr << "w=" << w << " h=" << h << endl; - cerr << "winsurf: w=" << winsurf->w << " h=" << winsurf->h << endl; - cerr << "mandelsurf: w=" << mandelsurf->w << " h=" << mandelsurf->h << endl; - mandel_render(imgdata, ctx, &par); + return action::idle; + } +}; - if (SDL_MUSTLOCK(winsurf)) SDL_LockSurface(winsurf); - SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr); - if (SDL_MUSTLOCK(winsurf)) SDL_UnlockSurface(winsurf); +int main() { + State state; - SDL_UpdateWindowSurface(window); + while (true) { + state.update_title(); + + State::action action; + if (state.current_resolution == 1) { + SDL_Event ev = state.wait_event(); + action = state.handle_event(ev); + } else { + if (optional optev = state.poll_event()) { + action = state.handle_event(*optev); + } else { + action = State::action::idle; + } } - } -cleanup: - SDL_DestroyWindow(window); - SDL_Quit(); + switch (action) { + case State::action::quit: + return 0; + + case State::action::redraw: + state.current_resolution = 4; + + SDL_GetWindowSize(state.window, &state.w, &state.h); + cerr << "w=" << state.w << " h=" << state.h << endl; + // cerr << "mandelsurf: w=" << state.mandelsurf->w << " h=" << state.mandelsurf->h << endl; + mandel_render(state.imgdata, state.ctx, &state.par, state.current_resolution); + + state.blit(); + SDL_UpdateWindowSurface(state.window); + break; + + case State::action::idle: + if (state.current_resolution > 1) { + state.current_resolution /= 2; + mandel_render(state.imgdata, state.ctx, &state.par, state.current_resolution); + } + state.blit(); + SDL_UpdateWindowSurface(state.window); + break; + } + } } #endif -- cgit v1.2.3-54-g00ecf