Advertisement
Guest User

Untitled

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