Problem with image in SDL

Started by
2 comments, last by Affected1 9 years, 10 months ago

So i have created this test app with examples found around the net. It works but there is a problem with the test image i'm using.

Here is complete code:


#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>

int main(int argc, char* args[])
{
	SDL_Surface *imageData = NULL;
	SDL_Event event;
	bool run = true;

	SDL_Init(SDL_INIT_VIDEO);

	SDL_Window *mainWindow;
	SDL_Renderer *mainRender;
	mainWindow = SDL_CreateWindow("SDL_2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
	mainRender = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED);

	imageData = IMG_Load("test.png");

	while(run)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				run = false;
			}
		}

		imageData = SDL_CreateRGBSurfaceFrom((void*)imageData->pixels,
			imageData->w,
			imageData->h,
			32,
			imageData->pitch,
			0xff0000, 0x00ff00, 0x0000ff, 0);
		SDL_Texture *texture = SDL_CreateTextureFromSurface(mainRender, imageData);
		SDL_RenderCopy(mainRender, texture, NULL, NULL);
		SDL_RenderPresent(mainRender);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(imageData);

		SDL_Delay(100);
	}

	SDL_FreeSurface(imageData);
	SDL_DestroyRenderer(mainRender);
	SDL_DestroyWindow(mainWindow);
	SDL_Quit();
	return 0;
}

Here is my test image:

http://i1153.photobucket.com/albums/p503/dbig17/test_zps0c7223a9.png

and here is how the program looks right now:

http://i1153.photobucket.com/albums/p503/dbig17/Untitled_zps3c3c782c.png

What am i doing wrong?

Advertisement

You need to call IMG_Init() prior to calling IMG_Load() [much like you called SDL_INIT() prior to SDL_Window() and SDL_Renderer()]. Take a peek here https://www.libsdl.org/projects/SDL_image/docs/SDL_image_frame.html.

Be sure to read on there about what flags to pass to IMG_Init to handle the proper file types.

Small Edit:

Don't forget to IMG_Quit() also smile.png

Try this code:


#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>

int main(int argc, char* args[])
{
	SDL_Surface *imageData = NULL;
	SDL_Event event;
	bool run = true;

	SDL_Init(SDL_INIT_VIDEO);
	IMG_Init(IMG_INIT_PNG); //initialize SDL_image

	SDL_Window *mainWindow;
	SDL_Renderer *mainRender;
	mainWindow = SDL_CreateWindow("SDL_2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
	mainRender = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED);

	imageData = IMG_Load("test.png");

	while (run)
	{
		while (SDL_PollEvent(&event))
		{
			if (event.type == SDL_QUIT)
			{
				run = false;
			}
		}
		
		imageData = SDL_CreateRGBSurfaceFrom((void*)imageData->pixels,
			imageData->w,
			imageData->h,
			32,
			imageData->pitch,
			imageData->format->Rmask, imageData->format->Gmask, imageData->format->Bmask, imageData->format->Amask);

		SDL_Texture *texture = SDL_CreateTextureFromSurface(mainRender, imageData);
		SDL_RenderCopy(mainRender, texture, NULL, NULL);
		SDL_RenderPresent(mainRender);
//		SDL_DestroyTexture(texture);
//		SDL_FreeSurface(imageData);
	}

	SDL_FreeSurface(imageData);
	SDL_DestroyRenderer(mainRender);
	SDL_DestroyWindow(mainWindow);
	SDL_Quit();
	return 0;
}

"For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life." - John 3:16

Blog: exchargedeeletri.wordpress.com

Now it works alright! Thanks guys smile.png

This topic is closed to new replies.

Advertisement