uppdev update

git-svn-id: svn://ultimatepp.org/upp/trunk@544 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2008-10-19 15:37:51 +00:00
parent 4d38b96aa9
commit 85eade94fa
12 changed files with 804 additions and 2 deletions

23
uppdev/GLCtrl/GLCtrl.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "GLCtrl.h"
NAMESPACE_UPP
void GLCtrl::StdView()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
Size sz = GetSize();
glViewport(0, 0, (GLsizei)sz.cx, (GLsizei)sz.cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)(sz.cx)/(GLfloat)(sz.cy), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
END_UPP_NAMESPACE

158
uppdev/GLCtrl/GLCtrl.h Normal file
View file

@ -0,0 +1,158 @@
#ifndef _GLCtrl_GLCtrl_h
#define _GLCtrl_GLCtrl_h
#include <CtrlLib/CtrlLib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "GlPicking.h"
NAMESPACE_UPP
#ifdef PLATFORM_X11
#include <GL/glx.h>
class GLCtrl : public DHCtrl
{
private:
GlPicking _picking;
// OpenGL Context
GLXContext WindowContext;
// Number of instances
static int Instances;
// Current instance number
int InstanceNum;
// OpenGL parameters
int DepthSize;
int StencilSize;
int NumberOfSamples;
bool DoubleBuffering;
bool MultiSampleBuffering;
// Currently activated context number
static int ContextActivated;
// Activates current OpenGL context
void ActivateContext();
// Ovverridden method to choose the correct visual
virtual XVisualInfo *CreateVisual(void);
// Overridden method for attribute setting
virtual void SetAttributes(unsigned long &ValueMask, XSetWindowAttributes &attr);
// Overridden method to create and destroy OpenGL context
virtual void AfterInit(bool Error);
virtual void BeforeTerminate(void);
// Overridden method to resize GL windows
virtual void Resize(int w, int h);
// Internal OpenGL Paint method
void doPaint(void);
// Paint method - with graphic context
// Called from DHCtrl - Graphic context is *not* used
virtual void Paint(Draw &/*draw*/);
protected:
// Overridable methods for derived controls
// Called after succesful OpenGL initialization
virtual void GLInit() {}
// Called just before OpenGL termination
virtual void GLDone() {}
// Called on resize events
virtual void GLResize( int w, int h ) {}
// Called on paint events
virtual void GLPaint() {}
virtual void GLPickingPaint() {}
public:
typedef GLCtrl CLASSNAME;
// Constructor class GLCtrl
GLCtrl( int depthsize = 24,
int stencilsize = 0,
bool doublebuffer = true,
bool multisamplebuffering = false,
int numberofsamples = 0 );
// Destructor class GLCtrl
~GLCtrl();
// Initializes OpenGL context to a standard view
void StdView();
void InitPickMatrix() { _picking.InitPickMatrix(); }
Vector<int> Pick(int x, int y);
}; // END Class GLCtrl
#else
class GLCtrl : public ParentCtrl {
typedef GLCtrl CLASSNAME;
struct GLPane : DHCtrl {
GLCtrl *ctrl;
virtual void State(int reason);
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
};
friend class GLCtrl;
private:
HDC hDC;
HGLRC hRC;
GlPicking _picking;
GLPane glpane;
void OpenGL();
void CloseGL();
protected:
// Overridable methods for derived controls
// Called after succesful OpenGL initialization
virtual void GLInit() {}
// Called just before OpenGL termination
virtual void GLDone() {}
// Called on resize events
virtual void GLResize(int w, int h) {}
// Called on paint events
virtual void GLPaint();
virtual void GLPickingPaint() {}
public:
Callback WhenGLPaint;
GLCtrl();
~GLCtrl();
void StdView();
HDC GetDC() const { return hDC; }
HGLRC GetHGLRC() const { return hRC; }
void InitPickMatrix() { _picking.InitPickMatrix(); }
Vector<int> Pick(int x, int y);
};
#endif
END_UPP_NAMESPACE
#endif

16
uppdev/GLCtrl/GLCtrl.upp Normal file
View file

