Hello Guest

help with lwjgl and slick

  • 14 Replies
  • 19330 Views
help with lwjgl and slick
« on: November 25, 2011, 21:04:08 »
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
Code: [Select]
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
Code: [Select]
/*
 * 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
Code: [Select]
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
Code: [Select]
/*
 * 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();
    }
}

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #1 on: November 25, 2011, 21:12:54 »
where is startup.png on your computer? for whatever reason its not finding it in your project folder.

Re: help with lwjgl and slick
« Reply #2 on: November 26, 2011, 03:54:46 »
its in the folder with the java files.

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #3 on: November 26, 2011, 11:22:29 »
can you paste the full path of the file here?

Re: help with lwjgl and slick
« Reply #4 on: November 26, 2011, 15:04:53 »
F:\Users\Revix2k10\Documents\NetBeansProjects\CrazyChickens\src\crazychickens\startup.png

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #5 on: November 26, 2011, 15:09:17 »
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
« Last Edit: November 26, 2011, 15:11:55 by kappa »

Re: help with lwjgl and slick
« Reply #6 on: November 26, 2011, 15:14:47 »
should i make a folder to separate gfx ? im new to java obviously lol.

new error lol guess i didnt do anything right
Code: [Select]
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)
« Last Edit: November 26, 2011, 15:16:33 by revix2k9 »

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #7 on: November 26, 2011, 15:25:06 »
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.

new error lol guess i didnt do anything right
Code: [Select]
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.
« Last Edit: November 26, 2011, 15:27:38 by kappa »

Re: help with lwjgl and slick
« Reply #8 on: November 26, 2011, 15:32:53 »
startupscreens
Code: [Select]
/*
 * 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
Code: [Select]
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.

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #9 on: November 26, 2011, 15:51:02 »
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.

Re: help with lwjgl and slick
« Reply #10 on: November 26, 2011, 16:00:48 »
So texcords basicly points the image where to go ?

i added
Code: [Select]
GL11.glTexCoord2f(0,0);
100.GL11.glVertex2f(0,0);
to the middle black screen still.
« Last Edit: November 26, 2011, 16:02:36 by revix2k9 »

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #11 on: November 26, 2011, 16:04:07 »
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.
« Last Edit: November 26, 2011, 16:07:32 by kappa »

Re: help with lwjgl and slick
« Reply #12 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?

*

Offline kappa

  • *****
  • 1319
Re: help with lwjgl and slick
« Reply #13 on: November 26, 2011, 16:15:43 »
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.

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.
« Last Edit: November 26, 2011, 16:18:23 by kappa »

Re: help with lwjgl and slick
« Reply #14 on: November 26, 2011, 16:28:22 »
yeah thanks for all your help ill go all over slick2d.