From 3d18ddc8e4b607cadf9caecb37fb94872ae3b0f8 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 17 Nov 2017 17:18:33 +0000 Subject: [PATCH] Take into account previous #line directives when expanding @foreach shader macros --- components/shader/shadermanager.cpp | 18 ++++++++++++++++-- files/shaders/water_fragment.glsl | 2 -- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/components/shader/shadermanager.cpp b/components/shader/shadermanager.cpp index ea971586ab..59c81f40a6 100644 --- a/components/shader/shadermanager.cpp +++ b/components/shader/shadermanager.cpp @@ -123,8 +123,22 @@ namespace Shader std::string content = source.substr(contentStart, contentEnd - contentStart); size_t overallEnd = contentEnd + std::string("$endforeach").length(); - // This will be wrong if there are other #line directives, so that needs fixing - int lineNumber = std::count(source.begin(), source.begin() + overallEnd, '\n') + 2; + + size_t lineDirectivePosition = source.rfind("#line", overallEnd); + int lineNumber; + if (lineDirectivePosition != std::string::npos) + { + size_t lineNumberStart = lineDirectivePosition + std::string("#line ").length(); + size_t lineNumberEnd = source.find_first_not_of("0123456789", lineNumberStart); + std::string lineNumberString = source.substr(lineNumberStart, lineNumberEnd - lineNumberStart); + lineNumber = std::stoi(lineNumberString); + } + else + { + lineDirectivePosition = 0; + lineNumber = 2; + } + lineNumber += std::count(source.begin() + lineDirectivePosition, source.begin() + overallEnd, '\n'); std::string replacement = ""; for (std::vector::const_iterator element = listElements.cbegin(); element != listElements.cend(); element++) diff --git a/files/shaders/water_fragment.glsl b/files/shaders/water_fragment.glsl index ca09aa94be..fddd81eff2 100644 --- a/files/shaders/water_fragment.glsl +++ b/files/shaders/water_fragment.glsl @@ -1,7 +1,5 @@ #version 120 -//DUMPME - #define REFRACTION @refraction_enabled #define SHADOWS @shadows_enabled