Showing posts with label libgdx. Show all posts
Showing posts with label libgdx. Show all posts

Thursday, August 30, 2018

GWT application on Facebook

Leave a Comment

How can I publish my GWT application on Facebook?

EDITED: Facebook requires from the application to have some FacebookSDK configuration in <body> tag on index.html page.

The GWT-generated index.html doesn't contain the <body> tag at all, (it has <bodies> instead o_O ).

How do I do that?

I tried to add these lines to the <bodies> tag and upload app to facebook, but when I click "next" on App QuickStart page I get "Something went wrong. We're working on getting it fixed as soon as we can." Facebook popup.

The app is generated by libgdx and works perfectly in browser on my own server. Is there better way to publish GWT app?

There are some docs and question here and there, but it looks like they are related to old approach. Thanks!

0 Answers

Read More

Wednesday, March 14, 2018

Load Libgdx game to a Fragment shows a black screen on android

Leave a Comment

The following code shows how I load a Libgdx game (MyGdxGame) to a LinearLayout. on game create I set up a camera and load a small wav sound file. Texture is loaded after view is loaded.

The issue is during fragment transition to this fragment, there is a noticeable black flash and delay. Is it possible to reduce this some how and improve the fragment transition.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {     View view = null;     Bundle bundle = getArguments();     if (bundle != null) {         this.input = bundle.getParcelable(Utils.FRAGMENT_INPUT_KEY);     } else {         input = new FragmentInput();     }      view = inflater.inflate(R.layout.solve, container, false);     surface = (LinearLayout) view.findViewById(R.id.puzzleView);      activity = (MainActivity) getActivity();      com.badlogic.gdx.graphics.Color color = new com.badlogic.gdx.graphics.Color(Color.red(input.backColor) / 255f,             Color.green(input.backColor) / 255f,             Color.blue(input.backColor) / 255f, 1.0f);     size = input.pieceCount;     cols = input.columns;     GameData gdata = new GameData();     gdata.rows = size / cols;     gdata.columns = cols;     gdata.backColor = color;     gdata.setCategory(input.category);     if (input.rotated) {         gameView = new MyGdxGameRotate(this, gdata);     } else {         gameView = new MyGdxGame(this, gdata);     }      if (surface != null) {         AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();         View game = initializeForView(gameView, config);         surface.addView(game);     }     return view; } 

2 Answers

Answers 1

onCreateView is called before the fragment is shown, doing heavy work here means you delay showing that view (the 'black flash'), instead you should start by moving the heavy work into onResume or at least onViewCreated

Secondly, place the load on a background thread so the UI thread can continue to process the display of the fragment. The simplest way to do this in Android is to use an AsyncTask which will allow you to easily notify the UI when everything is loaded

