Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits> // std::numeric_limits<>
  3. #include <algorithm> // std::numeric_limits<>
  4. #include <cmath> // round()
  5. #include <tuple>
  6. #include <unordered_map>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. #ifdef __linux
  13.     #include <SDL/SDL.h>
  14. #else
  15.     #include <SDL.h>
  16. #endif
  17.  
  18. #if __cplusplus < 201103L
  19.     #error "Wymagany C++11!"
  20. #endif // __cplusplus
  21.  
  22.  
  23. SDL_Surface* screen;
  24. const int width = 900, height = 600;
  25. const char* title = "GKiM - Lab 8 - Nazwisko Imie";
  26.  
  27.  
  28. /** Reguly oceniania:
  29. - Za bycie źródłem plagiatu -0,5 punkta. Proszę swoje rozwiązanie zlikwidować z komputera.
  30. - Za plagiat przyznaje za zadanie 0 punktow!!! Dodam, ze mam program antyplagiatowy, zresztą Państwo wiedzą
  31. - Za niewpisanie w pliku z kodem swojego Nazwiska i Imienia powyzej [-0.1 punkta]
  32. - Za wrzucenie mi na pendrive/wyslanie na maila/załadowanie na elfa wiecej niz jednego pliku zrodlowego (*.cpp) [-0.1 punkta]
  33. - Za nienazwanie pliku zrodlowego NazwiskoImie.cpp, gdzie Nazwisko i Imie zastepujemy odpowiednio swoim nazwiskiem i imieniem, prosze nie dawac zadnych podkreslen, spacji i numeru grupy [-0.1 punkta].
  34.  
  35. ***************************************** zajecia 7:
  36. Skalowanie dwukrotne
  37. Zadanie 1: Zaimplementowanie Funkcja1(), aby zaladowany obrazek umieszczala na gorze z lewen z 2-krotnym zmniejszeniem [1p]
  38. Zadanie 2: Zaimplementowanie Funkcja2(), aby zaladowany obrazek umieszczala na gorze z lewej z 2-krotnym powiekszeniem [1p]
  39.  
  40. Dodatkowo: Za ladny, czytelny, czysty, KONSEKWENTNY kod implementowanych funkcji, zaimplementowanych w 100% [+0.5 punkta].
  41. **/
  42.  
  43. void Funkcja1();
  44. void Funkcja2();
  45.  
  46. int initSdl();
  47.  
  48. void loadBMP(const char* nazwa, int x, int y);
  49.  
  50. void clearScreen(Uint8 R=0, Uint8 G=0, Uint8 B=0);
  51.  
  52. int main(int argc, char** argv)
  53. {
  54.     if (initSdl() )
  55.     {
  56.         return -1;
  57.     }
  58.  
  59.     pair<int, int> mousePosition{0, 0};
  60.     // program main loop
  61.     bool done = false;
  62.     while (!done)
  63.     {
  64.         // message processing loop
  65.         SDL_Event event;
  66.         while (SDL_PollEvent(&event))
  67.         {
  68.             if (SDL_QUIT == event.type)
  69.             {
  70.                 done = true;
  71.             }
  72.             else if(SDL_KEYDOWN == event.type)
  73.             {
  74.                 switch(event.key.keysym.sym)
  75.                 {
  76.                 case SDLK_ESCAPE:
  77.                     done = true;
  78.                     break;
  79.                 case SDLK_a:
  80.                     loadBMP("obrazek1.bmp", 0, 0);
  81.                     break;
  82.                 case SDLK_s:
  83.                     loadBMP("obrazek2.bmp", 0, 0);
  84.                     break;
  85.                 case SDLK_d:
  86.                     loadBMP("obrazek3.bmp", 0, 0);
  87.                     break;
  88.                 case SDLK_f: // zrodlo ponizszych obrazkow: https://pl.wikipedia.org/wiki/Algorytm_Floyda-Steinberga
  89.                     loadBMP("obrazek4.bmp", 0, 0);
  90.                     break;
  91.                 case SDLK_g:
  92.                     loadBMP("obrazek5.bmp", 0, 0);
  93.                     break;
  94.  
  95.                 case SDLK_b:
  96.                     clearScreen(0, 0, 0);
  97.                     break;
  98.  
  99.                 case SDLK_1:
  100.                     Funkcja1();
  101.                     break;
  102.                 case SDLK_2:
  103.                     Funkcja2();
  104.                     break;
  105.                 }
  106.             }
  107.             if(event.type == SDL_MOUSEMOTION)
  108.             {
  109.                 mousePosition = make_pair(event.motion.x, event.motion.y);
  110.             }
  111.             if(event.type == SDL_MOUSEBUTTONDOWN)
  112.             {
  113.                 cout << "Kliknieto: (" << mousePosition.first << ", " << mousePosition.second << ")\n";
  114.             }
  115.         } // end of message processing
  116.     } // end main loop
  117.     return 0;
  118. }
  119.  
  120. #ifdef _WIN32
  121. // to solve problem: https://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16
  122. int WinMain()
  123. {
  124.     return main(1, NULL);
  125. }
  126. #endif // _WIN32
  127.  
  128. int initSdl()
  129. {
  130.     if (SDL_Init( SDL_INIT_VIDEO ) < 0)
  131.     {
  132.         fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
  133.         return 1;
  134.     }
  135.  
  136.     atexit(SDL_Quit);
  137.  
  138.     const int bitDepth = 32;
  139.  
  140.     /// create a new window
  141.     screen = SDL_SetVideoMode(width, height, bitDepth, SDL_HWSURFACE | SDL_DOUBLEBUF);
  142.     if ( !screen )
  143.     {
  144.         printf("Unable to set video: %s\n", SDL_GetError());
  145.         return 1;
  146.     }
  147.  
  148.     SDL_WM_SetCaption(title, NULL);
  149.  
  150.     return 0;
  151. }
  152.  
  153. void loadBMP(char const* nazwa, int x, int y)
  154. {
  155.     SDL_Surface* bmp = SDL_LoadBMP(nazwa);
  156.     if (!bmp)
  157.     {
  158.         printf("Unable to load bitmap: %s\n", SDL_GetError());
  159.     }
  160.     else
  161.     {
  162.         SDL_Rect dstrect;
  163.         dstrect.x = x;
  164.         dstrect.y = y;
  165.         SDL_BlitSurface(bmp, 0, screen, &dstrect);
  166.         SDL_Flip(screen);
  167.         SDL_FreeSurface(bmp);
  168.     }
  169. }
  170.  
  171. void clearScreen(Uint8 R, Uint8 G, Uint8 B)
  172. {
  173.     SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  174.     SDL_Flip(screen);
  175. }
  176.  
  177. inline bool inScreen(int x, int y)
  178. {
  179.     return x >= 0 && x < screen->w && y >= 0 && y < screen->h;
  180. }
  181.  
  182. Uint8* getPixelAddress(int x, int y)
  183. {
  184.     if (inScreen(x, y))
  185.     {
  186.         /* Pobieramy informacji ile bajtów zajmuje jeden pixel */
  187.         const int bpp = screen->format->BytesPerPixel;
  188.  
  189.         /* Obliczamy adres pixela */
  190.         return (Uint8*)screen->pixels + y * screen->pitch + x * bpp;
  191.     }
  192.     return NULL;
  193. }
  194.  
  195. SDL_Color getPixel(int x, int y)
  196. {
  197.     Uint8* pixelAddress = getPixelAddress(x, y);
  198.  
  199.     SDL_Color color = {};
  200.     if (pixelAddress)
  201.     {
  202.         Uint32 col = 0;
  203.  
  204.         memcpy(&col, pixelAddress, screen->format->BytesPerPixel);
  205.         SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  206.     }
  207.  
  208.     return color;
  209. }
  210.  
  211. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  212. {
  213.     Uint8* pixelAddress = getPixelAddress(x, y);
  214.     if (pixelAddress)
  215.     {
  216.         Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  217.  
  218.         switch(screen->format->BytesPerPixel)
  219.         {
  220.         case 1: //8-bit
  221.             *pixelAddress = pixel;
  222.             break;
  223.  
  224.         case 2: //16-bit
  225.             *(Uint16*)pixelAddress = pixel;
  226.             break;
  227.  
  228.         case 3: //24-bit
  229.             if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  230.             {
  231.                 pixelAddress[0] = (pixel >> 16) & 0xff;
  232.                 pixelAddress[1] = (pixel >> 8) & 0xff;
  233.                 pixelAddress[2] =  pixel & 0xff;
  234.             }
  235.             else
  236.             {
  237.                 pixelAddress[0] =  pixel & 0xff;
  238.                 pixelAddress[1] = (pixel >> 8) & 0xff;
  239.                 pixelAddress[2] = (pixel >> 16) & 0xff;
  240.             }
  241.             break;
  242.         case 4: //32-bit
  243.             *(Uint32*)pixelAddress = pixel;
  244.             break;
  245.         }
  246.     }
  247.     /* update the screen (aka double buffering) */
  248. }
  249.  
  250. void setPixel(int x, int y, SDL_Color color)
  251. {
  252.     setPixel(x, y, color.r, color.g, color.b);
  253. }
  254.  
  255.  
  256. ////////////////////////////////////////////////////////////////////////////////////////////////////////////// TODO:
  257.  
  258. void Funkcja1() // zmniejszanie, w trakcie pisania
  259. {
  260.     constexpr auto halveW = width / 2;
  261.     constexpr auto halveH = height / 2;
  262.  
  263.     SDL_Color bufor[halveW/2][halveH/2];
  264.  
  265.     for (int x=0; x<halveW; x+=2)
  266.     {
  267.         for (int y=0; y<halveH; y+=2)
  268.         {
  269.             SDL_Color color1 = getPixel(x, y);
  270.             SDL_Color color2 = getPixel(x+1, y);
  271.             SDL_Color color3 = getPixel(x, y+1);
  272.             SDL_Color color4 = getPixel(x+1, y+1);
  273.  
  274.             int sumR = color1.r+color2.r+color3.r+color4.r;
  275.             int sumG = color1.g+color2.g+color3.g+color4.g;
  276.             int sumB = color1.b+color2.b+color3.b+color4.b;
  277.  
  278.             int newR = sumR/4;
  279.             int newG = sumG/4;
  280.             int newB = sumB/4;
  281.  
  282.             SDL_Color newColor = {newR, newG, newB};
  283.             bufor[x/2][y/2] = newColor;
  284.         }
  285.     }
  286.  
  287.     clearScreen(0, 0, 0);
  288.  
  289.     for(int i = 0; i < halveW/2; i++)
  290.     {
  291.         for(int j = 0; j < halveH/2; j++)
  292.         {
  293.             setPixel(i, j, bufor[i][j]);
  294.         }
  295.     }
  296.  
  297.  
  298.     SDL_Flip(screen);
  299. }
  300.  
  301. void Funkcja2() // zwiekszanie
  302. {
  303.     constexpr auto halveW = width / 2;
  304.     constexpr auto halveH = height / 2;
  305.  
  306.     SDL_Color bufor[width][height];
  307.  
  308.     for (int x=0; x<halveW; ++x)
  309.     {
  310.         for (int y=0; y<halveH; ++y)
  311.         {
  312.             SDL_Color currentColor = getPixel(x, y);
  313.  
  314.             bufor[2*x][2*y]     = currentColor;
  315.             bufor[2*x+1][2*y]   = currentColor;
  316.             bufor[2*x][2*y+1]   = currentColor;
  317.             bufor[2*x+1][2*y+1] = currentColor;
  318.         }
  319.     }
  320.  
  321.     for (int i = 0; i < width; i++)
  322.     {
  323.         for (int j = 0; j < height; j++)
  324.         {
  325.             setPixel(i, j, bufor[i][j]);
  326.         }
  327.     }
  328.  
  329.     SDL_Flip(screen);
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement