SHARE
TWEET

Untitled

a guest Dec 15th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <SDL2/SDL.h>
  3. #include "../../include/glad/glad.h"
  4. #include "../../include/imgui/imgui.h"
  5. #include "../../include/imgui/impl/imgui_impl_sdl.h"
  6. #include "../../include/imgui/impl/imgui_impl_opengl3.h"
  7.  
  8. int main(int argc, char const *argv[])
  9. {
  10.     SDL_Window *window = NULL;
  11.     int width = 700, height = 500;
  12.  
  13.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  14.     {
  15.         std::cout << "Failed to initialize sdl2: " << SDL_GetError() << std::endl;
  16.         return -1;
  17.     }
  18.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  19.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  20.     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  21.  
  22.     SDL_GL_SetAttribute(
  23.         SDL_GL_CONTEXT_PROFILE_MASK,
  24.         SDL_GL_CONTEXT_PROFILE_CORE);
  25.     const char *glsl_version = "#version 150";
  26.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
  27.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  28.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  29.  
  30.     SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  31.  
  32.     window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
  33.     SDL_SetWindowMinimumSize(window, width, height);
  34.  
  35.     SDL_GLContext gl_context = SDL_GL_CreateContext(window);
  36.     SDL_GL_MakeCurrent(window, gl_context);
  37.  
  38.     SDL_GL_SetSwapInterval(1); //VSync
  39.  
  40.     bool err = gladLoadGL() == 0;
  41.     glViewport(0, 0, width, height);
  42.  
  43.     IMGUI_CHECKVERSION();
  44.     ImGui::CreateContext();
  45.     ImGuiIO &io = ImGui::GetIO();
  46.     (void)io;
  47.     io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
  48.     ImGuiStyle &style = ImGui::GetStyle();
  49.  
  50.     ImGui::StyleColorsDark();
  51.     ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
  52.     ImGui_ImplOpenGL3_Init(glsl_version);
  53.     style.WindowBorderSize = 0.0f;
  54.     style.WindowRounding = 4.0f;
  55.  
  56.     ImVec4 background = ImVec4(35 / 255.0f, 35 / 255.0f, 35 / 255.0f, 1.00f);
  57.  
  58.     bool show_demo_window = true;
  59.     bool loop = true;
  60.     while (loop)
  61.     {
  62.         SDL_Event event;
  63.         while (SDL_PollEvent(&event))
  64.         {
  65.             ImGui_ImplSDL2_ProcessEvent(&event);
  66.             if (event.type == SDL_QUIT)
  67.             {
  68.                 loop = false;
  69.             }
  70.             if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window))
  71.             {
  72.                 int w, h;
  73.                 SDL_GetWindowSize(window, &w, &h);
  74.             }
  75.             if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
  76.                 loop = false;
  77.         }
  78.         ImGui_ImplOpenGL3_NewFrame();
  79.         ImGui_ImplSDL2_NewFrame(window);
  80.         ImGui::NewFrame();
  81.  
  82.         //ImGui Window
  83.         if (show_demo_window)
  84.             ImGui::ShowDemoWindow(&show_demo_window);
  85.  
  86.         ImGui::Render();
  87.         glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
  88.         glClearColor(background.x, background.y, background.z, background.w);
  89.         glClear(GL_COLOR_BUFFER_BIT);
  90.         //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  91.         ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  92.         SDL_GL_SwapWindow(window);
  93.     }
  94.     ImGui_ImplOpenGL3_Shutdown();
  95.     ImGui_ImplSDL2_Shutdown();
  96.     ImGui::DestroyContext();
  97.     SDL_GL_DeleteContext(gl_context);
  98.     SDL_DestroyWindow(window);
  99.  
  100.     SDL_Quit();
  101.     return 0;
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top