c++ - SDL Infinite Tile Background -


i'm trying create tiled background in sdl, 1 scrolls , continues scroll indefinitely.

so, came code , tested out. works enough, can travel 1920 pixels along x axis , 1080 along y.

here's code:

void background::render(sdl_renderer *renderer){     int xoffset = 0;     int yoffset = 0;     for(int y = 0; (y * 411) < 1080; y++){         for(int x = 0; (x * 405) < 1920; x++){             xoffset = 0;             yoffset = 0;             if(gamemanager::getinstance().getglobalx() + (405 * x) + 405 < 0){                 xoffset = 1920;             }             if(gamemanager::getinstance().getglobaly() + (411 * y) + 411 < 0){                 yoffset = 1080;             }             sdl_rect backrect = {gamemanager::getinstance().getglobalx() + (405 * x) + xoffset, gamemanager::getinstance().getglobaly() + (411 * y) + yoffset, 405, 411};             sdl_rendercopy(renderer, resourcemanager::getinstance().gettexture("background"), 0, &backrect);         }     } } 

the getglobalx() , getglobaly() object should relative player.

you should able draw 1920x1080 background more once.

the algorithm this.

  1. draw background starting @ (-1920,0) (completely out of screen)
  2. draw copy of background, time starting @ (0,0).
  3. every frame, draw both backgrounds 1 pixel right, you'll have scrolling illusion, end of background exiting right come out left.
  4. once background @ step 1 has come (0,0), draw background @ (-1920,0) , keep scrolling.

so basically, push 2 backgrounds right , keep putting 1 on left every time need to. should simple code.