Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_image.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <random>
  6.  
  7.  
  8. struct Vector2f
  9. {
  10.     Vector2f()
  11.         :x(0), y(0)
  12.     {}
  13.  
  14.     Vector2f(float _x, float _y)
  15.         :x(_x), y(_y)
  16.     {}
  17.  
  18.     float x;
  19.     float y;
  20.  
  21.     Vector2f operator+(Vector2f& rhs)
  22.     {
  23.         Vector2f temp;
  24.         temp.x = this->x += rhs.x;
  25.         temp.y = this->y += rhs.y;
  26.         return temp;
  27.     }
  28. };
  29.  
  30. struct SpiralSettings
  31. {
  32.     SpiralSettings(SDL_Texture* _texture, float _x, float _y, float _a = 1, float _b = 0.5)
  33.         :center(_x, _y), texture(_texture), a(_a), b(_b)
  34.     {}
  35.     Vector2f center;
  36.     float a;
  37.     float b;
  38.  
  39.     SDL_Texture* texture;
  40. };
  41.  
  42. void drawTextureAtPosition(SDL_Renderer*, SDL_Texture*, Vector2f, float);
  43. void drawSpiral(SDL_Renderer*, SpiralSettings);
  44.  
  45. int main(int argc, char** argv)
  46. {
  47.     Uint32 SCREEN_X         = NULL;
  48.     Uint32 SCREEN_Y         = NULL;
  49.     Uint32 SCREEN_WIDTH     = 1024;
  50.     Uint32 SCREEN_HEIGHT    = 720;
  51.     Uint32 COLOUR_DEPTH     = 32;
  52.  
  53.     Uint32 RED_MASK         = 0xff000000;
  54.     Uint32 GREEN_MASK       = 0x00ff0000;
  55.     Uint32 BLUE_MASK        = 0x0000ff00;
  56.     Uint32 ALPHA_MASK       = 0x000000ff;
  57.  
  58.     Uint32 WINDOW_FLAGS     = 0;
  59.     Uint32 SURFACE_FLAGS    = 0;
  60.  
  61.     SDL_Window* window      = nullptr;
  62.     SDL_Renderer* renderer  = nullptr;
  63.     SDL_Surface* surface    = nullptr;
  64.     SDL_Surface* glow1      = nullptr;
  65.     SDL_Surface* glow2      = nullptr;
  66.  
  67.     SDL_Texture* tex_glow1  = nullptr;
  68.     SDL_Texture* tex_glow2 = nullptr;
  69.     SDL_Rect tex_glow1_rect;
  70.  
  71.     window = SDL_CreateWindow(  "SDL Rendering",
  72.                                 SDL_WINDOWPOS_CENTERED,
  73.                                 SDL_WINDOWPOS_CENTERED,
  74.                                 SCREEN_WIDTH,
  75.                                 SCREEN_HEIGHT,
  76.                                 WINDOW_FLAGS    );
  77.  
  78.     surface = SDL_CreateRGBSurface( SURFACE_FLAGS,
  79.                                     SCREEN_WIDTH,
  80.                                     SCREEN_HEIGHT,
  81.                                     COLOUR_DEPTH,
  82.                                     RED_MASK,
  83.                                     GREEN_MASK,
  84.                                     BLUE_MASK,
  85.                                     ALPHA_MASK  );
  86.  
  87.     glow1 = IMG_Load("glow2.png");
  88.  
  89.     renderer = SDL_CreateSoftwareRenderer(surface);
  90.  
  91.     tex_glow1 = SDL_CreateTextureFromSurface(renderer, glow1);
  92.     tex_glow1_rect.h = glow1->h;
  93.     tex_glow1_rect.w = glow1->w;
  94.     tex_glow1_rect.x = 100;
  95.     tex_glow1_rect.y = 100;
  96.    
  97.  
  98.     SDL_Event event;
  99.     bool running = true;
  100.     while (running)
  101.     {
  102.         while (SDL_PollEvent(&event) != 0)
  103.         {
  104.             if (event.type == SDL_QUIT)
  105.             {
  106.                 running = false;
  107.             }
  108.  
  109.             if (event.type == SDL_KEYDOWN)
  110.             {
  111.                 if (event.key.keysym.sym == SDLK_RIGHT)
  112.                 {
  113.                     SpiralSettings ss(tex_glow1, float(SCREEN_WIDTH) / 2, float(SCREEN_HEIGHT) / 2, 400, -0.5);
  114.                     ss.texture = tex_glow1;
  115.                     drawSpiral(renderer, ss);
  116.                     SpiralSettings ss2(tex_glow1, float(SCREEN_WIDTH) / 2, float(SCREEN_HEIGHT) / 2, -200, -0.5);
  117.                     ss2.texture = tex_glow1;
  118.                     drawSpiral(renderer, ss2);
  119.                     running = false;
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     SDL_RenderPresent(renderer);
  126.     SDL_SaveBMP(surface, "image.png");
  127.  
  128.     SDL_Quit();
  129.  
  130.     return 0;
  131. }
  132.  
  133.  
  134. void drawTextureAtPosition(SDL_Renderer* renderer, SDL_Texture* texture, Vector2f position, float scale = 1)
  135. {
  136.     SDL_Rect draw_area;
  137.     SDL_QueryTexture(texture, NULL, NULL, &draw_area.w, &draw_area.h);
  138.  
  139.     draw_area.w = int(draw_area.w * scale);
  140.     draw_area.h = int(draw_area.h * scale);
  141.     draw_area.x = int(position.x) - (draw_area.w / 2);
  142.     draw_area.y = int(position.y) - (draw_area.h / 2);
  143.  
  144.     SDL_RenderCopy(renderer, texture, NULL, &draw_area);
  145. }
  146.  
  147. void drawLine(SDL_Renderer* renderer, Vector2f start, Vector2f end)
  148. {
  149.     //SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0x00, 0xff);
  150.     SDL_RenderDrawLine(renderer, int(start.x), int(start.y), int(end.x), int(end.y));
  151. }
  152.  
  153. void drawRandomCopiesAlongLine(SDL_Renderer* renderer, SDL_Texture* texture, Vector2f begin, Vector2f end, unsigned int num_points, float spread, float taper, unsigned int seed)
  154. {
  155.     std::uniform_real_distribution<float> distribution(spread*-1, spread);
  156.     std::uniform_real_distribution<float> distribution_2(0.03, 0.05);
  157.     std::mt19937 generator(76585);
  158.  
  159.     float distance = sqrt(pow(end.x - begin.x, 2) + pow(end.y - begin.y, 2));
  160.    
  161.     Vector2f step_vector((end.x - begin.x) / num_points, (end.y - begin.y) / num_points);
  162.     Vector2f current_position = begin;
  163.  
  164.     for (unsigned int i = 1; i < num_points + 1; i++)
  165.     {
  166.         Vector2f displacement(0,0);
  167.         if (taper != 0)
  168.         {
  169.             displacement.x = distribution(generator);
  170.             displacement.y = distribution(generator);
  171.         }
  172.         else
  173.         {
  174.             displacement.x = distribution(generator);
  175.             displacement.y = distribution(generator);
  176.         }
  177.         Vector2f t(current_position.x + displacement.x, current_position.y + displacement.y);
  178.         drawTextureAtPosition(renderer, texture, t, distribution_2(generator));
  179.  
  180.         current_position = Vector2f(current_position.x + step_vector.x, current_position.y + step_vector.y);
  181.         //std::cout << "## " << current_position.x << ", " << current_position.y << "\n";
  182.     }
  183. }
  184.  
  185. void drawSpiral(SDL_Renderer* renderer, SpiralSettings settings)
  186. {
  187.     std::cout << "Draw Spiral\n";
  188.     Vector2f current_position;
  189.     Vector2f last_position;
  190.     Vector2f start_position;
  191.     current_position = settings.center;
  192.     last_position = settings.center;
  193.     start_position = settings.center;
  194.    
  195.     float a = settings.a;
  196.     float b = settings.b;
  197.    
  198.  
  199.     SDL_Surface* glow2 = nullptr;
  200.     SDL_Texture* tex_glow2 = nullptr;
  201.     glow2 = IMG_Load("glow4.png");
  202.     tex_glow2 = SDL_CreateTextureFromSurface(renderer, glow2);
  203.  
  204.     unsigned int numPoints = 10;
  205.  
  206.     for (float t = 1; t < numPoints; t += 0.1)
  207.     {
  208.         std::cout << current_position.x << ", " << current_position.y << "\n";
  209.  
  210.         drawRandomCopiesAlongLine(renderer, settings.texture, last_position, current_position, 10, 100, 0.5, 56877);
  211.  
  212.         SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0x00, 0xff);
  213.         //drawLine(renderer, current_position, last_position);
  214.  
  215.  
  216.         last_position = current_position;
  217.         current_position.x = float(start_position.x + (a * pow(t, (1 / b)) * cos(float(t))));
  218.         current_position.y = float(start_position.y + (a * pow(t, (1 / b)) * sin(float(t))));
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement