Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (846)
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  
  Difference between these 2 ways of rendering  (Read 1577 times)
0 Members and 1 Guest are viewing this topic.
Offline PocketCrafter7

Senior Devvie


Medals: 6
Projects: 2
Exp: 2 years


One man's bug is another man's feature


« Posted 2014-03-14 11:58:55 »

So, I saw two ways to draw render.

CODE 1
-------

private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

Then in the render method,
for (int i = 0; i < pixels.length; i++) {
     pixels = 0;
}

g.drawImage(image, 0, 0, getWidth(), getHeight(), null);


CODE 2
-------

private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

In the render method,

g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);


So what I basically want to understand is, what is the difference between the two and which one is better.
I know this is a very confusing question, but someone please help.




Nothing is difficult in this world. It is just how you look at it.
Offline Grunnt

JGO Kernel


Medals: 143
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #1 - Posted 2014-03-14 12:15:52 »

Some tips: use code tags to make clearer where your code examples are.

Both ways are identical, the first one just has some nonsensical loop that does nothing at all and probably doesnt even compile:
1  
2  
3  
for (int i = 0; i < pixels.length; i++) {
     pixels = 0;
}


If pixels is an array and you try to do
1  
pixels = 0
, I highly doubt you get anything interesting other than a compile error.

Offline PocketCrafter7

Senior Devvie


Medals: 6
Projects: 2
Exp: 2 years


One man's bug is another man's feature


« Reply #2 - Posted 2014-03-14 12:21:47 »

That is not all the code, there is to it. There is a lot more code.

Rendering the player with the pixel array way.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   public void renderPlayer(int xp, int yp, Sprite sprite) {
      xp -= xOffset;
      yp -= yOffset;
      for (int y = 0; y < 16; y++) {
         int ya = y + yp;
         for (int x = 0; x < 16; x++) {
            int xa = x + xp;
            if (xa < -16 || xa >= width || ya < 0 || ya >= height) break;
            if (xa < 0) xa = 0;
            int col = sprite.pixels[x + y * 16];
            pixels[xa + ya * width] = col;
         }
      }
   }


Rendering the player the other way.
1  
g.drawImage(player_image, x, y, player_size, player_size, null);

Nothing is difficult in this world. It is just how you look at it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Opiop
« Reply #3 - Posted 2014-03-14 13:08:58 »

Take what theCherno says with a grain of salt. He doesn't know much, he's just a copycat.

Anyway. They both have their advantages and disadvantages. The "better" way allows you to edit the pixels individually.
Offline PocketCrafter7

Senior Devvie


Medals: 6
Projects: 2
Exp: 2 years


One man's bug is another man's feature


« Reply #4 - Posted 2014-03-14 13:17:54 »

So you recognized this as Cherno's code, huh ?
Cheesy
Cherno copied this from Notch.

So how do you render ?

I think I am gonna go back to basics now.

Nothing is difficult in this world. It is just how you look at it.
Offline trollwarrior1
« Reply #5 - Posted 2014-03-14 13:23:04 »

So you recognized this as Cherno's code, huh ?
Cheesy
Cherno copied this from Notch.

So how do you render ?

I think I am gonna go back to basics now.

Both of these ways are basic.. It doesn't really matter which one you use. They are both really slow and not worth using for a bigger game.
Offline PocketCrafter7

Senior Devvie


Medals: 6
Projects: 2
Exp: 2 years


One man's bug is another man's feature


« Reply #6 - Posted 2014-03-14 13:30:09 »

Say which way should I use, I am making a Top Down Survival game with random dungeons to explore. How should I go about doing this ?

Nothing is difficult in this world. It is just how you look at it.
Offline Opiop
« Reply #7 - Posted 2014-03-14 13:40:36 »

First off, if you want to talk about Java2D I would recommend talking to my friend Kpars on here. He's good at utilizing Java2D.

I would just use the first way because its easier and you most likely won't need to modify the pixels while the game is running.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #8 - Posted 2014-03-14 13:44:21 »

You should really stick to the standardized method: drawImage(), especially if you don't have enough experience with low-level graphics manipulation. Accessing pixel arrays directly in Java2D is really a last resort if you just have to be able to read and write every pixel individually. Here that is not the case, you are just rendering an image onto another, so use the API provided to do that.

It's also much more readable and concise: 1 line vs. 14+ lines, and will execute faster as Java2D can perform the operation in VRAM, as opposed to that loop, which runs on the CPU and behind the scenes continuously has to tell the graphics card to fetch the modified data.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (351 views)
2018-06-10 19:43:48

EgonOlsen (352 views)
2018-06-10 19:43:44

EgonOlsen (292 views)
2018-06-10 19:43:20

DesertCoockie (527 views)
2018-05-13 18:23:11

nelsongames (843 views)
2018-04-24 18:15:36

nelsongames (830 views)
2018-04-24 18:14:32

ivj94 (1259 views)
2018-03-24 14:47:39

ivj94 (395 views)
2018-03-24 14:46:31

ivj94 (1066 views)
2018-03-24 14:43:53

Solater (413 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!