Ramaraunt1

Stellar Engine Reworked

May 11th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. #include <iostream>
  2. #include "engine.h"
  3.  
  4. int Engine::Initialize(int framerate, int width, int height, std::string titleBarText, bool fullscreen, bool resizable, Color splashColor, bool shouldDrawSplash, GLuint glClearMode, int openGLVersionMajor, int openGLVersionMinor, GLuint profile, GLuint compatiblity, int intervalToSwap)
  5. {
  6.  
  7. //variable assigning
  8. std::cout << "Starting OpenStellar Engine! Please wait while it initializes!" << std::endl;
  9. std::cout << "Assigning Variables!" << std::endl;
  10. if (resizable)
  11. {
  12. resizable = GL_TRUE;
  13. }
  14. else
  15. {
  16. resizable = GL_FALSE;
  17. }
  18. screenTitle = titleBarText;
  19. if (fullscreen == true)
  20. {
  21. screenFullscreen = glfwGetPrimaryMonitor();
  22. }
  23. else
  24. {
  25. screenFullscreen = nullptr;
  26. }
  27. textureIdIncrement = 0;
  28. roomIdIncrement = 0;
  29. loopHasStarted = false;
  30. screenGLVersionMajor = openGLVersionMajor;
  31. screenGLVersionMinor = openGLVersionMinor;
  32. screenProfile = profile;
  33. screenCompatability = compatiblity;
  34. screenWidth = width;
  35. screenHeight = height;
  36. screenSplashColor = splashColor;
  37. screenClearMode = glClearMode;
  38. swapInterval = intervalToSwap;
  39. fps = framerate;
  40. readyForFrame = true;
  41. curFrame = 0;
  42. deltaTime = 0;
  43. pause = false;
  44.  
  45. //GLFW
  46. if (!GLFWinitialize())
  47. {
  48. std::cout << "ERROR_CODE::0::ERROR::GLFW - GLFW failed to initialize!" << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. std::cout << "Creating game window!" << std::endl;
  52. const char * titleText = screenTitle.c_str();
  53. window = glfwCreateWindow(screenWidth, screenHeight, titleText, nullptr, nullptr);
  54. if (window == nullptr)
  55. {
  56. std::cout << "ERROR_CODE::1::ERROR::GLFW - GLFW failed window creation!" << std::endl;
  57. glfwTerminate();
  58. return EXIT_FAILURE;
  59. }
  60. glfwMakeContextCurrent(window);
  61.  
  62. //GLEW
  63. std::cout << "Setting up GLEW!" << std::endl;
  64. glewExperimental = GL_TRUE;
  65. if (glewInit() != GLEW_OK)
  66. {
  67. std::cout << "ERROR_CODE::2::ERROR::GLEW - Glew failed to initialize!" << std::endl;
  68. glfwTerminate();
  69. return EXIT_FAILURE;
  70. }
  71.  
  72. //viewport
  73. int wid, hei;
  74. glfwGetFramebufferSize(window, &wid, &hei);
  75. glViewport(0, 0, wid, hei);
  76.  
  77. //Done! :D
  78. engineIsInitialized = true;
  79. return EXIT_SUCCESS;
  80. }
  81.  
  82. bool Engine::GLFWinitialize()
  83. {
  84. std::cout << "Setting up GLFW!" << std::endl;
  85. if (!glfwInit())
  86. {
  87. return false;
  88. }
  89. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, screenGLVersionMajor);
  90. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, screenGLVersionMinor);
  91. glfwWindowHint(GLFW_OPENGL_PROFILE, screenProfile);
  92. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, screenCompatability);
  93. glfwWindowHint(GLFW_RESIZABLE, screenResizable);
  94. return true;
  95. }
  96.  
  97. void Engine::Start()
  98. {
  99. std::cout << "Starting game!" << std::endl;
  100. gameHasStarted = true;
  101.  
  102. //create event
  103. std::vector<Room> roomRunOrder = refreshRoomRunOrder();
  104. while (!roomRunOrder.empty())
  105. {
  106. Room curRoom = roomRunOrder.back();
  107. roomRunOrder.pop_back();
  108. curRoom._create();
  109. }
  110.  
  111. while (!glfwWindowShouldClose(this->window))
  112. {
  113. glfwPollEvents();
  114. if (readyForFrame && !pause)
  115. {
  116. //setup room run order
  117. roomRunOrder = refreshRoomRunOrder();
  118.  
  119. //loop through rooms and call events
  120. while (!roomRunOrder.empty())
  121. {
  122. Room curRoom = roomRunOrder.back();
  123. roomRunOrder.pop_back();
  124. curRoom._background_step();
  125. }
  126.  
  127. //The current room
  128. glClearColor(screenSplashColor.red, screenSplashColor.green, screenSplashColor.blue, screenSplashColor.alpha);
  129. glClear(screenClearMode);
  130. Room curRoom = getCurrentRoom();
  131. curRoom._early_control();
  132. curRoom._control();
  133. curRoom._late_control();
  134. curRoom._draw();
  135. curRoom._GUI();
  136.  
  137.  
  138. //finalize
  139. glfwSwapBuffers(window);
  140. }
  141.  
  142. //fps capping
  143. if (readyForFrame)
  144. {
  145. readyForFrame = false;
  146. }
  147. double currentFrame = glfwGetTime();
  148. deltaTime = currentFrame - lastFrame;
  149.  
  150. if (1.0 / fps < deltaTime)
  151. {
  152. readyForFrame = true;
  153. curFrame++;
  154. lastFrame = currentFrame;
  155. if (curFrame % fps == 0)
  156. {
  157. std::cout << "FPS: " << (ceil(1 / deltaTime)) << std::endl;
  158. }
  159. }
  160.  
  161. }
  162. std::cout << "Game loop stopped!" << std::endl;
  163. }
  164.  
  165. void Engine::Stop()
  166. {
  167. if (engineIsInitialized && gameHasStarted)
  168. {
  169. std::cout << "Stopping game!" << std::endl;
  170. glfwSetWindowShouldClose(window, GL_TRUE);
  171. }
  172. }
  173.  
  174. void Engine::setSwapInterval(int interval)
  175. {
  176. if (engineIsInitialized)
  177. {
  178. swapInterval = interval;
  179. glfwSwapInterval(swapInterval);
  180. }
  181. }
  182.  
  183. int Engine::getSwapInterval()
  184. {
  185. if (engineIsInitialized)
  186. {
  187. return swapInterval;
  188. }
  189. return NULL;
  190. }
  191.  
  192. double Engine::getDeltaTime()
  193. {
  194. if (engineIsInitialized)
  195. {
  196. return deltaTime;
  197. }
  198. return NULL;
  199. }
  200.  
  201. int Engine::getFrameRate()
  202. {
  203. if (engineIsInitialized)
  204. {
  205. return fps;
  206. }
  207. return NULL;
  208. }
  209.  
  210. void Engine::setFrameRate(int fps)
  211. {
  212. if (engineIsInitialized)
  213. {
  214. this->fps = fps;
  215. }
  216. }
  217.  
  218. Demand Engine::getTextureLoadingOnDemand()
  219. {
  220. if (engineIsInitialized)
  221. {
  222. return textureLoadingOnDemand;
  223. }
  224. return NullDemand;
  225. }
  226.  
  227. void Engine::setTextureLoadingOnDemand(Demand textureLoadingOnDemand)
  228. {
  229. if (!gameHasStarted && engineIsInitialized)
  230. {
  231. this->textureLoadingOnDemand = textureLoadingOnDemand;
  232. }
  233. }
  234.  
  235. Demand Engine::getRoomLoadingOnDemand()
  236. {
  237. if (engineIsInitialized)
  238. {
  239. return roomLoadingOnDemand;
  240. }
  241. return NullDemand;
  242. }
  243.  
  244. void Engine::setRoomLoadingOnDemand(Demand roomLoadingOnDemand)
  245. {
  246. if (!gameHasStarted && engineIsInitialized)
  247. {
  248. this->roomLoadingOnDemand = roomLoadingOnDemand;
  249. }
  250. }
  251.  
  252. bool Engine::addTexture(TextureInfo texture)
  253. {
  254. if (engineIsInitialized)
  255. {
  256. bool matched = false;
  257. for (int inc = 0; inc < textures.size(); inc++)
  258. {
  259. if (textures[inc].name == texture.name)
  260. {
  261. matched = true;
  262. }
  263. }
  264. if (!matched)
  265. {
  266. texture.id = textureIdIncrement;
  267. textureIdIncrement++;
  268. textures.push_back(texture);
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274.  
  275. TextureInfo* Engine::getTexture(std::string textureName)
  276. {
  277. if (engineIsInitialized)
  278. {
  279. for (int inc = 0; inc < textures.size(); inc++)
  280. {
  281. if (textures[inc].name == textureName)
  282. {
  283. return &textures[inc];
  284. }
  285. }
  286. }
  287. return nullptr;
  288. }
  289.  
  290. Room* Engine::getCurrentRoom()
  291. {
  292. if (engineIsInitialized)
  293. {
  294. return &currentRoom;
  295. }
  296. return nullptr;
  297. }
  298.  
  299. bool Engine::removeRoom(std::string name)
  300. {
  301. for (int inc = 0; inc < rooms.size(); inc++)
  302. {
  303. if (rooms[inc].name == name)
  304. {
  305. rooms.erase(rooms.begin() + inc);
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311.  
  312. bool Engine::addRoom(RoomInfo room)
  313. {
  314. if (engineIsInitialized)
  315. {
  316. bool matched = false;
  317. for (int inc = 0; inc < rooms.size(); inc++)
  318. {
  319. if (rooms[inc].name == room.name)
  320. {
  321. matched = true;
  322. }
  323. }
  324. if (!matched)
  325. {
  326. room.id = roomIdIncrement;
  327. roomIdIncrement++;
  328. rooms.push_back(room);
  329. return true;
  330. }
  331. }
  332. return false;
  333. }
  334.  
  335. bool Engine::getShouldDrawSplashColor()
  336. {
  337. if (engineIsInitialized)
  338. {
  339. return screenSplashColorShouldDraw;
  340. }
  341. return false;
  342. }
  343.  
  344. void Engine::setShouldDrawSplashColor(bool shouldDraw)
  345. {
  346. if (engineIsInitialized)
  347. {
  348. screenSplashColorShouldDraw = shouldDraw;
  349. }
  350. }
  351.  
  352. float Engine::getAlpha()
  353. {
  354. if (engineIsInitialized)
  355. {
  356. return screenSplashColor.alpha;
  357. }
  358. return -1;
  359. }
  360.  
  361. float Engine::getBlue()
  362. {
  363. if (engineIsInitialized)
  364. {
  365. return screenSplashColor.blue;
  366. }
  367. return -1;
  368. }
  369.  
  370. float Engine::getGreen()
  371. {
  372. if (engineIsInitialized)
  373. {
  374. return screenSplashColor.green;
  375. }
  376. return -1;
  377. }
  378.  
  379. float Engine::getRed()
  380. {
  381. if (engineIsInitialized)
  382. {
  383. return screenSplashColor.red;
  384. }
  385. return -1;
  386. }
  387.  
  388. Color Engine::getSplashColor()
  389. {
  390. if (engineIsInitialized)
  391. {
  392. return screenSplashColor;
  393. }
  394. return Color(-1, -1, -1, -1);
  395. }
  396.  
  397. void Engine::changeSplashColor(Color splashColor)
  398. {
  399. if (engineIsInitialized)
  400. {
  401. screenSplashColor = splashColor;
  402. }
  403. }
  404.  
  405. void Engine::changeSplashColor(float red, float green, float blue, float alpha)
  406. {
  407. if (engineIsInitialized)
  408. {
  409. screenSplashColor.red = red;
  410. screenSplashColor.green = green;
  411. screenSplashColor.blue = blue;
  412. screenSplashColor.alpha = alpha;
  413. }
  414. }
  415.  
  416. bool Engine::getIfPaused()
  417. {
  418. if (engineIsInitialized && gameHasStarted)
  419. {
  420. return pause;
  421. }
  422. return true;
  423. }
  424.  
  425. void Engine::setPause(bool pause)
  426. {
  427. if (engineIsInitialized && gameHasStarted)
  428. {
  429. this->pause = pause;
  430. }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment