终于搭好了一个游戏的窗口框架(一个调试窗口,一个消息窗口,一个主窗口)

阅读更多
//Map.cpp
#include 
  terrain.cpp:
// terrain.cpp : Defines the entry point for the application.
//

#include "StdAfx.h"
#include "Map.h"
#include "Console.h"
#include "IO.H"
#include 
#include 
#include 
static bool IsInit=true;
std::wstring reader;
using std::wstring;
using std::wcout;
using std::cout;
using std::endl;
HWND	hWnd=NULL;	// 保存 Windows 分配给程序的窗口句柄
HWND	hWnd2=NULL;	// 保存 Windows 分配给程序的窗口句柄
HWND    hConsole;
int Width=800;
LRESULT WINAPI ght(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam );
int Height=600;
//声明消息处理函数
//创建对话框,显示系统消息:
LPCTSTR  title="游戏初始化中,请稍等";

void InitMessagePrint(LPCWSTR  text);
LRESULT WINAPI MsgProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam );
LRESULT WINAPI lpDialogFunc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam );
//声明消息循环函数。
void GameLoop();
DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
{ 
	int Width=200;
	int Height=200;
	bool fullScreen =TRUE;
	DWORD	dwExStyle;		// Window 扩展风格
	DWORD	dwStyle;		// Window 窗口风格
	RECT	windowRect;		// 窗口尺寸
	int		nX=0,nY=0;
	int wid=GetSystemMetrics(SM_CXSCREEN);		// 获取当前屏幕宽
	int hei=GetSystemMetrics(SM_CYSCREEN);		// 获取当前屏幕高
	nX=0;nY=0;			// 计算窗口居中用
	dwExStyle=WS_EX_WINDOWEDGE;	// 使窗口具有3D外观
	dwStyle=WS_BORDER|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;				//创建一个带边框和标题的窗口。
	AdjustWindowRectEx(&windowRect,dwStyle,FALSE,dwExStyle);
			char cc1[]="terrainmessage";
	//窗口类
    WNDCLASSEX wc1 = { 
		              sizeof(WNDCLASSEX),
		              //所有的窗口共用一个设备。
		              CS_CLASSDC, 
					  //消息循环函数
					  ght, 
                      //窗口类额外的存储空间
					  0L, 
					  //窗口实例额外的存储空间。
					  0L, 
					  //获得创建窗口的进程单元句柄(.EXE文件的文件句柄)
                      GetModuleHandle(NULL),
                      //
					  NULL,
					  NULL, 
					  NULL, 
					  NULL,
                      cc1, 
					  NULL 
	};
	//注册窗口类
    RegisterClassEx( &wc1 );

	hWnd2 = CreateWindowEx(NULL,cc1,"我的第一个3D地形程序",
						  dwStyle|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
						  nX, nY,Width, Height,
						  NULL,NULL,NULL,NULL);	// 创建窗口

  //  PostQuitMessage(0);

	//显示窗口
	ShowWindow( hWnd2, SW_SHOWDEFAULT );				// 显示窗口

	//更新窗口
	UpdateWindow( hWnd2 );
	//消息循环
	//消息循环
	//窗口2的消息循环。

    GameLoop();	
//UnregisterClass(cc1,
  //  hInstance
//);
return 0;
} 

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{ 
    RedirectIOToConsole();
	//得到控制台窗口的窗口句柄。
//hConsole=GetConsoleWindow(void);
hConsole=FindWindow("ConsoleWindowClass",NULL); 
//HRGN hRgn=CreateRectRgn(0,0,500,500); 
//SetWindowRgn(h,hRgn,false); 

/*********************线程创建**********************/
  DWORD dwThreadId, dwThrdParam = 1; 
    HANDLE hThread; 
//创建线程ID,参数为dwThrdParam;
    hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size  
        ThreadFunc,                  // thread function 
        &dwThrdParam,                // argument to thread function 
        0,                           // use default creation flags 
        &dwThreadId);                // returns the thread identifier 
 if (hThread == NULL) 
   {
      printf( "CreateThread failed (%d)\n", GetLastError() ); 
   }
   else 
   {
      
      CloseHandle( hThread );
   }
   //检测窗口是否初始化完毕。
   while(IsInit){
    Sleep(1);
   }

