Compile with MinGW and install FTGL lib to render text in OpenGL - c++

Context
I'm making a game engine using SDL and OpenGL. I'm trying to find the best way to output text to the screen with fancy true type font and I came across FTGL. (If you have better/easier solution, feel free to share it.)
Compiling FTGL
It requires FreeType2 in order to work so I got it, compiled it (using Code::Block) and then I compiled the FTGL lib using Code::Block too.
I'm using Eclipse Indigo with the CDT plugin. I'm not sure which lib to take, since there are a .lib and a .dll file. I tried both, but this doesn't seem to change my problem at all, which curently is on this line:
FTfont* m_Font = new FTTextureFont(filename.c_str());
The error is:
The type 'FTTextureFont' must implement the inherited pure virtual method 'FTFont::MakeGlyph'
It doesn't change if I use another FTfont subclass, e.g. FTPixmapFont constructor.
Here are the linker flags which I tried:
-lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_net -lSDL_ttf -lftgl -lfreetype234_D -lopengl32 -lglu32
-lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_net -lSDL_ttf -lftgl -llibfreetype -lopengl32 -lglu32
I'm using eclipse with MinGW.
I have copied and pasted
the src/FTGL folder and
the content of the Include folder of the freetype library
to the include folder of the MinGW directory.
Both freetype2 (there a are freetype.lib, freetype248_D.lib and freetype-bcc.lib, not sure wich to link in the project properties) and ftgl (ftgl.dll, ftgl_D.lib, ftgl_static_D.lib) lib files into the corresponding lib folder of the MinGW directory.
If I click on the line that has the error and uses the Open Declaration option, eclipse takes me to the FTTextureFont header file which has FTTextureFont(char const*) defined and the MakeGlyph function defined too.
You can find the full code on the GetRandomGame project page if you want to see more, or as a reference.
Here's the Compiler version
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.1/configure --enable-languages=c,c++,fortran,objc,ob
j-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp -
-disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runti
me-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.1 (GCC)
EDIT
Similar question: FTGL undefined references everywere?, but the answer of that question doesn't solve my problem, I still get these two errors.
EDIT 2
I was compiling using MSVC when I first tried to use FTGL and had two errors.
I finally used code::block to import the MSVC project provided with both libraries. One of the two errors got solved! But, I still got this one:
The type 'FTTextureFont' must implement the inherited pure virtual method 'FTFont::MakeGlyph'
EDIT 3
I've tried to compile FTGL 2.1.2 with freetype 2.3.4 and that hasn't solved my problem. I've tried FTGL 2.1.3 rc5 with freetype 2.3.4, same thing. Just to be sure, I've also tried freetype 2.3.5 and 2.4.8 (the first time was already 2.4.8).
I've tried moving the MakeGlyph code into the header file, but that hasn't solved the problem.
Here are some useful links i've found, but they haven't solved the problem yet.
OpenGL font rendering with FTGL
Glut and FTGL
EDIT 4
When I remove the ftgl linker flag, the compiler complains about this:
undefined reference to FTPixmapFont::FTPixmapFont(char const*)'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
And I still get the pure virtual method MakeGlyph error.
With the ftgl linker flag, the only error I get is the "must implement the inherited Pure Virtual method MakeGlyph". The compiler builds the project fine (but crashes at start up):
-lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_net -lSDL_ttf -lfreetype -lftgl -lopengl32 -lglu32
Build complete for project GetRandomGame
So I'm thinking that the FTGL lib isn't compiled correctly since it should have the FTPixmapFont.cpp defining the pure virtual method MakeGlyph. I'm kind of new with all those lib things in c++ and I might be doing something wrong building the lib.
EDIT 5
I have read and seen in an example that I needed to have a config.h file, so I added a properly configured config.h file that comes with FTGL.
Then I found the following code in an example provided in the ftgl directory and I added that to my Font class.
class FTHaloFont : public FTFont
{
public:
FTHaloFont(char const *fontFilePath) : FTFont(fontFilePath) {}
protected:
virtual FTGlyph* MakeGlyph(FT_GlyphSlot slot)
{
return new FTHaloGlyph(slot);
}
};
Even if I define MakeGlyph there, Eclipse complains about an unimplemented pure virtual method FTFont::MakeGlyph.
EDIT 6
I've tried debugging from command line with gdb and it tells me that freetype6.dll can't be found. I'm trying right now to find a solution.

The compiler is telling you that the method FTTextureFont::MakeGlyph has not been written.
You have to either:
Inherit from FTTextureFont and write the MakeGlyph method.
Find a child class of FTTextureFont that does what you want.
According to the FTGL docs, any class derived from FTFont (which FTTextureFont has as a parent) has to implement MakeGlyph.
I'd look for code on the 'net to see what others have done were I you.

