Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Problem with image in SDL

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 Affected1   Members   

128
Like
0Likes
Like

Posted 27 June 2014 - 09:35 AM

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?



#2 viper1092   Members   

481
Like
1Likes
Like

Posted 27 June 2014 - 11:22 AM

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, 27 June 2014 - 11:23 AM.


#3 EEletri   Members   

580
Like
1Likes
Like

Posted 27 June 2014 - 11:22 AM

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, 27 June 2014 - 11:24 AM.

"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


#4 Affected1   Members   

128
Like
0Likes
Like

Posted 27 June 2014 - 11:42 AM

Now it works alright! Thanks guys smile.png






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.