Introduction to animations in games

Source: Internet
Author: User

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Discuss newsgroups and documents

Preface

This article only introduces simple animation technology, mainly for the future release of more abundant animation performance, so do not look too high for this article. The full text is a little more technical than Hello world. After briefly introducing some animation concepts, a simple animation is implemented using C ++ and Java applets. For how to run a Java Applet on a webpage, refer to the previous article "deploy and run a Java Applet in the csdn blog". It should be noted that to see the Java Applet, you must install Java first and allow it in the browser.


What is animation? In fact, the simplest explanation is the motion picture -_-! We are studying animation in a computer. " Computer Animation: Animation and motion are inseparable. It can be said that motion is the essence of animation, and animation is the art of motion. Traditionally, animation is a technology and art that produces dynamic vision by taking a series of individual images on a continuous multi-grid film, this kind of vision is reflected by showing the film at a certain rate. In general, an animation is a way to dynamically generate a series of related images, each of which is slightly different from the previous one.
Computer Animation uses continuous playback of static images to produce the effect of motion of scenes, that is, the technology that uses computers to produce motion of images and images. The principle of computer animation is basically the same as that of traditional animation, but computer technology is used for animation processing and application on the basis of traditional animation, and can achieve the effects that traditional animation cannot achieve. Due to the digital processing method, the animation motion effect, screen tone, texture, and light and shadow effects can be constantly changed, and the output methods are also diverse ." -- From reference 1

"Basic Principles of computer animationBased on the motion control method, computer animation can be divided into real-time animation and frame-by-frame animation. Real-time animation uses algorithms to realize object motion. Frame-by-frame animation is also known as frame animation or key frame animation, that is, the effect of motion is achieved by displaying the image sequence of the animation at a frame or frame. Based on different visual spaces, computer animation can be divided into two-dimensional animation and three-dimensional animation ." -- Also from reference 1

We are mainly studying real-time animation. In fact, the simple point is the animation calculated in real time, instead of the original one. Now we use it for release. However, in terms of the effect, we still play one frame at a time, but each frame is calculated in real time. The number of frames played per second is the FPS (frames per second). Generally, for computer games, 30 frames per second is the bottom line, and 60 frames are the ideal realm, for mobile games, there are also 20 frames. If the FPS is too low, it is easy to jump and pause, that is, what I often say is "Putting slides" (I don't know what others often say ). In fact, it is an everlasting topic for game programmers to achieve a better picture of the game as much as possible within the limits of hardware conditions and maintain a higher FPS.

In fact, because of the animation principle, the program used to depict the animation (the game is also calculated, the following is slightly) is very different from the ordinary program, normal programs often wait for the user to input before responding. They can be idle at ordinary times, but the animation does not work. Each frame has to be actually computed and drawn. Therefore, the structure of the program is not the same as that of a common program. First, you can use the pseudo code extracted from the game framework in "Windows game programming Masters' skills" to see what an animation program is about.

// Initialize game here
Game_init ();

// Enter main event Loop
While(True)
{
// Test if there is a message in queue, if so get it
If(Peekmessage (& MSG, null, 0, 0, pm_remove ))
{
// Test if this is a quit
If(Msg. Message = wm_quit)
Break;

// Translate any accelerator keys
Translatemessage (& MSG );

// Send the message to the Window Proc
Dispatchmessage (& MSG );
} // End if

// Main game processing goes here
Game_main ();

} // End while

// Closedown game here
Game_shutdown ();

// Return to Windows like this
Return(Msg. wparam );

IntGame_main (Void* Parms = NULL,IntNum_parms = 0)
{
DWORD dwstarttime;

Dwstarttime = gettickcount ();
// This is the main loop of the game, do all processing here

// For now test if user is hitting ESC and send wm_close
If(Keydown (vk_escape ))
Sendmessage (ghwnd, wm_close, 0, 0 );

// Another key down is test here

Sceneshow ();

// Control the Frame Rate
While(Gettickcount ()-dwstarttime {
Sleep (1 );
}

// Return success or failure or your own return code here
Return(1 );

} // End game_main

The above is the core element required by an animation program (actually a game program). The main part is the calculation, display cycle, and, the cycle frequency needs to be controlled (the FPS displayed is actually controlled), and user input must be accepted in the game program. The most important thing is that, even if there is no input, the program is still in a loop, which is very different from the message driver of the Windows program. In general, the animation playing is related to the frame, that is to say, frame-based animation is also used in the game to determine how to draw frames. It is also called frame-based animation, in essence, a game is an interactive animation that accepts player input. The two methods have the same effect on FPS stability and differ in FPS instability. In general, frame-based methods are easier to implement, time-based games can use different frame rates for different platforms.

The above framework is widely used in my blog to learn OpenGL code. You can see the actual example by clicking the source code below. Many OpenGL examples use the above framework and draw animations. No more examples are provided here. Many examples of the Win32 OpenGL series are as follows.


Java Applet Animation


Import java. Applet. Applet;
Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. graphics;
Import java. AWT. image;
Import java. util. date;

Import javax. xml. crypto. Data;

Public
Class appletanimation extends java. Applet. Applet implements runnable {
Int frame;
Int delay;
Thread animator;

/**
* Initialize the applet and compute the delay between frames.
*/
Public void Init (){
Int FPS = 30;
Delay = (FPS> 0 )? (1000/FPs): 100;
}

/**
* This method is called when the applet becomes visible on
* The screen. Create a thread and start it.
*/
Public void start (){
Animator =NewThread (this );
Animator. Start ();
}

/**
* This method is called by the thread that was created in
* The start method. It does the main animation.
*/
Public void run (){
// Remember the starting time
Long TM = system. currenttimemillis ();
While(Thread. currentthread () = animator ){
// Display the next frame of animation.
Repaint ();

// Delay depending on how far we are behind.
Try{
TM + = delay;
Thread. Sleep (math. Max (0, TM-system. currenttimemillis ()));
}Catch(Interruptedexception e ){
Break;
}

// Advance the frame
Frame ++;
}
}

Public void paint (Graphics g ){
G. setcolor (color. Black );
G. drawstring ("frame" + frame, 0, 30 );
}

/**
* This method is called when the applet is no longer
* Visible. Set the animator variable to null so that
* Thread will exit before displaying the next frame.
*/
Public void stop (){
Animator = NULL;
}
}

Follow the above ideas to create a loop, FPS control, display update, and OK for the Java Applet, And the animation display is completed. The only difference from C ++ is that the driver is no longer an endless loop, but a new thread, and other elements are completely unchanged. See Article 2 "animation in Java applets" as a complete animation example.

Examples of other languages: in fact, there are examples of OpenGL, QT, Windows GDI, and small basic animations in my blog. I will not list them here. Search for them by myself, in fact, it is similar to understanding the idea of animation formation.

Finally: in fact, this article is intended to talk about more and finally dragged on for too long, so the grass and grass are over, far from finishing the content originally intended to be done. at this time, Google document cannot be published, but it is still released to wlw. Why is wlw available? Is Google Doc suddenly unavailable? Since the launch of the Meta API after the Spring Festival, csdn is helpless.

Reference: I. Computer Animation basics http://www.telecarto.com/content/maincontent/multimediadesign/Animation/cha_713.htm-about the concept of animation many 2, "animation in Java applets", very detailed Java Applet tutorial, from simple text animation to a slightly complex animation, it is easy to understand and has rich examples.

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

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.