📄 frame.cpp
字号:
RECT dest_rect, // the destination rectangle
source_rect; // the source rectangle
// fill in the destination rect
dest_rect.left = x;
dest_rect.top = y;
dest_rect.right = x+width-1;
dest_rect.bottom = y+height-1;
// fill in the source rect
source_rect.left = 0;
source_rect.top = 0;
source_rect.right = width-1;
source_rect.bottom = height-1;
// test transparency flag
if (transparent)
{
// enable color key blit
// blt to destination surface
if (FAILED(dest->Blt(&dest_rect, source,
&source_rect,(DDBLT_WAIT | DDBLT_KEYSRC),
NULL)))
return(0);
} // end if
else
{
// perform blit without color key
// blt to destination surface
if (FAILED(dest->Blt(&dest_rect, source,
&source_rect,(DDBLT_WAIT),
NULL)))
return(0);
} // end if
// return success
return(1);
} // end DDraw_Draw_Surface
///////////////////////////////////////////////////////////////
int Scan_Image_Bitmap(BITMAP_FILE_PTR bitmap, // bitmap file to scan image data from
LPDIRECTDRAWSURFACE7 lpdds, // surface to hold data
int cx, int cy) // cell to scan image from
{
// this function extracts a bitmap out of a bitmap file
UCHAR *source_ptr, // working pointers
*dest_ptr;
DDSURFACEDESC2 ddsd; // direct draw surface description
// get the addr to destination surface memory
// set size of the structure
ddsd.dwSize = sizeof(ddsd);
// lock the display surface
lpdds->Lock(NULL,
&ddsd,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);
// compute position to start scanning bits from
cx = cx*(ddsd.dwWidth+1) ;
cy = cy*(ddsd.dwHeight+1);
gwidth = ddsd.dwWidth;
gheight = ddsd.dwHeight;
// extract bitmap data
source_ptr = bitmap->buffer + cy*bitmap->bitmapinfoheader.biWidth+cx;
// assign a pointer to the memory surface for manipulation
dest_ptr = (UCHAR *)ddsd.lpSurface;
// iterate thru each scanline and copy bitmap
for (int index_y=0; index_y < ddsd.dwHeight; index_y++)
{
// copy next line of data to destination
memcpy(dest_ptr, source_ptr, ddsd.dwWidth);
// advance pointers
dest_ptr += (ddsd.lPitch); // (ddsd.dwWidth);
source_ptr += bitmap->bitmapinfoheader.biWidth;
} // end for index_y
// unlock the surface
lpdds->Unlock(NULL);
// return success
return(1);
} // end Scan_Image_Bitmap
///////////////////////////////////////////////////////////////
LPDIRECTDRAWSURFACE7 DDraw_Create_Surface(int width, int height, int mem_flags, int color_key = 0)
{
// this function creates an offscreen plain surface
DDSURFACEDESC2 ddsd; // working description
LPDIRECTDRAWSURFACE7 lpdds; // temporary surface
// set to access caps, width, and height
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
// set dimensions of the new bitmap surface
ddsd.dwWidth = width;
ddsd.dwHeight = height;
// set surface to offscreen plain
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags;
// create the surface
if (FAILED(lpdd->CreateSurface(&ddsd,&lpdds,NULL)))
return(NULL);
// test if user wants a color key
if (color_key >= 0)
{
// set color key to color 0
DDCOLORKEY color_key; // used to set color key
color_key.dwColorSpaceLowValue = 0;
color_key.dwColorSpaceHighValue = 0;
// now set the color key for source blitting
lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key);
} // end if
// return surface
return(lpdds);
} // end DDraw_Create_Surface
///////////////////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
// what is the message
switch(msg)
{
case WM_CREATE:
{
hdc= GetDC(hwnd);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
ShowCursor(false);
char textout[500]="遥不可及的天庭,众神各自为盟,却鲜有真正为民请命的天将。然而血气方刚的后羿却是个例外......人间再次出现妖魔,百姓于水深火热之中。后羿主动请缨,奉命于危难之间,而我们的故事也由此正式开始。本故事根据民间传说“后羿射日”与“嫦娥奔月”联合改编而成,纯属虚构。版权所有,生乐B 区1 0 1 2 室奔月工作组2 0 0 5 年 4月";
SetTextColor(hdc,RGB(0,255,0));
SetBkColor(hdc,RGB(0,0,0));
char a[2];
int x=150,y=240,i=0,j=153,k=0,n=0;
while((j--)>0)
{
if(KEYDOWN(VK_ESCAPE))
break;
a[0]=textout[i++];
a[1]=textout[i++];
TextOut(hdc,x+=20,y,a,2);
Sleep(50);
k++;
if(k==34||k==45||k==79||k==93||k==127||k==146)
{
n++;
if(n==1||n==3)
{
y+=30;
x=120;
}
else if(n==6)
{
y+=30;
x=660;
}
else
{
y+=30;
x=150;
}
}
PlaySound(MAKEINTRESOURCE(IDR_WAVE6),hinstance_app,SND_ASYNC|SND_RESOURCE|SND_LOOP);
}
ReleaseDC(hwnd,hdc);
while(1)
{
if(KEYDOWN(VK_ESCAPE))
{
break;
}
Sleep(50);
}
GameState=START_PICTURE;
ShowCursor(true);
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_LBUTTONDOWN:
{
int Bx = (int)LOWORD(lparam);
int By = (int)HIWORD(lparam);
if(GameState==START_PICTURE)
{
if(Bx>955&&By>718&&Bx<992&&By<754) //退出
{
PostQuitMessage(0);
}
else if(Bx>731&&Bx<836&&By>478&&By<525) //绘制第一幅游戏场景
{
ShowCursor(false);
GameState=GAMEMAP_1;
}
else if(Bx>729&&Bx<839&&By>558&&By<606) //装载游戏
{
FILE *fp;
fp=fopen("save\\1.save","r");
if(fp==NULL)
MessageBox(main_window_handle,"无保存记录","无保存记录",0);
int a[7]; // a0 生命 a1 ,a2 坐标 a3 经验 a4 攻击 a5 防御 a6 最大生命
fscanf(fp,"%d%d%d%d%d%d%d%d",&GameState,&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);
fclose(fp);
HouYi.setlife(a[0]);
HouYi.set_xy(a[1],a[2]);
HouYi.set_experience(a[3]);
HouYi.set_att(a[4]);
HouYi.set_rec(a[5]);
HouYi.set_max_life(a[6]);
}
}
/*if(mark==3)
{
if(Bx>952&&Bx<989&&By>662&&By<697)mark=1;
InvalidateRect(hwnd,NULL,false);
}*/
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
}
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
///////////////////////////////////////////////////////////
int Draw_Text_GDI(char *text, int x,int y,COLORREF color, LPDIRECTDRAWSURFACE7 lpdds)
{
// this function draws the sent text on the sent surface
// using color index as the color in the palette
HDC xdc; // the working dc
// get the dc from surface
if (FAILED(lpdds->GetDC(&xdc)))
return(0);
// set the colors for the text up
SetTextColor(xdc,color);
// set background mode to transparent so black isn't copied
SetBkMode(xdc, TRANSPARENT);
// draw the text a
TextOut(xdc,x,y,text,strlen(text));
// release the dc
lpdds->ReleaseDC(xdc);
// return success
return(1);
} // end Draw_Text_GDI
///////////////////////////////////////////////////////////////
int Game_Main(void *parms = NULL, int num_parms = 0)
{
// static int m=0;
int tiangong();
int gamemap2();
int gamemap3();
static int k=0;
// lookup for proper walking sequence
static int animation_seq[4] = {0,1,0,2};
// make sure this isn't executed again
if (window_closed)
return(0);
// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
{
FILE * fp;
fp=fopen("save\\1.save","w");
int a[8];
a[0]=HouYi.get_life();
a[1]=HouYi.get_x();
a[2]=HouYi.get_y();
a[3]=HouYi.get_exp();
a[4]=HouYi.get_attack();
a[5]=HouYi.get_rec();
a[6]=HouYi.get_max_life();
fprintf(fp,"%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d",GameState,a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
fclose(fp);
PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;
} // end if
if(GameState== START_PICTURE )
{
ShowCursor(true);
if(k==0)
{
if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,start_pic.palette)))
return(0);
k++;
}
DDraw_Draw_Surface(start_picture,0,0, SCREEN_WIDTH,SCREEN_HEIGHT, lpddsback,0);
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
}
if(GameState==GAMEMAP_1 )
{
ShowCursor(false);
k=0;
tiangong();
}
if(GameState==GAMEMAP_2)
{
ShowCursor(false);
k=0;
gamemap2();
/*
DDraw_Draw_Surface(lpddsbackground[3],0,0,1024,768,lpddsback,0);
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
*/
}
if(GameState== GAMEMAP_3)
{
gamemap3();
}
// return success or failure or your own return code here
return(1);
} // end Game_Main
////////////////////////////////////////////////////////////
int Game_Init(void *parms = NULL, int num_parms = 0)
{
// create IDirectDraw interface 7.0 object and test for error
if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
return(0);
// set cooperation to full screen
if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
return(0);
// set display mode to 640x480x8
if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))
return(0);
// we need a complex surface system with a primary and backbuffer
// clear ddsd and set size
DDRAW_INIT_STRUCT(ddsd);
// enable valid fields
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
// set the backbuffer count field to 1, use 2 for triple buffering
ddsd.dwBackBufferCount = 1;
// request a complex, flippable
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
// create the primary surface
if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
return(0);
// now query for attached surface from the primary surface
// this line is needed by the call
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
// get the attached back buffer surface
if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
return(0);
// build up the palette data array
for (int color=1; color < 255; color++)
{
// fill with random RGB values
palette[color].peRed = rand()%256;
palette[color].peGreen = rand()%256;
palette[color].peBlue = rand()%256;
// set flags field to PC_NOCOLLAPSE
palette[color].peFlags = PC_NOCOLLAPSE;
} // end for color
// now fill in entry 0 and 255 with black and white
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -