[游戏模版10] Win32 平面地图贴图 正

>_<:picture
resource

>_<:If you master the ways of mapping picture,then this problem is not
problem!Just using your head think some logical method.

>_<:Here,I use a array save
where and whith picture should show.For
example:use array mapindex save information.then when you map these
pictures,only need to calculate the real position and then move it to the bufdc
,finally put it in the mdc (here bufdc in charge of making an
exchange between every picture and mdc)


int mapindex[rows*cols]=
{2,2,3,2,0,1,0,1,
0,2,2,0,3,0,1,1,
0,3,0,7,0,0,0,1,
2,0,3,0,0,0,2,2,
2,7,0,6,0,2,2,2,
2,0,4,0,2,2,0,0,
0,0,2,3,2,1,0,1,
0,0,2,0,3,0,1,1 };


 1 //按照mapIndex 数组中的定义取出对应图块,进行地图拼接
2 for(i=0;i<rows*cols;i++)
3 {
4 SelectObject(bufdc,map[mapindex[i]]);
5 rowNum=i/cols;//求列编号
6 colNum=i%cols;//求行编号
7 x=colNum*50;//求贴图x坐标
8 y=rowNum*50;
9
10 BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);
11 }

>_<:Finally, only need to copy mdc to hdc.

>_<:Here is the code

 1 //{{NO_DEPENDENCIES}}
2 // Microsoft Visual C++ generated include file.
3 // Used by FE.RC
4 //
5 #define IDR_MAINFRAME 128
6 #define IDD_FE_DIALOG 102
7 #define IDD_ABOUTBOX 103
8 #define IDS_APP_TITLE 103
9 #define IDM_ABOUT 104
10 #define IDM_EXIT 105
11 #define IDS_HELLO 106
12 #define IDI_FE 107
13 #define IDI_SMALL 108
14 #define IDC_FE 109
15 #define IDC_MYICON 2
16 #define IDC_STATIC -1
17 // Next default values for new objects
18 //
19 #ifdef APSTUDIO_INVOKED
20 #ifndef APSTUDIO_READONLY_SYMBOLS
21
22 #define _APS_NEXT_RESOURCE_VALUE 129
23 #define _APS_NEXT_COMMAND_VALUE 32771
24 #define _APS_NEXT_CONTROL_VALUE 1000
25 #define _APS_NEXT_SYMED_VALUE 110
26 #endif
27 #endif

resourse.h

 1 // stdafx.h : include file for standard system include files,
2 // or project specific include files that are used frequently, but
3 // are changed infrequently
4 //
5
6 #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
7 #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
8
9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12
13 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
14
15
16 // Windows Header Files:
17 #include <windows.h>
18
19 // C RunTime Header Files
20 #include <stdlib.h>
21 #include <malloc.h>
22 #include <memory.h>
23 #include <tchar.h>
24
25 // Local Header Files
26
27 // TODO: reference additional headers your program requires here
28
29 //{{AFX_INSERT_LOCATION}}
30 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
31
32 #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

StdAfx.h

  1 #include "stdafx.h"
2 #include "resourse.h"
3 #include "stdio.h"
4 #include "time.h"
5
6 #define MAX_LOADSTRING 100
7
8 // Global Variables:
9 HINSTANCE hInst; // current instance
10 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
11 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
12 HBITMAP fullmap;
13 HDC mdc;
14 const int rows=9,cols=12;
15
16 // Foward declarations of functions included in this code module:
17 ATOM MyRegisterClass(HINSTANCE hInstance);
18 BOOL InitInstance(HINSTANCE, int);
19 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
20 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
21 void MyPaint(HDC hdc);
22 //========================================================================================
23 int APIENTRY WinMain(HINSTANCE hInstance,
24 HINSTANCE hPrevInstance,
25 LPSTR lpCmdLine,
26 int nCmdShow)
27 {
28 // TODO: Place code here.
29 MSG msg;
30
31 MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码;
32
33 // 调用InitInstance函数,进行初始化操作;
34 if (!InitInstance (hInstance, nCmdShow))
35 {
36 return FALSE;
37 }
38
39 // 消息循环(通过消息循环来获取信息,
40 //进行必要的键盘信息转换而后将控制权交给操作系统,
41 //有操作系统决定哪个程序的消息处理函数处理消息
42 while (GetMessage(&msg, NULL, 0, 0)) //获取程序消息
43 {
44 TranslateMessage(&msg);//转换伪码及字符
45 DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序;
46 }
47
48 return msg.wParam;
49 }
50 //=====================================================================================
51
52
53
54 //=============================================================================================
55 //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息,
56 //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。
57 //==============================================================================================
58 ATOM MyRegisterClass(HINSTANCE hInstance)
59 {
60 WNDCLASSEX wcex; //申请一个窗口类别“WNDCLASSEX”和结构”wcex“
61 //--------------------------------------------------------------
62 //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc)
63 //为WNDPROC,类别名称为(lpszClassName)为”fe";
64 //--------------------------------------------------------------
65 wcex.cbSize = sizeof(WNDCLASSEX);
66
67 wcex.style = CS_HREDRAW | CS_VREDRAW;
68 wcex.lpfnWndProc = (WNDPROC)WndProc;
69 wcex.cbClsExtra = 0;
70 wcex.cbWndExtra = 0;
71 wcex.hInstance = hInstance;
72 wcex.hIcon = NULL;
73 wcex.hCursor = NULL;
74 wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
75 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
76 wcex.lpszMenuName = NULL;
77 wcex.lpszClassName = "fe";
78 wcex.hIconSm = NULL;
79
80 return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
81 //此字符串即为类别名称”fe";
82 }
83 //============================================================================================
84
85
86 //============================================================================================
87 //按照前面所定义的窗口类别来建立并显示实际的程序窗口
88 //============================================================================================
89 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
90 {
91 HWND hWnd;
92 HDC hdc,bufdc;
93 hInst = hInstance; // 把instance handle 储存在全局变量中;
94
95 hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
96 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
97 //-----------------------------------------------
98 //调用CreateWindow函数来建立一个窗口对象
99 //第一个参数就是窗口建立依据的类别名称
100 //-----------------------------------------------
101 if (!hWnd)
102 {
103 return FALSE;
104 }
105 //------------------------------------------------
106 //设定窗口的位置及窗口的大小,然后绘制显示在设备上
107 //-------------------------------------------------
108 MoveWindow(hWnd,10,10,600,450,true);//位置及大小
109 ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
110 UpdateWindow(hWnd);//将窗口绘制在显示设备上
111
112 /*int mapindex[rows*cols]={2,2,3,2,0,1,0,1,
113 0,2,2,0,3,0,1,1,
114 0,3,0,7,0,0,0,1,
115 2,0,3,0,0,0,2,2,
116 2,7,0,6,0,2,2,2,
117 2,0,4,0,2,2,0,0,
118 0,0,2,3,2,1,0,1,
119 0,0,2,0,3,0,1,1};*/
120 int mapindex[rows*cols];
121 srand((unsigned)time(NULL));
122 for(int j=rows*cols-1;j>=0;j--)
123 {
124 mapindex[j]=rand()%12;
125 }
126
127
128 hdc=GetDC(hWnd);
129 mdc=CreateCompatibleDC(hdc);
130 bufdc=CreateCompatibleDC(hdc);
131 fullmap=CreateCompatibleBitmap(hdc,cols*50,rows*50);
132
133 SelectObject(mdc,fullmap);
134
135 HBITMAP map[12];
136 char filename[20]="";
137 int rowNum,colNum;
138 int i,x,y;
139
140 for(i=0;i<12;i++)
141 {
142 sprintf(filename,"map%d.bmp",i);
143 map[i]=(HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
144 }
145
146 //按照mapIndex 数组中的定义取出对应图块,进行地图拼接
147 for(i=0;i<rows*cols;i++)
148 {
149 SelectObject(bufdc,map[mapindex[i]]);
150 rowNum=i/cols;//求列编号
151 colNum=i%cols;//求行编号
152 x=colNum*50;//求贴图x坐标
153 y=rowNum*50;
154
155 BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);
156 }
157
158 MyPaint(hdc);
159
160 ReleaseDC(hWnd,hdc);
161 DeleteDC(bufdc);
162
163 return TRUE;
164 }
165 //============================================================================================
166
167
168 //============================================================================================
169 //
170 //============================================================================================
171 void MyPaint(HDC hdc)
172 {
173 SelectObject(mdc,fullmap);
174 BitBlt(hdc,0,0,cols*50,rows*50,mdc,0,0,SRCCOPY);
175 }
176 //============================================================================================
177
178
179 //============================================================================================
180 //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型
181 //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个
182 //程序都会接收信息,选择性接受、处理;
183 //============================================================================================
184 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
185 {
186 PAINTSTRUCT ps;
187 HDC hdc;
188
189 switch (message) //判断消息类型
190 {
191 case WM_PAINT: //窗口重绘制
192 hdc = BeginPaint(hWnd, &ps);
193 EndPaint(hWnd, &ps);
194 break;
195 case WM_DESTROY: //处理窗口结束消息
196 PostQuitMessage(0);
197 break;
198 default:
199 return DefWindowProc(hWnd, message, wParam, lParam);
200 }
201 return 0;
202 }
203 //============================================================================================

