Advertisement
Guest User

Untitled

a guest
May 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. /Using SDL, SDL_image, standard IO, and strings
  2. #include <SDL.h>
  3. #include <SDL_image.h>
  4. #include <stdio.h>
  5. #include <string>
  6. #include<iostream>
  7. #include <vector>
  8. #include "Regal.h"
  9. #include"Warzywa.h"
  10. #include"Bohater.h"
  11. #include"Produkty.h"
  12.  
  13. //Screen dimension constants
  14. const int SCREEN_WIDTH = 1270;
  15. const int SCREEN_HEIGHT = 730;
  16.  
  17. //Starts up SDL and creates window
  18. bool init();
  19.  
  20. //Loads media
  21. //bool loadMedia();
  22. Bohater moj(610, 670);
  23. std::vector<Produkty>produkty;
  24.  
  25. //Frees media and shuts down SDL
  26. void close();
  27.  
  28.  
  29. //Current displayed image
  30. SDL_Surface* gCurrentSurface = NULL;
  31. //Loads individual image as texture
  32. //SDL_Texture* loadTexture( std::string path );
  33.  
  34. //The window we'll be rendering to
  35. SDL_Window* gWindow = NULL;
  36.  
  37. //The window renderer
  38. SDL_Renderer* gRenderer = NULL;
  39. std::string student = "student.jpg";
  40. std::string chleb_1 = "chleb1.jpg";
  41. std::string chleb_2 = "chleb2.jpg";
  42. std::string woda_1 = "woda1.jpg";
  43. std::string woda_2 = "woda2.jpg";
  44. std::string mleko_1 = "mleko1.jpg";
  45. std::string mleko_2 = "mleko2.jpg";
  46. std::string pomidor = "pomidor.jpg";
  47. std::string rzodkiew = "rzodkiew.jpg";
  48. std::string salata = "salata.jpg";
  49. std::string brokul = "brokul";
  50. //SDL_Texture* gTexture = NULL;
  51. std::vector<Regal>regaly;
  52. bool init()
  53. {
  54. //Initialization flag
  55. bool success = true;
  56.  
  57. //Initialize SDL
  58. if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  59. {
  60. printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
  61. success = false;
  62. }
  63. else
  64. {
  65. //Set texture filtering to linear
  66. if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  67. {
  68. printf( "Warning: Linear texture filtering not enabled!" );
  69. }
  70.  
  71. //Create window
  72. gWindow = SDL_CreateWindow( "Studenckie zakupy", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  73. if( gWindow == NULL )
  74. {
  75. printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
  76. success = false;
  77. }
  78. else
  79. {
  80.  
  81. //Create renderer for window
  82. gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
  83. if( gRenderer == NULL )
  84. {
  85. printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
  86. success = false;
  87. }
  88. else
  89. {
  90. //Initialize renderer color
  91. SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  92.  
  93. //Initialize PNG loading
  94. int imgFlags = IMG_INIT_PNG;
  95. if( !( IMG_Init( imgFlags ) & imgFlags ) )
  96. {
  97. printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
  98. success = false;
  99. }
  100. }
  101. }
  102. }
  103.  
  104. return success;
  105. }
  106.  
  107. /*bool loadMedia()
  108. {
  109. //Loading success flag
  110. bool success = true;
  111. std::string t = "student.jpg";
  112. gTexture = loadTexture(t);
  113. if (gTexture == NULL)
  114. {
  115. printf("Failed to load texture image!\n");
  116. success = false;
  117. }
  118.  
  119.  
  120. return success;
  121. }*/
  122.  
  123. void close()
  124. {
  125. //Free loaded image
  126. //SDL_DestroyTexture( moj.obraz );
  127. //gTexture = NULL;
  128.  
  129. //Destroy window
  130. SDL_DestroyRenderer( gRenderer );
  131. SDL_DestroyWindow( gWindow );
  132. gWindow = NULL;
  133. gRenderer = NULL;
  134.  
  135. //Quit SDL subsystems
  136. IMG_Quit();
  137. SDL_Quit();
  138. }
  139.  
  140.  
  141.  
  142. int main( int argc, char* args[] )
  143. {
  144. moj.WczytajO(student, gRenderer);
  145. Produkty chleb1(5, 5);
  146. chleb1.WczytajO(chleb_1, gRenderer);
  147. produkty.push_back(chleb1);
  148. Produkty chleb2(1020, 115);
  149. chleb2.WczytajO(chleb_2, gRenderer);
  150. produkty.push_back(chleb2);
  151. Produkty woda1(820, 560);
  152. woda1.WczytajO(woda_1, gRenderer);
  153. produkty.push_back(woda1);
  154. Produkty woda2(380, 230);
  155. woda2.WczytajO(woda_2, gRenderer);
  156. produkty.push_back(woda2);
  157. Produkty mleko1(200, 450);
  158. mleko1.WczytajO(mleko_1, gRenderer);
  159. produkty.push_back(mleko1);
  160. Produkty mleko2(1200, 300);
  161. mleko2.WczytajO(mleko_2, gRenderer);
  162. produkty.push_back(mleko2);
  163.  
  164. double x, y, w, h;
  165. x = 80;
  166. y = 60;
  167. w = 500;
  168. h = 55;
  169. Regal lewy(300, 70, 53, 600);
  170. regaly.push_back(lewy);
  171. for (int i = 0; i < 7; i++)
  172. {
  173. Regal nowy(x, y, w, h);
  174. regaly.push_back(nowy);
  175. y = y + 111;
  176. }
  177. x = 680;
  178. y = 60;
  179. w = 500;
  180. h = 55;
  181. Regal prawy(910, 70, 53, 600);
  182. regaly.push_back(prawy);
  183. for (int i = 0; i < 7; i++)
  184. {
  185. Regal nowy(x, y, w, h);
  186. regaly.push_back(nowy);
  187. y = y + 111;
  188. }
  189. //Start up SDL and create window
  190. if( !init() )
  191. {
  192. printf( "Failed to initialize!\n" );
  193. }
  194. else
  195. {
  196. //Load media
  197. if (!moj.WczytajO(student, gRenderer) || !chleb1.WczytajO(chleb_1, gRenderer) || !chleb2.WczytajO(chleb_2, gRenderer)
  198. || !woda1.WczytajO(woda_1, gRenderer) || !woda2.WczytajO(woda_2, gRenderer) || !mleko1.WczytajO(mleko_1, gRenderer)
  199. || !mleko2.WczytajO(mleko_2, gRenderer))
  200. {
  201. printf( "Failed to load media!\n" );
  202. }
  203. else
  204. {
  205. //Main loop flag
  206. bool quit = false;
  207.  
  208. //Event handler
  209. SDL_Event e;
  210.  
  211. //While application is running
  212. while( !quit )
  213. {
  214. //Handle events on queue
  215. while (SDL_PollEvent(&e) != 0)
  216. {
  217. //User requests quit
  218. if (e.type == SDL_QUIT)
  219. {
  220. quit = true;
  221. }
  222. else if (e.type == SDL_KEYDOWN)
  223. {
  224. //Select surfaces based on key press
  225. switch (e.key.keysym.sym)
  226. {
  227. int a;
  228. case SDLK_UP:
  229. {
  230. a = 24;
  231. moj.Ruch(a);
  232. break;
  233. }
  234.  
  235. case SDLK_DOWN:
  236. {
  237. a = 25;
  238. moj.Ruch(a);
  239. break;
  240. }
  241. case SDLK_LEFT:
  242. {
  243. a = 26;
  244. moj.Ruch(a);
  245. break;
  246. }
  247. case SDLK_RIGHT:
  248. {
  249. a = 27;
  250. moj.Ruch(a);
  251. break;
  252. }
  253.  
  254. }
  255. }
  256. }
  257.  
  258.  
  259. //Clear screen
  260. SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  261. SDL_RenderClear(gRenderer);
  262. for (int i = 0; i < regaly.size() - 1; i++)
  263. {
  264. regaly[i].Rysuj(gRenderer);
  265. }
  266.  
  267. //Render texture to screen
  268. //Clear screen
  269. /*SDL_Rect wielkosc;
  270. wielkosc.x = 610;
  271. wielkosc.y = 670;
  272. wielkosc.w = 50;
  273. wielkosc.h = 50;*/
  274.  
  275.  
  276. //Render texture to screen
  277. moj.Wyswietl(gRenderer);
  278. chleb1.Wyswietl(gRenderer);
  279. chleb2.Wyswietl(gRenderer);
  280. woda1.Wyswietl(gRenderer);
  281. woda2.Wyswietl(gRenderer);
  282. mleko1.Wyswietl(gRenderer);
  283. mleko2.Wyswietl(gRenderer);
  284.  
  285.  
  286. //Update screen
  287. SDL_RenderPresent(gRenderer);
  288.  
  289. }
  290. }
  291. }
  292.  
  293. //Free resources and close SDL
  294. close();
  295.  
  296. return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement