Start

posted in 2D MO Game
Published July 25, 2012
Advertisement
Today, I begin my journey as a novice programmer.

A little about me: I am currently in school, but not taking any programming classes (yet).
Hobbies: Art, Music, Tennis, and now Game Programming
Why did I choose to program? It makes me happy inside to feel accomplishment, especially if it took a lot of effort.
Also, video games happen to blend my two favorite subjects, art and music, along with my intillectual personality.
Gender: I know you may question it after the previous statements, but I'm male.
Previous experience: It should be noted that I have little previous coding experience... in any language. In fact, all that I
have done is read this book, skimmed a couple chapters from this one, and looked at the first three chapters under
DX10 from this site. I have been involved in...emulation...and have in the past compiled pre-coded projects, but that
doesn't really count as experience. I looked at joining the GameMaker community a long long time ago, but I never
even got to downloading it.

The endgoal of my trek is to create a working, 2d MO game, and to DOCUMENT it. I will be tackling this from the absolute beginning... well, almost the absolute beginning. Other goals: I will attempt to keep the log here updated, and I will consider releasing a build, or even source, for each update or two. Then, if I succeed, or make substantial process, others can follow my tracks.

To start from scratch here means to nearly completely start from scratch. This particular project will be using no pre-coded engine.
Now to begin the real process. Normally you would start with buying a computer, but that is already taken care of for me, and the project will be done on a nice Windows 7 Home Edition x64.
So the first thing I need to do is... pick a language. If you look at any of those links, or notice that I mentioned DirectX, you will have realized that the game will be in C++. And it shall. But I will not be using DirectX.
Step two of my master plan: Pick a graphics API. I choose libSDL, a simple API that eventually leads into OpenGL. I have no SDL or OpenGL experience, and as I said before, I have only read a little about DirectX, and it was about 3d (as opposed to Direct2D).
Three: choose an IDE. I choose you, VS2010!
Four: choose a networking API: I elect winsock, because it won the coin flip.

Now that we have the introduction over with and general direction technologywise, I need to write a basic plan for the game itself. Everybody knows that a programmer is lost without a design to follow, even if the designer is himself.
The game will start out very simple, I will create a server-client system, so that the game can support a theoretically limitless population... well, I wouldn't say limitless, but it beats peer-peer for a multiplayer online game. Here is the end game, dreamed up to perfection and shined past reality:
A server application is run, and asks for a port to run on. The port is specified by the user, and the server loads a map from some sort of database file(s) and accepts incoming client connections, and authenticates them through a primitive logonserver. The server receives packets or something of the like (I know nothing about networking [yet]) designating where the client has moved, checks for collision, and returns a packet to tell the client whether they can move or not. This will be a periodic process, and there will be at least a two objects that are found in the game: a wall object, and a goal object. The purpose of these two objects is not to make this project a maze game, but rather to keep it simple. This can all be elaborated upon later, but they will likely be simplified more, to escape more work.
A client application, and eventually two identical ones, will be run from either the same or seperate computers in the same local network. They will ask for logon information, likely just a name, and connect to the logonserver to authenticate. They will then be received into the game at a starting point, and will poll for user actions. It will be possible to move up, down, left, and right. The client will draw the scene onto the window, and periodically send packets to the server regarding game mechanics such as collision. Simultanously, the client will use its own collision detector as a default and whenever packets are received from the server, the server has authority. This is to prevent lag and hacks, but will probably be scrapped due to difficulty or prove innefective. The client will have a single button or key shortcut that will log the client out, and quit.
Remember how I said it would be simple? For a game, that is extremely simple. For a novice programmer such as myself, that plan seems a pretty good start for a half decent game, especially for one written from scratch. It won't be easy, but it will be a great accomplishment when done (which if you remember well makes me happy inside).
Let's get started!

-First things first, install Mozilla Firefox because it is better. To be a programmer, you need top-of-the-line technology. That's why I have a five year old laptop to code this on.
-Install Visual Studio C++ 2010
-Download libSDL, version 1.2, the file was near the bottom "SDL-devel-1.2.15-VC.zip (Visual C++)"
-I extracted the SDL-devel-1.2.15-VC.zip to "C:/"
-Now, I'm going to pretend that I didn't read that one book I told you about and go to http://www.cplusplus.com/doc/tutorial/. I downloaded the thing as a pdf and read the entire thing. It took a long time, about an hour. But you should probably spend a few days on it. Now I've read two books about C++. I am nowhere near programming a game.
-After doing all of those tutorials from cplusplus.com, we can start learning some SDL. At this point, I delete all of the tutorial projects I had clogged my projects directory with, and visit lazyfoo.net. I also download the SDL API Reference from libSDL's site. I downloaded the SDLRef.chm, right clicked it, clicked properties, and unblocked it. That took way too long to figure out.
-On lazyfoo.net, they tell us how to set up SDL! I am so excited. I run their little lesson two example, read the next seven examples, and X out that window.

-Now, with the SDL API reference at my side, and the internet to search if I ever forget C++ basics, I am ready to try and begin. I start my project, named "MO", for Multiplayer Online. I can rename it later, I suppose. If you followed me into lazyfoo.net, apparently SDL projects are empty projects. So I create an empty project, and add a source file: main.cpp.
-Time to think of a framework. Our goal calls for a client who can connect to a server, then handle graphics and input. I made a picture (it took longer than I would have thought)


Framework 7-25.png
Don't worry, this empty picture will fill up as time goes by.

-So, source file main.cpp. Let's start coding.

// Main Function
int main(int argc, char* args[])
{
return 0;
}

This is the naked, required function. Nothing more. I check that SDL will load properly by adjusting the code just a little...

//////////////////////////////////////////////////////////////////////////////////////////
// Filename: main.cpp
//////////////////////////////////////////////////////////////////////////////////////////
//////////////
// INCLUDES //
//////////////
#include "SDL.h" // Temporary include
// Main Function
int main(int argc, char* args[])
{
// Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Quit SDL
SDL_Quit;

return 0;
}

But hold on, we need to practice good habits, even if we are only toying around like this. SDL has error handling functions, so says the Reference. We can replace that SDL_Init() with this:

if(!(SDL_Init(SDL_INIT_EVERYTHING)))
{
fprintf(stderr, "Could not initialize SDL: %s.\n", SDL_GetError());
return 1;
}


-I have to wonder whether I need to include stdio.h, but there were no compiler errors and I have no intention of keeping this all in the main function so I will let it pass. Compile, run, does nothing. Success.
-Let's encapsulate everything inside a class. To be specific, create a new header file: sysclass.h
-This class header will be responsible for the entire program, that's what encapsulation does. We will give it a blank constructor, a blank deconstructor, an initialization function, a shutdown funciton, and a loop function.

[source lang="cpp"]//////////////////////////////////////////////////////////////////////////////////////////
// Filename: Sysclass.h
//////////////////////////////////////////////////////////////////////////////////////////

#include "SDL.h"

class SysClass
{
public:
SysClass();
~SysClass();

bool Initialize();
void Shutdown();
void Loop();
};[/source]
For extra protection, let's wrap this in an #ifndef
[source lang="cpp"]//////////////////////////////////////////////////////////////////////////////////////////
// Filename: Sysclass.h
//////////////////////////////////////////////////////////////////////////////////////////
#ifndef _SYSCLASS_H_
#define _SYSCLASS_H_

//////////////
// INCLUDES //
//////////////
#include "SDL.h"

//////////////////////////////////////////////////////////////////////////////////////////
// Class name: SysClass
//////////////////////////////////////////////////////////////////////////////////////////
class SysClass
{
public:
SysClass();
~SysClass();

bool Initialize();
void Shutdown();
void Loop();
};

#endif[/source]
Here is the base class I made
[source lang="cpp"]//////////////////////////////////////////////////////////////////////////////////////////
// Filename: sysclass.cpp
//////////////////////////////////////////////////////////////////////////////////////////
#include "sysclass.h"

SysClass::SysClass()
{
//
}

SysClass::~SysClass()
{
//
}

bool SysClass::Initialize()
{
//
}

void SysClass::Shutdown()
{
//
}

void SysClass::Loop()
{
//
}[/source]
So SDL must be initialized from SysClass->Initialize(), and quit from SySClass->Shutdown().
Simple enough.

[source lang="cpp"]//////////////////////////////////////////////////////////////////////////////////////////
// Filename: sysclass.cpp
//////////////////////////////////////////////////////////////////////////////////////////
#include "sysclass.h"

SysClass::SysClass()
{
//
}

SysClass::~SysClass()
{
//
}

bool SysClass::Initialize()
{
if(!(SDL_Init(SDL_INIT_EVERYTHING)))
{
fprintf(stderr, "If there is an error, it will appear here: %s.\n", SDL_GetError());
return 1;
}

return 0;
}

void SysClass::Shutdown()
{
SDL_Quit();
}

void SysClass::Loop()
{
SDL_Delay(2000); //Replace the main loop with a 2 second delay, then proceed to shutdown
}[/source]

-I compile and run, no errors biggrin.png
Just a program which does nothing for two seconds.

Well, I'm tired, and feel pretty good about this after two and a half hours. No graphics, no sound, does nothing yet. Here is the source so far:

MO_7-25.zip

-Reloque
2 likes 1 comments

Comments

Servant of the Lord
Welcome to GameDev.net, and good luck on your two journeys (learning to program, and making a 2D online game)!
July 25, 2012 05:52 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Start

1347 views
Advertisement