C++ Game Development

Moderators: Lundin
Number of threads: 238
Number of posts: 519

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
class Polygon suggestions? Posted by Madifier on 9 Jul 2008 at 2:08 PM
I've been working on a game idea for awhile now and I'm having difficulty with makeing and drawing polygons. I've had success with what I made, but the foundation is weak. Basically, the NODE holds the information, the Edge will point to two known NODEs and for future development the FACE will point to three EDGES.

/***************/
//From file Polygon.h
class Polygon
{
public:
struct Nodes
{
double x;
double y;
double z;
Nodes( double x0 = 0, double y0 = 0, double z0 = 0 ): x(x0), y(y0), z(z0) {}
};
struct Edges
{
Nodes * start;
Nodes * end;
Edges( Nodes * a = NULL, Nodes * b = NULL ): start(a), end(b) {}
};
public:
Polygon( Nodes * n = NULL, int n_num = 0, Edges * e = NULL, int e_num = 0 ): nodes(n), nodes_num(n_num), edges(e), edges_num(e_num) {}
~Polygon();
void draw( Monitor * m );

Nodes addNode( const Nodes & n );
Edges addEdge( const Edges & e );
void subNode( Nodes & n );
void subEdge( Edges * e );

void setNodes( Nodes * x, int a );
void setEdges( Edges * x, int a );

Nodes * getNodes() { return nodes; }
Edges * getEdges() { return edges; }
int getNodesNum() { return nodes_num; }
int getEdgesNum() { return edges_num; }

private:
Nodes * nodes;
int nodes_num;
Edges * edges;
int edges_num;
};

/************/
//From file Polygon.cpp

#include "Polygon.h"

Polygon::~Polygon()
{
delete [] nodes;
delete [] edges;
}


void Polygon::draw( Monitor * m )
{
for( int temp = 0; temp < edges_num; temp++ )
m->DrawLine( int( edges[temp].start->x ), int( edges[temp].start->y ),
int( edges[temp].end->x ), int( edges[temp].end->y ),
WHITE );
}

Polygon::Nodes Polygon::addNode( const Nodes & n )
{
Nodes * temp = new Nodes[ getNodesNum() + 1 ];

int a = 0;
for( a = 0; a < getNodesNum(); a++ )
{
temp[a] = getNodes()[a];
}
temp[a] = n;
setNodes( temp, getNodesNum() + 1 );

return temp[a];

}

Polygon::Edges Polygon::addEdge( const Edges & e )
{
Edges * temp = new Edges[ getEdgesNum() + 1 ];
int a = 0;
for( a = 0; a < getEdgesNum(); a++ )
temp[a] = getEdges()[a];
temp[a] = e;

setEdges( temp, getEdgesNum() + 1 );

return *temp;
}


void Polygon::subNode( Nodes & n )
{

}
void Polygon::subEdge( Edges * e )
{

}



void Polygon::setNodes( Nodes * x, int a )
{
delete [] nodes;
nodes = x;
nodes_num = a;
}
void Polygon::setEdges( Edges * x, int a )
{
delete [] edges;
edges = x;
edges_num = a;
}


Any suggestions has to what I might be doing wrong? Or maybe changes to make it better? The more universal the better.
Report
Re: class Polygon suggestions? Posted by bilderbikkel on 13 Jul 2008 at 10:54 AM
Sure.

It looks as if you've coded everything by hand. If you want to consider using classes designed by experts, you might want to follow the following advices:
- Your Node class has the same functionality as boost::tuple<double,double,double>. Why not use that one? If you do, every expert program immediately understands your node class
- You manage the memory allocation of Node manually. Why not use a std::vector<Node> ? It at least saves you the headaches of memory management (by the way, you have made errors you are not aware of yet there (think of what would happen if you copy a Polygon and one of these would go out of scope) ) (by the way, it will also improve the speed of your program, as your Polygon::addNode is programmed more naive then std::vector<T>::push_back).

See ya, Bilderbikkel



Good luck,
bilderbikkel
Report
Re: class Polygon suggestions? Posted by Madifier on 12 Jan 2009 at 11:41 AM
Thanks for the info, I'll take a look at it. I've had a hard time in the past with implementing other people's code into my own. That's why I write all my own stuff. Still... I'm making progress.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.