开发者

The SDL_QUIT event does not trigger if I use a switch statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 13 hours ago.

Improve this question

Was following a tutorial by this guy, LimeOats, on how to remake Cave Story using C++ and SDL, I noticed that his event handling seemed a bit messy so I decided to integrate a switch command, which I had learned to do in a previous tutorial. However, it simply refuses to fire off SDL_QUIT at all, which should output a message, return 0, and stop the program. It's strange because I could have sworn it was working fine before. So I started a new program, set it all up, and it still doesn't work, however the initial "messy" implementation of nested if statements works flawlessly.

Non-Working Code

#include <SDL.h>

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    SDL_Event(event);
    

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* window = SDL_CreateWindow("The Unseen v0.01",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,
        480, 0);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);


    while (true) {

        while (SDL_PollEvent(&event)) {

            switch (event.type)
            {
            SDL_QUIT:
                cout << "SDL QUIT FIRED";
                return 0;

            default:
                break;
            }
        }
    }
        return 0;

}

Working Code

#include <SDL.h>

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    SDL_Event(event);
    

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* window = SDL_CreateWindow("The Unseen v0.01",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,
        480, 0);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);


    while (true) {

        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                return 0;
            }
        }
        
    }
        return 0;
}

I'm not a particularly gifted programmer, and I'm rather new to SDL so if I'm missing something here,开发者_如何学Python it would be much appreciated if anyone has any leads or solutions. Please and Thank ya's.

Tried: Replacing nested if statements with a switch. Expectation: I press the window escape(x) and SDL_QUIT is placed into SDL_Event event, the switch tests for event type, finds that SDL_QUIT is the current case, and returns 0. Result: Nothing

Tried: Reverting back to the code I was given, events are polled in an if statement, and then another if statement checks if the type is SDL_QUIT, if true it returns 0. Result: Successfully Quit

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