Posts Tagged ‘c++’

Posted by kent at 9 September 2011

Category: c++, Dev

Tags: , ,

Extract the sources to some folder, open the command prompt and go to that directory.

Then enter following commands…

> bootstrap.sh
> bjam.exe --toolset=msvc --link=static --runtime-link=static --build-type=complete stage
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 16 March 2011

Category: c++

Tags: , ,

Boost::format’s performance sucks.

I used boost::format in an inner loop in my project, and the optimized runtime was 20 seconds for 1 small case.

I optimized away boost::format by replacing it with stream manipulators and i got the runtime down to 2 seconds for the same case.

I’ll publish an article later on the details on how to do this.

 

VN:F [1.9.13_1145]
Rating: 1.0/5 (1 vote cast)
Share

Posted by kent at 14 March 2011

Category: c++

Tags: , , ,

I always forget how to use boost::format when I need it. So, here are a few samples from when I did remember.

Only need is to include the proper boost header. No compile of any of the boost libraries is necessary.

The source is also available in subversion (webclient) (subversion checkout).


// build with make or:
//
// g++ -I../../boost_1_46_0 fmt.cpp
//

#include <iostream>
#include <sstream>
#include <boost/format.hpp>

using namespace std;
using namespace boost;

int main()
{
    stringstream ss;

    int year    = 2011;
    int month   = 3;
    int day     = 11;
    int hour    = 12;
    int minute  = 1;
    double seconds= 23.35;
    double epoch  = 1231234567890.35;

    // Fill with whitespace, right align
    ss << format("#1.0 %4i %4i %4i\n") % year % month % day;
    // Output: #1.0 2011    3   11

    // Fill with whitespace, left align
    ss << format("#1.1 %-4i %-4i %-4i\n") % year % month % day;
    // Output: #1.1 2011 3    11

    // Fill with zero (pad)
    ss << format("#2.0 %04i %04i %04i\n") % year % month % day;
    // Output: #2.0 2011 0003 0011

    // Floating point precision
    ss << format("#3.0 %11.2f\n") % seconds;
    // Output: #3.0       23.35

    // Floating point precision with zero-fill (zero pad)
    ss << format("#4.0 %011.2f\n") % seconds;
    // Output: #4.0 00000023.35

    // Left aligned string literal with 30+20 columns
    ss << format("#5.0 %-30s%-20s\n") % "LEFT ALIGNED 1" % "LEFT ALIGNED 2";
    // Output: #5.0 LEFT ALIGNED 1                LEFT ALIGNED 2

    // Right aligned string literal
    ss << format("#6.0 %30s%20s\n") % "RIGHT ALIGNED 1" % "RIGHT ALIGNED 2";
    // Output: #6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2

    cout << ss.str();

    return 0;
}

The output should be:

#1.0 2011    3   11
#1.1 2011 3    11
#2.0 2011 0003 0011
#3.0       23.35
#4.0 00000023.35
#5.0 LEFT ALIGNED 1                LEFT ALIGNED 2
#6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2
VN:F [1.9.13_1145]
Rating: 4.0/5 (1 vote cast)
Share

Posted by kent at 22 January 2011

Category: Dev, Tip

Tags: , , ,

This code will open Explorer and select the file or folder given.

static void openInExplorer(string path)
{
    string cmd = "explorer.exe";
    string arg = "/select " + path;
    Process.Start(cmd, arg);
}
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 3 January 2011

Category: Dev

Tags: , , , , ,

static public List<foldersRow> getParentFolders(foldersRow parent)
{
    return (from p in m_Fildb.folders.Where( _p => _p.parent_folder_id == parent.folder_id) select p).ToList();
}

This returns a strongly typed List of type foldersRow.

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 20 October 2010

Category: c++

Tags: , , , , ,

This is a reminder to myself.

To sort a collection of non-POD types (not Plain Old Data) with STL we need to tell it how to sort these objects.

To the std::sort method we simply supply a predicate! It’s just a method taking 2 arguments of that type and apply our logic to tell if the first argument is lesser than the last argument. With templates it becomes a little more complex, but still it’s very simple when you know how it works.

Here is some sample code:

#include <iostream>     // Provides cout
#include <vector>       // Provides our basic container
#include <algorithm>    // Provides std::sort

using namespace std;

// Structures I'll sort with 1 template function
//
//
struct A
{
    A(size_t i, size_t t) : id(i), time(t), other_data(false){}
    size_t  id;
    size_t  time;
    bool    other_data;
};

struct B
{
    B(size_t i, size_t t) : id(i), time(t), other_data("string data"){}
    size_t  id;
    size_t  time;
    string  other_data;
};

struct C
{
    C(size_t i, size_t t) : id(i), time(t), other_data("character array data"){}
    size_t  id;
    size_t  time;
    char*   other_data;
};

// Our predicate
//
template<typename CLASS>
bool timeSortPredicate( const CLASS &a, const CLASS &b )
{
    return a.time < b.time;
}

template<typename CLASS>
bool idSortPredicate( const CLASS &a, const CLASS &b )
{
    return a.id < b.id;
}

// Our collection printer
//
template<typename CLASS>
void print( const CLASS &c )
{
    typedef typename CLASS::const_iterator cit;

    for ( cit it = c.begin(); it != c.end(); ++it )
    {
        cout << it->id << " " << it->time << endl;
    }
}

int main()
{
    // Construct the objects ... tedious I know
    A a1(1,15), a2(2,20),   a3(3,14);
    B b1(3,4),  b2(2,10),   b3(1,12);
    C c1(11,0), c2(12,0),   c3(13,0);

    // Construct collections
    vector<A> alist;
    alist.push_back(a1);
    alist.push_back(a2);
    alist.push_back(a3);

    vector<B> blist;
    blist.push_back(b1);
    blist.push_back(b2);
    blist.push_back(b3);

    vector<C> clist;
    clist.push_back(c1);
    clist.push_back(c2);
    clist.push_back(c3);

    // Print stuff
    cout << "Collection A" << endl;
    print(alist);

    cout << "Collection B" << endl;
    print(blist);

    cout << "Collection C" << endl;
    print(clist);

    cout << "Sorting collections by time" << endl;

    sort(alist.begin(), alist.end(), timeSortPredicate<A> );
    sort(blist.begin(), blist.end(), timeSortPredicate<B> );
    sort(clist.begin(), clist.end(), timeSortPredicate<C> );

    // Print stuff
    cout << "Collection A" << endl;
    print(alist);

    cout << "Collection B" << endl;
    print(blist);

    cout << "Collection C" << endl;
    print(clist);

    cout << "Sorting collections by id" << endl;

    sort(alist.begin(), alist.end(), idSortPredicate<A> );
    sort(blist.begin(), blist.end(), idSortPredicate<B> );
    sort(clist.begin(), clist.end(), idSortPredicate<C> );

    // Print stuff
    cout << "Collection A" << endl;
    print(alist);

    cout << "Collection B" << endl;
    print(blist);

    cout << "Collection C" << endl;
    print(clist);

    return 0;
}

Compile with:

g++ sortpredicate.cpp -o sortpredicate

The output should be:

Collection A
1 15
2 20
3 14
Collection B
3 4
2 10
1 12
Collection C
11 0
12 0
13 0
Sorting collections by time
Collection A
3 14
1 15
2 20
Collection B
3 4
2 10
1 12
Collection C
11 0
12 0
13 0
Sorting collections by id
Collection A
1 15
2 20
3 14
Collection B
1 12
2 10
3 4
Collection C
11 0
12 0
13 0
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 19 August 2010

Category: c++, Dev

Tags: , , , , ,

Okay. This is going to be somewhat personal.

I’m a professional software developer for a medium sized company, which is really not a software company. It’s only internal software I write.

I’ve also wanted to make a game since I started playing games. That’s probably why I’m a software developer today. I’ve been doing more or less C++ since I was 14, doing (at least trying to) 3D-graphics since I was 18. Without knowledge of the particular mathematics involved with 3D-graphics it proved to be difficult.

I’ve started numerous projects with OpenGL, and some are more advanced than others, but I didn’t continue working on a project for more than 2-3 days before I got tired.

I’ve started an other project now. I don’t know what I’ll make, or whether if I’ll make anything. But I’ve started an other project. The only real difference from previous projects is that I know the value of libraries and there is no point in re-inventing the wheel for every project.

Standard template library is there, Boost is there and some other libraries. I know how to use them. And I know they exist!

For this project I know I’m going to use

  • STL
  • The boost libraries
  • Ogre3D for graphics

I’m not so sure about the sound and physics part, but for now I think I’ll stick with

  • Bullet for physics
  • FMOD for sound

I must also review the licenses  for the libraries to make sure they are allowed to be used in commercial projects or not. I’m not saying my project is going to be commercial, but I won’t exclude it. I’ll also aim for Windows and Linux support. And probably Mac if it’s going to be commercial.

So far I have an empty scene with a blue background.

It’s a start! … again

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 16 August 2010

Category: c++, Dev

Tags: , ,

When I get this error, it’s always because I’ve opened the debugging process in Process Explorer.

Error    1    fatal error LNK1201: error writing to program database 'e:\blergh\blergh\bin\blergh_d.pdb'; check for insufficient disk space, invalid path, or insufficient privilege

Close the handle in Process Explorer or restart Process Explorer.

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by kent at 9 April 2010

Category: c++, Dev

Tags: , ,

This little macro prints out user warnings to the output window in a format the “Error List” within Visual Studio will understand and parse. Taken from Stack Overflow and modified a little.

#define STRINGISE_IMPL(x) #x
#define STRINGISE(x) STRINGISE_IMPL(x)

	// Use: #pragma message WARN("My message")
#if _MSC_VER
#   define FILE_LINE_LINK __FILE__ "(" STRINGISE(__LINE__) ") : "
#   define WARN(exp) (FILE_LINE_LINK "warning: " exp)
#else//__GNUC__ - may need other defines for different compilers
#   define WARN(exp) ("WARNING: " exp)
#endif

It will produce output like this:

1>.\src\bug.cpp(82) : warning: check me

When this line is present in the source file.

#pragma message WARN("check me")
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by Tatyana at 2 March 2010

Category:

Tags: , , , , , ,

Templates in C++ are the way functions and classes operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one [wiki].

Note that there is a difference between class template and a template class: class template is a template used to generate template classes. You cannot declare an object of a class template. Template class is an instance of a class template.

Here we are going to look at 3 main clas templates that are very useful in game and 3D design: Point, Vector and Matrix class templates.

Point and Point3D:

Here is how a simple Point template will look like in C++. This template is constructed to support multiple dimensions like Point2D, 3D and so on:

template <class T, int n> class Point {
protected:
	T mat[n];
	 void cp(const Point<T,n> &p)
    {
		memcpy(mat, p.mat, n*sizeof(T) );
	 }

    void cp(const T p[n])
    {
		memcpy(mat, p, n*sizeof(T) );
	}

    void cp(const T &val)
    {
		for (int i=0; i<n; i++)
			mat[i] = val;
    }

public:
	// new constructor
	Point(const T pt[n])
	{
		cp(pt);
	}

	// copy constructor
	Point(const Point<T,n> &cp)
	{
		this->cp(cp);
	}

	// default constructor
	Point()
	{
		for (int i=0; i<n; i++)
			mat[i] = 0;
	}
	virtual ~Point(void) {};
};

Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. Here vi can add multiple operator overloading functions to our Point class template, for example addition:

        //Point + some value
	Point<T,n> &operator+=(const T &val)
    {
		for (int i=0; i<n; i++)
			mat[i] += val;

		return (*this);
    }

    //sum of two Points
    Point<T,n> &operator+=(Point<T,n> &pt)
    {
		for (int i=0; i<n; i++)
			mat[i] += pt[i];

		return (*this);
    }

A Point3D class template will look like this:

template <class T> class Point3D : public Point<T,3> {
public:
	Point3D() : Point<T,3>() {};

	Point3D(const Point3D<T> &p) : Point<T,3>(p) {
		for (int i=0; i<3; i++)
		mat[i] = p.mat[i];
	}
	Point3D( const T px, const T py, const T pz)
	{
		mat[0]=px; mat[1]=py; mat[2]=pz;
	}
};

An example of how we can use it is a motion programming:

	Point3D<float> EulerAngle(float sensitivity, double x, double y, double z) {
		return Point3D<float> (
				sensitivity*cos(z),
				sensitivity*sin(z),
				0
		);
	}
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share