Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>glShaderSource</code> doesn't have to be quite that complex (it's not dependent on pointer-per-line and I'm not sure it will your code if you give it a line split on the 255th character). Put all your text into one string or buffer, take a pointer to it and call glShaderSource with a pointer to that pointer.</p> <pre><code>bool GLShader::LoadShader(string sFile, int a_iType) { std::ifstream isSrc(sFile.c_str(), std::ifstream::in); if(!isSrc.good()) return false; std::string sLines = ""; std::string sLine; while (isSrc.good()) { std::getline(isSrc, sLine); sLines.append(sLine); } isSrc.close(); uiShader = glCreateShader(a_iType); const char* szSrc = sLines.c_str(); glShaderSource(uiShader, 1, &amp;szSrc, NULL); </code></pre> <p>For fun and giggles, a version using memory-mapped file io.</p> <pre><code>bool GLShader::LoadShader(const std::string&amp; sFile, int a_iType) { bool success = false; // Windows implementation. HANDLE fileHandle = INVALID_HANDLE_VALUE, mappingHandle = INVALID_HANDLE_VALUE; do { // not actually a loop fileHandle = CreateFile(sFile.c_str(), FILE_GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); if (fileHandle == INVALID_HANDLE_VALUE) break; mappingHandle = CreateFileMapping(fileHandle, nullptr, PAGE_READONLY, 0, 0, nullptr); if (mappingHandle == INVALID_HANDLE_VALUE) break; LPVOID const lpvData = MapViewOfFileEx(mappingHandle, FILE_MAP_READ, 0, 0, 0, nullptr); if (!lpvData) break; /* ... other stuff */ const char* pszSrc = static_cast&lt;const char*&gt;(lpvData); glShaderSource(uiShader, 1, &amp;pszSrc, nullptr); /* ... the rest of your code */ success = true; } while (false); // never repeat // cleanup if (mappingHandle != INVALID_HANDLE_VALUE) CloseHandle(mappingHandle); if (fileHandle != INVALID_HANDLE_VALUE) CloseHandle(fileHandle); return success; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload