主要是作为自己做的2DRPG游戏日记 采用direct2D vs2015 c++ win32 还在开发中 预计写到3月底应该

简述:

本项目使用D2D API制作一款2DRPG游戏。暂定游戏设计有三个主角,6幅地图(1张城镇,5张迷宫),5幅战斗背景图,100个NPC

D2D如何渲染位图:

参见博客:http://www.cnblogs.com/graphics/archive/2011/05/23/1964273.html

第零步:C++Win32程序基本结构

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
2.    {
3.        switch (message)
4.        {
5.        case   WM_PAINT:   //Draw
6.            if (!pRenderTarget)
7.            {
8.                CreateResource(hwnd);
9.                Form_Load();
10.            }
11.            else
12.            {
13.                Begindraw();
14.                DrawMap(hwnd, pRenderTarget,map, player,npc,current_player,current_map, animation_ctrl);
15.                Enddraw();
16.            }
17.
18.            return 0;
19.
20.        case WM_KEYDOWN:
21.        {
22.
23.            end = GetTickCount();
24.            if (end - start > player[current_player].walk_interval)
25.            {
26.                start = end;
27.                animation_ctrl++;
28.                key_ctrl(hwnd, wParam, player, ¤t_player,map,¤t_map,npc);
29.            }
30.        }
31.        break;
32.
33.        case WM_DESTROY:
34.            Cleanup();
35.            PostQuitMessage(0);
36.            return 0;
37.        }
38.
39.        return DefWindowProc(hwnd, message, wParam, lParam);
40.    }
41.
42.    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
43.    {
44.
45.        WNDCLASSEX winClass;
46.
47.        winClass.lpszClassName = "Direct2D";
48.        winClass.cbSize = sizeof(WNDCLASSEX);
49.        winClass.style = CS_HREDRAW | CS_VREDRAW;
50.        winClass.lpfnWndProc = WndProc;
51.        winClass.hInstance = hInstance;
52.        winClass.hIcon = NULL;
53.        winClass.hIconSm = NULL;
54.        winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
55.        winClass.hbrBackground = NULL;
56.        winClass.lpszMenuName = NULL;
57.        winClass.cbClsExtra = 0;
58.        winClass.cbWndExtra = 0;
59.
60.        if (!RegisterClassEx(&winClass))
61.        {
62.            MessageBox(NULL, TEXT("This program requires Windows NT!"), "error", MB_ICONERROR);
63.            return 0;
64.        }
65.
66.        HWND hwnd = CreateWindowEx(NULL,
67.            "Direct2D",                 // window class name
68.            "寻仙传",            // window caption
69.            WS_MAXIMIZE,        // window style
70.            CW_USEDEFAULT,              // initial x position
71.            CW_USEDEFAULT,              // initial y position
72.            640,                        // initial x size
73.            480,                        // initial y size
74.            NULL,                       // parent window handle
75.            NULL,                       // window menu handle
76.            hInstance,                  // program instance handle
77.            NULL);                      // creation parameters
78.
79.        ShowWindow(hwnd, iCmdShow);
80.        UpdateWindow(hwnd);
81.
82.        MSG    msg;
83.        ZeroMemory(&msg, sizeof(msg));
84.
85.        while (GetMessage(&msg, NULL, 0, 0))
86.        {
87.            TranslateMessage(&msg);
88.            DispatchMessage(&msg);
89.        }
90.
91.        return msg.wParam;
92.    }  

第一步:创建D2D工厂,画布,WIC工厂

1.    VOID CreateResource(HWND hWnd)
2.    {
3.        HRESULT hr;
4.        //创建D2D工厂  D2D1CreateFactory
5.        hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
6.        if (FAILED(hr))
7.        {
8.            MessageBox(hWnd, "Create D2D factory failed!", "Error", 0);
9.            return;
10.        }
11.
12.        // Obtain the size of the drawing area
13.        RECT rc;
14.        GetClientRect(hWnd, &rc);
15.
16.        // Create a Direct2D render target
17.        hr = pD2DFactory->CreateHwndRenderTarget(
18.            D2D1::RenderTargetProperties(),
19.            D2D1::HwndRenderTargetProperties(
20.                hWnd,
21.                D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)
22.            ),
23.            &pRenderTarget
24.        );
25.        if (FAILED(hr))
26.        {
27.            MessageBox(hWnd, "Create render target failed!", "Error", 0);
28.            return;
29.        }
30.
31.        // Create WIC factory 用于加载位图
32.        hr = CoCreateInstance(
33.                    CLSID_WICImagingFactory1,
34.                        NULL,
35.                        CLSCTX_INPROC_SERVER,
36.                        IID_IWICImagingFactory,
37.                        reinterpret_cast<void **>(&pWICFactory)
38.                    );
39.        if (FAILED(hr))
40.        {
41.            MessageBox(hWnd, "Create render target failed!", "Error", 0);
42.            return;
43.        }
44.    }  

第二步:使用WIC从文件中加载位图到ID2D1Bitmap

1.    HRESULT LoadBitmapFromFile(
2.        ID2D1RenderTarget *pRenderTarget,
3.        IWICImagingFactory *pIWICFactory,
4.        PCWSTR uri,  // 文件的路径
5.        UINT destinationWidth,
6.        UINT destinationHeight,
7.        ID2D1Bitmap **ppBitmap
8.    )
9.    {
10.        HRESULT hr = S_OK;
11.
12.        IWICBitmapDecoder *pDecoder = NULL;
13.        IWICBitmapFrameDecode *pSource = NULL;
14.        IWICStream *pStream = NULL;
15.        IWICFormatConverter *pConverter = NULL;
16.        IWICBitmapScaler *pScaler = NULL;
17.
18.        hr = pIWICFactory->CreateDecoderFromFilename(
19.            uri,
20.            NULL,
21.            GENERIC_READ,
22.            WICDecodeMetadataCacheOnLoad,
23.            &pDecoder
24.        );
25.        if (SUCCEEDED(hr))
26.        {
27.
28.            // Create the initial frame.
29.            hr = pDecoder->GetFrame(0, &pSource);
30.        }
31.
32.        if (SUCCEEDED(hr))
33.        {
34.            // Convert the image format to 32bppPBGRA
35.            // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
36.            hr = pIWICFactory->CreateFormatConverter(&pConverter);
37.        }
38.        if (SUCCEEDED(hr))
39.        {
40.            // If a new width or height was specified, create an
41.            // IWICBitmapScaler and use it to resize the image.
42.            if (destinationWidth != 0 || destinationHeight != 0)
43.            {
44.                UINT originalWidth, originalHeight;
45.                hr = pSource->GetSize(&originalWidth, &originalHeight);
46.                if (SUCCEEDED(hr))
47.                {
48.                    if (destinationWidth == 0)
49.                    {
50.                        FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
51.                        destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
52.                    }
53.                    else if (destinationHeight == 0)
54.                    {
55.                        FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
56.                        destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
57.                    }
58.
59.                    hr = pIWICFactory->CreateBitmapScaler(&pScaler);
60.                    if (SUCCEEDED(hr))
61.                    {
62.                        hr = pScaler->Initialize(
63.                            pSource,
64.                            destinationWidth,
65.                            destinationHeight,
66.                            WICBitmapInterpolationModeCubic
67.                        );
68.                    }
69.                    if (SUCCEEDED(hr))
70.                    {
71.                        hr = pConverter->Initialize(
72.                            pScaler,
73.                            GUID_WICPixelFormat32bppPBGRA,
74.                            WICBitmapDitherTypeNone,
75.                            NULL,
76.                            0.f,
77.                            WICBitmapPaletteTypeMedianCut
78.                        );
79.                    }
80.                }
81.            }
82.            else // Don't scale the image.
83.            {
84.                hr = pConverter->Initialize(
85.                    pSource,
86.                    GUID_WICPixelFormat32bppPBGRA,
87.                    WICBitmapDitherTypeNone,
88.                    NULL,
89.                    0.f,
90.                    WICBitmapPaletteTypeMedianCut
91.                );
92.            }
93.        }
94.        if (SUCCEEDED(hr))
95.        {
96.            // Create a Direct2D bitmap from the WIC bitmap.
97.            hr = pRenderTarget->CreateBitmapFromWicBitmap(
98.                pConverter,
99.                NULL,
100.                ppBitmap
101.            );
102.        }
103.
104.        SAFE_RELEASE(pDecoder);
105.        SAFE_RELEASE(pSource);
106.        SAFE_RELEASE(pStream);
107.        SAFE_RELEASE(pConverter);
108.        SAFE_RELEASE(pScaler);
109.
110.        return hr;
111.    }  

第三步:开始绘图,结束绘图,释放资源函数

1.    VOID Begindraw()
2.    {
3.        pRenderTarget->BeginDraw();
4.    }
5.    VOID Enddraw()
6.    {
7.        HRESULT hr = pRenderTarget->EndDraw();
8.        if (FAILED(hr))
9.        {
10.            MessageBox(NULL, "Draw failed!", "Error", 0);
11.
12.            return;
13.        }
14.    }
15.    VOID Cleanup()
16.    {
17.        SAFE_RELEASE(pWICFactory);
18.        SAFE_RELEASE(pRenderTarget);
19.        SAFE_RELEASE(pD2DFactory);
20.    }  

第四步:绘图函数

1.    pRenderTarget->DrawBitmap(
2.                map[current_map].map,
3.                window_size,// D2D1_SIZE_ F格式窗口大小
4.                1.0f,
5.                D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
6.                map_size  // D2D1_SIZE_ F格式要显示的位图区域
7.            );  

将绘图函数放到begindraw与enddraw之间,几个函数放置在WndProc函数W的IN_PAIN中。

转载于:https://www.cnblogs.com/evazore/p/6428440.html

