2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman, pierre@codercorner.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
17 #include "OpcodeArraySAPTest.h"
18 #include "RenderingHelpers.h"
19 #include "GLFontRenderer.h"
21 static udword gNbCreatedPairs;
22 static udword gNbDeletedPairs;
23 static udword gTotalNbPairs;
25 static void* CBData = (void*)0x12345678;
26 static void* PairUserData = (void*)0xDeadDead;
28 static void* CreatePairCB(const void* object0, const void* object1, void* user_data)
30 assert(user_data==CBData);
36 static void DeletePairCB(const void* object0, const void* object1, void* user_data, void* pair_user_data)
38 assert(user_data==CBData);
39 assert(pair_user_data==PairUserData);
44 OpcodeArraySAPTest::OpcodeArraySAPTest(int numBoxes) :
54 OpcodeArraySAPTest::~OpcodeArraySAPTest()
59 void OpcodeArraySAPTest::Init()
64 mBoxes = new AABB[mNbBoxes];
65 mBoxTime = new float[mNbBoxes];
66 mHandles = new void*[mNbBoxes];
67 for(udword i=0;i<mNbBoxes;i++)
69 Point Center, Extents;
71 Center.x = (UnitRandomFloat()-0.5f) * 100.0f;
72 Center.y = (UnitRandomFloat()-0.5f) * 10.0f;
73 Center.z = (UnitRandomFloat()-0.5f) * 100.0f;
74 Extents.x = 2.0f + UnitRandomFloat() * 2.0f;
75 Extents.y = 2.0f + UnitRandomFloat() * 2.0f;
76 Extents.z = 2.0f + UnitRandomFloat() * 2.0f;
78 mBoxes[i].SetCenterExtents(Center, Extents);
80 mBoxTime[i] = 2000.0f*UnitRandomFloat();
83 UpdateBoxes(mNbBoxes);
85 for(udword i=0;i<mNbBoxes;i++)
87 // It is mandatory to pass a valid pointer as a first parameter. This is supposed to be the game object
88 // associated with the AABB. In this small example I just pass a pointer to the SAP itself.
89 mHandles[i] = (void*)mASAP.AddObject(&mASAP, i, mBoxes[i]);
92 udword MyNbInitialPairs = mASAP.DumpPairs(CreatePairCB, DeletePairCB, CBData);
93 gTotalNbPairs = MyNbInitialPairs;
96 void OpcodeArraySAPTest::Release()
98 DELETEARRAY(mHandles);
99 DELETEARRAY(mBoxTime);
103 extern float objectSpeed;
105 void OpcodeArraySAPTest::Select()
107 // Create a tweak bar
109 mBar = TwNewBar("OpcodeArraySAPTest");
110 TwAddVarRW(mBar, "Speed", TW_TYPE_FLOAT, &objectSpeed, " min=0.0 max=0.01 step=0.00001");
111 TwAddVarRW(mBar, "Amplitude", TW_TYPE_FLOAT, &mAmplitude, " min=10.0 max=200.0 step=0.1");
115 void OpcodeArraySAPTest::Deselect()
124 bool OpcodeArraySAPTest::UpdateBoxes(int numBoxes)
126 for(int i=0;i<numBoxes;i++)
128 mBoxTime[i] += objectSpeed;
130 Point Center,Extents;
131 mBoxes[i].GetExtents(Extents);
133 Center.x = cosf(mBoxTime[i]*2.17f)*mAmplitude + sinf(mBoxTime[i])*mAmplitude*0.5f;
134 Center.y = cosf(mBoxTime[i]*1.38f)*mAmplitude + sinf(mBoxTime[i]*mAmplitude);
135 Center.z = sinf(mBoxTime[i]*0.777f)*mAmplitude;
137 mBoxes[i].SetCenterExtents(Center, Extents);
142 extern int percentUpdate;
143 extern bool enableDraw;
145 void OpcodeArraySAPTest::PerformTest()
147 int numBoxes = (mNbBoxes*percentUpdate)/100;
155 UpdateBoxes(numBoxes);
160 for(int i=0;i<numBoxes;i++)
162 mASAP.UpdateObject((udword)mHandles[i], mBoxes[i]);
166 udword NbPairs = mASAP.DumpPairs(CreatePairCB, DeletePairCB, CBData, &Pairs);
168 gTotalNbPairs += gNbCreatedPairs;
169 gTotalNbPairs -= gNbDeletedPairs;
175 // printf("%d pairs colliding\r ", mPairs.GetNbPairs());
177 bool* Flags = (bool*)_alloca(sizeof(bool)*mNbBoxes);
178 ZeroMemory(Flags, sizeof(bool)*mNbBoxes);
179 for(udword i=0;i<NbPairs;i++)
181 Flags[Pairs[i].id0] = true;
182 Flags[Pairs[i].id1] = true;
189 CurrentBox.mRot.Identity();
190 for(udword i=0;i<mNbBoxes;i++)
192 if(Flags[i]) glColor3f(1.0f, 0.0f, 0.0f);
193 else glColor3f(0.0f, 1.0f, 0.0f);
194 mBoxes[i].GetCenter(CurrentBox.mCenter);
195 mBoxes[i].GetExtents(CurrentBox.mExtents);
201 sprintf(Buffer, "OpcodeArraySAPTest: %5.1f us (%d cycles) : %d pairs\n", mProfiler.mMsTime, mProfiler.mCycles, NbPairs);
202 GLFontRenderer::print(10.0f, 10.0f, 0.02f, Buffer);
205 void OpcodeArraySAPTest::KeyboardCallback(unsigned char key, int x, int y)
209 void OpcodeArraySAPTest::MouseCallback(int button, int state, int x, int y)
213 void OpcodeArraySAPTest::MotionCallback(int x, int y)