You're using wrong class.
The correct class to use is FTGLPixmapFont.
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <common/timer.h>
#include <common/keyboard.h>
#include <FTGL/FTGL.h>
#include <FTGL/FTGLPixmapFont.h>
#include <wchar.h>
#include <math.h>
using Keyboard::keyPressed;
static Timer timer;
static int scrWidth = 640;
static int scrHeight = 480;
int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
Keyboard::init();
timer.reset();
timer.setMaxFps(100);
timer.setMinFps(5);
if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
SDL_Quit();
return -1;
}
glewInit();
FTGLPixmapFont font("DejaVuSans.ttf");
font.CharMap(ft_encoding_unicode);
font.FaceSize(10);
SDL_ShowCursor(SDL_DISABLE);
glClearColor(0.2, 0.2, 0.2, 0.2);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
bool running = true;
while (running){
SDL_Event event;
if (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
};
}
timer.update();
Keyboard::update();
if (keyPressed(SDLK_ESCAPE))
running = false;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-scrWidth/2.0f, scrWidth/2.0f, scrHeight/2.0f, -scrHeight/2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
wchar_t buf[256];
swprintf(buf, 256, L"fps: %0.2f", timer.getFps());
glRasterPos2i(10-scrWidth/2, 20-scrHeight/2);
font.Render(buf);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
Also, read the tutorial.
Judging by my source code, the version I'm using is 2.1.2 (since 2.1.3 is rc5 which is "release candidate", which is not "stable"). It looks like you're trying to use 2.1.3 - rc5. Downgrade to 2.1.2 and try again. Also, remember to tell what library version you're using next time you run into problem like this.
Alternatively you could try to fix FTGL (which is pain, and project seems to be abandoned). Or you could try to make your own freetype2-based font renderer (which is also pain, but might pay off in the long run).

EDIT 6
I've tried debugging from command line with gdb and it tells me that freetype6.dll can't be found.
I have placed freetype6.dll besides my executable file in the debug folder and it worked.
These are things that have slowed me or that I have learn while searching a solution:
Compiling with the same Compiler that I'm using
Learn to use code::block to import Visual Studio project
Actually be able to compile libraries in code::block without any warnings
Playing with linker flag (remove one and see what's going on) to help solve error and find problems
Learn to read provided example to find files that are necessary
And use gdb from command line
I got another problem while renaming some file of the project and I had to reconfigure my whole project in Eclipse (literally delete all files and checkout the SVN again). After that, the pure virtual error had disappeared! So I tried to debug the project and I got this error:
gdb: unknown target exception 0xc0000135 at 0x7724f52f
Eclipse wasn't helping me that much, so I tried the command line debugger and it told me that freetype6.dll was missing. And that was it!

Related

"Undefined symbols for architecture x86_64" In SDL 2 (macOS)

Basically, I was trying to make my first SDL2 game in C but encountered the same error every time. Undefined symbols for architecture x86_64 Where i can't call simple functions like the SDL_CreateWindow from SDL. I thought something like this would happen because I have a very limited understanding of C and C++ as I only just learnt the language and concepts.
Originally, I downloaded the SDL 2 source code (SDL2.framework and put it into) the /Library/Frameworks file. Then I simply tried to include SDL2 by using the path #include </Library/Frameworks/SDL2.framework/Headers/SDL.h> to absolutely no avail.
I have tried looking up solutions but they all seem to be tutorials of EXACTLY what I have done or just answers for Windows users (I could rant about this).
I followed tutorials on the SDL website but I didn't understand them very well (I am new to shell and only 13) and most of the commands didn't work.
I tried troubleshooting the problem myself by looking inside of the SDL2 files and found these functions.
Please help, I have been searching for a while. Ideally I would just want a clear explanation (for newbies) on how to setup SDL2 so that I can compile my program with SDL2. This is what I tested:
#include <stdio.h>
#include "/Users/christianlincoln/Documents/programs/c/SDL2.framework/Versions/A/Headers/SDL.h"
int main(int argc, char * argv[]) {
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"Game", // window title
0, // initial x position
0, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
printf("Hello!\n");
return 0;
}
Sorry for wording my question wrong or being outright incompetent.
I think I figured it out, the problem was I also needed to compile the source libraries but I had no idea how to do this and how to ensure that gcc would link everything together.
All the other tutorials were just telling me to put the framework in /Library/Frameworks which was wrong.
Turns out I had to mess around with gcc for a while after compiling:
gcc game.c -I/usr/local/include/SDL2 -lSDL2
Although I am still unsure of how to do this with things like OpenGL.

sdl, sdl2 error: SDL_window (among others) not declared

I have played around with C++ for a while and just recently started to get into SDL and SDL2.
I was able to get the dot demo program to work.
But other programs, such as Lazy Foo' Productions's copied and pasted don't seem to work.
I have both SDL and SDL2 installed (and uninstalled and reinstalled.) I am on Ubuntu 15.04 and I have the IDE CodeBlocks linked (-ISDL2)
The errors are SDL_Window - SDL_WINDOWPOS_UNDEFINED - SDL_WINDOW_SHOWN - SDL_CreateWindow - SDL_GetWindowSurface - SDL_UpdateWindowSurface and finally, SDL_DestroyWindow -- was not declared in this scope.
Also, I include:
#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
I'm pretty sure that I don't need all of that location, but it didn't work without it either. One other note, when I type the #includes, CodeBlocks will suggest SDL2/SDL.h but not SDL/SDL.h.
What am I missing?
I don't think I can put Lazy Foo' code here - I didn't get permission...
The code you listed;
#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
Why don't you change it to
#include <SDL2/SDL.h>
#include <stdio.h>
as the first header is where SDL_CreateWindow and other SDL2 functions are declared?
Also you don't need to include both SDL and SDL2 headers. Indeed that could very well be the source of your problem as you would only need to include the version you're using.
If you're following the tutorials from lazyfoo's site, you can check if the ones you're following are using SDL1.2 or SDL2 from their table of contents, as the site actually have the tutorials for both versions.
UPDATE:
I didn't notice that your platform is a Linux platform. Then it is so much easier to solve your problem. The demo that you followed previously was done using SDL-1.2, whereas the gcc error hinted that you're using SDL-2.0, hence SDL_CreateWindow and other undefined errors. You should install SDL-2.0 library and SDL-2.0 development files (which will provide you with the necessary SDL-2.0 headers). You may refer this to SDL-2.0 packages provided by your platform distribution.
As for the compilation, it'll be the same as the tutorial you've followed with a minor change, instead of gcc sdltest.o -lSDL -o sdltest, you'll issue gcc sdltest.o -lSDL2 -o sdltest to indicate that you're linking your code against SDL2 library.
EDIT
A simple SDL program to test your environment. You can use any of the simpler text editor such as nano or gedit or others to edit this, and run the compilation command above to test your setup.
The simplest way to do this is by copy the code, then from your terminal, issue cat > sdltest.cpp and paste the code, then hit [ENTER] and [CTRL-C] to end it. Then you can issue the compilation command as mentioned previously,g++ sdltest.cpp -lSDL2 -o sdltest.
Code;
#include <SDL2/SDL.h>
#include <stdio.h>
int main()
{
SDL_Window *p;
SDL_Renderer *w;
p = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,800,640,SDL_WINDOW_SHOWN);
w = SDL_CreateRenderer(p, -1, 0);
SDL_RenderClear(w);
SDL_SetRenderDrawColor(w,255,0,0,255);
SDL_Rect Rect = {220,140,200,200};
SDL_RenderFillRect(w,&Rect);
SDL_RenderPresent(w);
SDL_Delay(3000);
SDL_DestroyRenderer(w);
SDL_DestroyWindow(p);
SDL_Quit();
return 0;
}
Hope that helps.

how do i get rid of these compiler errors in glu.h?

