Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm writing a class called TextRenderer to render text with OpenGL.

Here's my TextRenderer.hpp file

class TextRenderer
{
public:
    void renderText(std::string text, int x, int y);
private:
    void setup();
    GLuint program;
};

Here's my TextRenderer.cpp file:

#include "TextRenderer.hpp"
#include <string>
#include <GL/glew.h>

void TextRenderer::setup()
{

}

Here's my main file:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "TextRenderer.hpp"

int main()
{
    //Init GLFW
    glfwInit();
    //Set OpenGL version to 3.2
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    //Make window un-resizable
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    //Create GLFW window in fullscreen mode
    GLFWwindow* window = glfwCreateWindow(800, 600, "MOBA", glfwGetPrimaryMonitor(), nullptr);
    glfwMakeContextCurrent(window);

    //Init FreeType
    FT_Library ft;
    if (FT_Init_FreeType(&ft)) {
        fprintf(stderr, "FATAL: Could not init FreeType");
        return 1;
    }
    //Init Arial FreeType Face
    FT_Face arial;
    if (FT_New_Face(ft, "Arial", 0, &arial))
    {
        fprintf(stderr, "FATAL: Could not init font \"Arial\"");
        return 1;
    }

    //Main loop
    while (!glfwWindowShouldClose(window))
    {
        glfwSwapBuffers(window);
        glfwPollEvents();

        //Close on Esc key press
        //TODO: Bring up quit dialog
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        {
            glfwSetWindowShouldClose(window, GL_TRUE);
        }
    }
    //Terminate GLFW
    glfwTerminate();
    return 0;
}

Notice how I have a private GLuint, program. To do this I have to #include <GL/glew.h> (in TextRenderer.cpp), and I already have GL/glew.h included in my main.cpp file. What's the best way to do this?

share|improve this question

closed as off-topic by glampert, vnp, Edward, syb0rg, Nick Udell Jan 6 at 10:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Your header file should have some includes as well, right? –  rolfl Jan 4 at 21:24
1  
@rolfl The header file includes are in TextRenderer.cpp. As I've always been told, you're supposed to put the includes in the cpp file. Is this incorrect? –  Thomas Ross Jan 4 at 21:26
2  
2  
@Th0masR0ss: I know, but that's still considered an answer invalidation. –  Jamal Jan 4 at 21:56
1  
"I'm writing a class called TextRenderer" - meaning your code is incomplete and thus off-topic. See the help center. I'm placing a close vote on this. Provide the code for your class methods and I'll withdraw the vote and possibly provide you a code review. –  glampert Jan 5 at 3:20

1 Answer 1

up vote 3 down vote accepted

Most include files are written so that they are safe to be included multiple times. So, if your TextRenderer.h needs types from glew.h, you include it. Writing your own include files so that they include everything needed to use them (types, enums, ...) is usually a good thing.

You might write your TextRenderer.hpp something like this:

#ifndef TEXTRENDERER_HPP
#define TEXTRENDERER_HPP

#include <GL/glew.h> // For GLuint

class TextRenderer
{
    ...
}
#endif

The define TEXTRENDERER_HPP is there to make the header safe to be included multiple times. You may choose any unique identifier you like, but usually people use file names with some amounts of underscores at start and end.

share|improve this answer
    
Yes, I originally had the include guard, but I fail copy/pasted the file. –  Thomas Ross Jan 4 at 21:51
2  
Prefix underscore followed by a capitol letter is reserved for the implementation. It should be TEXTRENDERER_HPP. stackoverflow.com/a/228797/14065 –  Loki Astari Jan 5 at 1:51
1  
@LokiAstari, good point, edited my answer. Anyways, including types and such to header files usually saves lots of headache, and it's good to always guard header files. –  MaKo Jan 5 at 5:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.