Advertisement
GieeF

Untitled

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