@ -0,0 +1,16 @@
uses
CtrlLib;
library(WIN32 GCC) "glaux glu32 opengl32";
library(LINUX) "GL GLU";
library(FREEBSD) "GL GLU";
file
GLCtrl.h,
Win32GLCtrl.cpp,
X11GLCtrl.cpp,
GLCtrl.cpp,
GlPicking.h;

78
uppdev/GLCtrl/GlPicking.h Normal file
View file

@ -0,0 +1,78 @@
#ifndef _GLCtrl_GlPicking_h_
#define _GLCtrl_GlPicking_h_
NAMESPACE_UPP
class GlPicking
{
private:
static int const _bufferSize = 512;
bool _isPicking;
Point _pickPoint;
public:
GlPicking()
: _isPicking(false)
{}
void InitPickMatrix()
{
if (_isPicking)
{
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix(_pickPoint.x, viewport[3] - _pickPoint.y, 3, 3, viewport);
}
}
Vector<int> Pick(int x, int y, Callback resizeCallback, Callback paintCallback)
{
GLuint buffer[_bufferSize];
_pickPoint = Point(x, y);
glSelectBuffer(_bufferSize, buffer);
glRenderMode(GL_SELECT);
_isPicking = true;
resizeCallback();
glInitNames();
paintCallback();
_isPicking = false;
resizeCallback();
// returning to normal rendering mode
int hits = glRenderMode(GL_RENDER);
if (hits == 0)
return Vector<int>();
else
return ParseHits(buffer, hits);
}
private:
Vector<int> ParseHits(GLuint *buffer, int hits)
{
GLuint *minPtr = buffer;
for (int i = 0; i < hits; i++)
{
if (*(buffer + 1) < *(minPtr + 1))
minPtr = buffer;
buffer += *buffer + 3;
}
Vector<int> result;
for (GLuint i = 0; i < *minPtr; i++)
result.Add(*(minPtr + 3 + i));
return result;
}
};
END_UPP_NAMESPACE
#endif

View file

@ -0,0 +1,131 @@
#include "GLCtrl.h"
NAMESPACE_UPP
#ifdef PLATFORM_WIN32
#pragma comment( lib, "opengl32.lib" ) // Search For OpenGL32.lib While Linking
#pragma comment( lib, "glu32.lib" ) // Search For GLu32.lib While Linking
GLCtrl::GLCtrl()
{
hDC = NULL;
hRC = NULL;
glpane.ctrl = this;
Add(glpane.SizePos());
}
GLCtrl::~GLCtrl()
{
CloseGL();
}
void GLCtrl::OpenGL()
{
HWND hwnd = glpane.GetHWND();
if(!hwnd)
return;
hDC = ::GetDC(hwnd);
if(!hDC)
return;
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | 0x00008000;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
int pf = ChoosePixelFormat(hDC, &pfd);
if(!pf) {
CloseGL();
return;
}
if(!SetPixelFormat(hDC, pf, &pfd)) {
CloseGL();
return;
}
DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
hRC = wglCreateContext(hDC);
if (!hRC)
return;
wglMakeCurrent(hDC, hRC);
GLInit();
GLResize(GetSize().cx, GetSize().cy);
}
void GLCtrl::CloseGL()
{
if (hDC != NULL && hRC != NULL)
{
wglMakeCurrent(hDC, hRC);
GLDone();
wglMakeCurrent(NULL, NULL);
}
if(hRC)
wglDeleteContext(hRC);
if(hDC)
ReleaseDC(glpane.GetHWND(), hDC);
}
void GLCtrl::GLPaint()
{
WhenGLPaint();
}
void GLCtrl::GLPane::State(int reason)
{
if (reason == CLOSE)
ctrl->CloseGL();
if (reason == LAYOUTPOS && ctrl->hDC != NULL && ctrl->hRC != NULL)
{
wglMakeCurrent(ctrl->hDC, ctrl->hRC);
ctrl->GLResize(GetSize().cx, GetSize().cy);
wglMakeCurrent(NULL, NULL);
}
DHCtrl::State(reason);
if (reason == OPEN)
ctrl->OpenGL();
}
LRESULT GLCtrl::GLPane::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_PAINT && ctrl->hDC && ctrl->hRC)
{
PAINTSTRUCT ps;
BeginPaint(GetHWND(), &ps);
wglMakeCurrent(ctrl->hDC, ctrl->hRC);
ctrl->GLPaint();
glFlush();
glFinish();
SwapBuffers(ctrl->hDC);
wglMakeCurrent(NULL, NULL);
EndPaint(GetHWND(), &ps);
return 0;
}
else if(message == WM_ERASEBKGND)
return 1;
return DHCtrl::WindowProc(message, wParam, lParam);
}
Vector<int> GLCtrl::Pick(int x, int y)
{
wglMakeCurrent(hDC, hRC);
Vector<int> result = _picking.Pick(x, y, THISBACK2(GLResize, GetSize().cx, GetSize().cy), THISBACK(GLPickingPaint));
wglMakeCurrent(NULL, NULL);
return result;
}
#endif
END_UPP_NAMESPACE

