-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
324 lines (266 loc) · 7.46 KB
/
test.cpp
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
217
218
219
220
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SOIL/SOIL.h>
#include <glm/glm.hpp>
#include <iostream>
#include "window.hpp"
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
int main()
{
using namespace sre;
using namespace std;
// Initialize GLEW
glewExperimental = GL_TRUE;
glewInit();
#ifndef TESTWINDOW_CPP
#define TESTWINDOW_CPP
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <string>
#include <iostream>
#include <stdexcept>
#include <thread>
#include <atomic>
#include <sstream>
#include <memory>
#include "lexer.hpp"
#include "command.hpp"
#include "window.hpp"
#include "parser.hpp"
//#define GLEW_STATIC
const char* vertexSource =
"#version 150\n"
"in vec2 position;"
"in vec3 color;"
"out vec3 Color;"
"void main() {"
" Color = color;"
" gl_Position = vec4( position, 0.0, 1.0 );"
"}";
const char* fragmentSource =
"#version 150\n"
"in vec3 Color;"
"out vec4 outColor;"
"void main() {"
" outColor = vec4( Color, 1.0 );"
"}";
std::atomic< sre::Window *> win (nullptr);
// thread-safe output buffer for input
std::atomic< std::stringstream *> inputBuffer (nullptr);
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose( win.load()->getGLFWwindow(), GL_TRUE );
}
if else (key == GLFW_KEY_EQUAL && action == GLFW_PRESS) {
}
if else (key == GLFW_KEY_MINUS && action == GLFW_PRESS) {
}
}
void input();
void render(GLuint);
int main()
{
using namespace std;
using namespace sre;
// initialize inputBuffer
stringstream tempstr;
inputBuffer.store(&tempstr);
// GLFW init
if( !glfwInit() )
return 1;
// Window::init()
// create Window
DefaultHints defhints = DefaultHints();
defhints.apply();
WindowHints winHints = WindowHints();
winHints.resizable(1);
winHints.visible(1);
winHints.decorated(1);
// test for Framebuffer methods
FramebufferHints fbHints = FramebufferHints();
// fbHints.redBits(1);
// fbHints.greenBits(1);
// fbHints.blueBits(1);
// fbHints.alphaBits(1);
// fbHints.depthBits(1);
// fbHints.stencilBits(1);
// fbHints.accumRedBits(1);
// fbHints.accumGreenBits(1);
// fbHints.accumBlueBits(1);
// fbHints.accumAlphaBits(1);
// fbHints.auxBuffers(1);
// fbHints.samples(1);
// fbHints.refreshRate(30);
// fbHints.stereo(true);
// fbHints.sRGBCapable(true);
// test for ContextHints
ContextHints cHints = ContextHints();
//cHints.setAPIES();
cHints.versionMajor(3);
cHints.versionMinor(3);
//cHints.forwardCompat(true);
//cHints.debug(false);
cHints.setProfileAny();
Hints * hints;
hints = &cHints;
Window unatomicWin = Window( 640, 480, "OpenGL", *hints );
win.store(&unatomicWin);
if ( !win.load()->exists() )
{
throw runtime_error("Window creation failed.");
}
win.load()->makeActive();
// GLEW init
glewExperimental = GL_TRUE; // include all the fancy features!
if( glewInit() != GLEW_OK )
throw runtime_error("GLEW initialization failed.");
// vao
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// vertex array
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// vertexbuffer
GLuint vertexbuffer;
glGenBuffers( 1, &vertexbuffer );
glBindBuffer( GL_ARRAY_BUFFER, vertexbuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glfwSetKeyCallback(win.load()->getGLFWwindow(), key_callback);
// main loop
//create and initialize input thread
thread textInputThread(input);
textInputThread.detach();
while( !win.load()->shouldClose() )
{
render(vertexbuffer);
}
glfwTerminate();
cerr<<"end";
//garbage collection
return 0;
}
void input()
{
using namespace std;
using namespace sre::console;
istringstream inputbuf;
string str = "";
Token tok;
// now test Command
Type arg0 = Type("arg0"), arg1 = Type("arg1");
CommandDef cmdDef = CommandDef("nothing");
cmdDef.push(arg0);
cmdDef.push(arg1);
cmdDef.push(arg1);
shared_ptr<CommandFunc> cmdFuncPtr = shared_ptr<CommandFunc>(new EmptyCommand() );
cmdDef.setFunc(cmdFuncPtr);
Command cmd = Command("nothing");
cmd.push(arg0);
cmd.push(arg1);
cmd.push(arg1);
cout<<(*cmdFuncPtr)(cmd)<<endl;
cout<< "Is a command: " << (cmdDef == cmd) << endl;
// create CommandMap and VariableMap for Lexer to use
CommandMap cmdMap;
VariableMap varMap;
// create lexer
Lexer * lex = new Lexer("");
// create and link parser to lexer
Parser parser = Parser(*lex);
do
{
getline(cin, str, '\n');
//str.append("\n");
//cout<<str;
delete lex;
lex = new Lexer(str);
Token tok;
do {
tok = lex->next();
cout<<"type: "<<tok.name()<<" value: "<<tok.value<<std::endl;
} while (tok != Token::END_OF_INPUT && tok != Token::ERROR);
} while (true);
delete lex;
// string title;
// glm::ivec2 pos;
// glm::ivec2 size;
//
// while (true)
// {
// cout<<"set title: ";
// cout<<"title: ";
// cin>>title;
//
// win.load()->setTitle(title);
//
// cout<<"set pos: ";
// cin>>pos.x>>pos.y;
// win.load()->setPos(pos);
//
// cout<<"set size: ";
// cin>>size.x>>size.y;
// win.load()->setSize(size);
//
// cout<<endl;
//
// //spit out what the window is now
// cout<<"Window Values..."<<endl;
//
// pos = win.load()->getPos();
// cout<<"pos: "<<pos.x<<" "<<pos.y<<endl;
// size = win.load()->getSize();
// cout<<"size: "<<size.x<<" "<<size.y<<endl;
//
// cout<<endl;
// }
}
void render(GLuint vertexbuffer)
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
win.load()->swapBuffers();
// test various methods taking input from the user
glfwPollEvents();
}
#endif // TESTWINDOW_CPP
// Initialize GLFW
if (!glfwInit())
return -1;
Monitor mon();
WindowSettings winSet();
FrameBufferSettings frameBufSet();
ContextSettings contSet();
//Window winDefault = Window();
//Window winCustom = Window( 800, 600, "custom window");
//winDefault.setActive();
Window win = Window();
//glfwSetKeyCallback ( window.getGLFWWindow(), key_callback );
while( !win.windowShouldClose() )
{
win.swapBuffers();
glfwPollEvents();
}
// Terminate GLFW
glfwTerminate();
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}