Exemplo n.º 1
0
static void testStaticRandom() {
	unsigned int uirandResult;
	int irandResult;
	float ufrandResult, frandResult;
	
	sdrand(0);
	uirandResult = uirand();
	TestCase_assert(uirandResult == 2147418111u, "Expected 2147418111 but got %u\n", uirandResult);
	uirandResult = uirand();
	TestCase_assert(uirandResult == 2147352573u, "Expected 2147352573 but got %u\n", uirandResult);
	
	sdrand(0);
	irandResult = irand();
	TestCase_assert(irandResult == -65537, "Expected -65537 but got %d\n", irandResult);
	irandResult = irand();
	TestCase_assert(irandResult == -131075, "Expected -131075 but got %d\n", irandResult);
	
	sdrand(0);
	ufrandResult = ufrand(1.0f);
	TestCase_assert(fabs(ufrandResult - 0.999969f) < 0.00001f, "Expected 0.999969 but got %f\n", ufrandResult);
	ufrandResult = ufrand(1.0f);
	TestCase_assert(fabs(ufrandResult - 0.999939f) < 0.00001f, "Expected 0.999939 but got %f\n", ufrandResult);
	
	sdrand(0);
	frandResult = frand(1.0f);
	TestCase_assert(fabs(frandResult - -0.000031f) < 0.00001f, "Expected -0.000031 but got %f\n", frandResult);
	frandResult = frand(1.0f);
	TestCase_assert(fabs(frandResult - -0.000061f) < 0.00001f, "Expected -0.000061 but got %f\n", frandResult);
	
	sdrand(1000);
	uirandResult = uirand();
	TestCase_assert(uirandResult == 2081883111u, "Expected 2081883111 but got %u\n", uirandResult);
	uirandResult = uirand();
	TestCase_assert(uirandResult == 2147353574u, "Expected 2147353574 but got %u\n", uirandResult);
	
	sdrand(1000);
	irandResult = irand();
	TestCase_assert(irandResult == -65600537, "Expected -65600537 but got %d\n", irandResult);
	irandResult = irand();
	TestCase_assert(irandResult == -130074, "Expected -131075 but got %d\n", irandResult);
	
	sdrand(1000);
	ufrandResult = ufrand(2.0f);
	TestCase_assert(fabs(ufrandResult - 1.938905f) < 0.00001f, "Expected 1.938905 but got %f\n", ufrandResult);
	ufrandResult = ufrand(2.0f);
	TestCase_assert(fabs(ufrandResult - 1.999879f) < 0.00001f, "Expected 1.999879 but got %f\n", ufrandResult);
	
	sdrand(1000);
	frandResult = frand(2.0f);
	TestCase_assert(fabs(frandResult - -0.061095f) < 0.00001f, "Expected -0.061095 but got %f\n", frandResult);
	frandResult = frand(2.0f);
	TestCase_assert(fabs(frandResult - -0.000121f) < 0.00001f, "Expected -0.000121 but got %f\n", frandResult);
	
	sdrand(0);
	stirrand(1);
	uirandResult = uirand();
	TestCase_assert(uirandResult == 2147352573u, "Expected 2147352573 but got %u\n", uirandResult);
	
	sdrand(0);
	stirrand(2);
	uirandResult = uirand();
	TestCase_assert(uirandResult == 2147090425u, "Expected 2147090425 but got %u\n", uirandResult);
}
Exemplo n.º 2
0
int main(int argc, char ** argv) {
	if (argc < 2) {
		printUsage();
		return EXIT_FAILURE;
	}
	
	if (!strcmp(argv[1], "-random")) {
		int randSeed = 0;
		unsigned int iterations = 3, stirIterations = 0, iteration;
		float frandRange = 1.0f;
		
		if (argc < 3) {
			printf("Specify random seed as argv[2] if desired; proceeding with %d\n", randSeed);
		} else {
			if (!sscanf(argv[2], "%d", &randSeed)) {
				printf("Couldn't parse \"%s\" as %%d; proceeding with random seed %d\n", argv[2], randSeed);
			}
		}
		
		if (argc < 4) {
			printf("Specify number of iterations as argv[3] if desired; proceeding with %u\n", iterations);
		} else {
			if (!sscanf(argv[3], "%u", &iterations)) {
				printf("Couldn't parse \"%s\" as %%u; proceeding with %u iterations\n", argv[3], iterations);
			}
		}
		
		if (argc < 5) {
			printf("Specify frand/ufrand range as argv[4] if desired; proceeding with %f\n", frandRange);
		} else {
			if (!sscanf(argv[4], "%f", &frandRange)) {
				printf("Couldn't parse \"%s\" as %%f; proceeding with frand/ufrand range %f\n", argv[4], frandRange);
			}
		}
		
		if (argc < 6) {
			printf("Specify stir iterations as argv[5] if desired; proceeding with %u\n", stirIterations);
		} else {
			if (!sscanf(argv[5], "%u", &stirIterations)) {
				printf("Couldn't parse \"%s\" as %%u; proceeding with %u stir iterations\n", argv[5], stirIterations);
			}
		}
		
		printf("sdrand(%d);\n", randSeed);
		sdrand(randSeed);
		if (stirIterations > 0) {
			printf("stirrand(%u)\n", stirIterations);
			stirrand(stirIterations);
		}
		for (iteration = 0; iteration < iterations; iteration++) {
			printf("uirand(): %u\n", uirand());
		}
		printf("\nsdrand(%d);\n", randSeed);
		sdrand(randSeed);
		if (stirIterations > 0) {
			printf("stirrand(%u)\n", stirIterations);
			stirrand(stirIterations);
		}
		for (iteration = 0; iteration < iterations; iteration++) {
			printf("irand(): %d\n", irand());
		}
		printf("\nsdrand(%d);\n", randSeed);
		sdrand(randSeed);
		if (stirIterations > 0) {
			printf("stirrand(%u)\n", stirIterations);
			stirrand(stirIterations);
		}
		for (iteration = 0; iteration < iterations; iteration++) {
			printf("ufrand(%f): %f\n", frandRange, ufrand(frandRange));
		}
		printf("\nsdrand(%d);\n", randSeed);
		sdrand(randSeed);
		if (stirIterations > 0) {
			printf("stirrand(%u)\n", stirIterations);
			stirrand(stirIterations);
		}
		for (iteration = 0; iteration < iterations; iteration++) {
			printf("frand(%f): %f\n", frandRange, frand(frandRange));
		}
		fflush(stdout);
		
	} else if (!strcmp(argv[1], "-tempfile")) {
		char * template = "tmpXXXXXX";
Exemplo n.º 3
0
void Test_1::Input(SDL_Event E) {
		int i,j;
		Float x,y,z;
		palBodyBase *pb= NULL;
		palCompoundBodyBase *pcb = NULL;
		switch(E.type) {
		case SDL_KEYDOWN:
			switch (E.key.keysym.sym) {
			case SDLK_1:
				pb = CreateBody("palBox",sfrand()*3,sfrand()*2+5.0f,sfrand()*3,ufrand()+0.1f,ufrand()+0.1f,ufrand()+0.1f,1);
				if (pb == NULL) {
					printf("Error: Could not create a box\n");
				} 
				break;
			case SDLK_q:
				pb = CreateBody("palStaticBox",sfrand()*3,sfrand()*2+3.0f,sfrand()*3,ufrand()+0.1f,ufrand()+0.1f,ufrand()+0.1f,1);
				if (pb == NULL) {
					printf("Error: Could not create a static box\n");
				} 
				break;
			case SDLK_2:
				palSphere *ps;
				ps = NULL;
				ps=dynamic_cast<palSphere *>(PF->CreateObject("palSphere"));
				if (ps) {
					ps->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,0.5f*ufrand()+0.05f,1);
					BuildGraphics(ps);
				} else {
					printf("Error: Could not create a sphere\n");
				} 
				pb = ps;
				break;
			case SDLK_w:
				pb = CreateBody("palStaticSphere",sfrand()*3,sfrand()*2+3.0f,sfrand()*3,0.5*ufrand()+0.05f,0,0,1);
				if (pb == NULL) {
					printf("Error: Could not create a static sphere\n");
				} 
				break;
			case SDLK_3:
				palCapsule *pc;
				pc = NULL;
				pc=dynamic_cast<palCapsule *>(PF->CreateObject("palCapsule"));
				if (pc) {
					float radius=0.5f*ufrand()+0.05f;
					pc->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,radius,radius+ufrand()+0.1f,1);
					BuildGraphics(pc);
				} else {
					printf("Error: Could not create a cylinder\n");
				} 
				pb = pc;
				break;
			case SDLK_4:
				{
				int dist;
				dist = 3;
				float mult;
				mult = 1.0f;
				for (j=-dist;j<dist;j++)
					for (i=-dist;i<dist;i++) {
						pb = CreateBody("palBox",i*mult,5.0f,j*mult,0.25,0.25,0.25,1.0f);
					}
				pb = 0;
				}
				break;
			case SDLK_r:
				{
				int dist;
				dist = 3;
				float mult;
				mult = 1.0f;
				for (j=-dist;j<dist;j++)
					for (i=-dist;i<dist;i++) {
						pb = CreateBody("palStaticBox",i*mult,1.0f,j*mult,0.25,0.25,0.25,1.0f);
					}
				pb = 0;
				}
				break;
			case SDLK_5:
				x = sfrand()*3;
				y = sfrand()*2+3.0f;
				z = sfrand()*3;
				pcb = dynamic_cast<palCompoundBodyBase *>(PF->CreateObject("palCompoundBody"));
				if (!pcb)
					return;
				dynamic_cast<palCompoundBody *>(pcb)->Init(x,y,z);
			case SDLK_t:
				if (!pcb) {
					x = sfrand()*3;
					y = sfrand()*2+3.0f;
					z = sfrand()*3;
					pcb = dynamic_cast<palCompoundBodyBase *>(PF->CreateObject("palStaticCompoundBody"));
					if (!pcb)
						return;
					dynamic_cast<palStaticCompoundBody *>(pcb)->Init(x,y,z);
				}
				if (pcb) {
					palBoxGeometry *pbg;
					pbg = pcb->AddBox();
					if (pbg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,1+x,y,z);
						pbg->Init(m,1,1,1,1);
					}
					pbg = pcb->AddBox();
					if (pbg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,-1+x,y,z);
						pbg->Init(m,1,1,1,1);
					}
					pcb->Finalize();
					BuildGraphics(pcb);
				} else {
					printf("Error: Could not create a compound body\n");
				}
				pb = pcb;
				break;
			case SDLK_6:
				palConvex *pcv;
				pcv = NULL;
				pcv=dynamic_cast<palConvex *>(PF->CreateObject("palConvex"));
				if (pcv) {
					Float pVerts[(36+36+1)*3];
					int nVerts = (36+36+1);
					MakeConvexCone(pVerts);
					pcv->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,pVerts,nVerts,1);
				//	float radius=0.5f*ufrand()+0.05f;
				//	pc->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,radius,radius+ufrand()+0.1f,1);
					BuildGraphics(pcv);
				} else {
					printf("Error: Could not create a convex object\n");
				} 
				pb = pcv;
				break;
			case SDLK_y:
				{
				palStaticConvex *pcv;
				pcv = NULL;
				pcv=dynamic_cast<palStaticConvex *>(PF->CreateObject("palStaticConvex"));
				if (pcv) {
					Float pVerts[(36+36+1)*3];
					int nVerts = (36+36+1);
					MakeConvexCone(pVerts);
					pcv->Init(sfrand()*3,sfrand()*2+3.0f,sfrand()*3,pVerts,nVerts);
				//	float radius=0.5f*ufrand()+0.05f;
				//	pc->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,radius,radius+ufrand()+0.1f,1);
					BuildGraphics(pcv);
				} else {
					printf("Error: Could not create a static convex object\n");
				} 
				pb = pcv;
				}
				break;
			case SDLK_7:
//				palCompoundBody *pcb;
				pcb = NULL;
				pcb = dynamic_cast<palCompoundBody *>(PF->CreateObject("palCompoundBody"));
				if (pcb) {
					
					Float x = sfrand()*3;
					Float y = sfrand()*2+5.0f;
					Float z = sfrand()*3;
					
					dynamic_cast<palCompoundBody *>(pcb)->Init(x,y,z);
					

					Float pVerts[(36+36+1)*3];
					int nVerts = (36+36+1);
					MakeConvexCone(pVerts);
				
					palConvexGeometry *pcg = 0;
					pcg = pcb->AddConvex();
					if (pcg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,1+x,y,z);
						pcg->Init(m,pVerts,nVerts,1);
					}
					pcg = pcb->AddConvex();
					if (pcg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,-1+x,y,z);
						pcg->Init(m,pVerts,nVerts,1);
					}

					pcb->Finalize();
					BuildGraphics(pcb);
				} else {
					printf("Error: Could not create a convex object\n");
				} 
				pb = pcb;
				break;	
			case SDLK_8:
				{
					if (bodies.size()>0) {
						int r= rand() % bodies.size();
						palBody *body = dynamic_cast<palBody*>(bodies[r]);
							if (body) {
						
						body->SetPosition(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,ufrand()*M_PI,ufrand()*M_PI,ufrand()*M_PI);
						body->SetActive(true);
							}
					}
				}
				break;
			case SDLK_9:
				if (bodies.size()>0) {
				DeleteGraphics(bodies[0]);
				delete bodies[0];
				
				bodies.erase(bodies.begin());
				}
				break;
			} 
			if (pb) {
				bodies.push_back(pb);
			}
			break;
		}
	}