Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __cplusplus
- #include <cstdlib>
- #else
- #include <stdlib.h>
- #endif
- #ifdef __APPLE__
- #include <SDL/SDL.h>
- #else
- #include <SDL.h>
- #endif
- #include <math.h>
- #define pi 3.14
- #include <time.h>
- #include <iostream>
- using namespace std;
- SDL_Surface *screen;
- int width = 900;
- int height = 600;
- char const* tytul = "GKiM - Lab 1 - Konrad Krukar";
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
- SDL_Color getPixel (int x, int y);
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
- void Funkcja1();
- void Funkcja2();
- void Funkcja3();
- void Funkcja4();
- void Funkcja5();
- void Funkcja6();
- //FUNKCJA 0
- void Funkcja1() {
- for(int i=0; i<450; i++)
- {
- for(int j=0; j<300;j++)
- {
- SDL_Color kolor = getPixel(i,j);
- setPixel(i+450,j,kolor.r,0,0);
- setPixel(i,j+300,0,kolor.g,0);
- setPixel(i+450,j+300,0,0,kolor.b);
- }
- }
- SDL_Flip(screen);
- }
- //FUNKCJA 1
- void Funkcja2() {
- for(int i=0; i<450; i++)
- {
- for(int j=0; j<300;j++)
- {
- SDL_Color kolor = getPixel(i,j);
- setPixel(i+450,j,255-kolor.r,255-kolor.g,255-kolor.b);
- //TO SAMO CO WYZEJ
- setPixel(i,j+300,255-kolor.r,255-kolor.g,255-kolor.b);
- //OTRZYMUJEMY TEN SAM OBRAZEK CO NA POCZATKU
- setPixel(i+450,j+300,kolor.r,kolor.g,kolor.b);
- }
- }
- SDL_Flip(screen);
- }
- //FUNKCJA 2
- void Funkcja3() {
- for(int i=0; i<450; i++)
- {
- for(int j=0; j<300;j++)
- {
- SDL_Color kolor = getPixel(i,j);
- float c = 1 - (kolor.r/255);
- float m = 1 - (kolor.g/255);
- float y = 1 - (kolor.b/255);
- float k=c;
- if (k>m) k=m;
- else k=c;
- if(k>y) k=y;
- float cc = (c-k)/(1-k);
- float mm = (m-k)/(1-k);
- float yy = (y-k)/(1-k);
- int sredRGB = (kolor.r + kolor.g + kolor.b)/3;
- float sredCMY = (cc+mm+yy)/3;
- //int dobra = ((kolor.r*0.2126) + (kolor.g*0.7152) + (kolor.b*0.0722));
- setPixel(i+450,j,sredRGB,sredRGB,sredRGB);
- //setPixel(i,j+300,dobra,dobra,dobra);
- setPixel(i,j+300,sredCMY*255,sredCMY*255,sredCMY*255);
- setPixel(i+450,j+300,k*255,k*255,k*255);
- //setPixel(i+450,j+300);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja4() {
- SDL_Flip(screen);
- }
- void Funkcja5() {
- for(int i=0; i<450; i++)
- {
- for(int j=0; j<300;j++)
- {
- SDL_Color kolor = getPixel(i,j);
- setPixel(i+450,j,kolor.g,kolor.r,kolor.b);
- setPixel(i,j+300,kolor.r,kolor.b,kolor.g);
- setPixel(i+450,j+300,kolor.b,kolor.g,kolor.r);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja6() {
- for(int i=0; i<450; i++)
- {
- for(int j=0; j<300;j++)
- {
- SDL_Color kolor = getPixel(i,j);
- setPixel(i+450,j,kolor.r,kolor.g,0);
- setPixel(i,j+300,kolor.r,0,kolor.b);
- setPixel(i+450,j+300,0,kolor.g,kolor.b);
- }
- }
- SDL_Flip(screen);
- }
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
- {
- if ((x>=0) && (x<width) && (y>=0) && (y<height))
- {
- /* Zamieniamy poszczególne sk³adowe koloru na format koloru pixela */
- Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
- /* Pobieramy informacji ile bajtów zajmuje jeden pixel */
- int bpp = screen->format->BytesPerPixel;
- /* Obliczamy adres pixela */
- Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
- /* Ustawiamy wartoœæ pixela, w zale¿noœci od formatu powierzchni*/
- switch(bpp)
- {
- case 1: //8-bit
- *p = pixel;
- break;
- case 2: //16-bit
- *(Uint16 *)p = pixel;
- break;
- case 3: //24-bit
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
- p[0] = (pixel >> 16) & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = pixel & 0xff;
- } else {
- p[0] = pixel & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = (pixel >> 16) & 0xff;
- }
- break;
- case 4: //32-bit
- *(Uint32 *)p = pixel;
- break;
- }
- /* update the screen (aka double buffering) */
- }
- }
- void ladujBMP(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 czyscEkran(Uint8 R, Uint8 G, Uint8 B)
- {
- SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
- SDL_Flip(screen);
- }
- SDL_Color getPixel (int x, int y) {
- SDL_Color color ;
- Uint32 col = 0 ;
- if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
- //determine position
- char* pPosition=(char*)screen->pixels ;
- //offset by y
- pPosition+=(screen->pitch*y) ;
- //offset by x
- pPosition+=(screen->format->BytesPerPixel*x);
- //copy pixel data
- memcpy(&col, pPosition, screen->format->BytesPerPixel);
- //convert color
- SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
- }
- return ( color ) ;
- }
- int main ( int argc, char** argv )
- {
- // console output
- freopen( "CON", "wt", stdout );
- freopen( "CON", "wt", stderr );
- // initialize SDL video
- if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
- {
- printf( "Unable to init SDL: %s\n", SDL_GetError() );
- return 1;
- }
- // make sure SDL cleans up before exit
- atexit(SDL_Quit);
- // create a new window
- screen = SDL_SetVideoMode(width, height, 32,
- SDL_HWSURFACE|SDL_DOUBLEBUF);
- if ( !screen )
- {
- printf("Unable to set video: %s\n", SDL_GetError());
- return 1;
- }
- SDL_WM_SetCaption( tytul , NULL );
- // program main loop
- bool done = false;
- while (!done)
- {
- // message processing loop
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- // check for messages
- switch (event.type)
- {
- // exit if the window is closed
- case SDL_QUIT:
- done = true;
- break;
- // check for keypresses
- case SDL_KEYDOWN:
- {
- // exit if ESCAPE is pressed
- if (event.key.keysym.sym == SDLK_ESCAPE)
- done = true;
- if (event.key.keysym.sym == SDLK_1)
- Funkcja1();
- if (event.key.keysym.sym == SDLK_2)
- Funkcja2();
- if (event.key.keysym.sym == SDLK_3)
- Funkcja3();
- if (event.key.keysym.sym == SDLK_4)
- Funkcja4();
- if (event.key.keysym.sym == SDLK_5)
- Funkcja5();
- if (event.key.keysym.sym == SDLK_6)
- Funkcja6();
- if (event.key.keysym.sym == SDLK_a)
- ladujBMP("obrazek1.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_s)
- ladujBMP("obrazek2.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_d)
- ladujBMP("obrazek3.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_b)
- czyscEkran(0, 0, 0); break;
- }
- } // end switch
- } // end of message processing
- } // end main loop
- // all is well ;)
- printf("Exited cleanly\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement