cJSON 的简单应用——保存/解析 Shader 参数 【代码示例】
【摘要】
自定义的 Shader 程序结构体:
struct ShaderProgram{ struct Path { char *vertex_shader_path; char *fragment_shader_path; } path; struct FragmentS...
自定义的 Shader 程序结构体:
-
struct ShaderProgram
-
{
-
struct Path
-
{
-
char *vertex_shader_path;
-
char *fragment_shader_path;
-
} path;
-
-
struct FragmentShaderUniformLocation
-
{
-
char *channel_0;
-
char *channel_1;
-
char *global_time;
-
char *resolution;
-
} fsul;
-
-
};
入口函数:
-
int main()
-
{
-
#ifdef TOFILE
-
createObjects();
-
#else
-
parseFile("result.txt");
-
#endif
-
return 0;
-
}
创建 (Shader Program) JSON 对象并保存为 txt 的函数:
-
void createObjects()
-
{
-
#if MUL
-
ShaderProgram sp[2] =
-
{
-
{
-
{ "../Shader/vs_0.glsl", "../Shader/fs_0.glsl" },
-
{ "iChannel0", "iChannel1", "iGlobalTime", "iResolution" }
-
}, // sp[0]
-
-
{
-
{ "../Shader/vs_1.glsl", "../Shader/fs_1.glsl" },
-
{ "iChannel0", "iChannel1", "iGlobalTime", "iResolution" }
-
} // sp[1]
-
};
-
#else
-
ShaderProgram sp =
-
{
-
{ "../Shader/vs_0.glsl", "../Shader/fs_0.glsl" },
-
{ "iChannel0", "iChannel1", "iGlobalTime", "iResolution" }
-
};
-
-
#endif
-
-
cJSON *root = cJSON_CreateArray();
-
cJSON *fld = NULL, *fldd_0 = NULL, *fldd_1;
-
-
#if MUL
-
for (int i = 0; i < 2; i++)
-
{
-
cJSON_AddItemToArray(root, fld = cJSON_CreateArray());
-
-
cJSON_AddItemToArray(fld, fldd_0 = cJSON_CreateObject());
-
cJSON_AddStringToObject(fldd_0, "Vertex Shader Path", sp[i].path.vertex_shader_path);
-
cJSON_AddStringToObject(fldd_0, "Fragment Shader Path", sp[i].path.fragment_shader_path);
-
-
cJSON_AddItemToArray(fld, fldd_1 = cJSON_CreateObject());
-
cJSON_AddStringToObject(fldd_1, "Channel 0", sp[i].fsul.channel_0);
-
cJSON_AddStringToObject(fldd_1, "Channel 1", sp[i].fsul.channel_1);
-
cJSON_AddStringToObject(fldd_1, "Global Time", sp[i].fsul.global_time);
-
cJSON_AddStringToObject(fldd_1, "Resolution", sp[i].fsul.resolution);
-
}
-
#else
-
-
cJSON_AddItemToArray(root, fld = cJSON_CreateArray());
-
-
cJSON_AddItemToArray(fld, fldd_0 = cJSON_CreateObject());
-
cJSON_AddStringToObject(fldd_0, "Vertex Shader Path", sp.path.vertex_shader_path);
-
cJSON_AddStringToObject(fldd_0, "Fragment Shader Path", sp.path.fragment_shader_path);
-
-
cJSON_AddItemToArray(fld, fldd_1 = cJSON_CreateObject());
-
cJSON_AddStringToObject(fldd_1, "Channel 0", sp.fsul.channel_0);
-
cJSON_AddStringToObject(fldd_1, "Channel 1", sp.fsul.channel_1);
-
cJSON_AddStringToObject(fldd_1, "Global Time", sp.fsul.global_time);
-
cJSON_AddStringToObject(fldd_1, "Resolution", sp.fsul.resolution);
-
-
#endif
-
-
char *out = cJSON_Print(root);
-
// 保存信息到文件
-
writeToFile("result.txt", out);
-
printf("Debug Info: %s", out);
-
free(out);
-
-
cJSON_Delete(root);
-
}
解析本地 txt 的函数:
-
void parseFile(char *filename)
-
{
-
FILE *f;
-
long len;
-
char *data;
-
-
f = fopen(filename, "rb");
-
fseek(f, 0, SEEK_END);
-
len = ftell(f);
-
fseek(f, 0, SEEK_SET);
-
data = (char *)malloc(len + 1);
-
fread(data, 1, len, f);
-
fclose(f);
-
-
//easyParse(data);
-
itemParse(data);
-
-
free(data);
-
}
-
void itemParse(char *text)
-
{
-
-
cJSON *json;
-
-
json = cJSON_Parse(text);
-
if (!json)
-
{
-
printf("Error before: [%s]\n", cJSON_GetErrorPtr());
-
}
-
else
-
{
-
int size_0 = cJSON_GetArraySize(json);
-
printf("Shader Program Nums = %d\n", size_0);
-
-
for (int i = 0; i < size_0; i++)
-
{
-
cJSON *pItem = cJSON_GetArrayItem(json, i);
-
if (NULL == pItem)
-
{
-
continue;
-
}
-
int size_1 = cJSON_GetArraySize(pItem);
-
//printf("Shader Program Item Nums = %d\n", size_1);
-
printf("===========================\n");
-
for (int j = 0; j < size_1; j++)
-
{
-
cJSON *ppItem = cJSON_GetArrayItem(pItem, j);
-
-
//printJSON(ppItem);
-
-
int size_2 = cJSON_GetArraySize(ppItem);
-
printf("Shader Program Item Size = %d\n", size_2);
-
//printf("---------------------------\n");
-
for (int k = 0; k < size_2; k++)
-
{
-
cJSON *pppItem = cJSON_GetArrayItem(ppItem, k);
-
-
printJSON(pppItem);
-
}
-
printf("===========================\n");
-
}
-
}
-
}
-
-
cJSON_Delete(json);
-
}
生成的 txt 内容为:
开启 MUL 宏的情况
[
[
{
"Vertex Shader Path": "../Shader/vs_0.glsl",
"Fragment Shader Path": "../Shader/fs_0.glsl"
},
{
"Channel 0": "iChannel0",
"Channel 1": "iChannel1",
"Global Time": "iGlobalTime",
"Resolution": "iResolution"
}
],
[
{
"Vertex Shader Path": "../Shader/vs_1.glsl",
"Fragment Shader Path": "../Shader/fs_1.glsl"
},
{
"Channel 0": "iChannel0",
"Channel 1": "iChannel1",
"Global Time": "iGlobalTime",
"Resolution": "iResolution"
}
]
]
或未开启 MUL 的情况
[
[
{
"Vertex Shader Path": "../Shader/vs_0.glsl",
"Fragment Shader Path": "../Shader/fs_0.glsl"
},
{
"Channel 0": "iChannel0",
"Channel 1": "iChannel1",
"Global Time": "iGlobalTime",
"Resolution": "iResolution"
}
]
]
相关阅读
使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/53312328
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)