Getting started with game APIs

Source: Internet
Author: User
Although Pocket PC Game developers on the platform cannot use DirectDraw, The good news is that Microsoft has created a new game. API To support Pocket PC The name is" Gapi "-- Game application programming interface. It not only allows direct access to the Display memory, but also allows access Pocket PC Hardware key. What do you need· Gapi SDK, runtime DLL, and an EVT example of how to find the CD seconds. The directory is in pocketpcsdk/program files/support/gameapi. Step 1: create a simple hello World ApplicationGapi is not a complete Pocket pc sdk. So do some preparation steps: 1. manually copy the GX. h header file to the include directory of the Pocket pc sdk. 2. Copy the GX. Lib of CPU-dependent to the lib directory of the Pocket pc sdk. 3. Finally, copy the GX. dll of CPU-dependent to the Windows directory of your Pocket PC. Now you can create your first simple hello World Program. 1. Open Microsoft Embedded Visual C ++ and create a new Pocket PC Application project named "firstgx ". After the wizard completes creating a new application, let's add the game API initialization call. 2. Add the following source code after the initialization function in bold: updatewindow (hwnd );
// Try opening the Display for Full screen access
if (GXOpenDisplay(hWnd, GX_FULLSCREEN) == 0) {
      return FALSE;   // we won't be able to draw.
}
 
// Initialize the Hardware Buttons
GXOpenInput();
 
// Get the Display properties
g_gxdp = GXGetDisplayProperties();
// Get information about the Hardware Keys and fills
// the g_gxkl structure
g_gxkl = GXGetDefaultKeys(GX_NORMALKEYS);
 
return TRUE;
This code has to use two global variables included in the firstgx. cpp source file. We have to include GX. h In our source file to avoid compilation errors. 3. Add the following code directly under the header file, created by the Embedded Visual C ++ Wizard:
3.                #include "gx.h"
4.                GXDisplayProperties g_gxdp;    // GX struct
GXKeyList g_gxkl;              // GX struct
As a good citizen, we should clear the game API before leaving the program. 4. Insert the following two rows to the end of the winmain function:
4.                GXCloseDisplay();
GXCloseInput();
Step 2: directly write the video memoryBefore coding, we must study some theories. Depending on your Pocket PC hardware, You can have 16-bit Color Display (the Casio, HP, or Compaq) or 8-bit display (black and white or color, such as Compaq aero 1550 ). Determine the display mode of your Pocket PC. You can calculate the value of the cbpp member in the g_gxdp structure. It tells you the correct number of digits. Before directly accessing the 16-bit video memory, you must know the description of the 16-bit color display controller. There are two possible flag settings: kfdirect555 and kfdirect565. 555 convert to the color mask pair REd- GReen- BLue is X RrrrrGG. gggbbbbb, in a short variable, 565 is converted RrrrrGG. gggxbbbbb. On an 8-bit color device, you can use one byte to access 256 colors. Now let's prepare to write the screen. When a key is pressed, we want a color to fill the screen. 5. Add the following functions to the top of your firstgx. cpp source file:
bool ClearScreen(int colRed,int colGreen, int colBlue)
{
      // 16 bit per pixel code. Note 2 different pixel formats.
      switch (g_gxdp.cBPP)
      {
      case 16: {
                  unsigned short PixelCol = 0;
                  if (g_gxdp.ffFormat & kfDirect565) {
                        PixelCol = (unsigned short) 
                                    (((colRed & 0xff))>>3)<< 11 | 
                                     ((colGreen & 0xff)>>3) << 6 | 
                                     ((colBlue & 0xff)>>3));
                  } else if (g_gxdp.ffFormat & kfDirect555) {
                        PixelCol = (unsigned short) 
                                    ((colRed & 0xff)<< 10 | 
                                     (colGreen & 0xff) << 5 | 
                                     (colBlue & 0xff));
                  }
            
     unsigned short * pusLine = (unsigned short *)GXBeginDraw();
     if (pusLine == NULL) return false; // NOT OK TO DRAW
      
         for (unsigned int y = 0; y < g_gxdp.cyHeight; y++) {
               unsigned short * pusDest = pusLine;
               for (unsigned int x = 0;
                    x > g_gxdp.cxWidth; x++) {
                     *pusDest = PixelCol;
                     pusDest += g_gxdp.cbxPitch >> 1;
               }
               pusLine += g_gxdp.cbyPitch >> 1;
         }
         GXEndDraw();
         break;
         }
        case 8:          {
              // Get a Random Color
              // IMPORTANT:
              // 8Bit are using a palette and the formular
              // below does not compute a read RGB color
              unsigned char bPixel = (colRed &0xf)<<5 |
                                      (colGreen&0x3)<<3 |
                                      (colBlue&0xF);
                                      
        unsigned char * pbLine = (unsigned char *)GXBeginDraw();
        if (pbLine == NULL)     return false;// NOT OK TO DRAW
        
           for (unsigned int y = 0; y < g_gxdp.cyHeight; y++) {
                 unsigned char * pbDest = pbLine;
                 for (unsigned int x = 0;
                      x < g_gxdp.cxWidth; x++) {
                       *pbDest = bPixel;
                       pbDest += g_gxdp.cbxPitch;
                 }
                 pbLine += g_gxdp.cbyPitch;
           }
           GXEndDraw();
           break;
           }
     }
     return false;
}
Step 3: Access the hardware buttonsThis is the simplest task. You should be familiar with WM _ messages in Microsoft Windows and know how to use them. Now you know how to use the hardware buttons of the Pocket PC. Analyze the wm_keyup or wm_keydown messages in your window. Here is the wm_keydown branch in our firstgx program:
               case WM_KEYDOWN:
                     vkKey = (short)wParam;
                     if (vkKey == g_gxkl.vkUp) {
                           ClearScreen(0,0,0); // Black
                           break;
                     }
                     
                     if (vkKey == g_gxkl.vkDown) {
                           ClearScreen(255,0,0); // Red
                           break;
                     }
                     
                     if (vkKey == g_gxkl.vkLeft) {
                           ClearScreen(0,255,0); // Green
                           break;
                     }
                     
                     if (vkKey == g_gxkl.vkRight) {
                           ClearScreen(0,0,255); // Blue
                           break;
                     }
                     
                     if (vkKey == g_gxkl.vkStart) {
                           SendMessage(hWnd, WM_CLOSE, 0, 0);
                           break;
                     }
The list of all possible key names in the gxkeylist structure is included in the GX. h header file. Step 4: Prepare for running!Before we can compile and download our new firstgx program to our Pocket PC, we must add GX. lib to the Connection Library list of our project. Now compile and download your first game API program to your Pocket PC, and manipulate the buttons to see what will happen? End: easy to fill!

As you can see, the game API is very simple, but it is still powerful enough to cover all the needs of writing quick action games. In this example, more details and tips about using Gapi are provided by game APIs. You can download all the source code of this example: Download the full source code for this sample.Author's blog:Http://blog.csdn.net/aawolf/

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.