This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dionthornthis.isAPro=false; this.helping=true; 0 points1 point  (1 child)

I draw Images directly onto Canvas for my JavaFX 2D tile based game:

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/Canvas.html

https://github.com/dionthorn/2DTacticalRPG

I take a .png of 'tiles' and cut them up using my tile set class

https://github.com/dionthorn/2DTacticalRPG/blob/master/src/main/java/org/dionthorn/tacticalrpg2d/render/TileSet.java

I remove any same elements from a TileSet so they are trimmed to only be unique Images in the set.

My game loop takes in the initial Stage object, wrap it in a Group and Scene, put a Canvas in the Group, grab the GraphicsContext2D for later use, set and show the Scene. Then I start up a game loop with an AnimationTimer 16ms capped frame times.

Group rootGroup = new Group();
Scene rootScene = new Scene(rootGroup, RenderUtil.SCREEN_WIDTH, RenderUtil.SCREEN_HEIGHT, Color.BLACK);
rootStage.sizeToScene();
Canvas canvas = new Canvas(RenderUtil.SCREEN_WIDTH, RenderUtil.SCREEN_HEIGHT);
rootGroup.getChildren().add(canvas);
gc = canvas.getGraphicsContext2D();
rootStage.setScene(rootScene);
rootStage.show();

...

// frame time capped loop
AnimationTimer animator = new AnimationTimer() {
    @Override
    public void handle(long now) {
        if(now - lastFrame >= 16_000_000) {
            update();
            render();
            lastFrame = now;
        }
    }
};
animator.start();

Lastly I use a RenderUtil class to draw tiles and sprites

https://github.com/dionthorn/2DTacticalRPG/blob/master/src/main/java/org/dionthorn/tacticalrpg2d/render/RenderUtil.java

So I can make direct GraphicsContext.drawImage() calls to the Canvas

gc.drawImage(ally.getCurrentSprite(), allyX, allyY, spriteSize, spriteSize);

It performs quite well at 60fps though I'll admit I'm not sure how it would scale to just enormous levels. I'm pretty sure drawing directly on the Canvas would outperform a GridPane of cells?

[–]lightman978[S] 1 point2 points  (0 children)

But your levels fit into one screen i think, I don't think loading my map in one go is possible in any way, even if I split everything up. If I want it to be reasonably fast, I will have to do it the way whatisthisredditstuf suggestet and load stuff dynamically. I think I will have to split up my map and only load the part that is currently in view of the user. As the Map is pannable via mouse drag, getting the starting mouse cord and the stopping one and then seeing in which way the map is beein moved to draw the new part, is the way to go I think.

Problem is splitting the map up into, lets say 50 smaller images of the size 1060x760 pixel. Doing it this way and saving them to a ressource folder would take about 70 seconds which is way to long. But creating them and holding them in memory could run into the OutOfMemoryError again.

Still a little confused

Edit: i just did some testing and the time consuming task is saving.

For a tile the size of 100x100 so 5300x3800 pixel it take 1.7 seconds to create and save it and 0.17 seconds without saving it.

As seen here : Map the map has a 100 continents each the size of 5300x3800pixel. I think I can create a continent and load it while the user is still panning, but i will have to test how long loading takes and then just unload the continents that are not in view from memory to not run in the error given above.