Skip to content

Commit e12c3d6

Browse files
committed
Version 0.5.6
1 parent cd48f93 commit e12c3d6

File tree

5 files changed

+86
-38
lines changed

5 files changed

+86
-38
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.5.6
2+
3+
* [[#190](https://github.com/bitbrain/braingdx/issues/190)] fix a crash due to box2dlights ray handler not being disposed
4+
15
# Version 0.5.5
26

37
* [[#187](https://github.com/bitbrain/braingdx/issues/187)] do not create frame buffers when no shaders are being used

core/src/main/java/de/bitbrain/braingdx/GameContext2DImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public void dispose() {
209209
renderManager.dispose();
210210
eventManager.clear();
211211
physicsManager.dispose();
212+
lightingManager.dispose();
212213
}
213214

214215
@Override

core/src/main/java/de/bitbrain/braingdx/graphics/lighting/LightingManager.java

+60-30
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import aurelienribon.tweenengine.TweenCallback;
2020
import aurelienribon.tweenengine.TweenEquation;
2121
import box2dLight.*;
22+
import com.badlogic.gdx.Gdx;
2223
import com.badlogic.gdx.graphics.Color;
2324
import com.badlogic.gdx.graphics.OrthographicCamera;
2425
import com.badlogic.gdx.graphics.g2d.Batch;
2526
import com.badlogic.gdx.math.Vector2;
27+
import com.badlogic.gdx.utils.Disposable;
2628
import de.bitbrain.braingdx.tweens.ColorTween;
2729
import de.bitbrain.braingdx.tweens.SharedTweenManager;
2830

@@ -35,7 +37,7 @@
3537
* @author Miguel Gonzalez Sanchez
3638
* @version 1.0.0
3739
*/
38-
public class LightingManager {
40+
public class LightingManager implements Disposable {
3941

4042
private static final int DEFAULT_RAYS = 80;
4143
private final RayHandler handler;
@@ -48,6 +50,8 @@ public class LightingManager {
4850
private Color ambientLightColor = Color.WHITE.cpy();
4951
private int rays;
5052

53+
private boolean disposed = false;
54+
5155
public LightingManager(RayHandler rayHandler, OrthographicCamera camera) {
5256
this(rayHandler, camera, new LightFactory() {
5357

@@ -154,40 +158,52 @@ public ConeLight addConeLight(String id, float x, float y, float distance, float
154158
return light;
155159
}
156160

157-
public boolean removePointLight(String id) {
158-
PointLight light = pointLights.remove(id);
159-
if (light != null) {
160-
light.remove();
161-
return true;
162-
}
163-
return false;
161+
public void removePointLight(final String id) {
162+
Gdx.app.postRunnable(new Runnable() {
163+
@Override
164+
public void run() {
165+
PointLight light = pointLights.remove(id);
166+
if (light != null) {
167+
light.remove();
168+
}
169+
}
170+
});
164171
}
165172

166-
public boolean removeDirectionalLight(String id) {
167-
DirectionalLight light = dirLights.remove(id);
168-
if (light != null) {
169-
light.remove();
170-
return true;
171-
}
172-
return false;
173+
public void removeDirectionalLight(final String id) {
174+
Gdx.app.postRunnable(new Runnable() {
175+
@Override
176+
public void run() {
177+
DirectionalLight light = dirLights.remove(id);
178+
if (light != null) {
179+
light.remove();
180+
}
181+
}
182+
});
173183
}
174184

175-
public boolean removeChainLight(String id) {
176-
ChainLight light = chainLights.remove(id);
177-
if (light != null) {
178-
light.remove();
179-
return true;
180-
}
181-
return false;
185+
public void removeChainLight(final String id) {
186+
Gdx.app.postRunnable(new Runnable() {
187+
@Override
188+
public void run() {
189+
ChainLight light = chainLights.remove(id);
190+
if (light != null) {
191+
light.remove();
192+
}
193+
}
194+
});
182195
}
183196

184-
public boolean removeConeLight(String id) {
185-
ConeLight light = coneLights.remove(id);
186-
if (light != null) {
187-
light.remove();
188-
return true;
189-
}
190-
return false;
197+
public void removeConeLight(final String id) {
198+
Gdx.app.postRunnable(new Runnable() {
199+
@Override
200+
public void run() {
201+
ConeLight light = coneLights.remove(id);
202+
if (light != null) {
203+
light.remove();
204+
}
205+
}
206+
});
191207
}
192208

193209
public void clear() {
@@ -198,11 +214,17 @@ public void clear() {
198214
handler.removeAll();
199215
}
200216

201-
void render(Batch batch, float delta) {
217+
void render() {
218+
if (disposed) {
219+
return;
220+
}
202221
handler.renderOnly();
203222
}
204223

205224
void beforeRender() {
225+
if (disposed) {
226+
return;
227+
}
206228
handler.setAmbientLight(ambientLightColor);
207229
handler.setCombinedMatrix(camera);
208230
handler.update();
@@ -213,6 +235,14 @@ void resize(int width, int height) {
213235
handler.resizeFBO(width, height);
214236
}
215237

238+
@Override
239+
public void dispose() {
240+
if (!disposed) {
241+
handler.dispose();
242+
disposed = true;
243+
}
244+
}
245+
216246
public static interface LightFactory {
217247
PointLight newPointLight(RayHandler handler, int rays, Color color, float distance, float x, float y);
218248

core/src/main/java/de/bitbrain/braingdx/graphics/lighting/LightingManagerRenderLayer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void beforeRender() {
1919

2020
@Override
2121
public void render(Batch batch, float delta) {
22-
manager.render(batch, delta);
22+
manager.render();
2323
}
2424

2525
@Override

core/src/test/java/de/bitbrain/braingdx/graphics/lighting/LightingManagerTest.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import box2dLight.PointLight;
44
import box2dLight.RayHandler;
5+
import com.badlogic.gdx.Application;
6+
import com.badlogic.gdx.Gdx;
57
import com.badlogic.gdx.graphics.Color;
68
import com.badlogic.gdx.graphics.OrthographicCamera;
79
import com.badlogic.gdx.math.Vector2;
@@ -11,13 +13,14 @@
1113
import org.junit.runner.RunWith;
1214
import org.mockito.InjectMocks;
1315
import org.mockito.Mock;
16+
import org.mockito.invocation.InvocationOnMock;
1417
import org.mockito.runners.MockitoJUnitRunner;
18+
import org.mockito.stubbing.Answer;
1519

1620
import static org.junit.Assert.assertFalse;
1721
import static org.junit.Assert.assertTrue;
1822
import static org.mockito.Matchers.*;
19-
import static org.mockito.Mockito.mock;
20-
import static org.mockito.Mockito.when;
23+
import static org.mockito.Mockito.*;
2124

2225
@RunWith(MockitoJUnitRunner.class)
2326
public class LightingManagerTest {
@@ -26,31 +29,41 @@ public class LightingManagerTest {
2629
private RayHandler rayHandler;
2730

2831
@Mock
29-
private OrthographicCamera camera;
32+
private LightFactory lightFactory;
3033

3134
@Mock
32-
private LightFactory lightFactory;
35+
PointLight pointLightMock;
3336

3437
@InjectMocks
3538
private LightingManager lightingManager;
3639

3740
@Before
3841
public void beforeTest() {
39-
PointLight pointLightMock = mock(PointLight.class);
4042
when(lightFactory.newPointLight(any(RayHandler.class), anyInt(), any(Color.class), anyFloat(), anyFloat(), anyFloat())).thenReturn(pointLightMock);
43+
Application mockApp = mock(Application.class);
44+
Gdx.app = mockApp;
45+
doAnswer(new Answer<Void>() {
46+
@Override
47+
public Void answer(InvocationOnMock invocation) {
48+
((Runnable)invocation.getArguments()[0]).run();
49+
return null;
50+
}
51+
}).when(mockApp).postRunnable(any(Runnable.class));
4152
}
4253

4354
@Test
4455
public void testRemoveLight_Point() {
4556
lightingManager.addPointLight("pointlight", new Vector2(), 0f, Color.WHITE);
46-
assertTrue(lightingManager.removePointLight("pointlight"));
57+
lightingManager.removePointLight("pointlight");
58+
verify(pointLightMock, times(1)).remove();
4759
}
4860

4961
@Test
5062
public void testClear() {
5163
lightingManager.addPointLight("", new Vector2(), 0f, Color.WHITE);
5264
lightingManager.clear();
53-
assertFalse(lightingManager.removePointLight("pointlight"));
65+
lightingManager.removePointLight("pointlight");
66+
verify(pointLightMock, never()).remove();
5467
}
5568

5669
}

0 commit comments

Comments
 (0)