栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[C++] [Win32] 生命游戏-01

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[C++] [Win32] 生命游戏-01

说明

这只是一个框架,请期待下一篇文章
(没错我就是牙膏厂厂长)

代码
#include 
#include 
#include 
#include 
#include 

typedef unsigned short ushort;
typedef unsigned int uint;

struct CellData
{
	ushort width = 0;
	ushort height = 0;
	std::vector data;
};

const uint WINDOW_WIDTH = 1000;
const uint WINDOW_HEIGHT = 500;
const int OpenButton = 10001;
const int SaveButton = 10002;
const int StatusButton = 10003;
const int QuitButton = 10004;

CellData cells;
uint getNeighbourCount(ushort x, ushort y);
CellData calcNextFrame();
void drawCells();
void saveToFile();
void readFromFile();

HWND createWindow();
inline HINSTANCE getInstance();
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
inline void redraw(HWND hwnd);

int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
	HWND window = createWindow();

	MSG msg;
	bool should_quit = false;
	while (!should_quit)
	{
		if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				should_quit = true;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	DestroyWindow(window);
	return msg.wParam;
}

HWND createWindow()
{
	WNDCLASSEX wc;
	memset(&wc, 0, sizeof(WNDCLASSEX));
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.hInstance = getInstance();
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "WindowClass";
	wc.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClassEx(&wc);
	return CreateWindowEx(WS_EX_WINDOWEDGE, "WindowClass", "Window", WS_VISIBLE | WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT + GetSystemMetrics(SM_CYCAPTION), nullptr, nullptr, getInstance(), nullptr);
}

inline HINSTANCE getInstance()
{
	return (HINSTANCE)GetModuleHandle(nullptr);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	if (msg == WM_CREATE)
	{
		CREATESTRUCT* cs = (CREATESTRUCT*)lp;
		CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Open", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 0, 0, 300, 50, hwnd, (HMENU)OpenButton, cs->hInstance, nullptr);
		CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Save", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 300, 0, 300, 50, hwnd, (HMENU)SaveButton, cs->hInstance, nullptr);
		CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Start", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 600, 0, 300, 50, hwnd, (HMENU)StatusButton, cs->hInstance, nullptr);
		CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Quit", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 900, 0, 100, 50, hwnd, (HMENU)QuitButton, cs->hInstance, nullptr);
	}
	else if (msg == WM_PAINT)
	{
		drawCells();
	}
	else if (msg == WM_COMMAND)
	{
		int id = LOWORd(wp);
		int event = HIWORd(wp);
		
		if(id == QuitButton&&event == BN_CLICKED)
		{
			exit(0);
		}
	}
	else if (msg == WM_DESTROY)
	{
		PostQuitMessage(0);
	}
	else
	{
		return DefWindowProc(hwnd, msg, wp, lp);
	}
	return 0;
}

inline void redraw(HWND hwnd)
{
	RECT rect;
	GetClientRect(hwnd, &rect);
	InvalidateRect(hwnd, &rect, true);
}

uint getNeighbourCount(ushort x, ushort y)
{
	uint count = 0;
	return count;
}

CellData calcNextFrame()
{
	CellData next;
	return next;
}

void drawCells()
{

}

void saveToFile()
{

}

void readFromFile()
{

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869932.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号