Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 2275 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Member





« Posted 2008-04-09 20: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);
  }
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-04-09 20: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
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2008-04-09 23: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-09 23: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 09: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.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!