1 Star 0 Fork 1

波波/Gut

forked from 2144/Gut 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
OglApp.cpp 8.00 KB
一键复制 编辑 原始数据 按行查看 历史
2144 提交于 2023-09-27 14:31 . s
#include "OglApp.h"
namespace
{
OglApp* g_app = nullptr;
void glfw_error_callback(int error, const char* description)
{
printf("Glfw Error %d: %s\n", error, description);
}
void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (g_app)
{
g_app->OnKeyCallback(key, scancode, action, mode);
}
}
void glfw_cursorpos_callback(GLFWwindow* window, double x, double y)
{
if (g_app)
{
g_app->OnCursorPos(x, y);
}
}
void glfw_mouse_callback(GLFWwindow* window, int key, int action, int mode)
{
if (g_app)
{
g_app->OnMouse(key, action, mode);
}
}
void glfw_frame_callback(GLFWwindow* window, int width, int height)
{
if (g_app)
{
g_app->OnFrameBufferSize(width, height);
}
}
void glfw_monitor_callback(GLFWmonitor* window, int ev)
{
if (g_app)
{
g_app->OnMonitor(ev);
}
}
void glfw_window_focus_callback(GLFWwindow* window, int e)
{
if (g_app)
{
g_app->OnFocus(e);
}
}
void glfw_close_callback(GLFWwindow* window)
{
if (g_app)
{
g_app->OnClose();
}
}
void glfw_joystick_callback(int joy, int event)
{
if (g_app)
{
g_app->OnJoystick(joy, event);
}
}
void glfw_window_iconify_callback(GLFWwindow* window, int ev)
{
if (g_app)
{
g_app->OnWindowIconify(ev);
}
}
void glfw_scroll_callback(GLFWwindow* window, double x, double y)
{
if (g_app)
{
g_app->OnScroll(x, y);
}
}
void glfw_refresh_callback(GLFWwindow* window)
{
if (g_app)
{
g_app->OnRefresh();
}
}
void glfw_pos_callback(GLFWwindow*, int x, int y)
{
if (g_app)
{
g_app->OnWindowPos(x, y);
}
}
}
OglApp::OglApp()
{
g_app = this;
}
bool OglApp::InitOpengl(const GLFWConfig& config)
{
if (!glfwInit())
{
LOGE("canont init glfw");
return 0;
}
glfwSetErrorCallback(glfw_error_callback);
// Decide GL+GLSL versions
// #if defined(IMGUI_IMPL_OPENGL_ES2)
// // GL ES 2.0 + GLSL 100
// const char *glsl_version = "#version 100";
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
// glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
// #elif defined(__APPLE__)
// // GL 3.2 + GLSL 150
// const char *glsl_version = "#version 150";
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
// #else
// // GL 3.0 + GLSL 130
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// // glfwWindowHint(GLFW_SAMPLES, 4);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
// #endif
#ifdef __linux__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif // DEBUG
window = glfwCreateWindow(config.width, config.height, config.title, nullptr, nullptr);
glfwSetWindowPos(window, config.posX, config.posY);
state.mClientWidth = config.width;
state.mClientHeight = config.height;
state.mClientPosX = config.posX;
state.mClientPosY = config.posY;
if (!window)
{
printf("Failed to create GLFW window");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, glfw_frame_callback);
glfwSetKeyCallback(window, glfw_key_callback);
glfwSetCursorPosCallback(window, glfw_cursorpos_callback);
glfwSetMouseButtonCallback(window, glfw_mouse_callback);
glfwSetWindowFocusCallback(window, glfw_window_focus_callback);
glfwSetWindowCloseCallback(window, glfw_close_callback);
glfwSetScrollCallback(window, glfw_scroll_callback);
glfwSetWindowPosCallback(window, glfw_pos_callback);
glfwSetWindowIconifyCallback(window, glfw_window_iconify_callback);
glfwSetJoystickCallback(glfw_joystick_callback);
glfwSetMonitorCallback(glfw_monitor_callback);
// glfwSetClipboardString(window, "hello world");
glfwSetWindowRefreshCallback(window, glfw_refresh_callback);
glfwSwapInterval(1); // Enable vsync
#ifdef _WIN32
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return 0;
}
#endif
OnFrameBufferSize(state.mClientWidth, state.mClientHeight);
return true;
};
const float OglApp::GetAspect()
{
if (state.mClientHeight != 0)
{
return (float)state.mClientWidth / (float)state.mClientHeight;
}
return 1.0f;
};
void OglApp::SetFullScreen()
{
glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, state.mClientWidth, state.mClientHeight, GLFW_DONT_CARE);
state.mFullScreen = true;
}
void OglApp::SetWindowMode()
{
glfwSetWindowMonitor(window, NULL, state.mClientPosX, state.mClientPosY, state.mClientWidth, state.mClientHeight, GLFW_DONT_CARE);
state.mFullScreen = false;
}
void OglApp::CloseWindow()
{
glfwSetWindowShouldClose(window, true);
}
bool OglApp::Create(const GLFWConfig& config)
{
if(!InitOpengl(config))
{
return false;
}
return true;
}
void OglApp::Execute()
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
OnFrameRender();
OnTick();
OnAfterUpdate();
glfwSwapBuffers(window);
}
}
void OglApp::OnCursorPos(double x, double y)
{
inputSystem.OnCursorPos(x, y);
}
void OglApp::OnMouse(int key, int action, int mode)
{
inputSystem.OnMouse(key, action, mode);
}
void OglApp::OnKeyCallback(int key, int scancode, int action, int mode)
{
inputSystem.OnKeyCallback(key, scancode, action, mode);
}
void OglApp::OnJoystick(int joy, int event)
{
inputSystem.OnJoystick(joy, event);
}
void OglApp::OnWindowIconify(int ev)
{
}
void OglApp::OnRefresh()
{
}
void OglApp::OnWindowPos(int x, int y)
{
if (!state.mFullScreen)
{
state.mClientPosX = x;
state.mClientPosY = y;
}
}
void OglApp::OnTick()
{
const char* name = glfwGetJoystickName(GLFW_JOYSTICK_1);
{
auto& joystick = inputSystem.joystick;
int axisCount;
const float* axis = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axisCount);
joystick.OnAxes(axis, axisCount);
int buttonCount;
const unsigned char* btns = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttonCount);
joystick.OnButton(btns, buttonCount);
}
}
void OglApp::OnAfterUpdate()
{
inputSystem.Clear();
}
void OglApp::OnDestroy()
{
glfwDestroyWindow(window);
glfwTerminate();
}
void OglApp::OnClose()
{
};
void OglApp::OnScroll(double x, double y)
{
inputSystem.OnScroll(x, y);
}
void OglApp::OnFrameRender()
{
glClearColor(0.5f, 1.0f, 0.52f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void OglApp::OnFrameBufferSize(int w, int h) {
state.mClientWidth = w;
state.mClientHeight = h;
glViewport(0, 0, w, h);
};
void OglApp::OnFocus(int e)
{
};
void OglApp::OnMonitor(int e)
{
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bobo110/gut.git
git@gitee.com:bobo110/gut.git
bobo110
gut
Gut
master

搜索帮助