add font drawing and faster surface-based rendering, command-line options

This commit is contained in:
2022-07-19 20:42:24 +02:00
parent d4cc9853cf
commit 32c36e65c1
2 changed files with 229 additions and 104 deletions

View File

@ -18,8 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "graphics.h"
#include "utils.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#include <math.h>
#include <cstdint>
#include <cstdlib>
#include <iostream>
@ -33,13 +38,18 @@ Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title),
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)
{
if (!window) {
std::cout << "Could not create window. "
<< SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
@ -49,17 +59,126 @@ Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title),
SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!r)
{
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);
@ -124,88 +243,73 @@ rgb hsv2rgb(hsv in)
//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)
{
// std::vector<uint8_t> screen = std::vector<uint8_t>();
// std::vector<uint8_t> screen1 = std::vector<uint8_t>();
// #pragma omp parallel sections
// {
// #pragma omp section
// {
// for (int i = 0; i < this->w/2; i+=2) {
// for (int j = 0; j < this->h/2; j+=2) {
// mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x;
// mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y;
// // std::cout << "px: " << x.to_str() << std::endl;
// // std::cout << "py: " << y.to_str() << std::endl;
// uint32_t max_iter = 50;
// uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter);
// std::cout << "iter: " << iter << std::endl;
// uint8_t color = 255 - (int)(iter*255/max_iter);
// screen.push_back(color);
// SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
// }
// }
// }
// #pragma omp section
// {
// for (int i = 1; i < this->w/2; i+=2) {
// for (int j = 1; j < this->h/2; j+=2) {
// mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x;
// mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y;
// // std::cout << "px: " << x.to_str() << std::endl;
// // std::cout << "py: " << y.to_str() << std::endl;
// uint32_t max_iter = 50;
// uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter);
// std::cout << "iter: " << iter << std::endl;
// uint8_t color = 255 - (int)(iter*255/max_iter);
// screen1.push_back(color);
// SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
// }
// }
// }
// }
// for (double i = 1.0; i < this->w/2; i+=2) {
// for (double j = 1.0; j < this->h/2; j+=2) {
// uint8_t color = screen[i*h/2+j];
// SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
// }
// }
// for (double i = 0.0; i < this->w; i++) {
// for (double j = 0.0; j < this->h; j++) {
// mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x;
// mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y;
// uint32_t max_iter = 50;
// uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter);
// //std::cout << "iter: " << iter << std::endl;
// uint8_t color = 255 - (iter*255/max_iter);
// SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
// }
// }
for (uint32_t i = 0; i < m.s.x; i++) {
for (uint32_t j = 0; j < m.s.y; j++) {
//uint8_t color = 255 - (m.screen[i][j]*255/m.max_iter);
uint8_t iter = m.screen[i][j];
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);
//std::cout << "rgb" << r.r << r.g << r.b << std::endl;
SDL_SetRenderDrawColor(this->renderer, r.r, r.g, r.b, 255);
SDL_RenderDrawPoint(renderer, (int)i, (int)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);
// }
}
}
}
@ -213,28 +317,29 @@ void Graphics::plot(Mandelbrotc const &m)
void Graphics::drawSelectionRectangle()
{
uint32_t pixel = SDL_MapRGB(this->pformat, 0, 0, 0);
if (this->mouseDown) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
if (m_start.x < m_end.x) {
for (int i = m_start.x; i < m_end.x; i++) {
SDL_RenderDrawPoint(renderer, i, m_start.y);
SDL_RenderDrawPoint(renderer, i, m_end.y);
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++) {
SDL_RenderDrawPoint(renderer, i, m_start.y);
SDL_RenderDrawPoint(renderer, i, m_end.y);
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++) {
SDL_RenderDrawPoint(renderer, m_start.x, i);
SDL_RenderDrawPoint(renderer, m_end.x, 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++) {
SDL_RenderDrawPoint(renderer, m_start.x, i);
SDL_RenderDrawPoint(renderer, m_end.x, i);
this->setPixel(m_start.x, i, pixel);
this->setPixel(m_end.x, i, pixel);
}
}
}
@ -261,6 +366,8 @@ void Graphics::zoom(Mandelbrotc &m)
void Graphics::mainLoop()
{
this->initFont("NotoSansMono-Regular.ttf", 16);
mpreal fx = mpreal(-2);
mpreal fy = mpreal(-1);
mpreal tx = mpreal(1);
@ -270,13 +377,17 @@ void Graphics::mainLoop()
Vec2i m_start = Vec2i(0, 0);
Vec2i m_end = Vec2i(0, 0);
std::chrono::time_point<std::chrono::system_clock> start, end;
std::chrono::duration<double> elapsed_seconds;
Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), 100);
m.thread_count = 16;
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))
@ -319,11 +430,6 @@ void Graphics::mainLoop()
break;
}
}
// std::cout << "mainloop range " << this->from.x << std::endl;
// std::cout << "mainloop range " << this->from.y << std::endl;
// std::cout << "mainloop range " << this->to.x << std::endl;
// std::cout << "mainloop range " << this->to.y << std::endl;
m.start_threads(false);
this->plot(m);
@ -331,11 +437,16 @@ void Graphics::mainLoop()
this->drawSelectionRectangle();
SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
//SDL_Delay(10);
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();
}