#include #include #include #include #include #include #include #include #ifndef CTURTLE_H #define CTURTLE_H ////////////////////////////////////////////////////////////////////////////// ////////////////////////// BASIC STRUCTURES AND TYPEDEFS ///////////////////// ////////////////////////////////////////////////////////////////////////////// #pragma pack(push, 1) // force compiler to not use padding in laying out this struct in memory /** * Represents a color with red, green, and blue components. */ struct Color { uint8_t r; uint8_t g; uint8_t b; }; typedef struct Color Color; // The Color type by itself refers to the struct as a whole. #pragma pack(pop) // undo the previous pack directive /** * Represents an image in memory, in addition to the current state of the turtle associated with it. */ struct Image { struct Color* pixels; // an heap-allocated array of Color structs, each representing // 3 channels of color -- number of elements is width*height int width; // width of the image, in pixels int height; // height of the image, in pixels // turtle settings: int turtle_x; // current x coordinate int turtle_y; // current y coordiante int turtle_angle; // current angle, in degrees bool turtle_pen_down; // true if the drawing pen is "down" (enabled) Color turtle_color; // current color of the turtle }; typedef struct Image* Image; // The Image type by itself refers to a _pointer_ to a struct Image! ////////////////////////////////////////////////////////////////////////////// ////////////////////////// IMAGE CREATE/READ ROUTINES //////////////////////// ////////////////////////////////////////////////////////////////////////////// /** * Create a new image of the given dimensions, all pixels defaulting to black. * The default settings for the turtle are: * Position: center of image * Direction: north * Pen: down * Color: white */ Image create_image(int width, int height); /** * Reads the given file and returns a new Image struct representing it. * If there is an error, return NULL, and the error encountered is available via get_last_error(). * The default settings for the turtle are: * Position: center of image * Direction: north * Pen: down * Color: white */ Image read_tga(const char* filename); /** * Reads the given file and returns a new Image struct representing it. * Automatically distinguishes between ASCII ("P3") and binary ("P6") formats. * If there is an error, return NULL, and the error encountered is available via get_last_error(). * The default settings for the turtle are: * Position: center of image * Direction: north * Pen: down * Color: white */ Image read_ppm(const char* filename); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// IMAGE DESTRUCTOR ////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// /** * Free memory associated with the given image. */ void destroy_image(Image image); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// IMAGE WRITING ROUTINES //////////////////////////// ////////////////////////////////////////////////////////////////////////////// /** * Write the image to a Targa (TGA) file. See the assignment description for file * format limitations. */ bool write_tga(const char* filename, Image image); /** * Write the image to a Portable Pixel Map (PPM) file. See the assignment * description for file format limitations. * If ascii is true, then the human-readable "P3" ASCII file format will be used. * If ascii is false, then the binary "P6" file format will be used. */ bool write_ppm(const char* filename, Image image, bool ascii); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// DIRECT GRAPHICS ROUTINES ////////////////////////// ////////////////////////////////////////////////////////////////////////////// /** * Set the given pixel to the given color. */ void do_pixel(Image image, int x, int y, Color color); /** * Draw a line from (x0,y0) to (x1,y1) using the given color. */ void do_line(Image image, int x0, int y0, int x1, int y1, Color color); /** * Blank the entire canvas with the given color. */ void do_clear(Image image, Color color); /** * Invert all pixels of the image, giving the "negative" image. * This is equivalent to doing "v = 255 - v" or "v = ~v" on all the color components. */ void do_invert(Image image); /** * Switch red and blue channels in the image. * This is needed internally to write out a TGA file, but it's also a neat looking * effect in general when applied to photographs. */ void do_reverse_rgb(Image image); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// TURTLE GRAPHICS ROUTINES ////////////////////////// ////////////////////////////////////////////////////////////////////////////// /** * Move the turtle to the given position without drawing anything. * Returns false if the destination it outside the image bounds. */ bool turtle_set_xy(Image image, int x, int y); /** * Move the turtle to the given position, drawing a line as it goes if the pen is down * Returns false if the destination it outside the image bounds. */ bool turtle_move(Image image, int x, int y); /** * Move the turtle int he current direction by d pixels, drawing as it goes if the pen is down * Returns false if the destination it outside the image bounds. */ bool turtle_forward(Image image, int d); /** * Reset the angle of the turtle to north */ void turtle_face_north(Image image); /** * Turn left by the number of degrees given. */ void turtle_left(Image image, int delta); /** * Turn right by the number of degrees given. */ void turtle_right(Image image, int delta); /** * Lift the "pen", causing turtle move and forward to not draw anything. */ void turtle_pen_up(Image image); /** * Put the "pen" down, causing turtle move and forward to draw lines. */ void turtle_pen_down(Image image); /** * Change the current turtle color. */ void turtle_set_color(Image image, Color color); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// ERROR HANDLING ROUTINES /////////////////////////// ////////////////////////////////////////////////////////////////////////////// /** * Return a string representing the last error encountered by the library. */ const char* get_last_error(); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// OPTIONAL EXTENDED FUNCTIONS /////////////////////// ////////////////////////////////////////////////////////////////////////////// // (you may add extra functions here to add new capabilities to your library) #endif