LWJGL + Slick - Can't display picture, using example program.

Started by qwertz, December 20, 2011, 22:37:17

Previous topic - Next topic

qwertz

Ok, hello!

So, I've got a small problem, and I can't find out why it's happening, especially that the example you've got on your page ran OK previously, but now when I try to run it, all I see is a black screen, nothing more.

Here's how I initialize LWJGL:
private Controller() {

        initGraphics();

        try {
            Keyboard.create();
            Mouse.setGrabbed(false);
            Mouse.create();
        } catch (LWJGLException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

        mainMenu = new MainMenu();
        controllerState = ControllerState.MENU;
    }

    private void initGraphics() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
            Display.setFullscreen(false);
            Display.setTitle("Memory Game");
            Display.setVSyncEnabled(true);
        } catch (LWJGLException ex) {
            ex.printStackTrace();
            System.exit(0);
        }

        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glViewport(0, 0, WIDTH, HEIGHT);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    }


Here's how I render it:

Controller.java:
Quotevoid process {

        for (;;) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            switch (controllerState) {
                case MENU:
                    mainMenu.process();
                    break;
                case OPTIONS:
                    break;
                case GAME:
                    break;
                case HISCORE:
                    break;
                case EXIT:
                    cleanup();

                default:
                    break;
            }


            Display.update();

            if (Display.isCloseRequested()) {
                controllerState = ControllerState.EXIT;
            }

        }
    }

MainMenu.java:
public class MainMenu {

    DisplayableObject gameButton;
    DisplayableObject optionsButton;
    DisplayableObject hiscoreButton;
    DisplayableObject exitButton;
    
    
DisplayableObject backgroundImage;
    
    public MainMenu(){
        backgroundImage = new DisplayableObject(0,0,"background.png");
        gameButton = new DisplayableObject(100,100,"gameButton.png");
        hiscoreButton = new DisplayableObject(100,250,"hiscoreButton.png");
        exitButton = new DisplayableObject(100,400,"exitButton.png");
    }
    
    void process() {
        System.out.println("PROCESS");
        backgroundImage.render();
        //gameButton.render();
        //hiscoreButton.render();
        //exitButton.render();
    }
    
}


DisplayableObject.java:
public class DisplayableObject {

    int x;
    int y;

    Texture texture;    
    public DisplayableObject(int tX, int tY, String imageName){
        x = tX;
        y = tY;

        try {
            texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/" + imageName));
        } catch (IOException ex) {
            Logger.getLogger(DisplayableObject.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(imageName);
            System.exit(0);
        }
        System.out.println(imageName + " X:" + texture.getTextureWidth() + " Y: " + texture.getTextureHeight());
    }

    public void render() {
        
        Color.white.bind();
        texture.bind();
        
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(x, y);
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex2f(x + texture.getWidth(), y);
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex2f(x + texture.getWidth(), y + texture.getHeight());
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex2f(x, y + texture.getHeight());
        GL11.glEnd();
        
    }
}


And all I see is a black box.

What's funnier, though, is that the code used to run OK, but suddenly after I've done pretty much nothing connected to these methods, it stopped working :/.
So, what might be the problem here?

Marltoro

Here's something you could try:
Replace:
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/" + imageName));

With:
texture = TextureLoader.getTexture("PNG", new FileInputStream("C:/image.png"));

Place a file called image.png in your C:/ drive and see if that works.

Edit: Also make sure your image sizes are powers of 2. (4,8,16,32,64,128,256,512,ect..)

CodeBunny

First off: does the Console say anything? Also, are you calling Display.sync() anywhere? Also, where is your main method?

However, if I had to guess, it sounds like your controllerState value gets set to something other than ControllerState.MENU. I don't see how that could happen from the given code, though. I'd recommend putting a lot more print statements in your code, to see the path of execution.

BTW - you shouldn't need to mess about with a FileInputStream, getResourceAsStream should work fine. If it is something in the standard library messing up, you should see error messages in the console. Also, since I assume your program doesn't immediately exit (it's setup to close if an exception occurs while loading images), you can temporarily assume your images are getting loaded.

Additionally (this is more of a stylistic point): Why are you using for() for your main loop? Have a boolean value that determines whether or not you should exit, and have a while loop be controlled by that value. Example:

    void process
    {
         boolean exit = false;
         while (!exit)
         {
              GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
              switch (controllerState)
             {
                 case MENU:
                     mainMenu.process();
                     break;
                 case OPTIONS:
                     break;
                 case GAME:
                     break;
                 case HISCORE:
                     break;
                 default:
                     break;
            }
            Display.update();

            if (Display.isCloseRequested())
                exit = true;
        }
        cleanup();
    }


It's cleaner and more straightforward.