/* The GPLv3 License (GPLv3) Copyright (c) 2022 Akos Horvath This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "graphics.h" #include "mpreal.h" #include #include #include #include #include #include #include #include #include #include #include using namespace Mandelbrot; using namespace mpfr; Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title), from(Vec2mp(0.0, 0.0)), to(Vec2mp(0.0, 0.0)), m_start(Vec2i(0, 0)), m_end(Vec2i(0, 0)), mouseDown(false) { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { std::cout << "Could not initialize SDL. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } SDL_Window *window = SDL_CreateWindow(this->title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->w, this->h, 0); if (!window) { std::cout << "Could not create window. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->window = window; SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (!r) { std::cout << "Could not create renderer. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->renderer = r; SDL_Surface* screensurface = SDL_GetWindowSurface(this->window); if(!screensurface) { std::cout << "Could not create screen surface. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->screenSurface = screensurface; SDL_Surface* surface = SDL_CreateRGBSurface(0, this->w, this->h, 32, 0, 0, 0, 0); if(!surface) { std::cout << "Could not create surface. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->surface = surface; SDL_PixelFormat* format = this->surface->format; this->pformat = format; this->running = true; } Graphics::Graphics(int w, int h, std::string title, int tcount, int maxiter) : w(w), h(h), title(title), from(Vec2mp(0.0, 0.0)), to(Vec2mp(0.0, 0.0)), m_start(Vec2i(0, 0)), m_end(Vec2i(0, 0)), mouseDown(false), tcount(tcount), maxiter(maxiter) { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { std::cout << "Could not initialize SDL. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } SDL_Window *window = SDL_CreateWindow(this->title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->w, this->h, 0); if (!window) { std::cout << "Could not create window. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->window = window; SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (!r) { std::cout << "Could not create renderer. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->renderer = r; SDL_Surface* screensurface = SDL_GetWindowSurface(this->window); if(!screensurface) { std::cout << "Could not create screen surface. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->screenSurface = screensurface; SDL_Surface* surface = SDL_CreateRGBSurface(0, this->w, this->h, 32, 0, 0, 0, 0); if(!surface) { std::cout << "Could not create surface. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->surface = surface; SDL_PixelFormat* format = this->surface->format; this->pformat = format; this->running = true; } void Graphics::initFont(const std::string path, uint16_t size) { TTF_Init(); TTF_Font* font = TTF_OpenFont(path.c_str(), size); if(!font) { std::cout << "Could not load font. " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } this->font = font; } void Graphics::drawText(const std::string string) { SDL_Color textColor = {0, 0, 0, 255}; this->textSurface = TTF_RenderText_Solid(this->font, string.c_str(), textColor); SDL_BlitSurface(this->textSurface, NULL, this->surface, NULL); } void Graphics::drawPoint(const Vec2i &pos, const rgb &col) { SDL_RenderDrawPoint(this->renderer, pos.x, pos.y); } rgb hsv2rgb(hsv in) { double hh, p, q, t, ff; long i; rgb out; if(in.s <= 0.0) { // < is bogus, just shuts up warnings out.r = in.v; out.g = in.v; out.b = in.v; return out; } hh = in.h; if(hh >= 360.0) hh = 0.0; hh /= 60.0; i = (long)hh; ff = hh - i; p = in.v * (1.0 - in.s); q = in.v * (1.0 - (in.s * ff)); t = in.v * (1.0 - (in.s * (1.0 - ff))); switch(i) { case 0: out.r = in.v; out.g = t; out.b = p; break; case 1: out.r = q; out.g = in.v; out.b = p; break; case 2: out.r = p; out.g = in.v; out.b = t; break; case 3: out.r = p; out.g = q; out.b = in.v; break; case 4: out.r = t; out.g = p; out.b = in.v; break; case 5: default: out.r = in.v; out.g = p; out.b = q; break; } //std::cout << "ret" << out.g << out.b << out.b << std::endl; return out; } void Graphics::setPixel(uint16_t x, uint16_t y, uint32_t pixel) { uint32_t* const target_pixel = (uint32_t*) ((uint8_t*) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel); *target_pixel = pixel; } void Graphics::plot(Mandelbrotc const &m) { for (uint32_t i = 0; i < m.s.x; i++) { for (uint32_t j = 0; j < m.s.y; j++) { uint8_t c = 255 - (m.screen[i][j]*255.0/(double)m.max_iter); // SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); // SDL_RenderDrawPoint(renderer, (int)i, (int)j); uint32_t pixel = SDL_MapRGB(this->pformat, c, c, c); this->setPixel(i, j, pixel); // double iter = m.screen[i][j]; // if (i < m.s.x-1 && j < m.s.y-1) { // double iter1 = m.screen[i+1][j+1]; // hsv h; // h.h = 255*(double)iter/(double)m.max_iter; // h.s = 255; // if (iter < m. max_iter) // h.v = 255; // else // h.v = 0; // rgb r; // r = hsv2rgb(h); // hsv h1; // h1.h = 255*(double)iter1/(double)m.max_iter; // h1.s = 255; // if (iter1 < m. max_iter) // h1.v = 255; // else // h1.v = 0; // rgb r1; // r1 = hsv2rgb(h1); // uint8_t ri = linear_interpolate(r.r, r1.r, (int)iter % 1); // uint8_t gi = linear_interpolate(r.g, r1.g, (int)iter % 1); // uint8_t bi = linear_interpolate(r.b, r1.b, (int)iter % 1); // SDL_SetRenderDrawColor(this->renderer, ri, gi, bi, 255); // SDL_RenderDrawPoint(renderer, (int)i, (int)j); // } else { // hsv h; // h.h = 255*(double)iter/(double)m.max_iter; // h.s = 255; // if (iter < m. max_iter) // h.v = 255; // else // h.v = 0; // rgb r; // r = hsv2rgb(h); // SDL_SetRenderDrawColor(this->renderer, r.r, r.g, r.b, 255); // SDL_RenderDrawPoint(renderer, (int)i, (int)j); // } } } } void Graphics::drawSelectionRectangle() { uint32_t pixel = SDL_MapRGB(this->pformat, 0, 0, 0); if (this->mouseDown) { if (m_start.x < m_end.x) { for (int i = m_start.x; i < m_end.x; i++) { this->setPixel(i, m_start.y, pixel); this->setPixel(i, m_end.y, pixel); } } else { for (int i = m_end.x; i < m_start.x; i++) { this->setPixel(i, m_start.y, pixel); this->setPixel(i, m_end.y, pixel); } } if (m_start.y < m_end.y) { for (int i = m_start.y; i < m_end.y; i++) { this->setPixel(m_start.x, i, pixel); this->setPixel(m_end.x, i, pixel); } } else { for (int i = m_end.y; i < m_start.y; i++) { this->setPixel(m_start.x, i, pixel); this->setPixel(m_end.x, i, pixel); } } } } void Graphics::zoom(Mandelbrotc &m) { if (abs(m_end.x-m_start.x) < 10 || abs(m_end.y-m_start.y) < 10) return; mpreal s_x = (mpfr::abs(m.f.x - m.t.x) * ((double)this->m_start.x / (double)this->w)) + m.f.x; mpreal s_y = (mpfr::abs(m.f.y - m.t.y) * ((double)this->m_start.y / (double)this->h)) + m.f.y; mpreal e_x = (mpfr::abs(m.f.x - m.t.x) * ((double)this->m_end.x / (double)this->w)) + m.f.x; mpreal e_y = (mpfr::abs(m.f.y - m.t.y) * ((double)this->m_end.y / (double)this->h)) + m.f.y; m.f = Vec2mp(s_x, s_y); m.t = Vec2mp(e_x, e_y); } void Graphics::mainLoop() { this->initFont("NotoSansMono-Regular.ttf", 16); mpreal fx = mpreal(-2); mpreal fy = mpreal(-1); mpreal tx = mpreal(1); mpreal ty = mpreal(1); this->from = Vec2mp(fx, fy); this->to = Vec2mp(tx, ty); Vec2i m_start = Vec2i(0, 0); Vec2i m_end = Vec2i(0, 0); std::chrono::time_point start, end; std::chrono::duration elapsed_seconds; Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), maxiter); m.thread_count = this->tcount; m.start_threads(true); while (this->running) { start = std::chrono::system_clock::now(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: running = false; break; case SDL_MOUSEWHEEL: if (event.wheel.y > 0) { std::cout << "wheel" << std::endl; this->from.x += 0.2; this->to.x -= 0.2; this->from.y += 0.2; this->to.y -= 0.2; } if (event.wheel.y < 0) { std::cout << "wheel" << std::endl; this->from.x -= 0.2; this->to.x += 0.2; this->from.y -= 0.2; this->to.y += 0.2; } break; case SDL_MOUSEBUTTONDOWN: if (this->mouseDown) break; this->mouseDown = true; this->m_start.x = event.button.x; this->m_start.y = event.button.y; break; case SDL_MOUSEBUTTONUP: this->mouseDown = false; this->zoom(m); break; case SDL_MOUSEMOTION: this->m_end.x = event.motion.x; this->m_end.y = event.motion.y; break; case SDL_KEYUP: if (event.key.keysym.scancode == SDL_SCANCODE_R) { mpreal::set_default_prec(mpfr::digits2bits(20)); m.f.x = mpreal(-2.0); m.f.y = mpreal(-1.0); m.t.x = mpreal(1.0); m.t.y = mpreal(1.0); m.diff = mpreal(0.0001); } break; } } m.start_threads(false); this->plot(m); m.stop_threads(); this->drawSelectionRectangle(); this->drawText("from x:" + m.f.x.toString() + " y: " + m.f.y.toString() + "to x:" + m.t.x.toString() + " y: " + m.t.y.toString()); SDL_BlitSurface(this->surface, NULL, this->screenSurface, NULL); SDL_UpdateWindowSurface(this->window); end = std::chrono::system_clock::now(); elapsed_seconds = end - start; //std::cout << "FPS: " << 1 / elapsed_seconds.count() << std::endl; } m.stop_threads(); }