move everything to functions

This commit is contained in:
Crispy 2023-04-02 16:53:04 +02:00
parent ef25392907
commit 6aef8f92d7

View file

@ -12,6 +12,7 @@ static vr::ETrackingUniverseOrigin TRACKING_UNIVERSE = vr::ETrackingUniverseOrig
uint16_t width;
uint16_t height;
bool should_exit = false;
Display *xdisplay;
Window root_window;
@ -24,11 +25,7 @@ vr::Texture_t vr_texture;
GLuint screen_texture;
GLFWwindow *gl_window;
void cleanup(int _sig = 0);
int main(int argc, char **argv)
{
signal(SIGINT, cleanup);
void init_x11()
{
xdisplay = XOpenDisplay(nullptr);
assert(xdisplay != nullptr);
@ -40,6 +37,7 @@ int main(int argc, char **argv)
height = attributes.height;
}
void init_glfw()
{
assert(glfwInit() == true);
glfwWindowHint(GLFW_VISIBLE, false);
@ -47,36 +45,7 @@ int main(int argc, char **argv)
assert(gl_window != nullptr);
glfwMakeContextCurrent(gl_window);
printf("Created GLFW context\n");
}
{
vr::EVRInitError init_err;
ovr_sys = vr::VR_Init(&init_err, vr::EVRApplicationType::VRApplication_Overlay);
assert(init_err == 0);
ovr_overlay = vr::VROverlay();
printf("Initialized openvr overlay\n");
}
{
auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &main_overlay);
assert(overlay_err == 0);
ovr_overlay->ShowOverlay(main_overlay);
ovr_overlay->SetOverlayWidthInMeters(main_overlay, 2.5f);
uint8_t col[4] = {20, 50, 50, 255};
ovr_overlay->SetOverlayRaw(main_overlay, &col, 1, 1, 4);
printf("Created overlay instance\n");
}
{
vr::HmdMatrix34_t transform;
auto err = ovr_overlay->GetOverlayTransformAbsolute(main_overlay, &TRACKING_UNIVERSE, &transform);
assert(err == 0);
transform.m[1][1] = -1;
err = ovr_overlay->SetOverlayTransformAbsolute(main_overlay, TRACKING_UNIVERSE, &transform);
assert(err == 0);
}
{
glGenTextures(1, &screen_texture);
glBindTexture(GL_TEXTURE_2D, screen_texture);
@ -85,22 +54,56 @@ int main(int argc, char **argv)
vr_texture.handle = (void *)(uintptr_t)screen_texture;
}
while (1)
void init_vr()
{
vr::EVRInitError init_err;
ovr_sys = vr::VR_Init(&init_err, vr::EVRApplicationType::VRApplication_Overlay);
assert(init_err == 0);
ovr_overlay = vr::VROverlay();
printf("Initialized openvr overlay\n");
}
void init_overlay()
{
auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &main_overlay);
assert(overlay_err == 0);
ovr_overlay->ShowOverlay(main_overlay);
ovr_overlay->SetOverlayWidthInMeters(main_overlay, 2.5f);
uint8_t col[4] = {20, 50, 50, 255};
ovr_overlay->SetOverlayRaw(main_overlay, &col, 1, 1, 4);
printf("Created overlay instance\n");
vr::HmdMatrix34_t transform;
auto err = ovr_overlay->GetOverlayTransformAbsolute(main_overlay, &TRACKING_UNIVERSE, &transform);
assert(err == 0);
transform.m[1][1] = -1; // flip Y axis
err = ovr_overlay->SetOverlayTransformAbsolute(main_overlay, TRACKING_UNIVERSE, &transform);
assert(err == 0);
}
void cleanup()
{
printf("\nShutting down\n");
vr::VR_Shutdown();
glfwDestroyWindow(gl_window);
glfwTerminate();
exit(0);
}
void render_desktop()
{
auto frame = XGetImage(xdisplay, root_window, 0, 0, width, height, AllPlanes, ZPixmap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, frame->data);
XDestroyImage(frame);
{
auto set_err = ovr_overlay->SetOverlayTexture(main_overlay, &vr_texture);
if (set_err)
{
printf("error setting texture: %d\n", set_err);
// if (set_err)
// printf("error setting texture: %d\n", set_err);
assert(set_err == 0);
}
}
{ // update cursor position
void update_cursor()
{
int pix_x, pix_y;
{
Window _t1;
@ -117,19 +120,28 @@ int main(int argc, char **argv)
ovr_overlay->SetOverlayCursorPositionOverride(main_overlay, &pos);
}
void interrupted(int _sig)
{
should_exit = true;
}
int main()
{
init_x11();
init_glfw();
init_vr();
init_overlay();
signal(SIGINT, interrupted);
while (!should_exit)
{
render_desktop();
update_cursor();
glfwSwapBuffers(gl_window);
usleep(1000000 / FRAMERATE);
}
cleanup();
return 0;
}
void cleanup(int _sig)
{
printf("\nCleaning up\n");
vr::VR_Shutdown();
glfwDestroyWindow(gl_window);
glfwTerminate();
printf("Shutting down\n");
exit(0);
}