cJSON 的简单应用——保存/解析 Shader 参数 【代码示例】

举报
ShaderJoy 发表于 2021/12/30 00:49:32 2021/12/30
【摘要】   自定义的 Shader 程序结构体: struct ShaderProgram{ struct Path { char *vertex_shader_path; char *fragment_shader_path; } path; struct FragmentS...

 

自定义的 Shader 程序结构体:


  
  1. struct ShaderProgram
  2. {
  3. struct Path
  4. {
  5. char *vertex_shader_path;
  6. char *fragment_shader_path;
  7. } path;
  8. struct FragmentShaderUniformLocation
  9. {
  10. char *channel_0;
  11. char *channel_1;
  12. char *global_time;
  13. char *resolution;
  14. } fsul;
  15. };


入口函数:


  
  1. int main()
  2. {
  3. #ifdef TOFILE
  4. createObjects();
  5. #else
  6. parseFile("result.txt");
  7. #endif
  8. return 0;
  9. }

 

创建 (Shader Program) JSON 对象并保存为 txt 的函数:


  
  1. void createObjects()
  2. {
  3. #if MUL
  4. ShaderProgram sp[2] =
  5. {
  6. {
  7. { "../Shader/vs_0.glsl", "../Shader/fs_0.glsl" },
  8. { "iChannel0", "iChannel1", "iGlobalTime", "iResolution" }
  9. }, // sp[0]
  10. {
  11. { "../Shader/vs_1.glsl", "../Shader/fs_1.glsl" },
  12. { "iChannel0", "iChannel1", "iGlobalTime", "iResolution" }
  13. } // sp[1]
  14. };
  15. #else
  16. ShaderProgram sp =
  17. {
  18. { "../Shader/vs_0.glsl", "../Shader/fs_0.glsl" },
  19. { "iChannel0", "iChannel1", "iGlobalTime", "iResolution" }
  20. };
  21. #endif
  22. cJSON *root = cJSON_CreateArray();
  23. cJSON *fld = NULL, *fldd_0 = NULL, *fldd_1;
  24. #if MUL
  25. for (int i = 0; i < 2; i++)
  26. {
  27. cJSON_AddItemToArray(root, fld = cJSON_CreateArray());
  28. cJSON_AddItemToArray(fld, fldd_0 = cJSON_CreateObject());
  29. cJSON_AddStringToObject(fldd_0, "Vertex Shader Path", sp[i].path.vertex_shader_path);
  30. cJSON_AddStringToObject(fldd_0, "Fragment Shader Path", sp[i].path.fragment_shader_path);
  31. cJSON_AddItemToArray(fld, fldd_1 = cJSON_CreateObject());
  32. cJSON_AddStringToObject(fldd_1, "Channel 0", sp[i].fsul.channel_0);
  33. cJSON_AddStringToObject(fldd_1, "Channel 1", sp[i].fsul.channel_1);
  34. cJSON_AddStringToObject(fldd_1, "Global Time", sp[i].fsul.global_time);
  35. cJSON_AddStringToObject(fldd_1, "Resolution", sp[i].fsul.resolution);
  36. }
  37. #else
  38. cJSON_AddItemToArray(root, fld = cJSON_CreateArray());
  39. cJSON_AddItemToArray(fld, fldd_0 = cJSON_CreateObject());
  40. cJSON_AddStringToObject(fldd_0, "Vertex Shader Path", sp.path.vertex_shader_path);
  41. cJSON_AddStringToObject(fldd_0, "Fragment Shader Path", sp.path.fragment_shader_path);
  42. cJSON_AddItemToArray(fld, fldd_1 = cJSON_CreateObject());
  43. cJSON_AddStringToObject(fldd_1, "Channel 0", sp.fsul.channel_0);
  44. cJSON_AddStringToObject(fldd_1, "Channel 1", sp.fsul.channel_1);
  45. cJSON_AddStringToObject(fldd_1, "Global Time", sp.fsul.global_time);
  46. cJSON_AddStringToObject(fldd_1, "Resolution", sp.fsul.resolution);
  47. #endif
  48. char *out = cJSON_Print(root);
  49. // 保存信息到文件
  50. writeToFile("result.txt", out);
  51. printf("Debug Info: %s", out);
  52. free(out);
  53. cJSON_Delete(root);
  54. }

 

解析本地 txt 的函数:


  
  1. void parseFile(char *filename)
  2. {
  3. FILE *f;
  4. long len;
  5. char *data;
  6. f = fopen(filename, "rb");
  7. fseek(f, 0, SEEK_END);
  8. len = ftell(f);
  9. fseek(f, 0, SEEK_SET);
  10. data = (char *)malloc(len + 1);
  11. fread(data, 1, len, f);
  12. fclose(f);
  13. //easyParse(data);
  14. itemParse(data);
  15. free(data);
  16. }

 


  
  1. void itemParse(char *text)
  2. {
  3. cJSON *json;
  4. json = cJSON_Parse(text);
  5. if (!json)
  6. {
  7. printf("Error before: [%s]\n", cJSON_GetErrorPtr());
  8. }
  9. else
  10. {
  11. int size_0 = cJSON_GetArraySize(json);
  12. printf("Shader Program Nums = %d\n", size_0);
  13. for (int i = 0; i < size_0; i++)
  14. {
  15. cJSON *pItem = cJSON_GetArrayItem(json, i);
  16. if (NULL == pItem)
  17. {
  18. continue;
  19. }
  20. int size_1 = cJSON_GetArraySize(pItem);
  21. //printf("Shader Program Item Nums = %d\n", size_1);
  22. printf("===========================\n");
  23. for (int j = 0; j < size_1; j++)
  24. {
  25. cJSON *ppItem = cJSON_GetArrayItem(pItem, j);
  26. //printJSON(ppItem);
  27. int size_2 = cJSON_GetArraySize(ppItem);
  28. printf("Shader Program Item Size = %d\n", size_2);
  29. //printf("---------------------------\n");
  30. for (int k = 0; k < size_2; k++)
  31. {
  32. cJSON *pppItem = cJSON_GetArrayItem(ppItem, k);
  33. printJSON(pppItem);
  34. }
  35. printf("===========================\n");
  36. }
  37. }
  38. }
  39. cJSON_Delete(json);
  40. }

生成的 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

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。