Changeset d8cb15e in opengl-game


Ignore:
Timestamp:
Aug 30, 2019, 7:30:53 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
5529ab5
Parents:
d5f2b42
Message:

Implement GetError() in game-gui-glfw and start using game-gui-glfw in opengl-game to show a window

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • game-gui-glfw.cpp

    rd5f2b42 rd8cb15e  
    11#include "game-gui-glfw.hpp"
    22
     3string GameGui_GLFW::s_errorMessage;
     4
     5void glfw_error_callback(int error, const char* description) {
     6   GameGui_GLFW::s_errorMessage = description;
     7}
     8
     9string& GameGui_GLFW::GetError() {
     10   return GameGui_GLFW::s_errorMessage;
     11}
     12
    313bool GameGui_GLFW::Init() {
    4    return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_SUCCESS;
     14   GameGui_GLFW::s_errorMessage = "No error";
     15   glfwSetErrorCallback(glfw_error_callback);
     16
     17   return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
    518}
    619
  • game-gui-glfw.hpp

    rd5f2b42 rd8cb15e  
    1212class GameGui_GLFW : public GameGui {
    1313   public:
     14      string& GetError();
     15
     16      static string s_errorMessage; // Has to be public so that glfw_error_callback can access it
     17
    1418      bool Init();
    1519      void Shutdown();
     
    2933};
    3034
     35void glfw_error_callback(int error, const char* description);
     36
    3137#endif // _GAME_GUI_GLFW_H
  • makefile

    rd5f2b42 rd8cb15e  
    2525        $(CC) $^ $(DEP) $(CFLAGS) -o $@
    2626
    27 openglgame: main-opengl.cpp opengl-game.cpp
     27openglgame: main-opengl.cpp opengl-game.cpp game-gui-glfw.cpp
    2828        $(CC) $^ $(DEP) $(CFLAGS) -o $@
    2929
  • opengl-game.cpp

    rd5f2b42 rd8cb15e  
    33#include <iostream>
    44
     5#include "game-gui-glfw.hpp"
     6
    57using namespace std;
    68
    79OpenGLGame::OpenGLGame() {
     10   gui = nullptr;
     11   window = nullptr;
    812}
    913
     
    1216
    1317void OpenGLGame::run() {
    14    cout << "Running like a boss!" << endl;
     18   if (initWindow() == RTWO_ERROR) {
     19      return;
     20   }
     21   initOpenGL();
     22   mainLoop();
     23   cleanup();
    1524}
     25
     26bool OpenGLGame::initWindow() {
     27   gui = new GameGui_GLFW();
     28
     29   if (gui->Init() == RTWO_ERROR) {
     30      cout << "UI library could not be initialized!" << endl;
     31      cout << gui->GetError() << endl;
     32      return RTWO_ERROR;
     33   }
     34   cout << "GUI init succeeded" << endl;
     35
     36   window = (GLFWwindow*) gui->CreateWindow("OpenGL Game", SCREEN_WIDTH, SCREEN_HEIGHT);
     37   if (window == nullptr) {
     38      cout << "Window could not be created!" << endl;
     39      return RTWO_ERROR;
     40   }
     41
     42   return RTWO_SUCCESS;
     43}
     44
     45void OpenGLGame::initOpenGL() {
     46}
     47
     48void OpenGLGame::mainLoop() {
     49   while (!glfwWindowShouldClose(window)) {
     50      glfwPollEvents();
     51
     52      glfwSwapBuffers(window);
     53   }
     54}
     55
     56void OpenGLGame::cleanup() {
     57   gui->DestroyWindow();
     58   gui->Shutdown();
     59   delete gui;
     60}
  • opengl-game.hpp

    rd5f2b42 rd8cb15e  
    33
    44#include "game-gui-glfw.hpp"
     5
     6const int SCREEN_WIDTH = 800;
     7const int SCREEN_HEIGHT = 600;
    58
    69class OpenGLGame {
     
    1215
    1316   private:
     17      GameGui* gui;
     18      GLFWwindow* window;
     19
     20      bool initWindow();
     21      void initOpenGL();
     22      void mainLoop();
     23      void cleanup();
    1424};
    1525
Note: See TracChangeset for help on using the changeset viewer.