public void onViewCreated() {     new AsyncTask<Void, Void, Void>() {         public Void doInBackground() {             //Do your loading         }          public void onPostExecute(Void aVoid) {             //Update display, play sound (on resumed fragment)         }     }.execute(); } 

If you are new to Android, be careful about leaking context with this approach

Answers 2

I am not familiar with Libgdx so my observations are not specific to Libgdx. If the view initialization takes time, and assuming that it must be done on the UI thread, maybe you can use a Framelayout and overlay a startup graphics in an ImageView that is placed on top of the current puzzleView, with its initial visibility set to visible in the xml. In the onCreateView, inflate the view as you are doing, and then only find a reference to the puzzleView, but don't do any initialization. You can then check if the view is initialized when the onResume method is called (create a new boolean field for that) and if not, then initialize it and then set the visibility of the top layer to invisible to reveal the puzzleView. The delay will still be there, but the screen will be covered by the top layer graphics until the onResume() returns, by which time (presumably) the initialization is complete. If the heavy part of the initialization can be done on a background thread, then maybe you fire off a background thread that does the the work, when the onCreateView is called, and change the visibility of the top layer and finishing part of the initialization (which almost certainly needs to be done on the UI thread) to a Runnable which should get started via a runOnUiThread() method from the background thread.

Read More

Wednesday, November 22, 2017

How to use CocoaPods with LibGDX?

Leave a Comment

I have a LibGDX project which I can run on all platforms that are supported by LibGDX. Recently I started integrating the Google Nearby Messages API. I got it working on Android through platform-specific code, pretty much like the description on Google says it should be done. Now I want to integrate it in the iOS project, which uses RoboVM, but according to their documentation (https://developers.google.com/nearby/messages/ios/get-started) I should use CocoaPods, which doesn't seem so straightforward for integration, especially in a LibGDX project. I hope someone with some experience in doing this can help me out, or point me to a project in which that's already done. Thanks!

0 Answers

Read More

Tuesday, May 30, 2017

Deploying libgdx to html

Leave a Comment

I tried deploying Libgdx to html using gradle. I copied the content

html/build/dist

and all I see in the browser is the badlogic image with a red background (what you would see if you just created a project) Why is that?

Using the superdev I can open it in the browser, i see where it says drag this button but can't play it. there's nothing

The code server is ready at http://127.0.0.1:9876/ GET /clean/html    Cleaning disk caches.       Cleaned in 29ms. GET /superdev.html    [WARN] ignored get request: /superdev.html    [WARN] not handled: /superdev.html > Building 91% > :html:superDev^C%    

enter image description here

turning the dev mod on I see Can't find any GWT Modules on this page.

If I build it normally, I see some warnings about depreciated methods, it builds successfully.

enter image description here

Assets aren't being loaded by html

1 Answers

Answers 1

I've few links that may be help you :

How to start Super Dev mode in GWT

Also check this thread https://stackoverflow.com/a/24265470/3445320

EDIT

I've tested on Android Studio with MacOS

Steps :

Run ./gradlew html:clean to clean your html module

  • On Android Studio Terminal I run ./gradlew html:superDev command
  • I got The code server is ready at http://127.0.0.1:9876/ on terminal
  • Then I open Google Chrome, View -> Always Show Bookmarks Bar
  • I typed http://127.0.0.1:9876/ in adress field -> enter
  • I got a page having Dev Mode On and Dev Mode Off, two buttons
  • Drag Dev Mode On to bookmarks bar, that is below my Address bar
  • Done! now I search http://localhost:8080/html/ , I got my game in browser
  • Press Dev Mode On button that is in bookmarks bar, I got option for compile

Now I need to deploy my html project

I run ./gradlew html:dist in Android Studio Terminal

I got BUILD SUCCESSFUL in Terminal then I find dist folder inside my html module.

dist folder is inside html module -> build -> dist

I copied dist folder and deployed to my server.

EDIT 2

Make sure badlogic.jpg is in your assets folder of android module

Check entry in assets.txt file and find badlogic.jpg inside html module->war->assets folder. If file or entry not exist.

Somehow program not able to write in assets folder

  • Check what programs have the file/s open
  • Check permissions and administrative rights
  • Delete the cache files, run "gradlew html:clean html:superDev" for every new run of superDev.
  • Check any anti-virus software, they can flag some of GWT's behaviour as suspicious
Read More

Thursday, July 28, 2016

Memory usage in LibGdx

Leave a Comment

I am in the middle of a huge application in a team of developers and the memory is something to consider early on. When I run the program as it is, it takes about 44 MB of memory (Found from the Task manager). I then create 10,000 bodies. The memory usage is now about 83 MB. I have a method to destroy the bodies when I click space, this is how it looks.

public static void disposeAllBodies(){     Array<Body> bodies = new Array<Body>();     world.getBodies(bodies);     int destroyCount = 0;     System.out.println("Attempting to destroy " + world.getBodyCount()+ " bodies");     for(Body b : bodies){         world.destroyBody(b);         destroyCount++;     }      System.out.println("Successfully destroyed " + destroyCount + " body(s), " + world.getBodyCount() + " remain");   } 

It disposes all the bodies with no problem, and these were the only things in the application. After they are disposed, the memory goes down to about 66MB for a few seconds then jumps up to 78 MB and stays there.

So I am wondering is there a better way to dispose these bodies? This application will be creating millions of bodies but most will be destroyed, however if the memory just keeps going up, it won't be able to handle this much disposing since the memory stays pretty much static.

Also, the CPU goes from 0.2% (before any bodies) to 23% (when 10,000 bodies present) then dwon to 2.3% (when disposed bodies). So even the CPU is doing more work after the disposing of bodies.

Thanks for any help!

Update: The code for creating the bodies is as follows:

BodyDef bodyDef = new BodyDef();     bodyDef.type = type;     bodyDef.position.set(new Vector2(position.x, position.y));      Body body = world.createBody(bodyDef);      FixtureDef fixtureDef = new FixtureDef();     Fixture fixture;     if(isCircle){         CircleShape circle = new CircleShape();         circle.setRadius(dimensions.x);         fixtureDef.shape = circle;         fixture = body.createFixture(fixtureDef);         circle.dispose();     }else{         PolygonShape rectangle = new PolygonShape();         rectangle.setAsBox(dimensions.x, dimensions.y);         fixtureDef.shape = rectangle;         fixture = body.createFixture(fixtureDef);         rectangle.dispose();     } 

These are all just Box2D bodies, no sprites attached or anything. Thanks!

1 Answers

Answers 1

Did you try a stripped down version of the "box2d only" code you posted to see if it still has the same problem? The reason I ask is that you also posted another question on "changing FixtureDef properties" at Changing FixtureDef properties Java Libgdx and you've given a lot more of your overall code. (The code from this question is a subset of the code from the other question). Looking at that code, there could be some issues.

In the other question you put bodies, bodyDefs, fixtures and fixtureDefs into a HashMap, you don't show how you retrieve/clear the map. That might/might not cause memory leaks. I'd say likely not, but you never know.

But I did see this bit, which I'm pretty sure will cause issues:

public void attachNewSprite(String internalPath){     entitySprite = new Sprite(new Texture(Gdx.files.internal(internalPath)));     ((Body)bodyObjects.get(BodyReferences.BODY)).setUserData(entitySprite); } 

In this question you said you used no sprites, but if you do the above somewhere in your code, each new Texture() will take up memory. You have to explicitly dispose each texture you create. You shouldn't really be creating a new Texture each time you create a new Sprite. Ideally, you create the texture once, then use the Sprite, which is a TextureRegion, to map to the texture. Then dispose the texture when you're all done (at the end of the level/game/etc). In order to dispose the texture, you'll have to keep a reference to it.

Edit/Update:

I had some time this morning so I took your posted code and added a bit to create a barebones simple app with your body creation and body deletion code. I setup a Timer to fire every X seconds just to see what happens when you create/destroy 10k bodies and the code you posted seemed fine. So I think your issue may be elsewhere with code you haven't posted. The memory on my machine would fluctuate a bit (you never really know when the GC will kick in, but it never really topped above 45 MB).

Unless you see something different below than what you're doing (or if you have more code to post, etc), I don't see an issue with what you've shared so far.

import java.util.concurrent.ThreadLocalRandom;  import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; import com.badlogic.gdx.physics.box2d.CircleShape; import com.badlogic.gdx.physics.box2d.Fixture; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.PolygonShape; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Timer; import com.badlogic.gdx.utils.Timer.Task;  public class Memory implements ApplicationListener {      private static World world;         private static void createNewBodies(boolean isCircle, Vector2 position, Vector2 dimensions) {             BodyDef bodyDef = new BodyDef();             //bodyDef.type = type; //all bodies here are dynamic             bodyDef.type =  BodyType.DynamicBody;             bodyDef.position.set(position);              Body body = world.createBody(bodyDef);              FixtureDef fixtureDef = new FixtureDef();             Fixture fixture;             if(isCircle){                 CircleShape circle = new CircleShape();                 circle.setRadius(dimensions.x);                 fixtureDef.shape = circle;                 fixture = body.createFixture(fixtureDef);                 circle.dispose();             }else{                 PolygonShape rectangle = new PolygonShape();                 rectangle.setAsBox(dimensions.x, dimensions.y);                 fixtureDef.shape = rectangle;                 fixture = body.createFixture(fixtureDef);                 rectangle.dispose();             }        }         public static void disposeAllBodies(){             Array<Body> bodies = new Array<Body>();             world.getBodies(bodies);             int destroyCount = 0;             System.out.println("Attempting to destroy " + world.getBodyCount()+ " bodies");             for(Body b : bodies){                 world.destroyBody(b);                 destroyCount++;             }              System.out.println("Successfully destroyed " + destroyCount + " body(s), " + world.getBodyCount() + " remain");          }         private static void buildAllBodies() {            int minPos = 10;            int maxPos = 400;            int minWidthHeight = 50;             Vector2 position = new Vector2();            Vector2 dimensions = new Vector2();             for (int i=0; i<10000; i=i+2) {                position.x = ThreadLocalRandom.current().nextInt(minPos, maxPos+1);                position.y = ThreadLocalRandom.current().nextInt(minPos*2, maxPos*2+1);                dimensions.x = ThreadLocalRandom.current().nextInt(minWidthHeight, minWidthHeight+1);                dimensions.y = dimensions.x;                createNewBodies(true, position, dimensions);                createNewBodies(false, position, dimensions);            }        }         @Override        public void create() {             world = new World ( new Vector2(0.0f, -9.8f), true);             Timer.schedule(new Task() {                    @Override                    public void run() {                        buildAllBodies();                        disposeAllBodies();                    }               }                , 1.0f                , 10.0f //how often to do the cycle (in seconds)               );        }         @Override        public void render() { }         @Override        public void dispose() {            world.dispose();        }         @Override        public void resize(int width, int height) { }         @Override        public void pause() { }         @Override        public void resume() { } } 
Read More

Thursday, March 31, 2016

How to identify a user on iOS?

Leave a Comment

I am using LibGDX and for the Android version of my game I use the "Get Accounts" permission to identify a user by their Gmail address.

Is their a similar way to identify a user for iOS?

3 Answers

Answers 1

According to the App Store Guidelines, you shouldn't get user's personal data without obtaining the user's prior permission. The only identifier you can use anonymously is identifierForVendor:

UIDevice.currentDevice().identifierForVendor?.UUIDString 

This identifier is common for all your apps on the user's device. If all the apps were deleted from the device, the identifier may change. More.

Answers 2

Your best bet is to use GameCenter on iOS for identifying the players this link provides a little more info on handling users with GameCenter: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html

Answers 3

According to Apple, you shouldn't identify your users, unless you have a good reason to do so.

Like @Marat said, what you are looking for is the UUID, but keep in mind that this value may change (for example, if the user delete your app and have none of your other apps).

A possible solution would be to keep the UUID value in the keychain, and that way you will always use the same value.

Edit: Since your app is a game, you can use Apple's GameCenter. This will identify the users for you.

https://developer.apple.com/game-center/

Read More