lwjgl texture = java.lang.NullPointerException

时间:2013-04-18 20:20:20

标签: java textures lwjgl 2d-games

好的,我在过去的4天里一直试图解决这个问题,而且我已经在谷歌搜索了几个小时的答案。

我知道由于某种原因,我的纹理不会被加载,纹理等于null, 好像它从未被宣布过。

错误

Exception in thread "main" java.lang.NullPointerException
    at RetinaEngine.RetinaDraw.drawTexture(RetinaDraw.java:65)
    at GameTest.Game.renderGame(Game.java:61)
    at RetinaEngine.Main.renderGame(Main.java:97)
    at RetinaEngine.Main.startGameLoop(Main.java:60)
    at RetinaEngine.Main.main(Main.java:46)
Java Result: 1

所以我希望你们其中一个人看到问题。

我的主要课程

  package RetinaEngine;

import GameTest.Game;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

/**
 * @author ***** ********
 */
public class Main {

    /*
     * Declare variables
     */

    private static int gameWindowWidth = 1280;
    private static int gameWindowHeight = 720;
    private static String gameWindowName = "IndieSpaceGame";
    private static boolean fullScreen = false;
    private static boolean startFullScreen = false;
    private static DisplayMode displayModeFullScreen;
    private static DisplayMode displayModeWindowed;
    private static Game game;

    /*
     * The Main Methode
     */

    public static void main(String[] args) 
    {
        initializeDisplayModes();
        initializeDisplay();
        initializeOpenGL();
        initializeGame();
        startGameLoop();
        destroyDisplay();        
    }

    /*
     * This starts the loop that runs this game
     */

    private static void startGameLoop()
    {
        while(!Display.isCloseRequested())
        {
            getUserInput();
            updateGame();
            renderGame();
        }
    }

    /*
     * The Method that looks if the user had anny input
     */

    private static void getUserInput()
    {
        if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) & Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) & !Keyboard.isRepeatEvent())
        {
            switchFullScreen();
        }
        game.getUserInput();
    }

    /*
     * The Method that updates all the objects in the game
     */

    private static void updateGame()
    {
        game.updateGame();
    }

    /*
     * The method that starts rendereing the game 
     */

    private static void renderGame()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
        glLoadIdentity();

        glColor3f(1,1,1);

        game.renderGame();

        Display.update();
        Display.sync(60);
    }

    /*
     * The Methode That sets up the Display
     */

    private static void initializeDisplay()
    {
        try 
        {   if(startFullScreen)
            {
                Display.setDisplayMode(displayModeFullScreen);
            }
            else
            {
                Display.setDisplayMode(displayModeWindowed);
            }
            Display.setTitle(gameWindowName);
            Display.setFullscreen(true);
            Display.setResizable(false);
            Display.create();
            Display.setVSyncEnabled(true);
        } 
        catch (LWJGLException ex) 
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     * The methode that initialize all the openGL code
     */

    private static void initializeOpenGL()
    {

        //glViewport(0, gameWindowWidth, 0, gameWindowHeight);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-(gameWindowWidth / 2), (gameWindowWidth / 2), -(gameWindowHeight / 2), (gameWindowHeight / 2),-1,1);

        glMatrixMode(GL_TEXTURE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        glMatrixMode(GL_MODELVIEW);
        glClearColor(0,0,0,0);

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glDisable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);



    }

    /*
     * The methode that initializes the Game
     */

    private static void initializeGame()
    {
        game = new Game();        
    }

    /*
     * initialize all the diffrent display modes
     */

    private static void initializeDisplayModes()
    {
        try 
        {
            DisplayMode[] modes = Display.getAvailableDisplayModes();

             for (int i = 0; i < modes.length; i++)
             {
                 if (modes[i].getWidth() == gameWindowWidth && modes[i].getHeight() == gameWindowHeight && modes[i].isFullscreenCapable())
                   {
                        displayModeFullScreen = modes[i];
                   }
             }

            displayModeWindowed = new DisplayMode(gameWindowWidth, gameWindowHeight);
        } 
        catch (LWJGLException ex) 
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     * if the game ends destroy the display
     */

    private static void destroyDisplay()
    {
        Display.destroy();
    }

    /*
     * The methode that switches between the display modes
     */

    private static void switchFullScreen()
    {
            try 
            {
                if(!fullScreen)
                {
                    Display.setDisplayMode(displayModeFullScreen);
                    fullScreen = true;
                }
                else
                {
                    Display.setDisplayMode(displayModeWindowed);
                    fullScreen = false;
                }
            } 
            catch (LWJGLException ex) 
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
    }


}

我的游戏课程

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

import org.newdawn.slick.opengl.Texture;
import static RetinaEngine.RetinaDraw.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.opengl.TextureLoader;
/**
 *
 * @author Jesse
 */
public class Game {

    private Texture terror;

    public void Game() {
        try {
            terror = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/terror.png")));
            // Replace PNG with your file extension
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        } catch (IOException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }
    }

    /*
     * Get user input in our Game
     */

    public void getUserInput()
    {

    }

    /*
     * Update all the game objects
     */

    public void updateGame()
    {

    }

    /*
     * Render the game
     */
    public void renderGame()
    {
        drawTexture(0, 0, 128, 128, 0, terror);
    }
}

绘图功能

    public static void drawTexture(float x, float y, float width, float height, float rotation, Texture texture)
    {
        Texture tex = texture;
        glPushMatrix();
        { 
            glTranslatef(x, y, 0);
            glRotatef(rotation, 0, 0, 1);

            Color.white.bind();
            tex.bind();
            //glBindTexture(GL_TEXTURE_2D, tex.getTextureID());
            glBegin(GL_QUADS);                
                glTexCoord2f(0,0);
                glVertex2f(-width / 2, -height / 2);
                glTexCoord2f(0,1);
                glVertex2f(-width / 2,  height / 2);
                glTexCoord2f(1,1);
                glVertex2f( width / 2,  height / 2);
                glTexCoord2f(1,0);
                glVertex2f( width / 2, -height / 2);
            glEnd();
        }
        glPopMatrix();
    }

0 个答案:

没有答案