File: fpscount.cpp

package info (click to toggle)
amoeba 1.1-13
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 732 kB
  • ctags: 971
  • sloc: cpp: 8,315; makefile: 178
file content (177 lines) | stat: -rw-r--r-- 3,895 bytes parent folder | download | duplicates (12)
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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif


#include "fpscount.h"
#include "../demolib_prefs.h"
#include "../image/image.h"
#include "../exception.h"

#if DEMOLIB_FPSCOUNTER

#include <GL/gl.h>

FPSCounter::FPSCounter()
{
	Image *digits = load_image("digitsxp.png");
	memcpy(this->digits_bm, digits->get_pixel_data(), 128 * 16 * 4);
	delete digits;
	this->reset();
}

FPSCounter::~FPSCounter()
{
}

void FPSCounter::reset()
{
#ifdef WIN32
	this->tv[0] = GetTickCount();	
	this->tv[1] = GetTickCount();	
	this->tv[2] = GetTickCount();	
	this->tv[3] = GetTickCount();	
	this->tv[4] = GetTickCount();	
	this->tv[5] = GetTickCount();	
	this->tv[6] = GetTickCount();	
	this->tv[7] = GetTickCount();	
	this->tv[8] = GetTickCount();	
	this->tv[9] = GetTickCount();	
#else	
	gettimeofday(&(this->tv[0]), NULL);
	gettimeofday(&(this->tv[1]), NULL);
	gettimeofday(&(this->tv[2]), NULL);
	gettimeofday(&(this->tv[3]), NULL);
	gettimeofday(&(this->tv[4]), NULL);
	gettimeofday(&(this->tv[5]), NULL);
	gettimeofday(&(this->tv[6]), NULL);
	gettimeofday(&(this->tv[7]), NULL);
	gettimeofday(&(this->tv[8]), NULL);
	gettimeofday(&(this->tv[9]), NULL);
#endif
}

float FPSCounter::getfps()
{
	float fps;
	
#ifdef WIN32
	long now;
//	QueryPerformanceCounter((LARGE_INTEGER*)&now);
	now = GetTickCount();	
#else
	struct timeval now;
	gettimeofday(&now, NULL);
#endif

#ifdef WIN32
	fps = 10000.0f / (float)((now - this->tv[0]));
#else
	fps = 10000000.0f / 
		(float)((now.tv_sec  - this->tv[0].tv_sec) * 1000000 +
			(now.tv_usec - this->tv[0].tv_usec));
#endif

	tv[0] = tv[1];
	tv[1] = tv[2];
	tv[2] = tv[3];
	tv[3] = tv[4];
	tv[4] = tv[5];
	tv[5] = tv[6];
	tv[6] = tv[7];
	tv[7] = tv[8];
	tv[8] = tv[9];
	tv[9] = now;

	return fps;
}

void FPSCounter::draw()
{
	char fpsstring[10];
	int i;
//	GLuint texnum;

	float fps = this->getfps();

	sprintf(fpsstring, "%6.2ffps", fps);

	memset(texbuf, 0x00, 64*16*4);

	for (i = 0; i <= 8; i++) {
		int offs = 0;
		if (fpsstring[i] == ' ') continue;
		switch (fpsstring[i]) {
			case '0': offs =  0; break;
			case '1': offs =  6; break;
			case '2': offs = 12; break;
			case '3': offs = 18; break;
			case '4': offs = 24; break;
			case '5': offs = 30; break;
			case '6': offs = 36; break;
			case '7': offs = 42; break;
			case '8': offs = 48; break;
			case '9': offs = 54; break;
			case '.': offs = 60; break;
			case 'f': offs = 66; break;
			case 'p': offs = 72; break;
			case 's': offs = 78; break;
		}
		for (int y = 0; y < 10; y++) {
			memcpy(this->texbuf + y*64*4 + i*6*4, this->digits_bm + y*128*4 + offs*4, 24);
		}
	}

	/*glGenTextures(1, &texnum); */
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 0);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 64, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, this->texbuf);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0.0f,1.0f,0.0f,1.0f,0.0f,10.0f);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glEnable(GL_TEXTURE_2D);

	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
       
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f,1.0f);
		glVertex3f(0.8f,0.9f,0.0f);
		glTexCoord2f(1.0f,1.0f);
		glVertex3f(1.0f,0.9f,0.0f);
		glTexCoord2f(1.0f,0.0f);
		glVertex3f(1.0f,1.0f,0.0f);
		glTexCoord2f(0.0f,0.0f);
		glVertex3f(0.8f,1.0f,0.0f);
	glEnd();

	glPopMatrix();

	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

//	glDeleteTextures(1, &texnum);
}
#endif