|
|
#include "ggl.h"
#include "scene.h"
#include "utils.h"
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "winmm.lib")
POINT originalPos;//�����Ҽ�������ʱ��λ��
bool rotateView = false;
unsigned char* LoadFileContent(const char* path, int &filesize) { unsigned char* fileContent = nullptr; filesize = 0;
FILE *pFile = fopen(path, "rb");
if (pFile) { fseek(pFile, 0, SEEK_END);//���ļ�ָ���ƶ����ļ�ĩβ
int nLen = ftell(pFile);//�ļ�λ��ָ�뵱ǰλ���������ļ���ƫ���ֽ�����������ʵ���ǵõ����ļ����ֽڳ���
if (nLen > 0) { rewind(pFile);//���ļ�ָ���Ƶ�ͷ��
fileContent = new unsigned char[nLen + 1]; fread(fileContent, sizeof(unsigned char), nLen, pFile); fileContent[nLen] = '\0'; filesize = nLen; } fclose(pFile); } return fileContent; }
/**
* ��ȡÿ֡������ʱ�� */ float GetFrameTime() { static unsigned long lastTime = 0, timeSinceComputerStart = 0; timeSinceComputerStart = timeGetTime(); unsigned long frameTime = lastTime == 0 ? 0 : timeSinceComputerStart - lastTime; lastTime = timeSinceComputerStart; return float(frameTime) / 1000.0f; }
/**
* @hwnd ������Ϣ�Ĵ��� * @msg ��Ϣ���� */ LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_RBUTTONDOWN: GetCursorPos(&originalPos);//��ȡ����λ��
ShowCursor(false);//��������
rotateView = true; return 0; case WM_RBUTTONUP: SetCursorPos(originalPos.x, originalPos.y); ShowCursor(true); rotateView = false; return 0; case WM_MOUSEMOVE: if (rotateView) { POINT currentPos; GetCursorPos(¤tPos);//��һ�λ�ȡ���굱ǰλ��
int deltaX = currentPos.x - originalPos.x; int deltaY = currentPos.y - originalPos.y; OnMouseMove(deltaX, deltaY); SetCursorPos(originalPos.x, originalPos.y); } return 0; case WM_KEYDOWN: OnKeyDown(wParam); return 0; case WM_KEYUP: OnKeyUp(wParam); return 0; case WM_CLOSE: PostQuitMessage(0); return 0; }
return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
}
/**
* @hinstance ��Ӧ�ó�������ʵ�� * @hPrevInstance ��һ��Ӧ�ó�ʽ��ʵ�� * @IpCmdLine �����еIJ��� * @ShowCmd ��ô��ʾ���� */ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR IpCmdLine, int ShowCmd) {
/*ע�ᴰ��*/ WNDCLASSEX wndclass; wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
wndclass.hbrBackground = NULL; //���ڱ���
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
wndclass.lpszClassName = L"GLWindow";//��������
wndclass.lpszMenuName = NULL;//�˵�����
wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
ATOM atom = RegisterClassEx(&wndclass); if (!atom) { MessageBox(NULL, L"Register failed", L"Error", MB_OK); return 0; }
/*��������*/ //�������ڴ�С
RECT rect; rect.left = 0; rect.right = 800; rect.top = 0; rect.bottom = 600; AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL); int windowWidth = rect.right - rect.left; int windowHeight = rect.bottom - rect.top;
//��������һ��Ҫ�ղ�ע�ᴰ�ڵı���һ��
HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL); //������Ⱦ����
HDC dc = GetDC(hwnd);//��ȡ�豸������
PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nVersion = 1; pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER ; //��ʾ������������������
//�������ظ�ʽ
int pixelFormat = ChoosePixelFormat(dc, &pfd); SetPixelFormat(dc, pixelFormat, &pfd); //����OpenGL��Ⱦ����
HGLRC rc = wglCreateContext(dc); wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
Init();
/*��ʾ����*/ ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd);
/*�����������û�����*/ MSG msg; while (true) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); }
Draw(); SwapBuffers(dc); }
return 0; }
|