Skip to main content
gamedev.net
🔒 Locked

Function not working properly

Started by zippo88 2 replies 850+ views
Original Post
zippo88
zippo88
Hi. I have made some code that works fine but when i make the code a function it will not work. This is the function i made. void BlockDown(SDL_Surface* screen, SDL_Rect gubbeposition){ if (gubbeposition.y >= (screen->h - 40)){ gubbeposition.y = (screen->h - 40); } } and in main i call this: BlockDown(screen, gubbeposition); full code: #include #include #include #include #include void BlockDown(SDL_Surface* screen, SDL_Rect gubbeposition){ if (gubbeposition.y >= (screen->h - 40)){ gubbeposition.y = (screen->h - 40); } } int main(int argc, char **argv) { bool gamerunning = true; bool keysHeld[323] = {false}; // everything will be initialized to false int level = 1; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER ); SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); SDL_WM_SetCaption("SDLTest", 0); //Ladda BMP fil SDL_Surface* bakgrund = SDL_LoadBMP("Bakgrund.bmp"); SDL_Surface* gubbe = SDL_LoadBMP("Gubbe.bmp"); // Göm färgen Magneta för bild SDL_SetColorKey(gubbe, SDL_SRCCOLORKEY, SDL_MapRGB(gubbe->format, 255, 0, 255)); //BakgrundBildens koordinater som ska laddas SDL_Rect bakgrundDel; bakgrundDel.x = 0; bakgrundDel.y = 0; bakgrundDel.w = 640; bakgrundDel.h = 480; //BakgrundBildens koordinater i SDL rutan SDL_Rect bakgrundposition; bakgrundposition.x = 0; bakgrundposition.y = 0; bakgrundposition.w = bakgrundDel.w; bakgrundposition.h = bakgrundDel.h; //GubbBildens koordinater som ska laddas SDL_Rect gubbeDel; gubbeDel.x = 0; gubbeDel.y = 0; gubbeDel.w = 40; gubbeDel.h = 40; //GubbBildens koordinater i SDL rutan SDL_Rect gubbeposition; gubbeposition.x = screen->w / 2 - (gubbeDel.w / 2); gubbeposition.y = (screen->h / 2) - (gubbeDel.h / 2); gubbeposition.w = gubbeDel.w; gubbeposition.h = gubbeDel.h; SDL_Event event; while (gamerunning) { if (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { gamerunning = false; } if (event.type == SDL_KEYDOWN) { keysHeld[event.key.keysym.sym] = true; } if (event.type == SDL_KEYUP) { keysHeld[event.key.keysym.sym] = false; } } if (keysHeld[SDLK_ESCAPE]) { gamerunning = false; } if ( keysHeld[SDLK_LEFT] ) { gubbeposition.x -= 1; gubbeDel.x = 40; } if ( keysHeld[SDLK_RIGHT] ) { gubbeposition.x += 1; gubbeDel.x = 40; } if ( keysHeld[SDLK_UP] ) { gubbeposition.y -= 1; gubbeDel.x = 0; } if (keysHeld[SDLK_DOWN]) { gubbeposition.y += 1; gubbeDel.x = 0; } if (level==1) { if (gubbeposition.x == screen->w){ gubbeposition.x = 1; bakgrundDel.x = 640; level = 2; } if (gubbeposition.y == screen->h){ gubbeposition.y = 1; bakgrundDel.y = 480; level = 3; } } if (level==2) { if (gubbeposition.x >= (screen->w -40)){ gubbeposition.x = (screen->w -40); } if (gubbeposition.x == 0){ gubbeposition.x = screen->w; bakgrundDel.x = 0; level = 1; } if (gubbeposition.y == screen->h){ gubbeposition.y = 1; bakgrundDel.y = 480; level = 4; } } if (level==3) { BlockDown(screen, gubbeposition); if (gubbeposition.x == screen->w){ gubbeposition.x = 1; bakgrundDel.x = 640; level = 4; } if (gubbeposition.y == 0){ gubbeposition.y = 480; bakgrundDel.y = 0; level = 1; } } if (level==4) { BlockDown(screen, gubbeposition); if (gubbeposition.x >= (screen->w - 40)){ gubbeposition.x = (screen->w - 40); } if (gubbeposition.y == 0){ gubbeposition.y = 480; bakgrundDel.y = 0; level = 2; } if (gubbeposition.x == 0){ gubbeposition.x = 640; bakgrundDel.x = 0; level = 3; } } //Gör skärmen svart efter varje loop SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); //Hämta en del av en .BMP fil. FIL / Del av bild / Yta / bild position SDL_BlitSurface(bakgrund, &bakgrundDel, screen, &bakgrundposition); SDL_BlitSurface(gubbe, &gubbeDel, screen, &gubbeposition); SDL_Flip(screen); SDL_Delay(1); } SDL_FreeSurface(gubbe); SDL_FreeSurface(bakgrund); SDL_Quit(); return 0; } i was hoping to make most of that level crap functions so it's easier to understand. But i can't even get 1 to work so :(
Ron AF Greve
Ron AF Greve
Hi,


It seems you are passing gubbeposition by value (i.e. the function wont' have any effect:

void BlockDown(SDL_Surface* screen, SDL_Rect gubbeposition){
if (gubbeposition.y >= (screen->h - 40)){
gubbeposition.y = (screen->h - 40);
}
}

Try changing it to:
void BlockDown(SDL_Surface* screen, SDL_Rect& gubbeposition){
if (gubbeposition.y >= (screen->h - 40)){
gubbeposition.y = (screen->h - 40);
}
}
Ron AF Greve
zippo88
zippo88
Quote:
Original post by Ron AF Greve
Hi,


It seems you are passing gubbeposition by value (i.e. the function wont' have any effect:

void BlockDown(SDL_Surface* screen, SDL_Rect gubbeposition){
if (gubbeposition.y >= (screen->h - 40)){
gubbeposition.y = (screen->h - 40);
}
}

Try changing it to:
void BlockDown(SDL_Surface* screen, SDL_Rect& gubbeposition){
if (gubbeposition.y >= (screen->h - 40)){
gubbeposition.y = (screen->h - 40);
}
}


Thanks it worked :). If i understand it correctly before i just copied the values and changed them in the function, and now i actually changed the values that are in main? I hope i understood it correctly. I am having some trouble understanding pointers.
Ron AF Greve
Ron AF Greve
Hi,

Yes, that is correct. Think of it this way; The struct gubbeposition has some space in memory. Now if you pass it by value it is pushed onto the stack. It is this copy of the struct on the stack that gets changed in the function. Values on the stack are discarded when returning from the function call.

With a pointer (or referene which is just a pointer in disguise). The address of the structure is pushed on the stack. Changes are then done to whatever the address is pointing to (i.e. the original struct in this case). After the function returns, again the values (in this case the address of the struct) is discarded. However since the original struct in memory was changed the changes to the struct remain.
Ron AF Greve

Topic Locked

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

Sign in to reply to this topic.