2d fun part 2

sage

Newbie
Joined
Sep 6, 2003
Messages
97
Reaction score
0
My school is having a game programming competition. Unfortunatly the final entry must be sent in on the 29th. I wont have any screen shots till my next few posts because my new isp hasnt sent me my adsl modem yet... Ill upload it from a cd soon. The game im workign on is a simple 2d platformer(megaman/mario) programmed in c++ using direct draw.

So far I rewrote my map editor and added a few new features. I also plan to emplement layers for rendering backgrounds/foregrounds and triggers for event ei. if player is @ x,y, give medkit...
 
Ive started work on the components for the game.
I started with a quick class that will allow me to log warnings/errors and messages to a log file durring runtime.

BTW: I have yet to take any c++ classes at college so if you see a problems or have any tips..
please please tell me or emial me at dsage84[at]hotmail.com

EDIT: Gasp! I left out error checking... shame on me... Ignore it for now.

Code Snippit #1

Log.h

Code:
#ifndef LOG_H
#define LOG_H

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>

using namespace std;

// Class - CLog: Used for logging messages and errors during runtime to a text file
// Usage: Defaults to "log.txt" - LogMessage() & LogError() both append to EOF
// Dependats: iostream.h fstream.h string.h ctime.h
// Todo: Add priority ei. tab less important things
class CLog
{
    public:
        CLog();
        CLog(const char *file_name);
        ~CLog();
        void LogMessage(const char *buffer);
        void LogMessage(string buffer);
        void LogError(int error);
    private:
		char m_file_name[256];
		char m_buffer[256];
		ofstream logFile;
};

#endif // LOG_H

Log.cpp

Code:
#include <iostream>
#include <fstream>
#include <ctime>

#include "log.h"

using namespace std;

CLog::CLog()
{ 
	strcpy(m_file_name, "Log.txt");
	logFile.open(m_file_name, fstream::out | fstream::app);
}

CLog::CLog(const char *file_name)
{
    strcpy(m_file_name, file_name);
    logFile.open(m_file_name, fstream::out | fstream::app);
}    

CLog::~CLog()
{
	logFile.close();
}

void CLog::LogMessage(const char *buffer)
{
	strcpy(m_buffer, buffer);

    // time stamp
    tm *cur_time;
    time_t long_time;
    time(&long_time);
	cur_time = localtime(&long_time);

    logFile << cur_time->tm_mon << "/" << cur_time->tm_mday << "/";
    logFile << (cur_time->tm_year + 1900) << " " << cur_time->tm_hour << ":";
    logFile << cur_time->tm_min << ":" << cur_time->tm_sec << " - ";
    logFile << buffer << endl;
}

void CLog::LogMessage(string buffer)
{
    buffer.copy(m_buffer, sizeof(m_buffer));

    // time stamp
    tm *cur_time;
    time_t long_time;
    time(&long_time);
	cur_time = localtime(&long_time);

    logFile << cur_time->tm_mon << "/" << cur_time->tm_mday << "/";
    logFile << (cur_time->tm_year + 1900) << " " << cur_time->tm_hour << ":";
    logFile << cur_time->tm_min << ":" << cur_time->tm_sec << " - ";
    logFile << buffer << endl;
}    

void CLog::LogError(int error)
{
    // time stamp
    tm *cur_time;
    time_t long_time;
    time(&long_time);
	cur_time = localtime(&long_time);
	
	logFile << cur_time->tm_mon << "/" << cur_time->tm_mday << "/";
    logFile << (cur_time->tm_year + 1900) << " " << cur_time->tm_hour << ":";
    logFile << cur_time->tm_min << ":" << cur_time->tm_sec << " - ";
    logFile << "Error: " << error << endl;
}

Usage - main.cpp

Code:
#include <iostream>
#include "log.h"

using namespace std;

int main()
{
    const char message[] = "Const char buffer.";
    char message2[] = "Non-const char buffer.";
    string message3("Sting message");

    CLog Log;
    Log.LogMessage(message);
    Log.LogMessage(message2);
    Log.LogMessage(message3);
    Log.LogMessage("Static Message.");
    Log.LogMessage("Next message will be an error message.");
    Log.LogError(0123);

    return 0;
}
 
looks good to me, but then again, I didn't take cpp at uni either.

have fun :p I wish we have some contests here .. (in the forums)
 
Personally I'd start using the standard string class for internal stuff. There's no need to use char* unless you are using an output function that requires it, in which case you can call String.c_str().

The standard library is your friend. I'll try and post some more about this later.
 
but passing string in parameters is ugly ..
 
hasan said:
but passing string in parameters is ugly ..
?? Care to explain...

If you're worried about calling it's copy constructor, you could always pass a pointer to it.
 
Im starting to get around to file parsing for my cfg files...

Code Snippit #2

Config.h
Code:
#ifndef CONFIG_H
#define CONFIG_H

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class CConfig
{
    public:
        CConfig(string strFileName);
        ~CConfig();
        string GetParameter(string strParam);
        int GetIntParameter(int iDefault, string strParam);
    private:
        ifstream ConfigFile;
        string m_strFilename;
        string m_strBuffer;     
        char m_szBuffer[256];
};

#endif
 
Also trying to hammer out game events like physics

Code Snippit #3

GameEvent.h

Code:
#ifndef CGAMEEVENT_H
#define CGAMEEVENT_H

#include <iostream>
#include "CGameEvent.h"
#include "CTrigger.h"
#include "CPlayer.h"

using namespace std;

class CGameEvent
{
    public:
        CGameEvent();
        ~CGameEvent();
        void CheckTriggers(CPlayer Player, CTrigger Trigger);
        void UpdatePhysics(CPlayer Player); // add objects/enemies/etc..
};    

#endif
 
I finished the main part of the cfg file parser... take a look.

It opens the file goes line by line eats white space. Next it looks for the name of the variable then looks after the equal sign ('=') then it is converted to what it needs to be(int/string). Next i need to work on saving variable to the cfg file, add more conversions and add in comments.

Code Snippit #4

main.cpp - config parser test

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class CConfig
{
    public:
        CConfig(string strFileName);
        ~CConfig();
        string GetParam(string strParam, string strDefault);
        int GetIntParam(string strParam, int nDefault);
    private:
        fstream ConfigFile;
};    

CConfig::CConfig(string strFileName)
{
    ConfigFile.open(strFileName.c_str(), fstream::in);
}

CConfig::~CConfig()
{
    ConfigFile.close();
}

string CConfig::GetParam(string strParam, string strDefault)
{
    char szBuffer[256];
    string strBuffer;
    
    while(!ConfigFile.eof())
    {
        ConfigFile.getline(szBuffer, 256);
        strBuffer = szBuffer;
        
        if(strBuffer.find_first_not_of(" ") == string::npos)
        {
            continue;
        }
        while(strBuffer.find_first_of(" ") != string::npos)
        {
            strBuffer.erase(strBuffer.find_first_of(" "), 1);
        }
        if(strBuffer.substr(strBuffer.find_first_not_of("="), strBuffer.find_first_of("=")) == strParam)
        {
            return strBuffer.substr(strBuffer.find_last_of("=")+1, (strBuffer.find_last_of("=") - strBuffer.find_last_of('\n')));
        }   
    }
    return strDefault;    
}        

int CConfig::GetIntParam(string strParam, int nDefault)
{
    char buffer[80];
    string strDefault;
    strDefault = itoa(nDefault, buffer, 10);
    return atoi(GetParam(strParam, strDefault).c_str());
}    

int main()
{
    char *name;
    int pMHp, pMMp = 0;
    int pCHp, pCMp = 0;
    CConfig Config("config.cfg");
    strcpy(name, Config.GetParam("name", "default").c_str());
    pCHp = Config.GetIntParam("pMHp", pMHp);
    pCMp = Config.GetIntParam("pMMp", pMMp);
    cin >> test;
    return 0;
}
 
