Hi everyone,

I have an extremely complex topic to discuss/ask about. I'm hoping what I have already done is the WRONG way to do it, so that I can implement a more simplistic approach to this issue. To start, here is some background information on my project.

I want to create a networked game that uses SDL as the library for graphics. In the most simplistic sense, you will run your client program, and your local client program will maintain the state of any entities around you, including yourself. You will ALWAYS be drawn on the screen.

At this current moment, I have a working structure which can create and destroy objects when I need them, but I am not sure if it is the BEST way to handle this. There is NO networking involved at all yet; The solo client program has hard-coded the creation of a few players.

Here is the code. It creates the game, initializes what would be YOUR character, and creates a few, hard-coded player characters (which would eventually be other players who are currently logged on), draws all of the players as well as yourself to the screen, and repeats:

#include <SDL.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>


#include "Player.h"
using namespace std;

int main(int argc, char* args[])
{
	ofstream log;
	log.open ("log.txt");

	const int SCREEN_WIDTH = 640;
	const int SCREEN_HEIGHT = 480;
	const int SCREEN_BPP = 32;

	SDL_Surface* screen = NULL;

	bool quit = false;
	SDL_Event event;

	/*    INITIALIZATION    */
	SDL_Init(SDL_INIT_EVERYTHING); 

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
	SDL_WM_SetCaption("Sentinel", NULL);
	
	//create vector to store objects
	vector<Player*> Players;
	Player* temp;

	//create 3 objects in vector
	temp = new Player(300, 300);
	Players.push_back(temp);
	Players.back()->Load("enemy.bmp");

	temp = new Player(400, 300);
	Players.push_back(temp);
	Players.back()->Load("enemy.bmp");

	temp = new Player(350, 100);
	Players.push_back(temp);
	Players.back()->Load("enemy.bmp");

	//create player's character and background without using vector
	Player background(0, 0);
	background.Load("background.bmp");
	Player hero(200, 200);
	hero.Load("hero.bmp");

	/*	       MAIN LOOP		*/
	while (quit == false)
	{
		/*		HANDLE USER INPUT		*/
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{	
				case SDL_QUIT:
					quit = true;
					break;
			}
		}
		


		/*        UPDATE         */ 
		SDL_FillRect(screen, NULL, 0x00000000); // clear screen
		background.Draw(screen);
		hero.Draw(screen);
		//step through all players and draw them to the screen
		vector<Player*>::iterator myIterator;
		for(myIterator = Players.begin(); myIterator != Players.end(); ++myIterator)
		{
			(*myIterator)->Draw(screen); //Draw every player character to the screen
										 //Because the "game" is one, small 640x480 map so far,
									     //do not do any distance checks between objects and main player
		}


		/*          DRAW         */
		if (SDL_Flip(screen) == -1)
		{
			log << "Screen could not be flipped\n";
			return 1;
		}
	}

	/*       CLEAN       */
	Players.clear(); //clear the player object vector
	SDL_Quit();	//close SDL
	log.close();  //close the log file
	return 0;
}

Here is an image of this running:
http://img43.imageshack.us/img43/7097/sentinel.png

Back to what I was saying. What I want to be able to do is allow the Server program to maintain the state of ALL objects, and allow the Client program to maintain the state of ALL objects WITHIN VIEWING DISTANCE. As you can see, I was able to come up with a way for the client program to maintain all objects currently within view.

Here is where my uncertainty grows, and so I must ask you all this.

How do I handle the actual maintenance of all viewable players? Do I have the client send a packet to the server that says "nearby players" and have it receive that, and then send back some data for EACH character nearby, so that my client can extract that data (player's name and x, y coordinates) and then turn them into player objects on the client side (in the vector) and then draw all of these objects to the screen?

I'm completely lost, which prevents me from coding anything on the client side because I don't want the game to become too complex as a single player game, and then go back and re-do everything to make it networked.

All I'm trying to achieve at this point in time is networked movement between more than one player in a small 640x480 screen.

Recommended Answers

All 2 Replies

Your code has a memory leak :

Player* temp;
 
	//create 3 objects in vector
	temp = new Player(300, 300); // You ask for memory for temp
	Players.push_back(temp);
	Players.back()->Load("enemy.bmp");
 
	temp = new Player(400, 300); // You again ask for memory for temp.
       //code removed...
       //...
       //...
	temp = new Player(350, 100); //later on you again ask for memory for temp.
       // So the previous memory allocated is lost. You should delete temp before asking for memory.

Yikes, great catch. I thought temp being a pointer, creating a new object would take the place of the same memory location. I guess not!

I appreciate the help. Hopefully some people with experience in creating networked games can come in and provide some information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.