GieeF

Untitled

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