Shaders: Trim whitespace and comments on preset strings.

Report some file open failures.
This commit is contained in:
Brandon Wright 2019-01-25 18:51:50 -06:00
parent 61f5141bcc
commit 4c3d886dab
2 changed files with 41 additions and 20 deletions

View File

@ -19,6 +19,17 @@ static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f };
static std::string strip(const std::string str)
{
size_t start = 0, end = 0;
end = str.rfind("//");
if (end == std::string::npos)
end = str.length();
for (start = 0; isspace(str[start]); start++) {}
for (; isspace(str[end - 1]); end--) {}
return str.substr(start, end - start);
}
static int scale_string_to_enum(const char *string, bool last)
{
if (!strcasecmp(string, "source"))
@ -146,46 +157,46 @@ bool GLSLShader::load_shader_preset_file(char *filename)
pass.filter = conf.Exists(key) ? (conf.GetBool(key) ? GL_LINEAR : GL_NEAREST) : GLSL_UNDEFINED;
sprintf(key, "::scale_type%u", i);
const char* scaleType = conf.GetString(key, "");
std::string scaleType = strip(conf.GetString(key, ""));
if (!strcasecmp(scaleType, ""))
if (!scaleType.empty())
{
sprintf(key, "::scale_type_x%u", i);
const char* scaleTypeX = conf.GetString(key, "");
pass.scale_type_x = scale_string_to_enum(scaleTypeX, i == shader_count - 1);
std::string scaleTypeX = strip(conf.GetString(key, ""));
pass.scale_type_x = scale_string_to_enum(scaleTypeX.c_str(), i == shader_count - 1);
sprintf(key, "::scale_type_y%u", i);
const char* scaleTypeY = conf.GetString(key, "");
pass.scale_type_y = scale_string_to_enum(scaleTypeY, i == shader_count - 1);
std::string scaleTypeY = strip(conf.GetString(key, ""));
pass.scale_type_y = scale_string_to_enum(scaleTypeY.c_str(), i == shader_count - 1);
}
else
{
int scale_type = scale_string_to_enum(scaleType, i == shader_count - 1);
int scale_type = scale_string_to_enum(scaleType.c_str(), i == shader_count - 1);
pass.scale_type_x = scale_type;
pass.scale_type_y = scale_type;
}
sprintf(key, "::scale%u", i);
const char* scaleFloat = conf.GetString(key, "");
if (!strcasecmp(scaleFloat, ""))
std::string scaleFloat = strip(conf.GetString(key, ""));
if (!scaleFloat.empty())
{
sprintf(key, "::scale_x%u", i);
const char* scaleFloatX = conf.GetString(key, "1.0");
pass.scale_x = atof(scaleFloatX);
std::string scaleFloatX = strip(conf.GetString(key, "1.0"));
pass.scale_x = atof(scaleFloatX.c_str());
sprintf(key, "::scale_y%u", i);
const char* scaleFloatY = conf.GetString(key, "1.0");
pass.scale_y = atof(scaleFloatY);
std::string scaleFloatY = strip(conf.GetString(key, "1.0"));
pass.scale_y = atof(scaleFloatY.c_str());
}
else
{
pass.scale_x = pass.scale_y = atof(scaleFloat);
pass.scale_x = pass.scale_y = atof(scaleFloat.c_str());
}
sprintf(key, "::shader%u", i);
strcpy(pass.filename, conf.GetString(key, ""));
strcpy(pass.filename, strip(conf.GetString(key, "")).c_str());
sprintf(key, "::wrap_mode%u", i);
pass.wrap_mode = wrap_mode_string_to_enum (conf.GetString (key ,""));
pass.wrap_mode = wrap_mode_string_to_enum(strip(conf.GetString (key ,"")).c_str());
sprintf(key, "::frame_count_mod%u", i);
pass.frame_count_mod = conf.GetInt(key, 0);
@ -207,7 +218,7 @@ bool GLSLShader::load_shader_preset_file(char *filename)
pass.srgb = false;
sprintf(key, "::alias%u", i);
strcpy(pass.alias, conf.GetString(key, ""));
strcpy(pass.alias, strip(conf.GetString(key, "")).c_str());
this->pass.push_back(pass);
}
@ -222,10 +233,10 @@ bool GLSLShader::load_shader_preset_file(char *filename)
sprintf(key, "::%s", id);
strcpy(lut.id, id);
strcpy(lut.filename, conf.GetString(key, ""));
strcpy(lut.filename, strip(conf.GetString(key, "")).c_str());
sprintf(key, "::%s_wrap_mode", id);
lut.wrap_mode = wrap_mode_string_to_enum (conf.GetString (key, ""));
lut.wrap_mode = wrap_mode_string_to_enum(strip(conf.GetString (key, "")).c_str());
sprintf(key, "::%s_mipmap", id);
lut.mipmap = conf.GetBool (key);
@ -494,7 +505,7 @@ bool GLSLShader::load_shader(char *filename)
}
else
{
printf ("Failed to load PNG LUT: %s\n", temp);
printf ("Failed to load PNG LUT: \"%s\"\n", temp);
}
}
else if (!strcasecmp(&temp[length - 4], ".tga"))

View File

@ -90,7 +90,10 @@ bool loadPngImage(const char *name, int &outWidth, int &outHeight,
FILE *fp;
if ((fp = fopen(name, "rb")) == NULL)
{
printf ("File not found: \"%s\"\n", name);
return false;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@ -136,6 +139,7 @@ bool loadPngImage(const char *name, int &outWidth, int &outHeight,
outHasAlpha = false;
break;
default:
printf ("Unsupported color type: %d\n", png_get_color_type(png_ptr, info_ptr));
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return false;
@ -254,7 +258,10 @@ static char *read_file(const char *filename)
file = fopen(filename, "rb");
if (!file)
{
printf("Couldn't open file \"%s\".\n", filename);
return NULL;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
@ -282,7 +289,10 @@ void read_shader_file_with_includes(std::string filename,
{
char *file_contents = read_file(filename.c_str());
if (!file_contents)
{
printf("File contents not read.\n");
return;
}
std::string string_contents(file_contents);
delete[] file_contents;