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 5 - Morawski Mariusz";
- struct linia {
- int xp;
- int yp;
- int xk;
- int yk;
- };
- linia wzorzec[] = {
- {0,0,0,20},
- {0,20,10,20},
- {10,20,10,50},
- {10,50,30,50},
- {30,50,30,20},
- {30,20,50,20},
- {50,20,30,0},
- {30,0,0,0},
- };
- linia mlotek[8];
- 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 Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
- void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
- void Elipsa(int x, int y, int a, int b, 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 inicjalizacja(){
- for(int i=0;i<8;i++){
- mlotek[i]=wzorzec[i];
- }
- }
- void rysowanie(Uint8 R, Uint8 G, Uint8 B){
- for(int i=0;i<8;i++){
- Linia(mlotek[i].xp,mlotek[i].yp,
- mlotek[i].xk,mlotek[i].yk,R,G,B);
- }
- }
- void Funkcja1() {
- inicjalizacja();
- rysowanie(255,255,255);
- SDL_Flip(screen);
- }
- void przesuniecie(int px, int py){
- for(int i=0;i<8;i++){
- mlotek[i].xp += px;
- mlotek[i].xk += px;
- mlotek[i].yp += py;
- mlotek[i].yk += py;
- }
- }
- void Funkcja2() {
- przesuniecie(20,10);
- rysowanie(255,255,0);
- SDL_Flip(screen);
- }
- void skalowanie(float sx, float sy){
- int x= mlotek[0].xp; int y = mlotek[0].yp;
- przesuniecie(-x,-y);
- for(int i=0;i<8;i++){
- mlotek[i].xp *= sx;
- mlotek[i].xk *= sx;
- mlotek[i].yp *= sy;
- mlotek[i].yk *= sy;
- }
- przesuniecie(x,y);
- }
- void Funkcja3() {
- skalowanie(1.2,1.2);
- rysowanie(255,0,0);
- SDL_Flip(screen);
- }
- bool operator !=(const SDL_Color& kolor1, const SDL_Color& kolor2){
- if(kolor1.r!=kolor2.r) return true;
- if(kolor1.g!=kolor2.g) return true;
- if(kolor1.b!=kolor2.b) return true;
- return false;
- }
- void sianie(int x, int y, SDL_Color brzeg, SDL_Color wypelnienie){
- setPixel(x,y,wypelnienie.r,wypelnienie.g,wypelnienie.b);
- SDL_Color kolor;
- kolor = getPixel(x-1,y);
- if(kolor!= brzeg && kolor!=wypelnienie) sianie(x-1,y,brzeg,wypelnienie);
- kolor = getPixel(x,y-1);
- if(kolor!= brzeg && kolor!=wypelnienie) sianie(x,y-1,brzeg,wypelnienie);
- kolor = getPixel(x,y+1);
- if(kolor!= brzeg && kolor!=wypelnienie) sianie(x,y+1,brzeg,wypelnienie);
- kolor = getPixel(x+1,y);
- if(kolor!= brzeg && kolor!=wypelnienie) sianie(x+1,y,brzeg,wypelnienie);
- }
- void Funkcja4() {
- SDL_Color brzeg = {255,255,255};
- SDL_Color wypelnienie={.r=255,.g=0,.b=255};
- Okrag(300,300,50,brzeg.r,brzeg.g,brzeg.b);
- sianie(300,300,brzeg, wypelnienie);
- SDL_Flip(screen);
- }
- void Funkcja5() {
- Linia(rand()%width, rand()%height,
- rand()%width, rand()%height, rand()%255,rand()%255,rand()%255);
- SDL_Flip(screen);
- }
- bool operator ==(const SDL_Color& kolor1, const SDL_Color& kolor2){
- if(kolor1.r!=kolor2.r) return false;
- if(kolor1.g!=kolor2.g) return false;
- if(kolor1.b!=kolor2.b) return false;
- return true;
- }
- void sianie2(int x, int y, SDL_Color wypelnienie, SDL_Color tlo){
- setPixel(x,y,wypelnienie.r,wypelnienie.g,wypelnienie.b);
- SDL_Color kolor;
- kolor = getPixel(x-1,y);
- if(kolor== tlo && kolor!=wypelnienie) sianie2(x-1,y,wypelnienie,tlo);
- kolor = getPixel(x,y-1);
- if(kolor== tlo && kolor!=wypelnienie) sianie2(x,y-1,wypelnienie,tlo);
- kolor = getPixel(x,y+1);
- if(kolor== tlo && kolor!=wypelnienie) sianie2(x,y+1,wypelnienie,tlo);
- kolor = getPixel(x+1,y);
- if(kolor== tlo && kolor!=wypelnienie) sianie2(x+1,y,wypelnienie,tlo);
- }
- void Funkcja6() {
- SDL_Color tlo = getPixel(600,300);
- SDL_Color wypelnienie={.r=255,.g=255,.b=0};
- Okrag(600,300,50,255,255,255);
- sianie2(600,300,wypelnienie, tlo);
- SDL_Flip(screen);
- }
- void Funkcja7() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja8() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja9() {
- //...
- SDL_Flip(screen);
- }
- void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x1,y1,R,G,B);
- setPixel(x2,y2,R,G,B);
- // zmienne pomocnicze
- int d, dx, dy, ai, bi, xi, yi;
- int x = x1, y = y1;
- // ustalenie kierunku rysowania
- if (x1 < x2)
- {
- xi = 1;
- dx = x2 - x1;
- }
- else
- {
- xi = -1;
- dx = x1 - x2;
- }
- // ustalenie kierunku rysowania
- if (y1 < y2)
- {
- yi = 1;
- dy = y2 - y1;
- }
- else
- {
- yi = -1;
- dy = y1 - y2;
- }
- // pierwszy piksel
- setPixel(x, y, R, G, B);
- // oś wiodąca OX
- if (dx > dy)
- {
- ai = (dy - dx) * 2;
- bi = dy * 2;
- d = bi - dx;
- // pętla po kolejnych x
- while (x != x2)
- {
- // test współczynnika
- if (d >= 0)
- {
- x += xi;
- y += yi;
- d += ai;
- }
- else
- {
- d += bi;
- x += xi;
- }
- setPixel(x, y, R, G, B);
- }
- }
- // oś wiodąca OY
- else
- {
- ai = ( dx - dy ) * 2;
- bi = dx * 2;
- d = bi - dy;
- // pętla po kolejnych y
- while (y != y2)
- {
- // test współczynnika
- if (d >= 0)
- {
- x += xi;
- y += yi;
- d += ai;
- }
- else
- {
- d += bi;
- y += yi;
- }
- setPixel(x, y, R, G, B);
- }
- }
- SDL_Flip(screen);
- }
- void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
- setPixel(x+x0, y+y0, R, G, B);
- setPixel(y+x0, x+y0, R, G, B);
- setPixel(y+x0, -x+y0, R, G, B);
- setPixel(x+x0, -y+y0, R, G, B);
- setPixel(-x+x0, -y+y0, R, G, B);
- setPixel(-y+x0, -x+y0, R, G, B);
- setPixel(-y+x0, x+y0, R, G, B);
- setPixel(-x+x0, y+y0, R, G, B);;
- }
- void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
- int x =0;
- int y=r;
- int d = 3-2*r;
- while (x <= y){
- if (d<0){
- d=d+4*x+6;
- RysujOkrag(x0, y0, x, y, R, G, B);
- } else{
- d=d+4*(x-y)+10;
- y--;
- RysujOkrag(x0, y0, x, y, R, G, B);
- }
- x++;
- }
- SDL_Flip(screen);
- }
- void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
- int xc = 0,
- yc = b;
- int aa = a * a,
- aa2 = aa + aa,
- bb = b * b,
- bb2 = bb + bb;
- int d = bb - aa * b + (aa / 4),
- dx = 0,
- dy = aa2 * b;
- while (dx < dy) {
- setPixel (x - xc,y - yc, R, G, B);
- setPixel (x - xc,y + yc, R, G, B);
- setPixel (x + xc,y - yc, R, G, B);
- setPixel (x + xc,y + yc, R, G, B);
- if (d > 0){
- yc = yc-1;
- dy -= aa2;
- d -= dy;
- }
- xc =xc+1;
- dx += bb2;
- d += bb + dx;
- }
- d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
- while (yc >= 0) {
- setPixel (x - xc,y - yc, R, G, B);
- setPixel (x - xc,y + yc, R, G, B);
- setPixel (x + xc,y - yc, R, G, B);
- setPixel (x + xc,y + yc, R, G, B);
- if (d < 0) {
- xc =xc+1;
- dx += bb2;
- d += (bb + dx);
- }
- yc = yc-1;
- dy -= aa2;
- d += aa - dy;
- }
- 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);
- }
- 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_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