Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

02 c++g3 d


Published on


Published in: Technology, Art & Photos
  • Be the first to comment

  • Be the first to like this

02 c++g3 d

  1. 1. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science Graphics Programming in C++ with G3DC++ is the most widely used programming language for computer graphics in bothindustry and research. It combines low-level features like direct memory manipulationwith high-level features like exceptions, classes, and macros. These make C++ incrediblypowerful and adaptable, but also create pitfalls for new programmers.G3D is an open source, cross-platform library for creating interactive 3D programs likegames. 3D applications, such as games, medical software, artist tools, and film renderingsoftware are incredibly complex. Nobody writes them from scratch because it takes about100,000 lines of infrastructure code before you are even ready to begin the programmingthat is specific to your project. There are several large libraries (“engines”) that areregularly used in research and the software industry to support 3D graphics, includingOGRE, Source, OpenCV, and Unreal. G3D is the one you will use as the primary supportlibrary for your projects in this course. It contains OpenGL, libpng, libjpg, zlib, ffmpeg,and other popular graphics libraries, and extends them to be both easier to use and morepowerful.This document is a quick-start guide for programmers with Java experience but little orno C++ and graphics experience. Some additional resources are: • G3D Manual, /usr/local/371/html/index.html • iCompile Manual, /usr/local/371/html/icompile-manual.html • Lerner, C++ for Java Programmers, • Fiamingo, DeBula, and Condron, Introduction to Unix • Kernighan and Ritchie, The C Programming Language • Stroustrup, The C++ Programming Language • Neider, Davis, and Woo, OpenGL Programming Guide (also available online at Critical Things You Need To Know About C++ 1. C++ variables are objects by default. Java variables are pointers. In C++ you have to explicitly declare a pointer if you want one. Usually you can use a reference (e.g., Vector3&) or a reference-counted pointer (e.g., TextureRef) instead to avoid some of the dangers of C++ pointers. 1/17
  2. 2. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science 2. C++ lets you control whether objects are allocated on the stack or the heap. You have to manually delete objects in the heap when you are done with them, which is error prone, so try to allocate on the stack whenever possible. 3. C++ has separate static compilation and linking steps. Linker errors usually occur because you forgot to implement a method (possibly due to misspelling) or because your .icompile file isn’t configured correctly. 4. Put a semi-colon at the end of class declarations. 5. Object assignment copies the whole object (which might be really slow). Matrix4 A; Matrix4 B; … Matrix4& C(A); // Alias for A Matrix4 D = A; // Copy of A C[0][2] = 1.0f; // Also changes A D[0][2] = 7.0f; // Leaves A (and C) unchanged 6. Stack declarations accept constructor arguments. Use them instead of copying! // Good Array<int> a(1024*1024); // Bad: copies 4 MB of memory! Array<int> a = Array<int>(1024*1024); 7. Don’t use: a. C array declaration syntax, e.g., int x[10] (use Array instead) b. Multiple inheritance c. Pointer arithmetic d. Multiple object declaration (e.g., don’t write Vector3 a, b, c; put each on its own line instead.) e. C memory management: malloc, calloc, free (use stack allocation or new) f. C++ new and delete, if you can avoid them with stack allocation g. std:: data structures when a G3D:: one is available (use Array, Table, Set, Queue instead) h. C strings (char*) except for initialization (use std::string instead) 8. Pass large objects by reference using &: int computeSum(const Array<int>& array); 9. Never return a reference or a pointer to an object on the stack from a method. 10. Make arguments (especially references) and methods const whenever possible 11. NULL is in all caps. It is equal to 0. 12. The Boolean data type is bool (not Bool, Boolean, BOOL, or anything else). It requires 4 bytes on most machines. 2/17
  3. 3. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science 13. Primitive types, including int, float, bool, and pointers, are uninitialized when declared as locals or member variables (they are not zero/NULL like in Java.) 14. onGraphics() runs 30 times per second or more to redraw the screen. Don’t do anything slow from it, like running a ray tracer or loading a file from disk. 15. Put class declarations only in .h files 16. Put class implementations only in .cpp files 17. Wrap every .h file in a header guard. E.g., for Foo.h: #ifndef Foo_h #define Foo_h … #endif Foo_h2 Compiling CodePrograms written in any language pass through two steps before they are run. The firststep is compilation, which checks syntax and types and then produces an object file (inJava, these end in .class; in C++ they end in .o or .obj). The second step is linking, whichconnects all of the object files into a single program. In Java, the linking step happens atrun time and is not explicitly seen by the programmer. In C++, the linking step isperformed in two parts: explicitly after compilation (“static linking”) and at run time(“dynamic linking). Knowing how to invoke the compiler and linker is a valuable skill. Itis also one that takes some time to acquire. To help you get started quickly, I’ve provideda program called “iCompile” that will automatically compile and link your programs. Itwill also provide some additional functions if you request them, like copying data filesfrom your source directory to your build directory and creating documentation for yourcode.To learn more about compiling and linking you can run iCompile with the “—verbosity3” argument. All of the green text that it prints contains the actual commands beingissued to the compiler. You can use “man gcc” to see a description of those commands.3 DebuggingThe GNU debugger, gdb, is a command-line program that you can use to pause a runningprogram, look at its variables, and see a stack trace. You can run your program undergdb by specifying the “—gdb” flag instead of the “—run” flag to iCompile. Compared toa visual debugger, gdb is unfortunately hard to use and many programmers use simpleprint statements and clever rendering tricks to try and diagnose problems in theirprograms. There are some situations that are impractical to debug by those methods,however, and in those situations gdb is essential. The most common of these is when anassertion failure occurs and you need a call stack to locate the source of the problem inyour program (the assertion failure typically contains a line number inside a library).When this happens, select “(3) Debug” from the G3D assertion prompt, which will leaveyou at the gdb prompt. Type “bt” to see a backtrace (call stack), which will identify theline of your code that triggered the failure. Type “help” in gdb to learn more about othercommands, e.g., for looking at local variables. 3/17
  4. 4. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer ScienceG3D creates a log.txt file every time your program is run. This contains information thatis useful in debugging, including the last assertion message and a list of all files that yourprogram accessed.G3D provides several routines to aid in debugging your programs. The names of theseroutines all begin with “debug”.The debugPrintf function is like printf except that it disappears when you make anoptimized build of your program and works in a useful way on all operating systems.The debugBreak macro pauses execution. It is a way of programmatically setting abreakpoint for the debugger.The debugAssert and debugAssertM macros execute a debugBreak if the specifiedcondition is not true. A command-line prompt then allows you to see more informationabout the error and choose how you would like to continue. Sometimes you want anassertion to remain in an optimized build. Use alwaysAssertM in that case.Three routines output debugging text. screenPrintf displays information directly on your3D window. It is most useful for debugging interactive programs that continuouslyrendering. debugPrintf prints to the xterm that created your program. logPrintf prints tothe log.txt file, which is useful if your program is crashing in a way that takes out thexterm (or the computer).Later in the semester when using OpenGL in more advanced ways, the getOpenGLStatefunction allows you to see the current values of the OpenGL registers.G3D makes it really easy to create GUI controls to adjust parameters in your program,which can be very helpful for debugging. Commands like: debugPane->addCheckBox(“Visible”, &visible);let you map variables from your program directly to controls on the screen so that youcan try many variations without recompiling your program. 4/17
  5. 5. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science4 C++ Declarations4.1 C++ Syntaxpublic class A : public B { A is a subclass of Bprivate: float x; The following declarations are all private members & methods.public: The following declarations are all public A(); members & methods. ~A(); Destructor, the opposite of a constructor. This runs when an instance is deallocated. void foo();}; Note the semi-colon4.2 DeclarationsThe class is split across two files. The declaration is in a header (.h) file. This contains allof the information needed to tell the compiler how big the class is, what methods it has,and what members it has. The implementation is in a .cpp file; this is what tells thecompiler what the methods do. The #include statement effectively pastes one file intoanother. Classes must be declared before they are used in C++. This is why we haveheader files—they enable us to paste the declaration before any use of the class, even ifthe implementations of two classes depend on each other.When the declarations of two classes depend on each other, C++ uses a forwarddeclaration: // forward declaration class B; class A { public: void add(B& b); }; class B { public: void add(A& a); };Compilers need to know how big objects are. Since all pointers are the same size, thecompiler knows how big B* and B& are (and the special case of a B as a return value),even if they don’t know how big an instance of B is. The reason for the forwarddeclaration is to assure the compiler that you didn’t type “B” by accident. The forwarddeclaration how you promise the compiler that a declaration of B will show up at somepoint in the future (if one doesn’t, then the linker will complain).4.3 FunctionsC++ allows the definition of functions, which are like static methods without anassociated class. You probably won’t have to declare many of your own functions, but 5/17
  6. 6. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Scienceyou likely will have to call functions written by others. For example, endsWith is afunction in the following code snippet: if (! endsWith(filename, “/”)) { filename += “/”; }5 ConstantsThe order of initialization of global constants is undefined in C++. For example, thismeans that the following code could produce ANY value for the perimeter of the unitcircle, because PI might be initialized after unit. const float PI = 3.1415926; class Circle { public: float perimenter; Circle(float radius); }; Circle::Circle(float radius) : perimeter(PI * 2.0f * radius) { } Circle unit(1.0f);There are several tricks to avoid being caught by this behavior. For integer constants, theenum type is always created at compile time. So integer constants can be expressed using(the admittedly awkward syntax): enum {DAYS_IN_WEEK = 7};Macros can be used for any kind of constant: #define PI (3.1415926) #define DAYS_IN_WEEK (7)For class constants, it is necessary to create an inline function that returns a static constvariable. This can also be used for other data types: inline const Matrix3& rot45() { static const Matrix3 M = Matrix3::fromAxisAngle(Vector3::unitX(), toRadians(45)); return M; }That example uses a number of C++ features too complicated to explain here, but is apattern that you can follow for creating constants and static class member constants thatare guaranteed to be valid at the global level. Constants inside methods and functions donot require such care and can be defined as if they were variables. 6/17
  7. 7. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science6 InheritanceInheritance in C++ is tricky. In general, never use multiple inheritance—it is especiallyconfusing and can produce code that compiles but does something other than what youexpected.In Java, every class can be subclassed and every method overridden unless explicitlymarked with “final”. C++ has the opposite convention. Unless a method is explicitlymarked as “virtual”, it cannot be overridden (well, it can be overridden, but the resultwon’t be what you wanted.)In addition, unless a class has a destructor that is declared as virtual, it may also leakmemory when instances are destroyed. The G3D classes that you need to subclass aredeclared with virtual destructors and appropriate methods (e.g., event handlers) declaredvirtual. The G3D classes that you don’t need to but might like to subclass (e.g., Vector3)are not declared virtual and your program will be in error if you try to inherit from them.A pure virtual method is one that is not implemented by the base class (Java calls this“abstract”). Pure virtual methods declarations have “=0” at the end, like: virtual void intersect(const Ray& worldRay, float& hitDistance, class Hit& hitResult) = 0;See for a discussionof inheritance.7 Factory MethodsC++ provides overloaded constructors so that a class may be instantiated in multipleways. Sometimes the constructors would be confusing if they were distinguished only byoverloading. In theses cases it is common practice to provide a static method on the classthat produces the desired instance. These so-called “factory” methods are also used foralternative memory allocation schemes like buffer pooling. Here are examples of usingfactory methods: Vector3 up = Vector3::unitY(); Matrix3 rot = Matrix3::fromAxisAngle(Vector3::unitX(), toRadians(15);8 Manual Memory ManagementUnlike Java, in C and C++ programmers must both allocate (using “new” or “malloc”)memory for objects and deallocate that memory (using “delete” or “free”) when theobject is no longer in use. Forgetting to deallocate memory leads to memory leaks,which will eventually make a program run slowly and crash. Deallocating memory thatis still in use creates a “dangling” pointer and will likely cause a program to crash. Asyou can imagine, this manual memory management is one of the trickiest parts of C++programming and is the source many errors.Fortunately, G3D helps you avoid ever having to manually manage memory. You shouldalmost never have to write “new” or “delete” in your program, in fact. Two tools thatG3D provides are common data structures and reference counting. 7/17
  8. 8. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science9 Data StructuresThe G3D Array, Queue, Set, and Table data structures are all dynamic. This means thatyou just declare variables of these types, for example, Array<int> myIntegerArray;and G3D will handle allocating and deallocating the underlying memory that is used torepresent the data structure. C++ also provides a standard library with arrays and queues,and the C language provides built-in arrays (like Java’s built-in arrays). You shouldgenerally not use those in this class. The G3D versions are better optimized for graphics,are more platform independent, and are easier to use.For string processing, use the built-in std::string class. This can be initialized from a C-string (char*), but is a class with useful methods. To convert std::string to C-string, usethe std::string::c_str method. An example of using std::string is: std::string name = “McGuire”; debugPrintf(“Length: %dn”, name.size()); debugPrintf(“Value: ”%s”n”), name.c_str(); if (name == “Murtagh”) { debugPrintf(“username = tomn”); } else { name += “ – Username Unknown!”); }Note that words in quotes are not strings; they are values that can become strings. So“hello” + “ there” does not concatenate strings, but std::string(“hello”) + std::string(“there”) does.10 Reference CountingReference counting is a form of automatic memory management (also known as garbagecollection). Most G3D classes, like Texture, GFont, and Shader, are managed withreference counting. For these classes, declare your instance to be the corresponding “ref”type (“ref” means reference, another word for pointer). Use the appropriate factorymethod to create an instance of this class. For example: TextureRef tex = Texture::fromFile(“apple.jpg”);The ref classes act like regular C++ pointers in almost every way. They can be shared(aliased), set to NULL, and tested for equality. Use the -> or * syntax to dereference apointer. Testing for NULL has a special syntax. Examples: 8/17
  9. 9. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science int sumWidth(TextureRef tex1, TextureRef tex2) { if (tex1.notNull()) { if (tex1 == tex2) { return 2 * tex1->texelWidth(); } else { return tex1->texelWidth() + tex2->texelWidth(); } } else { return 0; } }Never use C++ pointer syntax with these reference counted classes, e.g. DO NOT writeTexture*. Never call delete or new with a ref class. You can make your own referencecounted classes; see the G3D documentation for more information.11 Variable InitializationC++ does not initialize variables unless they are of a class with a default constructor. Thismeans that most member variables and stack variables have undefined values. Youshould always initialize these immediately after declaring them or in your constructor.When writing your own class constructors, a special syntax allows you to initializemembers before the constructor executes (this is equivalent to Java’s member assignmentduring definition). The syntax is a colon after the constructor signature followed by acomma-separated list of members, each with its own constructor arguments inparentheses: class Player { public: Vector2 position; std::string name; Player(); }; Player::Player() : position(10, 2), name(“Harvey”) { }12 Pass By ReferenceIn C++, a type written without a “*” declares a variable to be the actual object, not apointer to the object as is the case in Java. Since you have been instructed not to usepointers in most cases, this means that almost all of your variables will be stack allocatedobjects or members defined inside another object.When assigning one object to another, a copy occurs. For a large object like GImage oran Array, this is potentially slow and is probably not desirable. This means that you willusually want to pass values into methods and functions by reference to avoid the copy. InC++, an ampersand (&) placed after a type makes it into a reference type that acts like apointer but cannot be reassigned. Passing an object to a reference incurs no copy.For example, the following code passes a GImage by reference. Note the use of the constkeyword to specify that the function cannot change. 9/17
  10. 10. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science int findRedPixelX(const GImage& image, int y) { for (int x = 0; x < image.width; ++x) { if (image.pixel3(x, y).r == 0) { return x; } } return -1; } … GImage image(“logo.gif”); int x = findRedPixelX(im, 0);Note that the int was not passed as a reference in this example. That is because smalldata types like int are the same size as a pointer, so there’s no performance advantage topassing them by reference. It is possible to pass a reference to an int, and in fact that is aconvenient way of returning more than one value from a method. The following exampledemonstrates two values returned by passing them by reference. void findRedPixel(const GImage& image, int& x, int& y) { x = -1; y = -1; for (y = 0; y < image.height; ++y) { for (x = 0; x < image.width; ++x) { if (image.pixel3(x, y).r == 0) { return; } } } } … GImage image(“logo.gif”); int x; int y; findRedPixel(im, x, y);13 OpenGL Register ModelOpenGL is the platform independent library that allows access to graphics cards. Itpresents a virtual interface to the card called a Hardware Abstraction Layer (HAL). Thisabstraction layer refers to registers and other low-level hardware features. Graphics cardsare generally operated by setting state (values in the registers) and then executing afunction that uses that state.G3D follows this model. The RenderDevice class contains many “set” methods thatassign values to OpenGL registers. Only two methods trigger actual rendering. These areRenderDevice::sendVertex and RenderDevice::sendIndices. Because graphics cards aredesigned as pipelines, “sending” a value means sending it down the pipeline toprocessing.Most bugs in OpenGL programs are from having the wrong state set when sending data.RenderDevice::push and RenderDevice::pop allow you to easily save and restore all ofthe state in the rendering system, which can be used to isolate one piece of code fromanother and reduce the number of state-based errors. 10/17
  11. 11. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science14 Color ModelTo a graphics card, 0 is the darkest value that can be displayed and 1 is the brightest.These values between 0 and 1 are often rescaled and stored in bytes as values between 0and 255. A color is represented by a triplet of red, green, and blue intensities (alwayswritten in that order). It can also be written as the hexadecimal representation of thebytes in such a triple, e.g. 0xFF8800 is redish yellow because it contains bright red, dimgreen, and no blue. The Color3 and Color3uint8 classes represent colors in G3D.In addition to red, green, and blue, an “alpha” channel is used to represent translucencyand partial occlusion in many situations. An alpha value of 0 generally meanscompletely translucent and a value of 1 represents completely opaque. Color4 andColor4uint8 extend Color3 with an alpha value. The alpha value is represented as thehigh-order bits when writing in hex: 0xFF000000 is opaque black.15 2D Coordinate SystemFor historical reasons, in 2D (0, 0) is the upper-left corner of an image or the screen. Thex-axis increases to the right and the y-axis increases down. This is used forRenderDevice push2D rendering, Texture coordinates, and for GImage pixel addressing.Texture coordinates vary from (0, 0) in the upper-left to (1, 1) in the lower right, whileRenderDevice push2D, Image, and GImage use integer pixel coordinates.16 Immediate vs. Retained Mode GraphicsIn a retained mode graphics API, you create objects and they persist until destroyed,drawing themselves on screen and interacting with the user. An example is the SwingJComponent API in Java, where you create objects like JButton and JTextArea that thenappear on screen until dismissed.In an immediate mode graphics API, you explicitly draw on a canvas that is periodicallyerased. There are no persistent objects; if the canvas is erased by your code or by anotherwindow drawing over it, your image will be lost forever and must be redrawn fromscratch. An example of this is the Swing Graphics2D API used in the Component.paintmethods.G3D provides pieces for writing retained mode code, but it is intended primarily forimmediate mode rendering. In GApplet, the onGraphics event handler that youimplement will be invoked several times a second. It will use calls on a RenderDevice tocreate an image that lasts only until the next frame of animation.17 Starting Your ProgramiCompile will automatically copy the contents of the data-files directory into the builddirectory, change into the build directory, and then launch your program. At that point,the main() function begins. Main must have the following syntax: int main(int argc, char** argv) { … return …; } 11/17
  12. 12. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer ScienceInteger argc is the number of command line arguments that were passed to your programas strings. There is always at least one, which is the name of your program. Variable argvis a pointer to a pointer to a character. In C (not C++) syntax, it can also be viewed as anarray of arrays, or an array of strings. That’s because a C array is just a pointer to the firstelement in the array. C doesn’t keep track of how many elements are in an array foryou…if you go off the end, you simply start corrupting memory and the program crashes!That’s why argc is provided. A string in C (as opposed to a C++ std::string) is just anarray of characters. The last character is ‘0’ (this is a “null-terminated” string), which ishow you know when the string has ended.An example of simple command line argument processing is: int main(int argc, char** argv) { if ((argc > 1) && (std::string(“hello”) == argv[1])) { … } return …; }Here, constructing a C++ std::string lets us use the == operator to tell if the two stringsare identical. If we just said “hello” == argv[1], then we’d be testing if the memorypointer to “hello” was the same as the memory pointer argv[1], not asking if theycontained equivalent strings. If you’ve programmed in Java, then you might recall thedifference between String.equals and ==; in C++, that’s the difference betweenstd::string::operator== and C-string == on pointers.18 C++ Syntax Reference18.1 Pointers and ReferencesObject: Vector3 v;Reference: this acts like an object but re-uses the memory allocated for v and can neverbe assigned to different memory. Vector3& r(v);Pointer: this stores the address of the memory for v. It can be reassigned to a differentaddress (or to NULL). Vector3* p(&v); 12/17
  13. 13. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer ScienceReference counted pointer: this acts like a pointer, but garbage collects the object when itis no longer used. These are only available for some objects: TextureRef tex = Texture::fromFile(“hello.jpg”);Assign to a member: v.x = 1.0f; r.x = 3.0f; p->x = 7.0f; *p.x = 8.0f; tex->invertY = true; *tex.invertY = false;Read a member/invoke a method float f; f = v.y; v.unitize(); f = r.y; f = p->y; f = *p.y; f = tex->width(); f = *tex.width();Copy the value: v = *p; *p = v; r = v; v = r; *p = r;Point at the same value: p = &r; p = &v; Vector3* q(p); Vector3* s(&r); Vector3& z(*p); Vector3& c(r); q = p;18.2 AllocationAllocate on the stack (will automatically be deallocated when it goes out of scope): Vector3 v(5, 2, -15); 13/17
  14. 14. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer ScienceAllocate a C++ array on the stack (will automatically be deallocated when it goes out ofscope), where the contents of the array are in the heap: Array<Vector3> v(10); // G3D; recommended std::vector<Vector3> x(10); // std; not recommended for 371Allocate a C array on the stack, where the contents of the array are also on the stack(generally undesirable): Vector3 v[10];Allocate a reference-counted object on the heap (will automatically be deallocated whenthe last pointer is dropped, just like in Java): Image3Ref im = Image3::createEmpty(100, 100);Allocate a non-reference counted object on the heap (avoid this whenever possible, sinceyou must explicitly deallocate it or memory will leak!): MyObject* obj = new MyObject(); … delete obj; obj = NULL;18.3 staticDeclare that a method is on a class, not a member (same as in Java): class Ball { public: static BallRef create(const std::string& name); };In the .cpp file for the above example, do not put “static” in front of the implementation: BallRef Ball::create(const std::string& name) { return new Ball(); }Declare that a member variable belongs to a class, not a member (same as in Java): class Cube { public: static int numCubes; };In the .cpp file for the above example, numCubes is initialized as: int Cube::numCubes = 0; 14/17
  15. 15. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer ScienceDeclare that a variable should only be initialized the first time a method is invoked, andits value remembered after that (rarely a good idea for non-const variables): void MyClass::sketchy() { static int x = 0; … }Declare that a function or variable should not be visible outside a .cpp file (rarely used): static int helper(int y, int z) { Return y + 2 * z; }18.4 constDeclare that a method never mutates a value: void safeMethod(const std::string& x);Declare that a method never mutates any member variable: void alsoSafe(int y) const;Declare that a member variable is never changed after initialization: class Clown { public: const Color3 hair; Clown(); };Create a mutable pointer to an immutable int, i.e., “don’t change the value x is pointingat”: const int* x; int const* x;Create an immutable pointer to a mutable int, i.e., “don’t reassign the pointer”/ “don’tchange x”: int* const x; 15/17
  16. 16. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science18.5 Iterator Set<int> keep; … Set<int>::Iterator it = keep.begin(); Set<int>::Iterator end = keep.end(); while (it != end) { … ++it; }18.6 Function PointersYou don’t need to use function pointers very often, but if you do, the syntax is odd andyou’ll want to check back here.Pass a non-static method pointer as an argument: debugPane->addButton(“Exit”, &App::exit);Declare a function pointer/static method pointer (e.g., to atan2): double(*atan2Ptr)(double, double);Create a typedef for a function pointer type: typedef double(*)(double, double) ATan2Type;Pass a function pointer as an argument or use as a value: passPointer( &atan2 ); ATan2Type atan2Ptr = &atan2;Call a function using its pointer (two syntaxes available): atan2Ptr(x, y); (*atan2Ptr)(x, y);18.7 printfG3D provides three ways of printing text: logPrintf(), debugPrintf(), and screenPrintf().You can also print to a std::string with format(). Type “man printf” to see a fullspecification for these. The common idioms you’ll use are: 16/17
  17. 17. CS371 Fall 2008 Williams CollegeProf. McGuire Department of Computer Science std::string s; int i; float f; … debugPrintf(“The int’s value is %dn”, i); debugPrintf(“A string %s and a float %fn”, s.c_str(), f);19 Coding ConventionsWe’re going to be looking at a lot of each other’s code between group projects, samplecode, and helping each other debug. To make it easy to understand code written by othersand to cut and paste between projects, please follow the following coding conventions. Insome cases you may find that the conventions are making it harder to read your code.When that occurs, write in the way that is easiest for another person to read even if itbreaks with convention. • Follow Java coding conventions wherever they apply to C++ • Make header guards case-sensitive, e.g., #ifndef Film_h • Prefix private and protected member variables with m_, e.g., m_photonMap • Indent member variables and method arguments in columns, e.g., int m_numPhotons; std::string m_filename; • Put a space before the open parent of FOR, WHILE, and IF, e.g., if (! b) • Put the open-brace on the same line as FOR, WHILE, IF, TRY, etc. • Always uses braces, even for single-line FOR, WHILE, etc. • Use four-space indenting and no tabs • Put comments before the code to which they apply • Keep lines to about 80 characters (for printing) • Fully-parenthesize logical expressions, e.g., (x || y) && (z > 2) • When writing pointers, the * goes with the class name, e.g., RenderDevice* rd; 17/17