在集成图形上绘制缓慢的 Java2D [英] Slow Java2D Drawing on Integrated Graphics

查看:39
本文介绍了在集成图形上绘制缓慢的 Java2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的 2D 游戏,通过 Java2D API 进行渲染.我注意到当我尝试在集成显卡上绘图时,性能会崩溃.

I'm working on a simple 2D game, rendering via the Java2D API. I've noticed that when I try to draw on integrated graphics card the performance crashes.

我已经在我的主装备上用更新的 ATI Radeon 和我的 5 岁笔记本电脑上测试了这个游戏,这台笔记本电脑也有一个(令人难以置信的过时)Radeon.在这两种情况下,我都获得了不错的 FPS,但是当我尝试使用英特尔 i5 的板载 HD 4000 显卡时,它以大约 20 FPS 的速度爬行.

I've tested this game on both my main rig with a newer ATI Radeon and my 5 year old laptop which also has an (incredibly antiquated) Radeon. On both I get good FPS, but when I try to use my Intel i5's onboard HD 4000 graphics, it crawls at around 20 FPS.

我使用的是全屏独占模式.

I'm using Full Screen Exclusive mode.

在任何给定时刻,我一次渲染大约 1000 张图像.

At any given moment, I am rendering approximately 1000 images at once.

令人讨厌的是,当我尝试 getAvailableAcceleratedMemory() 时,它只为这张卡返回 -1,而且它似乎拒绝加速任何图像.

Annoyingly, when I try to getAvailableAcceleratedMemory() it just returns -1 for this card, and it seems to refuse to accelerate any images.

有人知道如何解决这个问题吗?

Does anyone have any ideas how to fix this issue?

渲染代码:

    Graphics g = bufferStrategy.getDrawGraphics();
    g.drawImage(img, x, y, img.getWidth(), img.getHeight(), null)
    g.dispose();
    bufferStrategy.show();

图片加载代码:

    BufferedImage I = null;
    I = ImageIO.read(new File(currentFolder+imgPath));
    imgMap.put(imgIdentifier, I);

图像存储在由字符串标识的 BufferedImages 的哈希图中,因此当实体需要绘制和成像时,它只需将其从哈希图中取出并绘制即可.在当前情况下,实体主要是地板和墙砖,因此它们永远不会改变(因此除了第一次之外,不必从哈希图中获取图像).

The images are stored in a hashmap of BufferedImages identified by strings, so when an entity needs to draw and image it just gets it out of the hashmap and draws it. In the current case, the entities are mostly floor and wall tiles, so they never change (and thus don't have to get the image from the hashmap other than the very first time).

编辑 - 我已经合并了 MadProgrammer 的方法,但它并没有改变我的 FPS.

EDIT - I've incorporated MadProgrammer's method, but it didn't change my FPS.

推荐答案

这是一个将图像转换为兼容图像的示例...本身并不是答案

这是我使用的一些库代码...

This is some of the library code that I use...

public static BufferedImage createCompatibleImage(BufferedImage image) {
    BufferedImage target = createCompatibleImage(image, image.getWidth(), image.getHeight());
    Graphics2D g2d = target.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    return target;
}

public static BufferedImage createCompatibleImage(BufferedImage image,
        int width, int height) {
    return getGraphicsConfiguration().createCompatibleImage(width, height, image.getTransparency());
}

public static GraphicsConfiguration getGraphicsConfiguration() {
    return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}

我会做类似...

I = createCompatibleImage(ImageIO.read(new File(currentFolder+imgPath)));
imgMap.put(imgIdentifier, I);

这篇关于在集成图形上绘制缓慢的 Java2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