首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
 您现在的位置: 中国IT实验室 >> 游戏开发 >> 游戏开发入门 >> 正文
载入位图文件到 DirectDraw的方法
来源:ChinaItLab 作者: 时间:2004-12-26


  Introduction
  介绍
  
  In this article I will briefly describe the process of using Windows' functions to load a bitmap (.bmp) file of any type, and place it on a newly-created DirectDraw surface. In this particular article, we will be using the DirectX 7.0 SDK.
  在这篇文章里,我将简要叙述一下使用Windows函数来载入任何类型的BMP图象文件数据,并将数据存
  放到一个新建立的DirectDraw的表面中。在这以后的篇幅里,我们将使用DirectX 7.0 SDK。
  
  DirectDraw Surface Creation
  创建DirectDraw表面
  
  Creating a new DirectDraw surface is very easy, and this function will create a surface of any size. This can also be used as a general-purpose surface creation function, and if you were writing an engine class you'd probably put it in there somewhere.
  创建一个DirectDraw表面是非常容易的,这个函数将根据大小来创建一个表面。这同样能用于多种多
  样的表面创建函数,如果你要制作一个引擎类,你也许会把这个放在引擎的某处。
  
  Listing 1
  代码列表 1
  
  void CreateSurface(LPDIRECTDRAWSURFACE7 *lpSource, int xs, int ys)
  {
  DDSURFACEDESC2 ddsd;
  
  ZeroMemory(&ddsd, sizeof(ddsd));
  ddsd.dwSize = sizeof(ddsd);
  ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  ddsd.dwWidth = xs;
  ddsd.dwHeight = ys;
  lpdd->CreateSurface(&ddsd, lpSource, NULL);
  }
  
  All this code does is create a DDSURFACEDESC2 structure that describes the dimensions of the surface, and tells DirectDraw that it's an off-screen surface. Then, the call to DirectDraw (in this case, DirectDraw is the lpdd pointer) just creates the surface.
  所有的这些代码是为表面建立一个DDSURFACEDESC2的数据结构来记录大小尺度,并且告诉DirectDraw
  那是一个离屏表面。然后,呼叫DirectDraw(在这部分,DirectDraw是lpdd的指针)真正创建这个表面
  。
  
  Bitmap Loading
  位图载入
  
  To load .bmp files we will use the standard Windows graphics library (GDI). This is best because if we're in a mode with a palette then Windows will automatically re-map the bitmap colours to the nearest in the palette. Here is the code that blits a loaded bitmap to a surface….
  要载入.BMP图形文件,我们将使用标准的Windows图形库(GDI)。这个是最好的方法,因为如果我们是
  在一个调色板的视频模式,那么Windows将自动将位图映射到最接近的调色板颜色。以下的代码是绘
  制一个位图数据到表面……
  
  Listing 2
  代码列表 2
  
  void DrawHBitmap(IDirectDrawSurface7 *lpSurface, HBITMAP hBitmap, int x, int y, int width, int height)
  {
  HDC hdcImage;
  HDC hdc;
  BITMAP bm;
  
  if (lpSurface == NULL || hBitmap == NULL)
  return;
  lpSurface->Restore();
  
  hdcImage = CreateCompatibleDC(NULL);
  SelectObject(hdcImage, hBitmap);
  
  GetObject(hBitmap, sizeof(bm), &bm);
  width = width == 0 ? bm.bmWidth : width;
  height = height == 0 ? bm.bmHeight : height;
  
  lpSurface->GetDC(&hdc);
  BitBlt(hdc, x, y, width, height, hdcImage, 0, 0, SRCCOPY);
  lpSurface->ReleaseDC(hdc);
  DeleteDC(hdcImage);
  }
  
  
  and here is the code that loads, blits, then unloads the bitmap:
  以下的代码是把位图载入、绘制、到数据释放:
  
  Listing 3
  代码列表 3
  
  void CreateBitmapSurface(LPDIRECTDRAWSURFACE7 lpSurface, char *fname, int xs, int ys)
  {
  HBITMAP hBitmap;
  
  CreateSurface(&lpSurface, xs, ys);
  hBitmap = LoadImage(NULL, fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  DrawHBitmap(lpSurface, hBitmap, 0, 0, xs, ys);
  DeleteObject(hBitmap);
  }
  
  
  Quick Example
  快速例程
  
  And to round it all up, here's some example code that loads the file "test.bmp" (width = 128, height = 128) into the lpddsTest surface, and then releases it again:
  围绕着以下部分,我们将结束此章节,这里是一些例程代码,它载入"test.bmp"文件(宽=128,高=12
  8)到lpddTest表面中,并且将其释放:
  
  Listing 4
  代码列表 4
  
  void Example(void)
  {
  /*
  * Declare the surface object
  * 声名表面对象
  */
  LPDIRECTDRAWSURFACE7 lpddsTest;
  
  /*
  * Load the bitmap file into it
  * 载入位图文件
  */
  CreateBitmapSurface(lpddsTest, “test.bmp”, 128, 128);
  
  /*
  * The lpddsTest surface now contains the “test.bmp” file
  * lpddsTest表面已经包含了"test.bmp"文件数据
  */
  
  /*
  * Release the surface
  * 释放表面
  */
  lpddsTest->Release();
  }
【责编:admin】

中国IT教育热线咨询

相关文章
游戏开发新手入门之位图化图形
给希望成为游戏美术设计师的朋友
Ogre游戏引擎鼠标选取物体演示  
所有绘画的核心灵魂——素描知识
DirectDraw与DirectInput游戏编程体验
推荐文章

 精彩友情推荐
·Asp源码 PHP源码
·CGI源码 JSP源码
·建站书籍教程
·服务器软件 .net源码
·建站工具软件
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
 基础入门  开发文档
 最新推荐
  多数的Windows程序都需要Windows.h和Windowsx.h这两个头文件,要确保使用它们。当然,你还需要其它......
游戏引擎演化史
在Windows上安装OGRE的方法
关于滤镜遮罩概念,Sobel 遮罩
游戏开发新手入门之Windows编程
游戏开发新手入门之位图化图形
教你实现卡通渲染的另类勾边方法
游戏设计大师谈如何成为一名游戏设
Visual C#编写 3D游戏框架示例
真正的 Java 学习从入门到精通
游戏开发经验——游戏开发的基本常
  为什么要研究攻击行为在人类有记载的5600年的历史中,共计发生了14,400次战争;今天,平均一天要发生............
游戏开发中显示对话的特殊句法
游戏原型设计的介绍
网络游戏中的攻击行为
谈动作类游戏的必要条件
规则的多元分析模式
载入位图文件到DirectDraw
Archer Game Suite 是什么?
浅谈游戏企划-新手入门篇
暴雪称霸游戏业界的六大秘密绝招
骨骼动画及示例Skinned Mesh的解析
  培训中心