1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 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 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 167 168 169 170 171 172 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 207 208 209 210 211 212 213 214 215 216
|
/********************************************************************************
* *
* O p e n G L C a n v a s O b j e c t *
* *
*********************************************************************************
* Copyright (C) 1997,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXAccelTable.h"
#include "FXCursor.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXException.h"
#include "FXVisual.h"
#include "FXGLVisual.h"
#include "FXGLCanvas.h"
#include "FXGLContext.h"
/*
Notes:
- Since this only adds SetPixelFormat, perhaps not a bad idea to contemplate
moving this call to SetPixelFormat somewhere else [candidates are FXGLVisual,
FXWindow, or FXGLContext].
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
// Object implementation
FXIMPLEMENT(FXGLCanvas,FXCanvas,nullptr,0)
// For serialization
FXGLCanvas::FXGLCanvas(){
flags|=FLAG_ENABLED|FLAG_SHOWN;
context=nullptr;
}
// Construct a GL canvas with its private context and private display lists
FXGLCanvas::FXGLCanvas(FXComposite* p,FXGLVisual *vis,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h):FXCanvas(p,tgt,sel,opts|GLCANVAS_OWN_CONTEXT,x,y,w,h){
flags|=FLAG_ENABLED|FLAG_SHOWN;
context=new FXGLContext(getApp(),vis);
visual=vis;
}
// Construct a GL canvas with its private context but shared display lists
FXGLCanvas::FXGLCanvas(FXComposite* p,FXGLVisual *vis,FXGLCanvas* share,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h):FXCanvas(p,tgt,sel,opts|GLCANVAS_OWN_CONTEXT,x,y,w,h){
flags|=FLAG_ENABLED|FLAG_SHOWN;
context=new FXGLContext(getApp(),vis,share ? share->getContext() : nullptr);
visual=vis;
}
// Construct a GL canvas with a shared context
FXGLCanvas::FXGLCanvas(FXComposite* p,FXGLContext* ctx,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h):FXCanvas(p,tgt,sel,opts,x,y,w,h){
flags|=FLAG_ENABLED|FLAG_SHOWN;
context=ctx;
visual=ctx->getVisual();
}
#ifdef WIN32
const void* FXGLCanvas::GetClass() const { return TEXT("FXGLCanvas"); }
#endif
// Change context
void FXGLCanvas::setContext(FXGLContext *ctx,FXbool owned){
if(!ctx){ fxerror("%s::setContext: NULL context\n",getClassName()); }
if(xid){ fxerror("%s::setContext: context should be set before calling create()\n",getClassName()); }
if(context!=ctx){
if(options&GLCANVAS_OWN_CONTEXT) delete context;
context=ctx;
visual=ctx->getVisual();
}
options^=((0-owned)^options)&GLCANVAS_OWN_CONTEXT;
}
// Return true if it is sharing display lists
FXbool FXGLCanvas::isShared() const {
return (context->getShared()!=nullptr);
}
// Create X window (GL CANVAS)
void FXGLCanvas::create(){
FXCanvas::create();
if(xid){
if(getApp()->isInitialized()){
// Create context
context->create();
// Change frame buffer according to visual
#ifdef HAVE_GL_H
#if defined(WIN32)
PIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR),1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
HDC hdc=::GetDC((HWND)xid); // FIXME should this be this->GetDC()
SetPixelFormat(hdc,(FXint)(FXival)visual->id(),&pfd);
::ReleaseDC((HWND)xid,hdc); // FIXME should this be this->ReleaseDC()
//#elif defined(GLX_VERSION_1_3)
// xid=glXCreateWindow((Display*)getApp()->getDisplay(),(GLXFBConfig)visual->id(),xid,nullptr);
#endif
#endif
}
}
}
// Detach the GL Canvas
void FXGLCanvas::detach(){
context->detach();
FXCanvas::detach();
}
// Destroy the GL Canvas
void FXGLCanvas::destroy(){
if(xid){
if(getApp()->isInitialized()){
if(options&GLCANVAS_OWN_CONTEXT) context->destroy();
#ifdef HAVE_GL_H
//#if defined(GLX_VERSION_1_3)
// glXDestroyWindow((Display*)getApp()->getDisplay(),xid);
// xid=0;
//#endif
#endif
}
}
FXCanvas::destroy();
}
// Make the rendering context of GL Canvas current
FXbool FXGLCanvas::makeCurrent(){
return context->begin(this);
}
// Make the rendering context of GL Canvas current
FXbool FXGLCanvas::makeNonCurrent(){
return context->end();
}
// Return true if this window's context is current
FXbool FXGLCanvas::isCurrent() const {
return context->isCurrent();
}
// Used by GL to swap the buffers in double buffer mode, or flush a single buffer
void FXGLCanvas::swapBuffers(){
context->swapBuffers();
}
// Save object to stream
void FXGLCanvas::save(FXStream& store) const {
FXCanvas::save(store);
store << context;
}
// Load object from stream
void FXGLCanvas::load(FXStream& store){
FXCanvas::load(store);
store >> context;
}
// Close and release any resources
FXGLCanvas::~FXGLCanvas(){
destroy();
if(options&GLCANVAS_OWN_CONTEXT) delete context;
context=(FXGLContext*)-1L;
}
}
|