Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __cplusplus
- #include <cstdlib>
- #else
- #include <stdlib.h>
- #endif
- #include <SDL/SDL.h>
- #include <math.h>
- #define pi 3.1415926536
- #include <time.h>
- #include <iostream>
- using namespace std;
- SDL_Surface *screen;
- int width = 900;
- int height = 600;
- char const* tytul = "GKiM2019 - Lab 3 - Pabiańczyk Paweł";
- 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();
- void Funkcja7();
- void Funkcja8();
- void Funkcja9();
- void Funkcja1() {
- int szer = width/6;
- for(int x=0; x<width; x++){
- for(int y=0; y<height; y++){
- if(x<szer)
- setPixel(x, y, 0, (1.0*x/szer*255), 255);
- else if (x<2*szer)
- setPixel(x, y, 0, 255, 255-(1.0*(x-szer)/szer*255));
- else if (x<3*szer)
- setPixel(x, y, (1.0*(x-2*szer)/szer*255), 255, 0);
- else if (x<4*szer)
- setPixel(x, y, 255, 255-(1.0*(x-3*szer)/szer*255), 0);
- else if (x<5*szer)
- setPixel(x, y, 255, 0 , (1.0*(x-4*szer)/szer*255));
- else if (x<6*szer)
- setPixel(x, y, 255-(1.0*(x-5*szer)/szer*255), 0 , 255);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja2() {
- SDL_Color kolor;
- int R, G, B;
- int szer = (width/2); //szerokość
- for(int x=0; x<szer; x++){
- for(int y=0; y<height/2; y++){
- kolor = getPixel(x, y);
- R = kolor.r;
- G = kolor.g;
- B = kolor.b;
- if(R<127 && G<127 && B<127)
- setPixel(x+szer, y, 0, 0, 0);
- else if (R>127 && G>127 && B>127)
- setPixel(x+szer, y, 255, 255, 255);
- else if (R>127 && G<127 && B<127)
- setPixel(x+szer, y, 255, 0, 0);
- else if (R<127 && G>127 && B<127)
- setPixel(x+szer, y, 0, 255, 0);
- else if (R<127 && G<127 && B>127)
- setPixel(x+szer, y, 0, 0, 255);
- else if (R>127 && G>127 && B<127)
- setPixel(x+szer, y, 255, 255, 0);
- else if (R>127 && G<127 && B>127)
- setPixel(x+szer, y, 255, 0, 255);
- else if (R<127 && G>127 && B>127)
- setPixel(x+szer, y, 0, 255, 255);
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja3() {
- SDL_Color kolor;
- int barwy[8][3];
- //black
- barwy[0][0] = 0;
- barwy[0][1] = 0;
- barwy[0][2] = 0;
- //white
- barwy[7][0] = 255;
- barwy[7][1] = 255;
- barwy[7][2] = 255;
- //blue
- barwy[1][0] = 170;
- barwy[1][1] = 0;
- barwy[1][2] = 0;
- //red
- barwy[2][0] = 0;
- barwy[2][1] = 170;
- barwy[2][2] = 0;
- //magenta
- barwy[3][0] = 0;
- barwy[3][1] = 0;
- barwy[3][2] = 170;
- //green
- barwy[4][0] = 85;
- barwy[4][1] = 255;
- barwy[4][2] = 85;
- //cyan
- barwy[5][0] = 255;
- barwy[5][1] = 85;
- barwy[5][2] = 85;
- //yellow
- barwy[6][0] = 85;
- barwy[6][1] = 85;
- barwy[6][2] = 255;
- barwy[8][0] = 170;
- barwy[8][1] = 170;
- barwy[8][2] = 170;
- float r=0; //blad tymczasowy
- int rgb[3];
- float blad = 0; //blad ogolny
- int naj = 0;
- for(int x=0; x<width; x++){
- for(int y=0; y<height; y++){
- kolor = getPixel(x, y);
- rgb[0] = kolor.r;
- rgb[1] = kolor.g;
- rgb[2] = kolor.b;
- for(int i=0; i<9; i++){
- for(int j=0; j<3; j++){
- r = r+ abs(barwy[i][j]-rgb[j]);
- }
- if(i=0)
- {
- blad = r;
- naj = i;
- }
- else if (r<blad)
- {
- blad = r;
- naj = i;
- }
- r = 0;
- }
- setPixel(x+width/2, y, barwy[naj][0],barwy[naj][1], barwy[naj][2] );
- }
- }
- SDL_Flip(screen);
- }
- void Funkcja4() {
- for(int x=0; x<width; x++){
- for(int y=0; y<height; y++){
- setPixel(x, y, 255, 0, 255);
- }}
- SDL_Flip(screen);
- }
- void Funkcja5() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja6() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja7() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja8() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja9() {
- //...
- 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 piksela */
- Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
- /* Pobieramy informację ile bajtów zajmuje jeden piksel */
- int bpp = screen->format->BytesPerPixel;
- /* Obliczamy adres piksela */
- Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
- /* Ustawiamy wartość piksela, 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;
- }
- /* ewentualna aktualizacja obrazu (aka double buffering) */
- }
- }
- SDL_Color getPixel (int x, int y) {
- SDL_Color color ;
- Uint32 col = 0 ;
- if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
- //określamy pozycję
- char* pPosition=(char*)screen->pixels ;
- //przesunięcie względem y
- pPosition+=(screen->pitch*y) ;
- //przesunięcie względem x
- pPosition+=(screen->format->BytesPerPixel*x);
- //kopiujemy dane piksela
- memcpy(&col, pPosition, screen->format->BytesPerPixel);
- //konwertujemy kolor
- SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
- }
- return ( color ) ;
- }
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
- {
- SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
- SDL_Flip(screen);
- }
- 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);
- }
- }
- 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_7)
- Funkcja7();
- if (event.key.keysym.sym == SDLK_8)
- Funkcja8();
- if (event.key.keysym.sym == SDLK_9)
- Funkcja9();
- 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_f)
- ladujBMP("obrazek4.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_g)
- ladujBMP("obrazek5.bmp", 0, 0);
- if (event.key.keysym.sym == SDLK_b)
- czyscEkran(0, 0, 10); 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