Slick2D: Initializing graphics object before game loop - java

I'm using the Slick2D Java game library and I want to set the scale of the graphics object that gets passed to the render method in my game loop. I'd like to do this outside the render method, so that I would only have to call the setScale() method once. I am implementing my game as StateBasedGame, so I'd to like to initialize my graphics object in my StateBasedGame class. Where would be the appropriate place to do this?

In the Init method, where you should be initializing your image. I can expand upon this, but your question seems to acknowledge that you already know how to scale the image.
EDIT: After rereading the question, the answer would be that you need to scale it every time you go into the render method of your gamestate object. This requires that you put graphics.scale(0.5f, 0.5f);
This is required, I believe, because the graphics object resets its scale every game tick so that you don't get a compounded effect as it calls the render method hundreds of times.

Related

Libgdx use ScreenUtils asynchronously and don't stop the game loop

I want to capture the screen. Libgdx provides some functions defined in the ScreenUtils class.
For instance final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
My problem is that it takes about 1-3 seconds to get that information. During that time the game loop is blocked and the user will define it as a lag.
Internally the ScreenUtils class, uses Gdx.gl.glReadPixels. If I run the ScreenUtils.getFrameBufferPixmap method in a thread, there is no lag, but it captures the screen at the wrong method, which is logical since the game loop runs and changes stuff.
Is it somehow possible to copy the GL context or save it and generate the Pixmap at a later point of time with the help of a Thread? Of course the copy/save operation should be done instantly, otherwise I don't spare anything.
I use ShapeRenderer to render my stuff onto the screen.
After little research I have found faster alternative to glReadPixels - Pixel Buffer Object which is available since Android 4.3 - https://vec.io/posts/faster-alternatives-to-glreadpixels-and-glteximage2d-in-opengl-es
It is not possible to call glReadPixels on other thread than render thread - https://stackoverflow.com/a/19975386/2158970
I have found a solution, which works in my case since I have only minor graphics / shapes. Basically I copy all the objects, which are drawn to the view, as described here. In a background thread I generate a Bitmap programmatically and use the information stored in my objects to draw to the bitmap.

Java Game moving real time(AP CS Final)

I'm taking the AP computer science class at my school. They never taught us about GUIs because the AP test didn't require you to know how to make one.
For our final project we wanted to make something like tron (game where bikes move around an are creating walls of light behind them in an attempt to crash the other player). Before we continue i just want to make sure we are going in the right direction. Should we use ImageIcon for the players or maybe something else?
We still have a lot to learn, but i thought this would be a good thing to start with. The reason why i'm asking is because i'm not sure if we would be able to move them without opening another window every time we want to move something.
This is probably a matter of opinion, but personally, I would use BufferedImage's as the primary image container.
The main reasons are:
They are easy to paint and easy to manipulate, you can actually draw onto them if you need to.
Loading a BufferedImage is done through ImageIO.read, it guarantees that the entire image is loaded before the read method returns and will throw an IOException if it can't read the image for some reason, which is better than ImageIcon which loads the image in a background thread and doesn't report errors if the image failed to load
Movement or placement of the images would be done, typically, by painting them onto an output Graphics context. This is (typically) done by overriding the paintComponent method of something that extends JComponent and using the passed Graphics context and Graphics#drawImage
Have a look at Performing Custom Painting, 2D Graphics and Reading/Loading an Image for more details

Java Snake Game -- Redrawing a specifc drawString() method

The problem:
I've been creating a Snake game in Java and everything is working smoothly. The only problem I've having is displaying the score. Each time the snake "eats" an apple, the score increases by 10. Instead of displaying the current score, it simply writes over the present score without actually erasing it. When I attempt to erase the content before re-displaying, I get an error and nothing appears, other than my background colour.
I have other Graphics components, including a paint() that I do NOT want affected.
My attempt (theoretical):
I display the score using a Graphics object that calls
drawString("Current score: " + currScore, 0, (Constants.TOTAL_HEIGHT + 15));
I figured I have to call a clearRect() method, which makes sense to me. Unfortunately, I get a NullPointerException at the line specifying clearRect().
I'm not using Graphics2D just so I can first be familiar with Graphics. If resolving this issue involves Graphics2D, I have no problem using it.
My attempt (the method in question):
public void displayScore(Graphics g) {
//clearScore.clearRect(0, getY(), getWidth(), getHeight());
//g.clearRect(0, getY(), getWidth(), getHeight());
g.drawString("Current score: " + currHighScore, 0, (Constants.TOTAL_HEIGHT + 15));
}
In the code, clearScore is a Graphics object declared in the class outside of any methods. My reason for using this was to avoid affecting the other graphics. I attempted each of the clearRect() lines and both produced the same NullPointerException, which is why I have them commented out.
(Constants.TOTAL_HEIGHT + 15) is responsible for assigning the Y-coordinate.
The Error Message:
Exception in thread "Thread-3" java.lang.NullPointerException
at com.nmw.view.CanvasFrame.displayScore(CanvasFrame.java:149)
at com.nmw.view.CanvasFrame.drawAll(CanvasFrame.java:43)
at com.nmw.view.CanvasFrame.run(CanvasFrame.java:64)
at java.lang.Thread.run(Thread.java:722)
drawAll is where I call all of my methods that take in Graphics to draw a segment of the game.
Apologies for the lengthy question =/
Your problem is that you are likely trying to draw with a null Graphics object. This usually occurs if you are not drawing within the paint(...) method if AWT or paintComponent(...) if Swing. The solution,
draw within one of those two methods (depending on the library you're using).
Always call the super's method within your painting method.
Or use a Label (AWT) or JLabel (Swing) to display the data.
To be blunt, your statement: "I have other Graphics components, including a paint() that I do NOT want affected." -- is an unrealistic requirement if this is an AWT project and if you don't want to use a Label.
And this statement:
In the code, clearScore is a Graphics object declared in the class outside of any methods. My reason for using this was to avoid affecting the other graphics.
Is not how you should do drawing, ever. You're finding out now why. If you happen to get a Graphics context during the running of the program, use it to assign Graphics to your class field, and then try to use that class field, you'll often get the NPE like you're seeing because a Graphics object thus obtained is not long-lasting.
If you need more in-depth help, please post your actual assignment requirements and more code. In particular your drawing methods. Best would be to create and post an sscce.
Edit
You state in comment:
My Snake game is an independent project I've been doing to learn more about Graphics. I've only been using the AWT library, however, I'll try using a Label. Thank you for your response and explanation of why I got the NullPointerException
If this is for your own project and not a school assignment, then I think that you will want to avoid using AWT and instead use Swing. It is much more powerful and flexible than AWT. There really is no reason why you'd want to use AWT now adays.

Optimizing Java Graphics

I have a custom UI drawn for my java application. Right now I draw the entire UI from scratch. I know for a fact some parts of the UI are static. I know I could save these static parts to an image, but will this yield a improvement in performance (Since even an image must be drawn every frame)?
Is it plausible to save a reference to the Graphics2D object after the UI has been drawn and assign that to the new graphics object every frame (starting from a point where all the static components are drawn)?
Thanks in advance,
Alan
You don't need to redraw everything in every frame. So if you have static parts of your UI (or even dynamic parts that you know haven't changed since last frame) then you simply don't need to repaint them.
In my code (Swing games and simulations mostly) I usually try to follow the following rules:
Pre-prepare static images (e.g. BufferedImage textures for UI elements)
Override the paintComponent() method for each UI element individually to do the painting
Only call the repaint() method of any given UI element if I know that something has changed
Call repaint() in a timer-based loop for animation, but only call it on the portion of the UI that is being animated (e.g. a nested JPanel)
This approach seems to work and perform pretty well (though I'd welcome comments if there are ways to improve it!!)
There are two main optimizations you can do here. The first is to make sure that when you cause your UI to be repainted, usually done by calling repaint, make sure you call the version of repaint where you specify a rectangle that has changed. Only make the rectangle big enough to encompass the parts that actually have changed, not the static parts. For this to be effective you also have to pay attention to the clipRect in the Graphics2D object you are passed in paint(). That is used by the system to tell you exactly what needs to be repainted, in the above case usually the rectangle that you passed to repaint. Don't paint anything that lies entirely outside that rectangle.
You can also get significant performance improvements by caching the static parts of your interface in an image. Writing an image is by far the fastest way of getting things onto a screen. My measurements indicate that small images are faster than even a few simple drawing primitives. However you need to make sure the image characteristics match the screen, by using createCompatibleImage().
Of course you may be using a lot of memory to get this speedup. I would recommend testing to see if you need to do image caching before implementing it.
if some parts of the screen is completely static, then never redraw that part. Don't do a full-screen/window clear, just clear the part of the screen/window that changes all the time.
This way, you don't unnecessarily redraw the static image.

How can I split up paint swing method in java?

I'm developing a fair sized hospital simulation game in java.
Right now, my pain method is starting to look a little big, and I need a way to split it up into different sections...
I have an idea, but I'm not sure if this is the best way.
It starts by painting the grass, then the hospital building, then any buildings, then people, then any building previews when building. The grass and hospital building will not change, so I only need to paint this once. The buildings themselves won't change very often, only when new ones are built.
I was thinking, use boolean values to determine which sections need repainting?
Ideal, id like to be able to split up the paint method, and then call each one when needed, but I'm unsure how to physically split it up.
I am still quite new to java, and learning on the go.
Thanks in advance.
Rel
Another idea is to create a super class or interface for all items that must be drawn on the screen. Lets cvall this class ScreenObject. You can then have a draw(Graphics2d g) method specified in the ScreenObject class. Next, each object that must be drawn implements the draw() method and is only concerned about drawing itself. You can even consider creating a variable that determines whether this draw method should be run at all.
In the main class that paints the screen you can have a reference to all ScreenObjects in an ArrayList and your paint() method will simply iterate over this calling draw() on each object.
I'm assuming from your description that your scene is split up into tiles. Keeping an array of booleans is a good way to keep track of which tiles need redrawn on the next update. A LinkedList might perform a little better in some situations. (I'm thinking of a Game of Life simulation where there are tons of tiles to redraw and you need to check each neighbor, so you may not need to go this route.)
Without seeing your code I can't give very specific advice on splitting up your paint method. I can tell you that in sprite animations, each sprite object typically has its own draw method that takes the main Graphics object (or more likely a buffer) as a parameter. Since the sprite should know its own image and location, it can then draw itself into the main image. Your paint method can then just loop through your list of sprites that need to be redrawn and call their draw method.
You might look to Killer Game Programming in Java for more detailed information.
Well I am not really an expert at programming but to split up my paint method Ive always just made a new method that takes a Graphics object and call that from paint, it has always helped me to keep my code organized but I have never had a big project like it sounds you are working on so it might not work for your situation.

Categories