/************************我们的线程就在这里*********************************/
    //The AdjustWindowRectEx function
	//calculates the required size of the window rectangle,
	//based on the desired size of the client rectangle. 
	//The window rectangle can then be passed to the CreateWindowEx 
	//function to create a window
	//whose client area is the desired size. 
    //根据所要求的客户区大小,来创建窗口。
    //是否全屏模式。
    bool fullScreen =TRUE;
	DWORD	dwExStyle;		// Window 扩展风格
	DWORD	dwStyle;		// Window 窗口风格
	RECT	windowRect;		// 窗口尺寸
	int		nX=0,nY=0;
	int wid=GetSystemMetrics(SM_CXSCREEN);		// 获取当前屏幕宽
	int hei=GetSystemMetrics(SM_CYSCREEN);		// 获取当前屏幕高
	nX=(wid-Width)/2;nY=(hei-Height)/2;			// 计算窗口居中用
	dwExStyle=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;	// 使窗口具有3D外观
	dwStyle=WS_BORDER|WS_CAPTION|WS_SYSMENU|WS_MAXIMIZE|WS_MINIMIZEBOX|WS_MAXIMIZEBOX;				//创建一个带边框和标题的窗口。
	AdjustWindowRectEx(&windowRect,dwStyle,FALSE,dwExStyle);
    /*	typedef struct {
    UINT cbSize;
    UINT style;
    WNDPROC lpfnWndProc;
    int cbClsExtra;
    int cbWndExtra;
    HINSTANCE hInstance;
    HICON hIcon;
    HCURSOR hCursor;
    HBRUSH hbrBackground;
    LPCTSTR lpszMenuName;
    LPCTSTR lpszClassName;
    HICON hIconSm;
    } WNDCLASSEX, *PWNDCLASSEX;
    */
	//窗口类名
	char cc[]="terrain";
	//窗口类
	InitMessagePrint(L"注册窗口类");
    WNDCLASSEX wc = { 
		              sizeof(WNDCLASSEX),
		              //所有的窗口共用一个设备。
		              CS_CLASSDC, 
					  //消息循环函数
					  MsgProc, 
                      //窗口类额外的存储空间
					  0L, 
					  //窗口实例额外的存储空间。
					  0L, 
					  //获得创建窗口的进程单元句柄(.EXE文件的文件句柄)
                      GetModuleHandle(NULL),
                      //
					  NULL,
					  NULL, 
					  NULL, 
					  NULL,
                      cc, 
					  NULL 
	};
	RegisterClassEx( &wc );
/*****************************hWnd2用来显示初始消息**************************************/
    InitMessagePrint(L"正在创建窗口");
/************************************************************/
    hWnd = CreateWindowEx(NULL,cc,"我的第一个3D地形程序",
						  dwStyle|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
						  nX, nY,Width, Height,
						  NULL,NULL,hInstance,NULL);	// 创建窗口
	//显示窗口发送
	ShowWindow( hWnd, SW_SHOWDEFAULT );				// 显示窗口
	//更新窗口
	PostThreadMessage(dwThreadId,WM_QUIT,0,0);
	UpdateWindow( hWnd );
   InitMessagePrint(L"正在处理小数点");
    //这个时候的消息循环再次发送至第一个窗口类的窗口函数,为什么。

	GameLoop();	
	return;
}
void GameLoop()
{   
	static int i=0;
	//cout<CleanUp();			// 结束处理
			PostQuitMessage(0);
			return 0;
			}
}
 void InitMessagePrint(LPCWSTR   text)
{ 
    wstring ws=wstring(text); 
	MyWrite(ws);
	RECT rect; 
    GetClientRect(hWnd2,&rect);
	InvalidateRect(hWnd2,&rect,true);
	//这四个参数分别对应MSG结构体中的4个参数。
	//也对应于消息处理函数中的四个参数。
}

