1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19*/
20// gl_ngraph.c
21
22#include "quakedef.h"
23
24extern byte		*draw_chars;				// 8*8 graphic characters
25
26int	netgraphtexture;	// netgraph texture
27
28#define NET_GRAPHHEIGHT 32
29
30static	byte ngraph_texels[NET_GRAPHHEIGHT][NET_TIMINGS];
31
32static void R_LineGraph (int x, int h)
33{
34	int		i;
35	int		s;
36	int		color;
37
38	s = NET_GRAPHHEIGHT;
39
40	if (h == 10000)
41		color = 0x6f;	// yellow
42	else if (h == 9999)
43		color = 0x4f;	// red
44	else if (h == 9998)
45		color = 0xd0;	// blue
46	else
47		color = 0xfe;	// white
48
49	if (h>s)
50		h = s;
51
52	for (i=0 ; i<h ; i++)
53		if (i & 1)
54			ngraph_texels[NET_GRAPHHEIGHT - i - 1][x] = 0xff;
55		else
56			ngraph_texels[NET_GRAPHHEIGHT - i - 1][x] = (byte)color;
57
58	for ( ; i<s ; i++)
59		ngraph_texels[NET_GRAPHHEIGHT - i - 1][x] = (byte)0xff;
60}
61
62void Draw_CharToNetGraph (int x, int y, int num)
63{
64	int		row, col;
65	byte	*source;
66	int		drawline;
67	int		nx;
68
69	row = num>>4;
70	col = num&15;
71	source = draw_chars + (row<<10) + (col<<3);
72
73	for (drawline = 8; drawline; drawline--, y++)
74	{
75		for (nx=0 ; nx<8 ; nx++)
76			if (source[nx] != 255)
77				ngraph_texels[y][nx+x] = 0x60 + source[nx];
78		source += 128;
79	}
80}
81
82
83/*
84==============
85R_NetGraph
86==============
87*/
88void R_NetGraph (void)
89{
90	int		a, x, i, y;
91	int lost;
92	char st[80];
93	unsigned	ngraph_pixels[NET_GRAPHHEIGHT][NET_TIMINGS];
94
95	x = 0;
96	lost = CL_CalcNet();
97	for (a=0 ; a<NET_TIMINGS ; a++)
98	{
99		i = (cls.netchan.outgoing_sequence-a) & NET_TIMINGSMASK;
100		R_LineGraph (NET_TIMINGS-1-a, packet_latency[i]);
101	}
102
103	// now load the netgraph texture into gl and draw it
104	for (y = 0; y < NET_GRAPHHEIGHT; y++)
105		for (x = 0; x < NET_TIMINGS; x++)
106			ngraph_pixels[y][x] = d_8to24table[ngraph_texels[y][x]];
107
108	x =	-((vid.width - 320)>>1);
109	y = vid.height - sb_lines - 24 - NET_GRAPHHEIGHT - 1;
110
111	M_DrawTextBox (x, y, NET_TIMINGS/8, NET_GRAPHHEIGHT/8 + 1);
112	y += 8;
113
114	sprintf(st, "%3i%% packet loss", lost);
115	Draw_String(8, y, st);
116	y += 8;
117
118    GL_Bind(netgraphtexture);
119
120	glTexImage2DHelper (GL_TEXTURE_2D, 0, gl_alpha_format,
121		NET_TIMINGS, NET_GRAPHHEIGHT, 0, GL_RGBA,
122		GL_UNSIGNED_BYTE, ngraph_pixels);
123
124	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
125	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127
128	x = 8;
129
130#ifdef USE_OPENGLES
131	glColor4f (1,1,1,1);
132	DrawQuad(x, y, NET_TIMINGS, NET_GRAPHHEIGHT, 0, 0, 1, 1);
133#else
134	glColor3f (1,1,1);
135	glBegin (GL_QUADS);
136	glTexCoord2f (0, 0);
137	glVertex2f (x, y);
138	glTexCoord2f (1, 0);
139	glVertex2f (x+NET_TIMINGS, y);
140	glTexCoord2f (1, 1);
141	glVertex2f (x+NET_TIMINGS, y+NET_GRAPHHEIGHT);
142	glTexCoord2f (0, 1);
143	glVertex2f (x, y+NET_GRAPHHEIGHT);
144	glEnd ();
145#endif
146}
147
148