winapp.hpp 6.21 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
# define WINCLASS "WinAppWnd"
#endif

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

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class Timer
{
public:
    enum UNITS
    {
        USEC = 0,
        MSEC,
        SEC
    };

    Timer() : m_t0(0), m_diff(0)
    {
        m_tick_frequency = (float)cv::getTickFrequency();

        m_unit_mul[USEC] = 1000000;
        m_unit_mul[MSEC] = 1000;
        m_unit_mul[SEC]  = 1;
    }

    void clear()
    {
        m_t0 = m_diff = 0;
    }

    void start()
    {
        m_t0 = cv::getTickCount();
    }

    void stop()
    {
        m_diff = cv::getTickCount() - m_t0;
    }

59
    float time(UNITS u = MSEC)
60 61 62 63 64 65 66 67 68 69 70 71 72
    {
        float sec = m_diff / m_tick_frequency;

        return sec * m_unit_mul[u];
    }

public:
    float m_tick_frequency;
    int64 m_t0;
    int64 m_diff;
    int   m_unit_mul[3];
};

Alexey Ershov's avatar
Alexey Ershov committed
73 74 75 76 77 78 79 80
class WinApp
{
public:
    WinApp(int width, int height, std::string& window_name)
    {
        m_width       = width;
        m_height      = height;
        m_window_name = window_name;
81
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
82 83 84 85 86 87
        m_hInstance   = ::GetModuleHandle(NULL);
#endif
    }

    virtual ~WinApp()
    {
88
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
89 90 91 92 93 94
        ::UnregisterClass(WINCLASS, m_hInstance);
#endif
    }

    int create()
    {
95
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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
        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();
    }

167 168
    virtual void cleanup()
    {
169
#if defined(_WIN32)
170 171 172 173 174 175 176
        ::DestroyWindow(m_hWnd);
#elif defined(__linux__)
        XDestroyWindow(m_display, m_window);
        XCloseDisplay(m_display);
#endif
    }

177
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
178 179 180 181 182
    virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
#endif

    int run()
    {
183
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        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
215
#endif
Alexey Ershov's avatar
Alexey Ershov committed
216 217 218 219
    }

protected:

220
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    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;

255
#if defined(_WIN32)
Alexey Ershov's avatar
Alexey Ershov committed
256 257 258 259 260 261 262 263 264 265 266 267 268
    HINSTANCE    m_hInstance;
    HWND         m_hWnd;
#elif defined(__linux__)
    Display*     m_display;
    XVisualInfo* m_visual_info;
    Window       m_window;
    long         m_event_mask;
    Atom         m_WM_DELETE_WINDOW;
    bool         m_end_loop;
#endif
    int          m_width;
    int          m_height;
    std::string  m_window_name;
269
    Timer        m_timer;
Alexey Ershov's avatar
Alexey Ershov committed
270
};