//消息窗口
LRESULT WINAPI ght(HWND hWnd2,UINT message,WPARAM wParam,LPARAM lParam ){
//cout<<"是系统消息么"<CleanUp();			// 结束处理
			PostQuitMessage(0);
			return 0;
    case WM_PAINT:
   // ReleaseDC(hWnd2, hdc);

//	LPSTR k[10]="123456789";
/*	char wsz[5] = "aadf";
	char the[5];
    //char thechar[10]=(char *)wsz;
    io.write(wsz,5);
	io.read(the,5);
	for(int i=0;i<5;i++)
	{
	cout<<"daaaaaaaaa"< 
 

Console.cpp:

 

//说明:此函数用来在MFC或者WindowsAPP中进行标准输入。
#include < process.h>
#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include "Console.h"
#define MAX_CONSOLE_LINES 10;
HANDLE  g_hConsoleOut;                   
// Handle to debug console
// This function dynamically creates a 
//"Console" window and points stdout and stderr to it.
// It also hooks stdin to the window
// You must free it later with FreeConsole
void RedirectIOToConsole()
{    
int                        hConHandle;
long                       lStdHandle;    
CONSOLE_SCREEN_BUFFER_INFO coninfo;  
FILE                       *fp;    
// allocate a console for this app    
AllocConsole();    
// set the screen buffer to be big enough to let us scroll text    
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);   
coninfo.dwSize.Y = MAX_CONSOLE_LINES; 
 // How many lines do you want to have in the console buffer    
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),coninfo.dwSize);   
 // redirect unbuffered STDOUT to the console    
g_hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);    
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);    
fp = _fdopen( hConHandle, "w" );    
*stdout = *fp; 
setvbuf( stdout, NULL, _IONBF, 0 );  
// redirect unbuffered STDIN to the console    
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);    
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);    
fp = _fdopen( hConHandle, "r" );    
*stdin = *fp;    
setvbuf( stdin, NULL, _IONBF, 0 );    
// redirect unbuffered STDERR to the console    
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);  
//把系统的文件句柄转化为c运行库的文件指针 
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
//用write 方法打开文件流    
fp = _fdopen( hConHandle, "w" );
//将stderr指向这个文件流。   
*stderr = *fp;  
//第二个参数为NULL,表明没有缓冲区和流相关联。 
setvbuf( stderr, NULL, _IONBF, 0 );		
SetConsoleTitle("The Console Titlebar Text");  
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog     
// point to console as well  Uncomment the next line if you are using c++ cio or comment if you don't    
ios::sync_with_stdio();
}

 

IO.cpp:

 

#include "StdAfx.h"
#include "IO.H"
#include 
using std::wstringstream;
wstringstream *pBuffer=new wstringstream();
bool MyWrite(std::wstring s)
{
	static bool InitLocal=true;
	if(InitLocal){
	std::locale loc("chs");   
    pBuffer->imbue(loc);
	InitLocal=false;
	}
	pBuffer->clear();
	if((*pBuffer)<put(L'\n');
		return true;
	}
	return false;
}
bool MyRead(std::wstring& s)
{
   if(NULL!=pBuffer)
   {
	  wchar_t name[256];
	  pBuffer->clear();
      pBuffer->getline(name,256);
	  s=std::wstring(name);
	  	//  pBuffer->ignore();
      if(s.length()>0)
	  {
		  return true;
	  }
   }
   return false;
}

 

// stdafx.cpp : source file that includes just the standard includes
//	terrain.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

 

Map.h
//常量表示程序的各种状态
//要想使用const必须把头文件包含进去。
//定义全局的cons会造成意想不到的错误。因为重复定义!!
const int START_STATE=0;
const int END_STATE=1;
const int WORKING_STATE=2;

 

Console.h
#include 
#include 
#include 
void RedirectIOToConsole();

 

//IO.H
#ifndef IO_H
#define IO_H
#include 
#include 
extern bool MyWrite(std::wstring s);
extern bool MyRead(std::wstring& s);
#endif

 

//Map.H
//常量表示程序的各种状态
//要想使用const必须把头文件包含进去。
//定义全局的cons会造成意想不到的错误。因为重复定义!!
const int START_STATE=0;
const int END_STATE=1;
const int WORKING_STATE=2;

 

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include 
// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

 

你可能感兴趣的:(游戏,框架,FP,iOS,Windows)