Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #ifdef __cplusplus
  2. #include <cstdlib>
  3. #else
  4. #include <stdlib.h>
  5.  
  6. #endif
  7. #ifdef __APPLE__
  8. #include <SDL/SDL.h>
  9. #else
  10. #include <SDL.h>
  11. #endif
  12. #include <math.h>
  13. #define pi 3.14
  14. #include <time.h>
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. SDL_Surface *screen;
  19. int width = 900;
  20. int height = 600;
  21. char const* tytul = "GKiM - Lab 1 - Konrad Krukar";
  22.  
  23.  
  24. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
  25. SDL_Color getPixel (int x, int y);
  26.  
  27. void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
  28.  
  29.  
  30. void Funkcja1();
  31. void Funkcja2();
  32. void Funkcja3();
  33. void Funkcja4();
  34. void Funkcja5();
  35. void Funkcja6();
  36.  
  37. //FUNKCJA 0
  38. void Funkcja1() {
  39. for(int i=0; i<450; i++)
  40. {
  41. for(int j=0; j<300;j++)
  42. {
  43. SDL_Color kolor = getPixel(i,j);
  44. setPixel(i+450,j,kolor.r,0,0);
  45. setPixel(i,j+300,0,kolor.g,0);
  46. setPixel(i+450,j+300,0,0,kolor.b);
  47. }
  48. }
  49. SDL_Flip(screen);
  50. }
  51.  
  52. //FUNKCJA 1
  53. void Funkcja2() {
  54. for(int i=0; i<450; i++)
  55. {
  56. for(int j=0; j<300;j++)
  57. {
  58. SDL_Color kolor = getPixel(i,j);
  59.  
  60. setPixel(i+450,j,255-kolor.r,255-kolor.g,255-kolor.b);
  61. //TO SAMO CO WYZEJ
  62. setPixel(i,j+300,255-kolor.r,255-kolor.g,255-kolor.b);
  63. //OTRZYMUJEMY TEN SAM OBRAZEK CO NA POCZATKU
  64. setPixel(i+450,j+300,kolor.r,kolor.g,kolor.b);
  65.  
  66. }
  67. }
  68. SDL_Flip(screen);
  69. }
  70.  
  71. //FUNKCJA 2
  72. void Funkcja3() {
  73. for(int i=0; i<450; i++)
  74. {
  75. for(int j=0; j<300;j++)
  76. {
  77.  
  78.  
  79. SDL_Color kolor = getPixel(i,j);
  80.  
  81. float c = 1 - (kolor.r/255);
  82. float m = 1 - (kolor.g/255);
  83. float y = 1 - (kolor.b/255);
  84.  
  85.  
  86. float k=c;
  87.  
  88. if (k>m) k=m;
  89. else k=c;
  90.  
  91. if(k>y) k=y;
  92.  
  93. float cc = (c-k)/(1-k);
  94. float mm = (m-k)/(1-k);
  95. float yy = (y-k)/(1-k);
  96.  
  97.  
  98.  
  99. int sredRGB = (kolor.r + kolor.g + kolor.b)/3;
  100. float sredCMY = (cc+mm+yy)/3;
  101.  
  102. //int dobra = ((kolor.r*0.2126) + (kolor.g*0.7152) + (kolor.b*0.0722));
  103. setPixel(i+450,j,sredRGB,sredRGB,sredRGB);
  104. //setPixel(i,j+300,dobra,dobra,dobra);
  105. setPixel(i,j+300,sredCMY*255,sredCMY*255,sredCMY*255);
  106.  
  107. setPixel(i+450,j+300,k*255,k*255,k*255);
  108.  
  109. //setPixel(i+450,j+300);
  110. }
  111. }
  112. SDL_Flip(screen);
  113. }
  114.  
  115. void Funkcja4() {
  116.  
  117. SDL_Flip(screen);
  118. }
  119.  
  120. void Funkcja5() {
  121. for(int i=0; i<450; i++)
  122. {
  123. for(int j=0; j<300;j++)
  124. {
  125. SDL_Color kolor = getPixel(i,j);
  126. setPixel(i+450,j,kolor.g,kolor.r,kolor.b);
  127. setPixel(i,j+300,kolor.r,kolor.b,kolor.g);
  128. setPixel(i+450,j+300,kolor.b,kolor.g,kolor.r);
  129. }
  130. }
  131. SDL_Flip(screen);
  132. }
  133.  
  134. void Funkcja6() {
  135. for(int i=0; i<450; i++)
  136. {
  137. for(int j=0; j<300;j++)
  138. {
  139. SDL_Color kolor = getPixel(i,j);
  140. setPixel(i+450,j,kolor.r,kolor.g,0);
  141. setPixel(i,j+300,kolor.r,0,kolor.b);
  142. setPixel(i+450,j+300,0,kolor.g,kolor.b);
  143. }
  144. }
  145. SDL_Flip(screen);
  146. }
  147.  
  148. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  149. {
  150. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  151. {
  152. /* Zamieniamy poszczególne sk³adowe koloru na format koloru pixela */
  153. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  154.  
  155. /* Pobieramy informacji ile bajtów zajmuje jeden pixel */
  156. int bpp = screen->format->BytesPerPixel;
  157.  
  158. /* Obliczamy adres pixela */
  159. Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
  160.  
  161. /* Ustawiamy wartoœæ pixela, w zale¿noœci od formatu powierzchni*/
  162. switch(bpp)
  163. {
  164. case 1: //8-bit
  165. *p = pixel;
  166. break;
  167.  
  168. case 2: //16-bit
  169. *(Uint16 *)p = pixel;
  170. break;
  171.  
  172. case 3: //24-bit
  173. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  174. p[0] = (pixel >> 16) & 0xff;
  175. p[1] = (pixel >> 8) & 0xff;
  176. p[2] = pixel & 0xff;
  177. } else {
  178. p[0] = pixel & 0xff;
  179. p[1] = (pixel >> 8) & 0xff;
  180. p[2] = (pixel >> 16) & 0xff;
  181. }
  182. break;
  183.  
  184. case 4: //32-bit
  185. *(Uint32 *)p = pixel;
  186. break;
  187.  
  188. }
  189. /* update the screen (aka double buffering) */
  190. }
  191. }
  192.  
  193.  
  194. void ladujBMP(char const* nazwa, int x, int y)
  195. {
  196. SDL_Surface* bmp = SDL_LoadBMP(nazwa);
  197. if (!bmp)
  198. {
  199. printf("Unable to load bitmap: %s\n", SDL_GetError());
  200. }
  201. else
  202. {
  203. SDL_Rect dstrect;
  204. dstrect.x = x;
  205. dstrect.y = y;
  206. SDL_BlitSurface(bmp, 0, screen, &dstrect);
  207. SDL_Flip(screen);
  208. SDL_FreeSurface(bmp);
  209. }
  210.  
  211. }
  212.  
  213.  
  214. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  215. {
  216. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  217. SDL_Flip(screen);
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224. SDL_Color getPixel (int x, int y) {
  225. SDL_Color color ;
  226. Uint32 col = 0 ;
  227. if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  228. //determine position
  229. char* pPosition=(char*)screen->pixels ;
  230. //offset by y
  231. pPosition+=(screen->pitch*y) ;
  232. //offset by x
  233. pPosition+=(screen->format->BytesPerPixel*x);
  234. //copy pixel data
  235. memcpy(&col, pPosition, screen->format->BytesPerPixel);
  236. //convert color
  237. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  238. }
  239. return ( color ) ;
  240. }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. int main ( int argc, char** argv )
  249. {
  250. // console output
  251. freopen( "CON", "wt", stdout );
  252. freopen( "CON", "wt", stderr );
  253.  
  254. // initialize SDL video
  255. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  256. {
  257. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  258. return 1;
  259. }
  260.  
  261. // make sure SDL cleans up before exit
  262. atexit(SDL_Quit);
  263.  
  264. // create a new window
  265. screen = SDL_SetVideoMode(width, height, 32,
  266. SDL_HWSURFACE|SDL_DOUBLEBUF);
  267. if ( !screen )
  268. {
  269. printf("Unable to set video: %s\n", SDL_GetError());
  270. return 1;
  271. }
  272.  
  273. SDL_WM_SetCaption( tytul , NULL );
  274. // program main loop
  275. bool done = false;
  276. while (!done)
  277. {
  278. // message processing loop
  279. SDL_Event event;
  280. while (SDL_PollEvent(&event))
  281. {
  282. // check for messages
  283. switch (event.type)
  284. {
  285. // exit if the window is closed
  286. case SDL_QUIT:
  287. done = true;
  288. break;
  289.  
  290. // check for keypresses
  291. case SDL_KEYDOWN:
  292. {
  293. // exit if ESCAPE is pressed
  294. if (event.key.keysym.sym == SDLK_ESCAPE)
  295. done = true;
  296. if (event.key.keysym.sym == SDLK_1)
  297. Funkcja1();
  298. if (event.key.keysym.sym == SDLK_2)
  299. Funkcja2();
  300. if (event.key.keysym.sym == SDLK_3)
  301. Funkcja3();
  302. if (event.key.keysym.sym == SDLK_4)
  303. Funkcja4();
  304. if (event.key.keysym.sym == SDLK_5)
  305. Funkcja5();
  306. if (event.key.keysym.sym == SDLK_6)
  307. Funkcja6();
  308. if (event.key.keysym.sym == SDLK_a)
  309. ladujBMP("obrazek1.bmp", 0, 0);
  310. if (event.key.keysym.sym == SDLK_s)
  311. ladujBMP("obrazek2.bmp", 0, 0);
  312. if (event.key.keysym.sym == SDLK_d)
  313. ladujBMP("obrazek3.bmp", 0, 0);
  314. if (event.key.keysym.sym == SDLK_b)
  315. czyscEkran(0, 0, 0); break;
  316. }
  317. } // end switch
  318. } // end of message processing
  319.  
  320. } // end main loop
  321.  
  322.  
  323. // all is well ;)
  324. printf("Exited cleanly\n");
  325. return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement