Windows GDI notes

Source: Internet
Author: User
Tags textout
1. device context
// Obtain the DC
HDC getdc (hwnd)
// Release the DC
Int releasedc (hwnd, hdc dc Name );

2. GDI object: Create-> select-> Delete
// Create a paint brush
Hpen createpen (INT style, int width, colorref color );
// Create a shadow image brush
Hbrush createhatchbrush (INT style, colorref color );
// Create a monochrome image brush
Hbrush createsolidbrush (colorref color );

// Select the GDI object
Hgdiobj SelectObject (HDC, hgdiobj GDI object );
// Delete the GDI object
Bool deleteobject (hgdiobj GDI object );

3.
// Draw a line
// Move the paint brush to the starting point of the line
Bool movetoex (HDC, int X, int y, lppoint current coordinates );
// Draw a line to the specified Coordinate
Bool lineto (HDC, int X, int y );

// Draw a rectangle
Bool rectangle (HDC, int X1, int Y1, int X2, int Y2 );

// Draw a polygon
Bool polygon (HDC, const point array pointer, int polygon points );

Bool polylineto (HDC, const point array pointer, int polygon points );
Bool polyline (HDC, const point array pointer, int polygon points );

Typedef struct tagpoint {
Long X;
Long y;
} Point;

// Draw an elliptical shape
Bool ellipse (HDC, int X1, int Y1, int X2, int Y2 );

// Draw a rounded rectangle
Bool roundrect (HDC, int X1, int Y1, int X2, int Y2, int rounded corner with an elliptical length, and INT rounded corner with an elliptical height );

// Draw slices
Bool pie (HDC, X coordinate on the top left of the int peripheral rectangle, Y coordinate on the top left of the int peripheral rectangle, X coordinate on the top right of the int peripheral rectangle, Y coordinate on the top right of the int peripheral rectangle, int X1, int Y1, int X2, int Y2 );

// Draw a bow
Chord (HDC, 300,210,550,340, 50, 600,300 );

4. String
Int x = loword (lparam );
// Set the string color
Settextcolor (HDC, # ff0000 );
Char STR [20] = "";
Sprintf (STR, "x coordinate: % d", X );
// Output string
Bool textout (HDC, int X coordinate, int y coordinate, lpctstr string pointer, int String Length );

5. Draw bitmap
(1) Load bitmap
Handle LoadImage (hinstance source object, lpctstr name, uint bitmap type, int loading width, int loading height, uint Loading Mode );
Bitmap types: image_bitmap, image_cursor, and image_icon
Loading Method: Load lr_loadfromfile from a file
(2) create a memory DC
HDC createcompatibledc (HDC );
Deletedc (HDC );
(3) Select a bitmap object
Hgdiobj SelectObject (HDC, hgdiobj GDI object );
(4) textures
Bool bitblt (HDC destdc, int destx, int desty, int destwidth, int destheight, HDC srcdc, int srcx, int srcy, DWORD texture );
Texture method:
Srccopy attaches the source bitmap to the Target DC
Srcand performs the "and" Operation on the source bitmap and the Target DC.
Srcpaint performs the "or" Operation on the source bitmap and the Target DC.

6. Transparent Processing
(1) Transparency
SelectObject (MDC, BG );
Bitblt (HDC, 600,450, MDC, srccopy );
SelectObject (MDC, DRA );
Bitblt (HDC, 280,320, MDC, srcand );
Bitblt (HDC, 280,320, MDC, srcpaint );

(2) translucent Effect
Translucent color = foreground color * opacity + background color * (1-opacity)
A. Obtain the bitmap structure.
Int GetObject (hgdiobj GDI object, int structure size, lpvoid structure variable );
Structure Variable typedef struct tagbitmap {
Long bmtype;
Long bmwidth;
Long bmheight;
Long bmwidthbytes;
Word bmplanes;
Word bmbitspixel;
Lpvoid bmbits;
} Bitmap;

GetObject (bitmap, sizeof (Bitmap), & BM );
B. Create a temporary storage array
Unsigned char * PX = new unsigned char [BM. bmheight * BM. bmwidthbytes];
C. Obtain bitmap values
Long getbitmapbits (hbitmap bitmap, number of bytes obtained by long, array pointer stored by lpvoid );

Getbitmapbits (bitmap, BM. bmheight * BM. bmwidthbytes. px );
D. Merge pixel color values

E. Reset the bitmap color.
Long setbitmapbits (hbitmap bitmap, DWORD color array size, const void array pointer );

(3) transparent and translucent Effect
// Create a bitmap
Hbitmap createcompatiblebitmap (HDC, int width, int height );

7. Timer
Uint settimer (hwnd, uint timer code, uint interval, timerproc processor response function );
The uint timer code must be unique in the same window and not 0

// Delete the timer
Bool killtimer (INT timer Code );

8. game loop
DWORD tpre, tnow;

Winmain
While (msg. message! = Wm_quit)
{
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
{
Tnow = gettickcount ();
If (tnow-tpre> = 40)
Mypaint (HDC );
}
}

Initinstance
HDC = getdc (hwnd );
MDC = createcompatibledc (HDC );
Bufdc = createcompatibledc (HDC );
Hbitmap BMP = createcompatiblebitmap (HDC, 640,480 );
SelectObject (MDC, BMP );
Dra = (hbitmap) LoadImage (null, "dra2.bmp", image_bitmap, 760,198, lr_loadfromfile );
BG = (hbitmap) LoadImage (null, "bg.bmp", image_bitmap, 640,480, lr_loadfromfile );
Num = 0;
X = 640;
Y = 360;
Mypaint (HDC );

Int frame = 0;
Int tcheck = 0, FPS;
Void mypaint (HDC)
{
If (num = 8)
Num = 0;
SelectObject (bufdc, BG );
Bitblt (MDC, 640,480, bufdc, srccopy );

SelectObject (bufdc, DRA );
Bitblt (MDC, X, Y, 99, bufdc, num *, 99, srcand );
Bitblt (MDC, X, Y, 99, bufdc, num *, srcpaint );

Bitblt (HDC, 640,480, MDC, srccopy );

Tpre = gettickcount ();
Num ++;

// Display text
Frame ++;
If (tnow-tcheck >=1000)
{
FPS = frame;
Frame = 0;
Tcheck = tnow;
}
Char STR [40] = "";
Sprintf (STR, "% d screenshots per second", FPS );
Textout (HDC, 0, 0, STR, strlen (STR ));
}

9. animation display problems
Sort textures
10. background animation design
A) Single background scrolling
B) Circular background scrolling
C) Multi-Background loop Animation

