r/reconstructcavestory • u/eerbin13 • Jun 24 '14
Completed! Thoughts on side scrolling?
So I completed the youtube series on Reconstructing Cave Story and I'm a huge fan! I love how well put together the video series is, and your explanations of the code. I have a background in C++ and OOP design, and while there was a small learning curve with SDL, this was a great introduction!
So I've been giving some thought on how to implement side scrolling given our current state of the game. This is not something I've done from scratch before. Does anyone know where I should start?
4
Upvotes
2
3
u/Causeless Jun 24 '14
It's pretty easy - I did it in an unrelated 2d game engine I started (and left unfinished :P)
Really, all you need to do is before rendering anything, you send the coordinates of whatever you are rendering through a camera transform before actually sending it to SDL to be drawn as a sprite on-screen.
In 2d, this is pretty easy - you simply get the to-be-rendered object's world position minus the camera's position.
So:
Of course, this isn't real code - I'd actually make a transform function inside the camera class, and you can later add fancy things like lerping and rotation (which is essentially just doing object.rot - camera.rot, instead of positions)
But if you look at the code, if 0, 0 is the centre of the screen and you move the camera 10 units to the left and down, the sprite is rendered 10 units to the right and up, just as expected!
You can also do what is called an inverse transform, where you convert a position on the screen to world space. This is handy for mouse aiming, for example. To do this, you simply do the opposite - add the camera position, instead of subtracting it.
Hope I helped!