allow cycling through shaders in both directions, load them from the game's shader directory
This commit is contained in:
parent
dc287b027d
commit
8d4c747023
4 changed files with 828 additions and 813 deletions
src
|
@ -10,7 +10,7 @@ extern PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebuffer;
|
|||
#endif
|
||||
extern GLuint fullscreenFramebuffer;
|
||||
extern unsigned ShaderIndex;
|
||||
extern bool LoadShaders(char* shadernameOut);
|
||||
extern bool LoadShaders(int direction, char* shadernameOut);
|
||||
extern bool LoadShaderExtensions();
|
||||
extern void SetupFramebuffer();
|
||||
extern void RenderFramebufferToScreen();
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -848,11 +848,11 @@ static void SdlDoEvent(const EventCallback &callbacks, SDL_Event &event)
|
|||
|
||||
case SDL_KEYDOWN:
|
||||
if (GLShaderPipelineSupported
|
||||
&& event.key.keysym.sym == SDLK_SLASH
|
||||
&& (event.key.keysym.sym == SDLK_SLASH || event.key.keysym.sym == SDLK_BACKSLASH)
|
||||
&& event.key.keysym.mod & KMOD_ALT
|
||||
&& event.key.keysym.mod & KMOD_CTRL) {
|
||||
char shadername[1024] = { '\0' };
|
||||
LoadShaders(shadername);
|
||||
LoadShaders(event.key.keysym.sym == SDLK_SLASH ? 1 : -1, shadername);
|
||||
SetMessage(shadername);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -81,9 +81,18 @@ void printProgramInfoLog(GLuint obj, const char* prefix)
|
|||
}
|
||||
}
|
||||
|
||||
unsigned ShaderIndex = 0;
|
||||
unsigned ShaderIndex = -1;
|
||||
|
||||
/* This does not have to be very efficient, it is only called when the shader
|
||||
is changed by the user.
|
||||
*/
|
||||
extern bool LoadShaders(int direction, char* shadernameOut) {
|
||||
ShaderIndex += direction;
|
||||
if (direction == 0 && ShaderIndex == -1) {
|
||||
// TODO: load from preferences
|
||||
ShaderIndex = 0;
|
||||
}
|
||||
|
||||
extern bool LoadShaders(char* shadernameOut) {
|
||||
GLuint vs, fs;
|
||||
GLint params;
|
||||
fs = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
@ -92,7 +101,13 @@ extern bool LoadShaders(char* shadernameOut) {
|
|||
}
|
||||
|
||||
std::vector<FileList> flp;
|
||||
int n = ReadDataDirectory(".", flp);
|
||||
std::string shaderPath(StratagusLibPath);
|
||||
#ifdef _WIN32
|
||||
shaderPath.append("\\shaders\\");
|
||||
#else
|
||||
shaderPath.append("/shaders/");
|
||||
#endif
|
||||
int n = ReadDataDirectory(shaderPath.c_str(), flp);
|
||||
int numShaderFiles = 0;
|
||||
int shaderFileToIdx[1024];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
|
@ -103,18 +118,18 @@ extern bool LoadShaders(char* shadernameOut) {
|
|||
}
|
||||
}
|
||||
if (numShaderFiles <= 0) return false;
|
||||
if (numShaderFiles <= ShaderIndex) {
|
||||
if (numShaderFiles <= ShaderIndex || ShaderIndex < 0) {
|
||||
ShaderIndex = ShaderIndex % numShaderFiles;
|
||||
}
|
||||
|
||||
if (shadernameOut) {
|
||||
strncpy(shadernameOut, flp[shaderFileToIdx[ShaderIndex]].name.c_str(), 1023);
|
||||
}
|
||||
std::ifstream myfile(flp[shaderFileToIdx[ShaderIndex]].name);
|
||||
shaderPath.append(flp[shaderFileToIdx[ShaderIndex]].name);
|
||||
std::ifstream myfile(shaderPath);
|
||||
std::string contents((std::istreambuf_iterator<char>(myfile)),
|
||||
std::istreambuf_iterator<char>());
|
||||
myfile.close();
|
||||
ShaderIndex++;
|
||||
|
||||
const char *fragmentSrc[2] = { "#define FRAGMENT\n", contents.c_str() };
|
||||
const char *vertexSrc[2] = { "#define VERTEX\n", contents.c_str() };
|
||||
|
@ -200,7 +215,7 @@ extern bool LoadShaderExtensions() {
|
|||
glDrawBuffers = (PFNGLDRAWBUFFERSPROC)(uintptr_t)SDL_GL_GetProcAddress("glDrawBuffers");
|
||||
glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)(uintptr_t)SDL_GL_GetProcAddress("glCheckFramebufferStatus");
|
||||
if (glCreateShader && glGenFramebuffers && glGetUniformLocation && glActiveTextureProc) {
|
||||
return LoadShaders(NULL);
|
||||
return LoadShaders(0, NULL);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue