2015-07-01 87 views
0

我想用sdl作爲openGL的窗口管理器。我看着使用Windows本機API,但看起來很混亂。一次執行類代碼C++

說到這裏,我有一個class Window,我想將所有SDL的東西包裝到我的Windows管理中。如果我發現我不想使用SDL,它會讓我稍後換掉Windows管理。

我猜測,很多openGL初始化代碼只需要運行一次。

if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { 
     exit(0x1); 
    } 

    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_BUFFER_SIZE,  32); 

    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,  8); 
    SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8); 
    SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); 
    SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8); 

    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); 
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2); 

然後在類的構造函數,我可以

Window::Window(int winW, int winH) { 

    if((Surf_Display = SDL_SetVideoMode(winW,winH,32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_RESIZABLE)) == NULL) { 
     exit(2); 
    } 

    glClearColor(0, 0, 0, 0); 
    glClearDepth(1.0f); 

    glViewport(0, 0, winW, winH); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    glOrtho(0, winW, winH, 0, 1, -1); 

    glMatrixMode(GL_MODELVIEW); 
    glEnable (GL_BLEND); 

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    glLoadIdentity(); 


} 

創建窗口,我只是不知道如何去這樣做。如果我在標題中定義類之前放置了代碼,是否達到了預期的效果?

;init code 
;class window { }; 

回答

1

最簡單的事情將是把該初始化代碼爲函數和只調用這個函數從main

/* header */ 
void init_window_management (void);  
/* some source file */ 
void init_window_management (void) { 
    // your code 
}  
/* main file */ 
// ... also include that header ... 
int main(int argc, char ** argv) { 
    // ... 
    init_window_management(); 
    // ... use instances of the window class 
} 

再就是也就是std::call_once

如果我在代碼頭中定義類之前放置了代碼,這是否達到了預期的效果?

否。標頭用於函數和類聲明。在(成員)函數中執行生命的代碼,然後通過main函數調用(最終)這些代碼。

+0

好吧,我只是覺得有一個類需要在main中進行一些初始化是很奇怪的,但可能是最好的選擇。謝謝。 – Chemistpp