A better alternative to solution files

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: A better alternative to solution files

Post by albinopapa » October 3rd, 2018, 5:51 am

Lots of lambdas in MainWindow constructor

Code: Select all

    glfwSetKeyCallback(window.get(), [](GLFWwindow* window, int key, int scancode, int action, int mods)
    {
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) // TODO: maybe temporary
            glfwSetWindowShouldClose(window, GL_TRUE);

        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        if (action == GLFW_PRESS)
        {
            mw->kbd.OnKeyPressed(key);
        }
        else if (action == GLFW_RELEASE)
        {
            mw->kbd.OnKeyReleased(key);
        }
        else if (action == GLFW_REPEAT)
        {
            if (mw->kbd.AutorepeatIsEnabled())
                mw->kbd.OnKeyPressed(key);
        }
        // else don't handle GLFW_KEY_UNKNOWN 
    });
    glfwSetWindowFocusCallback(window.get(), [](GLFWwindow* window, int focused)
    {
        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        if (!focused)
        {
            // The window lost input focus
            mw->kbd.ClearState();
        }
    });
    glfwSetCharCallback(window.get(), [](GLFWwindow* window, std::uint32_t codepoint)
    {
        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        mw->kbd.OnChar(codepoint);
    });
    glfwSetCursorPosCallback(window.get(), [](GLFWwindow* window, double xpos, double ypos)
    {
        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        mw->mouse.OnMouseMove((int)xpos, (int)ypos);
        // std::cout << xpos << ":" << ypos << '\n'; // TODO: remove
    });
    glfwSetCursorEnterCallback(window.get(), [](GLFWwindow* window, int entered)
    {
        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        if (entered)
        {
            mw->mouse.OnMouseEnter();
        }
        else
        {
            // The cursor left the client area of the window
            auto left_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
            auto right_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
            double xpos, ypos;
            glfwGetCursorPos(window, &xpos, &ypos);
            short xposi = (short)xpos, yposi = (short)ypos;
            if (left_state == GLFW_PRESS || right_state == GLFW_PRESS)
            {                
                xposi = std::clamp((unsigned int)xposi, 0u, Graphics::ScreenWidth - 1);
                yposi = std::clamp((unsigned int)yposi, 0u, Graphics::ScreenHeight - 1);
                mw->mouse.OnMouseMove(xposi, yposi);
            }
            else
            {
                mw->mouse.OnMouseLeave();
                mw->mouse.OnLeftReleased(xposi, yposi);
                mw->mouse.OnRightReleased(xposi, yposi);
            }
        }
    });
    glfwSetMouseButtonCallback(window.get(), [](GLFWwindow* window, int button, int action, int mods)
    {
        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        double xpos, ypos;
        glfwGetCursorPos(window, &xpos, &ypos);
        if (action == GLFW_PRESS)
        {
            if (button == GLFW_MOUSE_BUTTON_LEFT)
            {
                mw->mouse.OnLeftPressed((int)xpos, (int)ypos);
            }
            else if (button == GLFW_MOUSE_BUTTON_RIGHT)
            {
                mw->mouse.OnRightPressed((int)xpos, (int)ypos);
            }
        }
        else if (action == GLFW_RELEASE)
        {
            if (button == GLFW_MOUSE_BUTTON_LEFT)
            {
                mw->mouse.OnLeftReleased((int)xpos, (int)ypos);
            }
            else if (button == GLFW_MOUSE_BUTTON_RIGHT)
            {
                mw->mouse.OnRightReleased((int)xpos, (int)ypos);
            }
        }
    });
    glfwSetScrollCallback(window.get(), [](GLFWwindow* window, double xoffset, double yoffset)
    {
        MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
        double xpos, ypos;
        glfwGetCursorPos(window, &xpos, &ypos);
        if (yoffset > 0.f) // TODO: check directions
        {
            mw->mouse.OnWheelDown((int)xpos, (int)ypos);
        }
        else if (yoffset < 0.f)
        {
            mw->mouse.OnWheelUp((int)xpos, (int)ypos);
        }
    });
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

PotatoWedgie
Posts: 6
Joined: June 12th, 2017, 10:33 pm

Re: A better alternative to solution files

Post by PotatoWedgie » October 3rd, 2018, 10:44 am

I'm kinda back in Unreal Land atm but i'll give it a test at some point...

Post Reply