I have an appointment with super Mario

  • 2020-04-01 21:26:33
  • OfStack

First, let me talk about the most intuitive experience of C++! Familiar with. Net smart tips, at first found that C++ no hint at all. Later, I did a little Google search and downloaded a plug-in called visual assist that was much better than the vs autoprompt. Then, just get used to writing all the declarations and method implementations in the same file in.net. But that's not the case with C++. He makes a statement in the header file, implements it in the source file, says exactly how it started and how it got used to it. Then I got used to it. Then, writing C++ files is a fucking pain, it's no better than.net, Microsoft already better than you package, in C++, a lot of things need to write their own.   First, a destructor needs to free up its own resources. While. Net has an automatic gc garbage collection, the release of resources do not release, care about your birds. There is no way, only their release. Be a good programmer by the rules. This is my most intuitive experience with C++.
Back to normal, say something about this super Mario game. First of all, I have a look at the classification of the class structure of the game, if there is something wrong, please correct.

From the point of view of the hierarchy, into these several layers image layer, logic layer, structure and table.

The image layer includes image base class MYBITMAP, game background MYBKSKYaMYBITMAP, game picture MYANIOBJaMYBITMAP, magic attack MYANIMAGICaMYBITMA.
The logic layer includes game logic GAMEMAP, clock processing MYCLOCK, font processing MYFONT, tracking printing FILEREPORT, player control MYROLEaMYBITMAP.
Structure and table include wizard structure ROLE, item structure MapObject, map information table MAPINFO.
Then the structure of each kind is so like, is the mule or the horse out to walk. We're looking down.
The structure of the image layer is so simple, the logic layer only needs to determine "which image, which frame" these two parameters, can draw all the pictures on the screen.
  Say something about the base class of an image. The architecture of his source code looks like this.
Today I will first talk about the most basic image class MYBITMAP:
List of member functions:


//Function to initialize the image according to a bitmap file
//Number of horizontal bitmaps number of vertical bitmaps
void Init(HINSTANCE hInstance,int iResource,int row,int col);
//Function setting environment information
//Input destination DC(the DC to draw the image), temporary DC, the width and height of the region to be drawn
void SetDevice(HDC hdest,HDC hsrc,int wwin,int hwin);-- Mama of, C++ I need one for the Chinese drawing hdc , the device context needs to be one that contains information about the drawing properties of a device, such as a monitor or printer  Windows  Data structure. All drawing calls are made through device context objects that encapsulate the lines, shapes, and text used to draw  Windows API . Device context is allowed in  Windows  Perform device-independent drawing in. The device context can be used to draw to a screen, printer, or metafile. .net I don't need this   The context object, he has one .netframework   I don't need this shit. 
//Function to set the picture position 1
//Input setting method horizontal and vertical coordinates
void SetPos(int istyle,int x,int y);
//Function picture display
//Input picture display mode
void Draw(DWORD dwRop);
//Function image zooming display
//Scale in the vertical and horizontal direction
void Stretch(int x,int y);
//Function image zooming display
//Scale in the vertical and horizontal direction  Scale the image ID( Which is the vertical number )
void Stretch(int x,int y,int id);
//Function to display a picture in a specified location
//Enter the horizontal and vertical coordinates
void Show(int x,int y);
//Feature horizontal center to show pictures
//Enter the ordinate
void ShowCenter(int y);
//Function to tile an image in an area
//Enter the coordinate picture ID of the upper left and lower right boundary (horizontal position)
void ShowLoop(int left,int top,int right,int bottom,int iframe);
//Feature irregular picture display
//Enter the horizontal and vertical coordinates  The picture ID( Which one is horizontal )
void ShowNoBack(int x,int y,int iFrame);
//Feature irregular pictures horizontally tiled
//Enter the horizontal and vertical coordinates  The picture ID( Which one is horizontal )  The number of tiles 
void ShowNoBackLoop(int x,int y,int iFrame,int iNum);
 //The flash
//Function automatically play all the frames of the picture, the function did not implement, but must be used in the future :)
//Into the reference no
void ShowAni();
//Function sets animation coordinates
//Enter the horizontal and vertical coordinates
void SetAni(int x,int y);
 Members of the data 
//Trace print class
// FILEREPORT f;
//Handle to the image
HBITMAP hBm;
//Divide it equally by column and column
int inum;
int jnum;
//The width and height of each image after dividing by rows and columns (obviously the size of each image is the same, after derivation, the width and height here is no longer useful)
int width;
int height;
//Wide screen high
int screenwidth;
int screenheight;
//Dc to draw the picture
HDC hdcdest;
//Temporary dc used to select images
HDC hdcsrc;
//The current position
int xpos;
int ypos;
// Whether in the The flash In the ( Function not implemented )
int iStartAni;

This part of the base class functions and variables, in this game is not used, survived is once upon a time a few games, so look a little messy. The main image of the game function, performed by its derived classes. Due to the base class that encapsulates the physical information (dc and handle), the writing of the derived class is easier, would it be possible for me to focus on the logical meaning.
Base class function implementation, very simple, mainly is the following:
1. Image initialization:

//According to the program instance handle, the resource ID of the bitmap file, import the bitmap and get the bitmap handle
hBm=LoadBitmap(hInstance,MAKEINTRESOURCE(iResource));
//Gets 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 Mario, the width and height information is processed by a derived class)
width=bm.bmWidth/inum;
height=bm.bmHeight/jnum;

