Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
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  
  fps drops after brightening tile images  (Read 1952 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Member





« Posted 2008-04-09 22:41:02 »

Hi when running at no limit i get 100 fps
when i run this code for every tile on my map (30x30) the fps drops to 8.
Is there a better way to brighten/shade Bufferedimages?

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  
    /* Draw the image with changed brightness, by using a RescaleOp.
    Any alpha channel is unaffected. */

  public void drawBrighterImage(Graphics2D g2d, BufferedImage im,  int x, int y, float brightness) {
    if (im == null) {
      System.out.println("drawBrighterImage: input image is null");
      return;
    }

    if (brightness < 0.0f) {
      System.out.println("Brightness must be >= 0.0f; setting to 0.5f");
      brightness = 0.5f;
    }
    // brightness may be less than 1.0 to make the image dimmer

    RescaleOp brigherOp;
    if (hasAlpha(im)) {
      float[] scaleFactors = {brightness, brightness, brightness, 1.0f};
      // don't change alpha
     // without the 1.0f the RescaleOp fails, which is a bug (?)
     float[] offsets = {0.0f, 0.0f, 0.0f, 0.0f};
      brigherOp = new RescaleOp(scaleFactors, offsets, null);
    } else   // not transparent
     brigherOp = new RescaleOp(brightness, 0, null);

    g2d.drawImage(im, brigherOp, x, y);
  }
Online Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #1 - Posted 2008-04-09 22:48:33 »

It seems you are performing the operation everytime you render the image (?)


You should write the result into another image and use that multiple times, that way you save recalculating the images all the time.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline CaptainJester

JGO Knight


Medals: 10
Projects: 2


Make it work; make it better.


« Reply #2 - Posted 2008-04-10 01:01:57 »

Alpha blending in Java2D is done in software, so that could also be the problem.  If you use an OpenGL based 2D engine/renderer, that will get you alpha blending in hardware.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline stef569

Junior Member





« Reply #3 - Posted 2008-04-10 01:19:22 »

oops indeed i created the effect time after time...

1  
2  
3  
4  
5  
6  
7  
8  
9  
  
  // Create additional darker Terrain images, used for Fog
   for (BufferedImage img : terrainImages) {
      BufferedImage darkerCopy;
      darkerCopy = new BufferedImage(img.getWidth(), img.getWidth(), BufferedImage.OPAQUE);
      Graphics2D g = (Graphics2D) darkerCopy.getGraphics();
      imgSFX.drawBrighterImage(g, darkerCopy, 0, 0, 0.5F);
      darkTerrainImages.add(darkerCopy);
    }


I'm trying to make the darker images in the constructor of my map, but it shows a black square when i paint.
is there something wrong with the code above? I don't see it.

OPAQUE means all pixels should be drawen
It's not giving a null pointer exception so it's there already
I used the 0.5 before should make it a bit darker. but not black!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
  private void paintTerrain(Graphics2D g, Terrain terrain, boolean fogged) {
    if (terrain != null) {
      BufferedImage buf;
      if (fogged) {
        buf = darkTerrainImages.get(terrain.getType().ordinal());
      } else {
        buf = terrainImages.get(terrain.getType().ordinal());
      }
      g.drawImage(buf, 0, 0, null);
    }
  }


@CaptainJester
I would like to stick to java2D atm
Offline stef569

Junior Member





« Reply #4 - Posted 2008-04-10 11:07:42 »

got it! I first need to copy the other image  Cheesy

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
  public BufferedImage grabImage(int width, int height, BufferedImage srcImg, int col, int row) {
    BufferedImage copy;
    Graphics2D graphics;
    int transparency = srcImg.getColorModel().getTransparency();

    // Create new Image
   copy = this.gc.createCompatibleImage(width, height, transparency);

    // create a graphics context for the new Image
   graphics = copy.createGraphics();

    // copy image from srcImg to copy
   graphics.drawImage(srcImg, 0, 0, width, height,       // copy img rectangle
           (col * width), row * height,                  // source img top Left rectangle cord
           ((col + 1) * width), (height * (row + 1)),    // source img lower Right rectangle cord
           null);
    graphics.dispose();
    return copy;
  }

solved, on to the next
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (67 views)
2013-05-17 21:29:12

alaslipknot (76 views)
2013-05-16 21:24:48

gouessej (109 views)
2013-05-16 00:53:38

gouessej (104 views)
2013-05-16 00:17:58

theagentd (115 views)
2013-05-15 15:01:13

theagentd (104 views)
2013-05-15 15:00:54

StreetDoggy (149 views)
2013-05-14 15:56:26

kutucuk (172 views)
2013-05-12 17:10:36

kutucuk (170 views)
2013-05-12 15:36:09

UnluckyDevil (179 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.171 seconds with 20 queries.