Preliminary Research on computer game production (also from the Internet copy, the knowledge inside is very basic)

Source: Internet
Author: User
Windows Platform
Windows in programmer's eyes

In the eyes of programmers, Windows is very different from ordinary users-not fast and convenient, but rather troublesome and annoying. Especially during the DOS editing process, Windows is very awkward and has many strange concepts. When windows was just introduced, almost all the amateur programmers were eliminated, because programming became quite complex and much more complex than DOS.

When the program is complex or too complex to a certain extent, no matter how good the brain can afford it. I read a foreign author in a Windows programming book and wrote: "When programmers write a Windows program, they usually write code from the past and rewrite it, instead of re-coding the code, because it is too troublesome, no one can remember so many things." Indeed, if you use VC and call WIN32API to write a standard windows program, you will feel that you like VB. Because VB does not write the form1 form generated by a line of code, there are hundreds of lines of code written in VC.

However, my impression on Windows is the APIs (application interfaces) provided by a bunch of dynamic link libraries ). There are thousands of APIs. You have to remember it! But you can only use it to write a game. Who makes Microsoft Windows everywhere.

Windows program concepts

First, we should talk about the event-driven mechanism. In the past, programmers wrote code on their own to accept users' buttons or mouse information, and then analyzed the keys pressed by the user to determine how the program runs. Now windows has done this for us. All we need to do is wait for Windows to send a message to your application and tell you what button the user has pressed for your program, or other messages. This is done to solve the trouble of multi-task. Imagine if windows runs 100 programs at the same time, and these programs are desperately trying to determine what button the user presses, it will be a mess, and it will also waste the CPU to calculate the same problem, therefore, these tasks are handled in a unified manner by windows, which is responsible for determining the buttons that the user is pressing. For example, if windows is running three programs at the same time and the user clicks the first program, Windows will convert the event into a message to tell the first program, the first program will know that the user is using it, and the other two programs will not know this event, so there will be no response. This is the event-driven mechanism. All the program has to do is wait for the user to operate the event and then handle it accordingly.

The second point is the message. Although the user's operation event is converted to a message by Windows, it does not mean that the message is only an operation event. There are many other types of messages that can be used to deliver information to programs. For example, when Windows wants to shut down the system, it will send a message to all running applications, so that everyone will see that the applications are automatically closed. As you know, when you close the system of wordstore, a dialog box is displayed prompting you to save the document. In fact, this is the message sent to word when Windows is about to close the system, word is afraid that the document you are editing will not be saved to prompt you whether to save the disk, instead of checking when Windows will be closed at any time.

Another concept is the callback function, which is called callback in English. It means that other programs call your function, rather than calling other functions. However, programmers in DOS may have never heard of this concept because the DOS system does not call the function you write. Some may have seen window processing functions and enumeration functions on some Windows systems. In fact, they are all callback functions. Callback functions are widely used. For example, when a message is sent in windows, the callback function you wrote is called to send the message. In fact, this function is called, then, the message is passed to the function through function parameters. Therefore, the function that receives the window message is also called the window processing function, which is only an application of the callback function. The enumeration function mentioned above uses the callback function for enumeration. In DirectX, the enumeration (enumeration) display mode is implemented using the callback function. DirectX sends all the display modes supported by the system to the callback function, instead of calling DirectX to check all the display modes supported by the system.

How to build a Windows program

Most programs use Windows as interfaces, so applications are usually closely integrated with windows. How to compile an application that only has windows but does not have any functions is an entry to learning to write Windows programs. It is a standard windows program, by understanding it, you will know how Windows works.

First, we should use the service functions in WIN32API to create a window, then write a window processing function for the system to call, and finally write a message loop to retrieve messages at all times. None of these steps is required. If a window is not created, the message loop and window processing functions are useless. If a window processing function is not written, the window cannot be created; if the message loop is not written, the messages sent to you by Windows will not be retrieved, and the windows on the screen will be like dead ones. No response is made to the point or pull, if the window processing function does not process a message, a similar phenomenon occurs.

As shown in:

This is the approximate structure. The application is always Retrieving messages, and then sending the messages to the window processing function for processing. To be exact, Windows does not actually directly send messages to the callback function of the application. Instead, it puts the messages into the message queue of the application and waits for the message loop to fetch them. This message queue is created along with the establishment of the window. It can store a part of the message or prevent the loss of the message. When a message is retrieved, the function that retrieves the message calls the window processing function to process the message.

Windows multitasking

A notable feature of Windows is multi-task, which means it can run multiple programs at the same time. This feature brings us great convenience. We can write articles, listen to music, and output articles on the printer at the same time.

Multi-task can make every program mistakenly think that it excludes the machine, because Windows does not give control of the machine to the program when executing a program, as in the previous dos, instead, the CPU is handed over to the program running in turn, so that every program can be executed. In fact, Windows only assigns a small amount of time to each program to use the CPU, so these programs seem to be running at the same time. This is how multitasking works. However, when Windows processes too many things, it will become very slow.

In the early days, Windows used a kind of "non-preemptible multitasking". The principle of this multitasking was that the windows system clock periodically interrupted a task and executed the next one, that is to say, it gives the same time to every program. Currently, Windows uses "preemptible multitasking". The difference between this multitasking and earlier versions is that Windows will run the programs it runs with a higher priority, windows assigns it a longer running time. Generally, Windows sets the user's programs to a high priority. Therefore, the user thinks that the programs he runs are fast, that is, there is no big impact on the running programs. The "non-preemptible multitasking" slows down when there are more programs, because it allocates a lot of time to programs that are useless to users.

Understanding of graphic hardware
Structure of the video Subsystem

The quality of the video system directly affects the performance of the computer. Many people even think that the picture he sees is everything on the computer. Others think that the computer is the monitor. Although this is absurd, it also reflects the position of visual images in people's minds.

Generally, video subsystems can be replaced, because most of them are what we call video cards, which are also integrated into the motherboard. It mainly includes display coprocessor and Display memory (Display memory), display coprocessor and CPU are connected with extended bus. The display coprocessor on the video subsystem also has its own set of bus, through which it can quickly access the above video memory, just as the CPU accesses the system memory. It is also an independent system, so it is called a video subsystem. Due to hardware design, the CPU can access the video memory of the video subsystem through the bus, but the display coprocessor cannot access the system memory on the motherboard. However, the speed of the bus is far from enough. When the CPU and display coprocessor perform a large amount of data throughput, the bus becomes abnormal and congested. To solve this problem, the AGP structure is generated. This structure connects the display system directly with the system memory, that is, it bypasses the system bus, this not only relieves the pressure on the bus, but also solves the problem of data exchange between the CPU and the display coprocessor. In addition, the speed of data transmission with AGP is much faster than that of the bus.

How images are displayed

The first thing we need to talk about is a display. It works similar to a TV set at home, except that the indicators and structures are somewhat different. It mainly includes a photo tube, a electron gun, a deflection coil, and a control circuit. It scans images based on computer images. In order to display color images, the electron guns are all made into three, namely red guns, green guns, and blue guns. The electrons they emit correspond to various red, green, and blue luminous points on the screen respectively, we can see the light when the electronic hits the light on the screen. When the image is displayed, the electron gun quickly sweeps the electron to display the corresponding image, and then the deflection coil begins to play its role. When it comes to coils, everyone will always think of electromagnetic. That's right, electrons are shifted to various positions on the screen only under the magnetic field of the deflection coil. If the deflection coil of the monitor is broken, we will see a very bright point in the middle of the screen. What is that? All the images were hit by the electron gun. Because the electron can no longer be bent, the electron gun then shot the electron at the center of the screen.

Each of the three electron guns has a voltage of 256, so the three gun combinations can display 16777216 colors, that is, what we call true color.

Image Data Representation

The data representation of an image refers to how to use the data to represent the image. By knowing the details, you can modify the image to produce a variety of image effects, for example, you can manually write a gradient or something.