Now let's talk about the game background class MYBKSKY
Class description: this is a special handling of the game background class. In the horizontal version of the game or shooting game, there is a background screen, mountain, sky, clouds, stars, and so on. These images are typically only 1 to 2 times the width of the screen, and then move in a circular motion like a scroll, joining together in one piece, which feels like a very long picture. This class is dedicated to this background. In super Mario bros., the main levels are three, each with a background image; From the water pipe, there are two levels, using an all-black picture. There are four pictures. The four maps are of the same size, arranged vertically in a bitmap file. MYBKSKY is a class derived from MYBITMAP. Since the background image only needs to complete the effect of looping, only one function needs to be implemented without any other concerns (such as handle, dc). Coding is simple, again reflecting the benefits of object orientation.
  Principle of implementation:
How do you make a picture move around like a scroll? Very simply, let's say I have a vertical dividing line, and I'm going to divide the picture into left and right parts. Show the right part first, then attach the left part to the end of the picture. Keep moving the dividing line to the right and the picture will be displayed in a loop.
  List of member functions:
 

class MYBKSKY:public MYBITMAP
{
public:
MYBKSKY();
~MYBKSKY();-- Destructor. Okay  C++ You have the resources   Release yourself. we .net  There's a terminator function that's also a little bit of a destructor, but doesn't necessarily free up resources on its own. 
//show
//The function displays a background.
//Into the reference no
void DrawRoll(); //Cycle to fill empty
//Function displays a background and zooming image
//Scale in the vertical and horizontal direction
void DrawRollStretch(int x,int y);
//The function specifies that a certain background should be displayed and the image should be zoomed. This function is used in the game
//Scale in the vertical and horizontal direction  The background image ID( Which is the vertical number )
void DrawRollStretch(int x,int y,int id);-- Operation on the horizontal axis 
//Function to set the image location
//Enter the new horizontal and vertical coordinates
void MoveTo(int x,int y);
//Function circularly moves the dividing line
//The distance of the input dividing line
void MoveRoll(int x);
 //data
//Secant abscissa
int xseparate;
};

Take a look at the picture display class MYANIOBJ
Class description: this class is responsible for the game image display. Menu background, clearance and game end prompt images, processed by MYBITMAP (uniform size static images). The game background is handled by MYBKSKY. The rest of the pictures, all the pictures in the game, are handled by MYANIOBJ.
Technical principle: the size of images in the game is not consistent, specifically in super Mario, can be divided into two categories: rectangular pictures and irregular pictures. In bitmap files, each picture is vertically arranged and each frame is horizontally arranged. 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 vertical coordinates of each image (the upper left corner of the bitmap file). When used, the logical layer specifies which frame of which picture is displayed in which position. In this way, the image display function is realized.
List of member functions:

class MYANIOBJ:public MYBITMAP
{
public:
MYANIOBJ();
~MYANIOBJ();
//init list
//Function initializes the width array height array vertical coordinate array whether there is a black and white graph
//Input width array address height array address picture number is there a black and white picture (0 has no, 1 has)
//(the ordinate information of the picture is calculated by the function)
void InitAniList(int *pw,int *ph,int inum,int ismask);
//The function initializes special bitmaps, such as images of the same size, or other patterns
//Parameter 1 parameter 2
//(reserved for future extensions to save the trouble of wide and high arrays)
void InitAniList(int style,int a,int b);
//show
//Function display picture (irregular picture)
//Enter the horizontal and vertical coordinates (the position to be displayed) picture id(the vertical number), picture frame (the horizontal number)
void DrawItem(int x,int y,int id,int iframe);
//Function display picture (rectangular picture)
//Enter the horizontal and vertical coordinates (the position to be displayed) picture id(the vertical number), picture frame (the horizontal number)
void DrawItemNoMask(int x,int y,int id,int iframe);
//Function specifies the width to display part of a picture (rectangular picture)
//Enter the horizontal and vertical coordinates (the position to be displayed) picture id(the vertical number), display the width picture frame (the horizontal number)
void DrawItemNoMaskWidth(int x,int y,int id,int w,int iframe);
//Function to play an animation that loops to show each frame
//Enter the horizontal and vertical coordinates (the position to be displayed)
void PlayItem(int x,int y,int id);
//Width array supports up to 20 images
int wlist[20];
//The height array supports up to 20 images
int hlist[20];
//The vertical array supports up to 20 images
int ylist[20];
//The current frame while the animation is playing
int iframeplay;
};

Take a look at the magic attack class MYANIMAGIC
There are two types of attack: normal attack (bullets), magic attack (whirlwind). This class deals specifically with cyclones. My original idea was to use some special bitblt methods to make special effects, such as or, and, or. Tried several times, but failed. In the end, the old "first and then or" method can only be used. This class can be regarded as a simplified version of MYANIOBJ, which only supports the display of irregular pictures.
List of member functions:

class MYANIMAGIC:public MYBITMAP
{
public:
MYANIMAGIC();
~MYANIMAGIC();
//init list
//Function initializes width array height array ordinate array (must have black and white)
//Width array address height array address number of images
//(the ordinate information of the picture 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 effect implementation)
void SetDevice(HDC hdest,HDC hsrc,HDC htemp);
//show
//Function displays a frame of an image
//Enter horizontal and vertical coordinates (display position) picture id(vertical number) frame (horizontal number)
void DrawItem(int x,int y,int id,int iframe);
//The width of the array
int wlist[20];
//Highly array
int hlist[20];
//Ordinate array
int ylist[20];
//Temporary dc for special effects, function does not implement L
HDC hdctemp;
};

So that's some of my architecture


Related articles: