Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2 CreateRenderer throwing Invalid Window Error

Tags:

c++

sdl

sdl-2

My current code is as follows:

#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;
}

It seems that the CreateRenderer function throws the error Invalid Window, I'm unsure as to why, I'm currently following this tutorial, I've copied the code down to a T besides the resolution and the use of SDL_image, SDL_image shouldn't really effect the creation of the renderer at this stage, I've also tried the resolution specified in the tutorial and it seems to still throw the same error.

Does anyone have any insight on this issue?

like image 571
Sn0wCrack Avatar asked Sep 16 '26 09:09

Sn0wCrack


1 Answers

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

Don't set win to nullptr and it probably won't be nullptr.

like image 142
genpfault Avatar answered Sep 17 '26 22:09

genpfault