Textures appear for a frame then disppear

Started by Neber, November 16, 2014, 05:11:23

Previous topic - Next topic

Neber

Hello all

I've only recently got into LWJGL library and have slowly been progressing with making a game however i've run into a problem. I'm using code mostly from the LWJGL Wiki of loading an image, in this case a PNG file. I've been able to display the texture in the past even got it rotating however now all of a sudden once i moved each of the code into separate classes, the textures are failing to appear for more than  a frame where i can see a brief flash of the image before it disappears at startup.

Even if i copy and paste the slick util loading images tutorial code the code only displays for a moment then disappears. If however i either remove the glClear code at each render or merely glClear once, the images appear however they do not rotate with the current code. I'd greatly appreciate if anyone could help with this as i keep hitting little problems with LWJGL and it's taking several hours longer than i'd expect to make any progress with my game.

Thanks in advance

Here's the code:

GraphicsEngine.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg2drpg;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.event.*;
import java.io.IOException;

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.TGAImageData;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

/**
 *
 * @author danielrigby
 */
public class GraphicsEngine {
    //graphics() - constructor set JFrame and JPanel settings to create window
    public GraphicsEngine()
    {
        jpanel = new JPanel();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screen_resolution = toolkit.getScreenSize();
        width = screen_resolution.width;
        height = screen_resolution.height;
        graphics_sprite = new GraphicsSprite();
        /*
        jpanel.setPreferredSize(screen_resolution);
        frame = new JFrame("Neber RPG");
        */
    }
    
    public void start()
    {
        angle = 0;
        try
        {
            //Set up our display
            Display.setTitle("Neber RPG");
            Display.setResizable(true);

            DisplayMode displayMode = null;

            DisplayMode[] modes = Display.getAvailableDisplayModes(); 
            for (int i = 0; i < modes.length; i++)
            {
                /*
                modes[i].getWidth() == 800
                && modes[i].getHeight() == 600
                */
                if (modes[i].isFullscreenCapable())
                  {
                       displayMode = modes[i];
                  }

            }     

            width = displayMode.getWidth();
            height = displayMode.getHeight();
            //Display.setDisplayMode(new DisplayMode(800, 600));
            System.out.println("Width: " + width + " Height: " + height);
            Display.setFullscreen(true);
            Display.setDisplayMode(displayMode);
            Display.setVSyncEnabled(false);
            
            Display.create();

            GL11.glEnable(GL11.GL_TEXTURE_2D);                    
            GL11.glClearColor(1f, 1f, 1f, 1f);
            
            // enable alpha blending
            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);
            
            
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            
            //Load Sprites
            graphics_sprite.loadSprite("resources/warrior.png", "PNG", 0);
        }
        catch(LWJGLException e)
        {
            Error.add("Failed to set up Display @ Width: " + width + "Height: " + height);
        }
        
        while(!Display.isCloseRequested())
        {
            render();
            Display.update();
        }
        Error.saveToFile();
        Display.destroy();
        System.exit(0);
    }
        
    //draw() - draws a texture
    public void draw(Texture texture)
    {
        
    }
    //render() - renders all elements of the game by iterating through all lists
    //of drawable items
    public void render()
    {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

        graphics_sprite.setDirection(angle);
        graphics_sprite.draw(0f, 0f);
        angle+=1f;      
    }
            
    private int width;
    private int height;
    private JPanel jpanel;
    private JFrame frame;
    private static double angle;
    GraphicsSprite graphics_sprite;
    private Texture texture;
    static int display = 0;
}


GraphicsSprite.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg2drpg;

import java.awt.Image;

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.TGAImageData;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
/**
 *
 * @author danielrigby
 */
public class GraphicsSprite {
    //draw() - draws the current GraphicsSprite to x, y location
    public void draw(float x, float y)
    {
            GL11.glLoadIdentity();
            GL11.glPushMatrix();
            
            //Draw with rotated direction
            graphics_texture.bind();
            GL11.glTranslatef(x + (graphics_texture.getWidth()/2), y + (graphics_texture.getHeight()/2), 0.0f);
            GL11.glRotated(direction, 0, 0, 1);
            GL11.glTranslatef(-(x + (graphics_texture.getWidth()/2)), -(y + (graphics_texture.getHeight()/2)), 0.0f);
            
            GL11.glBegin(GL11.GL_QUADS);
            
            GL11.glTexCoord2f(0,0);
            GL11.glVertex2f(x, y);
            GL11.glTexCoord2f(1,0);
            GL11.glVertex2f(x + graphics_texture.getWidth(), y);
            GL11.glTexCoord2f(1,1);
            GL11.glVertex2f(x + graphics_texture.getWidth(), y + graphics_texture.getHeight());
            GL11.glTexCoord2f(0,1);
            GL11.glVertex2f(x, y + graphics_texture.getHeight());
 
            GL11.glEnd();
            
            GL11.glPopMatrix();
    }
    
    //loadSprite - loads sprite from image file
    public void loadSprite(String file_name, String file_type, double new_initial_direction)
    {
        initial_direction = new_initial_direction;
        graphics_texture.loadTexture(file_name, file_type);
    }
    
    //setDirection() - sets direction of sprite
    public void setDirection(double new_direction)
    {
        direction = new_direction;
    }
    
    //sprite() - basic constructor for intializing
    public GraphicsSprite()
    {
        initial_direction = 0;
        direction = 0;
        graphics_texture = new GraphicsTexture();
    }
    
    //getImage() - gets bufferedImage object
    public Texture getTexture()
    {
        return graphics_texture.getTexture();
    }
    private double initial_direction;
    private double direction;

    private GraphicsTexture graphics_texture;

}


GraphicsTexture.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg2drpg;
import java.awt.Toolkit;
import java.awt.Image;
import java.lang.String;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
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;
/**
 *
 * @author danielrigby
 */
public class GraphicsTexture {
    //image() - constructor to initialize GraphicsTexture
    public GraphicsTexture()
    {
        
    }
    //GraphicsTexture(String, String) - constructor to load GraphicsTexture from file
    public GraphicsTexture(String file_name, String file_type)
    {
        loadTexture(file_name, file_type);
        
    }
    //loadTexture(String, String) - load GraphicsTexture from file
    public void loadTexture(String file_name, String file_type)
    {
        try 
        {
            // load texture from PNG file
            texture = TextureLoader.getTexture(file_type, ResourceLoader.getResourceAsStream(file_name));
            /*
            System.out.println("Texture loaded: "+texture);
            System.out.println(">> Image width: "+texture.getImageWidth());
            System.out.println(">> Image height: "+texture.getImageHeight());
            System.out.println(">> Texture width: "+texture.getTextureWidth());
            System.out.println(">> Texture height: "+texture.getTextureHeight());
            System.out.println(">> Texture ID: "+texture.getTextureID());
            */
        } catch(IOException e)
        {
            Error.add("Failed to load image: " + file_name);
        }
    }
    
    //getImage() - returns image object
    public Texture getTexture()
    {
        return texture;
    }
    
    //getWidth() - returns width of texture
    public int getWidth()
    {
        return texture.getImageWidth();
    }
    
    //getHeight() - returns height of texture
    public int getHeight()
    {
        return texture.getImageHeight();
    }

    //bind() - binds texture using opengl
    public void bind()
    {
        texture.bind();
    }
    private Texture texture;
}

Neber

I've just changed the code to display in windowed mode and now the texture is being displayed and rotated properly but i obviously want the game to be working in fullscreen, any ideas?

Neber

The problem seems to stemmed from where i set the display modes by looping through all available modes. The code was only selecting the first available mode with fullscreen and this was somehow preventing the textures for display from more than a frame somehow. By making it choose the widest available mode the textures are now displaying for more than a frame in fullscreen mode.