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

查看:138
本文介绍了集成图形上的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,但是当我尝试使用Intel 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.

我正在使用全屏独家

在任何给定时刻,我一次渲染大约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 would do something like...

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

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

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