Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Dorth

Pages: [1]
1
Feature requests / Added compatibility to the sfx effects.
« on: April 10, 2009, 12:20:23 am »
Hi there. New user, like the sfml a lot. I had to implement a small patch to keep compatibility with glsl shaders I wanted to use. It keeps backward compatibility while adding compatibility with most fragment shaders and a tad more optimization (removing empty lines or commented lines if the // is at the beginning of the line.)

Code: [Select]

Index: src/SFML/Graphics/PostFX.cpp
===================================================================
--- src/SFML/Graphics/PostFX.cpp (revision 1072)
+++ src/SFML/Graphics/PostFX.cpp (working copy)
@@ -339,7 +339,7 @@
 
     // Variable declarations
     std::string Line;
-    while (std::getline(File, Line) && (Line.substr(0, 6) != "effect"))
+    while (std::getline(File, Line) && (Line.substr(0, 6) != "effect") && (Line.substr(0,11) != "void main()"))
     {
         // Remove the ending '\r', if any
         if (!Line.empty() && (Line[Line.size() - 1] == '\r'))
@@ -349,10 +349,16 @@
         if (Line == "")
             continue;
 
+ // Skip comments
+ if (Line.substr(0,2) == "//")
+ {
+ continue;
+ }
+
         // Extract variables type and name and convert them
         std::string Type, Name;
         std::istringstream iss(Line);
-        if (!(iss >> Type >> Name))
+        if (!(iss >> Type) || ((Name = (iss.str()).substr(Type.size())) == ""))
         {
             std::cerr << "Post-effect error : invalid declaration (should be \"[type][name]\")" << std::endl
                       << "> " << Line << std::endl;
@@ -371,8 +377,16 @@
             Out += "uniform sampler2D " + Name + ";\n";
             myTextures.insert(Name);
         }
-        else
+        else if (Type == "uniform")
         {
+ Out += Type + " " + Name + ";\n";
+ if (Name.substr(0, 9) != "sampler2D")
+ {
+ myTextures.insert(Name.substr(10));
+ }
+        }
+ else
+        {
             // Other types are just copied to output with "uniform" prefix
             Out += "uniform " + Type + " " + Name + ";\n";
         }
@@ -382,21 +396,32 @@
     Out += "void main()\n";
     while (std::getline(File, Line))
     {
+ if (Line == "" || Line.substr(0,2) == "//")
+ {
+ continue;
+ }
+
         // Replace any texture lookup "T(" by "texture2D(T, "
         for (std::set<std::string>::const_iterator i = myTextures.begin(); i != myTextures.end(); ++i)
         {
-            std::string::size_type Pos = Line.find(*i);
+            std::string::size_type Pos = Line.find(*i + "(");
             if (Pos != std::string::npos)
+ {
                 Line.replace(Pos, i->size() + 1, "texture2D(" + *i + ", ");
+ }
         }
 
         // Replace "_in" by "gl_TexCoord[0].xy"
         for (std::string::size_type Pos = Line.find("_in"); Pos != std::string::npos; Pos = Line.find("_in"))
+ {
             Line.replace(Pos, 3, "gl_TexCoord[0].xy");
+ }
 
         // Replace "_out" by "gl_FragColor"
         for (std::string::size_type Pos = Line.find("_out"); Pos != std::string::npos; Pos = Line.find("_out"))
+ {
             Line.replace(Pos, 4, "gl_FragColor");
+ }
 
         // Write modified line to output string
         Out += Line + "\n";

Pages: [1]