SDL和OpenGL程序编译失败

时间:2012-03-14 04:47:41

标签: c++ opengl sdl

我正在尝试构建一个简单的openGL和SDL程序,它只是拒绝工作。

我正在使用最新的nvidia驱动程序(285。如果我记得的话),我使用mingw进行编译。我使用的机器运行Windows7。 我对mingw的了解有些限制,因此这可能是一个非常简单的链接错误。

使用的SDL版本是1.2(我应该升级到1.3吗?)

起初,一切正常,但我只是通过SDL打开一个opengl窗口。 这是原始makefile的代码

all:
g++ -o sdlGL.exe esg.cpp tiletest.cpp -lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32

这些是被称为

的两个函数
    void init_sdl(int width, int height) {
        //Start SDL
        if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
            fDebug << "Failed to init SDL" << std::endl;
            quit(1);
        }

        //Set SDL attrib.
        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_ALPHA_SIZE, 8 );
        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

        //Set SDL video mode
        if( SDL_SetVideoMode(width, height, 0, SDL_OPENGL) == 0 ) {
            fDebug << "Failed to set SDL video mode" << std::endl;
            quit(1);
        }
    }

    //Initializes OpenGL
    void init_opengl(int width, int height, bool alpha) {
        // Set the OpenGL state after creating the context with SDL_SetVideoMode
        glClearColor( 0, 0, 0, 0 );
        glClear(GL_COLOR_BUFFER_BIT);
        glEnable( GL_TEXTURE_2D ); // Need this to display a texture

        if(alpha) {
            glEnable (GL_BLEND);
            glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        }

        glViewport( 0, 0, width, height );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( 0, width, height, 0, -1, 1 );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    }

现在的内容包括:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

使用该代码,一切正常。 然后我添加了一些函数来处理着色器和着色器程序

    void CreateShader(GLenum eShaderType, const std::string &strShaderFile, std::vector<GLuint> &shaderList) {
        GLuint shader = glCreateShader(eShaderType);
        const char *strFileData = strShaderFile.c_str();
        glShaderSource(shader, 1, &strFileData, NULL);

        glCompileShader(shader);

        GLint status;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &status);


        shaderList.push_back(shader);
    }

    void ClearShader(std::vector<GLuint> &shaderList) {
        std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
    }

    GLuint CreateProgram(const std::vector<GLuint> &shaderList) {
        GLuint program = glCreateProgram();

        for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
            glAttachShader(program, shaderList[iLoop]);

        glLinkProgram(program);

        GLint status;
        glGetProgramiv (program, GL_LINK_STATUS, &status);

        for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
            glDetachShader(program, shaderList[iLoop]);

        return program;
    }

现在,编译器抱怨glCreateShader,glShaderSource,glCompileShader,glGetShaderiv,glDeleteShader,glCreateProgram,glAttachShader,glLinkProgram,glGetProgramiv和glDetachShader未在此范围内声明。

我认为我需要包括欢乐合唱团或者glew(对吗?)所以我选择了glew。 然后我将-glew32添加到我的makefile中并将glew.h添加到我的内容中

#include "GL/glew.h"

现在,编译器列出了glew.h的大量错误,例如----没有命名类型,并且没有在此范围内声明----

我不知道如何解决这个问题。 我应该补充一点,我对openGL没有多少经验。

1 个答案:

答案 0 :(得分:1)

  

然后我将-glew32添加到我的makefile中并将glew.h添加到我的内容

-glew32不对,它是-lglew32。在与OpenGL交互的所有其他内容之前,应包含#include <GL/glew.h>标头。