Zaki Mirza’s Blog


… About software and beyond!

“protected” usage

I know the title is weird, but i couldnt think of anything better. So here i was coding one of my CG assignments. It was a simple assignment though im very intrigued by game/software graphics engines so i plunged in to make my own. Its in very basic stages for now. Right now im going to talk about a problem i encountered.

Consider this, you have a class called ‘BaseObject’ which is basically for a graphical object. Now graphical objects are represented by points/vertices and how surfaces/lines are drawn between those vertices. (in my case those were indices of vertices in order. ill tell you what i mean in a sec). Now how i see it the client of the class shouldnt be allowed to create an 3d/2d object without specifing the vertices,indices and the kind of surface/line between the vertices(at the least, because otherwise it doesnt make sense for a graphical object to exists without a single vertex). The Draw() method would take care of drawing. (im using opengl as my graphics api and sdl for windowing and message handling).  So in a very usual case you’d refrain from making a default constructor of the BaseObject class and at the least it would take the array of vertices,indices and the draw method (e.g. GL_POLYGON, GL_LINES etc). So far, the code would be like this:

class BaseObject
GLfloat* _vertices;
short _numVertices;
GLint* _indices;
short _numIndices;
GLint _drawMode;

int numVertices,int numIndices,GLfloat vertices[],GLubyte indices[],GLfloat colors[],GLenum drawMode);virtual void Draw();
 Now consider this. I want to make an specialization of this class, say a SolidCube class. So instinctivly i would inherit our SolidCube class from BaseObject class, let use tell me the center of the cube, the width,height and depth and pass that to base class which will handle the drawing. So how about:

