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 4 - 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 linia1(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
- void linia2(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
- void linia3(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 linia1(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x1, y1, 255, 0, 0);
- setPixel(x2, y2, 255, 0, 192);
- int dx = x2;
- int dy = y2; //przyrost x i y
- int d = 0; //kąt
- int x = 0; //punkty początkowy x
- int y = 0; //punkt poczatkowy y
- if(dx>=dy){
- //jeżeli dx jest większy lub równy dy - linia jest jest pod kątem od 0 do 45 stopni względem góry
- d = 2*dy - dx;
- setPixel(x, y, 255, 255, 255);
- while(x<x2){
- if(d> 0){
- d = d + 2*(dy - dx);
- x++;
- y++;
- }
- else{
- d = d + 2*dy;
- x++;
- }
- setPixel(x, y, 255, 255, 255);
- }
- }
- else{
- //jeżeli dx jest mniejszy od dy - kąt jest większy niż 45 stopni wzgledem góry
- d = 2*dx -dy;
- setPixel(x, y, 255, 0, 0);
- while(y<y2){
- if(d>=0){
- d = d + 2*(dx-dy);
- y++;
- x++;
- }
- else{
- d = d +2*dx;
- y++;
- }
- setPixel(x, y, 255, 255, 0);
- }
- }
- SDL_Flip(screen);
- }
- void linia2(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x1, y1, 0, 255, 0);
- setPixel(x2, y2, 0, 255, 192);
- int dx = x2 -x1;
- int dy = y2 -y1; //przyrost x i y
- int d =0;
- int x = x1;
- int y = y1;
- if(dx>=dy){
- d = 2*dy - dy;
- setPixel(x, y, 255, 255, 255);
- while(x<x2){
- if(d> 0){
- d = d + 2*(dy - dx);
- x++;
- y++;
- }
- else{
- d = d + 2*dy;
- x++;
- }
- setPixel(x, y, 255, 255, 255);
- }
- }
- else{
- d = 2*dx -dy;
- setPixel(x, y, 255, 0, 0);
- while(y<y2){
- if(d>=0){
- d = d + 2*(dx-dy);
- y++;
- x++;
- }
- else{
- d = d +2*dx;
- y++;
- }
- setPixel(x, y, 255, 255, 0);
- }
- }
- SDL_Flip(screen);
- }
- void linia3(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x1, y1, 0, 0, 255);
- setPixel(x2, y2, 192, 0, 255);
- int dx = x2 -x1;
- int dy = y2 -y1; //przyrost x i y
- int kx = 1; //kierunek rysowania x
- int ky = 1; //kierunek rysowania y
- R =255;
- G =255;
- B =255;
- if(dx<0){
- dx = x1 - x2;
- kx = -1; //zmiana kierunku
- R =0;
- }
- if(dy<0){
- dy = y1 - y2;
- ky = -1; //zmiana kierunku
- G=0;
- }
- int d =0;
- int x = x1;
- int y = y1;
- if(dx>=dy){
- d = 2*dy - dy;
- setPixel(x, y, R, G, B);
- while(x!=x2){
- if(d>=0){
- d = d + 2*(dy - dx);
- x+=kx; //jeżeli x1 < x2 to dodajemy wartości, zaś jeżeli jest większy to odejmujemy
- y+=ky;
- }
- else{
- d = d + 2*dy;
- x+=kx;
- }
- setPixel(x, y, R, G, B);
- }
- }
- else{
- d = 2*dx -dy;
- setPixel(x, y, 255, 0, 0);
- while(y!=y2){
- if(d>=0){
- d = d + 2*(dx-dy);
- y+=ky;
- x+=kx;
- }
- else{
- d = d +2*dx;
- y+=ky;
- }
- setPixel(x, y, R, G, B);
- }
- }
- SDL_Flip(screen);
- }
- void okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B) {
- //wyznaczanie 5 punktów (środka koła oraz 4 puktów znajdujących się o promieć od środka
- /*
- setPixel(x, y, 255, 0, 0);
- setPixel(x+r, y, 255, 0, 192);
- setPixel(x-r, y, 255, 0, 192);
- setPixel(x, y+r, 255, 0, 192);
- setPixel(x, y-r, 255, 0, 192);
- */
- int xx = 0;
- int yy = r;
- //rysowanie 4 punktów o promień odległych od środka
- setPixel(x+xx, y+yy, 0, 255, 255);
- setPixel(x-xx, y+yy, 0, 255, 255);
- setPixel(x+xx, y-yy, 0, 255, 255);
- setPixel(x-xx, y-yy, 0, 255, 255);
- setPixel(x+yy, y+xx, 0, 255, 255);
- setPixel(x-yy, y+xx, 0, 255, 255);
- setPixel(x+yy, y-xx, 0, 255, 255);
- setPixel(x-yy, y-xx, 0, 255, 255);
- int d = 3 - 2*r;
- while(xx<=yy){
- if(d<0){
- d = d + 4*xx+6; //aktualny xx
- xx++; //nastepny xx
- }
- else{
- d = d + 4*(xx-yy) +10;
- xx++;
- yy--;
- }
- setPixel(x+xx, y+yy, 0, 255, 255);
- setPixel(x-xx, y+yy, 0, 255, 255);
- setPixel(x+xx, y-yy, 0, 255, 255);
- setPixel(x-xx, y-yy, 0, 255, 255);
- setPixel(x+yy, y+xx, 0, 255, 255);
- setPixel(x-yy, y+xx, 0, 255, 255);
- setPixel(x+yy, y-xx, 0, 255, 255);
- setPixel(x-yy, y-xx, 0, 255, 255);
- }
- SDL_Flip(screen);
- }
- void elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x, y, 0, 255, 0);
- setPixel(x+a, y, 0, 255, 192);
- setPixel(x-a, y, 0, 255, 192);
- setPixel(x, y+b, 0, 255, 192);
- setPixel(x, y-b, 0, 255, 192);
- //...
- SDL_Flip(screen);
- }
- void Funkcja1() {
- int ile = 10;
- for (int i=0; i<ile; i++) {
- linia1(0, 0, rand()%width, rand()%height, 255, 255, 255);
- }
- }
- void Funkcja2() {
- int ile = 10;
- int x1=0;
- int y1=0;
- for (int i=0; i<ile; i++) {
- x1 = rand()%(width-400);
- y1 = rand()%(height-400);
- linia2(x1, y1, x1+rand()%400, y1+rand()%400, 255, 255, 0);
- }
- }
- void Funkcja3() {
- int ile = 10;
- for (int i=0; i<ile; i++) {
- linia3(rand()%width, rand()%height, rand()%width, rand()%height, 255, 0, 255);
- }
- }
- void Funkcja4() {
- int ile = 5;
- for (int i=0; i<ile; i++)
- {
- okrag(rand()%width, rand()%height, rand()%200+10, 255, 255, 255);
- }
- }
- void Funkcja5() {
- int ile = 5;
- for (int i=0; i<ile; i++) {
- elipsa(rand()%width, rand()%height, rand()%200+10, rand()%200+10, 255, 255, 0);
- }
- }
- 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);
- }
- 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