193
uppdev/GLCtrl/X11GLCtrl.cpp Normal file
View file

@ -0,0 +1,193 @@
#include "GLCtrl.h"
NAMESPACE_UPP
#ifdef PLATFORM_X11
/////////////////////////////////////////////////////////////////////////////////////////
// Static members initialization
int GLCtrl::Instances = 0;
int GLCtrl::ContextActivated = 0;
/////////////////////////////////////////////////////////////////////////////////////////
// Constructor
GLCtrl::GLCtrl( int depthsize, int stencilsize, bool doublebuffer,
bool multisamplebuffering, int numberofsamples )
{
// Sets the current instance number and updates total instances
InstanceNum = ++Instances;
WindowContext = NULL;
DepthSize = depthsize;
StencilSize = stencilsize;
DoubleBuffering = doublebuffer;
NumberOfSamples = numberofsamples;
} // END Constructor class GLCtrl
/////////////////////////////////////////////////////////////////////////////////////////
// Destructor
GLCtrl::~GLCtrl()
{
// If glx context is still there, destroy it
// That can happen on unclean exit
if(WindowContext)
{
glXDestroyContext( (Display *)Xdisplay, WindowContext );
WindowContext = NULL;
}
} // END Destructor class GLCtrl
/////////////////////////////////////////////////////////////////////////////////////////
// Ovverridden method to choose the correct visual
XVisualInfo *GLCtrl::CreateVisual(void)
{
Vector<int> visual;
visual << GLX_RGBA << GLX_DEPTH_SIZE << DepthSize;
if( StencilSize > 0 )
visual << GLX_STENCIL_SIZE << StencilSize;
if( DoubleBuffering )
visual << GLX_DOUBLEBUFFER;
if( MultiSampleBuffering && NumberOfSamples > 1 )
{
visual << GLX_SAMPLE_BUFFERS_ARB << 1 << GLX_SAMPLES_ARB << NumberOfSamples;
}
visual << None;
// Try to find a visual
XVisualInfo *visualInfo = NULL;
visualInfo = glXChooseVisual( (Display*)Xdisplay, DefaultScreen(Xdisplay), visual );
if( visualInfo == NULL )
{
SetError(true);
SetErrorMessage("GLCtrl : Impossible to find a suitable visual.");
}
return visualInfo;
} // END GLCtrl::CreateVisual()
/////////////////////////////////////////////////////////////////////////////////////////
// Overridden method for attribute setting
void GLCtrl::SetAttributes(unsigned long &ValueMask, XSetWindowAttributes &winAttributes)
{
ValueMask |=
CWBorderPixel
| CWEventMask
| CWSaveUnder
;
winAttributes.border_pixel = 0;
winAttributes.event_mask = ExposureMask;
winAttributes.save_under = XFalse;
} // END GLCtrl::SetAttributes()
/////////////////////////////////////////////////////////////////////////////////////////
// Activates current OpenGL context
void GLCtrl::ActivateContext()
{
if( Instances > 0 && ContextActivated != InstanceNum )
{
glXMakeCurrent( (Display*)Xdisplay, GetWindow(), WindowContext );
ContextActivated = InstanceNum;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Overridden method to create OpenGL context
void GLCtrl::AfterInit(bool Error)
{
// Gets the activw XVisualInfo
XVisualInfo visualInfo = GetVisualInfo();
// Create an OpenGL rendering context
WindowContext = glXCreateContext
(
(Display *)Xdisplay,
&visualInfo,
NULL, // No sharing of display lists
GL_TRUE // Direct rendering if possible
);
if( WindowContext == NULL )
{
SetErrorMessage("GLCtrl : Error creating GLX context.");
SetError(true);
}
// Activate the created glxwindow
glXMakeCurrent( (Display*)Xdisplay, GetWindow(), WindowContext );
// Call user init function
GLInit();
// Calls resize and paint functions
GLResize( GetSize().cx, GetSize().cy );
GLPaint();
} // END GLCtrl::AfterInit()
/////////////////////////////////////////////////////////////////////////////////////////
// Overridden method to destroy OpenGL context
void GLCtrl::BeforeTerminate(void)
{
// Calls user terminate function
GLDone();
// Resets context and destroys it
glXMakeCurrent( (Display*)Xdisplay, None, NULL );
glXDestroyContext( (Display *)Xdisplay, WindowContext );
WindowContext = NULL;
} // END GLCtrl::BeforeTerminate()
/////////////////////////////////////////////////////////////////////////////////////////
// Overridden method to resize GL windows
void GLCtrl::Resize(int x, int y)
{
// Activates the current context
ActivateContext();
// Calls user resize hook
GLResize(x, y);
} // END GLCtrl::Resize()
/////////////////////////////////////////////////////////////////////////////////////////
// Internal OpenGL Paint method
void GLCtrl::doPaint(void)
{
// Activates the current context
ActivateContext();
// Calls user paint hook
GLPaint();
// Swap buffers or flush as needed
if( DoubleBuffering )
glXSwapBuffers( (Display*)Xdisplay, GetWindow() ); // Buffer swap does implicit glFlush
else
glFlush();
} // END GLCtrl::doPaint()
/////////////////////////////////////////////////////////////////////////////////////////
// Paint method - with graphic context
void GLCtrl::Paint(Draw &draw)
{
// Calls internal OpenGL Paint method
doPaint();
} // END GLCtrl::Paint()
Vector<int> GLCtrl::Pick(int x, int y)
{
ActivateContext();
Vector<int> result = _picking.Pick(x, y, THISBACK2(GLResize, GetSize().cx, GetSize().cy), THISBACK(GLPickingPaint));
return result;
}
#endif
END_UPP_NAMESPACE

4
uppdev/GLCtrl/init Normal file
View file

@ -0,0 +1,4 @@
#ifndef _GLCtrl_icpp_init_stub
#define _GLCtrl_icpp_init_stub
#include "CtrlLib/init"
#endif

View file

@ -0,0 +1,10 @@
LAYOUT(OpenGlTestLayout, 580, 332)
ITEM(LineEdit, _log, SetFont(ScreenFixedZ(10)).SetEditable(false).LeftPosZ(12, 200).VSizePosZ(24, 64))
ITEM(Label, dv___1, SetLabel(t_("Log:")).LeftPosZ(12, 33).TopPosZ(4, 21))
ITEM(MyGlCtrl, _glCtrl, HSizePosZ(220, 8).VSizePosZ(8, 8))
ITEM(Button, _removeButton, SetLabel(t_("Remove GLCtrl")).LeftPosZ(116, 96).BottomPosZ(36, 20))
ITEM(Button, _showButton, SetLabel(t_("Show GLCtrl")).LeftPosZ(12, 96).BottomPosZ(8, 20))
ITEM(Button, _addButton, SetLabel(t_("Add GLCtrl")).LeftPosZ(116, 96).BottomPosZ(8, 20))
ITEM(Button, _hideButton, SetLabel(t_("Hide GLCtrl")).LeftPosZ(12, 96).BottomPosZ(36, 20))
END_LAYOUT

View file

@ -0,0 +1,11 @@
uses
CtrlLib,
GLCtrl;
file
main.cpp,
OpenGlTest.lay;
mainconfig
"" = "GUI";

5
uppdev/OpenGlTest/init Normal file
View file

@ -0,0 +1,5 @@
#ifndef _OpenGlTest_icpp_init_stub
#define _OpenGlTest_icpp_init_stub
#include "CtrlLib/init"
#include "GLCtrl/init"
#endif

162
uppdev/OpenGlTest/main.cpp Normal file
View file

@ -0,0 +1,162 @@
#include <CtrlLib/CtrlLib.h>
#include <GLCtrl/GLCtrl.h>
using namespace Upp;
class MyGlCtrl : public GLCtrl
{
typedef MyGlCtrl CLASSNAME;
private:
LineEdit *_log;
ToolBar _sliderBar;
SliderCtrl _slider;
public:
MyGlCtrl()
: _log(NULL)
{
_slider.Range(9);
_slider.SetData(5);
_slider <<= THISBACK(SliderCtrlAction);
_sliderBar.Set(THISBACK(InitSliderBar));
_sliderBar.Bottom();
AddFrame(_sliderBar);
}
void SetLog(LineEdit &log)
{
_log = &log;
}
void Log(String const &s) { if(_log == NULL) return; _log->Insert(_log->GetLength(), String().Cat() << "\n" << s); _log->ScrollDown(); }
protected:
virtual void GLInit()
{
ASSERT(HasRenderingContext());
Log("-> GLInit()");
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
}
virtual void GLDone()
{
ASSERT(HasRenderingContext());
Log("-> GLDone()");
}
virtual void GLResize(int w, int h)
{
ASSERT(HasRenderingContext());
Log(String().Cat() << "-> GLResize(" << w << ", " << h << ")");
}
virtual void GLPaint()
{
ASSERT(HasRenderingContext());
Log("-> GLPaint()");
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < (int)_slider.GetData(); i++)
{
glRectf(-9.0 + i * 2, 5.0, -8.0 + i * 2, -5.0);
}
}
private:
void InitSliderBar(Bar &bar)
{
bar.Add(_slider, INT_MAX);
}
void SliderCtrlAction()
{
Log(String().Cat() << "-> SliderCtrlAction(" << _slider.GetData() << ")");
Refresh();
}
bool HasRenderingContext()
{
#ifdef PLATFORM_WIN32
return wglGetCurrentContext() != NULL;
#else
return glXGetCurrentContext() != NULL;
#endif
}
};
#define LAYOUTFILE <OpenGlTest/OpenGlTest.lay>
#include <CtrlCore/lay.h>
class OpenGlTest : public WithOpenGlTestLayout<TopWindow>
{
typedef OpenGlTest CLASSNAME;
public:
OpenGlTest()
{
_log.Insert(_log.GetLength(), "\nStart of OpenGlTest constructor");
_hideButton <<= THISBACK(Hide);
_showButton <<= THISBACK(Show);
_addButton <<= THISBACK(Add);
_removeButton <<= THISBACK(Remove);
_showButton.Enable(false);
_addButton.Enable(false);
_glCtrl.SetLog(_log);
CtrlLayout(*this, "OpenGL test");
_log.Insert(_log.GetLength(), "\nEnd of OpenGlTest constructor");
}
private:
void Hide()
{
_glCtrl.Hide();
_glCtrl.Log("-> Hide()");
_hideButton.Enable(false);
_showButton.Enable(true);
}
void Show()
{
_glCtrl.Show();
_glCtrl.Log("-> Show()");
_hideButton.Enable(true);
_showButton.Enable(false);
}
void Remove()
{
RemoveChild(&_glCtrl);
_glCtrl.Log("-> Remove()");
_removeButton.Enable(false);
_addButton.Enable(true);
}
void Add()
{
AddChild(&_glCtrl);
_glCtrl.Log("-> Add()");
_removeButton.Enable(true);
_addButton.Enable(false);
}
};
GUI_APP_MAIN
{
OpenGlTest().Sizeable().Run();
}

View file

@ -113,6 +113,7 @@ struct FindBrokenRefIterator : RichTxt::Iterator {
Uuid itemstyle;
void Test();
void Dnes();
virtual bool operator()(int pos, const RichPara& para)
{
@ -166,9 +167,16 @@ void Foo()
EditString es;
es. ;
es.NullText(). ;
THISBACK(
String x[5];
x[1]. ;
Rect r;
r.
}
struct FwTest;
struct Dlg : WithEditStringLayout<TopWindow> {
Dlg() {
Ctrl:: ;
@ -176,8 +184,11 @@ struct Dlg : WithEditStringLayout<TopWindow> {
x. ;
this. ;
this-> ;
a
text. ;
}
Vector<String>::
};
struct FwTest {
void Test();
};