The simplest is a real-color image, which is represented by 24-bit binary. RGB (red, green, and blue) occupies 8 bits each, that is, one byte, it is well-handled for programming languages that are especially not good at bit operations, and can be directly modified using specialized byte variables. 16-bit RGB is troublesome. It is usually in the 565 or 555 format. 565 represents red with 5-bit binary, 6-bit binary represents green, and 5-bit binary represents blue. 555 (also known as the 5551 format) and 565 mean the same, but it uses 5 bits to represent the green. In this case, binary operations are required to extract the RGB values separately. Because RGB is not a complete byte, it is difficult to perform operations. However, software development usually uses 16-bit color. for computers, it is faster than 24-bit and 32-bit images, and images of the same size are displayed, 16-bit color takes less space than 24 and 32-bit. Although 16-bit image rendering is a little more troublesome, most developers chose it to speed up.

The 32-bit color is a blind area for the majority of users. Many people believe that it is more colored than the 24-bit color, therefore, they usually set their windows to 32-bit colors. In fact, this is slow and useless. 32-bit colors only show 8 more than 24-bit colors. The first 24-bit and 24-bit colors are the same, while the remaining 8-bit colors are useless, it is used as a flag or other usage. Generally, 32-bit colors can be used in 3D operations, while 2D plotting or playing 2D games cannot be used because it is too slow.

Color palette

Let's talk about the color palette image separately because it is different from the display principle above. The color palette image is displayed in 8-bit RGB, which, unlike the above, uses 8-bit to represent the color index of the color palette rather than the real color. Because the true colors are in the color palette, the color palette image does not have bitwise operations in 16-bit colors to extract RGB colors, but it is often confusing to developers, the color will be messy if the color palette is used. However, in our old game consoles and old games, it earned a lot, because in the time when the hardware was expensive, the color palette was one of the most memory-saving display methods. Therefore, the images of game consoles such as MD and FC were poorly colored at the time, but with the development of technology, the color palette was basically abandoned by developers.
The use of the color palette depends on the software developed. If you want to develop a software with a low color but fast speed, the color palette method should be your best choice, because it is much faster than other display methods, such as fighting games and action games can all be displayed with a palette.

The following is the palette:

The data in the video display is the image to be displayed, but the true color value in the image is read from the color palette. In fact, the color index in the color palette is stored in the video display, therefore, a palette image cannot be displayed without a palette. You can also change another color palette for the same image, such:

The left side is a normal palette image, and the right side is a changed palette image, which can change the color of a game role's clothes, that is to say, we often use different colors to distinguish the same character in fighting games.

Tearing

Everyone should have seen the phenomenon of tearing, as long as you have a little bit of attention, you can see that some software will produce this phenomenon. In fact, the tearing phenomenon is the case where the image is misplaced when playing a game or watching a video. This microphone's beard is a tear that occurs during high-speed playback. The tearing phenomenon is a common problem for game developers, but it is not difficult to solve it. Next I will introduce how it is generated.

In terms of senses, the image we see on the display is actually a data area in the display memory. The size of this data area depends on the display mode, and DirectX is called the main graphic area. When running a game, the game needs to write the images on the background display page into the main graph, so that we can see the game image on the display. Not all of them are directly written into the main graph, but we have to constantly refresh the new main graph if we want to see continuous high-frequency animations. The problem lies in refresh. In fact, there is also a process for the display of images, but it is too fast. Generally, the display frequency ranges from 60Hz to Hz. In this way, the display is scanned by a row with a electron gun. The tearing phenomenon is between the refresh on the main graph and the refresh on the display, because the refresh is not synchronized. As shown in, the monitor is scanning the small ball in the main graph.

Step A shows that the monitor is scanning the main area but is scanning about 1/3 of the area. At this moment, the main chart is refreshed into the data that moves the ball to the right, which produces the phenomenon in step B. At this time, 1/3 is already the image in step a, but you need to continue reading the remaining 2/3 of the data in the main graph, however, this 2/3 of data is different from the image to be drawn in step a, so the tearing phenomenon is generated.

It is not difficult to solve this problem, as long as you know when the display's electron gun will be shut down and start scanning again from the upper left corner. The professional point is to say that the vertical blank (wait for vertical blank ), this prevents tearing and achieves synchronous display.

Related Article

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.