⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gapishow.cpp

📁 繪製星盤的遊戲
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        y += cyFont;

        lstrcpy (szTxt, PARSEFLAG(gxdp.ffFormat, kfLandscape));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfPalette));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfDirect));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfDirect555));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfDirect565));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfDirect888));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfDirect444));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
        lstrcpy (szTxt, PARSEFLAG (gxdp.ffFormat, kfDirectInverted));
        if (i = lstrlen (szTxt)) {  //Assignment in if
            ExtTextOut (hdc, 20, y, 0, &rect, szTxt,  i, 0);
            y += cyFont;
        }
    }
    EndPaint (hWnd, &ps); 
    hOldPlaying = fPlaying;
    return 0;
}
//----------------------------------------------------------------------
// DoSettingChangeMain - Process WM_SETTINGCHANGE message for window.
//
LRESULT DoSettingChangeMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                             LPARAM lParam) {

    // Notify shell of our WM_SETTINGCHANGE message.
    SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
    return 0;
}
//----------------------------------------------------------------------
// DoActivateMain - Process WM_ACTIVATE message for window.
//
LRESULT DoActivateMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                        LPARAM lParam) {

    // If activating, restore any hibernated stuff.
    if ((LOWORD (wParam) != WA_INACTIVE) && fHibernated) {
        fHibernated = FALSE;
    }
    // Notify shell of our activate message.
    SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoHibernateMain - Process WM_HIBERNATE message for window.
//
LRESULT DoHibernateMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                         LPARAM lParam) {

    // If not the active window, reduce our memory footprint.
    if (GetActiveWindow() != hWnd) {
        fHibernated = TRUE;
    }
    return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) {
    if (fPlaying) {
        // Clean up if playing game.
        KillTimer (hWnd, ID_TIMER);
        GXCloseInput();            
    }
    GXCloseDisplay();        
    PostQuitMessage (0);
    return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    SendMessage (hWnd, WM_CLOSE, 0, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandPlay - Process Play command.
//
LPARAM DoMainCommandPlay (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    if (!fPlaying) {
        if (!(gxdp.ffFormat & (kfDirect555 | kfDirect565))) {
            MessageBox (hWnd, 
                        TEXT ("GAPIShow supports only 16 bpp displays"),
                        szAppName, MB_OK);
            return 0;
        }
        InitGame (hWnd);
    } else {
        EndGame (hWnd);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Tools | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    // Use DialogBox to create modal dialog.
    DialogBox (hInst, MAKEINTRESOURCE (IDD_ABOUT), hWnd, AboutDlgProc);
    return 0;
}
//======================================================================
// About Dialog procedure
//
BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam,
                            LPARAM lParam) {
    switch (wMsg) {
    case WM_INITDIALOG:
        {
            SHINITDLGINFO idi;
            idi.dwMask = SHIDIM_FLAGS;
            idi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN |
                          SHIDIF_SIPDOWN;
            idi.hDlg = hWnd;
            SHInitDialog (&idi);
        }
        break;
    case WM_COMMAND:
        switch (LOWORD (wParam)) {
            case IDOK:
            case IDCANCEL:
                EndDialog (hWnd, 0);
                return TRUE;
        }
        break;
    }
    return FALSE;
}
//----------------------------------------------------------------------
// MyCreateMenuBar - Create the menu bar for the program.
//
HWND MyCreateMenuBar (HWND hWnd) {
    SHMENUBARINFO mbi;

    // Create a menu bar.
    memset(&mbi, 0, sizeof(SHMENUBARINFO)); // Zero structure
    mbi.cbSize = sizeof(SHMENUBARINFO);     // Size field
    mbi.hwndParent = hWnd;                  // Parent window
    mbi.nToolBarId = IDR_MENUBAR1;          // ID of toolbar resource
    mbi.hInstRes = hInst;                   // Inst handle of app

    // Create menu bar and check for errors.
    if (!SHCreateMenuBar(&mbi))
        return 0;
    return mbi.hwndMB;
}
//----------------------------------------------------------------------
// InitGame - Start game by capturing the keyboard, drawing star field, 
// and starting timer.
//
int InitGame (HWND hWnd) {
    PBYTE pBuff;

    // Make our window topmost and cover the entire screen.
    GetWindowRect (hWnd, &rectNorm);
    SetWindowPos (hWnd, HWND_TOPMOST, 0, 0, CxScreen, CyScreen, 0);
    DestroyWindow (hwndMenuBar);
    ValidateRect (hWnd, NULL);

    // Grab the keyboard.
    GXOpenInput();
    fPlaying = TRUE;

    // Initialize the display.
    pBuff = (PBYTE)    GXBeginDraw();
    if (pBuff) {
        ClearScreen_16 (pBuff, RGB (0, 0, 0));
        InitScreen_16 (pBuff);
        GXEndDraw();
    }
    // Start a very fast timer.
    SetTimer (hWnd, ID_TIMER, 10, NULL);
    return 0;
}
//----------------------------------------------------------------------
// EndGame - Clean up by re-creating the main window.
//
int EndGame (HWND hWnd) {
    fPlaying = FALSE;
    KillTimer (hWnd, ID_TIMER);
    GXCloseInput();

    // Create menu bar and check for errors.
    hwndMenuBar = MyCreateMenuBar (hWnd);
    if (!hwndMenuBar) {
        MessageBox (hWnd, TEXT("Couldn\'t create menu bar"), 
                    szAppName, MB_OK);
    }
    // Restore our window to the old position.
    SetWindowPos (hWnd, HWND_NOTOPMOST, rectNorm.left, rectNorm.top,
                  rectNorm.right - rectNorm.left, 
                  rectNorm.bottom - rectNorm.top, 0);
    InvalidateRect (hWnd, NULL, TRUE);
    return 0;
}
//----------------------------------------------------------------------
// ClearScreen_16 - 16 bpp version of clear screen
//
int ClearScreen_16 (PVOID lpBuff, COLORREF rgb) {
    WORD wPixel = 0;
    DWORD x, y;
    PBYTE pbLine, pbPixel;

    // Verify that we have a valid frame buffer.
    if (!lpBuff) return 0;

    // Format pixel from colorref data.    
    if (gxdp.ffFormat | kfDirect565) {
        wPixel = (WORD) ((GetRValue(rgb) >> 3) << 11 | 
                         (GetGValue(rgb) >> 2) << 5  | 
                         (GetBValue(rgb) >> 3));
    } else if (gxdp.ffFormat | kfDirect555) {
        wPixel = (WORD) ((GetRValue(rgb) >> 3) << 10 | 
                         (GetGValue(rgb) >> 3) << 5  | 
                         (GetBValue(rgb) >> 3));
    }
    // Do rows.
    pbLine = (PBYTE)lpBuff;
    for (y = 0; y < gxdp.cyHeight; y++) {

        // Do columns.
        pbPixel = pbLine;
        for (x = 0; x < gxdp.cxWidth; x++) {
            // Cast ptr and write.  
            *(PWORD)pbPixel = wPixel;
            pbPixel += gxdp.cbxPitch;   // Move to the next pixel.
        }
        pbLine += gxdp.cbyPitch;        // Move to the next line.
    }
    return 0;
}
//----------------------------------------------------------------------
// DrawScreen_16 - Compute new position for each star; redraw if it
// has moved. 
//
int DrawScreen_16 (PVOID lpBuff, int dx, int dy, int dv) {
    int i, nOldDist;    
    PBYTE pNew = 0;
    PBYTE pOld = 0;
    static nOldDX, nOldDY;

    for (i = 0; i < MAX_STARS; i++) {
        // Remove old star.
        if (((ptStars[i].x >> SHFT) < CxScreen+2) && 
            ((ptStars[i].y >> SHFT) < CyScreen+2))
            pOld = (PBYTE)lpBuff + 
                   (ptStars[i].x >> SHFT) * gxdp.cbxPitch + 
                   (ptStars[i].y >> SHFT) * gxdp.cbyPitch;
        nOldDist = ptStars[i].dist;
        // Update pos. New pos is related to its pos from middle of the
        // screen. This pushes the stars to the edges.  
        ptStars[i].x += ((ptStars[i].x-MID_X)/4 * dv + dx)/16+1;
        ptStars[i].y += ((ptStars[i].y-MID_Y)/4 * dv + dy)/16+1;
        ptStars[i].dist++;

        // If new position off screen, regenerate the star.
        if ((ptStars[i].x < 0) || (ptStars[i].y < 0) ||
            ((ptStars[i].x >> SHFT) >= CxScreen) || 
            ((ptStars[i].y >> SHFT) >= CyScreen)) {

            ptStars[i].x = Random() & MAX_X-1;
            ptStars[i].y = Random() & MAX_Y-1;
            ptStars[i].dist = 0;
        }
        // Compute pointer to new star.
        if (((ptStars[i].x >> SHFT) < CxScreen) && 
            ((ptStars[i].y >> SHFT) < CyScreen)) 
            pNew = (PBYTE)lpBuff + 
                   (ptStars[i].x >> SHFT) * gxdp.cbxPitch + 
                   (ptStars[i].y >> SHFT) * gxdp.cbyPitch;
        // Don't redraw star if in same place.
        if (pNew != pOld) {
            if (pOld) {
                *(PWORD)pOld = 0;
                // Stars older than 15 generations get bigger.
                if (nOldDist > 15) {
                    *(PWORD)(pOld + gxdp.cbxPitch) = 0;
                    *(PWORD)(pOld + gxdp.cbyPitch) = 0;
                    *(PWORD)(pOld + gxdp.cbxPitch + 
                               gxdp.cbyPitch) = 0;
                }
            } 
            if (pNew) {
                *(PWORD)pNew = 0xffff;
                if (ptStars[i].dist > 15) {
                    *(PWORD)(pNew + gxdp.cbxPitch) = 0xffff;
                    *(PWORD)(pNew + gxdp.cbyPitch) = 0xffff;
                    *(PWORD)(pNew + gxdp.cbxPitch + 
                              gxdp.cbyPitch) = 0xffff;
                }
            }
        }
    }
    return 0;
}
//----------------------------------------------------------------------
// InitScreen_16 - Initialize each star position and draw it.
//
int InitScreen_16 (PVOID lpBuff) {
    int i;    
    PBYTE pNew = 0;
    TCHAR szTxt[128];

    for (i = 0; i < MAX_STARS; i++) {
        // Initialize star.
        ptStars[i].x = Random() & MAX_X-1;
        ptStars[i].y = Random() & MAX_Y-1;
        ptStars[i].dist = 0;

        // If on the screen, draw star.
        if (((ptStars[i].x >> SHFT) < CxScreen) && 
            ((ptStars[i].y >> SHFT) < CyScreen)) 
            pNew = (PBYTE)lpBuff + 
                   (ptStars[i].x >> SHFT) * gxdp.cbxPitch + 
                   (ptStars[i].y >> SHFT) * gxdp.cbyPitch;
        __try {
        if (pNew)
            *(PWORD)pNew = 0xffff;
        }
        __except (EXCEPTION_EXECUTE_HANDLER) {
            wsprintf (szTxt, TEXT("Exception %d  %08x  (%d,%d)"), i, 
                      pNew, ptStars[i].x, ptStars[i].y);
            MessageBox (NULL, szTxt, szAppName, MB_OK);
            break;
        }
    }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -