0

I'm a Computer Science teacher, I'm trying to make Libgdx work inside an online development environment (replit.com). I can access and edit the pom.xml, I successfully added the Libgdx dependency:

<dependency>
  <groupId>com.badlogicgames.gdx</groupId>
  <artifactId>gdx</artifactId>
  <version>1.11.0</version>
</dependency>

and I copied and pasted the demo code to run a simple raindrops game.

Here's the Main.java:

    import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
    import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;

public class Main {
   public static void main (String[] arg) {
     
      Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
      config.setTitle("Drop");
      config.setWindowedMode(800, 480);
      config.useVsync(true);
      config.setForegroundFPS(60);
      new Lwjgl3Application(new Drop(), config);
   }
}

Drop.java:

import java.util.Iterator;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;

public class Drop extends ApplicationAdapter {
   private Texture dropImage;
   private Texture bucketImage;
   private Sound dropSound;
   private Music rainMusic;
   private SpriteBatch batch;
   private OrthographicCamera camera;
   private Rectangle bucket;
   private Array<Rectangle> raindrops;
   private long lastDropTime;

   @Override
   public void create() {
      // load the images for the droplet and the bucket, 64x64 pixels each
      dropImage = new Texture(Gdx.files.internal("droplet.png"));
      bucketImage = new Texture(Gdx.files.internal("bucket.png"));

      // load the drop sound effect and the rain background "music"
      dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
      rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));

      // start the playback of the background music immediately
      rainMusic.setLooping(true);
      rainMusic.play();

      // create the camera and the SpriteBatch
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();

      // create a Rectangle to logically represent the bucket
      bucket = new Rectangle();
      bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
      bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
      bucket.width = 64;
      bucket.height = 64;

      // create the raindrops array and spawn the first raindrop
      raindrops = new Array<Rectangle>();
      spawnRaindrop();
   }

   private void spawnRaindrop() {
      Rectangle raindrop = new Rectangle();
      raindrop.x = MathUtils.random(0, 800-64);
      raindrop.y = 480;
      raindrop.width = 64;
      raindrop.height = 64;
      raindrops.add(raindrop);
      lastDropTime = TimeUtils.nanoTime();
   }

   @Override
   public void render() {
      // clear the screen with a dark blue color. The
      // arguments to clear are the red, green
      // blue and alpha component in the range [0,1]
      // of the color to be used to clear the screen.
      ScreenUtils.clear(0, 0, 0.2f, 1);

      // tell the camera to update its matrices.
      camera.update();

      // tell the SpriteBatch to render in the
      // coordinate system specified by the camera.
      batch.setProjectionMatrix(camera.combined);

      // begin a new batch and draw the bucket and
      // all drops
      batch.begin();
      batch.draw(bucketImage, bucket.x, bucket.y);
      for(Rectangle raindrop: raindrops) {
         batch.draw(dropImage, raindrop.x, raindrop.y);
      }
      batch.end();

      // process user input
      if(Gdx.input.isTouched()) {
         Vector3 touchPos = new Vector3();
         touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         camera.unproject(touchPos);
         bucket.x = touchPos.x - 64 / 2;
      }
      if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
      if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();

      // make sure the bucket stays within the screen bounds
      if(bucket.x < 0) bucket.x = 0;
      if(bucket.x > 800 - 64) bucket.x = 800 - 64;

      // check if we need to create a new raindrop
      if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();

      // move the raindrops, remove any that are beneath the bottom edge of
      // the screen or that hit the bucket. In the latter case we play back
      // a sound effect as well.
      for (Iterator<Rectangle> iter = raindrops.iterator(); iter.hasNext(); ) {
         Rectangle raindrop = iter.next();
         raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
         if(raindrop.y + 64 < 0) iter.remove();
         if(raindrop.overlaps(bucket)) {
            dropSound.play();
            iter.remove();
         }
      }
   }

   @Override
   public void dispose() {
      // dispose of all the native resources
      dropImage.dispose();
      bucketImage.dispose();
      dropSound.dispose();
      rainMusic.dispose();
      batch.dispose();
   }
}

It does compile but when it runs it gives me the following message:

[LWJGL] GLFW_PLATFORM_UNAVAILABLE error
    Description : Failed to detect any supported platform
    Stacktrace  :
        org.lwjgl.glfw.GLFW.glfwInit(GLFW.java:1047)
        com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.initializeGlfw(Lwjgl3Application.java:88)
        com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:138)
        Main.main(Main.java:12)
Exception in thread "main" com.badlogic.gdx.utils.GdxRuntimeException: Unable to initialize GLFW
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.initializeGlfw(Lwjgl3Application.java:89)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:138)
    at Main.main(Main.java:12)
exit status 1

Any idea?

Michele Minno
  • 273
  • 1
  • 3
  • 14
  • 2
    I highly doubt you can run libgdx applications via replit. – Federico klez Culloca Jun 19 '23 at 16:01
  • @FedericoklezCulloca do you know about some java framework to develop simple games I can run via replit? – Michele Minno Jun 19 '23 at 16:03
  • Nope, sorry. I'd suggest you develop on a actual machine. – Federico klez Culloca Jun 19 '23 at 16:21
  • @FedericoklezCulloca the problem is that my students code using chromebooks.. – Michele Minno Jun 19 '23 at 16:26
  • Have you tried developing software on a Chromebook? What actual problems do you encounter? – markspace Jun 19 '23 at 16:30
  • @markspace the problem is that a Chromebook is basically a big Android phone, so it's harder to install whatever you may want to install to an ordinary OS. An other more specific issue is that students in school doesn't have their own Chromebook, but they take the first available each time. So I prefer to keep everything online (e.g. via replit), so that they only need a browser to code. – Michele Minno Jun 19 '23 at 16:33
  • Your launcher looks like its setup to launch a desktop application, but possibly your target should be a web application if its running in the browser. Go here https://libgdx.com/community/ and ask on discord. Its unlikely that an expected string is missing from the replit environment (but I think its more desktop launcher is wrong). LibGDX has the concept of a single -core-, but there are separate platform specific launchers (and indeed builds). What host replit is I don't know but the web application is java to GWT, not pure java. – londonBadger Jun 20 '23 at 09:16
  • If you can run a basic android app in replit then I would try running libGDX using the android launcher,-not- the desktop one. – londonBadger Jun 20 '23 at 09:17
  • @londonBadger thank you, I'm trying to do what you suggested. – Michele Minno Jun 20 '23 at 09:50
  • Hey have you heard about processing? it's a java framework with which you could develop simple games :D and it even has a template for replit :DDD – Luis Fernando Frontanilla Jun 23 '23 at 14:44
  • @LuisFernandoFrontanilla yes someone else in the comments mentioned it, but from what I can see it’s not good to teach students object oriented programming – Michele Minno Jun 23 '23 at 19:05

0 Answers0