I get Parameter 'renderer' is invalid, when trying to render a texture.

I am following codegophers tutorial on youtube and I am on part 4 right now.
Here is the code for renderwindow.cpp:

RenderWindow::RenderWindow(const char* title, int w, int h)
	:window(NULL), renderer(NULL)
{
	SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN);
	if(window == NULL)
	{
		cout << "Window is NULL, Error: " << SDL_GetError() << endl;
	}

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}

SDL_Texture* RenderWindow::loadTexture(const char* p_filePath)
{
	SDL_Texture* texture = NULL;
	texture = IMG_LoadTexture(renderer, p_filePath);

	if(texture == NULL)
		cout << "Failed to load texture, Error: " << SDL_GetError() << endl;

	return texture;
}

void RenderWindow::cleanUp()
{
	SDL_DestroyWindow(window);
}

void RenderWindow::clear()
{
	SDL_RenderClear(renderer);
}

void RenderWindow::render(SDL_Texture* tex)
{
	SDL_RenderCopy(renderer, tex, NULL, NULL);
}

void RenderWindow::display()
{
	SDL_RenderPresent(renderer);
}

There are no compiler errors and the window shows up but I still get the messages “Window is NULL, Error:” and “Failed to load texture, Error: Parameter ‘renderer’ is invalid”. Since the window error is showing up that tells me that both the window and the renderer are set to null even though the window shows up. If I get rid of everything that initializes them to NULL then the window opens and closes on its own without loading the texture but without any messages.
Here is the code for main:

int main(int argc, char* argv[])
{
	if(SDL_Init(SDL_INIT_VIDEO) > 0)
		cout << "HEY.. SDL_Init HAS FAILED. SDL ERROR: " << SDL_GetError() << endl;

	if(!(IMG_Init(IMG_INIT_PNG)))
		cout << "IMG_Init has failed. Error: " << SDL_GetError() << endl;

	RenderWindow window("GAME v1.0", 1280, 720);

	SDL_Texture* grassTexture = window.loadTexture("res/GFX/ground_grass_1.png");

	bool gameRunning = true;

	SDL_Event event;

	while(gameRunning)//main loop
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
				gameRunning = false;
		}

		window.clear();
		window.render(grassTexture);
		window.display();
	}

	window.cleanUp();
	SDL_Quit();

	return 0;
}

Hi, welcome! It looks like you just missed assigning the window variable to SDL_CreateWindow in the first line of the RenderWindow constructor, therefore the window pointer continues being NULL despite the window being successfully created.

1 Like

Thank you that fixed it! I knew it was something simple I over looked haha