Need help with screen scrolling in SDL

I’m new to SDL and was confused about how scrolling over an image of a terrain works. Right now I’ve found a solution in which I offset the image to make it look like the player is moving. I’m not sure if this is the best way and would appreciate if someone could explain to me how to do this the correct way.

Can you be more specific with your question, its very vague.

I have no idea what your game is or what type of scrolling do you need. Camera based, vertical or horizontal.
Your approach could be perfect for your type of game.

I’m trying to create camera based scrolling. I have got to a point where the camera can move and collide with the edges of the world, but I’m having trouble making the character move the correct way when the camera is on the edge of the world map. I want the character to be able to move to the edge of the map while the camera doesn’t move. Something like how the camera in stardew valley works.

So you will want to do something like

        // Update Camera position based on some focus point, Player centre pos maybe .
        ...

		//Check Horizontal Boundaries
		if (m_camPos.x < 0) {
			m_camPos.x = 0;
		} else if (m_camPos.x > m_worldSize.x - m_screenSize.x ) {
			m_camPos.x = m_worldSize.x - m_screenSize.x;
		}

		//Check Vertical Boundaries
		if (m_camPos.y < 0) {
			m_camPos.y = 0;
		} else if (m_camPos.y > m_worldSize.x - m_screenSize.y) {
			m_camPos.y = m_worldSize.x - m_screenSize.y;
		}

Thanks. I just wanna understand that this changing the x and y variable that the world image is printed at. Is that right?

Also if you dont mind explaining, what would be the best way to make the player move away from the center of the camera when its on the edge and then recenter when its not?

Image

Lets say the following about the image.

Blue Square -> World Size
Red Square -> Screen Size and Camera
Pink Cross -> Players Centre Point

When the Player moves to the left the camera moves to the left keeping the player in the centre.
If the cameras x position < 0 or world size x pos
Set the cameras x position to 0

The player will still move left but the camera x position will be set to 0

I could of wrote if (camera xpos + camera width > screen width
set the camera pos to the screenwidth - camera width

I hope this is a better example.

Thank you so much. This makes it a lot easier to do compared to what I was doing before. I was putting the player in the center of the camera which caused for some bugs, but this is clearly a better way.

The only thing I don’t understand is how you make it so that whats inside the camera is on the screen. Or how to make what you see translate. I’ve looked at a lot of source and I just don’t understand what makes the screen move. Is it simply offsetting the x and y of other game objects?

I can give an example of code at some stage today with this working.

Okay I now have something that works. I think I could have got the system where I used world coordinates and the background image would offset the work. But what I saw in a video that the srcrect is where the camera should be being displayed.

I really appreciate your help and I’ve learned a lot about SDL and game development through this.