Moving camera in a 2D game

Hi guys, I'm trying to create a simple 2d game using c++ and SDL, I've made this function for testing purpose:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void drawTestGrid(Game* GAME,int xOff,int yOff,int lines = 50, int squares = 50)
{
	int xSize = 64;
	int ySize = 64;
	int lastColumn{};
	int r{ 0 }, g{ 0 }, b{ 50 };
	SDL_Rect fill{ 0,0,xSize,ySize };

	//Fill Squares
	for (int row = 0; row < squares;)
	{
		for (int column = 0; column < squares - 2;)
		{
			fill.x = xSize * (column - xOff);
			fill.y = ySize * (row - yOff);

			r = (((column + 1) * 11) % 256);
			g = (((row + 1) * 11) % 256);
			SDL_SetRenderDrawColor(GAME->renderer(), r, g, b, 255);
			SDL_RenderFillRect(GAME->renderer(), &fill);

			if (column == lastColumn)
			{
				++lastColumn;
			}

			row++;
			column--;
			if (column < 0 || row > squares)
			{
				row = 0;
				column = lastColumn;
			}
		}
		break;
	}

	//Draw lines
	SDL_SetRenderDrawColor(GAME->renderer(), 0, 130, 200, 255);
	for (int i = 0; i < lines; i++)
	{
		SDL_RenderDrawLine(GAME->renderer(), i*xSize, 0, i*xSize, GAME->screenSize().h);
		SDL_RenderDrawLine(GAME->renderer(), 0, i * ySize, GAME->screenSize().w, i * ySize);
	}


which generate this grid -> http://i.imgur.com/JVWdUME.png
in order to help me understand if I'm changing something inside the view,xOff and yOff which I'm passing to it are hooked up to the left-right up-down keys, and when I press the image does indeed appear to move around.
So my question is, is this the proper way to do it?
If I have many sprites most of which out of screen, should I basically iterate trough each one of them and offset their position based on those 2 value controlled by the arrows keys?

I am not sure because basically if my character/sprite take 1 step to the right, I'm then shifting the position of every other single sprite around,so... If you do games for a living, pls let me know what the right way is ^_^
Last edited on
Topic archived. No new replies allowed.