10 basic principles that should be adhered to in Java Game Development

Source: Internet
Author: User

Keyword: Java Optimization Efficiency awt swt swing Simplified Drawing Listening Game Development GC draw

 

Two fabricated concepts involved in the article:

I. plotting tool: As we all know, Java GUI uses paint to draw images and repaint to refresh images. The drawing component is used to complete the repaint and painting coherence process, I call it a plotting tool. In my personal experience, the time for the painter to call should always be before the painting after the repaint, that is, the refresh is triggered by the repaint and executed. When the specific logic completes the corresponding image painting, then, insert the image into the painting through the unified interface. to match the image, the plotting tool should always be implemented through the interface.

2. Listener: The Listener here is not a specific listener component, but a set of all listeners required for JAVA games. Because JAVA games are likely to switch between different game modes, the mouse or keyboard events to be handled in different models are also different. Therefore, in Java game development, we need a set of replaceable listeners to change different listening events in different game modes. in order to match the needs, listeners should always be implemented through interfaces.

Body of text: 10 basic principles that should always be adhered to in Java game development:

1. Always maintain the uniqueness of the canvas.

In real life, the nutrients taken by humans through the oral and digestive tract can be absorbed by the internal organs of the heart, liver, spleen, lung, and kidney, but no one wants to give his or her own heart, liver, spleen, lung, and kidney a mouth, because it is enough to implement a consistent function. However, sometimes we add and remove different panels or canvas in the game to convert the screen, it is nothing more than an unwise move to give the game the heart, liver, spleen, lung, and kidney to the mouth. You should never draw out the Java GUI. It is better to re-paint it, there is no need to switch components, otherwise it is effort-consuming.

2. The views and logic layers must be separated from each other, and the monitoring and Image Rendering operations are always performed using the interface method.

For some mixed-type games, such as SLG + AVG and RPG + STG, we will face the problem of switching the game listener and plotting devices in different modes.

At this time, the simplest choice is to create a corresponding panel for each game type for switching, so although the surface above the worry, but it is also the most laborious and thankless, not to mention that the flash problem needs to be solved independently, and the resource occupation problem, the light redundancy code is enough for a headache.

The second is to use switch judgment for different game types in a single panel to switch between listening and drawing. When there are few events, the efficiency is good, but a little more, I am afraid it is not that simple, we recommend that you do not use it if you are more depressed.

As far as I can see, the best way to solve this problem is to follow the MVC mode and build the plotting machine and listener in the interface mode. When the game changes, we only need to switch the listening and drawing interfaces to quickly change the game content without having to treat different implementations differently. This avoids the flickering and delay of component switching and simplifies the code, it is more conducive to the division of modules during development.

3. Always Load Common game resources in static mode, cache common objects, and release useless resources in time.

Even though the history has developed to this day, Java still does not completely get rid of the yarn of its system resource killer. GC mechanism also causes us to be unable to release resources in due time. The more new, the system slows down, especially for games that use large amounts of graphics resources. Therefore, we need to do everything possible to make common resources static as a unique instance to avoid repeated calls, and to quickly null some resources that will not be used or rarely used after the call to wait for the GC to automatically recycle. Otherwise, you will find that your game is so close to memory overflow ......

4. Start the game in a loop mode and use threads to control the game process to avoid freezing.

In fact, the so-called game development is, to some extent, a magic program made by a programmer mixed with various graphics algorithms to display various resources in a timely manner. The only difference is that, the human-machine interaction is weak in the general magic lamp program, while the human-machine interaction of the game is strong.

As we all know, no matter how you jump to the display page in the display of the magic program, there must be a fixed begin and end page, and it is bound to be able to repeat the begin and end order from start to end, to form a slide.

In fact, the same is true for game production. No matter how the game process changes, the game will inevitably have a main process, or a main cycle, so that we can start from the game to the end of the game, rather than from one end to another, that is to say, regardless of the number of details in the game, the main process processing and determination must be sequential. For this feature, we should take the game subject code into a large loop body, and then use the thread to control the game progress in the loop body, so as to better adapt to this process. To put it simply, we should regard every graph used in the loop body as a timeline in flash, and various thread controls as a timeline, it is used to adjust the playback speed and call time of different shards to complete various event interactions.

5. Always prepare textures directly when processing complex drawings instead of being drawn by the program.

We all know that Java plotting is actually implemented by GDI, so the efficiency of drawing complex images can be imagined. In general, unless the current effect is not implemented by programming, or the resource loss caused by it is indeed small to negligible, the best way is to change the efficiency of space, prepare the picture and paste it directly. I 'd rather increase the size of the program, and do not ask the players to greet your eight generations with the anger of waiting.

6. Always ensure that repaint only refreshes the required part to avoid unnecessary global repainting.

Each repaint operation is actually to print the image in the painting to the form once. The larger the form, the more complicated the processed image, and the more resource losses caused by repaint, the operation efficiency is bound to be lower. However, because Java allows us to limit the repaint range, we can limit the refresh to a specific region, to be more accurate, we can refresh the image only when the image needs to be changed to minimize the loss. In general, even if it takes a little time to compute the refresh area, it is much faster than the global repaint.

7. Always double buffering game images to avoid flickering.

For Java plotting, the entire screen is cleared every time the repaint method is called, and then the painting displays the screen. If the system speed is not enough and the system is visible to the user at a short interval between background clearing and Image Rendering, the so-called flickering phenomenon occurs. Simply put, the cause of the blinking is the low computing efficiency, this causes inconsistency between repaint and paint.

In this case, we need to use the dual-buffer technology to solve the problem.

The dual-buffer implementation is actually extremely simple. The main process is to first create an image of the same size as the image to be drawn, and then obtain its graphics, every time we draw a painting, we do not directly draw the image on the graphics of the painting function, but on the graphics of the buffered image we create, after the painting is complete, call the drawimage method provided by the paint function to draw the entire background image to the screen at a time. The advantage of this method is that most of the painting is performed in the background. Draw the background image to the screen at a time.

At this time, as long as the system speed is normal, we can see that the drawing will no longer flash.

8. Always apply AWT or SWT in a self-painted component desktop game, rather than swing.

As we all know, the swing (jfc) GUI is drawn on a local form based on AWT. Although AWT provides richer components, it also occupies more resources. In fact, most Java Desktop game components are drawn by developers. They are not provided by the swing library or supported by the swing library, we can choose AWT or SWT instead of Swing (SWT plot and AWT/swing plot are slightly different in method, but essentially the same, the old man does not need to work with swing, which consumes the system resources that are scarce due to the use of Java applications.

9. Never forget to dispose after graphics completes processing.

The dispose of graphics is the same as the close of the database connection. For this reason, I made a special experiment. In the endless loop, there was no interval, No optimization, and repeated repaint a large image of X2000. Although the refresh was slow and accompanied by flashes, it was generally normal, after removing dispose and running it for about one minute, all evil overflows ......

Of course, just as the connection should be closed only after all operations are completed, graphics only needs to dispose after drawing all, that is, after the last draw of the paint, don't forget to leave a dispose to close the door, unless you really want to see the overflow ......

10. Always give priority to computing efficiency. You can discard code readability and violate the OO principle.

The biggest problem with game development using Java is the loss of system resources. on key issues, don't let it go.


------ Gorgeous split line ------

If you can always stick to the above ten points, although you don't expect it to surpass the running efficiency of C/C ++ games, you can compete with Delphi games with a clear conscience, proud of games developed by tools such as VB6, Flash, rmxp, and rmvx (-_-| ).

However, it is easy to do it, and I have not done it in some cases. Please do not learn from me because of inertia ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.