C++游戏编程基础,平面地图,求教!谢谢啊~~
我是初学者,最近在看《VisualC++游戏编程基础》,学到了平面地图绘制这里。我照着书中代码做的可是结果出不来。代码我会复制下来。我知道是我复制图片到工程的方法不对,还是怎么回事,三种颜色的图片块,只能显示一种,是不是循环错呢?超级纠结。单步调试到sprintf()函数的时候还会出现Find Source对话框。内容是Please enter the path for SPRINTF.C~~这又是什么原因呢?
请大家帮帮我吧,谢谢大家!!
- C/C++ code
#include "stdafx.h"#include "resource.h"#include "stdio.h"// Global Variables:HINSTANCE hInst; // current instanceHBITMAP fullmap;//声明图块贴图,完成地图拼接HDC mdc;const int rows =8,cols=8;//定义地图上的行列,// Foward declarations of functions included in this code module:ATOM MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);void MyPaint(HDC hdc);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ // TODO: Place code here. MSG msg; HACCEL hAccelTable; MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CH2_9); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam;}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 = NULL;//LoadIcon(hInstance, (LPCTSTR)IDI_CH2_9); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;//(LPCSTR)IDC_CH2_9; wcex.lpszClassName = "canvas"; wcex.hIconSm = NULL;//LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ HWND hWnd; hInst = hInstance; // Store instance handle in our global variable HDC hdc,bufdc; hWnd = CreateWindow("canvas", "平面地图", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } MoveWindow(hWnd,10,10,430,450,true); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); int mapIndex[rows*cols]={2,2,2,2,0,1,0,1,//第一row 0,2,2,0,0,0,1,1, 0,0,0,0,0,0,0,1, 2,0,0,0,0,0,2,2, 2,0,0,0,2,2,0,0, 0,0,2,2,2,0,0,1, 0,0,2,0,0,0,1,1,}; hdc=GetDC(hWnd); mdc=CreateCompatibleDC(hdc); bufdc=CreateCompatibleDC(hdc); fullmap=CreateCompatibleBitmap(hdc,cols*50,rows*50);//建立fullmap空位图宽:列数*图片宽;高:行*图高 SelectObject(mdc,fullmap); HBITMAP map[3]; char filename[20]=""; int rowNum,colNum; int x,y,i; //加载各图块位图 for (i=0;i<3;i++) { sprintf(filename,"map%d.bmp",i); map[i]=(HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE); }//利用循环转换图片的文件名,取出各个图块存在map[]中; //按照mapIndex数组中的定义取出对应的图块,进行地图拼接; for (i=0;i<rows*cols;i++) { SelectObject(bufdc,map[mapIndex[i]]); rowNum=i/cols; colNum=i%cols; x=colNum*50; y=colNum*50; BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY); } MyPaint(hdc); ReleaseDC(hWnd,hdc); DeleteDC(bufdc); return TRUE;}void MyPaint(HDC hdc){ SelectObject(mdc,fullmap); BitBlt(hdc,10,10,cols*50,rows*50,mdc,0,0,SRCCOPY);}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... MyPaint(hdc); EndPaint(hWnd, &ps); break; case WM_DESTROY: DeleteDC(mdc); DeleteObject(fullmap); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;}
[解决办法]
for (i=0;i<rows*cols;i++)
{
SelectObject(bufdc,map[mapIndex[i]]);
rowNum=i/cols;
colNum=i%cols;
x=colNum*50;
y=rowNum*50;
BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);
}