First, let me talk about the most intuitive feeling of C ++! I was familiar with the. net Smart tip and found that C ++ was not prompted at first. Later, google downloaded a visual assist plug-in, which is much better than the vs automatic prompt. Then, we get used to writing all the statements and methods in. net in the same file. But this is not the case with C ++. He implements a declaration in the header file in the source file, to be honest, and how he gets used to it at the beginning. Later, I got used to it. Then, writing a C ++ file is really fucking painful. It is no better than. net. Microsoft has encapsulated it than you. In C ++, many things need to be written by yourself. First, a destructor needs to release resources on its own. In. net, a gc is automatically used for garbage collection, and the resources cannot be released. No way. You only need to release it and be a good programmer who complies with the rules. This is my most intuitive feeling for C ++.
Let's talk about this super-Mary game. Let's take a look at the classification of the game's category structure. If there is anything wrong with it, please correct it.
In terms of hierarchies, it is divided into the following layers: ① image layer, ② logic layer, and ③ structure and table.
The image layer includes ① image base class MYBITMAP, ② game background MYBKSKY à MYBITMAP, ③ Game Image MYANIOBJ à MYBITMAP, and ④ magic attack MYANIMAGIC à MYBITMA.
The logic layer includes ① game logic GAMEMAP, ② clock processing MYCLOCK, ③ Font Processing MYFONT, ④ tracking and printing FILEREPORT, and ⑥ players controlling MYROLE à MYBITMAP.
The structures and tables include ① genie ROLE, ② item structure MapObject, and ③ map information table MAPINFO.
Then the structure of each class looks like this, whether it's a scorpion or a horse. Let's take a look.
The structure of the image layer is so simple that the logic layer only needs to determine the two parameters "which image and which frame" to draw all the images on the screen.
Let's talk about the base class of an image. Its source code architecture looks like this again.
Today, let's talk about the most basic image class MYBITMAP:
Function list of member functions:
Copy codeThe Code is as follows: // The function initializes an image based on a bitmap file.
// Input parameter application instance handle resource ID horizontal bitmap count vertical bitmap count
Void Init (HINSTANCE hInstance, int iResource, int row, int col );
// Set the function environment
// Input parameter DC (DC of the image to be drawn), temporary DC, width and height of the area to be drawn
Void SetDevice (HDC hdest, HDC hsrc, int wwin, int hwin); -- A hdc is required for drawing in C ++, the device context must be a Windows data structure that contains information about the drawing properties of a device, such as a display or printer. All painting calls are performed through device context objects that encapsulate Windows APIs for drawing lines, shapes, and texts. The device context allows you to draw a device-independent image in Windows. The device context can be used to draw to a screen, printer, or Metafile .. Net does not need this context object. It does not need this fart if it has a. netframework.
// Function to set the image position 1
// Input parameter setting method
Void SetPos (int istyle, int x, int y );
// Function Image Display
// Input parameter Image Display Method
Void Draw (DWORD dwdrop );
// Function Image Scaling
// Proportional scaling of input parameters in the vertical and horizontal directions
Void Stretch (int x, int y );
// Function Image Scaling
// ID of the scaled image in the vertical and horizontal directions of the input parameter (number of vertical dimensions)
Void Stretch (int x, int y, int id );
// Display the image at the specified position
// Input parameter horizontal and vertical coordinates
Void Show (int x, int y );
// The image is displayed horizontally in the center.
// Input parameter ordinate
Void ShowCenter (int y );
// Function to tile an image in a region
// Coordinate image ID of the upper left and lower right boundary of the input parameter (number of horizontal coordinates)
Void ShowLoop (int left, int top, int right, int bottom, int iframe );
// Irregular image display
// Input parameter horizontal and vertical coordinate image ID (number of horizontal coordinates)
Void ShowNoBack (int x, int y, int iFrame );
// Horizontal Tile of irregular Images
// Input parameter x-axis and Y-axis image ID (number of horizontal coordinates) Tile count
Void ShowNoBackLoop (int x, int y, int iFrame, int iNum );
// Play the animation
// The function automatically plays all frames of the image. The function is not implemented, but it must be used later :)
// No input parameter
Void ShowAni ();
// Set animation coordinates
// Input parameter horizontal and vertical coordinates
Void SetAni (int x, int y );
Member Data
// Trace printing class
// FILEREPORT f;
// Image handle
HBITMAP hBm;
// Average number of rows and columns
Int inum;
Int jnum;
// After splitting by row column, the width and height of each image (obviously the size of each image are the same. After the derivation, the width and height are meaningless)
Int width;
Int height;
// Screen width and height
Int screenwidth;
Int screenheight;
// Dc of the image to be drawn
HDC hdcdest;
// Select the temporary dc for the image
HDC hdcsrc;
// Current location
Int xpos;
Int ypos;
// Whether the video is playing the animation (the function is not implemented)
Int iStartAni;
Some of the functions and variables of this base class are not used in this game. They were preserved in previous games, so they seem a little messy. the main image function of this game is completed by its derived class. because the base class encapsulates the physical layer information (dc and handle), it is easier to write the derived class, allowing me to focus on the logical meaning.
The implementation of basic functions is very simple, mainly including the following:
1. Image initialization:Copy codeThe Code is as follows: // according to the program instance handle and the resource ID of the bitmap file, import the bitmap to obtain the bitmap handle.
HBm = LoadBitmap (hInstance, MAKEINTRESOURCE (iResource ));
// Obtain information about the bitmap file
GetObject (hBm, sizeof (BITMAP), & bm );
// Calculate the width and height of each image based on the number of images in the vertical and horizontal directions (for super Mary, the width and height information is processed by the derived class)
Width = bm. bmWidth/inum;
Height = bm. bmHeight/jnum;
Next, let's talk about the game background class MYBKSKY.
Class description: This is a class dedicated to dealing with game backgrounds. In horizontal games or shooting games, there is a background image such as mountains, skies, clouds, and stars. These images are usually only one to two times the screen width, and then move cyclically like a scroll, forming a piece, and feeling like a very long image. This class specifically handles this background. In the super enhanced version of Mary, the main level is 3 levels, each with a background image; from the pipe in, there are two levels, all use a black picture. There are four images in total. The four images are in the same size and vertically arranged in a bitmap file. The MYBKSKY class is derived from MYBITMAP. As the background image only needs to be moved cyclically, you only need to implement one function without worrying about any other issues (such as handles and dc ). Coding is simple, reflecting the benefits of forward object again.
Implementation principle:
How can I keep an image moving like a scroll? It is very simple. Suppose there is a vertical split line, and the image is divided into the left and right parts. First show the right part, and then link the left part to the end of the image. The split line is continuously moved to the right, and the picture will be displayed cyclically.
Function list of member functions:Copy codeThe Code is as follows: class MYBKSKY: public MYBITMAP
{
Public:
MYBKSKY ();
~ MYBKSKY (); -- destructor. In C ++, you have resources to release. We have a terminator function in. net, which also has the taste of destructor. But do we not have to release resources on our own.
// Show
// A background is displayed.
// No input parameter
Void DrawRoll (); // loop completion
// Display a background and scale the image
// Proportional scaling of input parameters in the vertical and horizontal directions
Void DrawRollStretch (int x, int y );
// The function specifies to display a background and scale the image. This function is used in the game.
// ID of the background image with the vertical and horizontal scaling ratio of the input parameter (the number of vertical parameters)
Void DrawRollStretch (int x, int y, int id );
// Function to set the image position
// New X-axis and Y-axis of the input parameter
Void MoveTo (int x, int y );
// Function to move the split line cyclically
// Distance from the input parameter split line
Void MoveRoll (int x );
// Data
// Horizontal coordinate of the split line
Int xseparate;
};
Take a look at the image display class MYANIOBJ
Class description: this class is responsible for displaying images in the game. The menu background, customs clearance, and game end prompt images are processed by MYBITMAP (static images of the same size ). The game background is handled by MYBKSKY. The other images, that is, all the images in the game, are processed by MYANIOBJ.
Technical principle: the pictures in the game are different in size. in Super Mario, they can be divided into two types: rectangular pictures and irregular pictures. In bitmap files, images are arranged vertically and frames are arranged horizontally. Use two arrays to store the width and height of each image. To facilitate the display of an image, an array is used to store the ordinate values of each image (that is, the position in the upper left corner of the bitmap file ). During use, the logic layer specifies the "frame" of "which image" and the "position ". In this way, the image display function is realized.
Function list of member functions:Copy codeThe Code is as follows: class MYANIOBJ: public MYBITMAP
{
Public:
MYANIOBJ ();
~ MYANIOBJ ();
// Init list
// Function initialization width array height array ordinate array whether there is a black/white chart
// Input parameter width array address height array address image quantity whether there is a black-and-white image (0 NO, 1 Yes)
// (The image ordinate information is calculated by the function)
Void InitAniList (int * pw, int * ph, int inum, int ismask );
// Function initializes some special bitmaps, for example, the size of each image is consistent or there are other rules
// Input parameter initialization method parameter 1 parameter 2
// (For future extension, the purpose is to save the trouble of wide and high arrays)
Void InitAniList (int style, int a, int B );
// Show
// Function to display images (irregular images)
// Input parameter horizontal and vertical coordinates (position to be displayed) image id (vertical number), image frame (horizontal number)
Void DrawItem (int x, int y, int id, int iframe );
// Function display image (rectangular image)
// Input parameter horizontal and vertical coordinates (position to be displayed) image id (vertical number), image frame (horizontal number)
Void DrawItemNoMask (int x, int y, int id, int iframe );
// Specify the width of the function to display part of the image (rectangular image)
// Input parameter horizontal and vertical coordinates (position to be displayed) image id (portrait number), display width image frame (landscape number)
Void DrawItemNoMaskWidth (int x, int y, int id, int w, int iframe );
// Function to play an animation that displays frames cyclically
// Input parameter horizontal and vertical coordinates (position to be displayed) image id (vertical number)
Void PlayItem (int x, int y, int id );
// The width array supports up to 20 images.
Int wlist [20];
// The height array supports up to 20 images.
Int hlist [20];
// The ordinate array supports up to 20 images.
Int ylist [20];
// Current frame during animation playback
Int iframeplay;
};
Take a look at the Magic attack type MYANIMAGIC
Class description: There are two types of attacks for players: Normal attacks (bullets) and magic attacks (tornado ). This class is dedicated to dealing with tornado. My initial idea was to use some special bitblt methods to create special effects, such as or, And, or. Failed after several attempts. Finally, you can only use the old method of "first and last or. This class can be viewed as a simplified version of MYANIOBJ and only supports the display of irregular images.
Function list of member functions:Copy codeThe Code is as follows: class MYANIMAGIC: public MYBITMAP
{
Public:
MYANIMAGIC ();
~ MYANIMAGIC ();
// Init list
// Function initialization width array height array ordinate array (must have a black-and-white image)
// Input parameter width array address height array address image quantity
// (The image ordinate information is calculated by the function)
Void InitAniList (int * pw, int * ph, int inum );
// Function setting dc
// Input parameter display dc temporary dc (for image handle selection) Temporary dc (for special effect implementation)
Void SetDevice (HDC hdest, HDC hsrc, HDC htemp );
// Show
// Display a frame of an image
// Input parameter horizontal and vertical coordinates (display position) image id (vertical number) frame (horizontal number)
Void DrawItem (int x, int y, int id, int iframe );
// Array of width
Int wlist [20];
// Height Array
Int hlist [20];
// Array of ordinate values
Int ylist [20];
// Temporary dc used for special effects. The function is not implemented
HDC hdctemp;
};
This is some of my architectures.