Windows Game Programming Master Skills (Second Edition) Learning Road-1

First of all, I have a Chinese pdf resource of "Windows Game Programming Master Skills (Second Edition)". Students who need it can ask me for it. I will also upload it to the csdn resource, but it is estimated that points are required, so students who do not have points can ask me for it, it is completely free!

Windows Game Programming Master Tips (Second Edition)_Chinese version.pdf: https://download.csdn.net/download/yld10/10392882

This series is mainly about some points that I have recorded that I think are important, and the writing may be a bit scattered. If students who have not read the book come to see these knowledge points, I am afraid that they are a little unclear, so I suggest that you still read pdf or paper books. . This series can only be regarded as a little note when reading the book.

///////////////////////////////////////////// //////////////////////////////////////////
Start recording below
/ ///////////////////////////////////////////// /////////////////////////////////////////

Chapter 1 Learning is boundless

Windows Game Programming Master Skills (Second Edition) Chapter 1 Learn the boundless directory structure

ps : To be honest, the author of this book is quite funny (he came to the book to collect debts from his friends...), I think he writes very carefully, always considers the readers, and also gives a lot of pertinent suggestions, which shows that The author has profound programming skills and rich experience.

Here are some of the things mentioned in the book:

General game loop structure
Windows Game Programming Master Skills (Second Edition) Chapter 1 Figure 1-4

State transition diagram of a game loop (finite state automaton)
Windows Game Programming Master Skills (Second Edition) Chapter 1 Figure 1-5

Here are some of the techniques the author mentioned in Chapter 1:

1. Don't be afraid to use global variables, many video games don't let time-critical functions take parameters, but instead use some global variables to pass parameters. For example, the code for a function is as follows:

void Plot(int x, int y, int color)
{
// plots a pixel on the screen
video_buffer[x + y*MEMORY_PITCH] = color;
} // end Plot

Since the parameters are pushed and popped from the stack, the time required to execute the function body is less than the time required to call the function. In this case, a better approach may be to set up some global variables, and then assign values ​​before the call to Pass parameters as follows:

int gx, gy, gz, gcolor; // define some globals

void Plot_G(void)
{
// plot a pixel using globals
video_buffer[gx + gy*MEMORY_PITCH] = gcolor;
} // end Plot_G

2. Use inline functions. You can even improve on the previous tip by using the inline directive to get rid of function calls altogether. The inline directive instructs the compiler to replace function calls with function body code. Doing so will undoubtedly make the compiled program larger, but it will effectively increase the speed of execution. Here is an example:

inline void Plot_I(int x, int y, int color)
{
// plots a pixel on the screen
video_buffer[x + y*MEMORY_PITCH] = color;
} // end Plot_I

Note that globals are not used here, as the editor effectively enforces the same type of data aliasing, but globals are
still useful, especially if only one or two arguments change values ​​when the function is called—the rest are old Values ​​can be used without reloading.

3. Try to use 32-bit variables instead of 8-bit variables or 16-bit variables. Pentiums and newer CPUs are all 32-bit, which means they don't like 8- or 16-bit data words. In practice, smaller data may slow things down as cache and other associated internal memory addressing becomes less regular. For example, you define a struct type that looks like this:

struct CPOINT
{
short x, y;
unsigned char c;
} // end CPOINT

Note that it looks nice to define this structure, but it doesn't! First, the structure itself is a 5-byte long structure one (2*sizeof(short) + sizeof(char)) = 5 bytes. That's too bad, since you don't pay attention to byte alignment, you can have big problems with memory addressing. A better structure is as follows:

struct CPOINT
{
int x, y;
int c;
} // end CPOINT

Hint: Structs in C++ are very much like classes, except that the default accessibility of structures (Visibility) is PUBLIC.
This new structure is much better. First, all structure members have the same size - sizeof(int) = 4 bytes. Therefore, any structure member can be accessed through incrementing DWORD (double word, 2 bytes) boundaries with only one pointer. The size of this new structure is (3*sizeof(int)) = 12 bytes, a multiple of 4, or on a DWORD boundary. This will significantly improve performance.
In fact, if the reader really wants to be on the safe side, all structures can be properly padded to be a multiple of 32 bytes in size. 32 bytes is the standard internal cache width on Pentium family CPUs, so this is an optimal length. This requirement can be satisfied by filling the structure with useless variables or by using compiler directives (the easiest way). Granted, doing the padding wastes quite a bit of memory, but the speed gain is often worth it.

4. Comment out your code. Game programmers are notorious for not commenting code. Don't make the same mistake again in order to get neat. With well commented code, a little extra typing is definitely worth it.

5. Program in a manner similar to RISC (reduced instruction set computer). In other words, try to simplify your code, not make it more complicated. Pentium-class processors especially prefer simple instructions over complex ones. Your program can be longer, but try to use simple instructions to make the program simpler than the compiler. For example, don't write a program like this:

if ((x+=(2*buffer[index++])) > 10)
{
// do work
} // end if

Instead it should be written like this:

x += (2*buffer[index]);
index ++;
if (x > 10)
{
// do work
} // end if

There are two reasons to code this way. First, it allows the debugger to place breakpoints between parts of the code; second, it will make it easier for the compiler to pass simple instructions to the Pentium processor, which will allow the processor to use more execution units to process more in parallel Code, complex code is worse in this regard!

6. Use binary shift operations to perform simple integer multiplications where the multiplier is a power of 2. Because all data is stored in binary in a computer, shifting a group of bits to the left or right is equivalent to multiplying and dividing, respectively. E.g:

int y_pos = 10;
// multiply y_pos by 64
y_pos = (y_pos << 6);  // 2 ^ 6 = 64
相似的有:
// to divide y_pos by 8
y_pos = (y_pos >> 3);  // 1/2 ^ 3 = 1/8

You'll find more of these techniques in the chapter on optimization in this book.

7. Design efficient algorithms. No assembly language can make an O(n^2) algorithm run faster. It is better to use clear, efficient algorithms rather than brute force and exhaustive algorithms.

8. Don't optimize code during programming. This is usually just a waste of time. It is recommended that you wait until the main block of code or the entire program is complete before starting the heavy optimization work. Doing this will ultimately save you time because you don't have to do unnecessary optimizations on some obscure code. When the game is basically complete, it's time for performance testing (profiling) and finding problems that need to be optimized. On the other hand, the program code should be careful not to write in a disorderly manner.

9. Don't define too many complex data structures for simple objects. The linked list structure works great, but that doesn't mean you should use a linked list when all you need is a fixed array of about 256 elements, just statically allocate memory for it. 90% of video game programming is data manipulation. So data should be as simple and visible as possible so that it can be accessed quickly and manipulated at will. You should make sure your data structure is suitable for the problem you are really trying to solve.

10. Use C++ with caution. If you're a seasoned C++ expert, just do what you want, but don't write crazy classes or overload everything. After all, simple and intuitive code is the best program and the easiest to debug. I personally don't want to see multiple inheritance in game code!

11. If you know your car is about to hit a rough gravel road, the best thing to do is to stop, turn around and take a detour. I've seen a lot of game programmers go down a bad programming line until they're buried in bad code. It's far better to be able to realize your mistake and rewrite 500 lines of code than to write a code structure that's always unpleasant. So if you find a problem at work, reassess and make sure the time you save is worth it.

12. Back up your work often. When writing game code, you need to lock down the code in your codebase fairly frequently. It's easier to rewrite a sorting algorithm, but much harder to rewrite character AI or rewrite collision detection.

13. Before starting your game project, you should do some organizational work. Use sensible file and directory names, come up with a consistent variable naming convention, and try to use separate directories for graphics and sound data instead of putting everything in the same directory.

use tools

1. C/C++ compiler –> VC++ 6.0
2. 2D art software –> Corel Photo-Paint
3, sound processing software –> Sound Forge 4,
3D modeling software –> 3D Studio Max
5, music and MIDI sequencer –> I have already found all the above tools and resources of Cakewalk Sonar
use tools
. Students who need them can ask me for them, or go to the resource area to download them.
vc++ 6.0: https://download.csdn.net/download/yld10/10395122
Sound Forge 9.0a: https://download.csdn.net/download/yld10/10395126
The remaining three software are too big for me to upload.
But don't be discouraged, I also prepared a big gift package for the whole family. There are 5 softwares, but they are just put into Baidu cloud sharing, link file: https://download.csdn.net/download/yld10/10395144

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644592&siteId=291194637