Implementation of 2d game strabismus angle Map

Source: Internet
Author: User
Tags mul textout

Implementation of 2d game strabismus angle Map
Download the example code in this article (radasm project file)
Note:

1. This project is a scrolling demonstration of the Oblique Angle map. The implementation of this program does not consider the screen rendering efficiency and memory space savings. Therefore, the map rendering function does not exclude the unvisualized part of the graph. The implementation of this program fully uses the win32 assembly language, so using the gdi interface can also get a better frame rate.

 

2. This program reflects the idea of controlling 2d game maps. That is to say, the core issue is several coordinate transformation issues, which can solve the coordinate transformation problem.
 
1. Save the one-dimensional data index of the map. You need to convert this one-dimensional array index into map coordinates (column number and row number ). After this step is completed, the plotting program can traverse the entire one-dimensional array, just like traversing
A two-dimensional map block array is the same. The array index determines the coordinates on the map.

2. Map coordinates-the number of the block column and the row number. Map coordinates are converted into screen Paster coordinates. Because the map coordinates are (column numbers, row numbers), you must use the map coordinates to obtain the pixel coordinates in the upper left corner of the map block, that is, the texture coordinates.

3. Perform visual transformation. That is, when a map is very large, you can scroll the map by moving the view on the map to see different parts. Therefore, once the absolute coordinates on the map are determined in the upper left corner of the view, the relative coordinates of a map block inside the view-the final texture coordinates of the window client area can be obtained.

4. After several steps above, a map block can be drawn into a window.

Iii. Frame speed control:

A. Use the variable timeOld to record the last calculation frame speed end time.
B. The timeNew variable records the current time.
C. Use the variable frameCount to record the total number of frames played from timeOld to timeNew.

D. After obtaining the current time timeNew
The total amount of time (MS) from the last calculated frame rate to the present: timeNew-timeOld

When the interval is greater than 1 second, a frame rate calculation is performed.
TimeOld = timeNew, frameCount is also set to zero, and records are rerecorded from now on.
Frame playback for computing in the next second.

E. Use timeCur for time tracking to control the frame speed.
TimeCur records the end time of the last frame. TimeNew-timeCur
How long has it taken since the last frame was played.
Therefore, if one frame is performed each time when timeNew-timeCur is greater than 10
Play, and then make timeCur = timeNew For The Next frame playing Interval
Control. In this way, we can control the maximum playback speed of one frame within 10 ms, that is, the frame speed.
.

Code Description:

The main code in this example is the map rendering function, which implements several coordinate transformations.

DrawMap proc hdc1: HDC
LOCAL I: DWORD
LOCAL row: DWORD
LOCAL col: DWORD
LOCAL xAbs: DWORD
LOCAL yAbs: DWORD
LOCAL vx: DWORD
LOCAL vy: DWORD
LOCAL vp: POINT
 
Invoke BitBlt, buffer, 0, 0, 640,480, bgDC, 0, SRCCOPY
Mov I, 0
. Repeat
Convert array indexes into map coordinates
Xor edx, edx
Mov eax, I
Mov ebx, 20
Div ebx
Mov row, edx
Mov col, eax
Convert map coordinates into map coordinates (absolute coordinates)
Mov eax, row
Movebx, 64/2
Mul ebx
Mov xAbs, eax
Mov eax, col
Mul ebx
Sub xAbs, eax
Mov eax, xstart
Add xAbs, eax

Mov eax, col
Movebx, 32/2
Mul ebx
Mov yAbs, eax
Mov eax, row
Mul ebx
Add yAbs, eax
Mov eax, ystart
Add yAbs, eax
Convert the texture coordinates into the window (View) coordinates based on The View coordinates.
Mov eax, xAbs
Sub eax, viewRect. left
Mov vx, eax
Mov eax, yAbs
Sub eax, viewRect. top
Mov vy, eax
; Screen Paster
Mov eax, I
. If mapInfo [eax] = 0
Invoke BitBlt, buffer, vx, vy, 64, 32, tile0, 64, 0, SRCAND
Invoke BitBlt, buffer, vx, vy, 64, 32, tile0, 0, SRCPAINT
. Elseif mapInfo [eax] = 1
Invoke BitBlt, buffer, vx, vy, 64, 32, tile1, 64, 0, SRCAND
Invoke BitBlt, buffer, vx, vy, 64, 32, tile1, 0, SRCPAINT
. Elseif mapInfo [eax] = 2
Invoke BitBlt, buffer, vx, vy, 64, 32, tile2, 64, 0, SRCAND
Invoke BitBlt, buffer, vx, vy, 64, 32, tile2, 0, SRCPAINT
. Else
Invoke BitBlt, buffer, vx, vy, 64, 32, tile3, 64, 0, SRCAND
Invoke BitBlt, buffer, vx, vy, 64, 32, tile3, 0, SRCPAINT
. Endif
;
Add I, 1
. Until I = 400
 
; Process scenes
Mov I, 0
. Repeat
Convert array indexes into map coordinates
Xor edx, edx
Mov eax, I
Mov ebx, 20
Div ebx
Mov row, edx
Mov col, eax
Convert map coordinates into map coordinates (absolute coordinates)
Mov eax, row
Movebx, 64/2
Mul ebx
Mov xAbs, eax
Mov eax, col
Mul ebx
Sub xAbs, eax
Mov eax, xstart
Add xAbs, eax

Mov eax, col
Movebx, 32/2
Mul ebx
Mov yAbs, eax
Mov eax, row
Mul ebx
Add yAbs, eax
Mov eax, ystart
Add yAbs, eax
Convert the texture coordinates into the window (View) coordinates based on The View coordinates.
Mov eax, xAbs
Sub eax, viewRect. left
Mov vx, eax
Mov eax, yAbs
Sub eax, viewRect. top
Mov vy, eax
; Screen Paster
Mov eax, I
. If sceneInfo [eax] = 1
Add vx, 7
Sub vy, 44
Invoke BitBlt, buffer, vx, vy, 50, 60, scene1, 50, 0, SRCAND
Invoke BitBlt, buffer, vx, vy, 50, 60, scene1, 0, 0, SRCPAINT
. Elseif sceneInfo [eax] = 2
Add vx, 7
Sub vy, 30
Invoke BitBlt, buffer, vx, vy, 50, 60, scene2, 50, 0, SRCAND
Invoke BitBlt, buffer, vx, vy, 50, 60, scene2, 0, SRCPAINT
. Endif
;
Add I, 1
. Until I = 400
 
; <> <
Mov eax, viewRect. left
Mov vp. x, eax
Mov eax, viewRect. top
Mov vp. y, eax
Invoke GetStatus
. If eax = 0
Invoke SetDir
. Endif
Invoke DrawSprite, buffer, vp
; Print program information
Invoke SetBkMode, buffer, TRANSPARENT
Invoke TextOut, buffer, 50,300, addr frameInfo, 28
Invoke TextOut, buffer, 50,350, addr appInfo, sizeof appInfo-1
Invoke TextOut, buffer, 55,400, addr authInfo, sizeof authInfo-1
Invoke SetBkMode, buffer, OPAQUE
Invoke BitBlt, hdc1, 640,480, buffer, SRCCOPY
Ret

DrawMap endp

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.