Skip to content

Instantly share code, notes, and snippets.

@s-melnikov
Created March 31, 2019 19:37
Show Gist options
  • Save s-melnikov/3404fc56c925d4c1101cb6a77c15c4ad to your computer and use it in GitHub Desktop.
Save s-melnikov/3404fc56c925d4c1101cb6a77c15c4ad to your computer and use it in GitHub Desktop.
SDL2
#include <iostream>
#include <SDL.h>
#undef main
struct Point {
int x;
int y;
};
SDL_Window *window;
SDL_Renderer *renderer;
const int WIDTH = 1024;
const int HEIGHT = 768;
void change_color() {
SDL_SetRenderDrawColor(renderer, 155 + rand(), 155 + rand(), 155 + rand(), SDL_ALPHA_OPAQUE);
}
void line(Point a, Point b) {
change_color();
SDL_RenderDrawLine(renderer, a.x, a.y, b.x, b.y);
}
void triangle(Point a, Point b, Point c) {
line(a, b);
line(b, c);
line(a, c);
}
void fractal(Point a, Point b, Point c, int limit) {
if (limit > 0) {
Point _a = {
a.x + (b.x - a.x) / 2,
a.y - (a.y - b.y) / 2
};
Point _b = {
b.x + (c.x - b.x) / 2,
b.y - (b.y - c.y) / 2
};
Point _c = {
a.x + (c.x - a.x) / 2,
a.y
};
fractal(a, _a, _c, limit - 1);
fractal(_a, b, _b, limit - 1);
fractal(_c, _b, c, limit - 1);
} else {
triangle(a, b, c);
}
}
int main() {
bool done = false;
SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow(
"Fractals",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH,
HEIGHT,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer(window, -1, 0);
Point a = { 0, HEIGHT };
Point b = { WIDTH / 2, 0 };
Point c = { WIDTH, HEIGHT };
fractal(a, b, c, 6);
SDL_RenderPresent(renderer);
while (!done) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
done = true;
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment