int Game(void)
{
	DWORD passedTime = 0;
	FsPassedTime(true);

	//////////// initial setting up the scene ////////////////////////////////////////
	int timeSpan = 33; // milliseconds
	double timeInc = (double)timeSpan * 0.001; // time increment in seconds

	FsGetWindowSize(width, height);

	int lb,mb,rb,mx,my;
	glViewport(0, 0, width, height);

	////////////////////// main simulation loop //////////////////////////
	while (1)
	{
		FsPollDevice();
		FsGetMouseState(lb,mb,rb,mx,my);
		int key=FsInkey();
		if(key == FSKEY_ESC)
			break;
		timeInc = (double)(passedTime) * 0.001;
		clocktime += timeInc;
		/////////// update physics /////////////////
		updateNumPhysics(simBall1, timeInc);
		updatePrecisePhysics(realBall, timeInc);
		/////////////////////////////////////////
		renderScene();

		////// update time lapse /////////////////
		passedTime = FsPassedTime(); // Making it up to 50fps
		int timediff = timeSpan-passedTime;
	//	printf("\ntimeInc=%f, passedTime=%d, timediff=%d", timeInc, passedTime, timediff);
		while(timediff >= timeSpan/3)
		{
			FsSleep(5);
			passedTime=FsPassedTime(); // Making it up to 50fps
			timediff = timeSpan-passedTime;
	//		printf("--passedTime=%d, timediff=%d", passedTime, timediff);
		}
		passedTime=FsPassedTime(true); // Making it up to 50fps
	}
	return 0;
}
示例#2
0
void FsOpenWindow(int x0,int y0,int wid,int hei,int useDoubleBuffer)
{
	if(NULL!=fsWin32Internal.hWnd)
	{
		MessageBoxA(fsWin32Internal.hWnd,"Error! Window already exists.","Error!",MB_OK);
		exit(1);
	}

	// Note 2012/03/08 RegisterClassW and CreateWindowW doesn't seem to work.
	WNDCLASSA wc;
	HINSTANCE inst=GetModuleHandleA(NULL);

	wc.style=CS_OWNDC|CS_BYTEALIGNWINDOW;
	wc.lpfnWndProc=(WNDPROC)WindowFunc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hInstance=(HINSTANCE)inst;
	wc.hIcon=LoadIconA(inst,"MAINICON");
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName=NULL;
	wc.lpszClassName=WINCLASS;
	if(0!=RegisterClassA(&wc))
	{
		doubleBuffer=useDoubleBuffer;


		RECT rc;
		rc.left  =x0;
		rc.top   =y0;
		rc.right =(unsigned long)(x0+wid-1);
		rc.bottom=(unsigned long)(y0+hei-1);
		AdjustWindowRect(&rc,WINSTYLE,FALSE);
		wid  =rc.right-rc.left+1;
		hei  =rc.bottom-rc.top+1;

#ifdef _UNICODE
		// What's the point of using CreateWindowA?  Another weird Microsoft logic here.
		static wchar_t buf[256];
		const char *windowNameA=(const char *)WINNAME;
		for(int i=0; i<255 && 0!=windowNameA[i]; ++i)
		{
			buf[i]=windowNameA[i];
			buf[i+1]=0;
		}
		const char *windowNameUsed=(const char *)buf;
#else
		const char *windowNameUsed=(const char *)WINNAME;
#endif

		fsWin32Internal.hWnd=CreateWindowA(WINCLASS,windowNameUsed,WINSTYLE,x0,y0,wid,hei,NULL,NULL,inst,NULL);
		if(NULL!=fsWin32Internal.hWnd)
		{
			InitializeOpenGL(fsWin32Internal.hWnd);

			ShowWindow(fsWin32Internal.hWnd,SW_SHOWNORMAL);
			UpdateWindow(fsWin32Internal.hWnd);

			FsPassedTime();  // Reset Timer
		}
		else
		{
			printf("Could not open window.\n");
			exit(1);
		}
	}
}
int main(void)
{
	double cubeMatrix[16]=
	{
		1.0,0.0,0.0,0.0,
		0.0,1.0,0.0,0.0,
		0.0,0.0,1.0,0.0,
		0.0,0.0,0.0,1.0
	};

	FsOpenWindow(32,32,800,600,1); // 800x600 pixels, useDoubleBuffer=1

	glEnable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glDepthFunc(GL_LESS);

	FsPassedTime();  // Reset the timer

	while(FsInkey()==0)
	{
		int mx,my,lb,mb,rb,passed;
		double spinX,spinY;

		passed=FsPassedTime();

		FsPollDevice();
		FsGetMouseState(lb,mb,rb,mx,my);

		int wid,hei,cx,cy;
		FsGetWindowSize(wid,hei);
		cx=wid/2;
		cy=hei/2;

		spinX=(double)((mx-cx)/10)*(double)passed/1000.0;  // 1 pixel = degrees/sec
		spinY=(double)((my-cy)/10)*(double)passed/1000.0;  // 1 pixel = degrees/sec

		glClearColor(0.0,0.0,0.0,0.0);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		glViewport(0,0,wid,hei);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(45.0,(double)wid/(double)hei,1.0,20.0);
		glTranslated(0.0,0.0,-10.0);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glRotated(spinX,0.0,1.0,0.0);
		glRotated(spinY,1.0,0.0,0.0);
		glMultMatrixd(cubeMatrix);
		glGetDoublev(GL_MODELVIEW_MATRIX,cubeMatrix);

		glBegin(GL_QUADS);

		glColor3ub(0,0,255);
		glVertex3d(-2.5,-2.5,-2.5);
		glVertex3d(-2.5, 2.5,-2.5);
		glVertex3d( 2.5, 2.5,-2.5);
		glVertex3d( 2.5,-2.5,-2.5);

		glColor3ub(255,0,0);
		glVertex3d(-2.5,-2.5, 2.5);
		glVertex3d(-2.5, 2.5, 2.5);
		glVertex3d( 2.5, 2.5, 2.5);
		glVertex3d( 2.5,-2.5, 2.5);

		glColor3ub(255,0,255);
		glVertex3d(-2.5,-2.5,-2.5);
		glVertex3d(-2.5,-2.5, 2.5);
		glVertex3d(-2.5, 2.5, 2.5);
		glVertex3d(-2.5, 2.5,-2.5);

		glColor3ub(0,255,0);
		glVertex3d( 2.5,-2.5,-2.5);
		glVertex3d( 2.5,-2.5, 2.5);
		glVertex3d( 2.5, 2.5, 2.5);
		glVertex3d( 2.5, 2.5,-2.5);

		glColor3ub(0,255,255);
		glVertex3d(-2.5,-2.5,-2.5);
		glVertex3d(-2.5,-2.5, 2.5);
		glVertex3d( 2.5,-2.5, 2.5);
		glVertex3d( 2.5,-2.5,-2.5);

		glColor3ub(255,255,0);
		glVertex3d(-2.5, 2.5,-2.5);
		glVertex3d(-2.5, 2.5, 2.5);
		glVertex3d( 2.5, 2.5, 2.5);
		glVertex3d( 2.5, 2.5,-2.5);

		glEnd();

		FsSwapBuffers();

		FsSleep(20-passed);
	}

	return 0;
}