Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  Tiles  (Read 1397 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2013-08-17 14:12:06 »

How would you make and render tiles i make 2d array and 2 for loops but i dont think that is good
Help ??

Java freak! Cheesy
Offline jonjava
« Reply #1 - Posted 2013-08-17 14:17:50 »

I think that's good I do that too.

Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #2 - Posted 2013-08-17 16:10:28 »

That basically exactly what I do.

Works out just fine. Grin

"Experience is what you get when you did not get what you wanted"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-08-18 12:31:20 »

2D array. Loop through it every frame and draw them.

Offline PaidGEEK

Senior Member


Medals: 5
Projects: 3


twitter.com/PaidGEEK


« Reply #4 - Posted 2013-08-18 13:03:09 »

I always keep tiles in 1D array and draw them either with 2 for loops (accessing tileID with tiles[x+y*mapWidth]) or something like this:
(rough example)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
int mapWidth = 2;
int mapHeight = 2;
int[] tiles = {
   1, 1
   1, 2
};

for(int i = 0; i < tiles.length; i++) {
   int x = i % mapWidth;
   int y = i / mapWidth;
   int tileID = tiles[i];
         
   draw(tileID, x, y);
}

Improvisational programmer.
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #5 - Posted 2013-08-18 17:58:25 »

If data doesn't need to be loaded in chunks, why would a 2D array be a bad thing?

@PaidGEEK

You could also do:

1  
2  
3  
4  
5  
6  
7  
8  
final int mask = 15, shift = 4; // 16 x 16

for(int i = 0; i < tiles.length; i++) {
   int x = i & mask;
   int y = i >> shift;
   int tileID = tiles[i];
   draw(tileID, x, y);
}

for powers sizes of two. Or for everything else:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
for(int i = 0, x = 0, y = 0; i < tiles.length; i++)
{
   if(++x == width)
   {
      x = 0;
      y++;
   }
   int tileID = tiles[i];
   draw(tileID, x, y);
}


Smiley
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #6 - Posted 2013-08-18 19:04:49 »

I don't think you are looking at my or PaidGEEK's code correctly.

What good would storing the tile's location do? You want to know if the tile at (10, 15) is grass or water, not where a tile that happens to grass is located.
In a 1 or 2D array, you can find a tile's location given its index or a tile's index given its location.

Where do you get world and screen coordinates from? I see an assignment to x and y, but their values are derived from an array index not a separate X, Y pair.

Also, any assignment using ints in the form "a = b (operator) c;" is going to be much faster than an array look up.
Online BurntPizza
« Reply #7 - Posted 2013-08-18 19:07:05 »

Its a good idea, but why make it so complicated? I don't understand why you don't just store the tile location. If I'm looking at your code correctly, it seems like you're trying to get the world coordinates of a tile from its screen coordinates. Isn't that slow?

Nah, they're just using funky ways to get around using nested loops to iterate through the tile list, and still have a coordinates on hand by calculating them from the index and 'width'. Some say it's faster than a nested for loop. I don't have any evidence as to which is faster though.
If know you have a wide 2D array, you can save some mults by premultiplying the width:
1  
2  
3  
4  
5  
6  
7  
for(int y=0;y<h;y++) {
   int wy = y*w;
   for(int x=0;x<w;x++) {
      int i = wy+x;
      //stuff
  }
}
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #8 - Posted 2013-08-18 19:08:20 »

Oh, now I feel like an idiot. Deleted my post! I didn't realize what you were trying to accomplish, I just glanced at your code and thought that you were doing something funky. Sorry!

Offline Jeremy
« Reply #9 - Posted 2013-08-18 19:12:06 »

I agree with @opiop65 - nested loops are frequently used for multi-dimensional arrays, it makes most sense to me to use one.

Neither one is faster than the other - well, not to a point where it really matters anyway. Just chose the one that is easier to read for you (personally I find a nested loop easier to read)

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #10 - Posted 2013-08-18 19:18:42 »

Actually, now I'm wondering if it is faster. Has anyone tested that theory out? If so, its not too big of a change if it helps out with performance. Its really odd to read, but that's ok if I can scrape a few extra frames out using it. Although I agree, I would rather use nested loops.

Offline Jeremy
« Reply #11 - Posted 2013-08-18 19:22:53 »

Actually, now I'm wondering if it is faster. Has anyone tested that theory out? If so, its not too big of a change if it helps out with performance. Its really odd to read, but that's ok if I can scrape a few extra frames out using it. Although I agree, I would rather use nested loops.

It won't be faster. If it is, it doesn't even matter.Almost anything you can do in those loops will literally dwarf the time spend multiplying two numbers. It would be a relatively extreme micro-optimization.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Online BurntPizza
« Reply #12 - Posted 2013-08-18 19:30:34 »

Actually, now I'm wondering if it is faster. Has anyone tested that theory out? If so, its not too big of a change if it helps out with performance. Its really odd to read, but that's ok if I can scrape a few extra frames out using it. Although I agree, I would rather use nested loops.

It won't be faster. If it is, it doesn't even matter.Almost anything you can do in those loops will literally dwarf the time spend multiplying two numbers. It would be a relatively extreme micro-optimization.

I agree, these kind of things are mostly a waste of time, and belong in the performance tuning board  Tongue
The only time I have had any real use for playing with loops and micro-optimizing them was a long time ago in a program where I had to loop through half a million items every frame, and maintain several hundred fps. Other than such edge cases, it's a non-issue.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #13 - Posted 2013-08-18 19:34:34 »

That's what I thought, I was just making sure.

Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #14 - Posted 2013-08-18 19:36:53 »

Actually, don't use the second example I had. You can use nested loops with a one dimensional array and avoid the if while still avoiding division and mod. Lips Sealed Not that it matters a lot, but if you wanted to bother using a 1D array then you may as well make x y calculations fast, too.

Actually, now I'm wondering if it is faster. Has anyone tested that theory out? If so, its not too big of a change if it helps out with performance. Its really odd to read, but that's ok if I can scrape a few extra frames out using it. Although I agree, I would rather use nested loops.

It won't be faster. If it is, it doesn't even matter. Typically what you do in those loops literally dwarfs the time spend multiplying two numbers. It would be a relatively extreme micro-optimization.

I could be appropriate in some cases. Something is not a micro-optimization just because one line of code is different.

If you have a big array you want to be conscious of what order you traverse it in. If swap you the order of your x and y for loop, you would be accessing every w'th element instead of doing sequential access, which could have poor RAM performance. You also create a new object for each row with a 2D array. The difference between division/modulo and shift/mask or division/modulo and incrementing is big, but not nearly as much as the first thing.

Of course, that only matters if you are performance conscious of what is in the loop body. If you are dereferencing object variables or drawing in the loop, you won't see a difference. And you only need to be performance conscious if its something that does a lot of work, such as collision detection used by AI.
Online BurntPizza
« Reply #15 - Posted 2013-08-18 19:45:15 »

And you only need to be performance conscious if its something that does a lot of work, such as collision detection used by AI.

Always use a profiler and only bother optimizing the hot code, that's how you get results.

I think we might have slightly derailed this thread, but then again I'm not sure it was ever going anywhere.  Clueless
Offline Jeremy
« Reply #16 - Posted 2013-08-18 19:55:02 »

@Several Kilo-Bytes

I don't think it is micro-optimization because it is one line of code. It is micro-optimization because we are comparing multiplication performance overhead in what is likely adjacent to much more comprehensive render or update routine that no one has started talking about.

Ofcourse you can alter the context of anything to counter an argument, but I am speaking to the application here.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #17 - Posted 2013-08-18 20:27:10 »

Hooray for off-topicness!

@BurntPizza I agree with the sentiment, but sometimes you write a major portion of your game engine, find out it does not scale as well as it did with your test levels did once other features are added in, and then have to do a complete overhaul of your engine from scratch. Emo It is good advice, but like all rules you eventually learn some exceptions. I would not give new programmers the opposite advice, but I also would never tell them not to optimize disregard design decisions of a section of code they know will be hot. (Though the reason the rule exists is because people are notoriously bad at estimating what parts will eventually be a bottle neck.)

This quote from Wikipedia
Quote
A better approach is therefore to design first, code from the design and then profile/benchmark the resulting code to see which parts should be optimized. A simple and elegant design is often easier to optimize at this stage, and profiling may reveal unexpected performance problems that would not have been addressed by premature optimization.
would not be complete without this one:
Quote
In practice, it is often necessary to keep performance goals in mind when first designing software, but the programmer balances the goals of design and optimization.

@Jeremey I agree. My answer was limited to the context of opiop65's question. Yours is the same caveat I gave opiop65.
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.

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

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

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

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

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

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

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!