forked from plastictown/Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.cpp
More file actions
240 lines (196 loc) · 5.46 KB
/
Copy pathrender.cpp
File metadata and controls
240 lines (196 loc) · 5.46 KB
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
/*
* https://github.com/plastictown/Engine
* Copyright (C) 2018 Mikhail Domchenkov
*/
#include <render.h>
#include <exception>
#include <stdexcept>
Render* Render::instance = nullptr;
void Render::draw_object(const Drawable& o) {
auto res = callbacks.find(o.getType());
if (res != callbacks.cend())
callbacks[o.getType()](&o);
}
void Render::DrawObject(const Drawable& o) {
instance->draw_object(o);
}
void Render::draw_point2(const Drawable* po) {
auto p = dynamic_cast<const PointObject*>(po);
glBegin(GL_POINTS);
glColor4f(p->getColor().r, p->getColor().g, p->getColor().b, p->getColor().a);
glVertex2f(p->getPoint().x, p->getPoint().y);
glEnd();
}
void Render::draw_line2(const Drawable* po) {
auto p = dynamic_cast<const LineObject*>(po);
glBegin(GL_LINES);
glColor4f(p->getColor().r, p->getColor().g, p->getColor().b, p->getColor().a);
glVertex2f(p->getA().x, p->getA().y);
glVertex2f(p->getB().x, p->getB().y);
glEnd();
}
void Render::drawPoint2(const Drawable* po) {
instance->draw_point2(po);
}
void Render::drawLine2(const Drawable* po) {
instance->draw_line2(po);
}
void Render::Entry(int state) {
// TODO
}
void Render::entry_func(int state) {
instance->Entry(state);
}
void Render::Keyboard(unsigned char key, int x, int y) {
// TODO
if (key == KEY_ESCAPE)
#ifdef FREEGLUT
glutLeaveMainLoop();
#else
exit(0);
#endif //! FREEGLUT
}
void Render::keyboard_func(unsigned char key, int x, int y) {
instance->Keyboard(key, x, y);
}
void Render::Motion(int x, int y) {
// TODO
}
void Render::motion_func(int x, int y) {
instance->Motion(x, y);
}
void Render::Mouse(int button, int state, int x, int y) {
// int modifiers = glutGetModifiers();
// using with GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT, GLUT_ACTIVE_SHIFT
// TODO
}
void Render::mouse_func(int button, int state, int x, int y) {
instance->Mouse(button, state, x, y);
}
void Render::passive_motion_func(int x, int y) {
instance->PassiveMotion(x, y);
}
void Render::PassiveMotion(int x, int y) {
// TODO
}
void Render::idle_fucn() {
instance->Idle();
}
void Render::Idle() {
draw();
}
Point2f Render::getWindowSize() noexcept {
GLfloat x = glutGet(GLUT_WINDOW_WIDTH);
GLfloat y = glutGet(GLUT_WINDOW_HEIGHT);
return Point2f{x, y};
}
void Render::init() {
int image_flags = IMG_INIT_PNG;
int init = IMG_Init(image_flags);
if ((init & image_flags) != image_flags)
throw std::runtime_error("can't init SDL_Image");
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glutDisplayFunc(draw_func);
glutReshapeFunc(reshape_func);
glutIdleFunc(idle_fucn);
glutPassiveMotionFunc(passive_motion_func);
glutMouseFunc(mouse_func);
glutMotionFunc(motion_func);
glutEntryFunc(entry_func);
glutKeyboardFunc(keyboard_func);
setClearColor(Color4{1.0f, 1.0f, 1.0f, 0.5f});
// auto f = std::function<void(const Drawable* )>(Render::drawPoint2);
callbacks.insert(make_pair(string("point2f"), Render::drawPoint2));
callbacks.insert(make_pair(string("line2f"), Render::drawLine2));
}
void Render::draw_func() {
instance->draw();
}
void Render::reshape_func(GLint w, GLint h) {
instance->reshape(w, h);
}
void Render::setupCam(GLint x, GLint y, GLint w, GLint h) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glViewport(x, y, w, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Render::reshape(int w, int h) {
glutPostRedisplay();
setupCam(0, 0, w, h);
}
Render::Render(int argc,
char** argv,
GLuint w,
GLuint h,
GLuint x,
GLuint y,
char* name) {
instance = this;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA);
glutInitWindowSize(w, h);
if (!(x == 0 && y == 0))
glutInitWindowPosition(x, y);
if (name != nullptr)
glutCreateWindow(name);
else
glutCreateWindow("OpenGL window");
init(); // throws std::runtime_error
setupCam(0, 0, 640, 480);
}
Render::~Render() {
// disable callback
glutDisplayFunc(nullptr);
glutReshapeFunc(nullptr);
glutIdleFunc(nullptr);
glutPassiveMotionFunc(nullptr);
glutMouseFunc(nullptr);
glutMotionFunc(nullptr);
glutEntryFunc(nullptr);
glutKeyboardFunc(nullptr);
IMG_Quit();
Image::cleanup();
}
void Render::setClearColor(Color4&& c) {
m_clearColor = c;
glClearColor(c.r, c.g, c.b, c.a);
}
void Render::draw() {
glClear(GL_COLOR_BUFFER_BIT);
if (scene != nullptr) {
if (scene->isVisible())
scene->Draw();
}
glFlush();
glutSwapBuffers();
}
void Render::DrawImage(const Image& im, const Point2f& pos) {
glBindTexture(GL_TEXTURE_2D, im.getId());
Rectf r(Point2f(0, 0), Point2f(im.width(), im.height()));
r.heightByAspect(im.getAspect());
glPushMatrix();
glTranslatef(pos.x, pos.y, 0.0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(0, 0);
glTexCoord2f(0, 1);
glVertex2i(0, r.right_bottom.y);
glTexCoord2f(1, 1);
glVertex2i(r.right_bottom.x, r.right_bottom.y);
glTexCoord2f(1, 0);
glVertex2i(r.right_bottom.x, 0);
glEnd();
glPopMatrix();
}
void Render::run() {
glutMainLoop();
}
void Render::SetScene(shared_ptr<Scene>& s) {
scene = s;
}