Game Development with 45 degrees oblique (1)

Source: Internet
Author: User
Tags map data structure

 

Note: The write is very rough. If you cannot understand it, check the source code of kgamev1.0.

The best game is an RPG Game, but if it wins public praise, it must adopt (or even a standard) A 45-degree oblique map and character game engine, next we will analyze them separately.

I. Map Data Structure

Soft's definition of "Legend of the holy sword" is good. You can refer to it.
Before defining the map structure, we need to define the tile structure in two cases.

(1) items and scenes are processed by NPC (irregular processing, such as "magic baby "):
Typedef struct {
Bool isground; // whether to display the earth surface
Short groundpicnum; // The number of the Surface Image page (0 ~ 59), (0-55) the page is static, (56-55 ~ 59) the page is dynamic
Short groundpicx; // the abscissa of the surface material on the surface image (using the grid as the coordinate)
Short groundpicy; // The ordinate of the surface material on the surface image (using the grid as the coordinate)
Short block; // obstacle sign (id_block_t, id_block_f)
Short hook; // trap flag (id_hook_f, id_hook_t ............)
Char hookscriptname [28];
Char reserve; // Reserved Bit. (I guess we have some unexpected information. You can add it here later to avoid the problem after the map is edited, when modifying the cell structure, you can avoid re-editing the map file)
} Stcell;

I am not talking about anything, and the comments are clear!

(2). Items and scenes are also processed by tile, but NPCs are processed by irregular processing (for example, "xianjian Qixia" is not recommended !)
Typedef struct {
Bool isground; // whether to display the earth surface
Short groundpicnum; // The number of the Surface Image page (0 ~ 59), (0-55) the page is static, (56-55 ~ 59) the page is dynamic
Short groundpicx; // the abscissa of the surface material on the surface image (using the grid as the coordinate)
Short groundpicy; // The ordinate of the surface material on the surface image (using the grid as the coordinate)
Bool isobject1; // whether to display item 1
Short object1picnum; // The number of the Surface Image page (0 ~ 59), (0-55) the page is static, (56-55 ~ 59) the page is dynamic
Short object1picx; // The X coordinate of object material 1 on the object image (using the grid as the coordinate)
Short object1picy; // The ordinate value of object material 1 on the object image (using the grid as the coordinate)
Bool isobject2; // whether to display item 2
Short object2picnum; // The number of the Surface Image page (0 ~ 59), (0-55) the page is static, (56-55 ~ 59) the page is dynamic
Short object2picx; // The X coordinate of object material 2 on the object image (using the grid as the coordinate)
Short object2picy; // The ordinate of object material 2 on the object image (using the grid as the coordinate)
Short block; // obstacle sign (id_block_t, id_block_f)
Short hook; // trap flag (id_hook_f, id_hook_t ............)
Char hookscriptname [28];
Char reserve; // Reserved Bit. (I guess we have some unexpected information. You can add it here later to avoid the problem after the map is edited, when modifying the cell structure, you can avoid re-editing the map file)
} Stcell;

I don't want to explain it!

The next step is map. The map processing method is similar.
Int ID; // map ID
Char name [32]; // Map Name (Map File name)
Int width; // The width (taken as the coordinate of the grid ).
Int height; // The height (taken as the coordinate of the grid ).
Int mapstartx, mapstarty; // coordinates in the upper left corner
Stcell ** cell; // dynamic Lattice
Char filename [32]; // current map file name
Char scrfname [32]; // map initialization script file name
Char reserve [4]; // Reserved Bit, (I guess we have some unexpected information. You can add it here later to avoid modifying the map_struct format after the map is edited, destroys the previously compiled map file)

// You don't need to see it here.
Lpdirectdrawsurface7 lpdds_mapback; // map temporary save point
Lpdirectdrawsurface7 lpdds_tmouse; // tile mouse
Int mapbx, mapby; // used to optimize the machine

Isn't the introduction finished? It's easy, but it's getting harder and harder to come back and be prepared.

Ii. Coordinate Transformation

The major articles are going crazy here. I don't want to talk about the reasoning method anymore. Remember the following functions:

// Convert the 45-degree oblique coordinates to the screen coordinates
Inline void mitomd (INT dx, int dy, Int & IX, Int & Iy)
{
IX = (tilewidth> 1) * (DX-dy); // convert to absolute coordinate X
Iy = (tileheight> 1) * (dx + dy); // convert to the absolute coordinate Y large diamond
}

// Convert the screen coordinates to 45-degree oblique coordinates
Inline void mdtomi (int ix, int Iy, Int & dx, Int & Dy)
{
DX = int (0.5 * (Iy <1) + ix)/(tilewidth> 1 ));
DY = int (0.5 * (Iy <1)-IX)/(tilewidth> 1 ));
}

Where
# Define tilewidth 32 // The width of each Tile
# Define tileheight 16 // The height of each Tile

3. draw a map

What's wrong with creating a map! After the map is drawn, FPS can be increased several times.

Traditional map rendering methods:

For (INT I = 0; I {
For (int t = 0; t <width; t ++)
{
Draw .....
}
}

After improvement, I will do this.

If (mapbx! = X & maxby! = Y)
{
For (INT I = 0; I {
For (int t = 0; t <width; t ++)
{
Draw .....
Update maxbx and maxby
Back up a map (save as an image)
}
}
}
Else
{
Restore map (retrieve image)
}

In this way, the FPS in idle state will increase. What? I want to upgrade it!

The reasoning method is omitted (I do not remember it, written in spring ):

# Define mapdmx int (0.5 * (screenheight <1) + screenwidth)/(tilewidth> 1) + 1 // maximum draw point X
# Define mapdmy int (0.5 * (screenheight <1)/(tilewidth> 1) + 1 // maximum plot point y
# Define mapdsy int (0.5 * (-screenwidth)/(tilewidth> 1)-1 // minimum plot point y (special)

Then
If (mapbx! = X & maxby! = Y)
{
For (INT y = intsizel (mapstarty + mapdsy, 0); y <intsizes (mapstarty + mapdmy, height); y ++)
{
For (INT x = intsizel (MapStartX-1, 0); x <intsizes (mapstartx + mapdmx, width); X ++)
{
Draw .....
Update maxbx and maxby
Back up a map (save as an image)
}
}
Else
{
Restore map (retrieve image)
}

Here, intsizel takes two big ones, and intsizes takes two small ones!

The speed has been optimized to the limit. Well, I will write it first, and I will block it next time.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.