main.cpp

时间: 2024-08-27 11:35:50

[游戏模版10] Win32 平面地图贴图 正的相关文章

[游戏模版7] Win32 最简单贴图

>_<:this is the first using mapping. >_<:There will be introducing how to do: First load bitmap picture return handle give hbmp, as following: hbmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE); there "b

[游戏模版8] Win32 透明贴图

>_<:The same with previous introduction. In the InitInstance fanction make a little change: >_<:Yes just add another picture "dra.bmp" and give the handle to dra.And then call function MyPaint(...) 1 hdc=GetDC(hWnd); 2 mdc=CreateComp

[游戏模版20] Win32 物理引擎 加速运动

>_<:Compared with previous talk,there will be taking about how to create an accelerated speed.Only a little change in the MyPaint(...) function. >_<:resource >_<:code 1 #include <windows.h> 2 // C 运行时头文件 3 #include <stdlib.h>

[游戏模版21] Win32 物理引擎 能量守恒

>_<:Only a little change in the function of MyPaint(...),besides the initial value have some changes. >_<:resource >_<:code:    1 #include <windows.h> 2 // C 运行时头文件 3 #include <stdlib.h> 4 #include <cstdio> 5 #include &

[游戏模版19] Win32 物理引擎 匀速运动

>_<:Learning the physical engine >_<:resource >_<:code 1 #include <windows.h> 2 // C 运行时头文件 3 #include <stdlib.h> 4 #include <cstdio> 5 #include <malloc.h> 6 #include <memory.h> 7 #include <tchar.h> 8

[游戏模版14] Win32 键盘控制

>_<:compared with the previous article,this one only adds key-message listener. >_<:up down left right control the roal movement. 1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 2 { 3 int wmId, wmEvent; 4 PAIN

[游戏模版12] Win32 稳定定时

>_<:The last time,we learned how to use timer to make the picture run and change show,but sometimes the time may have a little change,for example when you move the window or do other things the frequency may be effected.And now we will talk about ho

[游戏模版2] Win32最小框架

>_<:Just the minimum Win32  frame don't have any other special function. 1 //{{NO_DEPENDENCIES}} 2 // Microsoft Visual C++ generated include file. 3 // Used by FE.RC 4 // 5 #define IDR_MAINFRAME 128 6 #define IDD_FE_DIALOG 102 7 #define IDD_ABOUTBOX

[游戏模版3] Win32 画笔 画刷 图形

>_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and some graphs. >_<!following 2 files are the same with the previous and file main.cpp has some changes. 1 //{{NO_DEPENDENCIES}} 2 // Microsoft Visual C+