Browse Source

slop no longer needs files installed to function, it can still load custom shaders from ~/.config/slop though

naelstrof 7 years ago
parent
commit
5775c18e1c

+ 4
- 15
CMakeLists.txt View File

1
 #Change this if you need to target a specific CMake version
1
 #Change this if you need to target a specific CMake version
2
 cmake_minimum_required(VERSION 3.1)
2
 cmake_minimum_required(VERSION 3.1)
3
 
3
 
4
-add_definitions(-DSLOP_VERSION="v5.3.21")
5
-
6
-if(NOT CMAKE_BUILD_TYPE)
7
-  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
8
-endif()
9
-
10
-if( NOT CMAKE_INSTALL_PREFIX )
11
-    set( CMAKE_INSTALL_PREFIX "/usr" )
12
-endif()
13
-
14
-if( NOT SHADER_PREFIX )
15
-    set( SHADER_PREFIX "${CMAKE_INSTALL_PREFIX}" CACHE PATH "The path where slop thinks it resides in, so that it can find the shaders. This is not necessarily the CMAKE_INSTALL_PREFIX. For example if the shaders will exist in /usr/share/slop, SHADER_PREFIX should be \"/usr\"." )
4
+set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)")
5
+if ( NOT CMAKE_INSTALL_PREFIX )
6
+    set(CMAKE_INSTALL_PREFIX "/usr")
16
 endif()
7
 endif()
17
 
8
 
18
 project(slop)
9
 project(slop)
10
+add_definitions(-DSLOP_VERSION="v5.3.21")
19
 
11
 
20
 set(CMAKE_CXX_STANDARD 14)
12
 set(CMAKE_CXX_STANDARD 14)
21
 set(CMAKE_CXX_STANDARD_REQUIRED on)
13
 set(CMAKE_CXX_STANDARD_REQUIRED on)
66
 
58
 
67
 # Install targets
59
 # Install targets
