Advertisement

SDL_WINDOWEVENT_EXPOSED not occurring

Started by November 28, 2014 04:50 AM
2 comments, last by Satharis 10 years, 1 month ago

I am following this guide: http://davidgow.net/handmadepenguin/ch2.html

I am trying to get the screen to flip between black and white when it is redrawn. According to this guide, it is saying for now to put the render code when the SDL_WINDOWEVENT_EXPOSED event occurs. However, this event never seems to occur (confirmed through LLDB). According to the SDL documentation, it seems that the event occurs when part of the window is covered by another window. I have tried doing this to no avail. The window stays black at all times.

I can't seem to find anything on Google.

The difference between me and this guide is that I am doing this on OS X. Could this be the reason?

Here is my code:


void render(SDL_Window* window) {
    SDL_Renderer* renderer = SDL_GetRenderer(window);


    static bool isBlack = true;


    if (isBlack) {
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        isBlack = false;
    }
    else {
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        isBlack = true;
    }


    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
}


void processWindowEvent(SDL_WindowEvent* we){
    switch (we->event) {
        case SDL_WINDOWEVENT_RESIZED: //this occurs at the proper time (when the window is resized)
            printf("SDL_WINDOWEVENT_RESIZED (%d, %d)\n", we->data1, we->data2);
            break;
        case SDL_WINDOWEVENT_EXPOSED: //this is never hit
            render(SDL_GetWindowFromID(we->windowID));
            break;        
    }
}


void processEvent(SDL_Event* e) {
    switch (e->type) {
        case SDL_QUIT:
            running = false;
            break;
        case SDL_WINDOWEVENT:
            processWindowEvent(&e->window);
            break;
    }
}

Thanks very much!

SDL_WINDOWEVENT_EXPOSED is just for when the graphics has to be redrawn. I'm not sure exactly when this happens but it probably depends on a lot of things. If the renderer is not hardware accelerated I suspect you never get this event.

If you want the graphics to update you should call the render function from somewhere else in the code. If you always update the whole screen very often you can probably ignore SDL_WINDOWEVENT_EXPOSED.
Advertisement

Okay cool. Odd that the guide was telling to do this (and it is not hardware accelerated in the guide). Maybe it sends this event on Linux (which is what he was using).

Thanks!

Okay cool. Odd that the guide was telling to do this (and it is not hardware accelerated in the guide). Maybe it sends this event on Linux (which is what he was using).

Thanks!

Assuming you're making a game you'll usually just be running things in a loop and drawing as often as you can, redrawing the entire screen. Reactive events like those are usually designed for standard desktop apps, where things will rarely if ever change onscreen and only need to be redrawn periodically. You won't have any timing control over reactive events, so if you need time at all you'll have to use a loop.

Usually you'd want something like this for your basic loop.
bool running = true;

while(running)
{
    SDL_Event Event;
    while(SDL_PollEvent(&Event))
    {
       // Do stuff here..
    }

    // Update

    // Draw
}

This topic is closed to new replies.

Advertisement