11.
1)
// Set the mouse message outside the window
Hwnd setcapture (hwnd );

// Release the mouse message outside the window
Bool releasecapture (void );
2)
// Set the cursor position. Note: (x, y) corresponds to screen coordinates instead of window coordinates.
Bool setcursorpos (int x, int y );

// Convert the window coordinate to the screen Coordinate
Bool clienttoscreen (hwnd, lppoint window coordinate );

// The screen coordinates are converted to window coordinates.
Bool screentoclient (hwnd, lppoint screen coordinate );

Point pt;
PT. x = 0;
PT. Y = 0;
Clienttoscreen (hwnd, & pt );
Setcursorpos (Pt. X, Pt. y );
3) show or hide the mouse
Int showcursor (bool true or false );
4)
// Restrict the moving area of the mouse cursor
Bool clipcursor (const rect move area rectangle );

// Lift the limit
Bool clipcursor (null );

Typedef struct rect {
Long left;
Long top;
Long right;
Long bottom;
} Rect;

// Obtain the rectangle of the external area of the window
Bool getwindowrect (hwnd, lprect rectangular structure );

// Obtain the rectangular area inside the window
Bool getclientrect (hwnd, lprect rectangular structure );

For example, to restrict the area where the mouse and cursor move
Rect;
Point lt, Rb;
Getclientrect (hwnd, & rect );
Lt. x = rect. Left;
Lt. Y = rect. Top;
RB. x = rect. Right;
RB. Y = rect. bottom;
Clienttoscreen (hwnd, & lt );
Clienttoscreen (hwnd, & Rb );
Rect. Left = lt. X;
Rect. Top = lt. Y;
Rect. Right = RB. X;
Rect. Bottom = RB. Y;
Clipcursor (& rect );

12. AI
Neural Network
GeneticsAlgorithm
Fuzzy Logic

1) monster chase
If (monster HP> 200)
{
P = rand () % 3;
If (P! = 1) // a probability of 2/3 correct orientation to the player
{
If (monster x> Player X)
Monster X --;
Else
Monster x ++;

If (monster y> player y)
Monster y --;
Else
Monster y ++;
}
}
Else
Monster HP + = 5; // The monster does not move, rest and blood
2) mobile Evasion
If (monster x> Player X)
Monster ++;
Else
Monster X --;

If (monster y> player y)
Monster y ++;
Else
Monster y --;
3) pattern movement: Chase, Dodge, random, fixed movement, etc.

4) monster Behavior
A. Common attacks
B. Cast attack magic
C. Make every effort to attack
D. Supplement blood
E. Escape

If (lifecycle> 20)
{
If (RAND () % 10! = 1) // The probability of Normal attacks is 9/10
Common attacks;
Else
Cast a magic attack;
}
Else
{
Switch (RAND () % 5)
{
Case0:
Common attacks;
Break;
Case 1:
Cast a magic attack;
Break;
Case 2:
Make every effort to attack;
Break;
Case 3:
Supplement blood;
Break;
Case 4:
Escape;
If (RAND () % 3 = 1) // The probability of a successful escape is 1/3.
Escape successful;
Else
Failed to escape;
Break;
}
}

}

Related Article

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.