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 (847)
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  
  2D Minecraft-Like Game, Rendering issue.  (Read 30028 times)
0 Members and 1 Guest are viewing this topic.
Offline Xolitude

Junior Devvie


Medals: 2
Projects: 1



« Posted 2015-07-12 10:55:52 »

Hey guys :3

How do I render what's only on the screen? I can currently only render an array of:

blocks[50][50], any higher and the game begins to lag. I'm guessing because I am looping through all of those tiles every frame and I need to know how to render much more but only what's on the screen so it doesn't lag... Any help?
Offline PlainBug
« Reply #1 - Posted 2015-07-12 11:24:58 »

Hi,
you can calculate which map-rectangle collides with the big screen-rectangle and if there is a collision between them, the map-rectangle will be rendered.

I hope this idea helps you  Smiley

Offline lcass
« Reply #2 - Posted 2015-07-12 11:29:27 »

Java 2d , may god have mercy on your soul.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bitbytebytes

Junior Devvie


Medals: 2



« Reply #3 - Posted 2015-07-12 14:29:32 »

It is certainly possible to have good performance in relatively simple games in Java2D.
For example, my Zelda clone (wip), which is entirely Java2D.

This is with 256 Octorocks wandering around the entire map, and they all get their ai updated every logic tick.
I keep the entire map in an array of static tile types and only render the tiles in view.
Offline lcass
« Reply #4 - Posted 2015-07-12 21:09:09 »

it's not the logic tick that's the issue.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #5 - Posted 2015-07-13 01:10:58 »

It is possible, although not as easy as an ogl solution: http://www.java-gaming.org/topics/renoria-java-mmorpg/30991/msg/287202/view.html#msg287202
Offline Xolitude

Junior Devvie


Medals: 2
Projects: 1



« Reply #6 - Posted 2015-07-13 01:36:37 »

Hi,
you can calculate which map-rectangle collides with the big screen-rectangle and if there is a collision between them, the map-rectangle will be rendered.

I hope this idea helps you  Smiley

I will try this, thank you Smiley

It is certainly possible to have good performance in relatively simple games in Java2D.
For example, my Zelda clone (wip), which is entirely Java2D.

This is with 256 Octorocks wandering around the entire map, and they all get their ai updated every logic tick.
I keep the entire map in an array of static tile types and only render the tiles in view.



Yes but my question is how did you get to render what was only on screen?
Offline chrislo27
« Reply #7 - Posted 2015-07-13 02:41:22 »

If you're iterating through your tiles like this:
1  
2  
3  
4  
5  
for(int x = 0; x < width; x++){
   for(int y = 0; y < height; y++){
      // render the tile at x, y
   }
}


That'll render all the tiles. But like you said, you only want to render some tiles, so change where x and y start and end. Convert the top left corner of the screen to the world coordinates and start there, and then figure out how wide the screen is in tiles (which is as easy as screenWidth / tileWidthInPixels and the same for height).

Hope that gets you started on what to do.
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #8 - Posted 2015-07-13 10:54:34 »

If the "camera" is pointed at tile (cameraX, cameraY), and the screen is say width x height tiles, then you would do something list this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
int xStart = Math.max(0, cameraX - (width / 2));
int xEnd = Math.min(width , cameraX + (width / 2));
int yStart = Math.max(0, cameraY - (height / 2));
int yEnd = Math.min(height, cameraY + (height / 2));
for(int x = xStart; x < xEnd; x++){
   for(int y = yStart; y < yEnd; y++){
      // render the tile at x, y
   }
}


Also, I wonder if the problem is really rendering. How are you doing collision detection? Collision detection can explode depending on how you are doing it. for example,  let's say you have 10 entities all moving, and each frame you simplistically check all the other entities for collision. That's 100 checks (including checking collision with self Smiley ). If you do the same for 100 entities, you are then doing 10,000 collision checks per frame.

Offline Ed_RockStarGuy
« Reply #9 - Posted 2015-07-13 11:00:36 »

I cant stand using java2D any more, i used to be in the same situation as you, but as soon as i switched over to LWJGL my games now run 1000% faster and a old freerome game i made can be 10,000x times the size of earth (that is if 1 block = 1mx1m) and still run 60 fps....make the change xD
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #10 - Posted 2015-07-13 11:11:31 »

The bottleneck in such a game is always likely to be collision detection. so if you make the world larger but do not increase the number of entities, the game will actually go faster as there is less collision (more correctly, fewer collidables per grid square if you are using a grid-based broad phase collision detection). Would you get 60 FPS if your world included 10,000 x 7 billion people?

If you are culling the view to the visible tiles and there is minimal action happening off-screen , why would the world scale make any difference?

Offline Soulfoam
« Reply #11 - Posted 2015-07-13 17:40:42 »

I cant stand using java2D any more, i used to be in the same situation as you, but as soon as i switched over to LWJGL my games now run 1000% faster and a old freerome game i made can be 10,000x times the size of earth (that is if 1 block = 1mx1m) and still run 60 fps....make the change xD

