Skip to content

Commit

Permalink
Added PlatformUtils.isGameThread method
Browse files Browse the repository at this point in the history
Added missing javadoc to PlatformUtils
Added PlatformUtils UAT
  • Loading branch information
augustozanellato committed Aug 14, 2019
1 parent a5e839d commit e4e2c75
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ project(":" + rootProject.name + "-libgdx-desktop") {
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
compile "de.golfgl.gdxcontrollerutils:gdx-controllers-jamepad:$gdxAdvancedControllersVersion"
compile "org.reflections:reflections:$reflectionsVersion"
implementation 'org.lwjgl.lwjgl:lwjgl:2.9.3'

testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testCompile "junit:junit:$junitVersion"
Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/org/mini2Dx/core/PlatformUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,42 @@ public abstract class PlatformUtils {
*/
public abstract void exit(boolean ignorePlatformRestrictions);

/**
* Platform independent version of {@link System#nanoTime()}
* @see System#nanoTime()
*/
public abstract long nanoTime();

/**
* Platform independent version of {@link System#currentTimeMillis()}
* @see System#currentTimeMillis()
*/
public abstract long currentTimeMillis();

/**
* Gets the total memory that can be used by the game
* @return Number of bytes that can be used by the game
*/
public abstract long getTotalMemory();

/**
* Gets the total memory available for the game
* @return Number of bytes available for the game
*/
public abstract long getAvailableMemory();

/**
* Gets the total memory used by the game
* @return Number of bytes used by the game
*/
public abstract long getUsedMemory();

/**
* Indicates if the current thread is the game thread
* @return if the current thread is the game thread
*/
public abstract boolean isGameThread();

/**
* Internal usage only: marks the beginning of update operations
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public AndroidGameWrapper(Context applicationContext, GameContainer gc, String g
public void initialise(String gameIdentifier) {
Mdx.di = new AndroidDependencyInjection(applicationContext);
Mdx.playerData = new AndroidPlayerData();
Mdx.platformUtils = new AndroidPlatformUtils();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright 2019 See AUTHORS file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.mini2Dx.android;

import org.mini2Dx.libgdx.LibgdxPlatformUtils;

public class AndroidPlatformUtils extends LibgdxPlatformUtils {
@Override
public boolean isGameThread() {
return Thread.currentThread().getName().startsWith("GLThread");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public DesktopGameWrapper(GameContainer gc, String gameIdentifier) {
public void initialise(String gameIdentifier) {
Mdx.di = new DependencyInjection(new DesktopComponentScanner());
Mdx.playerData = new DesktopPlayerData(gameIdentifier);
Mdx.platformUtils = new DesktopPlatformUtils();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright 2019 See AUTHORS file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.mini2Dx.libgdx.desktop;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.mini2Dx.libgdx.LibgdxPlatformUtils;

public class DesktopPlatformUtils extends LibgdxPlatformUtils {
@Override
public boolean isGameThread() {
try {
return Display.isCurrent();
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
}
}
11 changes: 7 additions & 4 deletions libgdx-ios/src/main/java/org/mini2Dx/ios/IOSPlatformUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
******************************************************************************/
package org.mini2Dx.libgdx;

import com.badlogic.gdx.Gdx;
import org.mini2Dx.core.JvmPlatformUtils;
import org.mini2Dx.libgdx.LibgdxPlatformUtils;

public class IOSPlatformUtils extends JvmPlatformUtils {
public class IOSPlatformUtils extends LibgdxPlatformUtils {
@Override
public void exit(boolean ignorePlatformRestrictions) {
if (ignorePlatformRestrictions) {
Gdx.app.exit();
super.exit();
}
}
@Override
public boolean isGameThread() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.badlogic.gdx.Gdx;
import org.mini2Dx.core.JvmPlatformUtils;
import org.mini2Dx.core.PlatformUtils;

public class LibgdxPlatformUtils extends JvmPlatformUtils {
public abstract class LibgdxPlatformUtils extends JvmPlatformUtils {
@Override
public void exit(boolean ignorePlatformRestrictions) {
Gdx.app.exit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public void create() {
Mdx.input = new LibgdxInput();
Mdx.log = new LibgdxLogger();
Mdx.reflect = new JvmReflection();
Mdx.platformUtils = new LibgdxPlatformUtils();

gameContainer.start(Mdx.graphicsContext);
}
Expand Down
6 changes: 6 additions & 0 deletions monogame/MonoGamePlatformUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
******************************************************************************/
using System;
using System.Diagnostics;
using System.Threading;

namespace monogame
{
Expand Down Expand Up @@ -51,5 +52,10 @@ public override long getUsedMemory()
{
return Process.GetCurrentProcess().WorkingSet64;
}

public override bool isGameThread()
{
return Thread.CurrentThread.ManagedThreadId == 1 && Thread.CurrentThread.Name == null;
}
}
}
88 changes: 88 additions & 0 deletions uats/src/main/java/org/mini2Dx/uats/PlatformUtilsUAT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright 2019 See AUTHORS file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.mini2Dx.uats;

import org.mini2Dx.core.Graphics;
import org.mini2Dx.core.Mdx;
import org.mini2Dx.core.PerformanceTracker;
import org.mini2Dx.core.game.GameContainer;
import org.mini2Dx.core.screen.BasicGameScreen;
import org.mini2Dx.core.screen.GameScreen;
import org.mini2Dx.core.screen.ScreenManager;
import org.mini2Dx.core.screen.transition.FadeInTransition;
import org.mini2Dx.core.screen.transition.FadeOutTransition;
import org.mini2Dx.gdx.Input;
import org.mini2Dx.uats.util.ScreenIds;
import org.mini2Dx.uats.util.UATSelectionScreen;

/**
* User acceptance testing of {@link org.mini2Dx.core.PlatformUtils} APIs
*/
public class PlatformUtilsUAT extends BasicGameScreen {

private String gameThreadTestStatus = "in progress";

@Override
public void initialise(GameContainer gc) {
new Thread(new Runnable() {

@Override
public void run() {
if(Mdx.platformUtils.isGameThread()){
PlatformUtilsUAT.this.gameThreadTestStatus = "failed";
} else {
PlatformUtilsUAT.this.gameThreadTestStatus = "in progress...";
}
}
}).start();
}

@Override
public void update(GameContainer gc, ScreenManager<? extends GameScreen> screenManager, float delta) {
if (Mdx.input.justTouched()) {
screenManager.enterGameScreen(UATSelectionScreen.SCREEN_ID, new FadeOutTransition(),
new FadeInTransition());
} else if (Mdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
Mdx.platformUtils.exit(false);
} else if (Mdx.input.isKeyJustPressed(Input.Keys.ENTER)) {
Mdx.platformUtils.exit(true);
}
if (gameThreadTestStatus.contains("...")){
if (Mdx.platformUtils.isGameThread()){
gameThreadTestStatus = "SUCCESS!";
} else {
gameThreadTestStatus = "failed";
}
}
}

@Override
public void render(GameContainer gc, Graphics g) {
int renderX, renderY;
renderX = renderY = 16;
g.drawString("PlatformUtils.isGameThread() test: " + gameThreadTestStatus, renderX, renderY);
renderY += 16;
g.drawString("Press SPACE to test PlatformUtils.exit(false)", renderX, renderY);
renderY += 16;
g.drawString("Press ENTER to test PlatformUtils.exit(true)", renderX, renderY);
PerformanceTracker.drawInBottomLeft(g);
}

@Override
public int getId() {
return ScreenIds.getScreenId(PlatformUtilsUAT.class);
}
}
6 changes: 1 addition & 5 deletions uats/src/main/java/org/mini2Dx/uats/ShapeClippingUAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

import org.mini2Dx.core.Graphics;
import org.mini2Dx.core.Mdx;
import org.mini2Dx.core.files.FileHandleResolver;
import org.mini2Dx.core.game.GameContainer;
import org.mini2Dx.core.graphics.Colors;
import org.mini2Dx.core.graphics.SpriteCache;
import org.mini2Dx.core.graphics.TextureAtlas;
import org.mini2Dx.core.graphics.TextureRegion;
import org.mini2Dx.core.screen.BasicGameScreen;
import org.mini2Dx.core.screen.GameScreen;
import org.mini2Dx.core.screen.ScreenManager;
Expand All @@ -32,7 +28,7 @@
import org.mini2Dx.uats.util.UATSelectionScreen;

/**
* User acceptance testing of {@link SpriteCache} APIs
* User acceptance testing of {@link Graphics} clipping APIs
*/
public class ShapeClippingUAT extends BasicGameScreen {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void initialise() {
addScreen(new GamePadMapping());
addScreen(new FlexUiUAT(assetManager, fallbackFileHandleResolver));
addScreen(new PixelUiUAT(assetManager, fallbackFileHandleResolver));
addScreen(new PlatformUtilsUAT());
addScreen(new ShapeClippingUAT());
addScreen(new TilingDrawableUAT(fallbackFileHandleResolver));
addScreen(new SpriteCacheUAT(fallbackFileHandleResolver));
Expand Down
10 changes: 10 additions & 0 deletions uats/src/main/java/org/mini2Dx/uats/util/UATSelectionScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ public void onActionEnd(ActionEvent event) {
nextScreenId = ScreenIds.getScreenId(PixelUiUAT.class);
}
})));
uatsDialog.add(FlexRow.withElements("row-platform-utils", UiUtils.createButton(uiNavigation, "Platform utils", new ActionListener() {
@Override
public void onActionBegin(ActionEvent event) {
}

@Override
public void onActionEnd(ActionEvent event) {
nextScreenId = ScreenIds.getScreenId(PlatformUtilsUAT.class);
}
})));
uatsDialog.add(FlexRow.withElements("row-shape-clipping", UiUtils.createButton(uiNavigation, "Shape Clipping", new ActionListener() {
@Override
public void onActionBegin(ActionEvent event) {
Expand Down

0 comments on commit e4e2c75

Please sign in to comment.