Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\fileio.cpp":
- #include <windows.h>
- #include <public_stuff.hpp>
- BinaryData::BinaryData(const char* file_path)
- : size( fileio_size(file_path) )
- {
- data_void = nullptr;
- if(size == FILEIO_SIZE_ERR) return;
- size_t _size;
- data_void = fileio_read(file_path, &_size);
- if(_size != size){ // Just in case
- mem_free(&data_void);
- }
- }
- BinaryData::BinaryData(const void* src, size_t _size)
- : size( _size )
- {
- data_void = mem_alloc(size+1);
- if(data_void == nullptr) return; // Failed to allocate memory for data
- // Adding +1 to the Alloc size makes it so that I can always
- // make sure that the file's data is null-terminated, like so:
- data_char[size] = 0;
- if(src != nullptr) mem_copy(data_char, src, size);
- }
- BinaryData::~BinaryData(){
- mem_free(&data_void);
- }
- /******************************************************************************/
- void* fileio_read(const char* file_path, size_t* dataSize_p){
- size_t size = fileio_size(file_path);
- if(size == FILEIO_SIZE_ERR) return nullptr;
- // Open the file
- HANDLE fileHandle = CreateFileA(file_path, GENERIC_READ, FILE_SHARE_READ,
- nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
- if(fileHandle == INVALID_HANDLE_VALUE) return nullptr; // Failed to open file
- // Allocate memory for file data
- void* data = mem_alloc(size+1);
- if(data == nullptr){ CloseHandle(fileHandle); return nullptr; }
- ((char*)data)[size] = 0;
- DWORD bytesRead;
- BOOL success = ReadFile(fileHandle, data, (DWORD)size, &bytesRead, nullptr);
- CloseHandle(fileHandle);
- if(!success || bytesRead < size)
- {
- mem_free(&data);
- return nullptr;
- }
- if(dataSize_p != nullptr) *dataSize_p = size;
- return data;
- }
- bool fileio_write(const char* file_path, const void* data,
- size_t data_size, bool append)
- {
- if(file_path == nullptr || data == nullptr) return false;
- // Writing files >=4GiB is currently unsupported!
- // (If you really wanted to, I guess you could write files larger than 4 gigs
- // with an overwrite and a series of appends, so that's always an option?)
- if(data_size >= 0xFFFFFFFF) return false;
- DWORD accessMode, creationDisposition;
- if(append){ // Create or append to existing
- accessMode = FILE_APPEND_DATA;
- creationDisposition = OPEN_ALWAYS;
- } else { // Create or overwrite existing
- accessMode = GENERIC_WRITE;
- creationDisposition = CREATE_ALWAYS;
- }
- HANDLE fileHandle = CreateFileA(file_path, accessMode, 0, nullptr,
- creationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
- if(fileHandle == INVALID_HANDLE_VALUE) return false; // Failed to open file
- // If data_size *is* 0, then just create/open the file without writing to it
- if(data_size > 0){
- DWORD bytesWritten;
- BOOL success = WriteFile(fileHandle, data, (DWORD)data_size, &bytesWritten, nullptr);
- CloseHandle(fileHandle);
- if(!success) return false; // Failed to write to file
- if(bytesWritten < data_size) return false; // Bytes read was less than data_size
- } else {
- CloseHandle(fileHandle);
- }
- return true;
- }
- /******************************************************************************/
- size_t fileio_size(const char* file_path){
- if(file_path == nullptr) return FILEIO_SIZE_ERR;
- HANDLE fileHandle = CreateFileA(file_path, 0, FILE_SHARE_READ, nullptr,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,nullptr);
- if(fileHandle == INVALID_HANDLE_VALUE){
- if(GetLastError() == ERROR_FILE_NOT_FOUND) return FILEIO_SIZE_ERR; // File not found
- else return FILEIO_SIZE_ERR; // Failed to open file
- }
- LARGE_INTEGER fileSize;
- BOOL success = GetFileSizeEx(fileHandle, &fileSize);
- CloseHandle(fileHandle); // Close the file whether or not the query succeeds
- if(!success) return FILEIO_SIZE_ERR;
- return (size_t)fileSize.QuadPart;
- }
- #define IS_READONLY(_attributes) \
- ( (_attributes)&FILE_ATTRIBUTE_READONLY )
- #define IS_DIRECTORY(_attributes) \
- ( (_attributes)&FILE_ATTRIBUTE_DIRECTORY )
- #define IS_READONLY_FILE(_attributes) \
- ( IS_READONLY(_attributes) && !IS_DIRECTORY(_attributes) )
- bool fileio_isreadonly(const char* file_path){
- if(file_path == nullptr) return false; // file_path is not a real string
- DWORD fileAttributes = GetFileAttributesA(file_path);
- if(fileAttributes == INVALID_FILE_ATTRIBUTES){
- if(GetLastError() != ERROR_FILE_NOT_FOUND) return false; // Failed to get file attributes
- else return false; // File doesn't exist
- } else if(IS_READONLY_FILE(fileAttributes)){
- return true; // File is read-only
- } else {
- return false; // File is not read-only
- }
- }
- #define IS_NORMAL_FILE(_attributes) \
- ( !( IS_READONLY(_attributes) || IS_DIRECTORY(_attributes) ) )
- bool fileio_exists(const char* file_path){
- if(file_path == nullptr) return false; // file_path is not a real string
- DWORD fileAttributes = GetFileAttributesA(file_path);
- if(fileAttributes == INVALID_FILE_ATTRIBUTES){
- if(GetLastError() != ERROR_FILE_NOT_FOUND) return false; // Failed to get file attributes
- else return false;
- } else if(IS_NORMAL_FILE(fileAttributes)){
- return true;
- } else {
- return false; // Path exists, but is not associated with a normal file
- }
- }
- bool fileio_delete(const char* file_path){
- if(!fileio_exists(file_path)) return false; // File does not exist
- if(!DeleteFileA(file_path) ) return false; // Failed to delete file
- return true;
- }
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\functions.cpp":
- #include <win32/audio.hpp>
- #include <win32/video.hpp>
- _Winmm_func_t _Winmm_func;
- char _Winmm_names_a[] = // prefix = "time"
- "GetDevCaps\0"
- "BeginPeriod\0"
- "\0";
- char _Winmm_names_b[] = // prefix = "waveOut"
- //"GetNumDevs\0"
- "GetDevCapsA\0"
- //"GetVolume\0"
- "SetVolume\0"
- //"GetErrorTextA\0"
- "Open\0"
- //"Close\0"
- "PrepareHeader\0"
- //"UnprepareHeader\0"
- "Write\0"
- "Pause\0"
- //"Restart\0"
- //"Reset\0"
- //"BreakLoop\0"
- //"GetPosition\0"
- //"GetPitch\0"
- //"SetPitch\0"
- //"GetPlaybackRate\0"
- //"SetPlaybackRate\0"
- //"GetID\0"
- //"Message\0"
- "\0";
- _User32_func_t _User32_func;
- char _User32_names_a[] = // prefix = ""
- "RegisterClassA\0"
- "CreateWindowExA\0"
- "DefWindowProcA\0"
- "InvalidateRect\0"
- "UpdateWindow\0"
- "BeginPaint\0"
- "EndPaint\0"
- "PeekMessageA\0"
- "DispatchMessageA\0"
- "DestroyWindow\0"
- "ReleaseDC\0"
- "GetDC\0"
- "PostQuitMessage\0"
- "MessageBoxA\0"
- "TranslateMessage\0"
- "GetWindowLongA\0"
- "AdjustWindowRectEx\0"
- "LoadCursorA\0"
- "MapVirtualKeyA\0"
- //"GetCursorPos\0"
- "ScreenToClient\0"
- "ShowCursor\0"
- "ClipCursor\0"
- "GetClientRect\0"
- "ClientToScreen\0"
- "SetCapture\0"
- "ReleaseCapture\0"
- "SetCursorPos\0"
- "RegisterRawInputDevices\0"
- "GetRawInputData\0"
- "FillRect\0"
- "ShowWindow\0"
- "\0";
- _Gdi32_func_t _Gdi32_func;
- char _Gdi32_names_a[] = // prefix = ""
- "CreateCompatibleDC\0"
- "CreateDIBSection\0"
- "SelectObject\0"
- "DeleteObject\0"
- "BitBlt\0"
- "DeleteDC\0"
- "StretchBlt\0"
- "CreateCompatibleBitmap\0"
- "SetStretchBltMode\0"
- "SetDIBColorTable\0"
- "CreateSolidBrush\0"
- "SetPixelFormat\0"
- "ChoosePixelFormat\0"
- "SwapBuffers\0"
- "\0";
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\input.cpp":
- #include <win32/input.hpp>
- #include <public_stuff.hpp>
- CRITICAL_SECTION events_lock;
- Event events_queue[65536];
- u16 events_next = 0;
- u16 events_end = 0;
- Event_Key_Mod key_mods;
- bool key_states[256] = {0};
- Point2d mouse_position = {0};
- bool mouse_was_moved_before = false;
- bool cursor_trapped = false;
- bool cursor_hidden = false;
- extern HWND win;
- int InputInit(){
- // This function has no failure condition
- InitializeCriticalSectionAndSpinCount(&events_lock, 2048);
- return 0;
- }
- void InputQuit(){}
- // Returns false if queue is full
- bool AddToEventQueue(Event& event){
- EnterCriticalSection(&events_lock);
- bool success = false;
- if((events_end+1) != events_next){
- events_queue[events_end++] = event;
- success = true;
- }
- LeaveCriticalSection(&events_lock);
- return success;
- }
- // Returns a EVENT_NULL event if queue is empty
- Event RemoveFromEventQueue(){
- EnterCriticalSection(&events_lock);
- Event event;
- event.type = EVENT_NULL;
- if(events_next != events_end)
- event = events_queue[events_next++];
- LeaveCriticalSection(&events_lock);
- return event;
- }
- // Calling this exclusively in the main thread is recommended
- bool pollEvent(Event* event_p){
- // Take one event off the current event queue and set it to *event_p
- EnterCriticalSection(&events_lock);
- Event event = RemoveFromEventQueue();
- LeaveCriticalSection(&events_lock);
- // If previous event queue is now empty, process any pending window messages,
- // while adding any events generated by WindowProc to the event queue
- if(events_next == events_end){
- MSG message;
- while(PeekMessageA(&message, nullptr, 0, 0, PM_REMOVE)){
- TranslateMessage(&message);
- DispatchMessageA(&message);
- }
- }
- if(event_p != nullptr) *event_p = event;
- return event.type != EVENT_NULL;
- }
- bool is_cursor_trapped(){
- return cursor_trapped; // Yep, that's all it does
- }
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\main.cpp":
- #include <win32/audio.hpp>
- #include <win32/video.hpp>
- #include <win32/input.hpp>
- #include <win32/opengl.hpp>
- #include <public_stuff.hpp>
- extern HWND win; // from video.cpp
- static HMODULE _Ole32_dll;
- static HMODULE _Winmm_dll;
- static HMODULE _User32_dll;
- static HMODULE _Gdi32_dll;
- static HMODULE _Opengl32_dll;
- int user_main(int argc, char** argv);
- // Doing "*((FARPROC*)&_name)" to work around not
- // being able to directly cast an lvalue.
- // (This is only really useful for isolated function pointers.)
- #define LOAD_FUNC(_pre, _post, _name, _dll) \
- *((FARPROC*)&_name) = loadFunction(_pre, _post, _dll)
- static char _functionNameBuffer[256];
- FARPROC loadFunction(const char* prefix, const char* postfix, HMODULE dll){
- strnCpy(_functionNameBuffer, prefix, 255);
- strCat(_functionNameBuffer, postfix);
- _functionNameBuffer[255] = 0; // Just in case
- return GetProcAddress(dll, _functionNameBuffer);
- }
- // Postfixes string should look like: "nameA\0nameB\0nameC\0\0"
- bool loadFunctions(const char* prefix, const char* postfixes, HMODULE dll,
- FARPROC* funcs, int from)
- {
- while(*postfixes != '\0'){
- if(!( funcs[from++] = loadFunction(prefix, postfixes, dll) ))
- return false;
- // +1 to get past null-terminator
- postfixes += strnLen(postfixes, 256)+1;
- }
- return true;
- }
- //
- #ifdef _DEBUG
- #define MAIN_ASSERT(_success, _code, _msg) \
- if(!(_success)){ \
- returnCode = (_code); \
- _printf("ERROR: %s\n", _msg); \
- goto _main_return; \
- }
- #else
- #define MAIN_ASSERT(_success, _code, _msg) \
- if(!(_success)){ \
- returnCode = (_code); \
- showMessageBox(_msg, "A fatal error occurred!", \
- MSGBOX_BTN_OK | MSGBOX_ICN_ERROR, 0); \
- goto _main_return; \
- }
- #endif
- int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
- LPSTR lpszArg, int nCmdShow)
- {
- int err, returnCode = 0;
- // g++ doesn't seem to allow gotos that might maybe possibly step
- // over declarations, so the exit is near the top as a workaround
- if(0){ _main_return:
- // Seemingly due to WinMM being janky and old, in some environments the
- // program hangs indefinitely unless I explicitly exit the process like
- // this for some reason!
- ExitProcess((UINT)returnCode);
- return returnCode;
- }
- #ifdef _DEBUG
- u64 timeStart = timeGetPerfCounter();
- #endif /* _DEBUG */
- _Ole32_dll = LoadLibrary("Ole32.dll");
- MAIN_ASSERT(_Ole32_dll, -1, "failed to load \"Ole32.dll\"");
- _Winmm_dll = LoadLibrary("Winmm.dll");
- MAIN_ASSERT(_Winmm_dll, -2, "failed to load \"Winmm.dll\"");
- _User32_dll = LoadLibrary("User32.dll");
- MAIN_ASSERT(_User32_dll, -3, "failed to load \"User32.dll\"");
- _Gdi32_dll = LoadLibrary("Gdi32.dll");
- MAIN_ASSERT(_Gdi32_dll, -4, "failed to load \"Gdi32.dll\"");
- _Opengl32_dll = LoadLibrary("Opengl32.dll");
- MAIN_ASSERT(_Opengl32_dll, -5, "failed to load \"Opengl32.dll\"");
- MAIN_ASSERT(loadFunctions("time", _Winmm_names_a, _Winmm_dll,
- _Winmm_func.ptrs, 0), -11,
- "failed to load \"time\" functions from WinMM");
- MAIN_ASSERT(loadFunctions("waveOut", _Winmm_names_b, _Winmm_dll,
- _Winmm_func.ptrs, 2), -12,
- "failed to load \"waveOut\" functions from WinMM");
- #define LOAD_USER32_FUNCS(_prefix, _namegroup, _from, _errcode, _msg) \
- MAIN_ASSERT(loadFunctions(_prefix, _User32_names_##_namegroup, _User32_dll,\
- _User32_func.ptrs, (_from)), (_errcode), _msg)
- LOAD_USER32_FUNCS("", a, 0, -13, "failed to load functions from User32");
- #define LOAD_GDI32_FUNCS(_prefix, _namegroup, _from, _errcode, _msg) \
- MAIN_ASSERT(loadFunctions(_prefix, _Gdi32_names_##_namegroup, _Gdi32_dll,\
- _Gdi32_func.ptrs, (_from)), (_errcode), _msg)
- LOAD_GDI32_FUNCS("", a, 0, -14, "failed to load functions from Gdi32");
- #define LOAD_OPENGL32_FUNCS(_prefix, _namegroup, _from, _errcode, _msg)\
- MAIN_ASSERT(loadFunctions(_prefix, _Opengl32_names_##_namegroup, \
- _Opengl32_dll, _Opengl32_func.ptrs_wgl_1_1,\
- (_from)), (_errcode), _msg)
- LOAD_OPENGL32_FUNCS("wgl", wgl_1_1, 0,
- -15, "failed to load \"wgl\" functions from Opengl32");
- LOAD_OPENGL32_FUNCS("gl", gl_1_1, WGL_1_1_LEN,
- -16, "failed to load \"gl\" functions from Opengl32");
- // Request 1ms minimum delay, to make timeSleep(16) reliably take 15 to 17 ms
- MAIN_ASSERT(!timeBeginPeriod(1), -50, "failed to set timer resolution");
- err = WaveOutInit(); // Also starts audio automatically
- MAIN_ASSERT(err == 0, err-100, "failed to initialize waveOut device");
- err = WindowInit(hThisInst);
- MAIN_ASSERT(err == 0, err-200, "failed to initialize window");
- err = InputInit();
- MAIN_ASSERT(err == 0, err-300, "failed to initialize event system");
- err = OpenGLInit();
- MAIN_ASSERT(err == 0, err-400, "failed to initialize OpenGL context");
- #if defined(STDLIB_USED)
- srand(timeGetPerfCounter()&0xFFFFFFFF);
- #endif
- #ifdef _DEBUG
- s64 timeDeltaTicks = (s64)(timeGetPerfCounter()-timeStart);
- f64 timeDeltaSecs = (f64)timeDeltaTicks/timeGetPerfFreq();
- _printf("Initialized in: %.4f seconds!\n", timeDeltaSecs);
- #endif
- ShowWindow(win, SW_SHOW);
- try {
- returnCode = user_main(0, nullptr); // TBD: provide main's arguments
- } catch(const char* errorText) {
- MAIN_ASSERT(false, returnCode-1000, errorText);
- } catch(...){
- MAIN_ASSERT(false, returnCode-1000, "(UNKNOWN EXCEPTION TYPE)");
- }
- ShowWindow(win, SW_HIDE);
- OpenGLQuit();
- InputQuit();
- WindowQuit();
- WaveOutQuit();
- // A nonzero value means there was a memory leak!
- if(mem_getNumAllocations() > 0){
- _printf("# OF ALLOCATIONS (THIS NUMBER SHOULD BE 0!): %llu\n",
- mem_getNumAllocations());
- }
- // Make sure no unused parameter warnings occur for these,
- // regardless of whether or not they end up being used
- (void)hThisInst;
- (void)hPrevInst;
- (void)lpszArg;
- (void)nCmdShow;
- goto _main_return;
- }
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\memory.cpp":
- #include <public_stuff.hpp>
- //turns something into a void**
- //(this makes some code here easier for me to read)
- #define VPP(_ptr_p) ((void**)(_ptr_p))
- size_t numAllocations = 0;
- void* mem_alloc(size_t size){
- void* newHeapMemory = malloc(size);
- if(newHeapMemory != nullptr) ++numAllocations;
- return newHeapMemory;
- }
- void mem_free(void* ptr_p){
- if(VPP(ptr_p) != nullptr && *VPP(ptr_p) != nullptr){
- --numAllocations;
- free(*VPP(ptr_p));
- *VPP(ptr_p) = nullptr;
- }
- }
- void* mem_realloc(void* ptr_p, size_t newSize){
- void* ptr_new = nullptr;
- if(VPP(ptr_p) != nullptr){
- ptr_new = realloc(*VPP(ptr_p), newSize);
- if(ptr_new != nullptr){
- if(*VPP(ptr_p) == nullptr) ++numAllocations;
- *VPP(ptr_p) = ptr_new;
- }
- }
- return ptr_new;
- }
- size_t mem_getNumAllocations(){ return numAllocations; }
- size_t mem_setNumAllocations(size_t value){ return numAllocations = value; }
- size_t mem_addNumAllocations(s32 amount){
- size_t originalState = numAllocations;
- numAllocations += amount;
- if(amount < 0 && numAllocations > originalState)
- numAllocations = 0; // Integer underflow protection
- return numAllocations;
- }
- // Currently just a wrapper, but now I can make my own implementation
- // whenever I want, without replacing every call to memset with it
- void* mem_set(void* ptr, s32 value, size_t size){
- if(ptr == nullptr) return nullptr; //now it's safe to pass nullptr :D
- return memSet(ptr, value, size);
- }
- void* mem_copy(void* destination, const void* source, size_t size){
- if(destination == nullptr || source == nullptr) return destination;
- return memCpy(destination, source, size);
- }
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\misc.cpp":
- #include <windows.h>
- #include <public_stuff.hpp>
- extern HWND win;
- extern bool win_closed;
- u32 showMessageBox(const char* text, const char* title,
- u32 type, u32 defaultButton)
- {
- if(!text ) text = "";
- if(!title) title = "";
- defaultButton = (defaultButton&3)<<8; //0x000 -> 0x300
- HWND win_handle = (!win_closed) ? win : nullptr;
- return MessageBoxA(win_handle, text, title, type|defaultButton);
- }
- u64 timeGetPerfCounter(){
- u64 result;
- QueryPerformanceCounter((LARGE_INTEGER*)&result);
- return result;
- }
- static u64 perfFreq = 0;
- u64 timeGetPerfFreq(){
- if(!perfFreq)
- QueryPerformanceFrequency((LARGE_INTEGER*)&perfFreq);
- return perfFreq;
- }
- f64 timeGetSeconds(){
- return (f64)timeGetPerfCounter()/timeGetPerfFreq();
- }
- void timeSleep(u32 milliseconds){
- Sleep((DWORD)milliseconds);
- }
- /******************************************************************************/
- /******************************************************************************/
- //"opengl_metaballs_2025-06-26\src\win32\opengl.cpp":
- #include <win32/video.hpp>
- #include <win32/opengl.hpp>
- #include <public_stuff.hpp>
- /******************************************************************************/
- _Opengl32_func_t _Opengl32_func;
- // Use loadFunction() (NOT loadOpenGLFunction; this is done in "src/main.cpp"!)
- char _Opengl32_names_wgl_1_1[] = // prefix = "wgl"
- "CopyContext\0"
- "CreateContext\0"
- "CreateLayerContext\0"
- "DeleteContext\0"
- "DescribeLayerPlane\0"
- "GetCurrentContext\0"
- "GetCurrentDC\0"
- "GetLayerPaletteEntries\0"
- "GetProcAddress\0"
- "MakeCurrent\0"
- "RealizeLayerPalette\0"
- "SetLayerPaletteEntries\0"
- "ShareLists\0"
- "SwapLayerBuffers\0"
- "UseFontBitmapsA\0"
- "UseFontOutlinesA\0"
- "\0";
- // Use loadFunction() (NOT loadOpenGLFunction; this is done in "src/main.cpp"!)
- char _Opengl32_names_gl_1_1[] = // prefix = "gl"
- "ClearColor\0"
- "Clear\0"
- "PolygonMode\0"
- "Viewport\0"
- "GetBooleanv\0"
- "GetDoublev\0"
- "GetFloatv\0"
- "GetIntegerv\0"
- "GetError\0"
- "\0";
- // Use loadOpenGLFunction()
- char _Opengl32_names_wgl[] = // prefix = "wgl"
- "\0";
- // Use loadOpenGLFunction()
- char _Opengl32_names_gl[] = // prefix = "gl"
- "CreateShader\0"
- "ShaderSource\0"
- "CompileShader\0"
- "GetShaderiv\0"
- "GetShaderInfoLog\0"
- "CreateProgram\0"
- "AttachShader\0"
- "LinkProgram\0"
- "GetProgramInfoLog\0"
- "DeleteShader\0"
- "GenVertexArrays\0"
- "GenBuffers\0"
- "BindVertexArray\0"
- "BindBuffer\0"
- "BufferData\0"
- "VertexAttribPointer\0"
- "VertexAttribIPointer\0"
- "EnableVertexAttribArray\0"
- "DisableVertexAttribArray\0"
- "UseProgram\0"
- "DrawArrays\0"
- "DeleteVertexArrays\0"
- "DeleteBuffers\0"
- "DeleteProgram\0"
- "GetInteger64v\0"
- "GetBooleani_v\0"
- "GetIntegeri_v\0"
- "GetInteger64i_v\0"
- "GetProgramiv\0"
- "BufferSubData\0"
- "MapBufferRange\0"
- "GenTextures\0"
- "BindTexture\0"
- "TexBuffer\0"
- "GetUniformLocation\0"
- "Uniform1f\0"
- "Uniform2f\0"
- "Uniform3f\0"
- "Uniform4f\0"
- "Uniform1i\0"
- "Uniform2i\0"
- "Uniform3i\0"
- "Uniform4i\0"
- //"Uniform1ui\0"
- //"Uniform2ui\0"
- //"Uniform3ui\0"
- //"Uniform4ui\0"
- //
- //"Uniform1fv\0"
- //"Uniform2fv\0"
- //"Uniform3fv\0"
- //"Uniform4fv\0"
- //
- //"Uniform1iv\0"
- //"Uniform2iv\0"
- //"Uniform3iv\0"
- //"Uniform4iv\0"
- //
- //"Uniform1uiv\0"
- //"Uniform2uiv\0"
- //"Uniform3uiv\0"
- //"Uniform4uiv\0"
- //
- //"UniformMatrix2fv\0"
- //"UniformMatrix3fv\0"
- //"UniformMatrix4fv\0"
- //"UniformMatrix2x3fv\0"
- //"UniformMatrix3x2fv\0"
- //"UniformMatrix2x4fv\0"
- //"UniformMatrix4x2fv\0"
- //"UniformMatrix3x4fv\0"
- //"UniformMatrix4x3fv\0"
- "DeleteTextures\0"
- "\0";
- /******************************************************************************/
- static char _funcNameBufferOpenGL[256];
- // Only works after wglGetProcAddress() has actually been loaded during init
- PROC loadOpenGLFunction(const char* prefix, const char* postfix){
- strnCpy(_funcNameBufferOpenGL, prefix, 255);
- strCat(_funcNameBufferOpenGL, postfix);
- _funcNameBufferOpenGL[255] = 0; // Just in case
- return wglGetProcAddress(_funcNameBufferOpenGL);
- }
- // postfixes string should look like: "nameA\0nameB\0nameC\0\0"
- bool loadOpenGLFunctions(const char* prefix, const char* postfixes,
- PROC* funcs, int from = 0)
- {
- while(*postfixes != '\0'){
- if(!( funcs[from++] = loadOpenGLFunction(prefix, postfixes) ))
- return false;
- // +1 to get past null-terminator
- postfixes += strnLen(postfixes, 256)+1;
- }
- return true;
- }
- /******************************************************************************/
- static inline bool SetupDummyPixelFormat(HDC hDC){
- PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), 1, // nSize, nVersion
- PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // dwFlags
- PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, // iPixelType, cColorBits, empty*6
- 0, 0, 0, 0, 0, 0, 0,
- 24, 8, 0, PFD_MAIN_PLANE,
- 0, 0, 0, 0
- };
- int pf = ChoosePixelFormat(hDC, &pfd);
- if(!pf) return false;
- return SetPixelFormat(hDC, pf, &pfd) == TRUE;
- }
- extern HDC win_dc;
- HGLRC renderCtx; // Rendering context
- PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB_;
- int OpenGLInit(){
- if(!SetupDummyPixelFormat(win_dc)) return -1;
- // Make a temporary, dummy context
- renderCtx = wglCreateContext(win_dc);
- if(renderCtx == nullptr) return -2;
- if(!wglMakeCurrent(win_dc, renderCtx)) return -3;
- // Get a pointer to wglCreateContextAttribsARB()
- #define WGL_CCA_ARB_t PFNWGLCREATECONTEXTATTRIBSARBPROC // Lol
- wglCreateContextAttribsARB_ = (WGL_CCA_ARB_t)loadOpenGLFunction(
- "wgl","CreateContextAttribsARB");
- if(wglCreateContextAttribsARB_ == nullptr) return -4;
- // Create the actual 'OpenGL v3.3 core' context (then delete the dummy one)
- #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
- #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
- #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
- #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
- const int context_attributes[] = {
- WGL_CONTEXT_MAJOR_VERSION_ARB, 3, // ver X._
- WGL_CONTEXT_MINOR_VERSION_ARB, 3, // ver _.X
- WGL_CONTEXT_PROFILE_MASK_ARB , WGL_CONTEXT_CORE_PROFILE_BIT_ARB, // core
- 0 // (null-terminator)
- };
- HGLRC renderCtx_old = renderCtx;
- renderCtx = wglCreateContextAttribsARB(win_dc, 0, context_attributes);
- if(renderCtx == nullptr) return -5;
- wglMakeCurrent(NULL, NULL);
- wglDeleteContext(renderCtx_old);
- wglMakeCurrent(win_dc, renderCtx);
- // Load the relevant OpenGL extensions
- if(!loadOpenGLFunctions("wgl", _Opengl32_names_wgl, _Opengl32_func.ptrs_wgl))
- {
- return -6;
- }
- if(!loadOpenGLFunctions("gl", _Opengl32_names_gl, _Opengl32_func.ptrs_gl))
- {
- return -7;
- }
- return 0;
- }
- void OpenGLQuit(){
- wglMakeCurrent(NULL, NULL);
- if(renderCtx != nullptr)
- wglDeleteContext(renderCtx);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement