Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
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  
  "hi res" textures  (Read 2775 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-11-02 10:53:28 »

Riiight, so more questions by me. Tongue

How do one go about handling hi res textures? Say the tile size of my game is 32x32, how can I apply a hi res texture to that? One that's 128x128, do I just scale the texture to fit the 32x32 that the tile should be?

Right now I'm scaling my towers in my TD from 64x64 to 32x32, but is that the right way to do it?
Online princec

« JGO Spiffy Duke »


Medals: 439
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2012-11-02 10:56:53 »

Works for me.

Cas Smiley

Offline Regenuluz
« Reply #2 - Posted 2012-11-02 10:58:46 »

Well, in that way I guess it'll work for me too.

Is this the right way to do it, in pure java?

1  
2  
3  
4  
AffineTransform tx = new AffineTransform();
tx.scale(0.5, 0.5);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
op.filter(this.actor.getImage(1), null);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online princec

« JGO Spiffy Duke »


Medals: 439
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2012-11-02 12:22:42 »

I don't actually rescale them permanently; I just draw them at whatever size I want them whenever I want them. (All my games operate on a system of having logical coordinates, mapped automatically to physical coordinates. Logically they are all 320x320 "pixels" in size)

Cas Smiley

Offline Danny02
« Reply #4 - Posted 2012-11-02 12:27:13 »

when you scale an image you get filtering issues when lowering the size more then half, because of the standard filter method.(see mipmaps for example).
So to get good results in a game one should either scale the images to the size they will be displayed or use mipmapping(should be quite easily implemented in java2d).

I read a some time ago a quite good blog article about scaleing images in java 2d and the final best method to do things was the following code.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public static BufferedImage getScaledImage(BufferedImage image, int targetWidth, int targetHeight) {
        int accWidth = image.getWidth();
        int accHeight = image.getHeight();

        BufferedImage result = image;
        do {
            accHeight = Math.max(accHeight / 2, targetHeight);
            accWidth = Math.max(accWidth / 2, targetWidth);
           
            BufferedImage tmp = new BufferedImage(accWidth, accHeight, image.getType());

            Graphics2D g2 = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                        RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.drawImage(result, 0, 0, accWidth, accHeight, null);

            result = tmp;
        } while (accHeight != targetHeight && accWidth != targetWidth);

        return result;
    }
Online SHC
« Reply #5 - Posted 2012-11-02 13:28:36 »

Try using SVG images by using the Batic library

Offline nsigma
« Reply #6 - Posted 2012-11-02 14:36:42 »

I read a some time ago a quite good blog article about scaleing images in java 2d and the final best method to do things was the following code. ...

This is almost identical to the example in Filthy Rich Clients - http://filthyrichclients.org/.  It's downloadable (with test code) from the chapter 4 examples.  There are two major differences - it uses BILINEAR instead of BICUBIC, which is faster and probably more appropriate in this case; and they only allocate a single temporary image for all loops.

Try using SVG images by using the Batic library

You could also switch to measuring the speed of your game in spf!  Tongue

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Danny02
« Reply #7 - Posted 2012-11-02 17:37:36 »

yeah bilinear should be faster, but one should cache scaled images anyway, so speed doesn't matter that much
Offline nsigma
« Reply #8 - Posted 2012-11-02 18:39:34 »

yeah bilinear should be faster, but one should cache scaled images anyway, so speed doesn't matter that much

I realise that.  It's actually the appropriateness I was more wondering about - most places I've seen this technique refer to it as progressive bilinear scaling.  The halving of dimensions comes from the fact that bilinear samples from 2 pixels in each direction.  Using an interpolation method that uses more pixels therefore seems somehow wrong - it may however be better.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Danny02
« Reply #9 - Posted 2012-11-02 21:19:49 »

The halving of dimensions comes from the fact that bilinear samples from 2 pixels in each direction.

yes because otherwise you would "miss" pixels(undersampling) while scaling. Some pixels wouldn't contribute to the final image which can cause artifacts.
With bicubic filtering one has 4 samples in each direction, but the 2 outer samples contribute not that much to the output, because of which I still prefer to do the scaling steps only by half.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Regenuluz
« Reply #10 - Posted 2012-11-02 22:50:11 »

Hm, how can I improve my rendering? the fps falls when I have around 100 towers on the screen.

Here's my tower render method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
AlphaComposite trans = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);
AlphaComposite solid = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);

@Override
public void render(Graphics g) {
   Graphics2D g2d = (Graphics2D) g;
     
   for(Entity e : this.shots) {
      e.render(g2d);
   }
     
   // Turret base shadow
   g2d.setComposite(this.trans);
   g2d.drawImage(this.actor.getImage(1), this.pos.x-16+1, this.pos.y-16+1, null);
     
   // Turret base
   g2d.setComposite(this.solid);
   g2d.drawImage(this.actor.getImage(0), this.pos.x-16, this.pos.y-16, null);
     
     
   AffineTransform tx = new AffineTransform();
   tx.rotate(this.rotation, 16, 16);
   AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
     
   // Turret shadow
   g2d.setComposite(this.trans);
   g2d.drawImage(op.filter(this.actor.getImage(5), null), this.pos.x-16+2, this.pos.y-16+2, null);
     
   // Turret
   g2d.setComposite(this.solid);
   g2d.drawImage(op.filter(this.actor.getImage(this.towerSprite), null), this.pos.x-16, this.pos.y-16, null);
}


At this point the sprites for the tower is already scaled (and cached in my spritesheet handling thing, for easy access if need be).

EDIT:
And for the curious, here's my "good looking" tower: http://infloop.org/tmp/tower.png

EDIT EDIT:
Cleaned the rendering code a little, now it's around 110 towers that the fps starts to drop.
Offline nsigma
« Reply #11 - Posted 2012-11-02 23:59:51 »

@Regenuluz Try using the transform (rotate) on the Graphics2d itself. You're not allowing the graphics pipeline to optimise by drawing in one command, as well as causing the op to continually create temporary images.

@Danny02 Yes, I know, though pretty sure cubic in Java2d is 3x3.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Regenuluz
« Reply #12 - Posted 2012-11-03 07:06:21 »

How would I use it directly on the g2d?
Offline nsigma
« Reply #13 - Posted 2012-11-03 13:44:05 »

How would I use it directly on the g2d?

Use

1  
g2d.rotate(this.rotation, x, y);


Bear in mind that the x and y will be different than you have now, at a guess this.pos.x and this.pos.y - needs to be in the coordinates of the Graphics2D, not your image.

If you're using the Graphics2D across multiple methods you'll also want to add

1  
2  
3  
4  
Graphics2D g2d = (Graphics2D) g;
AffineTransform originalTx = g2d.getTransform();
// rest of render method
g2d.setTransform(originalTx);


to remove the rotation afterwards.

You could also look at the version of drawImage() that takes a BufferedImageOp and pass your AffineTransformOp to that.  This might allow it to be optimised too, though I'd stick with using the transform on the Graphics2D myself.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline StumpyStrust
« Reply #14 - Posted 2012-11-04 02:43:05 »

So for each tower you have about 4 draw calls? You should be able to get way higher then that.

I think that all the affine transformations is what is slowing you down. I am by no means a java2D pro but it seems like you have WAY too many composites and transforms going on. Simplest way for me to do rotation and alpha compositions is this.

1  
2  
3  
4  
5  
6  
7  
Graphics2D g2d = (Graphics2D)g.create();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (fade)));
        g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));
        //draw stuffs...


g2d.dispose();


Very fast. The x and y or in your screens coordinate space.

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.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (21 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (60 views)
2014-12-15 09:26:44

Mr.CodeIt (48 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (117 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!