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

📄 bcuearthpong.cpp

📁 这是用C++BUILDER编写的开发3D界面的小程序
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "BCUEarthPong.h"
#include <mmsystem.h>
//---------------------------------------------------------------------------
#pragma package(smart_init);
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
    PrimaryCanvas = AttachedCanvas = NULL;
    Application->OnIdle = IdleLoop;
    frame = 0;
    globeX = globeY = 0;
    yDir = xDir = 5;
    EvenFrame = true;
}
//-------------------------------------------------------------------
void __fastcall TForm1::InitializeDirectDraw()
{
    HRESULT hr;
    hr = DirectDrawCreate(NULL, &DirectDraw, NULL);
    if(FAILED(hr))
        ShowMessage("DirectDrawCreate failed");
    hr = DirectDraw->SetCooperativeLevel(Handle,
                 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
    if(FAILED(hr))
        DisplayError("SetCooperativeLevel failed");
    hr = DirectDraw->SetDisplayMode(640, 480, 8);
    if(FAILED(hr))
        DisplayError("SetDisplayMode failed");
    SetupPrimaryCanvas();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DisplayError(char* error)
{
    DirectDraw->FlipToGDISurface();
    MessageBox(Handle, error, "Failure", MB_OK);
    Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetupPrimaryCanvas()
{
    DDSURFACEDESC ddsd;
    ZeroMemory(&ddsd, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount = 1;
    DirectDraw->CreateSurface(&ddsd, &PrimaryCanvas, NULL);
    DDSCAPS ddscaps;
    ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
    HRESULT result = PrimaryCanvas->GetAttachedSurface(&ddscaps, &AttachedCanvas);
    if(FAILED(result))
    {
        DirectDraw->FlipToGDISurface();
        MessageBox(Handle, "", "GetAttachedsurface failed", MB_OK);
    }
    if(!PrimaryCanvas)
    {
        DirectDraw->FlipToGDISurface();
        MessageBox(Handle, "", "Primary NULL", MB_OK);
    }
     if(!AttachedCanvas)
    {
        DirectDraw->FlipToGDISurface();
        MessageBox(Handle, "", "Attached NULL", MB_OK);
    }
    
}

void __fastcall TForm1::FormActivate(TObject *Sender)
{
    InitializeDirectDraw();
    LoadArt();
    Label1->Hide();
    DirectDraw->FlipToGDISurface();
    MessageBox(Handle, "Ready to Save Earth?", "Good Luck :-)", MB_OK);
    ShowCursor(false);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::LoadArt()
{
    LoadBackgroundArt();
    LoadSprites();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LoadBackgroundArt()
{
   BackgroundCanvas = DDLoadBitmap(DirectDraw, "spiral", 0, 0);
   BackgroundPalette = DDLoadPalette(DirectDraw, "spiral");
   BackgroundCanvas->SetPalette(BackgroundPalette);

   DDSURFACEDESC ddsd;
   ddsd.dwSize = sizeof(ddsd);
   ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
   ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
   ddsd.dwHeight = 15;
   ddsd.dwWidth = 8;
   DirectDraw->CreateSurface(&ddsd, &NumHitsCanvas, NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LoadSprites()
{

    for(int i = 0; i < NUM_GLOBE_SPRITES; i++)
    {
        char buf[12];
        wsprintf(buf,"globe%d", i + 1);
    	Globe[i] = DDLoadBitmap(DirectDraw, buf, 0, 0);
    	GlobePalette = DDLoadPalette(DirectDraw, buf);
    	Globe[i]->SetPalette(GlobePalette);
    	DDSetColorKey(Globe[i], CLR_INVALID);
    }
    Paddle = DDLoadBitmap(DirectDraw, "paddle", 0, 0);
    PaddlePalette = DDLoadPalette(DirectDraw, "paddle");
    Paddle->SetPalette(PaddlePalette);
    DDSetColorKey(Paddle, CLR_INVALID);
    paddleY = 432;
    paddleX = 640/2 - 32;
}

//---------------------------------------------------------------------------
void __fastcall TForm1::IdleLoop(TObject*, bool& done)
{
    done = false;

    AttachedCanvas->Blt(NULL, BackgroundCanvas, NULL, DDBLT_WAIT, NULL);
    AttachedCanvas->BltFast(paddleX, paddleY, Paddle, NULL, DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);

    if(frame ==  0 )
       	frame = NUM_GLOBE_SPRITES - 1;
    if (globeX <= 0)
    {
        xDir = 5;
    }
    if(globeX >= 640 - 42)
    {
    	xDir = -5;
    }
    if (globeY <= 0)
    {
    	yDir = 5;
    }
    if (globeY >= 480 - 42)
    {
        DirectDraw->FlipToGDISurface();
        ShowCursor(true);

        if(MessageBox(Handle, "Continue?", "You Lost Earth :-(", MB_OKCANCEL) == ID_OK)
        {
            globeX = globeY = 0;
            ShowCursor(false);
            hits = 0;
        }
        else
            Close();
    }
    if(globeX <= paddleX + 50 && globeX >= paddleX - 14
              && globeY >= paddleY - 24 && globeY <= paddleY)
    {
        PlaySound("Utopia Default.wav", NULL , SND_ASYNC | SND_FILENAME);
        hits++;
        yDir = -5;
    }



    globeX += xDir;
    globeY += yDir;

    if(EvenFrame)
    {
         EvenFrame = !EvenFrame;
         frame--;
    }
    else
        EvenFrame  = !EvenFrame;

    AttachedCanvas->BltFast(globeX, globeY, Globe[frame], NULL, DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);

    //DrawNumHits(hits);

    while(1)
    {
        HRESULT result = PrimaryCanvas->Flip(NULL, DDFLIP_WAIT);
        if(result == DD_OK)
          	break;
        if(result == DDERR_SURFACELOST)
        {
            PrimaryCanvas->Restore();
            AttachedCanvas->Restore();
            break;
        }
        if(result != DDERR_WASSTILLDRAWING)
           	break;
    }
}

void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
	if(Key == VK_ESCAPE)
    	Close();	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X,
        int Y)
{
        paddleX = X;
        if(paddleX <= 0)
            paddleX = 0;
        if(paddleX >= 640 - 64)
            paddleX = 640 - 64;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    PlaySound("Utopia Default.wav", NULL , SND_ASYNC | SND_FILENAME);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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