68
 install( TARGETS ${EXECUTABLE_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
60
 install( TARGETS ${EXECUTABLE_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
69
-install( DIRECTORY "${CMAKE_SOURCE_DIR}/share" DESTINATION "${CMAKE_INSTALL_PREFIX}" )
70
-
71
-add_definitions(-DSHADER_PREFIX="${SHADER_PREFIX}")

+ 0
- 9
share/slop/simple.frag View File

1
-#version 130
2
-
3
-uniform vec4 color;
4
-out vec4 outColor;
5
-
6
-void main()
7
-{
8
-    outColor = color;
9
-}

+ 0
- 9
share/slop/simple.vert View File

1
-#version 130
2
-
3
-in vec2 position;
4
-uniform mat4 projection;
5
-
6
-void main()
7
-{
8
-    gl_Position = projection*vec4(position,0,1);
9
-}

+ 0
- 13
share/slop/textured.frag View File

1
-#version 130
2
-
3
-uniform sampler2D texture;
4
-
5
-varying vec2 uvCoord;
6
-
7
-out vec4 outColor;
8
-
9
-void main()
10
-{
11
-    outColor = texture2D( texture, uvCoord );
12
-}
13
-

+ 0
- 12
share/slop/textured.vert View File

1
-#version 130
2
-
3
-attribute vec2 position;
4
-attribute vec2 uv;
5
-
6
-varying vec2 uvCoord;
7
-
8
-void main()
9
-{
10
-    uvCoord = uv;
11
-    gl_Position = vec4(position,0,1);
12
-}

+ 3
- 1
src/framebuffer.cpp View File

1
 #include "framebuffer.hpp"
1
 #include "framebuffer.hpp"
2
 
2
 
3
 Framebuffer::Framebuffer( int w, int h ) {
3
 Framebuffer::Framebuffer( int w, int h ) {
4
-    shader = new Shader( "textured.vert", "textured.frag" );
4
+    std::string vert = "#version 130\nattribute vec2 position;\nattribute vec2 uv;\nvarying vec2 uvCoord;\nvoid main()\n{\nuvCoord = uv;\ngl_Position = vec4(position,0,1);\n}\n";
5
+    std::string frag = "#version 130\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nout vec4 outColor;void main()\n{\noutColor = texture2D( texture, uvCoord );\n}\n";
6
+    shader = new Shader( vert, frag, false );
5
     glGenFramebuffers( 1, &fbuffer );
7
     glGenFramebuffers( 1, &fbuffer );
6
     glBindFramebuffer( GL_FRAMEBUFFER, fbuffer );
8
     glBindFramebuffer( GL_FRAMEBUFFER, fbuffer );
7
     glGenTextures(1, &image);
9
     glGenTextures(1, &image);

+ 1
- 0
src/main.cpp View File

31
     glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
31
     glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
32
     options.getColor("color", 'c', color);
32
     options.getColor("color", 'c', color);
33
     options.getString( "xdisplay", 'x', foo->xdisplay );
33
     options.getString( "xdisplay", 'x', foo->xdisplay );
34
+    options.getString( "shader", 's', foo->shader );
34
     foo->r = color.r;
35
     foo->r = color.r;
35
     foo->g = color.g;
36
     foo->g = color.g;
36
     foo->b = color.b;
37
     foo->b = color.b;

+ 4
- 1
src/rectangle.cpp View File

21
     our = ur + glm::vec2(border,border);
21
     our = ur + glm::vec2(border,border);
22
     obr = br + glm::vec2(border,-border);
22
     obr = br + glm::vec2(border,-border);
23
     generateBuffers();
23
     generateBuffers();
24
-    shader = new Shader( "simple.vert", "simple.frag" );
24
+    std::string vert = "#version 130\nin vec2 position;\nuniform mat4 projection;\nvoid main() {\ngl_Position = projection*vec4(position,0,1);\n }";
25
+    std::string frag = "#version 130\nuniform vec4 color;\nout vec4 outColor;\nvoid main() {\noutColor = color;\n}";
26
+
27
+    shader = new Shader( vert, frag, false );
25
 }
28
 }
26
 
29
 
27
 void Rectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {
30
 void Rectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {

+ 1
- 9
src/resource.cpp View File

28
         char* home = getpwuid(getuid())->pw_dir;
28
         char* home = getpwuid(getuid())->pw_dir;
29
         usrconfig += home;
29
         usrconfig += home;
30
         usrconfig += "/.config/slop/";
30
         usrconfig += "/.config/slop/";
31
-        // SHADER_PREFIX is defined by the user within CMake, defaults to `/usr`.
32
-        sysconfig = SHADER_PREFIX;
33
-        sysconfig += "/share/slop/";
34
         return;
31
         return;
35
     }
32
     }
36
     usrconfig += config;
33
     usrconfig += config;
37
     usrconfig += "/slop/";
34
     usrconfig += "/slop/";
38
-    sysconfig = SHADER_PREFIX;
39
-    sysconfig += "/share/slop/";
40
 }
35
 }
41
 
36
 
42
 std::string Resource::getRealPath( std::string localpath ) {
37
 std::string Resource::getRealPath( std::string localpath ) {
43
     if ( validatePath( usrconfig + localpath ) ) {
38
     if ( validatePath( usrconfig + localpath ) ) {
44
         return usrconfig + localpath;
39
         return usrconfig + localpath;
45
     }
40
     }
46
-    if ( validatePath( sysconfig + localpath ) ) {
47
-        return sysconfig + localpath;
48
-    }
49
-    std::string err = "The file or folder " + localpath + " was not found in either " + sysconfig + " or " + usrconfig + "\n";
41
+    std::string err = "The file or folder " + localpath + " was not found in " + usrconfig + "\n";
50
     throw new std::runtime_error(err);
42
     throw new std::runtime_error(err);
51
     return localpath;
43
     return localpath;
52
 }
44
 }

+ 0
- 1
src/resource.hpp View File

40
 private:
40
 private:
41
     bool validatePath( std::string path );
41
     bool validatePath( std::string path );
42
     std::string usrconfig;
42
     std::string usrconfig;
43
-    std::string sysconfig;
44
 };
43
 };
45
 
44
 
46
 extern Resource* resource;
45
 extern Resource* resource;

+ 35
- 28
src/shader.cpp View File

1
 #include "shader.hpp"
1
 #include "shader.hpp"
2
 
2
 
3
-Shader::Shader( std::string vert, std::string frag) {
4
-    vert = resource->getRealPath(vert);
5
-    frag = resource->getRealPath(frag);
6
-    // Create the program to link to.
7
-    m_program = glCreateProgram();
8
-
9
-    std::ifstream v( vert.c_str() );
10
-    std::string vert_contents((std::istreambuf_iterator<char>(v)),
11
-                               std::istreambuf_iterator<char>());
12
-    std::ifstream f( frag.c_str() );
13
-    std::string frag_contents((std::istreambuf_iterator<char>(f)),
14
-                              std::istreambuf_iterator<char>());
3
+Shader::Shader( std::string vert, std::string frag, bool file ) {
4
+    std::string vert_contents;
5
+    std::string frag_contents;
6
+    if ( file ) {
7
+        vert = resource->getRealPath(vert);
8
+        frag = resource->getRealPath(frag);
9
+        std::ifstream v( vert.c_str() );
10
+        vert_contents = std::string((std::istreambuf_iterator<char>(v)),
11
+                                   std::istreambuf_iterator<char>());
12
+        std::ifstream f( frag.c_str() );
13
+        frag_contents = std::string((std::istreambuf_iterator<char>(f)),
14
+                                  std::istreambuf_iterator<char>());
15
+    } else {
16
+        vert_contents = vert;
17
+        frag_contents = frag;
18
+    }
15
 
19
 
16
     const char* vertsrc = vert_contents.c_str();
20
     const char* vertsrc = vert_contents.c_str();
17
     const char* fragsrc = frag_contents.c_str();
21
     const char* fragsrc = frag_contents.c_str();
22
+
23
+    // Create the program to link to.
24
+    program = glCreateProgram();
18
     
25
     
19
     if ( vert_contents.length() <= 0 ) {
26
     if ( vert_contents.length() <= 0 ) {
20
         std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
27
         std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
67
 }
74
 }
68
 
75
 
69
 Shader::~Shader() {
76
 Shader::~Shader() {
70
-    glDeleteProgram( m_program );
77
+    glDeleteProgram( program );
71
 }
78
 }
72
 
79
 
73
 unsigned int Shader::getProgram() {
80
 unsigned int Shader::getProgram() {
74
-    return m_program;
81
+    return program;
75
 }
82
 }
76
 
83
 
77
 void Shader::bind() {
84
 void Shader::bind() {
78
-    glUseProgram( m_program );
85
+    glUseProgram( program );
79
 }
86
 }
80
 
87
 
81
 int Shader::compile( unsigned int shader, std::string& error ) {
88
 int Shader::compile( unsigned int shader, std::string& error ) {
97
 }
104
 }
98
 
105
 
99
 int Shader::link( unsigned int vertshader, unsigned int fragshader, std::string& error ) {
106
 int Shader::link( unsigned int vertshader, unsigned int fragshader, std::string& error ) {
100
-    glAttachShader( m_program, vertshader );
101
-    glAttachShader( m_program, fragshader );
102
-    glLinkProgram( m_program );
107
+    glAttachShader( program, vertshader );
108
+    glAttachShader( program, fragshader );
109
+    glLinkProgram( program );
103
 
110
 
104
     // Linking the shader is the easy part, all this junk down here is for printing the error it might generate.
111
     // Linking the shader is the easy part, all this junk down here is for printing the error it might generate.
105
     int result = GL_FALSE;
112
     int result = GL_FALSE;
106
     int logLength;
113
     int logLength;
107
-    glGetProgramiv( m_program, GL_LINK_STATUS, &result);
108
-    glGetProgramiv( m_program, GL_INFO_LOG_LENGTH, &logLength);
114
+    glGetProgramiv( program, GL_LINK_STATUS, &result);
115
+    glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logLength);
109
     if ( result == GL_FALSE ) {
116
     if ( result == GL_FALSE ) {
110
         char* errormsg = new char[ logLength ];
117
         char* errormsg = new char[ logLength ];
111
-        glGetProgramInfoLog( m_program, logLength, NULL, errormsg );
118
+        glGetProgramInfoLog( program, logLength, NULL, errormsg );
112
         error = errormsg;
119
         error = errormsg;
113
         delete[] errormsg;
120
         delete[] errormsg;
114
         return 1;
121
         return 1;
117
 }
124
 }
118
 
125
 
119
 unsigned int Shader::getUniformLocation( std::string name ) {
126
 unsigned int Shader::getUniformLocation( std::string name ) {
120
-    glUseProgram( m_program );
121
-    return glGetUniformLocation( m_program, name.c_str() );
127
+    glUseProgram( program );
128
+    return glGetUniformLocation( program, name.c_str() );
122
 }
129
 }
123
 
130
 
124
 void Shader::setParameter( std::string name, int foo ) {
131
 void Shader::setParameter( std::string name, int foo ) {
142
 }
149
 }
143
 
150
 
144
 void Shader::setAttribute( std::string name, unsigned int buffer, unsigned int stepsize ) {
151
 void Shader::setAttribute( std::string name, unsigned int buffer, unsigned int stepsize ) {
145
-    unsigned int a = glGetAttribLocation( m_program, name.c_str() );
152
+    unsigned int a = glGetAttribLocation( program, name.c_str() );
146
     glEnableVertexAttribArray( a );
153
     glEnableVertexAttribArray( a );
147
     glBindBuffer( GL_ARRAY_BUFFER, buffer );
154
     glBindBuffer( GL_ARRAY_BUFFER, buffer );
148
     glVertexAttribPointer( a, stepsize, GL_FLOAT, GL_FALSE, 0, NULL );
155
     glVertexAttribPointer( a, stepsize, GL_FLOAT, GL_FALSE, 0, NULL );
149
-    m_activeattribs.push_back( a );
156
+    activeAttributes.push_back( a );
150
 }
157
 }
151
 
158
 
152
 void Shader::unbind() {
159
 void Shader::unbind() {
153
-    for ( unsigned int i=0; i<m_activeattribs.size(); i++ ) {
154
-        glDisableVertexAttribArray( m_activeattribs[i] );
160
+    for ( unsigned int i=0; i<activeAttributes.size(); i++ ) {
161
+        glDisableVertexAttribArray( activeAttributes[i] );
155
     }
162
     }
156
-    m_activeattribs.clear();
163
+    activeAttributes.clear();
157
     glUseProgram( 0 );
164
     glUseProgram( 0 );
158
 }
165
 }

+ 5
- 3
src/shader.hpp View File

36
 
36
 
37
 class Shader {
37
 class Shader {
38
 public:
38
 public:
39
-    Shader( std::string vert, std::string frag );
39
+    Shader( std::string vert, std::string frag, bool file = true );
40
     ~Shader();
40
     ~Shader();
41
     unsigned int    getProgram();
41
     unsigned int    getProgram();
42
     void            bind();
42
     void            bind();
48
     void            setParameter( std::string name, glm::vec2 foo );
48
     void            setParameter( std::string name, glm::vec2 foo );
49
     void            setAttribute( std::string name, unsigned int buffer, unsigned int stepsize );
49
     void            setAttribute( std::string name, unsigned int buffer, unsigned int stepsize );
50
 private:
50
 private:
51
-    std::vector<unsigned int>   m_activeattribs;
51
+    void loadFromFile( std::string vert, std::string frag );
52
+    void loadFromMemory( std::string vert, std::string frag );
53
+    std::vector<unsigned int>   activeAttributes;
52
     unsigned int                getUniformLocation( std::string );
54
     unsigned int                getUniformLocation( std::string );
53
     int                         compile( unsigned int shader, std::string& error );
55
     int                         compile( unsigned int shader, std::string& error );
54
     int                         link( unsigned int vert, unsigned int frag, std::string& error );
56
     int                         link( unsigned int vert, unsigned int frag, std::string& error );
55
-    unsigned int                m_program;
57
+    unsigned int                program;
56
 };
58
 };
57
 
59
 
58
 #endif // N_SHADER_H_
60
 #endif // N_SHADER_H_

+ 3
- 1
src/slop.cpp View File

84
     // Set up window with GL context
84
     // Set up window with GL context
85
     SlopWindow* window = new SlopWindow();
85
     SlopWindow* window = new SlopWindow();
86
 
86
 
87
-    window->framebuffer->setShader( options->shader );
87
+    if ( options->shader != "textured" ) {
88
+        window->framebuffer->setShader( options->shader );
89
+    }
88
 
90
 
89
     // Init our little state machine, memory is a tad of a misnomer
91
     // Init our little state machine, memory is a tad of a misnomer
90
     SlopMemory memory( options );
92
     SlopMemory memory( options );