create visual studio project, add Windows initialization code
This commit is contained in:
156
engine/main.cpp
156
engine/main.cpp
@@ -5,6 +5,7 @@
|
||||
#include "image.hpp"
|
||||
#include "app.hpp"
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <Cocoa/Cocoa.h>
|
||||
#include <CoreVideo/CoreVideo.h>
|
||||
@@ -189,3 +190,158 @@ int main(int argc, const char * argv[])
|
||||
[app run];
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#pragma comment (lib, "opengl32.lib")
|
||||
#pragma comment (lib, "glew32.lib")
|
||||
|
||||
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);
|
||||
HINSTANCE hInst;
|
||||
HWND hWnd;
|
||||
HDC hDC;
|
||||
HGLRC hRC;
|
||||
char *className;
|
||||
bool keys[256];
|
||||
|
||||
int main()
|
||||
{
|
||||
WNDCLASSA wc;
|
||||
PIXELFORMATDESCRIPTOR pfd;
|
||||
|
||||
App::I.create();
|
||||
RECT clientRect = { 0, 0, App::I.width, App::I.height };
|
||||
|
||||
// Inizialize data structures to zero
|
||||
memset(&wc, 0, sizeof(wc));
|
||||
memset(&keys, 0, sizeof(keys));
|
||||
memset(&pfd, 0, sizeof(pfd));
|
||||
|
||||
// Create the main window
|
||||
|
||||
hInst = GetModuleHandle(NULL);
|
||||
className = "DrosophilaMain";
|
||||
|
||||
wc.hInstance = hInst;
|
||||
wc.lpfnWndProc = (WNDPROC)WndProc;
|
||||
wc.lpszClassName = className;
|
||||
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
|
||||
|
||||
RegisterClassA(&wc);
|
||||
|
||||
AdjustWindowRect(&clientRect, WS_OVERLAPPEDWINDOW, false);
|
||||
hWnd = CreateWindowA(wc.lpszClassName, "New Engine", WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT, clientRect.right - clientRect.left,
|
||||
clientRect.bottom - clientRect.top, 0, 0, hInst, 0);
|
||||
ShowWindow(hWnd, SW_NORMAL);
|
||||
|
||||
// Setup GL Rendering Context
|
||||
pfd.nSize = sizeof(pfd);
|
||||
pfd.nVersion = 1;
|
||||
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||
pfd.cColorBits = 32;
|
||||
pfd.cDepthBits = 16;
|
||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||
|
||||
hDC = GetDC(hWnd);
|
||||
int pxfmt = ChoosePixelFormat(hDC, &pfd);
|
||||
SetPixelFormat(hDC, pxfmt, &pfd);
|
||||
hRC = wglCreateContext(hDC); // Create OpenGL 2.1 or less
|
||||
wglMakeCurrent(hDC, hRC);
|
||||
|
||||
// Initialize extensions
|
||||
if (glewInit() != GLEW_OK)
|
||||
return 0;
|
||||
|
||||
printf("GL version: %s\n", glGetString(GL_VERSION));
|
||||
printf("GL vendor: %s\n", glGetString(GL_VENDOR));
|
||||
printf("GL renderer: %s\n", glGetString(GL_RENDERER));
|
||||
|
||||
// If supported create a 3.1 context
|
||||
if (wglewIsSupported("WGL_ARB_create_context"))
|
||||
{
|
||||
int attribs[] =
|
||||
{
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
||||
WGL_CONTEXT_FLAGS_ARB, 0,
|
||||
0
|
||||
};
|
||||
auto oldContext = hRC;
|
||||
hRC = wglCreateContextAttribsARB(hDC, NULL, attribs);
|
||||
wglMakeCurrent(NULL, NULL);
|
||||
wglDeleteContext(oldContext);
|
||||
wglMakeCurrent(hDC, hRC);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not supported, go fuck yourself we are not gonna use that shit
|
||||
return -1; // A negative number because you are a negative one
|
||||
}
|
||||
|
||||
App::I.init();
|
||||
|
||||
MSG msg;
|
||||
bool running = true;
|
||||
unsigned long t0 = GetTickCount();
|
||||
unsigned long t1;
|
||||
while (running)
|
||||
{
|
||||
// If there any message in the queue process it
|
||||
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
|
||||
{
|
||||
running = !(msg.message == WM_QUIT/* || gl.keys[VK_ESCAPE]*/);
|
||||
DispatchMessage(&msg);
|
||||
TranslateMessage(&msg);
|
||||
}
|
||||
else // Otherwise render next frame
|
||||
{
|
||||
t1 = GetTickCount();
|
||||
float dt = (float)(t1 - t0) / 1000.0f;
|
||||
if (dt > 1.0f / 60.0f)
|
||||
{
|
||||
App::I.update((float)(t1 - t0) / 1000.0f);
|
||||
t0 = t1;
|
||||
SwapBuffers(hDC);
|
||||
}
|
||||
else
|
||||
{
|
||||
Sleep((DWORD)(1.0f / 60.0f * 1000.f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up
|
||||
DestroyWindow(hWnd);
|
||||
UnregisterClassA(className, hInst);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case WM_CLOSE:
|
||||
PostQuitMessage(0);
|
||||
break;
|
||||
case WM_KEYDOWN:
|
||||
keys[wp] = true;
|
||||
break;
|
||||
case WM_KEYUP:
|
||||
keys[wp] = false;
|
||||
break;
|
||||
case WM_MOUSEMOVE:
|
||||
//current->pointerMove(LOWORD(lp), HIWORD(lp));
|
||||
break;
|
||||
case WM_LBUTTONDOWN:
|
||||
//current->pointerDown(LOWORD(lp), HIWORD(lp));
|
||||
break;
|
||||
case WM_LBUTTONUP:
|
||||
//current->pointerUp(LOWORD(lp), HIWORD(lp));
|
||||
break;
|
||||
}
|
||||
return DefWindowProc(hWnd, msg, wp, lp);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user