SDL窗口立即关闭

时间:2019-03-14 19:21:08

标签: c++ sdl

我遵循了有关如何开始使用SDL的youtube教程,而我目前的进展似乎跳过了handleEvents,更新和渲染功能,只是清除和关闭了窗口。

Main.cpp

#include "main.h"

using namespace std;

SDLMain* sdlwindow = nullptr;

int main(int argc, char *args[])
{
    sdlwindow = new SDLMain();
    sdlwindow->init("PLS - Partikel & Lyd System", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

    while (sdlwindow->online()) {
        sdlwindow->handleEvents();
        sdlwindow->update();
        sdlwindow->render();
    }

    sdlwindow->clean();
    return 0;
}

SDLMain.cpp

#include "SDLmain.h"

SDLMain::SDLMain(){}
SDLMain::~SDLMain(){}
void SDLMain::init(const char* TITLE, int XPOS, int YPOS, int WIDTH, int HEIGHT, bool FULLSCREEN){

    int FLAGS = 0;
    if (FULLSCREEN) {
        FLAGS = SDL_WINDOW_FULLSCREEN;
    }

    if (SDL_Init(SDL_INIT_EVERYTHING == 0)) {

        window = SDL_CreateWindow(TITLE, XPOS, YPOS, WIDTH, HEIGHT, FLAGS);
        if (window) {
            std::cout << "SDL Window was successfully created!" << std::endl;
        }
        else {
            std::cout << "SDL Window failed. SDL Error: " << SDL_GetError() << std::endl;
        }

        renderer = SDL_CreateRenderer(window, -1, 0);
        if (renderer) {
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
            std::cout << "SDL Renderer was successfully created!" << std::endl;
        }
        else {
            std::cout << "SDL Renderer failed. SDL Error: " << SDL_GetError() << std::endl;
        }

        isOnline = true;
    }
    else {
        isOnline = false;
    }
}

void SDLMain::handleEvents() {
    SDL_Event event;
    SDL_PollEvent(&event);

    switch (event.type) {
    case SDL_QUIT:
        isOnline = false;
        break;

    default:
        break;
    }
}
void SDLMain::update(){}
void SDLMain::render(){
    SDL_RenderClear(renderer);
    //Add stuff to render
    SDL_RenderPresent(renderer);
}
void SDLMain::clean(){
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    std::cout << "SDL System cleaned!" << std::endl;
}

SDLMain.h

#pragma once
#include <iostream>
#include <stdio.h>
#include <SDL.h>

class SDLMain {
public:
    SDLMain();
    ~SDLMain();

    void init(const char* TITLE, int XPOS, int YPOS, int WIDTH, int HEIGHT, bool FULLSCREEN);
    void handleEvents();
    void update();
    void render();
    void clean();

    bool online() {
        return isOnline;
    }

private:
    bool isOnline;
    SDL_Window* window;
    SDL_Renderer* renderer;
};

1 个答案:

答案 0 :(得分:2)

我猜这是

if (SDL_Init(SDL_INIT_EVERYTHING == 0)) {

是错字

if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {