Original Post
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 :(