chore: main cleanup in new files
This commit is contained in:
156
Utils.cpp
Normal file
156
Utils.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "Utils.h"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
BITMAPINFOHEADER bitmapInfoHeader;
|
||||
unsigned char* bitmapData;
|
||||
|
||||
void DisableQuickEdit() {
|
||||
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD prev_mode;
|
||||
GetConsoleMode(hInput, &prev_mode);
|
||||
SetConsoleMode(hInput, prev_mode & ~ENABLE_QUICK_EDIT_MODE & ~ENABLE_INSERT_MODE);
|
||||
}
|
||||
|
||||
void CreateConsole() {
|
||||
if (AllocConsole()) {
|
||||
FILE* conin; FILE* conout; FILE* conerr;
|
||||
freopen_s(&conin, "conin$", "r", stdin);
|
||||
freopen_s(&conout, "conout$", "w", stdout);
|
||||
freopen_s(&conerr, "conout$", "w", stderr);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char* LoadBitmapFile(char* filename, BITMAPINFOHEADER* bitmapInfoHeader) {
|
||||
FILE* filePtr;
|
||||
BITMAPFILEHEADER bitmapFileHeader;
|
||||
unsigned char* bitmapImage;
|
||||
int imageIdx = 0;
|
||||
unsigned char tempRGB;
|
||||
|
||||
filePtr = fopen(filename, "rb");
|
||||
if (filePtr == NULL) return NULL;
|
||||
|
||||
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
|
||||
if (bitmapFileHeader.bfType != BITMAP_ID) {
|
||||
fclose(filePtr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
|
||||
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
|
||||
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
|
||||
|
||||
if (!bitmapImage) {
|
||||
free(bitmapImage);
|
||||
fclose(filePtr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
|
||||
if (bitmapImage == NULL) {
|
||||
fclose(filePtr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3) {
|
||||
tempRGB = bitmapImage[imageIdx];
|
||||
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
|
||||
bitmapImage[imageIdx + 2] = tempRGB;
|
||||
}
|
||||
|
||||
fclose(filePtr);
|
||||
return bitmapImage;
|
||||
}
|
||||
|
||||
void MakeShadowMatrix(GLfloat shadowMat[16], GLfloat groundplane[4], GLfloat lightpos[4]) {
|
||||
GLfloat dot = groundplane[0] * lightpos[0] + groundplane[1] * lightpos[1] +
|
||||
groundplane[2] * lightpos[2] + groundplane[3] * lightpos[3];
|
||||
|
||||
shadowMat[0] = dot - lightpos[0] * groundplane[0];
|
||||
shadowMat[4] = 0.f - lightpos[0] * groundplane[1];
|
||||
shadowMat[8] = 0.f - lightpos[0] * groundplane[2];
|
||||
shadowMat[12] = 0.f - lightpos[0] * groundplane[3];
|
||||
|
||||
shadowMat[1] = 0.f - lightpos[1] * groundplane[0];
|
||||
shadowMat[5] = dot - lightpos[1] * groundplane[1];
|
||||
shadowMat[9] = 0.f - lightpos[1] * groundplane[2];
|
||||
shadowMat[13] = 0.f - lightpos[1] * groundplane[3];
|
||||
|
||||
shadowMat[2] = 0.f - lightpos[2] * groundplane[0];
|
||||
shadowMat[6] = 0.f - lightpos[2] * groundplane[1];
|
||||
shadowMat[10] = dot - lightpos[2] * groundplane[2];
|
||||
shadowMat[14] = 0.f - lightpos[2] * groundplane[3];
|
||||
|
||||
shadowMat[3] = 0.f - lightpos[3] * groundplane[0];
|
||||
shadowMat[7] = 0.f - lightpos[3] * groundplane[1];
|
||||
shadowMat[11] = 0.f - lightpos[3] * groundplane[2];
|
||||
shadowMat[15] = dot - lightpos[3] * groundplane[3];
|
||||
}
|
||||
|
||||
void LimitFPS(int targetFPS) {
|
||||
static auto lastTime = std::chrono::high_resolution_clock::now();
|
||||
double targetFrameDuration = 1.0 / targetFPS;
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> elapsed = currentTime - lastTime;
|
||||
|
||||
while (elapsed.count() < targetFrameDuration) {
|
||||
currentTime = std::chrono::high_resolution_clock::now();
|
||||
elapsed = currentTime - lastTime;
|
||||
}
|
||||
lastTime = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void SetDCPixelFormat(HDC hDC) {
|
||||
int nPixelFormat;
|
||||
static PIXELFORMATDESCRIPTOR pfd = {
|
||||
sizeof(PIXELFORMATDESCRIPTOR), 1,
|
||||
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
|
||||
PFD_TYPE_RGBA, 24,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0
|
||||
};
|
||||
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
|
||||
SetPixelFormat(hDC, nPixelFormat, &pfd);
|
||||
}
|
||||
|
||||
HPALETTE GetOpenGLPalette(HDC hDC) {
|
||||
HPALETTE hRetPal = NULL;
|
||||
PIXELFORMATDESCRIPTOR pfd;
|
||||
LOGPALETTE* pPal;
|
||||
int nPixelFormat;
|
||||
int nColors;
|
||||
int i;
|
||||
BYTE RedRange, GreenRange, BlueRange;
|
||||
|
||||
nPixelFormat = GetPixelFormat(hDC);
|
||||
DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
|
||||
|
||||
if (!(pfd.dwFlags & PFD_NEED_PALETTE)) return NULL;
|
||||
|
||||
nColors = 1 << pfd.cColorBits;
|
||||
pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
|
||||
|
||||
pPal->palVersion = 0x300;
|
||||
pPal->palNumEntries = nColors;
|
||||
|
||||
RedRange = (1 << pfd.cRedBits) - 1;
|
||||
GreenRange = (1 << pfd.cGreenBits) - 1;
|
||||
BlueRange = (1 << pfd.cBlueBits) - 1;
|
||||
|
||||
for (i = 0; i < nColors; i++) {
|
||||
pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
|
||||
pPal->palPalEntry[i].peRed = (unsigned char)((double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
|
||||
pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
|
||||
pPal->palPalEntry[i].peGreen = (unsigned char)((double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
|
||||
pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
|
||||
pPal->palPalEntry[i].peBlue = (unsigned char)((double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
|
||||
pPal->palPalEntry[i].peFlags = 0;
|
||||
}
|
||||
|
||||
hRetPal = CreatePalette(pPal);
|
||||
SelectPalette(hDC, hRetPal, FALSE);
|
||||
RealizePalette(hDC);
|
||||
free(pPal);
|
||||
return hRetPal;
|
||||
}
|
||||
Reference in New Issue
Block a user