Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2D - setFilter() on "blank" Image  (Read 1461 times)
0 Members and 1 Guest are viewing this topic.
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Posted 2012-12-30 12:40:40 »

Hey,

in my current game I create a minimap like this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
   public void init(GameContainer gameContainer, Map map) throws SlickException {
        this.map = map;
        miniMap = new Image(map.getMapWidth(), map.getMapHeight());
        mapGraphics = miniMap.getGraphics();

        for (int y = 0; y < miniMap.getHeight(); y++) {
            for (int x = 0; x < miniMap.getWidth(); x++) {
                if (map.getTileAt(x, y) == 0) {
                    mapGraphics.setColor(Color.green);

                } else if (map.getTileAt(x, y) == 66) {
                    mapGraphics.setColor(Color.blue);
                }
                else {
                    mapGraphics.setColor(Color.white);
                }
                mapGraphics.drawRect(x, y, 1, 1);
            }
        }
        mapGraphics.flush();
    }


and this is the rendering code
1  
2  
3  
4  
5  
6  
   public void render(GameContainer gameContainer, Graphics graphics) throws SlickException {
        int posX = gameContainer.getWidth() - (int)mapScale*miniMap.getWidth();
        miniMap.draw(posX, 0, mapScale);

   
    }


Works ok, but it uses the default GL_LINEAER filter for scaling. Now I need this to use GL_NEAREST as filter, but when I

1  
miniMap.setFilter(Image.FILTER_NEAREST);

before the draw call, slick crashes with

java.lang.RuntimeException: Resource not found: org.newdawn.slick.Image@5dd6b6fa
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)

I guess it's because there is no resource-texture availabe and it's a slick-limitation. Is there a known workaround maybe? Cheesy


Offline davedes
« Reply #1 - Posted 2012-12-30 15:58:08 »

This is most likely because you are using an old and outdated version of Slick. There have been some changes to setFilter to make it work a bit better.

You can download the new version here:
http://www.slick2d.org/ (Downloads -> Library Only)

Then you will also need to download the latest LWJGL version:
http://lwjgl.org/download.php

For more info on what this is about, see here.

Alternatively, if for some reason you don't want to use the latest version, here is some ugly code that ought to work:
1  
2  
3  
4  
5  
6  
7  
8  
9  
try {
    //load empty texture with NEAREST filter
   Texture tex = InternalTextureLoader.get().getTexture(new EmptyImageData(width, height), GL11.GL_NEAREST);
    //create a new image using empty texture
   image = new Image(tex);
} catch (IOException e) {
    //texture could not be created... do something
   e.printStackTrace();
}

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #2 - Posted 2012-12-30 16:00:35 »

Well, updating Slick fixed it -.-

Don't know why I was using an old version, thank you very much!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-12-30 16:08:41 »

It's worth mentioning: What you are doing right now is very slow.

Say your minimap is 32x32 pixels, and you want to update it every frame -- that would mean you'd be issuing 1,024 extra draw calls per frame.

You can read up on startUse/drawEmbedded/endUse, which will reduce draw calls. (i.e. Grab a 1x1 opaque white sub-image of your sprite sheet, then you simply bind a color before calling drawEmbedded.)

Alternatively, instead of pushing geometry for each pixel in your minimap, you might have better performance by actually transferring data to a texture. You can check out the PixelData utility that was conceived for this purpose.
1  
2  
3  
4  
5  
6  
... start of render() ...
    //place your colours in the array
   for (...)
        use pixelData.setPixel(..) or pixelData.put(..)
    //transfer the array to the texture on the GPU
   pixelData.apply(minimapImage.getTexture());

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #4 - Posted 2012-12-30 16:20:00 »

I'm confused. The Minimap get's initialized once and filled with the corresponding pixels.

The only rendering draw call is
1  
2  
3  
int posX = gameContainer.getWidth() - (int)mapScale*miniMap.getWidth();
        miniMap.setFilter(Image.FILTER_NEAREST);
        miniMap.draw(posX, 0, mapScale);


The only update that occurs is the player position on the minimap, which is a red dot currently. I only draw that pixel on top of the current graphics context, not the minimap itself, that way I do not need to reinitialize the minimap.

Thanks for the links, but the game runs at >1k fps. So I don't think performance is a problem, yet. Might come in handy later tho Smiley
Offline davedes
« Reply #5 - Posted 2012-12-30 16:40:19 »

drawRect will push a quad to the GPU. So you'd be pushing 1,024 quads individually (glBegin/glEnd) for a 32x32 minimap.

Again, this is mainly a concern if you plan on updating your minimap per frame. Wink

There is another small issue; images using getGraphics should use the static method
Image.createOffscreenImage(width, height)
for initialization, rather than the Image constructor. This is because of a long-standing problem with Slick. The image constructor will still work, but it will lead to an extra texture being created in VRAM -- probably not a big deal for your game, but something to keep in mind.

(Maybe you are starting to see how buggy Slick is? Tongue)

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #6 - Posted 2012-12-30 16:58:40 »

I don't know what you m- (must use libgdx for next project)  Grin Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!