-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
262 lines (224 loc) · 5.06 KB
/
code.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
#include<windows.h>
#include<scrnsave.h>
#include<glut.h>
#include<math.h>
#include"resource.h"
//define windows timer
#define TIMER 1000
#define szAppName TEXT("Sine screensaver-Jax")
void InitGL(HWND hWnd, HDC & hDC, HGLRC & hRC);
void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC);
void SetupAnimation(int Width, int Height);
void CleanupAnimation();
void OnTimer(HDC hDC);
int Width, Height; //globals for size of screen
// THE THREE FUNCTIONS
// Screen Saver Procedure
LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
{
static HDC hDC;// Handle to device context
static HGLRC hRC;//Handle to OpenGL rendering context
static RECT rect; //Instance of structure RECT which defines the coords of the
//upper-left and lower-right corners of a rectangle
switch(message)
{
case WM_CREATE:
// get window dimensions
GetClientRect( hWnd, &rect );
Width = rect.right; //Store width
Height = rect.bottom; // Store height
// setup OpenGL, then animation
InitGL( hWnd, hDC, hRC );
SetupAnimation(Width, Height);
//set timer to tick every 10 ms
SetTimer( hWnd, TIMER, 10, NULL );
return 0;
case WM_DESTROY:
KillTimer( hWnd, TIMER );
CleanupAnimation();
CloseGL( hWnd, hDC, hRC );
return 0;
case WM_TIMER:
OnTimer(hDC); //animate!
return 0;
}
return DefScreenSaverProc(hWnd,message,wParam,lParam);
}
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
{
//InitCommonControls();
//would need this for slider bars or other common controls
switch ( message )
{
case WM_INITDIALOG:
LoadString(hMainInstance, IDS_DESCRIPTION, szAppName, 40);
return TRUE;
case WM_COMMAND:
switch( LOWORD( wParam ) )
{
case IDOK:
EndDialog( hDlg, LOWORD( wParam ) == IDOK );
return TRUE;
}
} //end command switch
return FALSE;
}
// needed for SCRNSAVE.LIB
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return TRUE;
}
// Initialize OpenGL
static void InitGL(HWND hWnd, HDC & hDC, HGLRC & hRC)
{
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof pfd );
pfd.nSize = sizeof pfd;
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //blaine's
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
hDC = GetDC( hWnd ); //Retrieves a handle to a display device context
int i = ChoosePixelFormat( hDC, &pfd );
SetPixelFormat( hDC, i, &pfd );
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );
}
// Shut down OpenGL
static void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}
void SetupAnimation(int Width, int Height)
{
glViewport(0, 0,Width,Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0,-1, 0, 1, 0);
//background
glClearColor(0,0,0,0); //0.0s is black
}
float x, y;//point1(x,y)
float z,m;//point2(z,m)
float j,k;//point2(j,k)
float spin=0;
int color_num=1;//color counter
float color[60][3]={
{0.0,0.0,0.1},
{0.0,0.0,0.2},
{0.0,0.0,0.3},
{0.0,0.0,0.4},
{0.0,0.0,0.5},
{0.0,0.0,0.6},
{0.0,0.0,0.7},
{0.0,0.0,0.8},
{0.0,0.0,0.9},
{0.0,0.0,1.0},//0,0,1
{0.0,0.1,0.0},
{0.0,0.2,0.0},
{0.0,0.3,0.0},
{0.0,0.4,0.0},
{0.0,0.5,0.0},
{0.0,0.6,0.0},
{0.0,0.7,0.0},
{0.0,0.8,0.0},
{0.0,0.9,0.0},
{0.0,1.0,0.0},//0,1,0
{0.0,1.0,0.1},
{0.0,1.0,0.2},
{0.0,1.0,0.3},
{0.0,1.0,0.4},
{0.0,1.0,0.5},
{0.0,1.0,0.6},
{0.0,1.0,0.7},
{0.0,1.0,0.8},
{0.0,1.0,0.9},
{0.0,1.0,1.0},//0,1,1
{0.1,0,0},
{0.2,0,0},
{0.4,0,0},
{0.5,0,0},
{0.6,0,0},
{0.7,0,0},
{0.8,0,0},
{0.9,0,0},
{1.0,0,0},//1,0,0
{1.0,0.0,0.1},
{1.0,0.0,0.2},
{1.0,0.0,0.3},
{1.0,0.0,0.4},
{1.0,0.0,0.5},
{1.0,0.0,0.6},
{1.0,0.0,0.7},
{1.0,0.0,0.8},
{1.0,0.0,0.9},
{1.0,0.0,1.0},//1,0,1
{1.0,1.0,0.1},
{1.0,1.0,0.2},
{1.0,1.0,0.3},
{1.0,1.0,0.4},
{1.0,1.0,0.5},
{1.0,1.0,0.6},
{1.0,1.0,0.7},
{1.0,1.0,0.8},
{1.0,1.0,0.9},
{1.0,1.0,1.0}//1,1,1
};
//for every 10 milli seconds Ontimer will get called
void OnTimer(HDC hDC) //increment and display
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
spin++;
glPushMatrix();
glRotatef(spin, 1.0, 0.0, 0.0);
for(x=-10;x<10;x+=0.01)
{
if(color_num==60)
color_num=1;
glColor3fv(color[color_num]);
color_num++;
y=sin(10*x)/(1+(x*x));
glRasterPos3f(x,y,1.7);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'*');
}
glPushMatrix();
glRotatef(spin*-3.0, 0.0, 0.0, 1.0);
for(z=-10;z<10;z+=0.001)
{
if(color_num==60)
color_num=1;
glColor3fv(color[color_num]);
color_num++;
m=sin(10*z)/(1+(z*z));
glBegin(GL_POINTS);
glVertex2f(z,m);
glEnd();
}
glPushMatrix();
glRotatef(spin*-3.0 , 1.0, 0.0, 0.0);
for(j=-10;j<10;j+=0.001)
{
if(color_num==60)
color_num=1;
glColor3fv(color[color_num]);
color_num++;
k=sin(10*j)/(1+(j*j));
glBegin(GL_POINTS);
glVertex2f(j,k);
glEnd();
}
glPopMatrix();
glPopMatrix();
glFinish();
SwapBuffers(hDC);
glPopMatrix();
}
void CleanupAnimation()
{
//didn't create any objects, so no need to clean them up
}