I really suck at making tiles... if you have a few tutorial or site dont hesitate to send them my way :( .

Basic test tileset
default.bmp


Utility tileset (for selection)
Utility.bmp
 
Map Editor Remake!

After some work I added in the features I wanted. Its still very WIP. Im having second thoughts about not using mfc. I would like some opinions of mfc vs straight win32 coding. Thanks.

map%20editor%20v2.JPG
 
I started work on a little demo to try out different collision schemes. Also im going to make a little fighting game with it. I allready coded in physics using floats and I used the window frame as a collision box. I plan to have an animated background and two players and if I have time a font engine.
 
Im working on the ddraw text engine tonight. So far I have a few things planned out. Read from string or char array translate to ascii number and look it up onj the bmp. Next apply it to a surface then blt it to the x,y on the back buffer. I plan on expanding the engines capabilities later to enable word wrap etc.. when its needed.

Code:
// 32 - 27
/* 
 !"#$%&'()*+,-./012356789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]
^_`abcdefgijklmnopqrstuvwxyz{|}~
*/
 
Some thought on player movement...

int jump_lock = 0;

Code:
if(player is pressing the jump key)
{
	playerYv += 16; // whatever...
	jump_lock = 1;
}

if((player is on ground) && (player is not holding the jump key))
{
	jump_lock = 0;
}
 
cant edit last post but it should read..

if((player is pressing the jump key) && (jump_lock == 0))
{
playerYv += 16; // whatever...
jump_lock = 1;
}
 
re-coded some stuff... major code updates.

log.cpp
Code:
#include <iostream>
#include <fstream>

#include "log.h"

using namespace std;

CLog::CLog(const string &filename)
{
    LogFile.open(filename.c_str(), fstream::out | fstream::app);
}

CLog::~CLog()
{
	LogFile.close();
}

void CLog::LogMessage(const string &buffer)
{
    LogFile << buffer.c_str() << endl;
}    

void CLog::LogError(const int error)
{
    LogFile << "Error: " << error << endl;
}

log.h
Code:
#ifndef LOG_H
#define LOG_H

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Class - CLog: Used for logging messages and errors during runtime to a text file
// Usage: Defaults to "log.txt" - LogMessage() & LogError() both append to EOF
// Dependats: iostream.h fstream.h string.h ctime.h
// Todo: Add priority ei. tab less important things
class CLog
{
    public:
        CLog(const string &strFileName);
        ~CLog();
        void LogMessage(const string &buffer);
        void LogError(const int error);
    private:
		ofstream LogFile;
};

#endif // LOG_H

Log Outpt - Log.txt
Code:
Const char buffer.
Non-const char buffer.
Sting message
Static Message.
Next message will be an error message.
Error: 83
 
When the class goes out of scope it will call the deconstructor (with the prefix "~") and it will auto-close the file I opened to output my log test.
 
i only read part 2 of this, but i wonder how the code snippets should inform me more about how things are going. I see a nice class that you wrote there, i bet part 1 (if i can find it) tells me more about this?
 
Updates to come soon...

I plan on posting up some pics of what ive been doing lately. I have a simple player/collision/physics demo ive been playing with.
(a blue square that can run around and jump and stuff :burp: Maby ill make a little two player demo out of it? Anyhow I have been using ints up until this point for the players cords. Also I plan on posting up my new font/text ddraw class soon.
 
Looks good, but oh god does C++ look ugly after programming in Java, Python and Boo.
 
C++ rules imo. Java and all other languages still did not make a revolution to abandon C/C++. Long live C/C++!
 
SLH said:
?? Care to explain...

If you're worried about calling it's copy constructor, you could always pass a pointer to it.

I think he means passing c-style strings is ugly.

Personally, I think using c-style strings at all in c++ code is ugly, unless you have a good reason. And there are good reasons.

I prefer passing a const reference to a std::string.
 
Ive changed my style drastically since I started this little project. Im using namespaces, operator overloading, etc.. I now pass classes by const reference when needed. I use inclusion guards and dont use "using namespace std;". I am surprised how much one project can change your style. Also I quit using hungarian notation.
 
Here are some more little projects I have been working on...
I played with winsock and with my font class. Next on my list is sprites and animation.


IRC Console Client
console%20irc.JPG


and a frames per second test
font-eng-fps.jpg
 
Back
Top