Code: Select all
#include <windows.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
static HWND g_hWnd;
static HBRUSH g_bgBrush = NULL;
static IrrlichtDevice* device = nullptr;
static IVideoDriver* driver = nullptr;
static ISceneManager* smgr = nullptr;
static IGUIEnvironment* guienv = nullptr;
// -------------------
// Main
// -------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static char buffer[100];
switch (msg) {
case WM_ERASEBKGND:
return 1;
case WM_TIMER:
return 0;
case WM_CHAR: {
return 0;
}
case WM_KEYDOWN:
return 0;
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
// -------------------
// Loop with rows funcs
// -------------------
typedef void (*LoopFunc)();
static MSG g_msg;
static unsigned char g_exitFlag = 0;
void loop_func();
void end_func();
LoopFunc jump_targets[] = { loop_func, end_func };
void loop_func() {
BOOL hasMessage = GetMessage(&g_msg, NULL, 0, 0) * (1 - g_exitFlag);
unsigned char isQuitMsg = (g_msg.message == WM_QUIT);
g_exitFlag += (hasMessage <= 0) | isQuitMsg;
switch (!!hasMessage * (1 - isQuitMsg)) {
case 1:
device->run();
TranslateMessage(&g_msg);
DispatchMessage(&g_msg);
driver->beginScene(true, true, SColor(255, 30, 30, 30));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
jump_targets[!!g_exitFlag]();
}
void end_func() {
KillTimer(g_hWnd, 1);
UnregisterClass("TestWindow", GetModuleHandle(NULL));
ExitProcess((UINT)g_msg.wParam);
}
// -------------------
// WinMain
// -------------------
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow) {
g_bgBrush = CreateSolidBrush(RGB(30, 30, 30));
WNDCLASSEX wc = {
sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW | CS_OWNDC, WndProc,
0, 0, hInst, NULL, LoadCursor(NULL, IDC_ARROW), g_bgBrush,
NULL, "TestWindow", NULL
};
RegisterClassEx(&wc);
g_hWnd = CreateWindowEx(0, "TestWindow", "Irrlicht master race",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
512, 512, NULL, NULL, hInst, NULL);
SetTimer(g_hWnd, 1, 16, NULL);
ShowWindow(g_hWnd, nShow);
UpdateWindow(g_hWnd);
SIrrlichtCreationParameters params;
params.DriverType = EDT_SOFTWARE;
params.WindowId = g_hWnd;
params.Bits = 16;
params.WindowSize = dimension2d<u32>(512, 512);
params.AntiAlias = false;
params.Fullscreen = false;
params.ZBufferBits = 16;
params.Stencilbuffer = false;
device = createDeviceEx(params);
if (!device) return 1;
device->setWindowCaption(L"Irrlicht win32");
driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
// --- Loading assets ---
IAnimatedMesh* mesh = smgr->getMesh("../../media/Monster3_forirrlicht.md2");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
if (node) {
node->setScale(vector3df(14, 14, 14));
node->setRotation(vector3df(0, -60, 0));
node->setMD2Animation("salute");
node->setMaterialTexture(0, driver->getTexture("../../media/Monster3_Albedo.jpg"));
node->setMaterialFlag(EMF_LIGHTING, true);
node->addShadowVolumeSceneNode();
}
ISceneNode* box = smgr->addCubeSceneNode(10.0f, 0, -1, vector3df(0, -2.0f, 0),
vector3df(0, 0, 0), vector3df(10, 0.3f, 10));
if (box) {
box->setMaterialFlag(EMF_LIGHTING, true);
box->setMaterialTexture(0, driver->getTexture("../../media/rockwall.jpg"));
}
smgr->addLightSceneNode(0, vector3df(-20, 29, 3), SColorf(1.0f, 1.0f, 1.0f), 100.0f);
smgr->addCameraSceneNode(0, vector3df(0, 30, -80), vector3df(0, 5, 0));
jump_targets[0](); // start loop
return 0;
}
