Sign in to follow this  
Affected1

Problem with image in SDL

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

Edited by viper1092

Share this post


Link to post
Share on other sites

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;
}
Edited by EEletri

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this