#include #include #include #include #include "mandel.h" 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; } int w, h; SDL_GetWindowSize(window, &w, &h); Mandel *ctx = mandel_init(w, h); Params par = mandel_default_params(); uint8_t *imgdata = new uint8_t[3 * w * h]; mandel_render(imgdata, ctx, &par); SDL_Surface *mandelsurf = SDL_CreateRGBSurfaceFrom(imgdata, w, h, 24, 3 * w, 0x0000000ff, 0x0000ff00, 0x00ff0000, 0); SDL_Surface *winsurf = SDL_GetWindowSurface(window); SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr); SDL_UpdateWindowSurface(window); while (true) { const double min_img_size = min(par.imgw, mandel_imgh(ctx, &par)); const double scroll_dist = min_img_size * 0.1; 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()); } SDL_Event e; if (SDL_WaitEvent(&e) == 0) { cerr << "SDL event wait error: " << SDL_GetError() << endl; break; } winsurf = SDL_GetWindowSurface(window); switch (e.type) { case SDL_QUIT: break; 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_RIGHT: par.cx += scroll_dist; need_redraw = true; break; 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_PAGEUP: par.maxit += 128; need_redraw = true; break; case SDLK_PAGEDOWN: if (par.maxit > 256) { par.maxit -= 128; need_redraw = true; } break; case SDLK_q: goto cleanup; } break; case SDL_WINDOWEVENT: switch (e.window.event) { case SDL_WINDOWEVENT_FOCUS_GAINED: SDL_UpdateWindowSurface(window); break; 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; } 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); if (SDL_MUSTLOCK(winsurf)) SDL_LockSurface(winsurf); SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr); if (SDL_MUSTLOCK(winsurf)) SDL_UnlockSurface(winsurf); SDL_UpdateWindowSurface(window); } } cleanup: SDL_DestroyWindow(window); SDL_Quit(); } #endif