Sign in to follow this  

Tips on some code ....

This topic is 3484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code is not yet completed. The purpose of this post is to aid in the development in a larger project. The aim of this code is to design a generic OpenGL Shapes using polygons, quads, spheres, etc. The next posing will probably be dealing with multi-threading & or scripting. Common_objects.h -> namespace _objects{ /* --------------------------------------- ---------- Rendering Object ----------- --------------------------------------- */ class _robject{ public: void SetName( char *Name ){ name = Name; } virtual void Render(); private: char *name; // Object Name }; } Common_objects.cpp -> #include "common_objects.h" void _objects::_robject::Render(){} Common_geom.h -> #include "common_objects.h" #include <iostream> namespace _cGeom{ enum Side{ _top, _bottom, _front, _back, _left, _right }; class _color3f { public: float r, g, b; }; class _vert3f { public: _vert3f(){} // _vert3f( float x1, float y1, float z1 ) : x(x1), y(y1), z(z1) {} ~_vert3f(){}; void Set( float x1, float y1, float z1 ){x = x1; y = y1; z = z1; } float GetX() const { return x; } float GetY() const { return y; } float GetZ() const { return z; } private: float x, y, z; _color3f color; }; // template<class T> class _shape : public _objects::_robject { public: _shape(){} ~_shape(){} virtual void SetPos(){}; virtual void SetSize(){}; }; class _quad : public _shape { public: void SetPos( float x, float y, float z ); void SetSize( float sizef ) { size = sizef; } virtual void Render(); void InitQuad( Side side); // If it is part of a box, then set a side. // void Init( ); private: float size; // Size of the quad; _vert3f vertex[4]; // 4 Sets of vertexes }; class _box { public: _box( float bSize ); ~_box(); virtual void Render(); private: _quad sides[6]; }; } Common_geom.cpp -> #include <windows.h> #include <gl\gl.h> #include "common_geom.h" void _cGeom::_quad::InitQuad(_cGeom::Side side) { switch( side ) { case _top: this->vertex[0].Set( size, size,-size ); this->vertex[1].Set(-size, size,-size ); this->vertex[2].Set(-size, size, size ); this->vertex[3].Set( size, size, size ); break; case _bottom: this->vertex[0].Set( size,-size, size ); this->vertex[1].Set(-size,-size, size ); this->vertex[2].Set(-size,-size,-size ); this->vertex[3].Set( size,-size,-size ); break; case _front: this->vertex[0].Set( size, size, size ); this->vertex[1].Set(-size, size, size ); this->vertex[2].Set(-size,-size, size ); this->vertex[3].Set( size,-size, size ); break; case _back: this->vertex[0].Set( size,-size,-size ); this->vertex[1].Set(-size,-size,-size ); this->vertex[2].Set(-size, size,-size ); this->vertex[3].Set( size, size,-size ); break; case _left: this->vertex[0].Set(-size, size, size ); this->vertex[1].Set(-size, size,-size ); this->vertex[2].Set(-size,-size,-size ); this->vertex[3].Set(-size,-size, size ); break; case _right: this->vertex[0].Set( size, size,-size ); this->vertex[1].Set( size, size, size ); this->vertex[2].Set( size,-size, size ); this->vertex[3].Set( size,-size,-size ); break; } } void _cGeom::_quad::Render() { glBegin(GL_QUADS); for(int i = 0; i < 4; i++ ) { glVertex3f( this->vertex[i].GetX(), this->vertex[i].GetY(), this->vertex[i].GetZ() ); } glEnd(); } _cGeom::_box::_box( float bSize ) { for( int i = 0; i < 6; i++ ) { sides[i].SetSize( bSize ); if(i == 0)sides[i].InitQuad(_top); if(i == 1)sides[i].InitQuad(_bottom); if(i == 2)sides[i].InitQuad(_front); if(i == 3)sides[i].InitQuad(_back); if(i == 4)sides[i].InitQuad(_left); if(i == 5)sides[i].InitQuad(_right); } } _cGeom::_box::~_box() { } void _cGeom::_box::Render() { for( int i = 0; i < 6; i++ ) this->sides[i].Render(); } ///////////////////////////////////////// ////////////////// EoF ////////////////// ///////////////////////////////////////// Please post any suggestions or if you would like a copy of the lib.

Share this post


Link to post
Share on other sites
Why the hell is every identifier name starting with an underscore?

Your "name" member variable has lousy ownership semantics (and to make things worse, it's a pointer). Also, I suspect you wanted to make _robject a structure, and give it a virtual destructor.

Your vector "set" function is useless when you have a constructor and assignment operator. Also, your "color" member is useless.

Your quad needs constructors.

Your box constructor could be written in terms of an array of enumeration values, instead of the dreaded for-switch "idiom".

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Why the hell is every identifier name starting with an underscore?


It's a thing I've been seeing more and more lately. People are finally stopping to use hungarian notation, and for some reason, they're switching to something like this. I've seen a lot of C# / Jiva code where the member variables have an underscore in front of them.

As a note to the original author: why are you doing this? There is no actual reason to do so, because most IDE's will inform you about the scope of a variable. It's also a hell to type and has no added goodness.

Toolmaker

Share this post


Link to post
Share on other sites
Quote:
Original post by Toolmaker
It's a thing I've been seeing more and more lately. People are finally stopping to use hungarian notation, and for some reason, they're switching to something like this. I've seen a lot of C# / Jiva code where the member variables have an underscore in front of them.
It's actually in the C# coding standards. I would quit a job where I had to use it.

Share this post


Link to post
Share on other sites
[opinion]Does make it horrible to read, I think almost worse than the 'classic' hungarian, which at least had a meaning, even if it was pointless and confusing meaning...[/opinion]

I have only done stuff in DX, but I am not sure that having each object handle it's own rendering is exactly performant, although it is nice from an encapsulation POV. As you have written it, every quad has its own personal glBegin and glEnd calls, which I thought were pretty costly - maybe it would be smarter to batch these somehow, maybe using a QuadRenderer object or something which each quad renders itself through, ensuring only one glBegin and glEnd per frame, or at least per geometry type.

maybe each quad you create could add a pointer to itself to the drawing queue, and then only one call to the rendering class could render everything?

Hope this helps
Mathmo

Share this post


Link to post
Share on other sites
I sometimes use an underscore for member variables when I have properties of the same name, as in

Private _score as Integer

Public Property Score as Integer
Get
return _score
End Get
End Property

Share this post


Link to post
Share on other sites
imho:

- another vote get rid of the underscores.

- name should be a std:string not char*. If your C++ code has "char *" in it, you have a bug (that's in someone's sig around here...)

Share this post


Link to post
Share on other sites
It says if you post in the Beginner's Forum and you have char* in your code, you generally have a bug, but they way you worded is definitely not true.

Share this post


Link to post
Share on other sites
Just a tip about underscores:

if C++ switch to c99 standard you might get into trouble. C99 reserves underscore for std library and double underscore for compiler writers :).

Otherwise I myself writes more code in C then in C++, and I tend to go against reason and use udnerscore in a certain case: for private API functions (only those that HAS to be shared between different files, but in a library itself). Underscore reminds me that those are not seen from the user application (but I could use some other char indeed).

For user exposed functions I really can't see any use of underscore. It is awkward to type and if you constantly underscore every name, then there is no point of having underscore :D.

Beside what is point of your lib, there are already "prmiitives" for opengl, check out glu and glut libraries.

It is also awkward to have basic objects like vertex or vector or color with setters for their x,y,z types. A vertex is a position and as such x,y,z coordinates belongs to its interface which probably will never change, so there is no point to hide it ... some basic of 3d programming. It is just awkward to do something like

v1.setX(v2.getX());

when you can do this: v1.x = v2.x;

Check also glut and glu libraries, there are already primitives there:

http://resumbrae.com/ub/dms423_f06/09/

And why do you need windows.h in such basic and generic code??? It is just poluting your code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravuya
Quote:
Original post by Toolmaker
It's a thing I've been seeing more and more lately. People are finally stopping to use hungarian notation, and for some reason, they're switching to something like this. I've seen a lot of C# / Java code where the member variables have an underscore in front of them.
It's actually in the C# coding standards. I would quit a job where I had to use it.


Me too. Underscores are stupid.

Share this post


Link to post
Share on other sites
I disagree completely. I just started using underscores a couple months ago, to denote public members and aggregate-only classes. I also use them when I have properties of the same name, as ItsDan pointed out. I do not use them for local variables, procedures, and classes that I do not intend to aggregate. They're not difficult at all to type unless you have poor coordination, especially once you get used to it — to me, shift-minus is just another keystroke — and once your mind absorbs what they mean in context, code becomes much easier to follow. In the end, the people who hate underscores are the people who simply refuse to use them, for whatever reason.

Of course, as ToohrVyk and joe_bubamara pointed out, using them for every identifier is contextually pointless and can make your code much more difficult to read. I'd recommend camel-casing whenever possible, except for class and procedure names, where initial caps is the standard, and reference variables (after a requisite underscore, of course); the latter suits my personal taste.

As for structural suggestions, I can't add anything that hasn't already been said. Just wanted to toss in my two cents. Oh, use code tags next time. Posting two or three pages of raw source is really bad form.

Share this post


Link to post
Share on other sites
Quote:
Original post by joe_bubamara
Just a tip about underscores:

if C++ switch to c99 standard you might get into trouble. C99 reserves underscore for std library and double underscore for compiler writers :).

The C++ standard already has restrictions on the use of underscores, though the code posted mostly obeys them since most of the names that start with an underscore are followed by a lower case letter and most are not in the global namespace:
Quote:
17.4.3.1.2 Global names [lib.global.names]
1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

Share this post


Link to post
Share on other sites
Quote:
Original post by mattnewport
Quote:
Original post by joe_bubamara
Just a tip about underscores:

if C++ switch to c99 standard you might get into trouble. C99 reserves underscore for std library and double underscore for compiler writers :).

The C++ standard already has restrictions on the use of underscores, though the code posted mostly obeys them since most of the names that start with an underscore are followed by a lower case letter and most are not in the global namespace:
Quote:
17.4.3.1.2 Global names [lib.global.names]
1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

Macros do not respect scope.

Many implementation-defined elements use macros.

A macro with an underscore will not care what namespace or class scope contains it, it will still perform the replacements.

Share this post


Link to post
Share on other sites
Quote:
Original post by FippyDarkpaw
imho:

- another vote get rid of the underscores.

- name should be a std:string not char*. If your C++ code has "char *" in it, you have a bug (that's in someone's sig around here...)


You rang? :)

Share this post


Link to post
Share on other sites

This topic is 3484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this