[1097] | 1 | #ifndef MXGLPANE_INCLUDED // -*- C++ -*-
|
---|
| 2 | #define MXGLPANE_INCLUDED
|
---|
| 3 | #if !defined(__GNUC__)
|
---|
| 4 | # pragma once
|
---|
| 5 | #endif
|
---|
| 6 |
|
---|
| 7 | /************************************************************************
|
---|
| 8 |
|
---|
| 9 | MxGLPane
|
---|
| 10 |
|
---|
| 11 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
| 12 |
|
---|
| 13 | $Id: MxGLPane.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
| 14 |
|
---|
| 15 | ************************************************************************/
|
---|
| 16 |
|
---|
| 17 | #ifdef WIN32
|
---|
| 18 | //
|
---|
| 19 | // Include MFC stuff now so that <windows.h> doesn't get included for GL
|
---|
| 20 | //
|
---|
| 21 | # define VC_EXTRALEAN
|
---|
| 22 | # include <afxwin.h> // MFC core and standard components
|
---|
| 23 | # include <afxext.h> // MFC extensions
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | #include "MxGL.h"
|
---|
| 27 | #include "MxRaster.h"
|
---|
| 28 |
|
---|
| 29 | #define MX_BUTTONDOWN 1
|
---|
| 30 | #define MX_BUTTONUP 2
|
---|
| 31 | #define MX_POINTERMOTION 3
|
---|
| 32 | // Keep lowest digit unused to avoid clashes with the event symbols
|
---|
| 33 | #define MX_CTRLKEY 0x10
|
---|
| 34 | #define MX_SHIFTKEY 0x20
|
---|
| 35 |
|
---|
| 36 | // Specific ASCII keycodes
|
---|
| 37 | //
|
---|
| 38 | // BUG: This is not an appropriate home for this key stuff.
|
---|
| 39 | // At some point, we should consolidate this and related items
|
---|
| 40 | // into a unified event module.
|
---|
| 41 | //
|
---|
| 42 | #define MXKEY_BS 0x08
|
---|
| 43 | #define MXKEY_DEL 0x7f
|
---|
| 44 | #define MXKEY_NL 0x0a
|
---|
| 45 | #define MXKEY_CR 0x0d
|
---|
| 46 |
|
---|
| 47 | #if defined(WIN32)
|
---|
| 48 |
|
---|
| 49 | // Handy alternative to RGB()
|
---|
| 50 | inline COLORREF fRGB(float r, float g, float b)
|
---|
| 51 | {
|
---|
| 52 | return RGB((uint)(r*255.0f), (uint)(g*255.0f), (uint)(b*255.0f));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | class MxGLPane : public CFrameWnd
|
---|
| 56 | {
|
---|
| 57 | private:
|
---|
| 58 | HGLRC gl_context;
|
---|
| 59 | int pixel_format;
|
---|
| 60 | CDC *active_dc;
|
---|
| 61 |
|
---|
| 62 | bool set_pixel_format(HDC);
|
---|
| 63 | bool create_glcontext(HDC);
|
---|
| 64 |
|
---|
| 65 | void handle_mouse(int kind, UINT flags, CPoint point);
|
---|
| 66 | void do_redraw(CDC& dc);
|
---|
| 67 |
|
---|
| 68 | protected: // MFC virtual function overrides
|
---|
| 69 | BOOL PreCreateWindow(CREATESTRUCT& cs);
|
---|
| 70 |
|
---|
| 71 | protected: // MFC event handlers
|
---|
| 72 | afx_msg void OnPaint();
|
---|
| 73 | afx_msg BOOL OnEraseBkgnd(CDC *);
|
---|
| 74 | afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
|
---|
| 75 | afx_msg void OnDestroy();
|
---|
| 76 | afx_msg void OnSize(UINT nType, int cx, int cy);
|
---|
| 77 | afx_msg void OnLButtonDown(UINT flags, CPoint point);
|
---|
| 78 | afx_msg void OnLButtonUp(UINT flags, CPoint point);
|
---|
| 79 | afx_msg void OnRButtonDown(UINT flags, CPoint point);
|
---|
| 80 | afx_msg void OnRButtonUp(UINT flags, CPoint point);
|
---|
| 81 | afx_msg void OnMButtonDown(UINT flags, CPoint point);
|
---|
| 82 | afx_msg void OnMButtonUp(UINT flags, CPoint point);
|
---|
| 83 | afx_msg void OnMouseMove(UINT flags, CPoint point);
|
---|
| 84 | afx_msg void OnChar(UINT ch, UINT repcount, UINT flags);
|
---|
| 85 | afx_msg void OnTimer(UINT timer_id);
|
---|
| 86 | DECLARE_MESSAGE_MAP ()
|
---|
| 87 |
|
---|
| 88 | public:
|
---|
| 89 | float background_color[3];
|
---|
| 90 |
|
---|
| 91 | public:
|
---|
| 92 | MxGLPane();
|
---|
| 93 |
|
---|
| 94 | void create(char *name);
|
---|
| 95 | inline void make_current(HDC dc) { wglMakeCurrent(dc, gl_context); }
|
---|
| 96 | inline void finish(HDC dc) { SwapBuffers(dc); }
|
---|
| 97 | void get_canvas_geometry(int *x, int *y, int *w, int *h);
|
---|
| 98 | void draw_string(int x, int y, const char *str);
|
---|
| 99 |
|
---|
| 100 | MxRaster *grab_snapshot();
|
---|
| 101 | void snapshot_ppm(const char *name=NULL);
|
---|
| 102 | void snapshot_tiff(const char *name=NULL);
|
---|
| 103 |
|
---|
| 104 | virtual void gl_init();
|
---|
| 105 | virtual void begin_redraw();
|
---|
| 106 | virtual void end_redraw();
|
---|
| 107 |
|
---|
| 108 | void post_redraw();
|
---|
| 109 | void require_redraw();
|
---|
| 110 | virtual void redraw();
|
---|
| 111 | virtual void illustrate();
|
---|
| 112 | virtual void key(char);
|
---|
| 113 | virtual void button(int kind, int which, int x, int y);
|
---|
| 114 | virtual void size(int width, int height);
|
---|
| 115 |
|
---|
| 116 | unsigned int add_timer(unsigned int id, unsigned int millisecs);
|
---|
| 117 | bool remove_timer(unsigned int id);
|
---|
| 118 | virtual void timer(unsigned int);
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | class MxGLApp : public CWinApp
|
---|
| 122 | {
|
---|
| 123 |
|
---|
| 124 | public:
|
---|
| 125 | BOOL InitInstance(); // MFC interface
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | static MxGLApp *application;
|
---|
| 129 | static bool (*init_hook)(MxGLApp *);
|
---|
| 130 |
|
---|
| 131 | int argc;
|
---|
| 132 | char **argv;
|
---|
| 133 |
|
---|
| 134 | MxGLApp(bool (*h)(MxGLApp *)=NULL)
|
---|
| 135 | {
|
---|
| 136 | application = this;
|
---|
| 137 | init_hook = h;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void bind_pane(MxGLPane *, char *name);
|
---|
| 141 | const char *query_filename(const char *prompt, const char *cwd,
|
---|
| 142 | const char *filter, const char *default_name);
|
---|
| 143 |
|
---|
| 144 | virtual bool init_application() { return true; }
|
---|
| 145 | };
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | #else
|
---|
| 149 | #ifdef MIX_USE_X11_FORMS
|
---|
| 150 | #include <X11/forms.h>
|
---|
| 151 | #else
|
---|
| 152 | #include <forms.h>
|
---|
| 153 | #endif
|
---|
| 154 |
|
---|
| 155 | class MxGLPane
|
---|
| 156 | {
|
---|
| 157 | private:
|
---|
| 158 | FL_FORM *glpane;
|
---|
| 159 | FL_OBJECT *canvas;
|
---|
| 160 | GC gc;
|
---|
| 161 |
|
---|
| 162 | static MxGLPane *glpane_for_form(FL_FORM *);
|
---|
| 163 | static void glpane_for_form(FL_FORM *, MxGLPane *);
|
---|
| 164 | static MxGLPane *glpane_for_object(FL_OBJECT *);
|
---|
| 165 |
|
---|
| 166 | static int cb_mapnotify(FL_OBJECT *, Window, int, int, XEvent *, void *);
|
---|
| 167 | static int cb_configure(FL_OBJECT *, Window, int, int, XEvent *, void *);
|
---|
| 168 | static int cb_expose(FL_OBJECT *, Window, int, int, XEvent *, void *);
|
---|
| 169 | static int cb_button(FL_OBJECT *, Window, int, int, XEvent *, void *);
|
---|
| 170 | static int cb_key(FL_OBJECT *, Window, int, int, XEvent *, void *);
|
---|
| 171 |
|
---|
| 172 | public:
|
---|
| 173 | float background_color[3];
|
---|
| 174 |
|
---|
| 175 | public:
|
---|
| 176 | MxGLPane();
|
---|
| 177 | virtual ~MxGLPane();
|
---|
| 178 |
|
---|
| 179 | void create(char *name,int *x=NULL,int *y=NULL,int *w=NULL,int *h=NULL);
|
---|
| 180 | inline void make_current()
|
---|
| 181 | {
|
---|
| 182 | glXMakeCurrent(fl_display,
|
---|
| 183 | fl_get_canvas_id(canvas),
|
---|
| 184 | fl_get_glcanvas_context(canvas));
|
---|
| 185 | }
|
---|
| 186 | inline void finish() {glXSwapBuffers(fl_display,fl_get_canvas_id(canvas));}
|
---|
| 187 | void get_canvas_geometry(int *x, int *y, int *w, int *h);
|
---|
| 188 | void draw_string(int x, int y, char *str)
|
---|
| 189 | {
|
---|
| 190 | XDrawString(fl_display, fl_get_canvas_id(canvas), gc,
|
---|
| 191 | x, y+canvas->bw, str, strlen(str));
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | MxRaster *grab_snapshot();
|
---|
| 195 | void snapshot_ppm(const char *name=NULL);
|
---|
| 196 | void snapshot_tiff(const char *name=NULL);
|
---|
| 197 |
|
---|
| 198 | virtual void gl_init();
|
---|
| 199 | virtual void begin_redraw();
|
---|
| 200 | virtual void end_redraw();
|
---|
| 201 |
|
---|
| 202 | void post_redraw();
|
---|
| 203 | void require_redraw();
|
---|
| 204 | virtual void redraw();
|
---|
| 205 | virtual void illustrate();
|
---|
| 206 | virtual void key(char);
|
---|
| 207 | virtual void button(int kind, int which, int x, int y);
|
---|
| 208 | virtual void size(int width, int height);
|
---|
| 209 | virtual void timer(unsigned int);
|
---|
| 210 | };
|
---|
| 211 |
|
---|
| 212 | class MxGLApp
|
---|
| 213 | {
|
---|
| 214 | public:
|
---|
| 215 | static MxGLApp *application;
|
---|
| 216 | static bool (*init_hook)(MxGLApp *);
|
---|
| 217 |
|
---|
| 218 | int argc;
|
---|
| 219 | char **argv;
|
---|
| 220 |
|
---|
| 221 | MxGLApp(bool (*h)(MxGLApp *)=NULL)
|
---|
| 222 | {
|
---|
| 223 | application = this;
|
---|
| 224 | init_hook = h;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | void bind_pane(MxGLPane *, char *name);
|
---|
| 228 | const char *query_filename(const char *prompt, const char *cwd,
|
---|
| 229 | const char *filter, const char *default_name);
|
---|
| 230 |
|
---|
| 231 | virtual bool init_application() { return true; }
|
---|
| 232 | };
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 | #endif
|
---|
| 236 |
|
---|
| 237 | // MXGLPANE_INCLUDED
|
---|
| 238 | #endif
|
---|