0

我正在尝试在 mac 上编写一个非常简单的 sdl 和 gl 程序来创建一个三角形,但它并没有真正起作用。

(在 C++ 中)

#include <iostream>
#include "SDL/SDL.h"
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

/*INITIALIZE*/
void init()
{
    glClearColor(0.0,0.0,0.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,640.0/480.0,1.0,500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

/*DISPLAY*/
void display()   // drawing
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);

    glVertex3f(0.0,2.0,-5.0);
    glVertex3f(-2.0,-2.0,-5.0);
    glVertex3f(2.0,-2.0,-5.0);

    glEnd();
}

/*MAIN*/
int main(int argc, char** argv) // arguments required
{
    SDL_Init(SDL_INIT_EVERYTHING); // initialize and setup sdl
    SDL_Surface* screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
    bool running = true;
    Uint32 start;
    SDL_Event event;
    init();

    while (running)
    {
        start = SDL_GetTicks();

        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    running = false;
                    break;
            }
        }

        display();
        SDL_GL_SwapBuffers();
        if(1000/30 > (SDL_GetTicks() - start))
            SDL_Delay(1000/30 - (SDL_GetTicks() - start));
    }
    SDL_Quit();
    return 0;

}

但是当我编译它时:

Computer:sdlcode User$ g++ third.cpp -GL -GLU
third.cpp: In function ‘int main(int, char**)’:
third.cpp:34: error: ‘SDL_OPENGL’ was not declared in this scope
third.cpp:34: error: ‘SDL_SetVideoMode’ was not declared in this scope
third.cpp:55: error: ‘SDL_GL_SwapBuffers’ was not declared in this scope
Computer:sdlcode User$ 

这里出了什么问题,如何防止它有未声明的进一步命令?

4

2 回答 2

1

我看到您通过本地包含语句包含了 SDL 标头,即使用双引号而不是楔形。编译器消息表明包含SDL/SDL.h. 您的 SDL 安装已损坏,或者您不小心将一个空的或不匹配的 SDL/SDL.h 放入源目录。

无论哪种情况,您都必须确保您的 SDL 安装有效。

于 2013-08-19T10:38:01.640 回答
-1

看起来您只是在编译时没有与 SDL 库链接。只需添加适当的 -l 标志,它应该可以工作。

编辑: datenwolf 指出错误不是由于缺少库。但是,当您修复标题时,您仍然应该包含正确的库。:)

于 2013-08-19T10:30:47.367 回答