You can't stand Java2D because it punishes you for the bad code YOU write...? Remarkable ;P. What you meant was you're able to write unproficient code and take less of a performance hit when using LWJGL.

You can have a game that uses Java2D for rendering and is 10,000x times the size of earth as well, and get 60 FPS.... if you're rendering just what's on screen, then your world size doesn't matter.

Of course LWJGL > Java2D, but not for that reason.
Offline Archive
« Reply #12 - Posted 2015-07-14 02:18:28 »

I cant stand using java2D any more, i used to be in the same situation as you, but as soon as i switched over to LWJGL my games now run 1000% faster and a old freerome game i made can be 10,000x times the size of earth (that is if 1 block = 1mx1m) and still run 60 fps....make the change xD

You can't stand Java2D because it punishes you for the bad code YOU write...? Remarkable ;P. What you meant was you're able to write unproficient code and take less of a performance hit when using LWJGL.

You can have a game that uses Java2D for rendering and is 10,000x times the size of earth as well, and get 60 FPS.... if you're rendering just what's on screen, then your world size doesn't matter.

Of course LWJGL > Java2D, but not for that reason.
I don't know if this is bad advice, but I think that new (or relatively new) Java programmers should refrain from using libraries because it doesn't reflect their programming ability at all. When you write things from scratch, you learn how to make efficient and fast code.

Offline unlight
« Reply #13 - Posted 2015-07-14 02:38:39 »

Java 2D is actually a really good place to begin to learn before moving on to something else.

This is how I  render only what is on screen..

When you create your map, pass it the width and height of your screen to determine how many tiles to render at a time, i.e...

1  
2  
3  
4  
public TileMap(int screenWidth, int screenHeight) {
    rowsToRender = screenHeight / tileSize + 2;
    colsToRender = screenWidth / tileSize + 2;
}


Notice that you add an extra two tiles, this is just a safety buffer so you don't see tiles appearing and disappearing as you walk. Now say if you are telling your tile map where the player is (the position variable in the below), then you can render only the tiles immediately surrounding the player using a render loop similar to the below.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void render(Graphics2D g) {
      int rowOffset = -position.intY() % tileSize;
      int colOffset = -position.intX() % tileSize;
     
      String mapString;
      int row, col;

      for (int r = 0; r < rowsToRender; r++) {
         for (int c = 0; c < colsToRender; c++) {
            row = r + position.intY() / tileSize;
            col = c + position.intX() / tileSize;
           
            if (isTileOutOfBounds(row, col))
               break;
           
            // if ((mapString = mapArray[row][col]).equals("NA"))
               continue;
           
            // row = charToInt(mapString.charAt(0));
            // col = charToInt(mapString.charAt(1));
            g.drawImage(tiles[row][col].tile, c * tileSize + colOffset, r * tileSize + rowOffset, null);
         }
      }
   }


In this method, the row and col offset describe how far along the current tile you are on, without this, the player will jump from tile to tile instead of moving smoothly across it, you can see it added to the draw method at the bottom of the loop. The loop will now render the number of rows / cols that we set in the constructor.

The commented-out lines are relevant to how I build and read my maps and will be different for you..

I hope this helps!
Offline VIrtueeL
« Reply #14 - Posted 2015-08-18 04:22:12 »

consider putting blocks into chunks so one chunk is 64 blocks so that's a 8x8 chunk

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Husk

Senior Devvie


Medals: 20
Exp: 3 years


Want to learn everything.


« Reply #15 - Posted 2015-08-18 04:33:10 »

Well this thread is a little old, but anyway.

Chunks aren't a solution to this problem, you'd only be doing redundant work. We know exactly what tiles needed to be rendered with the use of basic arithmetic, so there's no need for acceleration algorithms.

Chunks are more beneficial for loading in parts of the map at run time, where we can't store all the data in memory, and incur I/O costs. Like Minecraft, you could also use it for a procedurally generated world, as it'd perhaps be a strange thing to do to keep rescaling a spartial partitioning structure to the new size of the world, and certainly wouldn't want to be processing through data that isn't being used.

Offline Dane

Senior Newbie





« Reply #16 - Posted 2015-08-31 18:22:53 »

I had a similar issue when I wrote my first 2d tile based game. Here's the solution I came up with:

Separate the map into chunks (like said above) and give each chunk its own image. That chunk will have a flag called 'update' which is checked every time the game is drawn. If the chunk is due for an update, the image of that chunk will be recreated and the update flag will turn off.

That way instead of drawing 8x8 tiles through a loop, you're drawing a single image which is comprised of those 8x8 tiles.

The cons of this approach is if you want tiles your character goes under, then you will need multiple 'layers' for each chunk so you can render over the character. Also if you have tiles that change frequently (animated) then it might be considerable to add a system which causes only specific tiles to be redrawn to that chunks image.
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

Solater (449 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!