File: util.cc

package info (click to toggle)
enemylines3 1.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,384 kB
  • ctags: 1,445
  • sloc: cpp: 16,323; makefile: 70; sh: 52
file content (252 lines) | stat: -rw-r--r-- 5,042 bytes parent folder | download | duplicates (6)
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
#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>

#include "util.h"
#include "mapbase.h"
#include "coordinate.h"
#include "release.h"

#include "font_ogl.h"


namespace el3 {
void error() {
   GLenum errorcode;
   const GLubyte *errorstring;


   if ((errorcode= glGetError()) != GL_NO_ERROR) {
      errorstring = gluErrorString(errorcode);
      std::cerr << "GL_ERROR: " << errorstring << std::endl;
   }

}

void ortho2d(float dx,float dy) {
	//disable stuff
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);


	// change projection to ortho 
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0,dx,dy,0.0);

	// back to mv and push
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
}

void ortho2d_off() {
	//enable stuff
	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);


	// pop projection matrix
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();


	// pop mv matrix
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
}

void screenshot(int w,int h,unsigned int t) {
	Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif
	SDL_Surface *my;
	my=SDL_CreateRGBSurface( SDL_SWSURFACE, w,h, 32, rmask, gmask, bmask, amask);
	glReadBuffer(GL_FRONT);
	glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, my->pixels);
	std::ostringstream sstr;
	if (t==0) t=time(NULL);
	sstr << SHORTNAME << "_screenshot_" << t << ".bmp";
	SDL_SaveBMP(my,sstr.str().c_str());
}


C3f unproject(int x, int y) {
	GLint viewport[4];
	GLdouble modelview[16];
	GLdouble projection[16];
	GLdouble winX, winY;
	GLfloat winZ;
	GLdouble posX, posY, posZ;

	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
	glGetDoublev( GL_PROJECTION_MATRIX, projection );
	glGetIntegerv( GL_VIEWPORT, viewport );

	winX = (GLdouble)x;
	winY = (GLdouble)viewport[3] - (GLdouble)y;
	glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

	gluUnProject( winX, winY,(GLdouble) winZ, modelview, projection, viewport, &posX, &posY, &posZ);

	return C3f((float)posX, (float)posY, (float)posZ);
}

float destdegree(C3f p1,C3f p2) {
   static const float radtodeg = 180/M_PI;
   float opp;
   opp=(float)(p1.z-p2.z);

   float a,b;
   a=float(p1.x-p2.x);
   b=float(p1.z-p2.z);
   a*=a;
   b*=b;

   float hyp=sqrtf(a+b);

   float sinalpha=opp/hyp;

   float deg;
   deg=asinf(sinalpha)*radtodeg;
	if (p2.x<p1.x) {
		deg=180.0f+deg*-1;
	}
   return deg;
}

void bar(C3 start,C3 size) {
   glBegin(GL_QUADS);
   glVertex2i(start.x,start.y);
   glVertex2i(start.x+size.x,start.y);
   glVertex2i(start.x+size.x,start.y+size.y);
   glVertex2i(start.x,start.y+size.y);
   glEnd();
}

void crosshair() {
	static GLuint dl=0;
	if (dl!=0) { glCallList(dl); return; }
	dl = glGenLists(1);
	glNewList(dl,GL_COMPILE_AND_EXECUTE);


   C3 center;
   center.x=320;
   center.y=240;
   int size=10;
   int d=4;


	glBegin(GL_LINES);
		glVertex2i(center.x,center.y-size-d);
		glVertex2i(center.x,center.y-d);
	glEnd();
	glBegin(GL_LINES);
		glVertex2i(center.x,center.y+size+d);
		glVertex2i(center.x,center.y+d);
	glEnd();
	glBegin(GL_LINES);
		glVertex2i(center.x-size-d,center.y);
		glVertex2i(center.x-d,center.y);
	glEnd();
	glBegin(GL_LINES);
		glVertex2i(center.x+size+d,center.y);
		glVertex2i(center.x+d,center.y);
	glEnd();

	glEndList();
}

void menu_bg() {
	glPushMatrix();
	glScalef(640,480,0);
	glBegin(GL_QUADS);
	glColor3ub(30,40,250);
	glVertex2i(0,0);

	glColor3ub(60,80,250);
	glVertex2i(1,0);

	glColor3ub(30,40,150);
	glVertex2i(1,1);

	glColor3ub(20,30,130);
	glVertex2i(0,1);
	glEnd();
	glPopMatrix();


}

void lockmouse(){
	SDL_ShowCursor(SDL_DISABLE);
	SDL_WM_GrabInput(SDL_GRAB_ON);
}
void unlockmouse(){
	SDL_ShowCursor(SDL_ENABLE);
	SDL_WM_GrabInput(SDL_GRAB_OFF);
}
void togglemouselock() {
	if (ismouselocked()) {
		unlockmouse();
	} else {
		lockmouse();
	}
}

bool ismouselocked() {
	if (SDL_ShowCursor(SDL_QUERY)==SDL_DISABLE) { return true; }
	return false;
}
bool testspeed() {
	Uint32 start,end,took;

	start=SDL_GetTicks();
	ortho2d();
	glColor3f(1,1,1);

	Font_ogl::write("loading...");
	SDL_GL_SwapBuffers();
	ortho2d_off();
	end=SDL_GetTicks();

	took=(end-start);

	if (took<600) return true;

   SDL_Event event;
   while (true) {
		glLoadIdentity();
     	while  (SDL_PollEvent(&event)) {
			if (event.type==SDL_QUIT) return false;
			if (event.type==SDL_MOUSEBUTTONDOWN) return true;
			if (event.type!=SDL_KEYDOWN) continue;
			if (event.key.keysym.sym==SDLK_ESCAPE) { return false; }
			return true;
		}
		ortho2d();
		glTranslatef(0,20,0);
		Font_ogl::write("  ... system slow");
		glTranslatef(0,20,0);
		Font_ogl::write("  probable cause: no properly configured 3d acceleration.");
		glTranslatef(0,20,0);
		Font_ogl::write("  ESC to quit. any other key: continue anyway.");
		ortho2d_off();
		SDL_GL_SwapBuffers();
		SDL_Delay(150);
	}
	return true;
}

} //namespace