setup first menu bar and possiblity to quit application.

This commit is contained in:
zxvfxwing
2022-07-09 00:44:55 +02:00
parent 8db60d993b
commit 55953b0388
4 changed files with 186 additions and 71 deletions
+38
View File
@@ -0,0 +1,38 @@
#ifndef MHACHEWINDOW_H
#define MHACHEWINDOW_H
#include <GLFW/glfw3.h>
class MHacheWindow {
private:
int _width;
int _height;
GLFWwindow* _window;
static constexpr const char* _title = "MHache 0.1";
static constexpr const char* _GLSL_version = "#version 130";
void prepare_frame();
void render_frame();
void build_window();
void build_menu();
private:
static void callback_glfw_error(int error, const char* description);
static void callback_glfw_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
MHacheWindow() = delete;
MHacheWindow(const MHacheWindow&) = delete;
MHacheWindow(MHacheWindow&&) = delete;
public:
MHacheWindow(int width, int height);
~MHacheWindow();
bool ready();
void display();
};
#endif
+7 -1
View File
@@ -3,11 +3,12 @@ find_package(OpenGL REQUIRED)
add_executable(
${PROJECT_NAME}
main.cpp
mhache_window.cpp
)
target_compile_options(
${PROJECT_NAME} PUBLIC
-std=c++11
-std=c++17
-Wall
-Wextra
-pedantic-errors
@@ -19,3 +20,8 @@ target_link_libraries(
glfw
imgui
)
target_include_directories(
${PROJECT_NAME} PUBLIC
../include
)
+4 -70
View File
@@ -1,75 +1,9 @@
#include <cstdlib>
#include <cstdio>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
static void callback_glfw_error(int error, const char* description)
{
fprintf(stderr, "GLFW error (%d) = %s\n", error, description);
}
#include <memory>
#include "mhache_window.h"
int main(int argc, char** argv)
{
glfwSetErrorCallback(&callback_glfw_error);
if (!glfwInit())
return 1;
constexpr const char* glsl_version = "#version 450";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
GLFWwindow* window = glfwCreateWindow(620, 480, argv[0], nullptr, nullptr);
if (!window)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
ImGui::Begin(argv[0]);
// ImGui::BeginMenu("App");
// ImGui::EndMenu();
ImGui::End();
}
ImGui::Render();
int window_w, window_h;
glfwGetFramebufferSize(window, &window_w, &window_h);
glViewport(0, 0, window_w, window_h);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
auto window = std::make_unique<MHacheWindow>(620, 480);
window->display();
return 0;
}
+137
View File
@@ -0,0 +1,137 @@
#include "mhache_window.h"
#include <cstdlib>
#include <cstdio>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
MHacheWindow::MHacheWindow(int width, int height)
: _width(width), _height(height), _window(nullptr)
{
glfwSetErrorCallback(&MHacheWindow::callback_glfw_error);
if (GLFW_TRUE == glfwInit()) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
_window = glfwCreateWindow(_width, _height, _title, nullptr, nullptr);
if (_window) {
glfwSetKeyCallback(_window, &MHacheWindow::callback_glfw_keyboard);
glfwMakeContextCurrent(_window);
glfwSwapInterval(1);
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(_window, true);
ImGui_ImplOpenGL3_Init(_GLSL_version);
}
}
}
MHacheWindow::~MHacheWindow()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(_window);
glfwTerminate();
}
void
MHacheWindow::callback_glfw_error(int error, const char* description)
{
fprintf(stderr, "GLFW error (%d) = %s\n", error, description);
}
void
MHacheWindow::callback_glfw_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (GLFW_PRESS == action)
{
if (GLFW_KEY_F4 == key && (GLFW_KEY_LEFT_ALT == key || GLFW_KEY_RIGHT_ALT == key)) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
}
void
MHacheWindow::prepare_frame()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void
MHacheWindow::render_frame()
{
ImGui::Render();
glfwGetFramebufferSize(_window, &_width, &_height);
glViewport(0, 0, _width, _height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(_window);
}
void
MHacheWindow::build_window()
{
ImGuiWindowFlags window_flags = 0;
window_flags |= ImGuiWindowFlags_NoBackground;
window_flags |= ImGuiWindowFlags_NoDecoration;
window_flags |= ImGuiWindowFlags_MenuBar;
window_flags |= ImGuiWindowFlags_NoSavedSettings;
window_flags |= ImGuiWindowFlags_NoMove;
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
if (main_viewport) {
ImGui::SetNextWindowPos(main_viewport->WorkPos);
ImGui::SetNextWindowSize(main_viewport->WorkSize);
}
ImGui::Begin(_title, nullptr, window_flags);
//--
build_menu();
//--
ImGui::End();
}
void
MHacheWindow::build_menu()
{
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("App")) {
if (ImGui::MenuItem("Quit", "Alt+F4")) {
glfwSetWindowShouldClose(_window, GLFW_TRUE);
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
}
bool
MHacheWindow::ready()
{
return (_window && !glfwWindowShouldClose(_window));
}
void
MHacheWindow::display()
{
while (ready()) {
glfwPollEvents();
prepare_frame();
build_window();
render_frame();
}
}