When to draw and swap buffers

Started by
12 comments, last by XTAL256 15 years, 11 months ago
Hmm, when do you need to use glFlush? And why don't you need to do that when double buffering?
Anyway, i have decided to just do what V-man and Decrius suggested and just render every 30ms or so. I am currently having problems with that but i should be able to get it sorted out.

EDIT: btw, glFlush worked for single buffering, thanks. And, strangely, there was no flickering or anything, is that because of OpenGL doing something? Anyway, depending on how things go, i may use single buffering for the GUI and double buffering for the game.
[Window Detective] - Windows UI spy utility for programmers
Advertisement
The idea is that any drawing you make in your drawing function is no happening directly on the screen, you are in fact altering the buffer. So, when you're done with drawing, you need to output that buffer onto the screen, which is what is called flushing. When you are using double buffering you don't need to flush the buffer, beacuse the SwapBuffers function does it for you.
That is why when you use single buffering, you need to do that manually or nothing will be drawn, everything will stay in the buffer.
Quote:Original post by XTAL256
Anyway, i have decided to just do what V-man and Decrius suggested and just render every 30ms or so. I am currently having problems with that but i should be able to get it sorted out.

Ok, i need help [grin]. I am using a timer (SDL) to render each frame but for some reason nothing is being drawn (just garbage on the screen).
UINT render(UINT interval, void* param) {    clearScreen();    Component::drawScreen();   // Draw the GUI on screen    SwapBuffers(hdc);    return interval;}thread::Timer timer(render, 30);int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int wndState) {    MSG msg;               // Windows message structure    BOOL done = FALSE;     // Bool variable to exit loop    // Create our OpenGL window, initiate SDL components and graphics    try {        initWindow(false);        initSDL();        initGraphics(hdc);    }    catch (Error e) {        displayError(e);        done = TRUE;    }    guiLoadData();    PlayerScreen screen;    timer.start();     // <-- i start the timer here    while(!done) {        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {            if (msg.message==WM_QUIT) {                done = TRUE;            }            else {                TranslateMessage(&msg);                DispatchMessage(&msg);            }        }    }    // shutdown stuff}/// Timer.h ///////////////////////////////////////class Timer {public:    SDL_TimerID id;    unsigned int interval;    bool isRunning;    Timer(UINT (*funcPtr)(UINT, void*), int interval, bool run = false);    ~Timer();    UINT (*funcPtr)(UINT, void*);    void start();};/// Timer.cpp /////////////////////////////////////Timer::Timer(UINT (*funcPtr)(UINT, void*), int interval, bool run) {    this->funcPtr = funcPtr;    this->interval = interval;    this->isRunning = run;    if (run)        start();    else        this->id = NULL;}Timer::~Timer() {    SDL_RemoveTimer(id);}void Timer::start() {    this->id = SDL_AddTimer(interval, funcPtr, NULL);    if (this->id == NULL)        throw TimerError("Could not initiate timer");}

I have tested the timer and it works, it calls render after i start it. And the drawing code works fine if i have it in the while loop (as an 'else' to 'if(PeekMessage...)').
Is it ok to call OpenGL operations from a separate thread/timer?
[Window Detective] - Windows UI spy utility for programmers
I just tested the render function running in an SDL_Thread and still nothing, just a blank screen.
Ok, i just realised what's happening. When i was doing some debugging before i noticed that code in the main while loop was being executed before code in the thread. I guessed that the loop was receiving a lot of messages like WM_CREATE (?) and that had priority over the thread (or had equal priority but was scheduled first). Anyway, i realised that i started the thread before the loop was entered. So although i started it after OGL and window was initialised, well i don't actually know what is happening. But what i did was put timer.start() after the loop, and when i quit i saw a little flicker of my GUI being drawn. So when do i actually need to start rendering the screen?
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement