Changeset 0e6ecf3 in opengl-game


Ignore:
Timestamp:
Jul 19, 2019, 8:50:06 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
8667f76
Parents:
75108ef
Message:

Create a game gui implementation using glfw3 and move window create/destruction and Vulkan surface creation to the game gui

Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • game-gui-sdl.cpp

    r75108ef r0e6ecf3  
    11#include "game-gui-sdl.hpp"
    2 
    3 #include <SDL2/SDL.h>
    4 
    5 #include <iostream>
    6 
    7 using namespace std;
    82
    93bool GameGui_SDL::Init() {
     
    2216   SDL_Quit();
    2317}
     18
     19void* GameGui_SDL::CreateWindow(const string& title, unsigned int width, unsigned int height) {
     20   // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
     21   // otherwise you will not receive a High DPI OpenGL canvas.
     22   window = SDL_CreateWindow(title.c_str(),
     23               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
     24               width, height,
     25               SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
     26
     27   return window;
     28}
     29
     30void GameGui_SDL::DestroyWindow() {
     31   // TODO: This function can throw some errors. They should be handled
     32   SDL_DestroyWindow(window);
     33}
     34
     35bool GameGui_SDL::CreateVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
     36   return SDL_Vulkan_CreateSurface(window, instance, surface) ?
     37      RTWO_SUCCESS : RTWO_ERROR;
     38}
  • game-gui-sdl.hpp

    r75108ef r0e6ecf3  
     1#ifndef _GAME_GUI_SDL_H
     2#define _GAME_GUI_SDL_H
     3
    14#include "game-gui.hpp"
     5
     6#include <SDL2/SDL.h>
     7#include <SDL2/SDL_vulkan.h>
    28
    39class GameGui_SDL : public GameGui {
     
    511      bool Init();
    612      void Shutdown();
     13
     14      void* CreateWindow(const string& title, unsigned int width, unsigned int height);
     15      void DestroyWindow();
     16
     17      bool CreateVulkanSurface(VkInstance instance, VkSurfaceKHR* surface);
     18
     19   private:
     20      SDL_Window* window;
    721};
     22
     23#endif // _GAME_GUI_SDL_H
  • game-gui.hpp

    r75108ef r0e6ecf3  
     1#ifndef _GAME_GUI_H
     2#define _GAME_GUI_H
     3
     4#include <vulkan/vulkan.h>
     5
     6#include <string>
     7
     8using namespace std;
     9
    110#define RTWO_SUCCESS true
    211#define RTWO_ERROR false
     
    817      virtual bool Init() = 0;
    918      virtual void Shutdown() = 0;
     19
     20      virtual void* CreateWindow(const string& title, unsigned int width, unsigned int height) = 0;
     21      virtual void DestroyWindow() = 0;
     22
     23      virtual bool CreateVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) = 0;
    1024};
     25
     26#endif // _GAME_GUI_H
  • makefile

    r75108ef r0e6ecf3  
    4242endif
    4343
    44 LIBS = `pkg-config --static --libs sdl2`
     44LIBS = `pkg-config --static --libs sdl2 glfw3`
    4545ifeq ($(OS),Darwin)
    4646        LIBS := $(VULKAN_SDK_PATH)/lib/libvulkan.dylib $(LIBS)
     
    5252LIB_FLAGS = $(LIB_PATHS) $(LIBS)
    5353
    54 vulkangame: vulkan-game.cpp game-gui-sdl.cpp
     54vulkangame: vulkan-game.cpp game-gui-sdl.cpp game-gui-glfw.cpp
    5555        $(CC) $(CXX_FLAGS) -o $@ $^ $(LIB_FLAGS)
    5656
  • vulkan-game.cpp

    r75108ef r0e6ecf3  
    1 #include <vulkan/vulkan.h>
    2 
    3 #include <SDL2/SDL.h>
    4 #include <SDL2/SDL_vulkan.h>
     1#include "game-gui-glfw.hpp"
     2
     3#include "game-gui-sdl.hpp"
    54
    65//#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath>
     
    1110#include <glm/mat4x4.hpp>
    1211
     12#include <fstream>
    1313#include <iostream>
     14#include <optional>
     15#include <set>
    1416#include <vector>
    15 #include <set>
    16 #include <stdexcept>
    17 #include <cstdlib>
    18 #include <optional>
    19 #include <algorithm>
    20 #include <fstream>
    21 
    22 #include "game-gui-sdl.hpp"
    2317
    2418using namespace std;
     
    137131            return RTWO_ERROR;
    138132         } else {
    139             // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
    140             // otherwise you will not receive a High DPI OpenGL canvas.
    141 
    142             // TODO: Move this into some generic method in game-gui-sdl
    143             window = SDL_CreateWindow("Vulkan Game",
    144                SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
    145                SCREEN_WIDTH, SCREEN_HEIGHT,
    146                SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
     133            window = (SDL_Window*) gui->CreateWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT);
    147134
    148135            if (window == nullptr) {
     
    274261         }
    275262
    276          if (!SDL_Vulkan_CreateSurface(window, instance, &surface)) {
     263         if (gui->CreateVulkanSurface(instance, &surface) == RTWO_ERROR) {
    277264            throw runtime_error("failed to create window surface!");
    278265         }
    279 
    280          /*
    281          if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
    282             throw runtime_error("failed to create window surface!");
    283          }
    284          */
    285266      }
    286267
     
    1014995         vkDestroyInstance(instance, nullptr);
    1015996
    1016          // TODO: Move this into some generic method in game-gui-sdl
    1017          SDL_DestroyWindow(window);
    1018 
     997         gui->DestroyWindow();
    1019998         gui->Shutdown();
    1020999         delete gui;
     
    10291008
    10301009         return VK_FALSE;
    1031 }
     1010      }
    10321011
    10331012      static vector<char> readFile(const string& filename) {
Note: See TracChangeset for help on using the changeset viewer.