speed-o-meters!


(sniser) #1

Let’s make speed-o-meters and post 'em yay! Here’s what I have right now:

disclaim0r: use at your own risk - the code is sucky, when you set SPEEDGRAPH_STEPS too high it crashes etc… but I hey, I figure it’s better than nothing.


#define SPEEDGRAPH_XSIZE		400
#define SPEEDGRAPH_YSIZE		180
#define SPEEDGRAPH_XOFFSET		120
#define SPEEDGRAPH_YOFFSET		120
#define SPEEDGRAPH_STEPS		1200
#define SPEEDGRAPH_MAXSPEED1	1000.0f
#define SPEEDGRAPH_MAXSPEED2	750.0f
#define SPEEDGRAPH_MAXSPEED3	500.0f
#define SPEEDGRAPH_MAXSPEED4	250.0f
#define SPEEDGRAPH_THRESHOLD	20.0f
void CG2005_DrawSpeedOGraph(void) {
	static float speed[SPEEDGRAPH_STEPS];
	static int currentStep = 0;
	float fade, blarr;
	const char*			str;
	int i,j;
	float max = 0;
	vec4_t color =	{	1.f,	0.f,	0.f,	0.0f	};
	speed[currentStep]=cg.xyspeed;
	for (i=0; i<SPEEDGRAPH_STEPS; i++) {
		if (speed[i] > max)
			max = speed[i];
	}
	if (max == 0)
		max = 0.0001f;
	for (i=currentStep; i>=0; i--) {
		color[3] = ((i+(SPEEDGRAPH_STEPS-currentStep))/(float)SPEEDGRAPH_STEPS)/4.0f; // wtf?
		// white
		if (speed[i] >= SPEEDGRAPH_MAXSPEED1) {
			color[0] = 1.0f;
			color[1] = 1.0f;
			color[2] = 1.0f;
		// yellow -> white
		} else if (speed[i] >= SPEEDGRAPH_MAXSPEED2) {
			color[0] = 1.0f;
			color[1] = 1.0f;
			color[2] = (speed[i]-SPEEDGRAPH_MAXSPEED2)/(SPEEDGRAPH_MAXSPEED1-SPEEDGRAPH_MAXSPEED2);
		// green -> yellow
		} else if (speed[i] >= SPEEDGRAPH_MAXSPEED3) {
			color[0] = (speed[i]-SPEEDGRAPH_MAXSPEED3)/(SPEEDGRAPH_MAXSPEED2-SPEEDGRAPH_MAXSPEED3);
			color[1] = 1.0f;
			color[2] = 0.0f;
		// red -> green
		} else if (speed[i] >= SPEEDGRAPH_MAXSPEED4) {
			color[0] = 1.0f-(speed[i]-SPEEDGRAPH_MAXSPEED4)/(SPEEDGRAPH_MAXSPEED3-SPEEDGRAPH_MAXSPEED4);
			color[1] = (speed[i]-SPEEDGRAPH_MAXSPEED4)/(SPEEDGRAPH_MAXSPEED3-SPEEDGRAPH_MAXSPEED4);
			color[2] = 0.0f;
		// black -> red
		} else if (speed[i] > 0) {
			color[0] = (speed[i]-SPEEDGRAPH_MAXSPEED4)/(SPEEDGRAPH_MAXSPEED3-SPEEDGRAPH_MAXSPEED4);
			color[1] = 0.0f;
			color[2] = 0.0f;
		// black
		} else {
			color[0] = 0.0f;
			color[1] = 0.0f;
			color[2] = 0.0f;
		}
		blarr = (speed[i]/max);
		CG_FillRect(
			SPEEDGRAPH_XOFFSET + (i+(SPEEDGRAPH_STEPS-currentStep))*((float)SPEEDGRAPH_XSIZE/SPEEDGRAPH_STEPS),
			480 - SPEEDGRAPH_YOFFSET - blarr*SPEEDGRAPH_YSIZE,
			(SPEEDGRAPH_STEPS/SPEEDGRAPH_XSIZE)+1,
			blarr*SPEEDGRAPH_YSIZE,
			color);
	}
	if (currentStep != SPEEDGRAPH_STEPS) {
		for (i=SPEEDGRAPH_STEPS; i>currentStep; i--) {
			color[3] = ((i-currentStep)/(float)SPEEDGRAPH_STEPS)/4.0f; // wtf?
			// white
			// 1000+
			if (speed[i] >= SPEEDGRAPH_MAXSPEED1) {
				color[0] = 1.0f;
				color[1] = 1.0f;
				color[2] = 1.0f;
			// yellow -> white
			// 750-1000
			} else if (speed[i] >= SPEEDGRAPH_MAXSPEED2) {
				color[0] = 1.0f;
				color[1] = 1.0f;
				color[2] = (speed[i]-SPEEDGRAPH_MAXSPEED2)/(SPEEDGRAPH_MAXSPEED1-SPEEDGRAPH_MAXSPEED2);
			// green -> yellow
			} else if (speed[i] >= SPEEDGRAPH_MAXSPEED3) {
				color[0] = (speed[i]-SPEEDGRAPH_MAXSPEED3)/(SPEEDGRAPH_MAXSPEED2-SPEEDGRAPH_MAXSPEED3);
				color[1] = 1.0f;
				color[2] = 0.0f;
			// red -> green
			} else if (speed[i] >= SPEEDGRAPH_MAXSPEED4) {
				color[0] = 1.0f-(speed[i]-SPEEDGRAPH_MAXSPEED4)/(SPEEDGRAPH_MAXSPEED3-SPEEDGRAPH_MAXSPEED4);
				color[1] = (speed[i]-SPEEDGRAPH_MAXSPEED4)/(SPEEDGRAPH_MAXSPEED3-SPEEDGRAPH_MAXSPEED4);
				color[2] = 0.0f;
			// black -> red
			} else if (speed[i] > 0) {
				color[0] = (speed[i]-SPEEDGRAPH_MAXSPEED4)/(SPEEDGRAPH_MAXSPEED3-SPEEDGRAPH_MAXSPEED4);
				color[1] = 0.0f;
				color[2] = 0.0f;
			// black
			} else {
				color[0] = 0.0f;
				color[1] = 0.0f;
				color[2] = 0.0f;
			}
			blarr = (speed[i]/max);
			CG_FillRect(
				SPEEDGRAPH_XOFFSET + (i-currentStep)*((float)SPEEDGRAPH_XSIZE/SPEEDGRAPH_STEPS),
				480 - SPEEDGRAPH_YOFFSET - blarr*SPEEDGRAPH_YSIZE,
				(SPEEDGRAPH_STEPS/SPEEDGRAPH_XSIZE)+1,
				blarr*SPEEDGRAPH_YSIZE,
				color);
		}
	}
	if (cg.xyspeed > SPEEDGRAPH_THRESHOLD)
		if (currentStep >= SPEEDGRAPH_STEPS)
			currentStep = 0;
		else
			currentStep++;
}

