CS60 Lecture 20 --------------- Announce ------------- - proj4 - hw14 posted, due mon Today ------------- - proj4 - review virtual funcs - file IO - C++ strings - OpenGL Proj4 ------------------- - questions? - y * width gives the rigth row - + x gives the right column - *3 since each el is 3 bytes - +0,+1,+2 for r, g, b respectively Review Virtual Functions -------------------------- - know about inheritance - some problems arise - constructors, destructors, copy const, assign op - solve by implementing these special funcs for ALL classes - some more problems - base function uses another member func only defed in child - solution is to use virtual functions - base class has a virtual function defned and uses in member func - child class use of base class member uses child class version of virtual func - base has 'center()' which uses 'draw()' - base has virtual function draw() - child has virtual function draw() - child instance.center() gets child draw() - if we never plan on using base class, declare PURE virtual functions - virtual = 0; C++ File IO ----------------------- - in C, we had file descriptors and FILE pointers - fd IO set: read, write, open, close - FILE IO set: fprintf, fscanf, fopen, fclose - IO channels generic (files, devices, screen, keyboard) - C++ uses streams, which are also generic - inheritance hierarchy fstream | \ ostream istream | \ ofstream ifstream - anything that is an output stream is of type 'ostream' like cout - input is 'istream' like cin - cout and cin are instances of 'ostream' and 'istream' objects - file IO happens through derived classes ifstream and ofstream - *fstream have member functions: - open - close - flush - overloaded ops << and >> .. like cout/cin! Ex: ofstream ofile; int a=10; ofile.open("/tmp/out.txt"); ofile << "A is = " << a << endl; ofile.close(); - similarly, ifstream objects work like cin - remember out cin works, though, ignores whitespace, newlines, etc - to do character output, use member functions: - get - put - eof char tmp; ifile.open("/tmp/foo.txt"); ifile.get(tmp); while(!ifile.eof()) { cout << tmp; ifile.get(tmp); } ifile.close(); - to check for failure, use 'fail' member function ofile.open("/etc/passwd"); if (ofile.fail()) { cout << "doh!\n"; exit(1); } - good for simple IO, also have functionality we're more familiar with: fstream - member functions are all above, but also 'read' and 'write' - TWO seeks, one for read (get), one for write (put) (seekp, seekg) - constants defined in ios class for IO flags - ios::in - ios::out - ios::app - OR them together just like we did with O_RDONLY, O_WRONLY, etc C++ Strings --------------------- - C strings work the same way - C++ has a standard string class - pretty nice actually - #include using namespace std; - string a, b, c, z; - initializing string d(" world"); a = "hello"; b = d; - modifying/accessing c = a + b; c[0] = 'H'; int i = c.find("world"); z = c.substr(i, c.length(c)); - comparing if (a == b) ... - other - a.length() - a.empty(); OpenGL -------------------- - we have been using OpenGL for project4 - OpenGL developed by SGI as a portable 3d graphics standard - Other standard is DirectX by microsoft - glut (gl utility toolkit) implements an eventloop and gives us an OpenGL context - openGL is a stateful programming model where you set a state, perform some operations while in that state, then leave the state - ex: drawing a square glColor3f(255.0, 0.0, 0.0); glBegin(GL_QUADS); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(1.0, 1.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glEnd(); - above draws a 2d rectangle in 3d space, can rotate and scale with simple functions - can add textures to surfaces with simple function, textures scale, rotate, etc with the surface they're attached to - this is how 3d video games are created! - great tutorials at http://nehe.gamedev.net - OpenGL very low level 3d primitives, higher level libraries have been created to do common operations - libSDL: Simple DirectMedia Layer - portable game/graphics library for UNIX/Windows/Mac - programming interface for graphics, sound, image manipulation, GUI, etc - tons of open source and commercial games use it: www.libsdl.org