glText icon indicating copy to clipboard operation
glText copied to clipboard

CMake sample for Ubuntu/Linux?

Open sbroberg opened this issue 3 years ago • 0 comments

I had some difficulty getting your simple.c to build & run on ubuntu; however, after some hacking I came up with these changes (I'm not sure how to push a branch up to your repository).

Major issues I had:

  • I don't know what "glad" is (I'm new to opengl, and it doesn't exist on ubuntu as an apt package); it seems unnecessary on ubuntu as I eventually got it to build without it. Is this something that's mostly used in Windows?
  • glBindFragDataLocation is not in any header that I can find (I tried GL/glcorearb.h, but the declaration is behind an #ifdef; setting the #ifdef doesn't seem to unlock the declaration, so there's something I'm missing). The project still links (with a warning), so I'm not sure what it does. Commenting out the call to glBindFragDataLocation in gltext.h doesn't seem to affect the behavior of program.
  • Your README implies that only one of the files that include gltext.h needs to define GLT_IMPLEMENTATION, but since this results in the functions having private (static) linkage, from gltext.h:92, that means functions defined will only be available to functions in that compilation unit; therefore you need to set GLT_IMPLEMENTATION in every file that includes gltext.h, making the define unnecessary. It seems to me that you want the chack on line 86 to be for the existence of GLT_IMPLEMENTATION, and to have GLT_API be defined to nothing in this case (so that you get public linkage). At least, you need to do this if you want to have gltext.h turn into a libgltext.a that can be used by multiple files in your project.
4 files changed, 36 insertions(+), 13 deletions(-)
CMakeLists.txt    | 19 +++++++++++++++++++
examples/gltext.c |  7 +++++++
examples/simple.c | 15 ++++++---------
gltext.h          |  8 ++++----

new file   CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 3.12...3.18 FATAL_ERROR)
+project (Test)
+
+set(CMAKE_C_STANDARD 99)
+
+find_package(OpenGL REQUIRED)
+find_package(glfw3 3.3 REQUIRED)
+
+set(CMAKE_EXPORT_COMPILE_COMMANDS Y)
+
+add_executable (Test examples/simple.c examples/gltext.c)
+target_include_directories(Test PUBLIC .)
+
+target_link_libraries(Test
+  PUBLIC
+  m
+  glfw
+  OpenGL::GL
+)
new file   examples/gltext.c
@@ -0,0 +1,7 @@
+// For building implementations for gltext functions
+#include <GL/gl.h>
+
+#include <GLES3/gl3.h>
+
+#define GLT_IMPLEMENTATION
+#include "gltext.h"
modified   examples/simple.c
@@ -11,13 +11,10 @@
 
 #include <math.h>
 
-#include <glad/glad.h> /* https://github.com/Dav1dde/glad */
 #include <GLFW/glfw3.h> /* https://github.com/glfw/glfw */
 
-#define GLT_IMPLEMENTATION
 #include "gltext.h" /* https://github.com/vallentin/glText */
 
-
 int main(int argc, char *argv[])
 {
 	if (!glfwInit())
@@ -52,12 +49,12 @@ int main(int argc, char *argv[])
 	glfwShowWindow(window);
 	glfwMakeContextCurrent(window);
 
-	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
-	{
-		fprintf(stderr, "Failed to load OpenGL functions and extensions\n");
-		glfwTerminate();
-		return EXIT_FAILURE;
-	}
+	/* if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) */
+	/* { */
+	/* 	fprintf(stderr, "Failed to load OpenGL functions and extensions\n"); */
+	/* 	glfwTerminate(); */
+	/* 	return EXIT_FAILURE; */
+	/* } */
 
 	glfwSwapInterval(1);
 
modified   gltext.h
@@ -83,13 +83,13 @@ extern "C" {
 #define GLT_NULL 0
 #define GLT_NULL_HANDLE 0
 
-#ifdef GLT_IMPORTS
+#ifndef GLT_IMPLEMENTATION
 #	define GLT_API extern
 #else
 #	ifndef GLT_STATIC
 #		define GLT_STATIC
 #	endif
-#	define GLT_API static
+#	define GLT_API
 #endif
 
 #define GLT_LEFT 0
@@ -956,7 +956,7 @@ GLT_API GLboolean _gltCreateText2DShader(void)
 	glBindAttribLocation(_gltText2DShader, _GLT_TEXT2D_POSITION_LOCATION, "position");
 	glBindAttribLocation(_gltText2DShader, _GLT_TEXT2D_TEXCOORD_LOCATION, "texCoord");
 
-	glBindFragDataLocation(_gltText2DShader, 0, "fragColor");
+	// glBindFragDataLocation(_gltText2DShader, 0, "fragColor");
 
 	glLinkProgram(_gltText2DShader);
 
@@ -1336,4 +1336,4 @@ GLT_API GLboolean _gltCreateText2DFontTexture(void)
 }
 #endif
 
-#endif
\ No newline at end of file
+#endif

sbroberg avatar Feb 03 '22 15:02 sbroberg