2013-08-19 15 views
0

我想在mac上編寫一個非常簡單的sdl和gl程序來創建一個三角形,但它並不真正工作。SDL/OpenGL命令未在範圍內設置?

(在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$ 

打算請告訴我錯在這裏,我如何防止它有未申報的進一步的命令?

+0

你在假設'g ++'是用'-GL'和'-GLU'標記做什麼的?你的意思是'-lGL'和'-lGLU'? – genpfault

回答

1

我看到您通過本地包含語句包含SDL標頭,即使用雙引號代替楔子。編譯器消息指出在包含SDL/SDL.h時出現問題。您的SDL安裝已損壞,或者您偶然會將空白或不匹配的SDL/SDL.h放入源目錄中。

無論哪種情況,您都必須確保您的SDL安裝有效。

+0

非常感謝,包含的文件有些不對。 –

-1

看起來像編譯時只是沒有與SDL庫鏈接。只需添加適當的-l標誌,它應該工作。

編輯:datenwolf指出,錯誤不是由於缺少庫。不過,您應該在修正標題時仍包含正確的庫。 :)

+1

「未在範圍內聲明」是編譯器消息,指的是缺少的函數聲明。即缺少標題。鏈接器消息將是「未定義的參考」。 – datenwolf