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  
  Best Method For Rendering?  (Read 1134 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2012-06-28 03:18:44 »

I'm trying to figure out the best method to rendering data on screen... and ideally NOT by just using drawImage.

Does anyone have a particular way of doing it that is scalable, reliable and has good performance?

I'm not only referring to the act of shifting the data, I'm also referring to the whole architecture.  What calls what, in what order, and how are the Objects organized?
Offline Cero
« Reply #1 - Posted 2012-06-28 03:42:07 »

good performance

dont use Java2D.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-06-28 06:41:23 »

Here's what I do for Java2D

At the beginning of my main render method, the Graphics object is sent to my Screen class.
After this, the game is rendered, which also renders all of the entities and tiles and things. Here's what a typical render method looks like for an entity:

1  
2  
3  
public void render() {
     Screen.draw(x, y, 0, false, false);
}


Where the parameters are x, y, sprite, xFlip and yFlip.
My Screen.draw method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   public static void draw(int x, int y, int sprite, boolean flipX, boolean flipY) {
      BufferedImage img = sheet.sprites[sprite];
     
      if (flipX) {
         AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
         tx.translate(-img.getWidth(), 0);
         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
         img = op.filter(img, null);
      }
      if (flipY) {
         AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
         tx.translate(0, -img.getHeight());
         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
         img = op.filter(img, null);
      }
     
      draw2(img, x, y, img.getWidth(), img.getHeight());
   }
   
   public static void draw2(BufferedImage img, int x, int y, int width, int height) {
      g.drawImage(img, x, y, width, height, null);
   }


At the end of the rendering cycle, my main render method gets the Graphics object back from the Screen class.

This prevents having to pass around the Graphics object to all of my classes that will be rendering, and it lets me condense my rendering logic.

Hope this helps somehow.
 | Nathan

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

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-06-28 07:06:00 »

Static draw methods?!? O____O Shocked

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2012-06-28 18:17:20 »

Oh God.. I can already tell I've been doing something wrong.

Lay it on me... what's wrong with using static draw methods?

 | Nathan

Check out my website!
Offline StumpyStrust
« Reply #5 - Posted 2012-06-28 18:24:35 »

Well if you want best performance use something other then Java2D like the other post said.

But if you are still using it, there is not a whole lot faster then just drawImage from what I understand.

One key thing to be careful of is to not create AffineTransforms or AlphaComposites every frame. By having a static composite or transform for that class I saw a huge boost in performance.


1  
2  
3  
4  
5  
6  
7  
8  
private static AlphaComposite alphaComp;

alphaComp = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, (fade));
      g2d.setComposite(alphaComp);
[/code

This is what I do. I am prob doing something wrong here but it works so take it or leave it.

Offline Rorkien
« Reply #6 - Posted 2012-06-28 18:28:37 »

Here's what I do for Java2D

At the beginning of my main render method, the Graphics object is sent to my Screen class.
After this, the game is rendered, which also renders all of the entities and tiles and things. Here's what a typical render method looks like for an entity:

1  
2  
3  
public void render() {
     Screen.draw(x, y, 0, false, false);
}


Where the parameters are x, y, sprite, xFlip and yFlip.
My Screen.draw method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   public static void draw(int x, int y, int sprite, boolean flipX, boolean flipY) {
      BufferedImage img = sheet.sprites[sprite];
     
      if (flipX) {
         AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
         tx.translate(-img.getWidth(), 0);
         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
         img = op.filter(img, null);
      }
      if (flipY) {
         AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
         tx.translate(0, -img.getHeight());
         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
         img = op.filter(img, null);
      }
     
      draw2(img, x, y, img.getWidth(), img.getHeight());
   }
   
   public static void draw2(BufferedImage img, int x, int y, int width, int height) {
      g.drawImage(img, x, y, width, height, null);
   }


At the end of the rendering cycle, my main render method gets the Graphics object back from the Screen class.

This prevents having to pass around the Graphics object to all of my classes that will be rendering, and it lets me condense my rendering logic.

Hope this helps somehow.
 | Nathan

Why make it static? I'm pretty sure you have an instance of the Screen class at all times Smiley
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #7 - Posted 2012-06-28 18:30:04 »

No, I don't. At the beginning a call

1  
new Font();


and Font extends Screen.

I am switching stuff around now so that this isn't static, I guess. Still waiting for a reply from ra4king Smiley

 | Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-06-28 18:37:18 »

Well design-wise...ever heard of OOP? Smiley

Offline jonjava
« Reply #9 - Posted 2012-06-28 19:09:48 »

Java2D is more than fine.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aazimon
« Reply #10 - Posted 2012-06-28 20:19:11 »

With a static method, you need to be more aware of "when" you call it. The Graphics object could be null or disposed of.
Passing in the graphic object instead will give you more control over it.
Offline Cero
« Reply #11 - Posted 2012-06-28 21:19:37 »

Lay it on me... what's wrong with using static draw methods?

Nothing. It's fine. Can be slightly cosmetically ugly.

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 (76 views)
2014-09-10 06:39:09

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

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

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

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

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

Longarmx (35 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!