Tutorial 2: Drawing a moving Triangle,a FPS counter and Keyboard Mapping.

hi there! in this tutorial i'll explain how to render triangles through a OpenGL-like way, displaying a FPS counter onscreen and a new way of handling the keyboard (thanks floh!).

We'll keep all the code from Tut1(except Main()) and just add some more lines. The winmain() function remains unchanged since it is in Init() Main() & End() that action takes place.

Let's start with Init()


Keyboard mapping works by assigning custom words or indexes to keys so that when you press, for ex, the left arrow key it fires the decreasex state. Then we'll query InputServer if it has been fired or not.

#include <mathlib/matrix.h> // include matrix lib to handle matrix stuff

void Init()
{
	KernelServer = new nKernelServer;
	GfxServer = (nGfxServer *) ks->New("nglserver","/sys/servers/gfx");
	InputServer = (nInputServer *) ks->New("ninputserver","/sys/servers/input");
	
	// these remain unchanged
	KernelServer->ts->EnableFrameTime();

	//enable the frametime counter
	//the Kernel server class has a member
	//of nTimeServer class that handles time ,
	//profiling etc called 'ts' (bad name)

	InputServer->BeginMap();
	InputServer->Map("keyb0:left.pressed", "decreasex");
	InputServer->Map("keyb0:right.pressed", "increasex");
	InputServer->Map("keyb0:up.pressed", "increasey");
	InputServer->Map("keyb0:down.pressed", "decreasey");
	InputServer->Map("keyb0:esc.up", "end");
	InputServer->EndMap();

	GfxServer->SetDisplayMode("-type(win)-w(640)-h(480)");
	GfxServer->SetClearColor(0.7f,0.7f,0.7f,1.0f);
	GfxServer->SetViewVolume(-100, 100, -100, 100, 0, 100);

	// NEW. We have to set the view volume so that polys dont get culled
	GfxServer->OpenDisplay();
}

Now on to Main() which has changed substantially:

Here we draw the triangle... you should have already noticed that it is just like OGL... you call Begin() with the primitive type you want to render , give the vertx info one by one and call end for it to draw. Simple.

void Main()
{
	static float rangle = 0.0f, // rotation angle for the triangle
	xpos=0.0f, // xpos for the tri
	ypos=0.0f, // ypos for the tri
	LastFrameTime=0.0f; // var to track time

	InputServer->Trigger();

	if(!GfxServer->Trigger()) 
		EndProgram();

	KernelServer->ts->Trigger();

	// Do Internal work, quit if needed, update times , etc;

	if (InputServer->GetButton("end")) 
	{
		EndProgram();
		return;
	}
	if (InputServer->GetButton("decreasex")) xpos-=0.01f;
	if (InputServer->GetButton("increasex")) xpos+=0.01f;
	if (InputServer->GetButton("decreasey")) ypos-=0.01f;
	if (InputServer->GetButton("increasey")) ypos+=0.01f;

	// if flags were fired, execute
	// correspondant action

	GfxServer->BeginScene();

	// tell the GfxServer and correspondant API(GL or DX)
	// that we are going to draw

	nRState RenderState;
	RenderState.Set(N_RS_CULLMODE,N_CULL_NONE);

	GfxServer->SetState(RenderState);
	// Init a renderstate block and disable Culling so that
	// when the poligon turns it doesnt get culled

	static matrix44 Transform;
	TransformMatrix.ident();
	
	// create a matrix to handle the
	// transformation operations and set it to
	// the identity
	
	rangle+=0.01f;
	// increase polygon rotation angle

	Transform.rotate_y(rangle);
	Transform.rotate_x(rangle*0.5f);
	Transform.rotate_z(rangle*0.75f);
	Transform.translate(vector3(xpos,ypos,0));
	// create a matrix which will rotate the points then
	// translate 'em by xpos and ypos
	
	GfxServer->SetMatrix(N_MXM_MODELVIEW,Transform);
	// set the ModelView matrix to our matrix

	GfxServer->Begin(N_PTYPE_TRIANGLE_LIST);
	GfxServer->Rgba(1.0f,0.0f,0.0f,1.0f);
	GfxServer->Coord(0.25, 0.0f, -0.25f);
	GfxServer->Rgba(0.0f,1.0f,0.0f,1.0f);
	GfxServer->Coord(0.25, 0.0f, +0.25f);
	GfxServer->Rgba(0.0f,0.0f,1.0f,1.0f);
	GfxServer->Coord(-0.25f, 0.0f, 0.25);
	GfxServer->End();

	char str[80];
	double fps = 1/(KernelServer->ts->GetFrameTime()-LastFrameTime);
	sprintf(str,"FPS:%f",fps);
	// calculate the fps through GetFrameTime() and LastFrameTime
	// and print it to str

	GfxServer->BeginText(); // Tell him that we're gonna draw text
	GfxServer->TextPos(-1,-1); // Set the Text output position
	GfxServer->Text(str); // send the string and draw it
	GfxServer->EndText(); // end the draw text block
	GfxServer->EndScene(); // End drawing (update screen,clear,etc)

	LastFrameTime = KernelServer->ts->GetFrameTime(); 
	// set last frame time

	// NOTE: GetFrameTime() here will return the same as it has returned when
	// we have calculated the fps cause it returns a var which is only updated
	// in InputServer->Trigger()
}

End() remains the same since we haven't added any new interface.

Try it. Press the cursor keys and it'll move !! :) See how fast it works in your computer with the fps counter... ;)

bye!

Hosted by www.Geocities.ws

1