Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- <one line to give the program's name and a brief idea of what it does.>
- Copyright (C) 2015 Bryan Palmer <interruptrequestlevel@gmail.com>
- 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 <http://www.gnu.org/licenses/>.
- */
- //cl /I SDL2\include /I SDL2_ttf\include /MD main.c /link SDL2\lib\x86\SDL2.lib SDL2\lib\x86\SDL2main.lib\SDL2_ttf\lib\x86\SDL2_ttf.lib /SUBSYSTEM:CONSOLE
- #include "SDL.h"
- #include "SDL_TTF.h"
- #define MANDLEBROT_MAX_ITER 1000
- #define INC_AMOUNT 1
- #define WIN_WIDTH 1280
- #define WIN_HEIGHT 780
- #define COLOR_THRES_R 0
- #define COLOR_THRES_G 2
- #define COLOR_THRES_B 16
- #define COLOR_FACTOR_R 255
- #define COLOR_FACTOR_G 16
- #define COLOR_FACTOR_B 10
- #define COLOR_STEP 2
- #define DEBUG_COLORS 0
- #define FULLSCREEN 0
- #define MAX_FRAMES 25 * 60
- #define RENDER_FMT "frames/frame%i.bmp"
- /* XPM */
- static const char *arrow[] = {
- /* width height num_colors chars_per_pixel */
- " 32 32 3 1",
- /* colors */
- "X c #000000",
- ". c #ffffff",
- " c None",
- /* pixels */
- "X ",
- "XX ",
- "X.X ",
- "X..X ",
- "X...X ",
- "X....X ",
- "X.....X ",
- "X......X ",
- "X.......X ",
- "X........X ",
- "X.....XXXXX ",
- "X..X..X ",
- "X.X X..X ",
- "XX X..X ",
- "X X..X ",
- " X..X ",
- " X..X ",
- " X..X ",
- " XX ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- "0,0"
- };
- static SDL_Cursor *init_system_cursor(const char *image[])
- {
- int i, row, col;
- Uint8 data[4*32];
- Uint8 mask[4*32];
- int hot_x, hot_y;
- i = -1;
- for (row=0; row<32; ++row) {
- for (col=0; col<32; ++col) {
- if (col % 8) {
- data[i] <<= 1;
- mask[i] <<= 1;
- } else {
- ++i;
- data[i] = mask[i] = 0;
- }
- switch (image[4+row][col]) {
- case 'X':
- data[i] |= 0x01;
- mask[i] |= 0x01;
- break;
- case '.':
- mask[i] |= 0x01;
- break;
- case ' ':
- break;
- }
- }
- }
- sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
- return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
- }
- int trip_vector = 0;
- int factorR = COLOR_FACTOR_R;
- int factorG = COLOR_FACTOR_G;
- int factorB = COLOR_FACTOR_B;
- void draw_mandlebrot(SDL_Renderer* renderer, int width, int height, double offsetX, double offsetY, double zoom) {
- int pixelY;
- int pixelX;
- int iter;
- int thresR = COLOR_THRES_R;
- int thresG = COLOR_THRES_G;
- int thresB = COLOR_THRES_B;
- for(pixelY = 0; pixelY < height; pixelY += INC_AMOUNT) {
- for(pixelX = 0; pixelX < width; pixelX += INC_AMOUNT) {
- double x0 = (((double)pixelX * 3.5) / (width * zoom)) - (offsetX / zoom);
- double y0 = (((double)pixelY * 2.0) / (height * zoom)) - (offsetY / zoom);
- double x = 0;
- double y = 0;
- int max_iter = MANDLEBROT_MAX_ITER;
- //for(iter = 0;(x*x + y*y < 4.0) && iter < max_iter; iter++) {
- for(iter = 0;(x*x + y*y < 4.0) && iter < max_iter; iter++) {
- double xtemp = x*x - y*y + x0;
- y = 2 * x * y + y0;
- x = xtemp;
- }
- /*SDL_Color clrFg = {255,255,255,0};
- SDL_Surface *sText = TTF_RenderText_Solid(font, "Test", clrFg);
- SDL_Texture *txtTexture = SDL_CreateTextureFromSurface(renderer, sText);
- SDL_Rect dstRect;
- dstRect.x = 10;
- dstRect.y = 10;
- dstRect.w = 100;
- dstRect.h = 20;*/
- //SDL_SetRenderDrawColor(renderer, (pixelY % 255) * 255, (pixelX % 255) * 255, 0, 255);
- SDL_SetRenderDrawColor(renderer,
- (iter > 999)?0:((iter > thresR)?(unsigned char)(iter * factorR):0),
- (iter > 999)?0:((iter > thresG)?(unsigned char)(iter * factorG):0),
- (iter > 999)?0:((iter > thresB)?(unsigned char)(iter * factorB):0),
- 0);
- //SDL_RenderClear(renderer);
- //SDL_RenderCopy(renderer, txtTexture, NULL, &dstRect);
- SDL_RenderDrawPoint(renderer, pixelX, pixelY);
- }
- }
- /*switch(trip_vector) {
- case 0:
- factorR -= 5;
- if(factorR < 1)
- trip_vector = 1;
- break;
- case 1:
- factorG += 5;
- if(factorG > 254)
- trip_vector = 2;
- break;
- case 2:
- factorG -= 5;
- if(factorG < 1)
- trip_vector = 3;
- break;
- case 3:
- factorB += 5;
- if(factorB > 254)
- trip_vector = 4;
- break;
- case 4:
- factorB -= 5;
- if(factorB < 1)
- trip_vector = 5;
- break;
- case 5:
- factorR += 5;
- if(factorR > 254)
- trip_vector = 0;
- break;
- default:
- break;
- }*/
- switch(trip_vector) {
- case 0:
- factorR -= (factorR < 10)?0:COLOR_STEP;
- factorG += (factorG > 254)?0:COLOR_STEP;
- if(factorR < 10)
- trip_vector = 1;
- break;
- case 1:
- factorG -= (factorG < 10)?0:COLOR_STEP;
- factorB += (factorB > 254)?0:COLOR_STEP;
- if(factorG < 10)
- trip_vector = 2;
- break;
- case 2:
- factorB -= (factorB < 10)?0:COLOR_STEP;
- factorR += (factorR > 254)?0:COLOR_STEP;
- if(factorB < 10)
- trip_vector = 0;
- break;
- default:
- break;
- }
- SDL_RenderPresent(renderer);
- }
- int main(int argc, char** args)
- {
- SDL_Color *palette[MANDLEBROT_MAX_ITER + 1];
- SDL_Window *win;
- SDL_Renderer *renderer;
- TTF_Font *font;
- int width = WIN_WIDTH;
- int height = WIN_HEIGHT;
- double currentX = 1.2;
- double currentY = 1.0;
- double currentZ = 1.0;
- double currentXfactor = 0.9;
- int iter;
- int frames = 0;
- //Start SDL
- SDL_Init(SDL_INIT_VIDEO);
- win = SDL_CreateWindow("s2v", 100, 100, width, height, SDL_WINDOW_ALLOW_HIGHDPI | ((FULLSCREEN)?SDL_WINDOW_FULLSCREEN:0));
- renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
- SDL_ShowCursor(SDL_DISABLE);
- TTF_Init();
- font = TTF_OpenFont("arial.ttf", 12);
- SDL_SetCursor(init_system_cursor(arrow));
- SDL_ShowCursor(1);
- if(DEBUG_COLORS)
- draw_mandlebrot(renderer, width, height, 7.5, 1, 4);
- for(iter = 0; frames < MAX_FRAMES; frames++) {
- SDL_Event e;
- if(SDL_PollEvent(&e)) {
- if(e.type == SDL_QUIT) {
- break;
- }
- /*else if(e.type == SDL_MOUSEBUTTONDOWN) {
- if(e.button.button == SDL_BUTTON_LEFT) {
- currentX = ((e.button.x / (double)width) * 2.5);
- currentY = (e.button.y / (double)height);
- draw_mandlebrot(renderer, palette, width, height, currentX, currentY, currentZ);
- }
- }
- else if(e.type == SDL_MOUSEWHEEL) {
- currentZ += e.wheel.y;
- currentX *= currentZ;
- currentY *= currentZ;
- draw_mandlebrot(renderer, palette, width, height, currentX, currentY, currentZ);
- }*/
- }
- if(/* SDL_GetTicks() % 2 == */1 && !DEBUG_COLORS) {
- if(iter == 0 && (currentZ == 1 || currentZ < -30)) {
- iter = 1;
- currentXfactor += 0.1;
- } else if(iter == 1 && currentZ > 10)
- iter = 0;
- if(iter) {
- currentZ += 0.15;
- currentX += 0.25 * currentXfactor;
- }
- else {
- currentZ -= 0.15;
- currentX -= 0.25 * currentXfactor;
- }
- draw_mandlebrot(renderer, width, height, currentX, currentY, currentZ);
- {
- // Create an empty RGB surface that will be used to create the screenshot bmp file
- SDL_Surface* pScreenShot = SDL_CreateRGBSurface(0, width, height, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
- if(pScreenShot)
- {
- char *fname = (char *)malloc(256);
- sprintf(fname, RENDER_FMT, frames);
- // Read the pixels from the current render target and save them onto the surface
- SDL_RenderReadPixels(renderer, NULL, SDL_GetWindowPixelFormat(win), pScreenShot->pixels, pScreenShot->pitch);
- // Create the bmp screenshot file
- SDL_SaveBMP(pScreenShot, fname);
- free(fname);
- // Destroy the screenshot surface
- SDL_FreeSurface(pScreenShot);
- }
- }
- }
- }
- TTF_CloseFont(font);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(win);
- //Quit SDL
- TTF_Quit();
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement