Advertisement
Guest User

Untitled

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