RenX99

Untitled

Nov 5th, 2021
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <libnoise/noise.h>
  6.  
  7. const int width = 640;
  8. const int height = 480;
  9.  
  10. float x = width / 2;
  11. float y = height / 2;
  12.  
  13. double map( double x, double in_min, double in_max, double out_min, double out_max) {
  14.   return  out_min + (( out_max - out_min) * (x - in_min) / (in_max - in_min));
  15. }
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.  
  20.   noise::module::Perlin myNoise;
  21.  
  22.   SDL_Window *window = nullptr;
  23.   SDL_Renderer *renderer = nullptr;
  24.  
  25.   if(SDL_Init(SDL_INIT_VIDEO) < 0 )
  26.     std::cout << "Video Initialization Error: " << SDL_GetError() << std::endl;
  27.   else
  28.   {
  29.  
  30.     window = SDL_CreateWindow("SDL Perlin Noise", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
  31.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  32.  
  33.     if (window == NULL || renderer == NULL)
  34.       std::cout << "Window creation error: " << SDL_GetError() << std::endl;
  35.     else
  36.     {
  37.       bool isRunning = true;
  38.       SDL_Event ev;
  39.  
  40.       SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  41.  
  42.  
  43.       double n = 0; // time step
  44.       while(isRunning)
  45.       {
  46.  
  47.         while(SDL_PollEvent(&ev) != 0)
  48.         {
  49.           if(ev.type == SDL_QUIT)
  50.             isRunning = false;
  51.         }
  52.  
  53.         double yoff = 0;
  54.         double xoff = 0;
  55.  
  56.  
  57.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  58.         SDL_RenderClear(renderer);
  59.  
  60.         for (int x = 0; x < width; x++){
  61.           yoff = 0;
  62.           for (int y = 0; y < height; y++){
  63.             int bright = map(myNoise.GetValue(xoff , yoff, n), -1.0, 1.0, 0, 255);
  64.             SDL_SetRenderDrawColor(renderer, bright, bright, bright, 255);
  65.             SDL_RenderDrawPoint(renderer, x, y);
  66.             yoff += 0.01;
  67.           }
  68.           xoff += 0.01;
  69.         }
  70.         n += 0.01;
  71.         SDL_RenderPresent(renderer);
  72.       }
  73.     }
  74.   }
  75.  
  76.   SDL_DestroyWindow(window);
  77.   SDL_Quit();
  78.   std::cout << "Good Bye.\n";
  79.  
  80.   return(0);
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment