hey everyone,
not really sure how to make the screen move along with my player in a side-scroller. I searched it in this forum and found some references to a camera, but I am not sure how to implement this.
By the way I am using Mappy for my tiled background
Currently I am using this:
the problem here is that when my player passes that certain pixel on the right, the entire screen continuously shifts until it hits the end of the map. I wanted something similar to most platformers, where after a certain point on the screen the camera jumps that much distance and no more 
Thanks everyone
Basic camera algorithm :
int camerax = playerx - SCREEN_WIDTH/2; int cameray = playery - SCREEN_HEIGHT/2; if (camerax < 0) {camerax = 0;} if (camerax >= MAP_WIDTH - SCREEN_WIDTH) {camerax = MAP_WIDTH - SCREEN_WIDTH;} if (cameray < 0) {cameray = 0;} if (cameray >= MAP_HEIGHT - SCREEN_HEIGHT) {cameray = MAP_HEIGHT - SCREEN_HEIGHT;}
Then you render everything at (x - camerax , y - cameray).
Thanks for the help. Just a question though, you said to render at (x - camerax, y- cameray). What is x and y? I tried using player.x - camerax, and that crashed immediately
Forgot to say you draw the world at (-camerax , -cameray).
As to why it crashes, I don't know. Use a debugger. Or show more code.
Thanks for the help. Unfortunately I still can't get it to work 
I tried the (-camerax, -cameray), but my Player and the screen were moving incorrectly in the opposite directions. Your algorithm helped a ton, but my issue is speed I think. Whats happening is that my player moves to the right of the screen (and eventually off of it) much quicker than the screen gets to adjust to his place.
Edgar: don't teach him to abuse C's whitespace rules like that. 
The block that follows an if clause should go on the next line. Not the same one.
Bad:
if ((foo || bar) && frotz) fnord = baz; // Where does the if statement end?
Good:
if ((foo || bar) && frotz) { // Much more readable! fnord = baz; }
allegronewb, You should look into using the ALLEGRO_TRANSFORMs as a camera. Something as simple as this will work nicely:
ALLEGRO_TRANSFORM camera_transform; ALLEGRO_STATE previous_state; al_store_state(&previous_state, ALLEGRO_STATE_TRANSFORM); al_build_transform(&camera_transform, -camera_x+SCREEN_W/2, -camera_y+SCREEN_H/2, 1, 1, 0); al_use_transform(&camera_transform); // draw all your world's stuff here, in regular world coordinates // ... al_restore_state(&previous_state);
If you then set the camera_x and camera_y to your player's coordinates, it will follow him. (I didn't test the above code, but it should work).
if ((foo || bar) && frotz) fnord = baz; // Where does the if statement end?
I don't have trouble reading it, but if you're stepping through the code with a debugger you can't see intermediate results.
I feel like an idiot here but I'm still not getting it working. I feel like everything is working at its barebones, but my Player still moves too fast for the Screen. Player ends up going off the screen by the time the screen catches up.
Could you attach the all of your code (and the files)?
Of course! Thanks. Also, I want to thank everyone else for contributing, seriously. I used to be a math tutor and would often get frustrated if someone didn't understand a topic quick enough (Didn't say I was a good tutor haha) but thanks for bearing with me.
Everything is inside the main.cpp, but to compile you would also need the the .c and the .h files I have attached. Also the image file I have attached from Mappy.
Thanks again
The easiest solution is to render your player (DrawPlayer()) relative to the camera:
void DrawPlayer(Player &player, float camerax, float cameray) { al_draw_filled_rectangle(player.x - player.bx - camerax, player.y - player.by - cameray, player.x + player.bx - camerax, player.y + player.by - cameray, al_map_rgb(0,0,255)); }
A more complex solution would involve an ALLEGRO_TRANSFORM as a camera but things could get a little strange since you're using the MapDrawBG for offsets and clipping, so I would just do the above.
Wow...that's perfect. Just put it into the code and it works like a charm. Definitely going to study it a little bit more to see what I was doing it wrong. Thanks a lot, everyone
Edgar: don't teach him to abuse C's whitespace rules like that.
Meh. I was being lazy and using forum type. And yeah, it makes it a bitch to debug.