My Tiled map is not being rendered in libgdx - java

I have spent the past couple of days trying to figure out what is wrong with my code. Im trying to render a tile map (.tmx) in libgdx but it does not render and it just shows a red screen with no error. I have re-written the code a few times and I always getting the same result.
my code:
public class Main extends ApplicationAdapter {
SpriteBatch batch;
TiledMap map;
TmxMapLoader loader;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
#Override
public void create () {
batch = new SpriteBatch();
loader = new TmxMapLoader();
map = loader.load("TiledMaps/TestMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
renderer.setView(camera);
renderer.render();
batch.begin();
batch.end();
}
#Override
public void dispose () {
batch.dispose();
renderer.dispose();
map.dispose();
}
}
Can anybody find what is happening here I have the tmx file with the tilesheet in the TileMaps folder in the android assets folder.
Any help is appreciated, Thanks in advance.

If I understand correctly, you're attempting something complex (map rendering) but encountering a simple problem (not rendering anything).
Have you got a simple program where you do have something rendering? Built it up so you have a few hard coded tiles rendering? Built it up a bit more to correctly read and display the first tile from the map? Before finally extending it further to read and display all the tiles, once you know the basics are working?
That's how I would tackle this problem. Been programming for years, and just used the same process to get CreateProcess working in C++ on Windows. Wasn't working within my code so I built an empty project that just runs CreateProcess on notepad.exe and tweaked things with answers from StackOverflow.com till I got it working, now it's about adding back in the other complications bit by bit so I'm not completely lost by taking on too much at once.

Related

How to get FPS in JOGL?

I currently trying to output my FPS using JOGL, to no avail. I have tried many things such as
glAutoDrawable.getAnimator().getLastFPS() but it returns 0.0. I have also tried using glAutoDrawable.getAnimator().setUpdateFPSFrames(3, System.out); but it never logs the FPS. I am trying to get the FPS of the ENTIRE application, not a animation.
I have checked Jogl animator always says FPS is 0 but this requires a Animator object which I am not using. When using a new Animator (glAutoDrawable.setAnimator(new Animator());) the FPS still returns 0.0.
Here is the code that I am using:
package ***.core.graphics;
import com.jogamp.opengl.*;
import com.jogamp.opengl.util.Animator;
import ***.log.Log;
import ***.tests.utils.GameTestMethods;
public class GameCanvas implements GLEventListener{
...
#Override
public void display(GLAutoDrawable glAutoDrawable) {
GL2 glDraw = glAutoDrawable.getGL().getGL2();
gameTestMethods.onRender(glDraw); // Draws a rectangle, not the issue.
Log.log(Log.LogLevel.DEBUG, "FPS: "+glAutoDrawable.getAnimator().getLastFPS());//Trying to log FPS, returns 0.0 every time.
}
#Override
public void init(GLAutoDrawable glAutoDrawable) {
glAutoDrawable.setAnimator(new Animator()); // No clue what i'm doing here...
glAutoDrawable.getAnimator().setUpdateFPSFrames(3, System.out); // NEVER logs FPS...
}
...
}
Would this be possible, or would I need my own FPS counter?

libGDX throwing an error for the default vertex shader in a SpriteBatch

I am new to libGDX and am using it to complete a big game for a school project. I have been following an online tutorial for most of the code, but can't seem to run because of a Vertex Shader error.
Error:
Fragment shader:
ERROR: 0:1: '' : #version required and missing.
ERROR: 0:7: 'varying' : syntax error: syntax error
at com.badlogic.gdx.graphics.g2d.SpriteBatch.createDefaultShader(SpriteBatch.java:161)
at com.badlogic.gdx.graphics.g2d.SpriteBatch.(SpriteBatch.java:124)
at com.badlogic.gdx.graphics.g2d.SpriteBatch.(SpriteBatch.java:78)
at com.tootireddevelopmentco.games.Splash.show(Splash.java:41)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.tootireddevelopmentco.games.RabbitRun.create(RabbitRun.java:17)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
I do not have any experiences with shaders, and have not created a shader during my project, or declared a version of any sort. The shader error seems to trace back to the SpriteBatch created in my program. Is there something wrong with my code, or is libGDX and my settings for it the issue.
Some extra code from my classes:
From the splash class - error points to SpriteBatch declaration.
public void show() {
// apply preferences
batch = new SpriteBatch();
tweenManager = new TweenManager();
Tween.registerAccessor (Sprite.class, new SpriteAccessor ());
splash = new Sprite(new Texture ("img/splash.png"));
Tween.set (splash, SpriteAccessor.ALPHA).target(0).start(tweenManager);
Tween.to(splash, SpriteAccessor.ALPHA, 1.5f).target(1).repeatYoyo(1, 2).setCallback(new TweenCallback () {
#Override
public void onEvent(int arg0, BaseTween<?> arg1) {
// TODO Auto-generated method stub
((Game) Gdx.app.getApplicationListener()).setScreen (new MainMenu ());
}
});
}
Thanks,
Julia
This looks like a gles2.0 vs 3.0 issue.
SpriteBatch doesn't support GLES 3 unless you use a GLES 3 compatible shader.
One thing to check is that you do not try and force you libGDX to gles 3.0.
Search your code for useGL30(it is called on your LwjglApplicationConfiguration object like so myLwjglAppConfig.useGL30 = false;), if that is set to true then either remove the line or set it to false.

LibGDX: AssetManager has asset in queue and is updating, but is never loading

FIX: Converting the models to g3db format fixed the issue.
I am using Java 1.8.0_60, Android 6.0
So I've decided to have a go at 3D in LibGdx, and I'm running into some weird issues with the AssetManager.
I've been following tutorials pretty much completely so far, and am trying to load a .obj model of a ship I got from one of the tutorials.
I am creating the AssetManager and adding the .obj file to the queue, with Model.class as the second parameter of the load method.
I am then waiting for the asset to load, calling update() on the AssetManager each render to check whether it has loaded.
Right now, my printlns are pretty much looking like this:
Assets in queue: 1
Progress: 0.0
My render method looks like this (In GameRenderer class):
public void render(){
if(!created){
return;
}
if(assets.loading() && assets.update()){
assets.bindModels();
}
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(assets.getInstances(), environment);
modelBatch.end();
}
And my asset loading methods look like this (in AssetLoader class):
public void loadModels(){
assets.load("ship.obj", Model.class);
loading = true;
}
public void bindModels() {
ModelInstance model = new ModelInstance(assets.get("ship.obj", Model.class));
instances.add(model);
loading = false;
}
public boolean update(){
System.out.println("Assets in queue: " + assets.getQueuedAssets());
return assets.update();
}
There are no exceptions being thrown.
My guess is that something is causing the AssetManager to jam, but I'm lost on what could be causing it. Any help would be very much appreciated.
Thanks!
EDIT:
If I call finishLoading() on the AssetManager, which blocks until the assets are all done, the game freezes.

Processing: Sketch gets stuck when using Capture class

I'm using Processing 2.0.3 on Windows 8. I tried to use the following code but I've no idea why my sketch couldn't run when I'm using the processing.video.* library:
import processing.video.*;
Capture cam;
void setup() {
size(200, 200);
cam = new Capture(this);
cam.start();
}
void draw() {
if (cam.available()) {
// Reads the new frame
cam.read();
}
image(cam, 0, 0);
}
I notice that the sketch will get stuck and will not open the sketch applet window at all if I call anything related to the Capture class. Calling println(Capture.list()); for example will cause the sketch to stuck at where ever that line was called.
What do I have to do to resolve this problem?

Nifty GUI control prevents the rest of my application from rendering

I've been trying to add some basic gui elements to a openGl (via LWJGL) application using nifty gui, and while I've been successful in rendering panels and static text on top of the the applications graphics, using the built-in nifty controls (e.g. an editable text field) causes the rest of the application to not render. The strange part is that I don't even have to render the gui control, merely declaring it appears to cause this problem.
Compiler ready code showing the basic layout of the problem:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.lwjgl.input.LwjglInputSystem;
import de.lessvoid.nifty.renderer.lwjgl.render.LwjglRenderDevice;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.TimeProvider;
public class NiftyBreaksRendering {
private Nifty nifty;
private Screen screen;
public NiftyBreaksRendering() throws Exception{
//init display
Display.setDisplayMode(new DisplayMode(640,480));
Display.create();
//init nifty gui
LwjglInputSystem inputSystem = new LwjglInputSystem();
inputSystem.startup();
nifty = new Nifty(
new LwjglRenderDevice(),
new NullSoundDevice(),
inputSystem,
new TimeProvider());
// load default styles
nifty.loadStyleFile("nifty-default-styles.xml");
// load standard controls
nifty.loadControlFile("nifty-default-controls.xml");
screen = new ScreenBuilder("start") {{
layer(new LayerBuilder("baseLayer") {{
childLayoutHorizontal();
text(new TextBuilder("test"){{
font("aurulent-sans-16.fnt");
color("#f00f");
backgroundColor("#33af");
text("l33t");
}});
//begin: lines that break the rendering
control(new TextFieldBuilder("input","asdf") {{
width("200px");
}});
//end: lines that break the rendering
}});
}}.build(nifty);
nifty.gotoScreen("start");
//init opengl
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,640,480,0,1,-1);
glMatrixMode(GL_MODELVIEW);
while(!Display.isCloseRequested())
{
//render
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2i(400,400); //Upper left
glVertex2i(450,400);//upper right
glVertex2i(450,450);//bottom right
glVertex2i(400,450);//bottom left
glEnd();
glBegin(GL_LINES);
glVertex2i(100,100);
glVertex2i(200,200);
glEnd();
nifty.render(false);
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args) throws Exception {
new NiftyBreaksRendering();
}
}
What would really help to diagnose this kind of problems is a link to a http://sscce.org/
I guess this is related to OpenGL states changed by Nifty OR to textures being loaded by the Nifty controls which might mess up the rest of your application.
If you could provide more or the complete code I'm pretty sure we can find the problem.
Modified answer after complete example code was provided:
Thanks for providing a complete example!
As expected the problem is, that Nifty changes OpenGL state and leaves you with OpenGL basically in undefined state. The solution is to save your OpenGL states before you call Nifty and restore it afterwards. Here is some code that does exactly that. I've added the call to nifty.update() as well so that Nifty actually updates the GUI (and makes keyboard and mouse events work):
// update nifty
nifty.update();
// save your OpenGL state
// (assuming you are in glMatrixMode(GL_MODELVIEW) all the time)
glPushMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
// render Nifty (this will change OpenGL state)
nifty.render(false);
// restore your OpenGL state
glPopAttrib();
glPopMatrix();
With this change to your original code your example works for me now.
NiftyGUI (as a non-shader-based render) will at some point modify the modelview matrix. Do you clean up the damage it does to the modelview matrix, by initializing it with identity? You know, like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluOrtho2D(0f,(float)VIEWPORT_DIMENSIONS[0],0f,(float)VIEWPORT_DIMENSIONS[1]);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(translation[0],translation[1],0);
glRectf(-1,-1,1,1);

Categories

Resources