D2D RPG游戏开发日记(一)--准备相关推荐

  1. SDL2 游戏开发日记(九) 单机麻将

    SDL2 游戏开发日记(九) 单机麻将 单机麻将的基本功能其实年前已经完成了,只是写文档麻烦,再加上懒癌和重度拖延症,就一直拖着没更新.今天周末一个人没什么事干,抽空把它更新了. 麻将的表示 用数组表 ...

  2. Unity完全学习教程-从初学者到C#中的RPG游戏开发

    打造3款游戏&学习Unity实用方式!从基础开始,以一个RPG游戏结束.使用Unity 2020和C# 你会学到: 通过创建酷游戏的实用方法 游戏开发的基础和核心概念 创建一个拥有大量功能的角 ...

  3. java模拟生存RPG游戏开发

    2013-01-22 MVC 将原有程序,改写为MVC模式,同时学习优化.单例的使用.部分技能开始尝试用状态机实现. 近期最想实现的是,让给矮人添加寻路算法,让他自己动起来.. 2013-01-07 ...

  4. unity3d模拟树叶飘动_Unity3D独立游戏开发日记(一):动态生成树木

    目前写的独立游戏是一个沙盒类型的游戏.游戏DEMO视频如下: 提到沙盒类型的游戏,就有人给出了这样的定义: 游戏世界离现实世界越近,自由度.随机度越高才叫沙盒游戏.所谓自由度,就是你在游戏里想干啥就干 ...

  5. Unity游戏开发日记(一):独自开发2d横板游戏:Small man(MainMenu主界面)基本构建

    目录 一.独自开发想法 前言: 开发构想: 二.主要内容:MainMenu主界面基本构建 (一)预想功能 (二)功能实现 1.主界面UI设计 2.Option功能设计 3.TypeOption功能设计 ...

  6. 项目游戏开发日记 No.0x000005

    14软二杨近星(2014551622) 还有一周就要交项目了, 看着周围的人也都忙碌了起来, 看着大部分人的项目都已经初具容貌, 我们团队里面也搞得人心惶惶, 一来是, 时间不多了, 还有很多事情要做 ...

  7. Unity3D独立游戏开发日记(二):摆放建筑物

    在沙盒游戏里,能自由建造是很重要的特点,比如说风靡全球的<我的世界>,用一个个方块就能搭建出规模宏大的世界.甚至有偏激的人说,没有自由建造,就不是一个真正的沙盒游戏.的确,沙盒游戏的魅力有 ...

  8. 小米VR一体机游戏开发日记(第一天)

    上周入手一个小米VR一体机,虽说画面颗粒感还是比较强,但那种身临其境的感觉还是非常酷的! 今天在网上看了一些VR的知识后突发奇想,跟儿子一起创作个小游戏试试,正好马上十一了,有七天时间可以利用:). ...

  9. 自学Unity游戏开发日记

    Unity游戏开发的启蒙老师是麦扣老师(在bilibili),讲的非常的棒,后悔没有早点发现麦扣老师. 第一个案例游戏(Sunny Land) 印象最深的问题:在按下空格进行跳跃时,跳不起来,或者说偶 ...

最新文章

  1. R语言使用ggplot2包geom_jitter()函数绘制分组(strip plot,一维散点图)带状图(单色填充、分组颜色填充)实战
  2. es分布式结构原理是什么?
  3. Android数据存储——2.文件存储_C_DOM解析XML文档
  4. 2020年,数据中心的绿色技术演进与创新
  5. 启动hive报错:java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang
  6. OpenGL 位移贴图实例
  7. Flutter 34: 图解自定义 View 之 Canvas (一)
  8. oracle java调用存储过程_Java调用Oracle存储过程
  9. 浏览器渲染原理与过程
  10. 《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format
  11. 什么是依赖,什么是抽象
  12. WordPress简约mkBlog博客主题模板v2.1
  13. ML-Agents训练智能AI使用技巧
  14. 中公教育12月04日内外盘分析
  15. 大数据是什么意思?就业前景如何?
  16. 控制台报400、500内部服务器错误是什么原因?怎么解决?
  17. 怎样才能胜任技术总监
  18. 基于Visual C++2010 与office2010开发办公自动化(14)-自定义excel2010工具栏
  19. Android 10 新增物理按键
  20. 北京联合大学计算机系怎样,北京联合大学计算机科学与技术怎么样

热门文章

  1. Android静态安全检查(十三):剪切板使用检测
  2. 软件测试工程师又一大挑战:大数据测试
  3. [每日一题] 62. 美国节日(日期计算、蔡勒公式)
  4. ### CausDriver com.mysql.jdbc.Driver claims to not accept jdbcUrl, mysql:jdbc://localhost:3306/mysql
  5. Qt 之 简单截图功能(一)实现鼠标选中区域截图
  6. 线上服务质量的问题该如何去处理?你有什么思路?
  7. Windows文件传输及执行—mshta
  8. 如何设计一款好玩的网络游戏
  9. 汇编语言lcall d200c,明天就要交课设了,蜂鸣器老有杂音,求大神赐教
  10. ESP8266AT指令接入阿里飞燕 , 轻松天猫精灵语音控制单片机;