添加“player”时出现 java.io.IOException 困难纹理进入屏幕

发布于 2024-12-21 19:27:18 字数 5650 浏览 6 评论 0原文

我从 Player 类加载“Player.png”文件时遇到这个奇怪的错误。

我知道我对代码的组织非常草率,而且我使用的方法很糟糕。一半的代码是从教程借来的。

两个类应该做的是创建一个屏幕,其中绿色瓷砖填满屏幕,其中有一个“玩家”精灵,可以使用 W、A、S、D 键向右、向左、向上和向下移动。

错误:

java.io.IOException:尝试为当前硬件分配大纹理 在 org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:293) 在 org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:231) 在 org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:184) 在 org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64) 在 org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24) 在 test.PlayerClass.render(PlayerClass.java:69) 在测试.Main.render(Main.java:110) 在测试.Main.run(Main.java:82) 在 test.Main.main(Main.java:27)

主类

public class Main{

    private static boolean running = true;
    public static final int WIDTH = 1024;
    public static final int HEIGHT = 768;
    private static Texture tile;

static PlayerClass playerClass = new PlayerClass(100, 100, 32, 32);


public static void main(String[] args){

    Main main = new Main();
    main.run(); 
}

//Initialize Method
public static void init(int width, int height ) throws LWJGLException
{
    DisplayMode[] m = Display.getAvailableDisplayModes();
    for(DisplayMode mode : m)
    {
        if(mode.getWidth() == 1024 && mode.getHeight() == 768   && mode.getBitsPerPixel() == 32)
        {
            Display.setDisplayMode(mode);
        }

    }
    Display.setTitle("Game");
    Display.setVSyncEnabled(true);
    Display.sync(100);
    Display.create();

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();

    GL11.glOrtho(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, -1, 1 );
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();

    GL11.glEnable(GL11.GL_ALPHA_TEST);
    GL11.glAlphaFunc(GL11.GL_GREATER, 0.2f);
    GL11.glEnable(GL11.GL_TEXTURE_2D);



    try {
        tile = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("RPG/tile.png"));
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public void run()
{
    try {
        init(1024, 768);
    } catch (LWJGLException e) {
        e.printStackTrace();
    }
    while(running)
    {
        Display.update();
        drawTiled(WIDTH, HEIGHT);
        input();
        update();
        render();

    }
    cleanup();
}

public static void input()
{
    if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
    {
        running = false;
    }

    playerClass.input();

}

public static void update()
{


}
public static void render()
{
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    GL11.glLoadIdentity();

    try {
        playerClass.render();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Display.update();


}

public static void cleanup()
{
    Display.destroy();
}

public void drawTiled(int screenWidth, int screenHeight) {
    Color.white.bind();
    tile.bind();
    int numberPerRow = screenWidth / tile.getTextureWidth();
    int numberOfRows = screenHeight / tile.getTextureHeight();
    GL11.glBegin(GL11.GL_QUADS);
    for (int j = 0; j < numberOfRows; j++) {
        //System.out.print("{");
        for (int i = 0; i < numberPerRow; i++) 
        {

            //top left
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(tile.getTextureWidth() * i, tile.getTextureHeight() * j);

            //top right
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex2f(tile.getTextureWidth() * (i + 1), tile.getTextureHeight() * j);

            //bottom right
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex2f(tile.getTextureWidth() * (i + 1), tile.getTextureHeight() * (j + 1));

            //bottom left
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex2f(tile.getTextureWidth() * i, tile.getTextureHeight() * (j + 1));    
       }

    }
  }
}

玩家类

public class PlayerClass {
  private float x, y;
  private int w, h;
  private Texture player;
  private FloatBuffer verts = BufferUtils.createFloatBuffer(2 * 4);
  private FloatBuffer tex = BufferUtils.createFloatBuffer(2 * 4);

public PlayerClass(float X, float Y, int W, int H)
{
    x = X;
    y = Y;
    w = W;
    h = H;

    verts.put(new float[]{
        0.0f, 0.0f,
        32.0f, 0.0f,
        32.0f, 32.0f,
        0.0f, 32.0f

    });

    tex.put(new float[]{
        0.0f, 0.0f,
        1.0f, 0.0f,
        1.0f, 1.0f,
        0.0f, 1.0f
    });     
}

public void input()
{
    if(Keyboard.isKeyDown(Keyboard.KEY_W))
    {
        y -= 10;
    }

    else if(Keyboard.isKeyDown(Keyboard.KEY_S))
    {
        y += 10;
    }

    if(Keyboard.isKeyDown(Keyboard.KEY_A))
    {
        x -= 10;
    }

    else if(Keyboard.isKeyDown(Keyboard.KEY_D))
    {
        x += 10;
    }

}

public void render() throws IOException
{
    player = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("RPG/player.png"));

    player.bind();
    verts.rewind();
    tex.rewind();

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    GL11.glTranslatef(x, y, 0.0f);

    GL11.glVertexPointer(2, 0, verts);
    GL11.glTexCoordPointer(2, 0, tex);

    GL11.glDrawArrays(GL11.GL_QUADS, 0, 4);

    GL11.glTranslatef(-x, -y, 0.0f);

    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
   }
 }

I get this strange error from loading "Player.png" file from the Player Class.

I understand that my organization of the code is very sloppy and the methods I use are terrible. half of the code is borrowed from tutorials.

What 2 classes are supposed to do is create a screen with green colored tiles filling up the screen with a "player" sprite that can move right, left, up, and down with the W,A,S,D keys.

error:

java.io.IOException: Attempt to allocate a texture to big for the current hardware
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:293)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:231)
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 test.PlayerClass.render(PlayerClass.java:69)
at test.Main.render(Main.java:110)
at test.Main.run(Main.java:82)
at test.Main.main(Main.java:27)

Main Class

public class Main{

    private static boolean running = true;
    public static final int WIDTH = 1024;
    public static final int HEIGHT = 768;
    private static Texture tile;

static PlayerClass playerClass = new PlayerClass(100, 100, 32, 32);


public static void main(String[] args){

    Main main = new Main();
    main.run(); 
}

//Initialize Method
public static void init(int width, int height ) throws LWJGLException
{
    DisplayMode[] m = Display.getAvailableDisplayModes();
    for(DisplayMode mode : m)
    {
        if(mode.getWidth() == 1024 && mode.getHeight() == 768   && mode.getBitsPerPixel() == 32)
        {
            Display.setDisplayMode(mode);
        }

    }
    Display.setTitle("Game");
    Display.setVSyncEnabled(true);
    Display.sync(100);
    Display.create();

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();

    GL11.glOrtho(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, -1, 1 );
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();

    GL11.glEnable(GL11.GL_ALPHA_TEST);
    GL11.glAlphaFunc(GL11.GL_GREATER, 0.2f);
    GL11.glEnable(GL11.GL_TEXTURE_2D);



    try {
        tile = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("RPG/tile.png"));
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public void run()
{
    try {
        init(1024, 768);
    } catch (LWJGLException e) {
        e.printStackTrace();
    }
    while(running)
    {
        Display.update();
        drawTiled(WIDTH, HEIGHT);
        input();
        update();
        render();

    }
    cleanup();
}

public static void input()
{
    if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
    {
        running = false;
    }

    playerClass.input();

}

public static void update()
{


}
public static void render()
{
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    GL11.glLoadIdentity();

    try {
        playerClass.render();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Display.update();


}

public static void cleanup()
{
    Display.destroy();
}

public void drawTiled(int screenWidth, int screenHeight) {
    Color.white.bind();
    tile.bind();
    int numberPerRow = screenWidth / tile.getTextureWidth();
    int numberOfRows = screenHeight / tile.getTextureHeight();
    GL11.glBegin(GL11.GL_QUADS);
    for (int j = 0; j < numberOfRows; j++) {
        //System.out.print("{");
        for (int i = 0; i < numberPerRow; i++) 
        {

            //top left
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(tile.getTextureWidth() * i, tile.getTextureHeight() * j);

            //top right
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex2f(tile.getTextureWidth() * (i + 1), tile.getTextureHeight() * j);

            //bottom right
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex2f(tile.getTextureWidth() * (i + 1), tile.getTextureHeight() * (j + 1));

            //bottom left
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex2f(tile.getTextureWidth() * i, tile.getTextureHeight() * (j + 1));    
       }

    }
  }
}

Player Class

public class PlayerClass {
  private float x, y;
  private int w, h;
  private Texture player;
  private FloatBuffer verts = BufferUtils.createFloatBuffer(2 * 4);
  private FloatBuffer tex = BufferUtils.createFloatBuffer(2 * 4);

public PlayerClass(float X, float Y, int W, int H)
{
    x = X;
    y = Y;
    w = W;
    h = H;

    verts.put(new float[]{
        0.0f, 0.0f,
        32.0f, 0.0f,
        32.0f, 32.0f,
        0.0f, 32.0f

    });

    tex.put(new float[]{
        0.0f, 0.0f,
        1.0f, 0.0f,
        1.0f, 1.0f,
        0.0f, 1.0f
    });     
}

public void input()
{
    if(Keyboard.isKeyDown(Keyboard.KEY_W))
    {
        y -= 10;
    }

    else if(Keyboard.isKeyDown(Keyboard.KEY_S))
    {
        y += 10;
    }

    if(Keyboard.isKeyDown(Keyboard.KEY_A))
    {
        x -= 10;
    }

    else if(Keyboard.isKeyDown(Keyboard.KEY_D))
    {
        x += 10;
    }

}

public void render() throws IOException
{
    player = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("RPG/player.png"));

    player.bind();
    verts.rewind();
    tex.rewind();

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    GL11.glTranslatef(x, y, 0.0f);

    GL11.glVertexPointer(2, 0, verts);
    GL11.glTexCoordPointer(2, 0, tex);

    GL11.glDrawArrays(GL11.GL_QUADS, 0, 4);

    GL11.glTranslatef(-x, -y, 0.0f);

    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-12-28 19:27:18

运行这个:

System.out.println(GL11.glGetInteger(GL11.GL_MAX_TEXTURE_SIZE));

它会告诉您显卡允许的最大纹理大小。确保您的显卡可以处理您尝试使用的大小的纹理。根据错误提示,不可以。

Run this:

System.out.println(GL11.glGetInteger(GL11.GL_MAX_TEXTURE_SIZE));

It'll tell you the max texture size allowed by your Graphics card. Make sure that your graphics card can handle a texture of the size that you are trying to use. According to the error, it can't.

机场等船 2024-12-28 19:27:18

tile.png 有多大 - 最大纹理大小可能会有所不同,具体取决于您的卡,我认为还有其他一些因素。您的文件可能超出此尺寸。

科迪的答案为您提供了获得卡片最大纹理尺寸的方法。如果您正在编写供许多其他人使用的游戏,您可能希望将该代码集成到您的游戏代码中,以便根据其运行的卡的功能加载不同的分辨率纹理,或者简单地找到适用于您想要支持的任何卡的默认值,并将其用于所有安装。

这里有一些额外的阅读材料,将解释有关此问题的更多信息、有关它的一些历史以及您可能会发现有用的更多技术信息:

How large is tile.png - the maximum texture size can vary depending your card, and I think, some other factors. Your file is probably outside this dimension.

Cody's answer gives you the way to get the max texture size for your card. If you're writing a game for use by many other people, you'll probably want to integrate that code into your game code in such a way that it loads different resolution textures based on the capabilities of the card it's running on, or simply find a default that works on any card that you want to support, and use that for all installations.

Here's some additional reading that will explain more about this issue, some history about it, and more technical info that you might find useful:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文