Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.04 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 3 - 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.  
  26. void Funkcja1();
  27. void Funkcja2();
  28. void Funkcja3();
  29. void Funkcja4();
  30. void Funkcja5();
  31. void Funkcja6();
  32. void Funkcja7();
  33. void Funkcja8();
  34. void Funkcja9();
  35.  
  36.  
  37. void Funkcja1() {
  38.  
  39. int szer = width/6;
  40.  
  41. for(int x=0; x<width; x++){
  42. for(int y=0; y<height; y++){
  43.  
  44. if(x<szer)
  45. setPixel(x, y, 0, (1.0*x/szer*255), 255);
  46. else if (x<2*szer)
  47. setPixel(x, y, 0, 255, 255-(1.0*(x-szer)/szer*255));
  48. else if (x<3*szer)
  49. setPixel(x, y, (1.0*(x-2*szer)/szer*255), 255, 0);
  50. else if (x<4*szer)
  51. setPixel(x, y, 255, 255-(1.0*(x-3*szer)/szer*255), 0);
  52. else if (x<5*szer)
  53. setPixel(x, y, 255, 0 , (1.0*(x-4*szer)/szer*255));
  54. else if (x<6*szer)
  55. setPixel(x, y, 255-(1.0*(x-5*szer)/szer*255), 0 , 255);
  56.  
  57.  
  58.  
  59.  
  60. }
  61. }
  62.  
  63. SDL_Flip(screen);
  64. }
  65.  
  66. void Funkcja2() {
  67.  
  68. SDL_Color kolor;
  69. int R, G, B;
  70.  
  71. int szer = (width/2); //szerokość
  72.  
  73. for(int x=0; x<szer; x++){
  74. for(int y=0; y<height/2; y++){
  75. kolor = getPixel(x, y);
  76.  
  77. R = kolor.r;
  78. G = kolor.g;
  79. B = kolor.b;
  80.  
  81. if(R<127 && G<127 && B<127)
  82. setPixel(x+szer, y, 0, 0, 0);
  83. else if (R>127 && G>127 && B>127)
  84. setPixel(x+szer, y, 255, 255, 255);
  85. else if (R>127 && G<127 && B<127)
  86. setPixel(x+szer, y, 255, 0, 0);
  87. else if (R<127 && G>127 && B<127)
  88. setPixel(x+szer, y, 0, 255, 0);
  89. else if (R<127 && G<127 && B>127)
  90. setPixel(x+szer, y, 0, 0, 255);
  91. else if (R>127 && G>127 && B<127)
  92. setPixel(x+szer, y, 255, 255, 0);
  93. else if (R>127 && G<127 && B>127)
  94. setPixel(x+szer, y, 255, 0, 255);
  95. else if (R<127 && G>127 && B>127)
  96. setPixel(x+szer, y, 0, 255, 255);
  97.  
  98. }
  99. }
  100.  
  101. SDL_Flip(screen);
  102. }
  103.  
  104. void Funkcja3() {
  105.  
  106. SDL_Color kolor;
  107.  
  108. int barwy[8][3];
  109.  
  110. //black
  111. barwy[0][0] = 0;
  112. barwy[0][1] = 0;
  113. barwy[0][2] = 0;
  114.  
  115. //white
  116. barwy[7][0] = 255;
  117. barwy[7][1] = 255;
  118. barwy[7][2] = 255;
  119.  
  120. //blue
  121. barwy[1][0] = 170;
  122. barwy[1][1] = 0;
  123. barwy[1][2] = 0;
  124.  
  125. //red
  126. barwy[2][0] = 0;
  127. barwy[2][1] = 170;
  128. barwy[2][2] = 0;
  129.  
  130. //magenta
  131. barwy[3][0] = 0;
  132. barwy[3][1] = 0;
  133. barwy[3][2] = 170;
  134.  
  135. //green
  136. barwy[4][0] = 85;
  137. barwy[4][1] = 255;
  138. barwy[4][2] = 85;
  139.  
  140. //cyan
  141. barwy[5][0] = 255;
  142. barwy[5][1] = 85;
  143. barwy[5][2] = 85;
  144.  
  145. //yellow
  146. barwy[6][0] = 85;
  147. barwy[6][1] = 85;
  148. barwy[6][2] = 255;
  149.  
  150. barwy[8][0] = 170;
  151. barwy[8][1] = 170;
  152. barwy[8][2] = 170;
  153.  
  154. float r=0; //blad tymczasowy
  155. int rgb[3];
  156. float blad = 0; //blad ogolny
  157. int naj = 0;
  158.  
  159. for(int x=0; x<width; x++){
  160. for(int y=0; y<height; y++){
  161. kolor = getPixel(x, y);
  162. rgb[0] = kolor.r;
  163. rgb[1] = kolor.g;
  164. rgb[2] = kolor.b;
  165.  
  166. for(int i=0; i<9; i++){
  167.  
  168. for(int j=0; j<3; j++){
  169. r = r+ abs(barwy[i][j]-rgb[j]);
  170. }
  171.  
  172. if(i=0)
  173. {
  174. blad = r;
  175. naj = i;
  176. }
  177. else if (r<blad)
  178. {
  179. blad = r;
  180. naj = i;
  181. }
  182.  
  183. r = 0;
  184.  
  185. }
  186.  
  187. setPixel(x+width/2, y, barwy[naj][0],barwy[naj][1], barwy[naj][2] );
  188.  
  189.  
  190.  
  191. }
  192. }
  193.  
  194. SDL_Flip(screen);
  195. }
  196.  
  197. void Funkcja4() {
  198.  
  199. for(int x=0; x<width; x++){
  200. for(int y=0; y<height; y++){
  201. setPixel(x, y, 255, 0, 255);
  202.  
  203. }}
  204.  
  205. SDL_Flip(screen);
  206. }
  207.  
  208. void Funkcja5() {
  209.  
  210. //...
  211.  
  212. SDL_Flip(screen);
  213. }
  214.  
  215. void Funkcja6() {
  216.  
  217. //...
  218.  
  219. SDL_Flip(screen);
  220. }
  221.  
  222. void Funkcja7() {
  223.  
  224. //...
  225.  
  226. SDL_Flip(screen);
  227. }
  228.  
  229. void Funkcja8() {
  230.  
  231. //...
  232.  
  233. SDL_Flip(screen);
  234. }
  235.  
  236. void Funkcja9() {
  237.  
  238. //...
  239.  
  240. SDL_Flip(screen);
  241. }
  242.  
  243.  
  244. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  245. {
  246. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  247. {
  248. /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
  249. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  250.  
  251. /* Pobieramy informację ile bajtów zajmuje jeden piksel */
  252. int bpp = screen->format->BytesPerPixel;
  253.  
  254. /* Obliczamy adres piksela */
  255. Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
  256.  
  257. /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
  258. switch(bpp)
  259. {
  260. case 1: //8-bit
  261. *p = pixel;
  262. break;
  263.  
  264. case 2: //16-bit
  265. *(Uint16 *)p = pixel;
  266. break;
  267.  
  268. case 3: //24-bit
  269. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  270. p[0] = (pixel >> 16) & 0xff;
  271. p[1] = (pixel >> 8) & 0xff;
  272. p[2] = pixel & 0xff;
  273. } else {
  274. p[0] = pixel & 0xff;
  275. p[1] = (pixel >> 8) & 0xff;
  276. p[2] = (pixel >> 16) & 0xff;
  277. }
  278. break;
  279.  
  280. case 4: //32-bit
  281. *(Uint32 *)p = pixel;
  282. break;
  283.  
  284. }
  285. /* ewentualna aktualizacja obrazu (aka double buffering) */
  286. }
  287. }
  288.  
  289.  
  290. SDL_Color getPixel (int x, int y) {
  291. SDL_Color color ;
  292. Uint32 col = 0 ;
  293. if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  294. //określamy pozycję
  295. char* pPosition=(char*)screen->pixels ;
  296. //przesunięcie względem y
  297. pPosition+=(screen->pitch*y) ;
  298. //przesunięcie względem x
  299. pPosition+=(screen->format->BytesPerPixel*x);
  300. //kopiujemy dane piksela
  301. memcpy(&col, pPosition, screen->format->BytesPerPixel);
  302. //konwertujemy kolor
  303. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  304. }
  305. return ( color ) ;
  306. }
  307.  
  308. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  309. {
  310. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  311. SDL_Flip(screen);
  312.  
  313. }
  314.  
  315. void ladujBMP(char const* nazwa, int x, int y)
  316. {
  317. SDL_Surface* bmp = SDL_LoadBMP(nazwa);
  318. if (!bmp)
  319. {
  320. printf("Unable to load bitmap: %s\n", SDL_GetError());
  321. }
  322. else
  323. {
  324. SDL_Rect dstrect;
  325. dstrect.x = x;
  326. dstrect.y = y;
  327. SDL_BlitSurface(bmp, 0, screen, &dstrect);
  328. SDL_Flip(screen);
  329. SDL_FreeSurface(bmp);
  330. }
  331.  
  332. }
  333.  
  334.  
  335. int main ( int argc, char** argv )
  336. {
  337. // console output
  338. freopen( "CON", "wt", stdout );
  339. freopen( "CON", "wt", stderr );
  340.  
  341. // initialize SDL video
  342. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  343. {
  344. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  345. return 1;
  346. }
  347.  
  348. // make sure SDL cleans up before exit
  349. atexit(SDL_Quit);
  350.  
  351. // create a new window
  352. screen = SDL_SetVideoMode(width, height, 32,
  353. SDL_HWSURFACE|SDL_DOUBLEBUF);
  354. if ( !screen )
  355. {
  356. printf("Unable to set video: %s\n", SDL_GetError());
  357. return 1;
  358. }
  359.  
  360. SDL_WM_SetCaption( tytul , NULL );
  361. // program main loop
  362. bool done = false;
  363. while (!done)
  364. {
  365. // message processing loop
  366. SDL_Event event;
  367. while (SDL_PollEvent(&event))
  368. {
  369. // check for messages
  370. switch (event.type)
  371. {
  372. // exit if the window is closed
  373. case SDL_QUIT:
  374. done = true;
  375. break;
  376.  
  377. // check for keypresses
  378. case SDL_KEYDOWN:
  379. {
  380. // exit if ESCAPE is pressed
  381. if (event.key.keysym.sym == SDLK_ESCAPE)
  382. done = true;
  383. if (event.key.keysym.sym == SDLK_1)
  384. Funkcja1();
  385. if (event.key.keysym.sym == SDLK_2)
  386. Funkcja2();
  387. if (event.key.keysym.sym == SDLK_3)
  388. Funkcja3();
  389. if (event.key.keysym.sym == SDLK_4)
  390. Funkcja4();
  391. if (event.key.keysym.sym == SDLK_5)
  392. Funkcja5();
  393. if (event.key.keysym.sym == SDLK_6)
  394. Funkcja6();
  395. if (event.key.keysym.sym == SDLK_7)
  396. Funkcja7();
  397. if (event.key.keysym.sym == SDLK_8)
  398. Funkcja8();
  399. if (event.key.keysym.sym == SDLK_9)
  400. Funkcja9();
  401. if (event.key.keysym.sym == SDLK_a)
  402. ladujBMP("obrazek1.bmp", 0, 0);
  403. if (event.key.keysym.sym == SDLK_s)
  404. ladujBMP("obrazek2.bmp", 0, 0);
  405. if (event.key.keysym.sym == SDLK_d)
  406. ladujBMP("obrazek3.bmp", 0, 0);
  407. if (event.key.keysym.sym == SDLK_f)
  408. ladujBMP("obrazek4.bmp", 0, 0);
  409. if (event.key.keysym.sym == SDLK_g)
  410. ladujBMP("obrazek5.bmp", 0, 0);
  411. if (event.key.keysym.sym == SDLK_b)
  412. czyscEkran(0, 0, 10); break;
  413. }
  414. } // end switch
  415. } // end of message processing
  416.  
  417. } // end main loop
  418.  
  419.  
  420. // all is well ;)
  421. printf("Exited cleanly\n");
  422. return 0;
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement