Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.82 KB | None | 0 0
  1. #ifdef __cplusplus
  2.     #include <cstdlib>
  3. #else
  4.     #include <stdlib.h>
  5.  
  6. #endif
  7. #include <SDL/SDL.h>
  8. #include <math.h>
  9. #define pi 3.1415926536
  10. #include <time.h>
  11. #include <iostream>
  12. #include <fstream>
  13. using namespace std;
  14.  
  15. SDL_Surface *screen;
  16. int width = 512;
  17. int height = 340;
  18. char const* tytul = "GKiM2019 - Lab 10 - Pabianczyk Pawel";
  19.  
  20. SDL_Color paleta[256];
  21. int ileKolorow=0;
  22.  
  23. struct YCbCr {
  24.     float y;
  25.     float cb;
  26.     float cr;
  27. };
  28.  
  29. struct HSV {
  30.     float h;
  31.     float s;
  32.     float v;
  33. };
  34.  
  35. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
  36. SDL_Color getPixel (int x, int y);
  37.  
  38. void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
  39.  
  40. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
  41. void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
  42. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B);
  43. YCbCr RGBtoYCbCr(SDL_Color rgb);
  44. SDL_Color YCbCrtoRGB(YCbCr ycbcr);
  45. SDL_Color HSVtoRGB(HSV hsv);
  46. HSV RGBtoHSV(SDL_Color rgb);
  47. void Funkcja1();
  48. void Funkcja2();
  49. void Funkcja3();
  50. void Funkcja4();
  51. void Funkcja5();
  52. void Funkcja6();
  53. void Funkcja7();
  54. void Funkcja8();
  55. void Funkcja9();
  56.  
  57. bool porownajKolory(SDL_Color k1, SDL_Color k2){
  58.     bool wynik = true;
  59.     if(k1.r != k2.r) wynik= false;
  60.     if(k1.g != k2.g) wynik= false;
  61.     if(k1.b != k2.b) wynik= false;
  62.     return wynik;
  63. }
  64.  
  65. int dodajKolor(SDL_Color kolor){
  66.     cout <<ileKolorow<<": ["<<(int)kolor.r<<","<<(int)kolor.g<<","<<(int)kolor.b<<"]"<<endl;
  67.     paleta[ileKolorow] = kolor;
  68.     ileKolorow++;
  69.     return (ileKolorow-1);
  70. }
  71.  
  72. int sprawdzKolor(SDL_Color kolor){
  73.     int wynik = -1;
  74.  
  75.     if(ileKolorow>0){
  76.         for(int k=0; k<ileKolorow; k++){
  77.             if(porownajKolory(kolor, paleta[k])){
  78.                 wynik = k;
  79.             }
  80.         }
  81.     }
  82.  
  83.     if(wynik<0){
  84.         wynik=dodajKolor(kolor);
  85.     }
  86.  
  87.     return wynik;
  88. }
  89.  
  90. void Funkcja1() {
  91.     ileKolorow = 0;
  92.  
  93.     SDL_Color kolor;
  94.  
  95.     for(int y=0; y<(height/2); y++){
  96.         for(int x=0; x<(width/2); x++){
  97.         kolor = getPixel(x, y);
  98.         sprawdzKolor(kolor);
  99.         //cout <<x<<": ["<<(int)kolor.r<<","<<(int)kolor.g<<","<<(int)kolor.b<<"]"<<endl;
  100.         }
  101.     }
  102.  
  103.  
  104.     SDL_Flip(screen);
  105. }
  106.  
  107.  
  108. void Funkcja2() {
  109.  
  110.     SDL_Color kolor;
  111.     Uint16 szerokosc=width/2;
  112.     Uint16 wysokosc = height/2;
  113.     Uint8 wartosc = 0;
  114.     cout <<"Zapisujemy za pomoca cout plik obrazek.bin"<<endl;
  115.     ofstream wyjscie("obrazek.bin");
  116.     wyjscie<< "DG"<<szerokosc<<wysokosc;
  117.     for(int y=0; y<wysokosc; y++){
  118.         for(int x=0; x<szerokosc; x++){
  119.             kolor = getPixel(x, y);
  120.             wartosc=sprawdzKolor(kolor);
  121.             wyjscie << wartosc;
  122.         }
  123.     }
  124.  
  125.     wyjscie.close();
  126.     SDL_Flip(screen);
  127. }
  128.  
  129. void Funkcja3() {
  130.  
  131.     SDL_Color kolor;
  132.     Uint16 szerokosc=width/2;
  133.     Uint16 wysokosc = height/2;
  134.     Uint8 wartosc = 0;
  135.     Uint8 kolory = ileKolorow;
  136.     char identyfikator[] = "DG";
  137.     cout <<"Zapisujemy za pomoca write plik obrazek.bin"<<endl;
  138.     ofstream wyjscie("obrazek2.bin", ios::binary);
  139.  
  140.     //wyjscie<< "DG"<<szerokosc<<wysokosc;
  141.  
  142.     wyjscie.write((char*)&szerokosc, sizeof(Uint16));
  143.     wyjscie.write((char*)&wysokosc, sizeof(Uint16));
  144.     wyjscie.write((char*)&kolory, sizeof(Uint8));
  145.     wyjscie.write((char*)&identyfikator, sizeof(char));
  146.     for(int y=0; y<wysokosc; y++){
  147.         for(int x=0; x<szerokosc; x++){
  148.             kolor = getPixel(x, y);
  149.             wartosc=sprawdzKolor(kolor);
  150.             wyjscie.write((char*)&wyjscie, sizeof(Uint8));
  151.         }
  152.     }
  153.  
  154.     wyjscie.close();
  155.  
  156.     SDL_Flip(screen);
  157. }
  158.  
  159. void Funkcja4() {
  160.  
  161.     SDL_Color kolor;
  162.     Uint8 wartosc=0;
  163.     Uint16 szerokosc = width/2;
  164.     Uint16 wysokosc= height/2;
  165.     Uint8 kolory;
  166.     char identyfikator[] = " ";
  167.  
  168.     cout << "Odczytujemy za pomoca read pliki obrazek2.bin"<< endl;
  169.     ifstream wejscie("obrazek2.bin", ios::binary);
  170.     wejscie.read((char*)&szerokosc, sizeof(Uint16));
  171.     wejscie.read((char*)&wysokosc, sizeof(Uint16));
  172.     wejscie.read((char*)&kolory, sizeof(Uint8));
  173.     wejscie.read((char*)&identyfikator, sizeof(char));
  174.  
  175.  
  176.     cout <<"Parametry: "    << endl;
  177.     cout <<"identyfikator: "<< identyfikator<<endl;
  178.     cout <<"szerekosc: "    << szerokosc<<endl;
  179.     cout <<"wysokosc: "     << wysokosc<<endl;
  180.     cout <<"kolory: "       << (int)kolory<<endl;
  181.  
  182.     for(int y=0; y<wysokosc; y++){
  183.         for(int x=0; x<szerokosc; x++){
  184.             wejscie.read((char*)&wartosc, sizeof(Uint8));
  185.             kolor=paleta[wartosc];
  186.             setPixel(x+(width/2), y, kolor.r, kolor.g, kolor.b);
  187.         }
  188.     }
  189.  
  190.     wejscie.close();
  191.     SDL_Flip(screen);
  192. }
  193.  
  194.  
  195. void Funkcja5() {
  196.  
  197.     //...
  198.  
  199.     SDL_Flip(screen);
  200. }
  201.  
  202.  
  203. void Funkcja6() {
  204.  
  205.     //...
  206.  
  207.     SDL_Flip(screen);
  208. }
  209.  
  210.  
  211. void Funkcja7() {
  212.  
  213.     //...
  214.  
  215.     SDL_Flip(screen);
  216. }
  217.  
  218.  
  219. void Funkcja8() {
  220.  
  221.     //...
  222.  
  223.     SDL_Flip(screen);
  224. }
  225.  
  226.  
  227. void Funkcja9() {
  228.  
  229.     //...
  230.  
  231.     SDL_Flip(screen);
  232. }
  233.  
  234.  
  235. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
  236.     setPixel(x1,y1,R,G,B);
  237.     setPixel(x2,y2,R,G,B);
  238.      // zmienne pomocnicze
  239.      int d, dx, dy, ai, bi, xi, yi;
  240.      int x = x1, y = y1;
  241.      // ustalenie kierunku rysowania
  242.      if (x1 < x2)
  243.      {
  244.          xi = 1;
  245.          dx = x2 - x1;
  246.      }
  247.      else
  248.      {
  249.          xi = -1;
  250.          dx = x1 - x2;
  251.      }
  252.      // ustalenie kierunku rysowania
  253.      if (y1 < y2)
  254.      {
  255.          yi = 1;
  256.          dy = y2 - y1;
  257.      }
  258.      else
  259.      {
  260.          yi = -1;
  261.          dy = y1 - y2;
  262.      }
  263.      // pierwszy piksel
  264.      setPixel(x, y, R, G, B);
  265.      // oś wiodąca OX
  266.      if (dx > dy)
  267.      {
  268.          ai = (dy - dx) * 2;
  269.          bi = dy * 2;
  270.          d = bi - dx;
  271.          // pętla po kolejnych x
  272.          while (x != x2)
  273.          {
  274.              // test współczynnika
  275.              if (d >= 0)
  276.              {
  277.                  x += xi;
  278.                  y += yi;
  279.                  d += ai;
  280.              }
  281.              else
  282.              {
  283.                  d += bi;
  284.                  x += xi;
  285.              }
  286.              setPixel(x, y, R, G, B);
  287.          }
  288.      }
  289.      // oś wiodąca OY
  290.      else
  291.      {
  292.          ai = ( dx - dy ) * 2;
  293.          bi = dx * 2;
  294.          d = bi - dy;
  295.          // pętla po kolejnych y
  296.          while (y != y2)
  297.          {
  298.              // test współczynnika
  299.              if (d >= 0)
  300.              {
  301.                  x += xi;
  302.                  y += yi;
  303.                  d += ai;
  304.              }
  305.              else
  306.              {
  307.                  d += bi;
  308.                  y += yi;
  309.              }
  310.              setPixel(x, y, R, G, B);
  311.          }
  312.      }
  313.     SDL_Flip(screen);
  314. }
  315.  
  316. void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
  317.     setPixel(x+x0, y+y0, R, G, B);
  318.     setPixel(y+x0, x+y0, R, G, B);
  319.     setPixel(y+x0, -x+y0, R, G, B);
  320.     setPixel(x+x0, -y+y0, R, G, B);
  321.     setPixel(-x+x0, -y+y0, R, G, B);
  322.     setPixel(-y+x0, -x+y0, R, G, B);
  323.     setPixel(-y+x0, x+y0, R, G, B);
  324.     setPixel(-x+x0, y+y0, R, G, B);;
  325. }
  326.  
  327.  
  328. void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
  329.     int x =0;
  330.     int y=r;
  331.     int d = 3-2*r;
  332.  
  333.     while (x <= y){
  334.         if (d<0){
  335.             d=d+4*x+6;
  336.             RysujOkrag(x0, y0, x, y, R, G, B);
  337.         } else{
  338.             d=d+4*(x-y)+10;
  339.             y--;
  340.             RysujOkrag(x0, y0, x, y, R, G, B);
  341.         }
  342.         x++;
  343.     }
  344.     SDL_Flip(screen);
  345. }
  346.  
  347. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
  348.     int xc  = 0,
  349.         yc  = b;
  350.     int aa  = a * a,
  351.         aa2 = aa + aa,
  352.         bb  = b * b,
  353.         bb2 = bb + bb;
  354.     int d   = bb - aa * b + (aa / 4),
  355.         dx  = 0,
  356.         dy  = aa2 * b;
  357.  
  358.     while (dx < dy) {
  359.         setPixel (x - xc,y - yc, R, G, B);
  360.         setPixel (x - xc,y + yc, R, G, B);
  361.         setPixel (x + xc,y - yc, R, G, B);
  362.         setPixel (x + xc,y + yc, R, G, B);
  363.         if (d > 0){
  364.             yc  = yc-1;
  365.             dy -= aa2;
  366.             d  -= dy;
  367.         }
  368.         xc  =xc+1;
  369.         dx += bb2;
  370.         d  += bb + dx;
  371.     }
  372.     d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
  373.  
  374.     while (yc >= 0) {
  375.         setPixel (x - xc,y - yc, R, G, B);
  376.         setPixel (x - xc,y + yc, R, G, B);
  377.         setPixel (x + xc,y - yc, R, G, B);
  378.         setPixel (x + xc,y + yc, R, G, B);
  379.         if (d < 0) {
  380.             xc =xc+1;
  381.             dx += bb2;
  382.             d  += (bb + dx);
  383.         }
  384.         yc  = yc-1;
  385.         dy -= aa2;
  386.         d  += aa - dy;
  387.     }
  388.     SDL_Flip(screen);
  389. }
  390.  
  391. YCbCr RGBtoYCbCr(SDL_Color rgb) {
  392.     YCbCr ycbcr;
  393.  
  394.     ycbcr.y =        0.299 * rgb.r       + 0.587*rgb.g + 0.114*rgb.b;
  395.     ycbcr.cb = 128 - 0.168736 * rgb.r - 0.331264*rgb.g +   0.5*rgb.b;
  396.     ycbcr.cr = 128 + 0.5 * rgb.r      - 0.418688*rgb.g - 0.081312*rgb.b;
  397.  
  398.     return ycbcr;
  399. }
  400.  
  401. float normalizacja(float wartosc) {
  402.     float wynik = wartosc;
  403.     if (wartosc > 255) wartosc = 255;
  404.     if (wartosc < 0) wartosc = 0;
  405.     return wartosc;
  406. }
  407.  
  408. SDL_Color YCbCrtoRGB(YCbCr ycbcr) {
  409.     SDL_Color rgb;
  410.     int R, G, B;
  411.  
  412.     R = ycbcr.y + 1.402*(ycbcr.cr-128);
  413.     G = ycbcr.y - 0.344136*(ycbcr.cb-128) - 0.714136*(ycbcr.cr-128);
  414.     B = ycbcr.y + 1.772*(ycbcr.cb-128);
  415.  
  416.     R = normalizacja(R);
  417.     G = normalizacja(G);
  418.     B = normalizacja(B);
  419.  
  420.     rgb = {.r=R, .g=G, .b=B};
  421.     return rgb;
  422. }
  423.  
  424. HSV RGBtoHSV(SDL_Color rgb) {
  425.     HSV hsv;
  426.     unsigned char rgbMin, rgbMax;
  427.  
  428.     rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
  429.     rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
  430.  
  431.     hsv.v = rgbMax;
  432.     if (hsv.v == 0) {
  433.         hsv.h = 0;
  434.         hsv.s = 0;
  435.         return hsv;
  436.     }
  437.  
  438.     hsv.s = 255.0*(rgbMax-rgbMin)/hsv.v;
  439.  
  440.     if (hsv.s == 0) {
  441.         hsv.h = 0;
  442.         return hsv;
  443.     }
  444.  
  445.     if (rgbMax == rgb.r) {
  446.         hsv.h = 0 + 43.0*(rgb.g-rgb.b)/(rgbMax-rgbMin);
  447.     }
  448.     else if (rgbMax == rgb.g) {
  449.         hsv.h = 85 + 43.0*(rgb.b-rgb.r)/(rgbMax-rgbMin);
  450.     }
  451.     else {
  452.         hsv.h = 171 + 43.0*(rgb.r-rgb.g)/(rgbMax-rgbMin);
  453.     }
  454.  
  455.     if (hsv.h>255) hsv.h-=255;
  456.     if (hsv.h<0) hsv.h+=255;
  457.  
  458.     hsv.s = normalizacja(hsv.s);
  459.     hsv.v = normalizacja(hsv.v);
  460.  
  461.     return hsv;
  462. }
  463.  
  464.  
  465. SDL_Color HSVtoRGB(HSV hsv) {
  466.     SDL_Color rgb;
  467.     int R, G, B;
  468.  
  469.     if (hsv.h < 0) hsv.h += 255;
  470.     if (hsv.h > 255) hsv.h -= 255;
  471.     hsv.s = normalizacja(hsv.s);
  472.     hsv.v = normalizacja(hsv.v);
  473.  
  474.     unsigned char region, p, q, t;
  475.     double h;
  476.     unsigned int s, v, reszta;
  477.  
  478.     if (hsv.s == 0) {
  479.         R = hsv.v;
  480.         G = hsv.v;
  481.         B = hsv.v;
  482.     }
  483.  
  484.     h = hsv.h;
  485.     s = hsv.s;
  486.     v = hsv.v;
  487.  
  488.  
  489.     region = h / 43;
  490.     reszta = (h - (region * 43) ) * 6;
  491.  
  492.     p = (v * (255 - s)) >> 8;
  493.     q = (v * (255 - ((s * reszta) >> 8 ))) >> 8;
  494.     t = (v * (255 - ((s * (255 - reszta)) >> 8 ))) >> 8;
  495.  
  496.     switch (region) {
  497.     case 0:
  498.         R = v;  G = t;  B = p;
  499.         break;
  500.     case 1:
  501.         R = q;  G = v;  B = p;
  502.         break;
  503.     case 2:
  504.         R = p;  G = v;  B = t;
  505.         break;
  506.     case 3:
  507.         R = p;  G = q;  B = v;
  508.         break;
  509.     case 4:
  510.         R = t;  G = p;  B = v;
  511.         break;
  512.     default:
  513.         R = v;  G = p;  B = q;
  514.         break;
  515.     }
  516.     rgb = {R, G, B};
  517.     return rgb;
  518. }
  519.  
  520.  
  521. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  522. {
  523.   if ((x>=0) && (x<width) && (y>=0) && (y<height))
  524.   {
  525.     /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
  526.     Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  527.  
  528.     /* Pobieramy informację ile bajtów zajmuje jeden piksel */
  529.     int bpp = screen->format->BytesPerPixel;
  530.  
  531.     /* Obliczamy adres piksela */
  532.     Uint8 *p1 = (Uint8 *)screen->pixels + (y*2) * screen->pitch + (x*2) * bpp;
  533.     Uint8 *p2 = (Uint8 *)screen->pixels + (y*2+1) * screen->pitch + (x*2) * bpp;
  534.     Uint8 *p3 = (Uint8 *)screen->pixels + (y*2) * screen->pitch + (x*2+1) * bpp;
  535.     Uint8 *p4 = (Uint8 *)screen->pixels + (y*2+1) * screen->pitch + (x*2+1) * bpp;
  536.  
  537.     /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
  538.     switch(bpp)
  539.     {
  540.         case 1: //8-bit
  541.             *p1 = pixel;
  542.             *p2 = pixel;
  543.             *p3 = pixel;
  544.             *p4 = pixel;
  545.             break;
  546.  
  547.         case 2: //16-bit
  548.             *(Uint16 *)p1 = pixel;
  549.             *(Uint16 *)p2 = pixel;
  550.             *(Uint16 *)p3 = pixel;
  551.             *(Uint16 *)p4 = pixel;
  552.             break;
  553.  
  554.         case 3: //24-bit
  555.             if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  556.                 p1[0] = (pixel >> 16) & 0xff;
  557.                 p1[1] = (pixel >> 8) & 0xff;
  558.                 p1[2] = pixel & 0xff;
  559.                 p2[0] = (pixel >> 16) & 0xff;
  560.                 p2[1] = (pixel >> 8) & 0xff;
  561.                 p2[2] = pixel & 0xff;
  562.                 p3[0] = (pixel >> 16) & 0xff;
  563.                 p3[1] = (pixel >> 8) & 0xff;
  564.                 p3[2] = pixel & 0xff;
  565.                 p4[0] = (pixel >> 16) & 0xff;
  566.                 p4[1] = (pixel >> 8) & 0xff;
  567.                 p4[2] = pixel & 0xff;
  568.             } else {
  569.                 p1[0] = pixel & 0xff;
  570.                 p1[1] = (pixel >> 8) & 0xff;
  571.                 p1[2] = (pixel >> 16) & 0xff;
  572.                 p2[0] = pixel & 0xff;
  573.                 p2[1] = (pixel >> 8) & 0xff;
  574.                 p2[2] = (pixel >> 16) & 0xff;
  575.                 p3[0] = pixel & 0xff;
  576.                 p3[1] = (pixel >> 8) & 0xff;
  577.                 p3[2] = (pixel >> 16) & 0xff;
  578.                 p4[0] = pixel & 0xff;
  579.                 p4[1] = (pixel >> 8) & 0xff;
  580.                 p4[2] = (pixel >> 16) & 0xff;
  581.             }
  582.             break;
  583.  
  584.         case 4: //32-bit
  585.             *(Uint32 *)p1 = pixel;
  586.             *(Uint32 *)p2 = pixel;
  587.             *(Uint32 *)p3 = pixel;
  588.             *(Uint32 *)p4 = pixel;
  589.             break;
  590.  
  591.     }
  592.          /* ewentualna aktualizacja obrazu (aka double buffering) */
  593.   }
  594. }
  595.  
  596. SDL_Color getPixel(int x, int y) {
  597.     SDL_Color color ;
  598.     Uint32 col = 0 ;
  599.     if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  600.         //określamy pozycję
  601.         char* pPosition=(char*)screen->pixels ;
  602.         //przesunięcie względem y
  603.         pPosition+=(screen->pitch*y*2) ;
  604.         //przesunięcie względem x
  605.         pPosition+=(screen->format->BytesPerPixel*x*2);
  606.         //kopiujemy dane piksela
  607.         memcpy(&col, pPosition, screen->format->BytesPerPixel);
  608.         //konwertujemy kolor
  609.         SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  610.     }
  611.     return ( color ) ;
  612. }
  613.  
  614. SDL_Color getPixelSurface(int x, int y, SDL_Surface *surface) {
  615.     SDL_Color color ;
  616.     Uint32 col = 0 ;
  617.     //określamy pozycję
  618.     char* pPosition=(char*)surface->pixels ;
  619.     //przesunięcie względem y
  620.     pPosition+=(surface->pitch*y) ;
  621.     //przesunięcie względem x
  622.     pPosition+=(surface->format->BytesPerPixel*x);
  623.     //kopiujemy dane piksela
  624.     memcpy(&col, pPosition, surface->format->BytesPerPixel);
  625.     //konwertujemy kolor
  626.     SDL_GetRGB(col, surface->format, &color.r, &color.g, &color.b);
  627.     return ( color ) ;
  628. }
  629.  
  630. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  631. {
  632.     SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  633.     SDL_Flip(screen);
  634.  
  635. }
  636.  
  637. void ladujBMP(char const* nazwa, int x, int y)
  638. {
  639.     SDL_Surface* bmp = SDL_LoadBMP(nazwa);
  640.     if (!bmp)
  641.     {
  642.         printf("Unable to load bitmap: %s\n", SDL_GetError());
  643.     }
  644.     else
  645.     {
  646.         SDL_Color kolor;
  647.         for (int yy=0; yy<bmp->h; yy++) {
  648.             for (int xx=0; xx<bmp->w; xx++) {
  649.                 kolor = getPixelSurface(xx, yy, bmp);
  650.                 setPixel(xx, yy, kolor.r, kolor.g, kolor.b);
  651.             }
  652.         }
  653.         SDL_FreeSurface(bmp);
  654.         SDL_Flip(screen);
  655.     }
  656.  
  657. }
  658.  
  659.  
  660. int main ( int argc, char** argv )
  661. {
  662.     // console output
  663.     freopen( "CON", "wt", stdout );
  664.     freopen( "CON", "wt", stderr );
  665.  
  666.     // initialize SDL video
  667.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  668.     {
  669.         printf( "Unable to init SDL: %s\n", SDL_GetError() );
  670.         return 1;
  671.     }
  672.  
  673.     // make sure SDL cleans up before exit
  674.     atexit(SDL_Quit);
  675.  
  676.     // create a new window
  677.     screen = SDL_SetVideoMode(width*2, height*2, 32,
  678.                                            SDL_HWSURFACE|SDL_DOUBLEBUF);
  679.     if ( !screen )
  680.     {
  681.         printf("Unable to set video: %s\n", SDL_GetError());
  682.         return 1;
  683.     }
  684.  
  685.     SDL_WM_SetCaption( tytul , NULL );
  686.     // program main loop
  687.     bool done = false;
  688.     while (!done)
  689.     {
  690.         // message processing loop
  691.         SDL_Event event;
  692.         while (SDL_PollEvent(&event))
  693.         {
  694.             // check for messages
  695.             switch (event.type)
  696.             {
  697.                 // exit if the window is closed
  698.             case SDL_QUIT:
  699.                 done = true;
  700.                 break;
  701.  
  702.                 // check for keypresses
  703.             case SDL_KEYDOWN:
  704.                 {
  705.                     // exit if ESCAPE is pressed
  706.                     if (event.key.keysym.sym == SDLK_ESCAPE)
  707.                         done = true;
  708.                     if (event.key.keysym.sym == SDLK_1)
  709.                         Funkcja1();
  710.                     if (event.key.keysym.sym == SDLK_2)
  711.                         Funkcja2();
  712.                     if (event.key.keysym.sym == SDLK_3)
  713.                         Funkcja3();
  714.                     if (event.key.keysym.sym == SDLK_4)
  715.                         Funkcja4();
  716.                     if (event.key.keysym.sym == SDLK_5)
  717.                         Funkcja5();
  718.                     if (event.key.keysym.sym == SDLK_6)
  719.                         Funkcja6();
  720.                     if (event.key.keysym.sym == SDLK_7)
  721.                         Funkcja7();
  722.  
  723.                     if (event.key.keysym.sym == SDLK_a)
  724.                         ladujBMP("obrazek1.bmp", 0, 0);
  725.                     if (event.key.keysym.sym == SDLK_s)
  726.                         ladujBMP("obrazek2.bmp", 0, 0);
  727.                     if (event.key.keysym.sym == SDLK_d)
  728.                         ladujBMP("obrazek3.bmp", 0, 0);
  729.                     if (event.key.keysym.sym == SDLK_z)
  730.                         ladujBMP("obrazek4.bmp", 0, 0);
  731.                     if (event.key.keysym.sym == SDLK_x)
  732.                         ladujBMP("obrazek5.bmp", 0, 0);
  733.                     if (event.key.keysym.sym == SDLK_c)
  734.                         ladujBMP("obrazek6.bmp", 0, 0);
  735.  
  736.                     if (event.key.keysym.sym == SDLK_l)
  737.                         Linia(rand()%width, rand()%height, rand()%width, rand()%height, 100+rand()%155, 100+rand()%155, 100+rand()%155);
  738.                     if (event.key.keysym.sym == SDLK_o)
  739.                         Okrag(rand()%width, rand()%height, rand()%200, 100+rand()%155, 100+rand()%155, 100+rand()%155);
  740.                     if (event.key.keysym.sym == SDLK_e)
  741.                         Elipsa(rand()%width, rand()%height, rand()%200, rand()%200, 100+rand()%155, 100+rand()%155, 100+rand()%155);
  742.                     if (event.key.keysym.sym == SDLK_b)
  743.                         czyscEkran(0, 0, 10);          break;
  744.                      }
  745.             } // end switch
  746.         } // end of message processing
  747.  
  748.     } // end main loop
  749.  
  750.  
  751.     // all is well ;)
  752.     printf("Exited cleanly\n");
  753.     return 0;
  754. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement