[ad_1]
To clear up this, you could possibly create a texture the scale of your whole map. SDL2 can then render to a texture as an alternative of rendering on to the display. Store each object’s place (participant, enemies and whatnot) in absolute phrases (from the top-left nook of the map), and render all the pieces. Once that is finished, you may then render a portion of the map texture to the display, utilizing SDL_RenderCopy[Ex]:
// initially of the sport:
SDL_Texture* world = SDL_CreateTexture(
renderer,
PIXEL_FORMAT,
SDL_TEXTUREACCESS_TARGET, // crucial, permits you to render to this texture.
worldWidth,
worldHeight);
SDL_Rect digital camera = {0, 0, cameraWidth, cameraHeight}; // change x and y to middle the digital camera wherever you'd prefer it to be
//
// in your sport loop
//
SDL_SetRenderTarget(renderer, world); // render all the pieces to the world texture
// render your enemies, participant and all
SDL_RenderCopy(renderer, participant, NULL, player_rect); // player_rect ought to maintain the place on the map
SDL_SetRenderTarget(render, NULL); // swap again the renderer in order that it targets the display
SDL_RenderCopy(renderer, world, digital camera, NULL); // it will render solely the portion of world that's sure by the digital camera rectangle.
SDL_RenderPresent(renderer);
this fashion, you do not have to offset the place of all the pieces relying on the place you digital camera is. You simply render as if the entire map was displayed, after which copy a tiny portion of this.
The solely downside with that technique is that you’ll render objects that, ultimately, are exterior the digital camera and won’t be displayed. to resolve that, you may examine if an object’s Rect lies within the digital camera rect with SDL_HasIntersection(&digital camera, &object_rect);. If not, simply by move SDL_RenderCopy for that object.
[ad_2]