游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3379|回复: 3

为什么背景没有显示?

[复制链接]

5

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
发表于 2004-7-14 10:00:00 | 显示全部楼层 |阅读模式
我是新手,下面的例子是我参考别人的程序修改的,背景显示不出来,不知道是为什么,还望前辈们指点.

// Game1.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "Game1.h"
#define MAX_LOADSTRING 100

//HOHO自处理字体句柄
LP_FONT g_pFont = NULL;
RECT rect;
HWND g_hWnd;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
bool g_bActive = true;
POINT pointMousePos;
bool g_bIsLeftDown = false;
bool g_bIsRightDown = false;

LP_BITMAPX g_pMouseBitmap = NULL;
LP_BITMAPX g_pBitmapBack = NULL;
LP_BITMAPX g_pBitmapSprite = NULL;

float Sprite_x,Sprite_y; //精灵位置
//注意其坐标系以背景左上角为原点
int TargetX,TargetY; //精灵移动目标
int Sprite_FootStatus; //精灵脚状态 (-1~3,-1表示精灵停止
//0~3代表sprite.bmp中的相应位置)
int Sprite_Face; //精灵朝向 (0~7)

int SW[6] = {800,800,1280,64,256,32};
int SH[6] = {600,600,800,62,512,26};

// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

long SystemMessage(void);
void Release(void);
void GameInit();
void MakeRect(int left, int top, int right, int bottom); //创建矩形的函数
void Blt(int x,int y,int src_id,int dest_id,LP_BITMAPX g_pBitMap);
void Game_Refresh();
void Sprite_Show(LP_BITMAPX g_pBitMap); //显示精灵
void Sprite_MoveTo(int x1,int y1); //移动精灵到某位置


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
  // TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;

// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GAME1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GAME1);

GameInit();

// 主消息循环:
while ( 1 )
{
SystemMessage();

if(g_bActive) //激活状态
{
Game_Refresh();
}
else //非激活状态
Sleep(1);
}
   
Release();
return 0;
}

void Release()
{
// 释放数据
SAFE_DELETE( g_pDisplay );
SAFE_DELETE( g_pMouse );
msReleaseFont( g_pFont );

exit(0);
}

long SystemMessage()
{
MSG msg;
if( GetAsyncKeyState(VK_ESCAPE) )
{
Release();
}

if(g_pMouse != NULL)
g_pMouse->RecieveMouseInput(); // 获得鼠标数据

// 通过图形底层来获得鼠标在窗口的坐标
GetGraphics()->GetRectPoint( &pointMousePos );

if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( !GetMessage( &msg, NULL, 0, 0 ) )
{
Release();
}
// Translate and dispatch the message
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return 1;
}
return 0;
}


//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释:
//
//    仅当希望在已添加到 Windows 95 的
//    “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
//    才需要此函数及其用法。调用此函数
//    十分重要,这样应用程序就可以获得关联的
//   “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GAME1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;//(LPCTSTR)IDC_GAME1;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HANDLE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{

   hInst = hInstance; // 将实例句柄存储在全局变量中

   g_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!g_hWnd)
   {
      return FALSE;
   }

   ShowWindow(g_hWnd, nCmdShow);
   UpdateWindow(g_hWnd);
   
   return TRUE;
}

//
//  函数: WndProc(HWND, unsigned, WORD, LONG)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND - 处理应用程序菜单
//  WM_PAINT - 绘制主窗口
//  WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_SETCURSOR: // 屏蔽鼠?
{
SetCursor( NULL );
}
break;
case WM_MOVE: // 窗口移动时的位置更新
{
if( GetGraphics() != NULL)
GetGraphics()->UpdateBounds();
}
break;
case WM_ACTIVATE: // 窗口的活???B
{
switch((LOWORD(wParam)))
{
case WA_ACTIVE:
case WA_CLICKACTIVE:
g_bActive = true;
break;
case WA_INACTIVE:
g_bActive = false;
break;
default:
break;
}

if( GetGraphics() != NULL )
GetGraphics()->Restore();
}
break;
case WM_LBUTTONUP:
Sprite_MoveTo(LOWORD(lParam)-320+Sprite_x,HIWORD(lParam)-240+Sprite_y);
break;
case WM_KEYDOWN:
switch((LOWORD(wParam)))
{
case VK_ESCAPE:
PostMessage(hWnd, WM_CLOSE, 0, 0);   // 向窗口句柄hWnd发送“关闭窗口”消息
break;
}
break;
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_about:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// “关于”框的消息处理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

