-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengl_window.cpp
170 lines (128 loc) · 4.6 KB
/
opengl_window.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
// opening_a_window using glut example
//
//////////////////////////////////////////////////////////////////////////////////////////
// includes
//////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RustyLib.h"
using RustyLib::Framework::StopWatch;
StopWatch* deltaTimer;
double last_time;
int width = 800;
int height = 600;
#include "Game.h"
Game game;
//////////////////////////////////////////////////////////////////////////////////////////
// renderScene() - render the scene
//////////////////////////////////////////////////////////////////////////////////////////
void renderScene()
{
// clear the back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// render code here...
game.Render();
// swap the buffers of the current window
glutSwapBuffers();
}
//////////////////////////////////////////////////////////////////////////////////////////
// winReshapeFunc() - gets called initially and whenever the window get resized
// resizing has been locked
//////////////////////////////////////////////////////////////////////////////////////////
void winReshapeFunc(GLint w, GLint h)
{
// specify current matrix
glMatrixMode(GL_PROJECTION);
// load an identity matrix
glLoadIdentity();
// create a projection matrix... i.e. 2D projection onto xy plane
gluOrtho2D( 0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// set up the viewport
glViewport
(
0, // lower left x position
0, // lower left y position
(GLsizei) width, // viewport width
(GLsizei) height // viewport height
);
}
//////////////////////////////////////////////////////////////////////////////////////////
// update() - update function
//////////////////////////////////////////////////////////////////////////////////////////
void update()
{
// add any update code here...
//Get the delta time
double currentTime = deltaTimer->GetTime();
const double DELTA_TIME = currentTime - last_time;
last_time = currentTime;
game.Update(DELTA_TIME);
// always re-render the scene..
renderScene();
}
//////////////////////////////////////////////////////////////////////////////////////////
// kbUpFunc() - program exit point
//////////////////////////////////////////////////////////////////////////////////////////
void kbUpFunc(unsigned char key, int x, int y)
{
RustyLib::Input::Keyboard::HandleKeyUp(key);
}
//////////////////////////////////////////////////////////////////////////////////////////
// kbDnFunc() - program exit point
//////////////////////////////////////////////////////////////////////////////////////////
void kbDnFunc(unsigned char key, int x, int y)
{
RustyLib::Input::Keyboard::HandleKeyDown(key);
}
//////////////////////////////////////////////////////////////////////////////////////////
// exitFunc() - program exit point
//////////////////////////////////////////////////////////////////////////////////////////
void exitFunc(void )
{
game.Exit();
}
//////////////////////////////////////////////////////////////////////////////////////////
// _tmain() - program entry point
//////////////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
// initialise the glut library
glutInit(&argc, argv);
// set up the initial display mode
// need both double buffering and z-buffering
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// set the initial window position
glutInitWindowPosition(100, 100);
// set the initial window size
glutInitWindowSize(width, height);
// create and name the window
glutCreateWindow("Rusty Asteroids");
// reshape callback for current window
glutReshapeFunc(winReshapeFunc);
// set display callback for current window
glutDisplayFunc(renderScene);
// set up the global idle callback
glutIdleFunc(update);
//Set up key func
glutKeyboardUpFunc(kbUpFunc);
glutKeyboardFunc(kbDnFunc);
//Set up exit function
atexit(exitFunc);
// good place for one-off initialisations and objects creation..
RustyLib::Graphics::Renderer::Instance().SetClipMode(
RustyLib::Graphics::ClipMode::CohenSutherland);
RustyLib::Graphics::Renderer::Instance().SetClippingRegion(
0, 0, 800, 600);
RustyLib::Graphics::Renderer::Instance().SetLineAlgorithm(
RustyLib::Graphics::LineAlgorithm::Bresenhams);
RustyLib::Graphics::Renderer::Instance().SetTriAlgorithm(
RustyLib::Graphics::TriAlgorithm::Take2);
RustyLib::Graphics::Renderer::Instance().SetRenderMode(
RustyLib::Graphics::RenderMode::SolidColour);
deltaTimer = new StopWatch();
game.Initialise();
// enter the glut event processing loop
glutMainLoop();
return 0;
}