Project

General

Profile

Bug #3608 » GL_WidgetSet_Test.cpp

Prasad Dixit, 09/27/2014 07:47 PM

 
1
#include <Wt/WApplication>
2
#include <Wt/WContainerWidget>
3
#include <Wt/WGLWidget>
4
#include <Wt/WEnvironment>
5
#include <Wt/WServer>
6
#include <Wt/WText>
7
#include <Wt/WImage>
8

    
9
using namespace std;
10
using namespace Wt;
11

    
12

    
13
/**********************GraphicsPanel class**********************************/
14
class GraphicsPanel : public Wt::WGLWidget
15
{
16
public:
17
	// Constructor(s)
18
	GraphicsPanel(Wt::WContainerWidget *root = nullptr);
19

    
20
	// Destructor(s)
21
	~GraphicsPanel();
22

    
23
protected:
24
	void initializeGL();
25
	void paintGL();
26
	void resizeGL(int width, int height);
27
private:
28

    
29
};
30

    
31
GraphicsPanel::GraphicsPanel(WContainerWidget *root) :
32
WGLWidget(root)
33
{
34
	
35
}
36

    
37
// Destructor(s)
38
GraphicsPanel::~GraphicsPanel()
39
{
40

    
41
}
42

    
43

    
44
void GraphicsPanel::initializeGL()
45
{	
46
	depthFunc(LESS);
47
	enable(DEPTH_TEST);
48
	clearStencil(0);
49
	clearColor(0.0, 1.0, 0.0, 1.0);
50
}
51

    
52
void GraphicsPanel::paintGL()
53
{
54
	clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
55
}
56

    
57
void GraphicsPanel::resizeGL(int  width, int height)
58
{
59
	if (height == 0)
60
	{
61
		height = 1;
62
	}
63

    
64
	viewport(0, 0, width, height);		// Reset The Current Viewport	
65
}
66

    
67
class GLApp : public Wt::WApplication
68
{
69
public:
70
	GLApp(const Wt::WEnvironment& env, bool embedded);
71
private:
72
	Wt::WText *greeting_;
73
	GraphicsPanel* _graphicsPanel;
74
};
75
/********************************************************************************/
76

    
77
/***********************************GLApp****************************************/
78
GLApp::GLApp(const Wt::WEnvironment& env, bool embedded)
79
	: WApplication(env)
80
{
81
	Wt::WContainerWidget *top;
82

    
83
	setTitle("GLApp with Wt - Alpha");
84

    
85
	if (!embedded)
86
	{
87
		/*
88
		* In Application mode, we have the root() is a container
89
		* corresponding to the entire browser window
90
		*/
91
		top = root();
92
	}
93
	else
94
	{
95
		/*
96
		* In WidgetSet mode, we create and bind containers to existing
97
		* divs in the web page. In this example, we create a single div
98
		* whose DOM id was passed as a request argument.
99
		*/
100
		top = new Wt::WContainerWidget();
101
		const std::string *div = env.getParameter("div");
102
		if (div)
103
		{
104
			setJavaScriptClass(*div);
105
			bindWidget(top, *div);
106
		}
107
		else
108
		{
109
			std::cerr << "Missing: parameter: 'div'" << std::endl;
110
			return;
111
		}
112
	}
113

    
114
	if (!embedded)
115
	{
116
		new Wt::WText
117
			("<p><emph>Note: you can also run this application "
118
			"from within <a href=\"test_wt_gl_widgetset.html\">a web page</a>.</emph></p>",
119
			root());
120
	}
121

    
122
	/*
123
	* Everything else is business as usual.
124
	*/
125
	//top->setInline(false);
126

    
127
	_graphicsPanel = new GraphicsPanel(top);
128
	_graphicsPanel->resize(500, 300);
129
	_graphicsPanel->setAlternativeContent(new WImage("nowebgl.png"));
130
	_graphicsPanel->setInline(false);
131

    
132

    
133
	greeting_ = new Wt::WText(top);
134
	greeting_->setText("Hello there, from GLApp");
135

    
136
}
137
/********************************************************************************/
138

    
139
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
140
{
141
	return new GLApp(env, false);
142
}
143

    
144
Wt::WApplication *createWidgetSet(const Wt::WEnvironment& env)
145
{
146
	return new GLApp(env, true);
147
}
148

    
149
int main(int argc, char **argv)
150
{
151
	Wt::WServer server(argv[0]);
152

    
153
	// Use default server configuration: command line arguments and the
154
	// wthttpd configuration file.
155
	server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
156

    
157
	// Application entry points. Each entry point binds an URL with an
158
	// application (with a callback function used to bootstrap a new
159
	// application).
160

    
161
	// The following is the default entry point. It configures a
162
	// standalone Wt application at the deploy path configured in the
163
	// server configuration.
164
	server.addEntryPoint(Wt::Application, createApplication);
165

    
166
	// The following adds an entry point for a widget set. A widget set
167
	// must be loaded as a JavaScript from an HTML page.
168
	server.addEntryPoint(Wt::WidgetSet, createWidgetSet, "/GLApp.js");
169

    
170
	// Start the server (in the background if there is threading support)
171
	// and wait for a shutdown signal (e.g. Ctrl C, SIGKILL)
172
	if (server.start())
173
	{
174
		Wt::WServer::waitForShutdown();
175

    
176
		// Cleanly terminate all sessions
177
		server.stop();
178
	}
179
}
(1-1/5)