trying to use this tutorial on 64-bit windows 8 with netbeans and cygwin 4.8.1.
i get many errors like this: /usr/include/w32api/GL/glu.h:68:79: error: expected ‘)’ before ‘*’ token.
on statements like this: void APIENTRY gluQuadricCallback(GLUquadric *qobj,GLenum which,void (CALLBACK *fn)());
the pointer on error message points to the * before the fn().
edit: including windef.h gets rid of the compiler error messages.
i am left with a bunch of undefined references like: glfwInit
edit2: using André Fischer's ideas, i can get a clean compile (you need to add the directory and a -l option for the linker).
i now have a: skipping incompatible ../../../../../Windows/SysWOW64/opengl32.dll when searching for -lopengl32 and: undefined reference to `_imp_vsnprintf'. so it looks like i have a 32/64 bit problems and an undefined external.
there must be a saner way to get opengl working on windows.
I assume you mean Tutorial 1: Opening a Window and are using Netbeans' builtin build system instead of CMake.
The order in which you include the header files is important (source). Try it like this:
#include <windef.h> // According to comments above
#include <GL/glew.h> // Before any gl headers
#include <GL/gl.h>
//#include <GL/glext.h> // Linux headers
//#include <GL/wglext.h> // Windows headers - Not sure which ones cygwin needs. Just try it
#include <GL/glu.h> // Always after gl.h
#include <GL/glfw.h> // When all gl-headers have been included
Create a directory named "include" in your project directory with a subfolder "GL".
Grab the binaries (32 bit, MinGW) from the GLFW Download Site and put the .dll/.so into your build-folder (Or extract them somewhere and add them to the search directories) and the header files into "include/GL".
Also the glfw code in the tutorial is slightly outdated; It does not work with glfw3 anymore.
You'll have to update it using GLFW's conversion guide/try this version (which I haven't been able to test, since I'm currently not at home) or use glfw2.
Finally download the GLEW sources and build it by following the instructions in the README.txt. Put the .dll/.so into your build-folder (or add to search directories) and the header files into "include/GL".
Add following to your Compiler-Flags:
-Iinclude/
Finally add following arguments to your Linker:
-L/lib -lglu32 -lopengl32 -lGL -lGLU -lglfw -lglew
You should be able to compile the tutorial now.
Edit: Added instructions for building GLEW, GLFW and completed my answer to include building everything from scratch.
Edit2: Linked glfw3-version of the tutorial-code.
Edit3: Added missing linker options.

Compiling Glew and SDL in C++ with Code::Blocks

I've spent the last 24 work hours trying to get this to compile, using multiple searches and tutorials (Some say I need to link it dynamically while others say I need to statically), I can not get this to work. I'm relatively new to compiling and linking so any help would be appreciated.
Here is my code(As you can tell by my comments I was very tired last night with coding this):
#define NO_SDL_GLEXT
#define GLEW_STATIC
#include "GL/glew.h"
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
int main (int argc, char* args[]){
//Loads the SDL video module
SDL_Init(SDL_INIT_VIDEO);
//Creates the SDL Surface for OpenGL to draw to
SDL_Surface* surface = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL );
//Sets the caption...
SDL_WM_SetCaption("OpenGL",0);
glewExperimental = GL_TRUE;
glewInit();
//Main event loop, this is the BREAD AND BUTTER LADIES AND GENTLEMEN
SDL_Event windowEvent;
while (true){
if (SDL_PollEvent(&windowEvent)){
//If you close the appplication, causes it to actually stop running :D
if (windowEvent.type == SDL_QUIT) break;
//Close it with the ESC key! :D:D:D:D:D:D:D:D:D:
if (windowEvent.type == SDL_KEYUP && windowEvent.key.keysym.sym == SDLK_ESCAPE) break;
}
//Literally the one extra line of code needed to do double buffering
SDL_GL_SwapBuffers();
}
//I wish I knew what this ended...LOPE JK
SDL_Quit();
return 0;
}
And in the search directories I have:
Compiler:
E:\Programs\CodeBlocks\glew-1.10.0\include
E:\Programs\CodeBlocks\SDL-1.2.15\include
Linker:
E:\Programs\CodeBlocks\glew-1.10.0\lib\Release\x64
E:\Programs\CodeBlocks\SDL-1.2.15\lib
And finally in the Linker settings I have
Link libraries:
E:\Programs\CodeBlocks\glew-1.10.0\lib\Release\x64\glew32s.lib
Other linker options:
-lmingw32 -lglew32s -DGLEW_STATIC -lSDLmain -lSDL
The error I am currently getting is:
In function 'SDL_main':
undefined reference to `glewExperimental'
undefined reference to `glewInit#0'
Thank you in advance for all the help!
You are getting linker errors. It looks like you are compiling your application using the Mingw compiler. But, are you sure the glew library you are using was also built with/for Mingw? If you didn't build glew yourself and downloaded a prebuilt binary, it was most probably built using Visual Studio. Note that its not straightforward to mix libraries from different compilers like this.
Look at this:
http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
P.S: Unless philosophical reasons against proprietary software is preventing you from doing so, you really ought to consider using Visual Studio compilers. The express editions of Visual Studio compilers are free (beer-like). You can use the Visual Studio compiler in really great IDEs like QtCreator.

sfml sf::Image() class issues

So I'm using the sfml frameworks in an XCode project I'm building and I'm having some trouble with the sf::Image() class.
Some of the functions work perfectly fine and as expected, but others give compiler errors.
For example, I can do the following with no errors and it'll print the correct result:
sf::Image *image = new sf::Image(600,600);
int width = image->GetWidth();
int height = image->GetHeight();
std::cout<<width<<" "<<height<<std::endl;
So I know the library is being at least somewhat integrated. However, functions like SaveToFile() and LoadFromFile() give me compiler errors. For example, if I do the following I get an "using undefined symbols for this architecture" error.
std::string filename = "myfile.jpg";
sf::Image *image = new sf::Image(600,600);
image->SaveToFile(filename);
I appreciate any and all help!
Are the SFML dll besides your EXE in your Debug directory?
In my own project, there's 3 dll from SFML which are:
sfml-graphics-2.dll
sfml-system-2.dll
sfml-window-2.dll
Which can be found in the SFML Bin directory. Also, don't forget add the SFML > lib dir to the Library Search Path of your project. Again, don't forget to use the appropriate lib:
sfml-graphics
sfml-window
sfml-system
opengl32