Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Mr Bullet: no crash in crowded levels

Mason363f67a1388be9c
diff --git a/games/mrbullet/src/box2d/Box2D/Collision/b2BroadPhase.cpp b/games/mrbullet/src/box2d/Box2D/Collision/b2BroadPhase.cpp
index 6cddf719..e95c32ed 100644
--- a/games/mrbullet/src/box2d/Box2D/Collision/b2BroadPhase.cpp
+++ b/games/mrbullet/src/box2d/Box2D/Collision/b2BroadPhase.cpp
@@ -1,3 +1,4 @@
+/* Altered for NumPlay's Mr Bullet (NumWorks calculator): at most 1024 pairs gathered a step */
/*
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
*
@@ -104,11 +105,20 @@ bool b2BroadPhase::QueryCallback(int32 proxyId)
// Grow the pair buffer as needed.
if (m_pairCount == m_pairCapacity)
{
- b2Pair* oldBuffer = m_pairBuffer;
+ // (NumPlay: at most 1024 pairs a step, 8 KB; the pairs left wait for the proxy's next move)
+ if (m_pairCapacity >= 1024)
+ {
+ return false;
+ }
+ b2Pair* newBuffer = (b2Pair*)b2Alloc(m_pairCapacity * 2 * sizeof(b2Pair));
+ if (newBuffer == NULL)
+ {
+ return false;
+ }
+ memcpy(newBuffer, m_pairBuffer, m_pairCount * sizeof(b2Pair));
+ b2Free(m_pairBuffer);
+ m_pairBuffer = newBuffer;
m_pairCapacity *= 2;
- m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
- memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
- b2Free(oldBuffer);
}
m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
diff --git a/games/mrbullet/src/box2d/Box2D/Common/b2Settings.cpp b/games/mrbullet/src/box2d/Box2D/Common/b2Settings.cpp
index 5ce1f78f..c14c1810 100644
--- a/games/mrbullet/src/box2d/Box2D/Common/b2Settings.cpp
+++ b/games/mrbullet/src/box2d/Box2D/Common/b2Settings.cpp
@@ -28,12 +28,18 @@ b2Version b2_version = {2, 3, 1};
/* Box2D's memory in use and the most it has used (a word before each block keeps its size), and the limit past
which no new contact is made (b2ContactManager::AddPair): a pair that would touch waits until there is room */
extern "C" { long b2_heap_now, b2_heap_peak, b2_heap_limit; }
+#ifdef HOST
+long b2_alloc_max;
+#endif
void* b2Alloc(int32 size)
{
long *p = (long *)malloc(size + sizeof(long) * 2);
if (!p) return NULL;
p[0] = size;
b2_heap_now += size + sizeof(long) * 2;
+#ifdef HOST
+ { extern long b2_alloc_max; if (size > b2_alloc_max) b2_alloc_max = size; }
+#endif
if (b2_heap_now > b2_heap_peak) b2_heap_peak = b2_heap_now;
return p + 2;
}
diff --git a/games/mrbullet/tests/play.c b/games/mrbullet/tests/play.c
index 737373e8..b3751b9e 100644
--- a/games/mrbullet/tests/play.c
+++ b/games/mrbullet/tests/play.c
@@ -263,9 +263,10 @@ int main(int argc, char **argv) {
}
if (argc >= 3 && !strcmp(argv[1], "seq")) {
/* levels in a row, shot as the calculator's measuring build shoots them: Box2D's memory after each */
- b2_heap_limit = 81000;
+ b2_heap_limit = getenv("LIMIT") ? atol(getenv("LIMIT")) : 81000;
for (int k = 2; k < argc; k++) {
b2_heap_peak = b2_heap_now;
+ max_contacts = 0;
game_load(atoi(argv[k]) - 1, (uint8_t *)level_mem);
for (int s = 0; s < 480; s++) {
if (s % 90 == 0) {
@@ -274,10 +275,11 @@ int main(int argc, char **argv) {
game_fire();
}
game_step();
+ if (world_contacts() > max_contacts) max_contacts = world_contacts();
}
long peak = b2_heap_peak;
game_end();
- printf("level %s: Box2D's memory at most %ld, %ld left after\n", argv[k], peak, b2_heap_now);
+ { extern long b2_alloc_max; printf("level %s: Box2D's memory at most %ld, %ld left after, biggest block %ld, contacts %d\n", argv[k], peak, b2_heap_now, b2_alloc_max, max_contacts); b2_alloc_max = 0; }
}
return 0;
}