help with lwjgl and slick

Started by revix2k9, November 25, 2011, 21:04:08

Previous topic - Next topic

revix2k9

So i started looking around wiki , i am trying to make a little 2d game. I am using lwjgl and slick.

my debugger gives me this error
run:
Exception in thread "main" java.lang.RuntimeException: Resource not found: startup.png
	at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
	at crazychickens.StartUpScreen.<init>(StartUpScreen.java:27)
	at crazychickens.CrazyChickens.<init>(CrazyChickens.java:14)
	at crazychickens.Game.main(Game.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
the filename is right. idk whats up.

I have 3 classes.

Game
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crazychickens;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

/**
 *
 * @author Revix2k10
 */


public class Game {
    //declarations



    



    
public static void main(String[] args) {
         CrazyChickens gameObject = new CrazyChickens();
         gameObject.start();
         
    }
}


CrazyChickens
package crazychickens;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Revix2k10
 */
public class CrazyChickens  {
public int screenWidth,screenHeight;
 StartUpScreen smenu = new StartUpScreen();
    public CrazyChickens(){
        screenWidth = 800;
        screenHeight = 600;
    
    
}
public void start(){
        try{
            Display.setDisplayMode(new DisplayMode(screenWidth,screenHeight));
            Display.create(); 
            Display.setVSyncEnabled(true);
            
        }catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }             
        while (!Display.isCloseRequested()){
            Display.update();
            smenu.Render();
        }
        Display.destroy();
    }
    
    
    
    
}
    
   


and start up screen
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crazychickens;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import java.io.IOException;

/**
 *
 * @author Revix2k10
 */

public class StartUpScreen {
    private Texture startupScreen;
    
    public StartUpScreen(){
       try{
           startupScreen = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("startup.png"));
       } catch (IOException e) {
           e.printStackTrace();
       }
        
    }
    
    public void Render(){
        Color.white.bind();
        startupScreen.bind();
    }
}


kappa

where is startup.png on your computer? for whatever reason its not finding it in your project folder.

revix2k9

its in the folder with the java files.

kappa

can you paste the full path of the file here?

revix2k9

F:\Users\Revix2k10\Documents\NetBeansProjects\CrazyChickens\src\crazychickens\startup.png

kappa

Ok that seems to be the problem, the resource files should be seperate from your source code. They should be in the base of the project directory (not inside a package in your src directory), so the startup.png should be in the following location:

F:\Users\Revix2k10\Documents\NetBeansProjects\CrazyChickens\startup.png

revix2k9

should i make a folder to separate gfx ? im new to java obviously lol.

new error lol guess i didnt do anything right
Exception in thread "main" java.lang.RuntimeException: Image based resources must be loaded as part of init() or the game loop. They cannot be loaded before initialisation.
	at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:228)
	at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:184)
	at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
	at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
	at crazychickens.StartUpScreen.<init>(StartUpScreen.java:27)
	at crazychickens.CrazyChickens.<init>(CrazyChickens.java:14)
	at crazychickens.Game.main(Game.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

kappa

Quote from: revix2k9 on November 26, 2011, 15:14:47
should i make a folder to separate gfx ? im new to java obviously lol.
Yeh, some people generally have a folder called "res" in the project folder where they keep their resources. But it doesn't really matter, you can keep them wherever you like.

Quote from: revix2k9 on November 26, 2011, 15:14:47
new error lol guess i didnt do anything right
Exception in thread "main" java.lang.RuntimeException: Image based resources must be loaded as part of init() or the game loop. They cannot be loaded before initialisation.
	at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:228)
	at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:184)
	at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
	at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
	at crazychickens.StartUpScreen.<init>(StartUpScreen.java:27)
	at crazychickens.CrazyChickens.<init>(CrazyChickens.java:14)
	at crazychickens.Game.main(Game.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

You're trying to load the texture before you've created a LWJGL Display (hence the opengl context), your texture loading code should be called sometime after you've created the Display (Display.create()) and somewhere before the game loop.

revix2k9

startupscreens
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crazychickens;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import java.io.IOException;

/**
 *
 * @author Revix2k10
 */

public class StartUpScreen {
    private Texture startupScreen;
    
    public StartUpScreen(){
      
        
    }
    
    public void Init(){
         try{
           startupScreen = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("startup.png"));
       } catch (IOException e) {
           e.printStackTrace();
       }
    }
    
    public void Render(){
        Color.white.bind();
        startupScreen.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glEnd();
    }
}


CrazyChickens
package crazychickens;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Revix2k10
 */
public class CrazyChickens  {
public int screenWidth,screenHeight;
 StartUpScreen smenu = new StartUpScreen();
    public CrazyChickens(){
        screenWidth = 800;
        screenHeight = 600;
    
    
}
public void start(){
        try{
            Display.setDisplayMode(new DisplayMode(screenWidth,screenHeight));
            Display.create(); 
            Display.setVSyncEnabled(true);
            smenu.Init();
        }catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }             
        while (!Display.isCloseRequested()){
            smenu.Render();
            Display.update();
            Display.sync(100);
            
        }
        Display.destroy();
    }
    
    
    
    
}
    
   
i added stuff game runs no errors , but picture is not there.

kappa

That is because you've got no glVertex or glTexCoords between your GL11.glBegin(GL11.GL_QUADS); and        GL11.glEnd(); calls.

Have another careful look at the following Slick-Util tutorial.

revix2k9

So texcords basicly points the image where to go ?

i added
GL11.glTexCoord2f(0,0);
100.GL11.glVertex2f(0,0);
to the middle black screen still.

kappa

I'd recommend you read an opengl book or guide on this, there is a nice list of resources  here.

To answer your question, you must do two thing to draw an image, firstly you must draw the quad, you specify what the quad look like using the glVertex calls (which will be the corners of the quad). You must also specify how the texture maps onto this quad by specifying its texture coordinates for each vertex using the glTexCoords calls.

Basically you need to go learn OpenGL, if however you don't want to do this, try a slightly higher level LWJGL library like Slick2D.

revix2k9


kappa

Just keep in mind Slick-Util is not the same as Slick2D. Slick2D is a full blown 2d games library with its own API so you don't need to use any OpenGL directly, it has its own wiki and tutorials here. While Slick-Util is nothing more than a helper library for raw OpenGL to make the task of loading textures, sounds and fonts slightly easier.

Quote from: revix2k9 on November 26, 2011, 16:12:25
i have slick 2d i was following this tutorial http://lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_1_-_Loading_Images_for_LWJGL

so i got draw 4 points to render the image?
Yeh you have to specify the 4 vextex points and the texture coordinates for the 4 points.

revix2k9

yeah thanks for all your help ill go all over slick2d.