FLamparski

The problematic SDL program

Jul 20th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. //colorizer.h
  2. #include <iostream>
  3. #include "SDL.h"
  4. #include "math.h"
  5.  
  6. class Colorizer
  7. {
  8.     public:
  9.         Colorizer(int width, int height, int bpp);
  10.         ~Colorizer();
  11.         void frame(int rx, int ry, int gx, int gy, int bx, int by);
  12.  
  13.     protected:
  14.         SDL_Surface* screen;
  15.         SDL_Surface* canvas;
  16.         void set_pixel(int x, int y, int r, int g, int b);
  17.         double distance(int from_x, int from_y, int to_x, int to_y);
  18.         int canvasW;
  19.         int canvasH;
  20.         int canvasBPP;
  21.         long framenumber;
  22. };
  23.  
  24. //-------------------------------------------------------------------------------------------
  25. //colorizer.cpp
  26. #include "colorizer.h"
  27.  
  28. Colorizer::Colorizer(int width, int height, int bpp)
  29. {
  30.     screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
  31.     if(!screen)
  32.     {
  33.         throw(SDL_GetError());
  34.     }
  35.     canvasW = width;
  36.     canvasH = height;
  37.     canvasBPP = bpp;
  38.     framenumber = 0;
  39. }
  40.  
  41. void Colorizer::frame(int rx, int ry, int gx, int gy, int bx, int by)
  42. {
  43.     if(SDL_LockSurface(screen) < 0){
  44.         std::cerr << "Can't lock screen: " << SDL_GetError() << std::endl;
  45.     }
  46.  
  47.     for(int pix = 0; pix < screen->h; pix++) //Per column
  48.     {
  49.         for(int row = 0; row < screen->w; row++) //Per pixel
  50.         {
  51.             double src_r = sin(0.5*distance(row, pix, rx, ry)*framenumber);
  52.             double src_g = cos(0.5*distance(row, pix, gx, gy)*framenumber);
  53.             double src_b = sin(0.5*distance(row, pix, bx, by)*framenumber);
  54.             int dst_r = static_cast<int> (fabs(floor(255*src_r)));
  55.             int dst_g = static_cast<int> (fabs(floor(255*src_g)));
  56.             int dst_b = static_cast<int> (fabs(floor(255*src_b)));
  57.             if(dst_r > 255 or dst_g > 255 or dst_b > 255 or dst_r < 0 or dst_g < 0 or dst_b < 0){
  58.                 std::cerr << "Error: Pixel: (" << row << "," << pix << ") r=" << dst_r << " g=" << dst_g << " b=" << dst_b << std::endl;
  59.                 throw ("One of the colours is invalid.");
  60.             }
  61.             set_pixel(row, pix, dst_r, dst_g, dst_b);
  62.         }
  63.     }
  64.  
  65.     SDL_UnlockSurface(screen);
  66.     SDL_Flip(screen);
  67.     framenumber++;
  68. }
  69.  
  70. double Colorizer::distance(int from_x, int from_y, int to_x, int to_y)
  71. {
  72.     double triW = static_cast<double> (from_x) - static_cast<double> (to_x);
  73.     double triH = static_cast<double> (from_y) - static_cast<double> (to_y);
  74.     double distSq = triW*triW + triH*triH;
  75.     return sqrt(distSq);
  76. }
  77.  
  78. void Colorizer::set_pixel(int x, int y, int r, int g, int b)
  79. {
  80.     if(x > 640 or y > 480){
  81.         std::cerr << "Invalid pixel: " << x << "," << y << std::endl;
  82.         return;
  83.     }
  84.     int color;
  85.     color = SDL_MapRGB(screen->format, r, g, b);
  86.  
  87.     int *pixel;
  88.     pixel = (int*) screen->pixels + x + y;
  89.     *pixel = color;
  90. }
  91.  
  92. Colorizer::~Colorizer()
  93. {
  94.     SDL_FreeSurface(screen);
  95. }
  96.  
  97. //-------------------------------------------------------------------------------------------
  98. //main.cpp
  99. #include <iostream>
  100. #include "SDL.h"
  101. #include "colorizer.h"
  102.  
  103. /*snip pretty bash colour functions*/
  104.  
  105. int main (int argc, char* argv[])
  106. {
  107.     int sdlinit = SDL_Init( SDL_INIT_EVERYTHING );
  108.     if( sdlinit < 0 ){
  109.         std::cerr << "Cannot initialize SDL: " << SDL_GetError() << std::endl;
  110.         return 1;
  111.     }
  112.  
  113.     try{
  114.         Colorizer app(640, 480, 32);
  115.         bool keypress = false;
  116.  
  117.         SDL_Event evt;
  118.         while(!keypress){
  119.             std::cerr << "."; //per frame
  120.             app.frame(100, 230, 25, 40, 200, 100); //default points
  121.             while(SDL_PollEvent(&evt)){
  122.                 std::cerr << "!"; //per event
  123.                 if (evt.type == SDL_QUIT or evt.type == SDL_KEYDOWN)
  124.                 {
  125.                     keypress = true;
  126.                 }
  127.             }
  128.         }
  129.         std::cout << std::endl;
  130.         return 0;
  131.     }catch(const char* &ex){
  132.         SDL_Quit();
  133.         return 1;
  134.     }
  135. }
  136. //-------------------------------------------------------------------------------------------
  137. // compile with: g++ *.cpp -O2 -o colorizer `pkg-config --cflags --libs sdl`
  138. //-------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment