Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  Best API for low-level 2D pixel buffer access?  (Read 2238 times)
0 Members and 1 Guest are viewing this topic.
Offline gmarc

Junior Newbie





« Posted 2009-08-06 20:39:35 »

What's the best JVM-friendly API for direct 2D pixel buffer access. I'm looking for development simplicity, elegance, and uninhibited rendering speed.

I'm loosely familiar with AWT, Swing, and JavaFX, but what specific control our class do I need to get a raw binary pixel buffer that I can write to?
Offline woogley
« Reply #1 - Posted 2009-08-07 00:23:40 »

Disclaimer: This isn't low-level, but still may be useful to you

If you do all your pixel manipulation in an int array, you can update a BufferedImage with those pixel values via the setRGB(0,0,width,height,array,0,array.length) method.

This of course requires you to do pixel manipulation on a one-dimensional array, which is actually good practice as it avoids excessive bounds checking.

The only thing faster than that (though I haven't tested in like 5 years) would be creating an Image via MemoryImageSource and updating the pixels via the newPixels() method. The only reason it would be faster is because Image has less overhead than BufferedImage. But on modern computers and newer JVMs, this difference may no longer be noticeable.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2009-08-07 00:31:34 »

Better:

int[] rgba = ((DataBufferInt)new BufferedImage(w, h, type).getRaster().getDataBuffer()).getData();
byte[] rgba = ((DataBufferByte)new BufferedImage(w, h, type).getRaster().getDataBuffer()).getData();

You can read/write directly into the int[] (or byte[] for that matter) and the image will be updated with every draw. Naturally, you lose hardware acceleration, but it's only positive to keep all data away from the GPU in this case. If you write once, draw many (sprite?), just blit the image into another (compatible) BufferedImage and use that to draw.


Very similar to MemorySourceImage, without the need to mess with Observers and Producers, the newPixels() call takes ages.

I use this method, and it's darn fast.


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2009-08-07 00:32:58 »

Very similar to MemorySourceImage, without the need to mess with Observers and Producers, the updatePixels() call takes ages.

Thanks for this, makes things more straightforward
Offline gmarc

Junior Newbie





« Reply #4 - Posted 2009-08-07 15:23:55 »

I can see how to build a BufferedImage with pixel data. So far, that's perfect.

What do I use to get that image into a window or screen? Should I use AWT, Swing, or JavaFX?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2009-08-07 15:30:00 »


class Viewport extends Canvas
{
   BufferedImage buf;

   public void paint(Graphics g)
   {
       g.drawImage(buf, 0, 0, null);
   }

   public void update(Graphics g)
   {
      // override, to not clear, just paint the canvas
      this.paint(g);
   }

   public Dimension getPreferredSize()
   {
      return new Dimension(buf.getWidth, buf.getHeight());
   }
}



Then add it to a Frame, with a BorderLayout, and call frame.pack(), then frame.setVisible(true)

Whenever you updated your int[], call myViewport.repaint()

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline NewOnJava

Junior Member




1970 born Java game hobbyer.


« Reply #6 - Posted 2009-08-07 21:12:40 »

Removed..
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2009-08-07 21:47:54 »

You wont need BufferedImage buffer with BufferStrategy Wink

So where would you grab hold of that int[] then? Lips Sealed

I think you got confused with implementing double buffering, when you saw my source code.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gmarc

Junior Newbie





« Reply #8 - Posted 2009-08-09 02:11:44 »

Thanks for the help. It actually took a few hours of futzing with JFrame, layouts, canvas/panels, to figure out exactly how to get them properly wired together, but it's working perfectly!

BTW, I'm using a JPanel instead of a Canvas which people say is slightly better...


class Viewport extends Canvas
{
   BufferedImage buf;

   public void paint(Graphics g)
   {
       g.drawImage(buf, 0, 0, null);
   }

   public void update(Graphics g)
   {
      // override, to not clear, just paint the canvas
      this.paint(g);
   }

   public Dimension getPreferredSize()
   {
      return new Dimension(buf.getWidth, buf.getHeight());
   }
}



Then add it to a Frame, with a BorderLayout, and call frame.pack(), then frame.setVisible(true)

Whenever you updated your int[], call myViewport.repaint()
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2009-08-09 02:18:31 »

BTW, I'm using a JPanel instead of a Canvas which people say is slightly better...

JPanel is another layer (level of abstraction, if you will) between the framebuffer and the ARGB int[]. It will do double buffering for you, and you really don't want yet another copy in this case.

So if there is no reason to deal with Swing, don't deal with Swing and work straight with AWT.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

Nickropheliac (9 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (71 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
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!