Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits> // std::numeric_limits<>
- #include <algorithm> // std::numeric_limits<>
- #include <cmath> // round()
- #include <tuple>
- #include <unordered_map>
- #include <ctime>
- using namespace std;
- #ifdef __linux
- #include <SDL/SDL.h>
- #else
- #include <SDL.h>
- #endif
- #if __cplusplus < 201103L
- #error "Wymagany C++11!"
- #endif // __cplusplus
- SDL_Surface* screen;
- const int width = 900, height = 600;
- const char* title = "GKiM - Lab 8 - Nazwisko Imie";
- /** Reguly oceniania:
- - Za bycie źródłem plagiatu -0,5 punkta. Proszę swoje rozwiązanie zlikwidować z komputera.
- - Za plagiat przyznaje za zadanie 0 punktow!!! Dodam, ze mam program antyplagiatowy, zresztą Państwo wiedzą
- - Za niewpisanie w pliku z kodem swojego Nazwiska i Imienia powyzej [-0.1 punkta]
- - Za wrzucenie mi na pendrive/wyslanie na maila/załadowanie na elfa wiecej niz jednego pliku zrodlowego (*.cpp) [-0.1 punkta]
- - 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].
- ***************************************** zajecia 7:
- Skalowanie dwukrotne
- Zadanie 1: Zaimplementowanie Funkcja1(), aby zaladowany obrazek umieszczala na gorze z lewen z 2-krotnym zmniejszeniem [1p]
- Zadanie 2: Zaimplementowanie Funkcja2(), aby zaladowany obrazek umieszczala na gorze z lewej z 2-krotnym powiekszeniem [1p]
- Dodatkowo: Za ladny, czytelny, czysty, KONSEKWENTNY kod implementowanych funkcji, zaimplementowanych w 100% [+0.5 punkta].
- **/
- void Funkcja1();
- void Funkcja2();
- int initSdl();
- void loadBMP(const char* nazwa, int x, int y);
- void clearScreen(Uint8 R=0, Uint8 G=0, Uint8 B=0);
- int main(int argc, char** argv)
- {
- if (initSdl() )
- {
- return -1;
- }
- pair<int, int> mousePosition{0, 0};
- // program main loop
- bool done = false;
- while (!done)
- {
- // message processing loop
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- if (SDL_QUIT == event.type)
- {
- done = true;
- }
- else if(SDL_KEYDOWN == event.type)
- {
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- done = true;
- break;
- case SDLK_a:
- loadBMP("obrazek1.bmp", 0, 0);
- break;
- case SDLK_s:
- loadBMP("obrazek2.bmp", 0, 0);
- break;
- case SDLK_d:
- loadBMP("obrazek3.bmp", 0, 0);
- break;
- case SDLK_f: // zrodlo ponizszych obrazkow: https://pl.wikipedia.org/wiki/Algorytm_Floyda-Steinberga
- loadBMP("obrazek4.bmp", 0, 0);
- break;
- case SDLK_g:
- loadBMP("obrazek5.bmp", 0, 0);
- break;
- case SDLK_b:
- clearScreen(0, 0, 0);
- break;
- case SDLK_1:
- Funkcja1();
- break;
- case SDLK_2:
- Funkcja2();
- break;
- }
- }
- if(event.type == SDL_MOUSEMOTION)
- {
- mousePosition = make_pair(event.motion.x, event.motion.y);
- }
- if(event.type == SDL_MOUSEBUTTONDOWN)
- {
- cout << "Kliknieto: (" << mousePosition.first << ", " << mousePosition.second << ")\n";
- }
- } // end of message processing
- } // end main loop
- return 0;
- }
- #ifdef _WIN32
- // to solve problem: https://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16
- int WinMain()
- {
- return main(1, NULL);
- }
- #endif // _WIN32
- int initSdl()
- {
- if (SDL_Init( SDL_INIT_VIDEO ) < 0)
- {
- fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
- return 1;
- }
- atexit(SDL_Quit);
- const int bitDepth = 32;
- /// create a new window
- screen = SDL_SetVideoMode(width, height, bitDepth, SDL_HWSURFACE | SDL_DOUBLEBUF);
- if ( !screen )
- {
- printf("Unable to set video: %s\n", SDL_GetError());
- return 1;
- }
- SDL_WM_SetCaption(title, NULL);
- return 0;
- }
- void loadBMP(char const* nazwa, int x, int y)
- {
- SDL_Surface* bmp = SDL_LoadBMP(nazwa);
- if (!bmp)
- {
- printf("Unable to load bitmap: %s\n", SDL_GetError());
- }
- else
- {
- SDL_Rect dstrect;
- dstrect.x = x;
- dstrect.y = y;
- SDL_BlitSurface(bmp, 0, screen, &dstrect);
- SDL_Flip(screen);
- SDL_FreeSurface(bmp);
- }
- }
- void clearScreen(Uint8 R, Uint8 G, Uint8 B)
- {
- SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
- SDL_Flip(screen);
- }
- inline bool inScreen(int x, int y)
- {
- return x >= 0 && x < screen->w && y >= 0 && y < screen->h;
- }
- Uint8* getPixelAddress(int x, int y)
- {
- if (inScreen(x, y))
- {
- /* Pobieramy informacji ile bajtów zajmuje jeden pixel */
- const int bpp = screen->format->BytesPerPixel;
- /* Obliczamy adres pixela */
- return (Uint8*)screen->pixels + y * screen->pitch + x * bpp;
- }
- return NULL;
- }
- SDL_Color getPixel(int x, int y)
- {
- Uint8* pixelAddress = getPixelAddress(x, y);
- SDL_Color color = {};
- if (pixelAddress)
- {
- Uint32 col = 0;
- memcpy(&col, pixelAddress, screen->format->BytesPerPixel);
- SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
- }
- return color;
- }
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
- {
- Uint8* pixelAddress = getPixelAddress(x, y);
- if (pixelAddress)
- {
- Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
- switch(screen->format->BytesPerPixel)
- {
- case 1: //8-bit
- *pixelAddress = pixel;
- break;
- case 2: //16-bit
- *(Uint16*)pixelAddress = pixel;
- break;
- case 3: //24-bit
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
- {
- pixelAddress[0] = (pixel >> 16) & 0xff;
- pixelAddress[1] = (pixel >> 8) & 0xff;
- pixelAddress[2] = pixel & 0xff;
- }
- else
- {
- pixelAddress[0] = pixel & 0xff;
- pixelAddress[1] = (pixel >> 8) & 0xff;
- pixelAddress[2] = (pixel >> 16) & 0xff;
- }
- break;
- case 4: //32-bit
- *(Uint32*)pixelAddress = pixel;
- break;
- }
- }
- /* update the screen (aka double buffering) */
- }
- void setPixel(int x, int y, SDL_Color color)
- {
- setPixel(x, y, color.r, color.g, color.b);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////// TODO:
- void Funkcja1() // zmniejszanie, w trakcie pisania
- {
- constexpr auto halveW = width / 2;
- constexpr auto halveH = height / 2;
- SDL_Color bufor[halveW/2][halveH/2];
- for (int x=0; x<halveW; x+=2)
- {
- for (int y=0; y<halveH; y+=2)
- {
- SDL_Color color1 = getPixel(x, y);
- SDL_Color color2 = getPixel(x+1, y);
- SDL_Color color3 = getPixel(x, y+1);
- SDL_Color color4 = getPixel(x+1, y+1);
- int sumR = color1.r+color2.r+color3.r+color4.r;
- int sumG = color1.g+color2.g+color3.g+color4.g;
- int sumB = color1.b+color2.b+color3.b+color4.b;
- int newR = sumR/4;
- int newG = sumG/4;
- int newB = sumB/4;
- SDL_Color newColor = {newR, newG, newB};
- bufor[x/2][y/2] = newColor;
- }
- }
- clearScreen(0, 0, 0);
- for(int i = 0; i < halveW/2; i++)
- {
- for(int j = 0; j < halveH/2; j++)
- {
- setPixel(i, j, bufor[i][j]);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja2() // zwiekszanie
- {
- constexpr auto halveW = width / 2;
- constexpr auto halveH = height / 2;
- SDL_Color bufor[width][height];
- for (int x=0; x<halveW; ++x)
- {
- for (int y=0; y<halveH; ++y)
- {
- SDL_Color currentColor = getPixel(x, y);
- bufor[2*x][2*y] = currentColor;
- bufor[2*x+1][2*y] = currentColor;
- bufor[2*x][2*y+1] = currentColor;
- bufor[2*x+1][2*y+1] = currentColor;
- }
- }
- for (int i = 0; i < width; i++)
- {
- for (int j = 0; j < height; j++)
- {
- setPixel(i, j, bufor[i][j]);
- }
- }
- SDL_Flip(screen);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement