You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.5 KiB
C

#include <SDL2/SDL.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/time.h>
#define VENTANA_ANCHO 800
#define VENTANA_ALTO 600
#define ANCHO VENTANA_ANCHO
#define ALTO VENTANA_ALTO
#define MAX_ITER 500
#define JUEGO_FPS 10
static double time_in_ms(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
}
//long double xc = - 1.7400623825793398724575; // Derecha: Achicar
//long double yc = 0.0281753397792109; // Abajo: Achicar
//long double xc = -1.740119020442992652289958266376L;
//long double yc = 0.028019209975637676263081582255L;
long double xc = 0.264226442163892496529931097626331393L, yc = 0.002572261418806922467217290773078275;
uint32_t computar_intensidad(long double x0, long double y0, long double zoom) {
//x0 = x0 * zoom - 1.7400623825793398724575; // Derecha: Achicar
//y0 = y0 * zoom + 0.0281753397792111; // Abajo: Achicar
x0 = x0 * zoom + xc; // Derecha: Achicar
y0 = y0 * zoom + yc; // Abajo: Achicar
//x0 = x0 * zoom - 1.78; // Derecha: Achicar
//y0 = y0 * zoom + 0.0; // Abajo: Achicar
long double x = 0;
long double y = 0;
int n = 0;
while(x*x + y*y <= 4 && n < MAX_ITER) {
long double xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
n++;
}
if(n == MAX_ITER)
return 0;
//float h = (n + 1 - log(log2(sqrt(x*x+y*y)))) / MAX_ITER * 360;
float h = (int)(n * 360.0 / 50) % 360;
float xxx = (1 - fabs(fmodf(h / 60.0, 2) - 1));
uint8_t xx = (xxx < 1 ? xxx : 1) * 255;
uint8_t r = 0, g = 0, b = 0;
if(h < 60)
r = 255, g = xx;
else if(h < 120)
g = 255, r = xx;
else if(h < 180)
g = 255, b = xx;
else if(h < 240)
b = 255, g = xx;
else if(h < 300)
b = 255, r = xx;
else
r = 255, b = xx;
return (r << 24) | (g << 16) | (b << 8);
}
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event event;
//SDL_CreateWindowAndRenderer(1000, 800, 0, &window, &renderer);
window = SDL_CreateWindow("Mandelbrot", 0, 0, ANCHO, ALTO, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// SDL_SetWindowTitle(window, "Mandelbrot");
int width, height;
SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, ANCHO, ALTO);
// SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, ANCHO, ALTO);
uint32_t canvas[ALTO * ANCHO];
int dormir = 0;
// BEGIN código del alumno
long double zoom = 0.179;
double factor = 0.99;
bool simulate = true;
// END código del alumno
unsigned int ticks = SDL_GetTicks();
while(1) {
if(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
break;
// BEGIN código del alumno
if (event.type == SDL_KEYDOWN) {
// Se apretó una tecla
switch(event.key.keysym.sym) {
case SDLK_UP:
yc += zoom*5;
break;
case SDLK_DOWN:
yc -= zoom*5;
break;
case SDLK_RIGHT:
xc += zoom*5;
break;
case SDLK_LEFT:
xc -= zoom*5;
break;
case SDLK_PLUS:
factor /= 1.01;
break;
case SDLK_MINUS:
factor *= 1.01;
break;
}
}
else if (event.type == SDL_KEYUP) {
// Se soltó una tecla
switch(event.key.keysym.sym) {
case SDLK_SPACE:
simulate = ! simulate;
case SDLK_UP:
break;
case SDLK_DOWN:
break;
case SDLK_RIGHT:
break;
case SDLK_LEFT:
break;
}
}
// END código del alumno
continue;
}
// BEGIN código del alumno
double begin = time_in_ms();
int i = 0;
if(zoom < 1e-20)
zoom = 0.179;
if(simulate)
zoom *= factor;
for(int vy = ALTO / 2; vy > - ALTO / 2; vy--)
for(int vx = - ANCHO / 2; vx < ANCHO / 2; vx++) {
canvas[i++] = computar_intensidad(vx, vy, zoom);
}
printf("%f %.5Le %.36Lf %.36Lf\n", time_in_ms() - begin, zoom, xc, yc);
// Implementar imagen_a_textura() cuanto antes!
// END código del alumno
SDL_UpdateTexture(texture, NULL, canvas, ANCHO * sizeof(uint32_t));
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
ticks = SDL_GetTicks() - ticks;
if(dormir) {
SDL_Delay(dormir);
dormir = 0;
}
else if(ticks < 1000 / JUEGO_FPS)
SDL_Delay(1000 / JUEGO_FPS - ticks);
else
printf("Perdiendo cuadros\n");
ticks = SDL_GetTicks();
}
// BEGIN código del alumno
// No tengo nada que destruir.
// END código del alumno
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}