class SolidCube : public BaseObject
SolidCube(GLfloat width,GLfloat height,GLfloat depth,Point3D cp
Now here comes the problem. When you inherit from a class you have to initialize that in the intializer list in the constructor. Given the center point, the height, the depth and width, i know how to calculate the 8 vertices of the cube. But i have no space to do the coding between the constructor definition and the initializer of base class in initializer list. e.g.

SolidCube::SolidCube(GLfloat width,GLfloat height,GLfloat depth,Point3D cp)
: BaseObject(){}
Now we intentionally excluded that default constructor from the BaseObject definition so that noone can make an empty object. (well ofcourse they can using NULL NULL for all the arrays and all, but hey we can sort that out in some other post. this is a lame design i know). So what do we do? Well my little solution was to make use of the “protected” access modifier. It lets you secure the variable/function for the derived classes. In this case, making BaseObject’s defualt constructor protected, i make sure that the clients of the class cant use this constructor but all the derived classes can. So now the above code can work and the derived class can manually set the vertices array after working out the vertices. By the way comming back to why im using indices ill write another post on that just right now.

The visual studio will let you do something like:

BaseObject a();

and if you try to do a.Draw() or anything with “a” it will give you a compile time error. It will give you a warning though when you try to do the above thing.


Filed under: C++, Code, programming

Setting up SDL-OPENGL in Visual Studio

And this CG assignment’s been giving some people pain in the gut so i thought okay why not share this with others. hopefully they’ll learn from it. So here goes.

Well the problem lies in the fact that people cant setup SDL and OpenGL together in Visual Studio (6,7,8).

Here are the steps you can follow to make it all work:

Downloading and setting up SDL:

1. For the Easiest way to do this, (on your home PC) download the sdl libraries and headers from . extract the file (in my case its: hunt the folder where you installled the visual studio. Find the folder (VC98 on VC6… VC7 on .net 2003 and vc8 on .net 2005).

2. in the include folder, create a folder called “sdl” and copy the contents of the “sdl_devel-1.2.11/include” folder ( where you jus extracted the sdl headers). These will be many header files. in other words copy *.* from sdl_devel-1.2.11/include to vc6/include. 🙂

3. in the lib folder (of visual studio) copy *.lib of the “lib” folder in sdl_devel-1.2.11 folder.

4. copy the sdl.dll file in the sdl_devel-1.2.11/lib folder to your windows/system32 folder.
Creating The SDL-OPENGL Project:

1. fireup Visual Studio of your choice.

2. Create a new Windows 32 Console Application. Name it .. i_suck :p

3. Click finish. The visual studio will create a default cpp file with the main() function declared in it.

4. In VC6 goto Project>Settings, in VC7 and VC8 right click the project in the solution explorer and click properties.

5. Under Linker>Input option: include the following libraries:
opengl32.lib glu32.lib sdl.lib sdlmain.lib

6. (OPTIONAL STEP) in the Linker>System option (im not sure if this is in VC6, but it is in VC7. This step is optional. Only if you do not want to show the console window with the graphics window) Set the subsystem to windows (or add /SUBSYSTEM:WINDOWS to the commandline option in the linker). in the linker>advanced option, set the entry point to “mainCRTStartup” or add “/ENTRY:mainCRTStartup” to commandline option of linker.

7. In c/C++ > Code Generation option, set the Runtime Library option to MULTI-THREADED DEBUG DLL.

8. Click ok and the project is setup for opengl and sdl.

The Code:

1. In you code, include the following files:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <sdl/sdl.h>

Note that the ordering of the file should be right. because gl.h file requires that windows.h be included beforehand.

2. build your code. it should be successful. if it isnt, reconsider the above steps.
The Coding Part:
Do it yourself :p. Read the SDL Documentation that came with the SDL libraries and headers. Its right there in the doc folder :p. And ah yes, it will be very comfy once you switch to Visual Studio .net 2003 (and even more to .net 2005, but .net 2003 is more stable. Thats from my experience and mishi and umar manzoor recommend that as well). I have tested these steps on .Net 2003. ( dunno about VC6, i left using it ages ago). Any problems you find drop in a comment. Yes i do take donations as well :p

Filed under: opengl, sdl, Troubleshooter, visual studio

Late-night Blogging

Ah, here again. Bloggin at 2:30 am. :s just completed some unfinished project for someone. that RDP one. hopefully its gotten done with now. and now back to my COM assignment.

I really dont like people who come up on the stage just to tell you “you dont know jack about what you think you know”. Well yeah in a way thats good cuz it tells you what more to learn, but it also gives a sense of ….umm whats the word…. infriority? :s i guess.. but neways id rather pefer that a teacher comes up and teaches as much as he can with the enthusiasm to teach and not to test or tell people that “well, uve come this far but do u know its like a 1000 miles more?”. What would you be thinking then?

There are three kinds o people that i know. the optimistic ones, the realistic ones and the pessimistic ones. The optimistic one would be like….” well, so? ive come so far i can go 1000 miles more…”. the pessimistic one would be more like “ah crap… u kidding me? iv walked my ass off day-in day-out! no way im gonna walk a 1000 miles more”. but i seriously wonder what the realistic guy wud say. maybe something like “well iv come so far, only time can tell if i can be there. iv set out to be there and well i knew it was gonna be like a million miles….so lez c”. In any class there are all of these sorts o guys and girls. The best way i think will work is, teach ur best! really. and motivate and inspire and not give hints of “well i know a lot more than u can ever imagine… and that microsoft interview? forget about it that u can even make it to the fonecalls!”. disheartning :s..

anyway, im more between realistic and optimistic when it comes that. thats not what turns me off. rather i like teachers who know a lot because i know that illl learn a lot as well. Dr. Qasim Shiekh (my algorithm analysis instructor) is one recent example. I took out time to have a lunch with him and i love the way he enjoys his life and inspires others as well. he also has this, innocence of some sort. hes friendly and passionate and believes in knowledge and always yearning for it. one of the best things i like about him is how hes keeping up with our generation even tho people of his age dont really. hes teaching us Google’s PageRank. pretty darn intresting thing theve comeup with. Umar Manzoor is very enthusiastic too about programming. he’s teaching distributed computing. he’s a bit pompous tho, and my earlier comments on this post we’re to do with him. not that i dont like him or his way of teaching. he just scares me :S tells me things that i dont know in a way that “u dont even know thatttt!!!” (mayb it really is that bad. :s i have to work on my concepts). im spoiled 😦 Oh and how can i not mention Ikraam and Uzma maroof, the other two very inspiring teachers. and kashif munir was a GEM! ikraam is in microsoft now, kashif is austria completing his doctrate in distributed computing etc. and mam uzma is still teaching here.

i love teachers who inspire me. They are mostly computing related teachers, but not neccessarily. i loved the way asif gondal taught numerical. (tho i flunked in it :().

enough of what i love, now to what i hate! im pissed today. y? cuz of the people who dont appriciate! who dont appriciate just anything. who dont want to learn. who dont want to indulge. really, have you gone through the time when, you love something, ur kinda good at it and ur passionate about it. someone asks you to teach it to them, or tell about it to them. u start doing it and all you get it “okay man … wahtever….. move on”. Or there is someone, whos not as good and u feel like maybe u shud slowdown and give em a hand and take em along with you and alll they do is push off ur hand and ignore you. Crap! that just makes me more inclined towards making lone groups etc.

Ah ive been missing a lot of CG and Management classes. oh, i had to drop computer architecture thanx to tehmina and humaira. wouldnt go in that detail. now i have management. which happens to be the first class in the morning at 830 and if ur der at 832 ur out:S… here we go into another stressful semester. first tehmina and humaira broke my plans and now this :s plus this 80% attendence in all courses bullshit. i mean…. wahts the point? is a guy with 100% attendence any better than the rest of them? or does he get anything for come in all* classes? or is it garanteed that he’ll learn? i dont see on point in imposing that school-time restriction. oh well… hopefully just one n a half year more :s

back to my work, no sleeeep tonite cuz wont be able to wake up den.

quote of the post:
There is no substitute for intelligence, experience and taste in programming. Bjarne Stroustrup

(ps: next time …. about my recent addiction to msdn blogs and usenet. )

Filed under: Late Night Blog, life

Blog Stats

  • 104,805 landed here so far...
March 2007
« Feb   Apr »


Error: Twitter did not respond. Please wait a few minutes and refresh this page.

RSS Google Shared Items

  • An error has occurred; the feed is probably down. Try again later.

RSS Google Reader Starred Items

  • An error has occurred; the feed is probably down. Try again later.

Top Clicks

  • None