Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "engine.h"
- 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)
- {
- //variable assigning
- std::cout << "Starting OpenStellar Engine! Please wait while it initializes!" << std::endl;
- std::cout << "Assigning Variables!" << std::endl;
- if (resizable)
- {
- resizable = GL_TRUE;
- }
- else
- {
- resizable = GL_FALSE;
- }
- screenTitle = titleBarText;
- if (fullscreen == true)
- {
- screenFullscreen = glfwGetPrimaryMonitor();
- }
- else
- {
- screenFullscreen = nullptr;
- }
- textureIdIncrement = 0;
- roomIdIncrement = 0;
- loopHasStarted = false;
- screenGLVersionMajor = openGLVersionMajor;
- screenGLVersionMinor = openGLVersionMinor;
- screenProfile = profile;
- screenCompatability = compatiblity;
- screenWidth = width;
- screenHeight = height;
- screenSplashColor = splashColor;
- screenClearMode = glClearMode;
- swapInterval = intervalToSwap;
- fps = framerate;
- readyForFrame = true;
- curFrame = 0;
- deltaTime = 0;
- pause = false;
- //GLFW
- if (!GLFWinitialize())
- {
- std::cout << "ERROR_CODE::0::ERROR::GLFW - GLFW failed to initialize!" << std::endl;
- return EXIT_FAILURE;
- }
- std::cout << "Creating game window!" << std::endl;
- const char * titleText = screenTitle.c_str();
- window = glfwCreateWindow(screenWidth, screenHeight, titleText, nullptr, nullptr);
- if (window == nullptr)
- {
- std::cout << "ERROR_CODE::1::ERROR::GLFW - GLFW failed window creation!" << std::endl;
- glfwTerminate();
- return EXIT_FAILURE;
- }
- glfwMakeContextCurrent(window);
- //GLEW
- std::cout << "Setting up GLEW!" << std::endl;
- glewExperimental = GL_TRUE;
- if (glewInit() != GLEW_OK)
- {
- std::cout << "ERROR_CODE::2::ERROR::GLEW - Glew failed to initialize!" << std::endl;
- glfwTerminate();
- return EXIT_FAILURE;
- }
- //viewport
- int wid, hei;
- glfwGetFramebufferSize(window, &wid, &hei);
- glViewport(0, 0, wid, hei);
- //Done! :D
- engineIsInitialized = true;
- return EXIT_SUCCESS;
- }
- bool Engine::GLFWinitialize()
- {
- std::cout << "Setting up GLFW!" << std::endl;
- if (!glfwInit())
- {
- return false;
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, screenGLVersionMajor);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, screenGLVersionMinor);
- glfwWindowHint(GLFW_OPENGL_PROFILE, screenProfile);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, screenCompatability);
- glfwWindowHint(GLFW_RESIZABLE, screenResizable);
- return true;
- }
- void Engine::Start()
- {
- std::cout << "Starting game!" << std::endl;
- gameHasStarted = true;
- //create event
- std::vector<Room> roomRunOrder = refreshRoomRunOrder();
- while (!roomRunOrder.empty())
- {
- Room curRoom = roomRunOrder.back();
- roomRunOrder.pop_back();
- curRoom._create();
- }
- while (!glfwWindowShouldClose(this->window))
- {
- glfwPollEvents();
- if (readyForFrame && !pause)
- {
- //setup room run order
- roomRunOrder = refreshRoomRunOrder();
- //loop through rooms and call events
- while (!roomRunOrder.empty())
- {
- Room curRoom = roomRunOrder.back();
- roomRunOrder.pop_back();
- curRoom._background_step();
- }
- //The current room
- glClearColor(screenSplashColor.red, screenSplashColor.green, screenSplashColor.blue, screenSplashColor.alpha);
- glClear(screenClearMode);
- Room curRoom = getCurrentRoom();
- curRoom._early_control();
- curRoom._control();
- curRoom._late_control();
- curRoom._draw();
- curRoom._GUI();
- //finalize
- glfwSwapBuffers(window);
- }
- //fps capping
- if (readyForFrame)
- {
- readyForFrame = false;
- }
- double currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- if (1.0 / fps < deltaTime)
- {
- readyForFrame = true;
- curFrame++;
- lastFrame = currentFrame;
- if (curFrame % fps == 0)
- {
- std::cout << "FPS: " << (ceil(1 / deltaTime)) << std::endl;
- }
- }
- }
- std::cout << "Game loop stopped!" << std::endl;
- }
- void Engine::Stop()
- {
- if (engineIsInitialized && gameHasStarted)
- {
- std::cout << "Stopping game!" << std::endl;
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- }
- void Engine::setSwapInterval(int interval)
- {
- if (engineIsInitialized)
- {
- swapInterval = interval;
- glfwSwapInterval(swapInterval);
- }
- }
- int Engine::getSwapInterval()
- {
- if (engineIsInitialized)
- {
- return swapInterval;
- }
- return NULL;
- }
- double Engine::getDeltaTime()
- {
- if (engineIsInitialized)
- {
- return deltaTime;
- }
- return NULL;
- }
- int Engine::getFrameRate()
- {
- if (engineIsInitialized)
- {
- return fps;
- }
- return NULL;
- }
- void Engine::setFrameRate(int fps)
- {
- if (engineIsInitialized)
- {
- this->fps = fps;
- }
- }
- Demand Engine::getTextureLoadingOnDemand()
- {
- if (engineIsInitialized)
- {
- return textureLoadingOnDemand;
- }
- return NullDemand;
- }
- void Engine::setTextureLoadingOnDemand(Demand textureLoadingOnDemand)
- {
- if (!gameHasStarted && engineIsInitialized)
- {
- this->textureLoadingOnDemand = textureLoadingOnDemand;
- }
- }
- Demand Engine::getRoomLoadingOnDemand()
- {
- if (engineIsInitialized)
- {
- return roomLoadingOnDemand;
- }
- return NullDemand;
- }
- void Engine::setRoomLoadingOnDemand(Demand roomLoadingOnDemand)
- {
- if (!gameHasStarted && engineIsInitialized)
- {
- this->roomLoadingOnDemand = roomLoadingOnDemand;
- }
- }
- bool Engine::addTexture(TextureInfo texture)
- {
- if (engineIsInitialized)
- {
- bool matched = false;
- for (int inc = 0; inc < textures.size(); inc++)
- {
- if (textures[inc].name == texture.name)
- {
- matched = true;
- }
- }
- if (!matched)
- {
- texture.id = textureIdIncrement;
- textureIdIncrement++;
- textures.push_back(texture);
- return true;
- }
- }
- return false;
- }
- TextureInfo* Engine::getTexture(std::string textureName)
- {
- if (engineIsInitialized)
- {
- for (int inc = 0; inc < textures.size(); inc++)
- {
- if (textures[inc].name == textureName)
- {
- return &textures[inc];
- }
- }
- }
- return nullptr;
- }
- Room* Engine::getCurrentRoom()
- {
- if (engineIsInitialized)
- {
- return ¤tRoom;
- }
- return nullptr;
- }
- bool Engine::removeRoom(std::string name)
- {
- for (int inc = 0; inc < rooms.size(); inc++)
- {
- if (rooms[inc].name == name)
- {
- rooms.erase(rooms.begin() + inc);
- return true;
- }
- }
- return false;
- }
- bool Engine::addRoom(RoomInfo room)
- {
- if (engineIsInitialized)
- {
- bool matched = false;
- for (int inc = 0; inc < rooms.size(); inc++)
- {
- if (rooms[inc].name == room.name)
- {
- matched = true;
- }
- }
- if (!matched)
- {
- room.id = roomIdIncrement;
- roomIdIncrement++;
- rooms.push_back(room);
- return true;
- }
- }
- return false;
- }
- bool Engine::getShouldDrawSplashColor()
- {
- if (engineIsInitialized)
- {
- return screenSplashColorShouldDraw;
- }
- return false;
- }
- void Engine::setShouldDrawSplashColor(bool shouldDraw)
- {
- if (engineIsInitialized)
- {
- screenSplashColorShouldDraw = shouldDraw;
- }
- }
- float Engine::getAlpha()
- {
- if (engineIsInitialized)
- {
- return screenSplashColor.alpha;
- }
- return -1;
- }
- float Engine::getBlue()
- {
- if (engineIsInitialized)
- {
- return screenSplashColor.blue;
- }
- return -1;
- }
- float Engine::getGreen()
- {
- if (engineIsInitialized)
- {
- return screenSplashColor.green;
- }
- return -1;
- }
- float Engine::getRed()
- {
- if (engineIsInitialized)
- {
- return screenSplashColor.red;
- }
- return -1;
- }
- Color Engine::getSplashColor()
- {
- if (engineIsInitialized)
- {
- return screenSplashColor;
- }
- return Color(-1, -1, -1, -1);
- }
- void Engine::changeSplashColor(Color splashColor)
- {
- if (engineIsInitialized)
- {
- screenSplashColor = splashColor;
- }
- }
- void Engine::changeSplashColor(float red, float green, float blue, float alpha)
- {
- if (engineIsInitialized)
- {
- screenSplashColor.red = red;
- screenSplashColor.green = green;
- screenSplashColor.blue = blue;
- screenSplashColor.alpha = alpha;
- }
- }
- bool Engine::getIfPaused()
- {
- if (engineIsInitialized && gameHasStarted)
- {
- return pause;
- }
- return true;
- }
- void Engine::setPause(bool pause)
- {
- if (engineIsInitialized && gameHasStarted)
- {
- this->pause = pause;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment