Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <SDL_image.h>
- #include <math.h>
- #include <iostream>
- #include <random>
- struct Vector2f
- {
- Vector2f()
- :x(0), y(0)
- {}
- Vector2f(float _x, float _y)
- :x(_x), y(_y)
- {}
- float x;
- float y;
- Vector2f operator+(Vector2f& rhs)
- {
- Vector2f temp;
- temp.x = this->x += rhs.x;
- temp.y = this->y += rhs.y;
- return temp;
- }
- };
- struct SpiralSettings
- {
- SpiralSettings(SDL_Texture* _texture, float _x, float _y, float _a = 1, float _b = 0.5)
- :center(_x, _y), texture(_texture), a(_a), b(_b)
- {}
- Vector2f center;
- float a;
- float b;
- SDL_Texture* texture;
- };
- void drawTextureAtPosition(SDL_Renderer*, SDL_Texture*, Vector2f, float);
- void drawSpiral(SDL_Renderer*, SpiralSettings);
- int main(int argc, char** argv)
- {
- Uint32 SCREEN_X = NULL;
- Uint32 SCREEN_Y = NULL;
- Uint32 SCREEN_WIDTH = 1024;
- Uint32 SCREEN_HEIGHT = 720;
- Uint32 COLOUR_DEPTH = 32;
- Uint32 RED_MASK = 0xff000000;
- Uint32 GREEN_MASK = 0x00ff0000;
- Uint32 BLUE_MASK = 0x0000ff00;
- Uint32 ALPHA_MASK = 0x000000ff;
- Uint32 WINDOW_FLAGS = 0;
- Uint32 SURFACE_FLAGS = 0;
- SDL_Window* window = nullptr;
- SDL_Renderer* renderer = nullptr;
- SDL_Surface* surface = nullptr;
- SDL_Surface* glow1 = nullptr;
- SDL_Surface* glow2 = nullptr;
- SDL_Texture* tex_glow1 = nullptr;
- SDL_Texture* tex_glow2 = nullptr;
- SDL_Rect tex_glow1_rect;
- window = SDL_CreateWindow( "SDL Rendering",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- WINDOW_FLAGS );
- surface = SDL_CreateRGBSurface( SURFACE_FLAGS,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- COLOUR_DEPTH,
- RED_MASK,
- GREEN_MASK,
- BLUE_MASK,
- ALPHA_MASK );
- glow1 = IMG_Load("glow2.png");
- renderer = SDL_CreateSoftwareRenderer(surface);
- tex_glow1 = SDL_CreateTextureFromSurface(renderer, glow1);
- tex_glow1_rect.h = glow1->h;
- tex_glow1_rect.w = glow1->w;
- tex_glow1_rect.x = 100;
- tex_glow1_rect.y = 100;
- SDL_Event event;
- bool running = true;
- while (running)
- {
- while (SDL_PollEvent(&event) != 0)
- {
- if (event.type == SDL_QUIT)
- {
- running = false;
- }
- if (event.type == SDL_KEYDOWN)
- {
- if (event.key.keysym.sym == SDLK_RIGHT)
- {
- SpiralSettings ss(tex_glow1, float(SCREEN_WIDTH) / 2, float(SCREEN_HEIGHT) / 2, 400, -0.5);
- ss.texture = tex_glow1;
- drawSpiral(renderer, ss);
- SpiralSettings ss2(tex_glow1, float(SCREEN_WIDTH) / 2, float(SCREEN_HEIGHT) / 2, -200, -0.5);
- ss2.texture = tex_glow1;
- drawSpiral(renderer, ss2);
- running = false;
- }
- }
- }
- }
- SDL_RenderPresent(renderer);
- SDL_SaveBMP(surface, "image.png");
- SDL_Quit();
- return 0;
- }
- void drawTextureAtPosition(SDL_Renderer* renderer, SDL_Texture* texture, Vector2f position, float scale = 1)
- {
- SDL_Rect draw_area;
- SDL_QueryTexture(texture, NULL, NULL, &draw_area.w, &draw_area.h);
- draw_area.w = int(draw_area.w * scale);
- draw_area.h = int(draw_area.h * scale);
- draw_area.x = int(position.x) - (draw_area.w / 2);
- draw_area.y = int(position.y) - (draw_area.h / 2);
- SDL_RenderCopy(renderer, texture, NULL, &draw_area);
- }
- void drawLine(SDL_Renderer* renderer, Vector2f start, Vector2f end)
- {
- //SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0x00, 0xff);
- SDL_RenderDrawLine(renderer, int(start.x), int(start.y), int(end.x), int(end.y));
- }
- void drawRandomCopiesAlongLine(SDL_Renderer* renderer, SDL_Texture* texture, Vector2f begin, Vector2f end, unsigned int num_points, float spread, float taper, unsigned int seed)
- {
- std::uniform_real_distribution<float> distribution(spread*-1, spread);
- std::uniform_real_distribution<float> distribution_2(0.03, 0.05);
- std::mt19937 generator(76585);
- float distance = sqrt(pow(end.x - begin.x, 2) + pow(end.y - begin.y, 2));
- Vector2f step_vector((end.x - begin.x) / num_points, (end.y - begin.y) / num_points);
- Vector2f current_position = begin;
- for (unsigned int i = 1; i < num_points + 1; i++)
- {
- Vector2f displacement(0,0);
- if (taper != 0)
- {
- displacement.x = distribution(generator);
- displacement.y = distribution(generator);
- }
- else
- {
- displacement.x = distribution(generator);
- displacement.y = distribution(generator);
- }
- Vector2f t(current_position.x + displacement.x, current_position.y + displacement.y);
- drawTextureAtPosition(renderer, texture, t, distribution_2(generator));
- current_position = Vector2f(current_position.x + step_vector.x, current_position.y + step_vector.y);
- //std::cout << "## " << current_position.x << ", " << current_position.y << "\n";
- }
- }
- void drawSpiral(SDL_Renderer* renderer, SpiralSettings settings)
- {
- std::cout << "Draw Spiral\n";
- Vector2f current_position;
- Vector2f last_position;
- Vector2f start_position;
- current_position = settings.center;
- last_position = settings.center;
- start_position = settings.center;
- float a = settings.a;
- float b = settings.b;
- SDL_Surface* glow2 = nullptr;
- SDL_Texture* tex_glow2 = nullptr;
- glow2 = IMG_Load("glow4.png");
- tex_glow2 = SDL_CreateTextureFromSurface(renderer, glow2);
- unsigned int numPoints = 10;
- for (float t = 1; t < numPoints; t += 0.1)
- {
- std::cout << current_position.x << ", " << current_position.y << "\n";
- drawRandomCopiesAlongLine(renderer, settings.texture, last_position, current_position, 10, 100, 0.5, 56877);
- SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0x00, 0xff);
- //drawLine(renderer, current_position, last_position);
- last_position = current_position;
- current_position.x = float(start_position.x + (a * pow(t, (1 / b)) * cos(float(t))));
- current_position.y = float(start_position.y + (a * pow(t, (1 / b)) * sin(float(t))));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement