I've stumbled accross a curious problem. It happens that if I declare two int variables before SDL_Init, the functions to make a button appear and move do not work. However, if I put them after they do work. It is strange because the functions accion and mostrar do not use those two variables at all. Have I overlooked something?

int main(int argc, char** args)
{
bool Quit=0;

int mousex, mousey;  //If declared here, the program doesn't work

SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *screen=SDL_SetVideoMode(1024,768,32,SDL_ANYFORMAT);
SDL_Surface *imgboton[4];

int mousex, mousey;  //If declared here, the program works

imgboton[MOUSEDOWN]=cargarImagen("mousedown.png");
imgboton[MOUSEUP]=cargarImagen("mouseup.png");
imgboton[MOUSEOUT]=cargarImagen("mouseout.png");
imgboton[MOUSEOVER]=cargarImagen("mouseover.png");
boton botonprincipal;
botonprincipal.inicializar(250,250,154,114, imgboton);
//TTF_Init();
SDL_Event event;
while(Quit==0)
	{
	SDL_PollEvent(&event);
	//SDL_GetRelativeMouseState(&mousex,&mousey);
	if(event.type==SDL_QUIT) {Quit=1;}
	botonprincipal.accion(event, imgboton);
	botonprincipal.mostrar(screen);
	SDL_Flip(screen);
	}
SDL_FreeSurface(imgboton[MOUSEDOWN]);
SDL_FreeSurface(imgboton[MOUSEUP]);
SDL_FreeSurface(imgboton[MOUSEOUT]);
SDL_FreeSurface(imgboton[MOUSEOVER]);
SDL_Quit();
return 0;
}

Here's the code of the functions

void boton::inicializar(int x, int y, int w, int h, SDL_Surface **imgboton)
{
area.x=x;
area.y=y;
area.h=h;
area.w=w;
frame=imgboton[MOUSEOUT];
return;
}

void boton::accion(SDL_Event event,SDL_Surface **imgboton)
{
int x=0, y=0;
if( event.type == SDL_MOUSEMOTION )
	{
	x=event.motion.x;
	y=event.motion.y;
	if((x>area.x)&&(x<area.x+area.w)&&(y>area.y)&&(y<area.y+area.h))
		{
		frame=imgboton[MOUSEOVER];    
		}
	else
		{
		frame=imgboton[MOUSEOUT];
		}    
	}
}

void boton::mostrar(SDL_Surface *screen)
{
SDL_BlitSurface(frame,NULL,screen,&area);
return;
}

What are the values of

MOUSEDOWN
MOUSEUP
MOUSEOUT
MOUSEOVER

They must be in the range 0..3, inclusive.

MOUSEDOWN
MOUSEUP
MOUSEOUT
MOUSEOVER

Their values range from 0 to 3.

What you are experiencing sounds like memory overrun(s) somewhere i.e. by introducing extra variables at 'proper places', problems magically disappear.
So, check e.g. that
- array/buffer indices are within the limits (remember, indices are zero-based).
- memory allocations are sufficiently large
- the SDL functions succeed (if they fail, use SDL_GetError() )

P.S. Since SDL_Event is a struct, you might want to pass it by reference i.e.

void boton::accion(SDL_Event & event,SDL_Surface **imgboton)
// or const reference, if it's not to be modified
void boton::accion(const SDL_Event & event,SDL_Surface **imgboton)

The weird thing is that it all works except the buttons. They do not appear nor do they respond when you click on the area they are supposed to be. The indices of the array of four animations are correct, from zero to three.

Besides, the variables mousex and mousey are not used in that version of the program, they are merely declared so I don't get why it's not working. The boolean variable certainly doesn't pose any problem.

Besides, the variables mousex and mousey are not used in that version of the program, they are merely declared so I don't get why it's not working.

Again, what you are experiencing sounds like memory overrun(s) somewhere i.e. by introducing extra variables at 'proper places', problems magically disappear (and vice versa). Perhaps read Wikipedia Buffer overflow for some insight.

Use debugger and step through the code line by line. Observe how your variables/functions are behaving.

PS. You might also search/post in a SDL forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.