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;
}