Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL wont draw[solved]

Started by Lith May 12, 2010 at 1:13 PM 9 replies 1.9k views
Original Post
Lith
Lith
Im pretty sure i have had this problem before, and i posted here, and it got solved. Now i think im having that problem again and i have forgotten the solution. The problem is that SDL wont draw: . All the DLL's are in the right place . All the linker settings are in the right place. . The screen surface is not NULL . The image that im trying to draw's surface is not NULL . The drawing and image loading code is taken out one of my other projects that worked here is my drawing function:

//www.lazyfoo.net
void E_CORE::DrawImg(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, clip, destination, &offset);
}


here is the image loading code:

//www.lazyfoo.net
SDL_Surface* E_CORE::LoadImg(string filename)
{
		SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());

	//check for errors
	if(loadedImage != NULL)
	{
		//if there were no errors
		optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
		if(optimizedImage != NULL)
		{
			//set the colour key
			Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 255, 0, 255);
			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
		}
	}

	if(optimizedImage == NULL)
	{
		cout << "Failed to load " << filename << endl;
	}
	else
	{
		cout << "Loaded " << filename << endl;
	}

	return optimizedImage;
}


the image is not all pink(colour keying colour). Please can someone help me [Edited by - Lith on May 13, 2010 11:25:51 AM]
Wooh
Wooh
I can't see anything wrong. Do you call SDL_Flip to update the screen?
Lith
Lith
Yes i do, also i check to see if it's failed and it doesnt:
bool E_CORE::UpdateScreen(){	if(SDL_Flip(ScreenPointer) == -1)	{		cout << "Error Updating Screen\n";		return false;	}	return true;}
krokko
krokko
If the code you pasted works in a different program then the error probably isn't there, so post the rest (or more) of the code :P

Do you properly call SDL_SetVideoMode on ScreenPointer?
rip-off
rip-off
SDL_BlitSurface() has a return code. Inspect it and print SDL_GetError() in the event of failure. This might be revealing.
Lith
Lith
Okay, ill post my main:
#include "E_CORE.h"#include "E_LUA.h"#include "SDL.h"#include <string>extern "C" {	#include "lua.h"	#include "lualib.h"	#include "lauxlib.h"}using namespace std;#define WIDTH 640#define HEIGHT 480int main(int argc, char **argv){	bool quit = false;	SDL_Event Event;	Uint8 *keystates;	SDL_Surface* Screen = NULL;	SDL_Surface* image = NULL;	//Initialization	E_CORE EngineCore;	EngineCore.Init();	EngineCore.ScreenInit(WIDTH,HEIGHT,false,"E_CORE rendering test");	E_LUA EngineLUA;	EngineLUA.Init();	Screen = EngineCore.GetScreenSurface();	Event = EngineCore.GetEventPointer();	//load an image	image = EngineCore.LoadImg("image.bmp");	//execute a script	EngineLUA.ExecuteScript("data/scripts/script01.lua");	while(quit == false)	{		//and update the keystates struct		keystates = SDL_GetKeyState(NULL);		//Process Input (RAW SDL)		while(SDL_PollEvent(&Event))		{			if(Event.type == SDL_QUIT)			{				quit = true;				cout << "Exiting Main Loop\n";			}		}		//Do Logic		//Render		EngineCore.DrawImg(20,20,image,Screen,NULL);		//update the screen with changes		/*		EngineCore.UpdateScreen();		*/		SDL_Flip(Screen);	}	cout << "Freeing Surfaces\n";	SDL_FreeSurface(image);	cout << "Calling E_LUA.End(\n)";	EngineLUA.End();	cout << "Calling E_CORE.End()\n";	EngineCore.End();	return 0;}


E_CORE init code:
bool E_CORE::Init(){	//This will initialize everything that the core deals with:	/*		Graphics		Sound	*/	//Init SDL	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)	{		//there was an error setting up SDL		return false;	}	//Init TTF Libs	if(TTF_Init() == -1)	{		//there was a problem setting up TTF		return false;	}	if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)	{		//there was an error setting up the audio		return false;	}	//if we get here then everything went fine	return true;}


E_CORE screen init code:
bool E_CORE::ScreenInit(int width, int height, bool fullscreen, string caption){	if(fullscreen == false)	{		ScreenPointer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);		if(ScreenPointer == NULL)		{			return false;		}	}	else	{		ScreenPointer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE|SDL_FULLSCREEN);		if(ScreenPointer == NULL)		{			return false;		}	}	//now we set the caption of the window	SDL_WM_SetCaption(caption.c_str(), NULL);	return true;}


i will try the things that rip-off said
Lith
Lith
I put cout << SDL_GetError(); in all of the failed initialization things and blit surface, but they all didnt fail!

Also i made it print out the return value of blitsurface, event if there want an error, and it was 0

EDIT:

im going to come back to this tommorow
krokko
krokko
What happens at runtime? Do you get a black window?
If you do I'd try using SDL_FillRect on Screen and see if you can at least render that
rip-off
rip-off
Try to reduce the code down to the essentials. For example, temporarily disable or comment out your Lua code. Likewise, you could use SDL_GetVideoSurface() as the argument to SDL_Flip(). Fill your code with assertions, particularly around pointers.

Don't forget to try some other things to check, for example fill the screen with a random colour using SDL_FillRect() (and disable your image drawing).

If you keep eliminating stuff, you will eventually find the error, or you will have a minimal program that you can post here and we can try it out.

It might look something like this (quicky error handling here, just to demonstrate):
#include <iostream>#include <stdexcept>#include "SDL.h"#include "SDL_image.h"void break(){   // Will bring the debugger up   int *ptr = 0;   *ptr = 42;}void checkError(int result){   if(result != 0)   {       break();   }}void checkError(void *pointer){   if(!pointer)   {       break();   }}int main(int, char **){    checkError(SDL_Init(SDL_INIT_VIDEO));    SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_ANYFORMAT);    checkError(screen);    SDL_Surface *surface = IMG_Load("image.bmp");    checkError(surface);    bool running = true;    while(running)    {        SDL_Event event;        while(SDL_PollEvent(&event))        {            if(event.type == SDL_QUIT)            {                running = false;            }        }        checkError(SDL_FillRect(screen, 0, 0));        checkError(SDL_BlitSurface(surface, 0, screen, 0));        checkError(SDL_Flip(screen));    }}
Lith
Lith
Hello again and thanks for all the replies.

@Dunge: I think your right, my other problem was simple to solve.

I tryed commenting out all LUA things and it didnt work

I tryed using SDL_FillRect() and it worked perfectly, so at least we know that it's a problem with my image loading or image drawing. I have posted those 2 functions.

Im going to try and comment out the colour keying code in the image loading function and see what happens

EDIT:

When i removed the colour keying code from the image loading function, the image displayed perfectly, im going to look in to the colour keying.

EDIT 2:

Tryed replaceing this:
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 255, 0, 255);

with this:
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xFF, 0, 0xFF);


but nothing worked

EDIT 3:

I tryed loading in an image from the project that i took the image loading and drawing code from. It loaded fine with colour keying!

So that must mean it's just a problem with my image format!

EDIT 4(Final):

So the problem was my image, not my code(that's a first). I think this is the problem:

I use Windows XP, and in Windows XP you can right click and select new->bitmap image. When i made the image that i tryed to load in, i made the image that way. When it was created, i simply right-clicked and pressed edit. But when i made all the other images, i first started up paint, drew the image and then saved it.

There must be a difference in format or something, that was a weird error.

Thanks for all your help :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.