I'm relatively new to C++ (but not programming as a whole), and I'm messing around with OpenGL in a C++ program being compiled with Visual Studio. I want to make it so I can:
images/texture.png
)The goal is to make it so 100% of my program is in a single .exe, without the need to have to carry around various data files (i.e. images) that the programmer may want the user to be unable to access or tinker with.
As it stands currently, my program is already statically-linking all libraries in use, so the compiler output is just an .exe (and a .pdb), but the program still relies on needing access to image files (which I have to copy in) in the same directory. Again, I want a single .exe I can run anywhere on my system (and potentially on other systems) by itself and have the program function.
I've heard of resource files, and they may be the solution to this problem, but I've found barely any usable information about them; everything I found was about C# or dotnet, neither of which apply to my program. I could simply be misinterpreting my research because of my unfamiliarity with C++.
I feel like I'm missing something obvious. Can someone care to enlighten me?
EDIT: Wanted to clarify that my intention is to have the image data be outputted in a "byte-per-color-value-per-pixel array" format (i.e. having a byte for R, G, B, and A per pixel, then each pixel stores sequentially) AKA the texture format OpenGL uses. Also, the linked question in one of the comments is similar, but none of the answers seemed applicable to my issue.
In Visual Studio C++ project -> Add new item: Resource File(.rc).
Right Click resource.rc file -> View Code.
Add IDB_MY_IMAGE PNG "your path\\texture.png"
in resource.rc
Add #define IDB_MY_IMAGE 10001
in resource.h(created automatically) first line. 10001 is custom ID.
Test code:
#include<Windows.h>
#include<iostream>
#include"resource.h"
int main() {
//load the resource
HMODULE hModule = GetModuleHandle(NULL);
HRSRC hResource = FindResource(hModule, MAKEINTRESOURCEW(IDB_MY_IMAGE), L"PNG");
DWORD imageSize = SizeofResource(hModule, hResource);
HGLOBAL hGlobal = LoadResource(hModule, hResource);
LPVOID imageBuffer = LockResource(hGlobal);
unsigned char* imageData = new unsigned char[imageSize];
memcpy(imageData, imageBuffer, imageSize);
/* Display the png here*/
////////////////
////////////////
UnlockResource(hGlobal);
FreeResource(hGlobal);
delete[] imageData;
return 0;
}