//游戏初始化
void GameInit()
{
g_pDisplay = CreatePlane2D( g_hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, true ); // g_hWnd=窗口句柄 SCREEN_WIDTH=窗口?? SCREEN_HEIGHT=窗口高 true=窗口/false=全屏

g_pBitmapSprite = GetGraphics()->CreateBitmapFromBMP( "Resource\\sprite.bmp" );
g_pBitmapBack = GetGraphics()->CreateBitmapFromBMP("Resource\\Tile.bmp");

//设置颜色健
   g_pBitmapSprite->SetColorKey(RGB2Hi(0,0,0));
   g_pBitmapBack->SetColorKey(RGB2Hi(255,0,255));

   //初始化HOHO字体
   g_pFont = msCreateFont( "Resource\\Font12.dat" );

   g_pMouseBitmap = GetGraphics()->CreateBitmapFromTGA( "Resource\\Cursor.tga" ); // ?入鼠????载入Alpha通道图像)
   
   g_pMouse = CreateMouse( hInst, g_hWnd );

   Sprite_x = 640;
   Sprite_y = 100;
   Sprite_FootStatus = -1;
   TargetX = 640;
   TargetY = 100;
   Sprite_Face = 0;

//-----------------------------初始化背景----------------------------
srand(GetTickCount()); //初始化随机数
for (int i=0;i<20;i++)
for (int j=0;j<20;j++)
{
int ran=rand()%2;
MakeRect(0,31,64,31+31);
GetGraphics()->DrawBitmapMMX( 608-32*i+32*j,16*i+16*j, g_pBitmapBack, SCREENBUFFER, &rect, true );
}
   

}

void Game_Refresh()
{
    //-----------------------------得到当前时间--------------------------
static int count=0;
static DWORD timee=0,timeb=0;
static float time=0;
count+=timee-timeb;

timeb=GetTickCount();
//-----------------------------刷新屏幕------------------------------
//-----------------------------先清屏幕------------------------------
GetGraphics()->ClearScreenMMX(0); // 清除背???? SCREENBUFFER

//-----------------------------放背景--------------------------------
int mx=Sprite_x+320;
int my=Sprite_y+240;
MakeRect(mx-640,my-480,mx,my);
Blt(0,0,2,1,g_pBitmapBack);
//-----------------------------放精灵--------------------------------
Sprite_Show(g_pBitmapSprite);
//-----------------------------放鼠标--------------------------------
// 绘制Alpha通道图像
// 带Alpha通道的图形光标
GetGraphics()->DrawBitmapAlphaChannelMMX( pointMousePos.x-14, pointMousePos.y-13, g_pMouseBitmap, SCREENBUFFER );

//-----------------------------翻页并完成刷新------------------------
// 更新缓冲区
g_pDisplay->UpdateScreen( );
g_pDisplay-&gtresent( );

//-----------------------------移动精灵------------------------------
if (Sprite_FootStatus!=-1)
{
if (count>100) //防止精灵的脚转换过快
{
Sprite_FootStatus=(Sprite_FootStatus+1)%4;
count=0;
}
float dx,dy;
dx=TargetX-Sprite_x;
dy=TargetY-Sprite_y;
if ((abs(dx)>1)||(abs(dy)>1)) //如还未到目标
{
float newx=Sprite_x+0.15*time*dx/(sqrt(dx*dx+dy*dy));
float newy=Sprite_y+0.15*time*dy/(sqrt(dx*dx+dy*dy));
Sprite_x=newx;
Sprite_y=newy;
}
else //已到目标
{
Sprite_FootStatus=-1;
}
}
//-----------------------------得到当前时间--------------------------
timee=GetTickCount();

}
//帮助创建矩形的函数
void MakeRect(int left, int top, int right, int bottom)
{
rect.bottom=bottom;
rect.left=left;
rect.right=right;
rect.top=top;
}

void Blt(int x,int y,int src_id,int dest_id,LP_BITMAPX g_pBitMap)
{
int rl,rt,tx1,tx2,ty1,ty2,tl,tt;
RECT rect2=rect;

rl=rect.left;
rt=rect.top;

if (rect.left>=SW[src_id])
goto noblt; //越界,不Blt
if (rect.top>=SH[src_id])
goto noblt;
if (rect.right<=0)
goto noblt;
if (rect.bottom<=0)
goto noblt;

if (rect.left<0)
rect.left=0;
if (rect.top<0)
rect.top=0;
if (rect.right>SW[src_id])
rect.right=SW[src_id];
if (rect.bottom>SH[src_id])
rect.bottom=SH[src_id];

tx1=x+rect.left-rl;
ty1=y+rect.top-rt;
tx2=x+rect.right-rl;
ty2=y+rect.bottom-rt;

if (tx2<=0)
goto noblt;
if (ty2<=0)
goto noblt;
if (tx1>=SW[dest_id])
goto noblt;
if (ty1>=SH[dest_id])
goto noblt;

tl=tx1;
tt=ty1;

if (tx1<0)
tx1=0;
if (ty1<0)
ty1=0;
if (tx2>SW[dest_id])
tx2=SW[dest_id];
if (ty2>SH[dest_id])
ty2=SH[dest_id];

rl=rect.left;
rt=rect.top;

rect.left=tx1-tl+rl;
rect.top=ty1-tt+rt;
rect.right=tx2-tl+rl;
rect.bottom=ty2-tt+rt;

GetGraphics()->DrawBitmapMMX( tx1,ty1, g_pBitMap, SCREENBUFFER, &rect, true );
noblt:
rect=rect2;
}

void Sprite_Show(LP_BITMAPX g_pBitMap)
{
int sm=Sprite_FootStatus;
if (sm==-1)
sm=0;
MakeRect(sm*64,Sprite_Face*64,sm*64+64,Sprite_Face*64+64);
Blt(288,192,4,1,g_pBitMap);
}

void Sprite_MoveTo(int x1,int y1)
{
int oldface=Sprite_Face;
int dx,dy;
dx=x1-Sprite_x;
dy=y1-Sprite_y;
if ((abs(dx*12)<abs(dy*5)))
if (dy>0)
Sprite_Face=7;
else
Sprite_Face=6;
else
{
if ((abs(dx*12)>abs(dy*29)))
if (dx>0)
Sprite_Face=5;
else
Sprite_Face=4;
else
{
if (dx>0)
if (dy>0)
Sprite_Face=3;
else
Sprite_Face=1;
else
if (dy>0)
Sprite_Face=0;
else
Sprite_Face=2;
}
}
TargetX=x1;
TargetY=y1;
if (Sprite_FootStatus==-1)
Sprite_FootStatus=0; //作移动标记
}
[em24]

1万

主题

1万

帖子

2万

积分

管理员

中级会员

Rank: 9Rank: 9Rank: 9

积分
20356
QQ
发表于 2004-7-14 10:50:00 | 显示全部楼层

Re:为什么背景没有显示?

汗!那么长一串代码。。。

能否提出去绘制背景的部分代码?

5

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
 楼主| 发表于 2004-7-14 10:55:00 | 显示全部楼层

Re:为什么背景没有显示?

//-----------------------------初始化背景----------------------------
srand(GetTickCount()); //初始化随机数
for (int i=0;i<20;i++)
for (int j=0;j<20;j++)
{
int ran=rand()%2;
MakeRect(0,31,64,31+31);
GetGraphics()->DrawBitmapMMX( 608-32*i+32*j,16*i+16*j, g_pBitmapBack, SCREENBUFFER, &rect, true );
}

上面的是初始化函数里GameInit();
下面的代码段是在刷新函数Game_Refresh()里放背景:
//-----------------------------放背景--------------------------------
int mx=Sprite_x+320;
int my=Sprite_y+240;
MakeRect(mx-640,my-480,mx,my);
Blt(0,0,2,1,g_pBitmapBack);

//-----------------------------初始化背景----------------------------
srand(GetTickCount()); //初始化随机数
for (int i=0;i<20;i++)
for (int j=0;j<20;j++)
{
int ran=rand()%2;
MakeRect(0,31,64,31+31);
GetGraphics()->DrawBitmapMMX( 608-32*i+32*j,16*i+16*j, g_pBitmapBack, SCREENBUFFER, &rect, true );
}

上面的代码段是初始化函数GameInit()里初始化地图(背景);

下面的代码段是在刷新函数Game_Refresh()里放背景:
//-----------------------------放背景--------------------------------
int mx=Sprite_x+320;
int my=Sprite_y+240;
MakeRect(mx-640,my-480,mx,my);
Blt(0,0,2,1,g_pBitmapBack);

在初始化时,显示出地图,然后在刷新函数里可以显示地图(背景),同时可以根据点击鼠标,使其能移动,这样看起来就象是精灵在走动一样.现在初始化时不能显示背景,刷新时背景不会动,而且点击鼠标后没,精灵一直在做走动的动作,不能停下来.

5

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
 楼主| 发表于 2004-7-20 16:57:00 | 显示全部楼层

Re:为什么背景没有显示?

搞定!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-5-13 02:16

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表