SDL2 CreateRenderer抛出无效窗口错误

时间:2014-04-07 04:11:56

标签: c++ sdl sdl-2

我目前的代码如下:

#include <iostream>
#include "SDL.h"
#include "SDL_image.h"

int main(int argc, char **argv) {
    if (SDL_Init(SDL_INIT_EVERYTHING != 0)) {
        std::cout << "SDL_Init() Error: " << SDL_GetError() << std::endl;
        std::cin.get();
        return 1;
    }

    SDL_Window *win = SDL_CreateWindow("RandomSDL2", 100, 100, 1280, 720, SDL_WINDOW_SHOWN);
    if (win = nullptr) {
        std::cout << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
        std::cin.get();
        return 1;
    }

    SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (ren == nullptr) {
        std::cout << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
        std::cin.get();
        return 1;
    }

    SDL_Surface *img = IMG_Load("res/test.png");
    if (img == nullptr) {
        std::cout << "IMG_Load() Error: " << IMG_GetError() << std::endl;
        std::cin.get();
        return 1;
    }

    SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, img);
    SDL_FreeSurface(img);
    if (tex == nullptr) {
        std::cout << "SDL_CreateTextureFromSurface() Error: " << SDL_GetError << std::endl;
        std::cin.get();
    }

    SDL_RenderClear(ren);
    SDL_RenderCopy(ren, tex, NULL, NULL);
    SDL_RenderPresent(ren);

    SDL_Delay(2000);

    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);

    SDL_Quit();

    return 0;
}

似乎CreateRenderer函数抛出了错误无效窗口,我不确定为什么,我正在关注this教程,我已经将代码复制到T除了分辨率和使用SDL_image,SDL_image在这个阶段不应该真正影响渲染器的创建,我也尝试了教程中指定的分辨率,它似乎仍然会抛出同样的错误。

有没有人对此问题有任何见解?

1 个答案:

答案 0 :(得分:4)

if (win = nullptr) {
        ^ assignment, probably wanted ==

请勿将win设置为nullptr,而且可能不会nullptr