winapp.hpp 5.52 KB
Newer Older
1
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
2 3 4 5 6 7 8 9 10 11 12
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#elif defined(__linux__)
# include <X11/X.h>
# include <X11/Xlib.h>
# include <X11/Xutil.h>
#endif

#include <string>

#include <GL/gl.h>
13
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
14 15 16 17 18
# include <GL/glu.h>
#elif defined(__linux__)
# include <GL/glx.h>
#endif

19
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
20 21 22 23 24 25 26 27 28 29 30 31 32
# define WINCLASS "WinAppWnd"
#endif

#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }

class WinApp
{
public:
    WinApp(int width, int height, std::string& window_name)
    {
        m_width       = width;
        m_height      = height;
        m_window_name = window_name;
33
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
34 35 36 37 38 39
        m_hInstance   = ::GetModuleHandle(NULL);
#endif
    }

    virtual ~WinApp()
    {
40
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
41 42 43 44 45 46
        ::UnregisterClass(WINCLASS, m_hInstance);
#endif
    }

    int create()
    {
47
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        WNDCLASSEX wcex;

        wcex.cbSize        = sizeof(WNDCLASSEX);
        wcex.style         = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc   = &WinApp::StaticWndProc;
        wcex.cbClsExtra    = 0;
        wcex.cbWndExtra    = 0;
        wcex.hInstance     = m_hInstance;
        wcex.hIcon         = LoadIcon(0, IDI_APPLICATION);
        wcex.hCursor       = LoadCursor(0, IDC_ARROW);
        wcex.hbrBackground = 0;
        wcex.lpszMenuName  = 0L;
        wcex.lpszClassName = WINCLASS;
        wcex.hIconSm       = 0;

        ATOM wc = ::RegisterClassEx(&wcex);

        RECT rc = { 0, 0, m_width, m_height };
        ::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);

        m_hWnd = ::CreateWindow(
                     (LPCTSTR)wc, m_window_name.c_str(),
                     WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                     rc.right - rc.left, rc.bottom - rc.top,
                     NULL, NULL, m_hInstance, (void*)this);

        if (!m_hWnd)
            return -1;

        ::ShowWindow(m_hWnd, SW_SHOW);
        ::UpdateWindow(m_hWnd);
        ::SetFocus(m_hWnd);
#elif defined(__linux__)
        m_display = XOpenDisplay(NULL);

        if (m_display == NULL)
        {
            return -1;
        }

        m_WM_DELETE_WINDOW = XInternAtom(m_display, "WM_DELETE_WINDOW", False);

        static GLint visual_attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
        m_visual_info = glXChooseVisual(m_display, 0, visual_attributes);

        if (m_visual_info == NULL)
        {
            XCloseDisplay(m_display);
            return -2;
        }

        Window root = DefaultRootWindow(m_display);

        m_event_mask = ExposureMask | KeyPressMask;

        XSetWindowAttributes window_attributes;
        window_attributes.colormap = XCreateColormap(m_display, root, m_visual_info->visual, AllocNone);
        window_attributes.event_mask = m_event_mask;

        m_window = XCreateWindow(
            m_display, root, 0, 0, m_width, m_height, 0, m_visual_info->depth,
            InputOutput, m_visual_info->visual, CWColormap | CWEventMask, &window_attributes);

        XMapWindow(m_display, m_window);
        XSetWMProtocols(m_display, m_window, &m_WM_DELETE_WINDOW, 1);
        XStoreName(m_display, m_window, m_window_name.c_str());
#endif

        return init();
    }

119 120
    virtual void cleanup()
    {
121
#if defined(_WIN32)
122 123 124 125 126 127 128
        ::DestroyWindow(m_hWnd);
#elif defined(__linux__)
        XDestroyWindow(m_display, m_window);
        XCloseDisplay(m_display);
#endif
    }

129
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
130 131 132 133 134
    virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
#endif

    int run()
    {
135
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        MSG msg;

        ::ZeroMemory(&msg, sizeof(msg));

        while (msg.message != WM_QUIT)
        {
            if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
            {
                ::TranslateMessage(&msg);
                ::DispatchMessage(&msg);
            }
            else
            {
                idle();
            }
        }

        return static_cast<int>(msg.wParam);
#elif defined(__linux__)
        m_end_loop = false;

        do {
            XEvent e;

            if (!XCheckWindowEvent(m_display, m_window, m_event_mask, &e) || !handle_event(e))
            {
                idle();
            }
        } while (!m_end_loop);

        return 0;
Alexey Ershov's avatar
Alexey Ershov committed
167
#endif
Alexey Ershov's avatar
Alexey Ershov committed
168 169 170 171
    }

protected:

172
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        WinApp* pWnd;

        if (message == WM_NCCREATE)
        {
            LPCREATESTRUCT pCreateStruct = ((LPCREATESTRUCT)lParam);
            pWnd = (WinApp*)(pCreateStruct->lpCreateParams);
            ::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pWnd);
        }

        pWnd = GetObjectFromWindow(hWnd);

        if (pWnd)
            return pWnd->WndProc(hWnd, message, wParam, lParam);
        else
            return ::DefWindowProc(hWnd, message, wParam, lParam);
    }

    inline static WinApp* GetObjectFromWindow(HWND hWnd)
    {
        return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
    }
#endif

#if defined(__linux__)
    virtual int handle_event(XEvent& e) = 0;
#endif

    virtual int init() = 0;
    virtual int render() = 0;

    virtual void idle() = 0;

207
#if defined(_WIN32)
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
208 209
    HINSTANCE     m_hInstance;
    HWND          m_hWnd;
Alexey Ershov's avatar
Alexey Ershov committed
210
#elif defined(__linux__)
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
211 212 213 214 215 216
    Display*      m_display;
    XVisualInfo*  m_visual_info;
    Window        m_window;
    long          m_event_mask;
    Atom          m_WM_DELETE_WINDOW;
    bool          m_end_loop;
Alexey Ershov's avatar
Alexey Ershov committed
217
#endif
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
218 219 220 221
    int           m_width;
    int           m_height;
    std::string   m_window_name;
    cv::TickMeter m_timer;
Alexey Ershov's avatar
Alexey Ershov committed
222
};