💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.4 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resources/resource-loader.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "resources/text-file.hpp"
  22. #include "gl/shader-template.hpp"
  23. #include <sstream>
  24. /**
  25. * Handles `#pragma include` directives by loading the specified text files and inserting them in place.
  26. */
  27. static void handle_includes(text_file* source, resource_manager* resource_manager)
  28. {
  29. // For each line in the source
  30. for (std::size_t i = 0; i < source->size(); ++i)
  31. {
  32. std::string token;
  33. std::istringstream line_stream((*source)[i]);
  34. // If line contains a `#pragma include` directive
  35. if (line_stream >> token && token == "#pragma" &&
  36. line_stream >> token && token == "include")
  37. {
  38. // If third token is enclosed in quotes or angled brackets
  39. if (line_stream >> token && token.size() > 2 &&
  40. ((token.front() == '\"' && token.back() == '\"') ||
  41. (token.front() == '<' && token.back() == '>')))
  42. {
  43. // Extract include path
  44. std::string path = token.substr(1, token.length() - 2);
  45. // Load include file
  46. if (!resource_manager->load<text_file>(path))
  47. {
  48. (*source)[i] = "#error file not found (" + path + ")";
  49. }
  50. else
  51. {
  52. // Create copy of include file
  53. text_file include_file = *(resource_manager->load<text_file>(path));
  54. // Handle `#pragma include` directives inside include file
  55. handle_includes(&include_file, resource_manager);
  56. // Replace #pragma include directive with include file contents
  57. source->erase(source->begin() + i);
  58. source->insert(source->begin() + i, include_file.begin(), include_file.end());
  59. i += include_file.size() - 1;
  60. }
  61. }
  62. else
  63. {
  64. (*source)[i] = "#error malformed include directive";
  65. }
  66. }
  67. }
  68. }
  69. template <>
  70. gl::shader_program* resource_loader<gl::shader_program>::load(resource_manager* resource_manager, PHYSFS_File* file)
  71. {
  72. // Load shader template source
  73. text_file source_lines = *resource_loader<text_file>::load(resource_manager, file);
  74. // Handle `#pragma include` directives
  75. handle_includes(&source_lines, resource_manager);
  76. // Join vector of source lines into single string
  77. std::ostringstream stream;
  78. std::copy(source_lines.begin(), source_lines.end(), std::ostream_iterator<std::string>(stream, "\n"));
  79. // Create shader template
  80. gl::shader_template* shader_template = new gl::shader_template(stream.str());
  81. // Build shader program
  82. gl::shader_program* program = shader_template->build(gl::shader_template::dictionary_type());
  83. // Check if shader program was linked successfully
  84. if (!program->was_linked())
  85. {
  86. throw std::runtime_error("Shader program linking failed: " + program->get_info_log());
  87. }
  88. // Destroy shader template
  89. delete shader_template;
  90. return program;
  91. }