This one looks bad unless you change CG_FilledBar() to use gradients if an endcolor is supplied :wink:


#define CG_SPEEDOMETER__PEAK_FADE_TIME		250
void CG2005_DrawSpeedOMeter ( void ) {
	float frac,peakFrac;
	vec4_t color	=	{	0.f,	0.f,	0.f,	0.0f	};
	vec4_t color2	=	{	0.f,	0.f,	0.f,	0.0f	};
	vec4_t bgcolor	=	{	0.f,	0.f,	0.f,	0.4f	};
	int flags = BAR_BG|BAR_BGSPACING_X0Y0;
	char*				str;
	float				w, width=0.3f, height=0.3f;
	float				width2=0.2f, height2=0.2f;
	float				speed;
	static float		peak=0.0f;
	static int			peakTime=0;
// Crash? :/
//	speed = VectorLength(cg.snap->ps.velocity);
	speed = cg.xyspeed;
	if (peakTime > CG_SPEEDOMETER__PEAK_FADE_TIME) {
		peak -= 16.0f;
		if (peak < 0.0f)
			peak = 0.0f;
	} else
		peakTime++;
	if (speed>peak) {
		peak = speed;
		peakTime=0;
	}
	// red -> yellow
	if (speed < 1000.f) {
		frac = (speed / 1000.f);
		peakFrac = (peak / 1000.f);
		color[0] = 1.0f-frac;
		color[1] = frac;
		color[2] = 0.0f;
	// yellow -> green
	} else if (speed < 3000.f) {
		frac = (speed / 3000.f);
		peakFrac = (peak / 3000.f);
		color[0] = 1.0f-frac;
		color[1] = 1.0f;
		color[2] = frac;
	// green -> white
	} else {
		frac = (speed / 10000.f);
		peakFrac = (peak / 10000.f);
		color[0] = frac;
		color[1] = 1.0f;
		color[2] = frac;
	}
	if (frac > 1.0f) frac = 1.0f; else if (frac < 0.0f) frac = 0.0f;
	if (peakFrac > 1.0f) peakFrac = 1.0f; else if (peakFrac < 0.0f) peakFrac = 0.0f;
	color2[0] = color[0]/4;
	color2[1] = color[1]/4;
	color2[2] = color[2]/4;
	color[3] = 0.8f*(1.0f-peakTime/(float)CG_SPEEDOMETER__PEAK_FADE_TIME);
	color2[3] = 0.15f*(1.0f-peakTime/(float)CG_SPEEDOMETER__PEAK_FADE_TIME);
	CG_FilledBar(128, 480 - 24, 640 - 256, 10, color, color2, bgcolor, peakFrac, flags);
	CG_FilledBar(128, 480 - 14, 640 - 256, 14, color2, color, bgcolor, peakFrac, flags);
	color[3] = 0.950f;
	color2[3] = 0.2f;
	CG_FilledBar(128, 480 - 24, 640 - 256, 10, color, color2, bgcolor, frac, flags);
	CG_FilledBar(128, 480 - 14, 640 - 256, 14, color2, color, bgcolor, frac, flags);
	str = va("%.3f", speed);
	w = CG_Text_Width_Ext( str, width, 0, &cgs.media.limboFont1 );
	CG_Text_Paint_Ext(640-126-w, 480-2, width, height, color,  str, 0, 0, ITEM_TEXTSTYLE_SHADOWED, &cgs.media.limboFont1);
	if (peakTime < CG_SPEEDOMETER__PEAK_FADE_TIME) {
		str = va("%.3f", peak);
		color[3] = 1.0f-peakTime/(float)CG_SPEEDOMETER__PEAK_FADE_TIME;
		w = CG_Text_Width_Ext( str, width2, 0, &cgs.media.limboFont1 );
		CG_Text_Paint_Ext(640-126-w, 480-16, width2, height2, color,  str, 0, 0, ITEM_TEXTSTYLE_SHADOWED, &cgs.media.limboFont1);
	}
}


(pgh) #2

Any chance you could attempt to quick fix the too high bug (just set a limit can u not?) and compile it to a client side? Wouldnt mind using this for a few things :o


(sniser) #3

Too high speed is not the problem, just “too high horizontal resolution” of the graph… compiling… yeah, but my net connection is a bit fux0red (corrupt downloads, having to reload webpages), so I’ll have to retry a few times -> gimme some time. If anybody else wants to use/compile this feel free by the way, all rights reversed :smiley:


(sniser) #4

Here you go:
http://snism2.net/et2005/stuff/SPEEDOMETAR001__cgame_mp_x86.rar

cg_drawspeedometer 1
cg_drawspeedograph 1


(Ramoonus) #5

nice compiling
how to enable it, readme?


(sniser) #6

Hhrm… too lazy, ask pgh :smiley:


(pgh) #7

Ill answer tonite :slight_smile: Work atm, cant test… Im guessing thisll work over demos too? :o Was thinking of using it my movie for some of the trickjump bits to show some things… :]


(bacon) #8

All you really need to do is create an if statement so that if the horizontal resolution is higher than a value you want to stop at you make it take the max value instead.

EG. If you want it to stop at 1000, you do something like this (haven’t looked at your code so you have to replace this with whatever):

 if (SPEEDSTEPS > 1000)
     SPEEDSTEPS = 1000;

Or something along the lines of that.


(Fusen) #9

wtf is a speed-o-meter anyway?

durrrr nevermind awnsered my own question its just at first didnt see what the graph was about but just figured it out so nevermind… cant really see a use for it tho


Amc Pacer


(sniser) #10

But that’s not the problem, it doesn’t crash on too high speeds - you only get problems if you set SPEEDSTEPS too high before compiling (it’s a #define, not a variable). Which isn’t exactly a problem either, since the current value works fine and looks good :smiley:


(HerrK) #11

Really nice work…it’s like the Lagometer for OSP! :banana:

(NdK) :wink:


(Bludd) #12

Hrm, I assumed I was to rename this to cgame_mp_x86.dll and replace the existing one, but the cvars still do nothing…?


(sniser) #13

You need to start the game with “+sv_pure 0”, too (e.g. make a copy of the ET.exe shortcut, right-click->properties, add it there).

cant really see a use for it tho

It’s nice e.g. for practising strafejumping - or for modders playing around with speed/acceleration (it’s sometimes hard to judge the effect some changes have by just comparing how it “feels”).


(LION) #14

im sorry for disturbing but i tried that code above and i get these errors:

Compiling…
cg_draw.c
C:\etmoding\sf\src\cgame\cg_draw.c(4682) : warning C4101: ‘str’ : unreferenced local variable
C:\etmoding\sf\src\cgame\cg_draw.c(4681) : warning C4101: ‘fade’ : unreferenced local variable
C:\etmoding\sf\src\cgame\cg_draw.c(4683) : warning C4101: ‘j’ : unreferenced local variable
C:\etmoding\sf\src\cgame\cg_draw.c(4793) : error C2065: ‘BAR_BG’ : undeclared identifier
C:\etmoding\sf\src\cgame\cg_draw.c(4793) : error C2065: ‘BAR_BGSPACING_X0Y0’ : undeclared identifier
C:\etmoding\sf\src\cgame\cg_draw.c(4803) : error C2065: ‘CG_SPEEDOMETER__PEAK_FADE_TIME’ : undeclared identifier
Error executing cl.exe.

cg_draw.obj - 3 error(s), 3 warning(s)

btw putted it right in the end of cg_draw.c !!


(Fusen) #15

umm seeing as you brought this back to life, doesnt etpro now have it?


(bacon) #16

I’m sorry but if you don’t know how to fix those errors you shouldn’t be coding a mod in the first place. Go learn C/C++ then try again.