Rookie question, but I can’t set the screen up inside
another function. It has to be in the main() body.
For example, if I try:
int main( int argc, char* args[] )
{
SDL_Surface* scr;
scr = SDL_SetVideoMode(x, y, 0,SDL_HWSURFACE|SDL_HWPALETTE);
}
… then it works okay.
However, if I instead use:
void screen(int x, int y, SDL_Surface *scr) {
scr = SDL_SetVideoMode(x, y, 0,SDL_HWSURFACE|SDL_HWPALETTE);
}
int main( int argc, char* args[] )
{
SDL_Surface* scr;
screen(1024,768,scr);
}
… it causes a crash (unhandled exception). Anyone know why?
Cheers, Dan–
www.skytopia.com
Try this instead:
void screen(int x, int y, SDL_Surface **scr) {
*scr = SDL_SetVideoMode(x, y, 0,SDL_HWSURFACE|SDL_HWPALETTE);
}
int main( int argc, char* args[] )
{
SDL_Surface* scr;
screen(1024,768,&scr);
}
Cheers,
Andre
Daniel White wrote:> Rookie question, but I can’t set the screen up inside
another function. It has to be in the main() body.
For example, if I try:
int main( int argc, char* args[] )
{
SDL_Surface* scr;
scr = SDL_SetVideoMode(x, y, 0,SDL_HWSURFACE|SDL_HWPALETTE);
}
… then it works okay.
However, if I instead use:
void screen(int x, int y, SDL_Surface *scr) {
scr = SDL_SetVideoMode(x, y, 0,SDL_HWSURFACE|SDL_HWPALETTE);
}
int main( int argc, char* args[] )
{
SDL_Surface* scr;
screen(1024,768,scr);
}
… it causes a crash (unhandled exception). Anyone know why?
Cheers, Dan
This is just the way C works - pointers are passed by value. You can
modify the pointee in the function, but changes to the pointer itself
are lost because the copy passed to the function is discarded.
Another option, besides Andre’s suggestion of using a pointer to a
pointer, is simply to give screen() a return value:
SDL_Surface *screen(int x, int y)
{
return SDL_SetVideoMode(x, y, 0,SDL_HWSURFACE|SDL_HWPALETTE);
}
HTH
– brianOn Wed, Oct 7, 2009 at 2:54 PM, Daniel White wrote:
Rookie question, but I can’t set the screen up inside
another function. It has to be in the main() body.