Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.32 KB | None | 0 0
  1. #ifdef __cplusplus
  2. #include <cstdlib>
  3. #else
  4. #include <stdlib.h>
  5.  
  6. #endif
  7. #include <SDL/SDL.h>
  8. #include <math.h>
  9. #define pi 3.1415926536
  10. #include <time.h>
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. SDL_Surface *screen;
  15. int width = 900;
  16. int height = 600;
  17. char const* tytul = "GKiM2019 - Lab 5 - Morawski Mariusz";
  18.  
  19. struct linia {
  20. int xp;
  21. int yp;
  22. int xk;
  23. int yk;
  24. };
  25.  
  26. linia wzorzec[] = {
  27. {0,0,0,20},
  28. {0,20,10,20},
  29. {10,20,10,50},
  30. {10,50,30,50},
  31. {30,50,30,20},
  32. {30,20,50,20},
  33. {50,20,30,0},
  34. {30,0,0,0},
  35. };
  36.  
  37. linia mlotek[8];
  38.  
  39. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
  40. SDL_Color getPixel (int x, int y);
  41.  
  42. void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
  43.  
  44. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
  45. void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
  46. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B);
  47. void Funkcja1();
  48. void Funkcja2();
  49. void Funkcja3();
  50. void Funkcja4();
  51. void Funkcja5();
  52. void Funkcja6();
  53. void Funkcja7();
  54. void Funkcja8();
  55. void Funkcja9();
  56.  
  57. void inicjalizacja(){
  58. for(int i=0;i<8;i++){
  59. mlotek[i]=wzorzec[i];
  60. }
  61. }
  62.  
  63. void rysowanie(Uint8 R, Uint8 G, Uint8 B){
  64.  
  65. for(int i=0;i<8;i++){
  66. Linia(mlotek[i].xp,mlotek[i].yp,
  67. mlotek[i].xk,mlotek[i].yk,R,G,B);
  68. }
  69. }
  70.  
  71. void Funkcja1() {
  72. inicjalizacja();
  73. rysowanie(255,255,255);
  74.  
  75. SDL_Flip(screen);
  76. }
  77.  
  78. void przesuniecie(int px, int py){
  79. for(int i=0;i<8;i++){
  80. mlotek[i].xp += px;
  81. mlotek[i].xk += px;
  82. mlotek[i].yp += py;
  83. mlotek[i].yk += py;
  84. }
  85. }
  86.  
  87. void Funkcja2() {
  88.  
  89. przesuniecie(20,10);
  90. rysowanie(255,255,0);
  91.  
  92. SDL_Flip(screen);
  93. }
  94.  
  95. void skalowanie(float sx, float sy){
  96. int x= mlotek[0].xp; int y = mlotek[0].yp;
  97. przesuniecie(-x,-y);
  98. for(int i=0;i<8;i++){
  99. mlotek[i].xp *= sx;
  100. mlotek[i].xk *= sx;
  101. mlotek[i].yp *= sy;
  102. mlotek[i].yk *= sy;
  103. }
  104. przesuniecie(x,y);
  105. }
  106.  
  107. void Funkcja3() {
  108.  
  109. skalowanie(1.2,1.2);
  110. rysowanie(255,0,0);
  111.  
  112. SDL_Flip(screen);
  113. }
  114.  
  115. bool operator !=(const SDL_Color& kolor1, const SDL_Color& kolor2){
  116. if(kolor1.r!=kolor2.r) return true;
  117. if(kolor1.g!=kolor2.g) return true;
  118. if(kolor1.b!=kolor2.b) return true;
  119. return false;
  120. }
  121.  
  122. void sianie(int x, int y, SDL_Color brzeg, SDL_Color wypelnienie){
  123. setPixel(x,y,wypelnienie.r,wypelnienie.g,wypelnienie.b);
  124.  
  125. SDL_Color kolor;
  126.  
  127. kolor = getPixel(x-1,y);
  128. if(kolor!= brzeg && kolor!=wypelnienie) sianie(x-1,y,brzeg,wypelnienie);
  129. kolor = getPixel(x,y-1);
  130. if(kolor!= brzeg && kolor!=wypelnienie) sianie(x,y-1,brzeg,wypelnienie);
  131. kolor = getPixel(x,y+1);
  132. if(kolor!= brzeg && kolor!=wypelnienie) sianie(x,y+1,brzeg,wypelnienie);
  133. kolor = getPixel(x+1,y);
  134. if(kolor!= brzeg && kolor!=wypelnienie) sianie(x+1,y,brzeg,wypelnienie);
  135.  
  136. }
  137.  
  138. void Funkcja4() {
  139.  
  140. SDL_Color brzeg = {255,255,255};
  141. SDL_Color wypelnienie={.r=255,.g=0,.b=255};
  142.  
  143. Okrag(300,300,50,brzeg.r,brzeg.g,brzeg.b);
  144. sianie(300,300,brzeg, wypelnienie);
  145. SDL_Flip(screen);
  146. }
  147.  
  148.  
  149. void Funkcja5() {
  150.  
  151. Linia(rand()%width, rand()%height,
  152. rand()%width, rand()%height, rand()%255,rand()%255,rand()%255);
  153.  
  154. SDL_Flip(screen);
  155. }
  156.  
  157. bool operator ==(const SDL_Color& kolor1, const SDL_Color& kolor2){
  158. if(kolor1.r!=kolor2.r) return false;
  159. if(kolor1.g!=kolor2.g) return false;
  160. if(kolor1.b!=kolor2.b) return false;
  161. return true;
  162. }
  163.  
  164. void sianie2(int x, int y, SDL_Color wypelnienie, SDL_Color tlo){
  165. setPixel(x,y,wypelnienie.r,wypelnienie.g,wypelnienie.b);
  166.  
  167. SDL_Color kolor;
  168.  
  169. kolor = getPixel(x-1,y);
  170. if(kolor== tlo && kolor!=wypelnienie) sianie2(x-1,y,wypelnienie,tlo);
  171. kolor = getPixel(x,y-1);
  172. if(kolor== tlo && kolor!=wypelnienie) sianie2(x,y-1,wypelnienie,tlo);
  173. kolor = getPixel(x,y+1);
  174. if(kolor== tlo && kolor!=wypelnienie) sianie2(x,y+1,wypelnienie,tlo);
  175. kolor = getPixel(x+1,y);
  176. if(kolor== tlo && kolor!=wypelnienie) sianie2(x+1,y,wypelnienie,tlo);
  177.  
  178. }
  179.  
  180. void Funkcja6() {
  181.  
  182. SDL_Color tlo = getPixel(600,300);
  183. SDL_Color wypelnienie={.r=255,.g=255,.b=0};
  184.  
  185. Okrag(600,300,50,255,255,255);
  186. sianie2(600,300,wypelnienie, tlo);
  187. SDL_Flip(screen);
  188. }
  189.  
  190.  
  191. void Funkcja7() {
  192.  
  193. //...
  194.  
  195. SDL_Flip(screen);
  196. }
  197.  
  198.  
  199. void Funkcja8() {
  200.  
  201. //...
  202.  
  203. SDL_Flip(screen);
  204. }
  205.  
  206.  
  207. void Funkcja9() {
  208.  
  209. //...
  210.  
  211. SDL_Flip(screen);
  212. }
  213.  
  214.  
  215. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
  216. setPixel(x1,y1,R,G,B);
  217. setPixel(x2,y2,R,G,B);
  218. // zmienne pomocnicze
  219. int d, dx, dy, ai, bi, xi, yi;
  220. int x = x1, y = y1;
  221. // ustalenie kierunku rysowania
  222. if (x1 < x2)
  223. {
  224. xi = 1;
  225. dx = x2 - x1;
  226. }
  227. else
  228. {
  229. xi = -1;
  230. dx = x1 - x2;
  231. }
  232. // ustalenie kierunku rysowania
  233. if (y1 < y2)
  234. {
  235. yi = 1;
  236. dy = y2 - y1;
  237. }
  238. else
  239. {
  240. yi = -1;
  241. dy = y1 - y2;
  242. }
  243. // pierwszy piksel
  244. setPixel(x, y, R, G, B);
  245. // oś wiodąca OX
  246. if (dx > dy)
  247. {
  248. ai = (dy - dx) * 2;
  249. bi = dy * 2;
  250. d = bi - dx;
  251. // pętla po kolejnych x
  252. while (x != x2)
  253. {
  254. // test współczynnika
  255. if (d >= 0)
  256. {
  257. x += xi;
  258. y += yi;
  259. d += ai;
  260. }
  261. else
  262. {
  263. d += bi;
  264. x += xi;
  265. }
  266. setPixel(x, y, R, G, B);
  267. }
  268. }
  269. // oś wiodąca OY
  270. else
  271. {
  272. ai = ( dx - dy ) * 2;
  273. bi = dx * 2;
  274. d = bi - dy;
  275. // pętla po kolejnych y
  276. while (y != y2)
  277. {
  278. // test współczynnika
  279. if (d >= 0)
  280. {
  281. x += xi;
  282. y += yi;
  283. d += ai;
  284. }
  285. else
  286. {
  287. d += bi;
  288. y += yi;
  289. }
  290. setPixel(x, y, R, G, B);
  291. }
  292. }
  293. SDL_Flip(screen);
  294. }
  295.  
  296. void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
  297. setPixel(x+x0, y+y0, R, G, B);
  298. setPixel(y+x0, x+y0, R, G, B);
  299. setPixel(y+x0, -x+y0, R, G, B);
  300. setPixel(x+x0, -y+y0, R, G, B);
  301. setPixel(-x+x0, -y+y0, R, G, B);
  302. setPixel(-y+x0, -x+y0, R, G, B);
  303. setPixel(-y+x0, x+y0, R, G, B);
  304. setPixel(-x+x0, y+y0, R, G, B);;
  305. }
  306.  
  307.  
  308. void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
  309. int x =0;
  310. int y=r;
  311. int d = 3-2*r;
  312.  
  313. while (x <= y){
  314. if (d<0){
  315. d=d+4*x+6;
  316. RysujOkrag(x0, y0, x, y, R, G, B);
  317. } else{
  318. d=d+4*(x-y)+10;
  319. y--;
  320. RysujOkrag(x0, y0, x, y, R, G, B);
  321. }
  322. x++;
  323. }
  324. SDL_Flip(screen);
  325. }
  326.  
  327. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
  328. int xc = 0,
  329. yc = b;
  330. int aa = a * a,
  331. aa2 = aa + aa,
  332. bb = b * b,
  333. bb2 = bb + bb;
  334. int d = bb - aa * b + (aa / 4),
  335. dx = 0,
  336. dy = aa2 * b;
  337.  
  338. while (dx < dy) {
  339. setPixel (x - xc,y - yc, R, G, B);
  340. setPixel (x - xc,y + yc, R, G, B);
  341. setPixel (x + xc,y - yc, R, G, B);
  342. setPixel (x + xc,y + yc, R, G, B);
  343. if (d > 0){
  344. yc = yc-1;
  345. dy -= aa2;
  346. d -= dy;
  347. }
  348. xc =xc+1;
  349. dx += bb2;
  350. d += bb + dx;
  351. }
  352. d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
  353.  
  354. while (yc >= 0) {
  355. setPixel (x - xc,y - yc, R, G, B);
  356. setPixel (x - xc,y + yc, R, G, B);
  357. setPixel (x + xc,y - yc, R, G, B);
  358. setPixel (x + xc,y + yc, R, G, B);
  359. if (d < 0) {
  360. xc =xc+1;
  361. dx += bb2;
  362. d += (bb + dx);
  363. }
  364. yc = yc-1;
  365. dy -= aa2;
  366. d += aa - dy;
  367. }
  368. SDL_Flip(screen);
  369. }
  370.  
  371.  
  372.  
  373. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  374. {
  375. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  376. {
  377. /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
  378. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  379.  
  380. /* Pobieramy informację ile bajtów zajmuje jeden piksel */
  381. int bpp = screen->format->BytesPerPixel;
  382.  
  383. /* Obliczamy adres piksela */
  384. Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
  385.  
  386. /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
  387. switch(bpp)
  388. {
  389. case 1: //8-bit
  390. *p = pixel;
  391. break;
  392.  
  393. case 2: //16-bit
  394. *(Uint16 *)p = pixel;
  395. break;
  396.  
  397. case 3: //24-bit
  398. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  399. p[0] = (pixel >> 16) & 0xff;
  400. p[1] = (pixel >> 8) & 0xff;
  401. p[2] = pixel & 0xff;
  402. } else {
  403. p[0] = pixel & 0xff;
  404. p[1] = (pixel >> 8) & 0xff;
  405. p[2] = (pixel >> 16) & 0xff;
  406. }
  407. break;
  408.  
  409. case 4: //32-bit
  410. *(Uint32 *)p = pixel;
  411. break;
  412.  
  413. }
  414. /* ewentualna aktualizacja obrazu (aka double buffering) */
  415. }
  416. }
  417.  
  418.  
  419. SDL_Color getPixel (int x, int y) {
  420. SDL_Color color ;
  421. Uint32 col = 0 ;
  422. if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  423. //określamy pozycję
  424. char* pPosition=(char*)screen->pixels ;
  425. //przesunięcie względem y
  426. pPosition+=(screen->pitch*y) ;
  427. //przesunięcie względem x
  428. pPosition+=(screen->format->BytesPerPixel*x);
  429. //kopiujemy dane piksela
  430. memcpy(&col, pPosition, screen->format->BytesPerPixel);
  431. //konwertujemy kolor
  432. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  433. }
  434. return ( color ) ;
  435. }
  436.  
  437. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  438. {
  439. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  440. SDL_Flip(screen);
  441.  
  442. }
  443.  
  444.  
  445.  
  446. int main ( int argc, char** argv )
  447. {
  448. // console output
  449. freopen( "CON", "wt", stdout );
  450. freopen( "CON", "wt", stderr );
  451.  
  452. // initialize SDL video
  453. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  454. {
  455. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  456. return 1;
  457. }
  458.  
  459. // make sure SDL cleans up before exit
  460. atexit(SDL_Quit);
  461.  
  462. // create a new window
  463. screen = SDL_SetVideoMode(width, height, 32,
  464. SDL_HWSURFACE|SDL_DOUBLEBUF);
  465. if ( !screen )
  466. {
  467. printf("Unable to set video: %s\n", SDL_GetError());
  468. return 1;
  469. }
  470.  
  471. SDL_WM_SetCaption( tytul , NULL );
  472. // program main loop
  473. bool done = false;
  474. while (!done)
  475. {
  476. // message processing loop
  477. SDL_Event event;
  478. while (SDL_PollEvent(&event))
  479. {
  480. // check for messages
  481. switch (event.type)
  482. {
  483. // exit if the window is closed
  484. case SDL_QUIT:
  485. done = true;
  486. break;
  487.  
  488. // check for keypresses
  489. case SDL_KEYDOWN:
  490. {
  491. // exit if ESCAPE is pressed
  492. if (event.key.keysym.sym == SDLK_ESCAPE)
  493. done = true;
  494. if (event.key.keysym.sym == SDLK_1)
  495. Funkcja1();
  496. if (event.key.keysym.sym == SDLK_2)
  497. Funkcja2();
  498. if (event.key.keysym.sym == SDLK_3)
  499. Funkcja3();
  500. if (event.key.keysym.sym == SDLK_4)
  501. Funkcja4();
  502. if (event.key.keysym.sym == SDLK_5)
  503. Funkcja5();
  504. if (event.key.keysym.sym == SDLK_6)
  505. Funkcja6();
  506. if (event.key.keysym.sym == SDLK_7)
  507. Funkcja7();
  508. if (event.key.keysym.sym == SDLK_b)
  509. czyscEkran(0, 0, 10); break;
  510. }
  511. } // end switch
  512. } // end of message processing
  513.  
  514. } // end main loop
  515.  
  516.  
  517. // all is well ;)
  518. printf("Exited cleanly\n");
  519. return 0;
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement