1 / 32

CSE 20232 Lecture 32 – Coding, X11, Inheritance, & Makefiles

CSE 20232 Lecture 32 – Coding, X11, Inheritance, & Makefiles. Coding and decoding – next assignment X11 windows (Xlib) Makefiles in a bit more detail Examples: X11 Move & show program (Square:TwoDShape:Shape) X11 image display. Next homework. A simple encoder/decoder program

merrill
Download Presentation

CSE 20232 Lecture 32 – Coding, X11, Inheritance, & Makefiles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 20232Lecture 32 – Coding, X11, Inheritance, & Makefiles • Coding and decoding – next assignment • X11 windows (Xlib) • Makefiles in a bit more detail • Examples: • X11 Move & show program • (Square:TwoDShape:Shape) • X11 image display

  2. Next homework • A simple encoder/decoder program • Uses string manipulation • You may use either C++ strings or C-style strings • You will probably need to use some of each • C-style will be required for handling the command line argument (argv) • The program will use a variation of a shift cipher along with randomly embedded distracters

  3. Coder • Usage • coder -c for encoding plain text • coder -d for decoding previously coded text • The program will be used with the Linux pipe (|) and I/O redirection (> < ) symbols so that there is no user interaction. See sample usage below. • coder –c < plain.txt • Encodes text from file “plain.txt” and outputs to screen • coder –d < coded.txt • Decodes text from file “coded.txt” and outputs to screen • cat plain.txt | coder –c • Output from cat is piped into coder where it is encoded and sent to screen • echo test it on this | coder –c > test.txt • “test it on this” is piped into coder, encoded, and sent to file “text.txt” • echo hello | coder –c | coder –d • “hello” is encoded and then decoded and then sent to the screen

  4. Coder • Format of code is • [s][b]xx[block1]xx[block2]xx[block3]……. • [s] is a shift value 1..25 • It is used to shift all letters up by that amount • The shift wraps around in the alphabet • Upper case and lower case maintain their case in the code • [b] is the block size • Each block is a group of characters from the original plain text • xx stands for two random characters with ASCII values 33..126 that are • Inserted between blocks when encoding • Skipped over during decoding

  5. Coder • Format of code is • [s][b]xx[block1]xx[block2]xx[block3]……. • Example: • This is a test. • Gets encoded differently based on shift and block size • Shift 22 and clock size 2 • w2/PPd*)eo6h eKfo iQw tTpa)=opiV. • Shift 24 and block size 3 • y3TtRfg.pq gibq yxZ rc>,qr.

  6. Coding and Decoding • Some ASCII basics • ASCII code is listed in Appendix B, page 1231 • Digits ‘0’..’9’ are ASCII codes 48..57 • Letters ‘A’..’Z’ are ASCII codes 65..90 • Letters ‘a’..’z’ are ASCII codes 97..122 • Some easy tasks • Convert digit to equivalent number • (e.g., ‘5’  5) • Given char ch = ‘5’; … int value = ch – ‘0’; • Convert number to equivalent ASCII character • (e.g., 4  ‘4’) • Given int value = 4; … char ch = (char)(value + ‘0’);

  7. Coding and Decoding • Shifting a letter n=5 positions in the alphabet • ABCDEFGHIJKLMNOPQRSTUVWXYZ (plain text) • FGHIJKLMNOPQRSTUVWXYZABCDE (coded text) • Approach # 1: Add n to ASCII code for letter and if result is greater than code for ‘Z’, then subtract 26 char letter = ‘W’; int shift = 5; char codeLetter = letter + n; if (codeLetter > ‘Z’) codeLetter = codeLetter - 26; • Approach # 2: Use modular arithmetic to add the relative position of the new letter in the alphabet to the position of ‘A’ char letter = ‘W’; int shift = 5; int codeLtrPos = ((letter – ’A’) + n) % 26; char codeLetter = ‘A’ + codeLtrPos;

  8. Coding and Decoding • Shifting a letter n=5 positions in the alphabet • ABCDEFGHIJKLMNOPQRSTUVWXYZ (plain text) • FGHIJKLMNOPQRSTUVWXYZABCDE (coded text) • Decoding: If letter was coded by shifting n=5 positions right, then decode • (1) by shifting r = -n or -5 positions left or … • (2) by shifting 26 – n = 26 - 5 = 21 positions further right • Correct for shifts past end of alphabet as previously shown

  9. XWindows (X11) • Here is a brief intro to X Windows (for more see wikipedia.org) • The X Window System (commonly X11 or X) is a protocol and associated software to provide windowing on bitmap displays. • It provides the standard toolkit and protocol to build graphical user interfaces (GUIs) on Unix, Linux, and is supported by most other modern operating systems. • X provides the basic framework for a GUI environment: • drawing and moving windows on the screen • interacting with a mouse and/or keyboard. • X does not specify a particular user interface so different programs may present radically different interfaces.

  10. Xlib • Xlib is the basic X function library for creating X applications • It involves … • Creating and managing displays, windows, graphics contexts, color maps, … • Detecting and queuing events from mouse, keyboard, … • Handling resizing and hiding/exposing of windows

  11. ezXwindows • For our last program I have created a simplified version of Xlib (ezXwindows) • It allows you to … • Open a window • Change colors • Draw using lines • Example programs • ezXmandel.cpp – shows Mandelbrot image • ezXshowShapes.cpp – shows bouncing box

  12. ezXwindows class class ezXwindow { public: // create an ezXwindow object (includes dynamic // allocation of WinStuff structure) ezXwindow(void); // call X_close_win() if the window is actually still // open and then destroy the ezXwindow object // (delete WinStuff structure) ~ezXwindow(void);

  13. ezXwindows class // handle all details of opening an X11/Xlib window of // specified size. Initial foreground color is system // white, and background color is system black. This // function MUST BE CALLED BEFORE any actual changes // of colors, or drawing can be performed. void X_open_win(int width=SCREEN_WIDTH, int height=SCREEN_HEIGHT); // handle all details of closing X11/Xlib window and // freeing resources void X_close_win(void); // indicates whether X11 window has be opened bool isOpen(void) const;

  14. ezXwindows class // indicates whether current foreground color is // system black bool isBlack(void) const; // indicates whether current foreground color is // system white bool isWhite(void) const; // sets current foreground color to (r,g,b) color // specified and returns the index (color number) of // the window's Colormap entry int X_set_color(int r, int g, int b);

  15. ezXwindows class // sets current foreground color to system black and // return the index (color number) of the window's // Colormap entry int X_set_color_black(void); // sets current foreground color to system white and // return the index (color number) of the window's // Colormap entry int X_set_color_white(void); // sets foreground color as specified and draws an edge // from x1,y1 to x2,y2 in the current foreground color void X_draw_edge(double x0, double y0, double x1, double y1, int color);

  16. ezXwindows class // sends buffered output to window X_flush(); private: WinStuff *X; // pointer to the X window structure bool Open; // true means X window is open bool black, white; // color is system black or // system white or custom };

  17. WinStuff structure details #define NIL 0 #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 #define MAXCOLOR 255 typedef struct { XColor pC; // a color Colormap m_map; // a colormap Display *dpy; // the display Window w; // the window GC gc; // the graphics context double midX, midY; // middle of screen double scale; // minimum screen dimension int blackColor, whiteColor; // default background & // foreground colors } WinStuff;

  18. Shapes Revisited (X11 style) • ShapeX class • drawColor & setDrawColor() • drawX() draws a point at the origin • TwoDShape class • x, y, movePositX() & getPositX() • drawX() draws a point at its position • SquareX and CircleX classes • size, setSize() & getSize() • drawX() functions draw shape outlines

  19. SquareX::drawX(…) void SquareX::drawX(ezXwindow &w) { if (size == 1) // just a point TwoDShapeX::drawX(w); else // has area, so draw Square outline { int left = x - size/2, right = x + size/2, top = y - size/2, bottom = y + size/2; w.X_draw_edge(left,top,right,top,drawColor); w.X_draw_edge(right,top,right,bottom,drawColor); w.X_draw_edge(right,bottom,left,bottom,drawColor); w.X_draw_edge(left,bottom,left,top,drawColor); } }

  20. CircleX::drawX(…) void CircleX::drawX(ezXwindow &w) { if (size == 1) // it’s just a point TwoDShapeX::drawX(w); else // has area, so draw Circle outline { int inc(12), // must evenly divide 360 radius = size/2, startX = x + radius, startY = y; for (int a = inc; a <= 360; a += inc) { float angle = 3.14159*a/180.0; int endX = (int)round(x + radius*cos(angle)), endY = (int)round(y - radius*sin(angle)); w.X_draw_edge(startX,startY,endX,endY,drawColor); startX = endX; startY = endY; } } }

  21. A Program using ezXwindow,SquareX & CircleX classes // ezXmvShapes08.cpp - JHS 2008 #include <unistd.h> // usleep #include <curses.h> // CURSES I/O #include <ctime> // time #include <cctype> // tolower #include <cstdlib> // system, exit rand, srand #include "shapes2008.h" #include "ezXwindow2.h" using namespace std; int main(int argc, char **argv) { // run CURSES in a small xterm, with display in an ezXwindow if (argc == 1) { // no command line arg so restart in xterm system("xterm -geometry 80x8+40+540 -e ezxms ON &"); exit(0); }

  22. Program using ezXwindow(cont) SquareX sq; // create a Square CircleX circ; // create a Circle time_t theTime; int key, width(640), height(480), px, py, dx, dy; bool squareSelected(true); // declare and open an ezXwindow (640 x 480 pixels) ezXwindow ezWin; ezWin.X_open_win(width,height); // intitialize curses I/O initscr(); int scrwidth, scrheight; getmaxyx(stdscr, scrheight, scrwidth); raw(); cbreak(); noecho(); nodelay(stdscr,true); srand((int)time(&theTime));

  23. Program using ezXwindow(cont) dx = 5; dy = 3; // set initial movements px = width/2; py = height/2; // set initial position sq.movePositX(px, py); // put sq in screen center sq.setSizeX(10); // set sq size circ.movePositX(px, py); // put circ in screen center circ.setSizeX(10); // set circ size int black = ezWin.X_set_color_black(), color = ezWin.X_set_color(rand()%256,rand()%256,rand()%256); do { // draw the square or circle in the ezXwindow if (squareSelected) { sq.setDrawColorX(color); sq.drawX(ezWin); // show SQUARE in Xwindow } else { circ.setDrawColorX(color); circ.drawX(ezWin); // show CIRCLE in Xwindow } ezWin.X_flush();

  24. Program using ezXwindow(cont) // update information on CURSES screen clear(); mvprintw(0,0,"Shape:center(%3d,%3d)size(%3d)",px,py,sq.getSizeX()); mvprintw(0,scrwidth-38,"<< Shapes in ezXwindow (c) 2008 JHS >>"); mvprintw(scrheight-4,0,"(c) and (s) select circle or square"); mvprintw(scrheight-3,0,"(+) and (-) change size"); mvprintw(scrheight-2,0,"(q) quits!"); mvprintw(scrheight-1,0,"ENTER SELECTION --> "); refresh(); usleep(50000); // pause 1/20th second key = getch(); // check for keyboard input, but don't wait // erase the square or circle in the ezXwindow if (squareSelected) { sq.setDrawColorX(black); sq.drawX(ezWin); } else { circ.setDrawColorX(black); circ.drawX(ezWin); }

  25. Program using ezXwindow(cont) if ((key == '+') && (sq.getSizeX() < 200)) { sq.setSizeX(sq.getSizeX()+2); // increase sq size circ.setSizeX(sq.getSizeX()+2); // increase circ size color = ezWin.X_set_color(rand()%256,rand()%256,rand()%256); } else if ((key == '-') && (sq.getSizeX() > 2)) { sq.setSizeX(sq.getSizeX()-2); // increase sq size circ.setSizeX(sq.getSizeX()-2); // increase circ size color = ezWin.X_set_color(rand()%256,rand()%256,rand()%256); } else if (tolower(key) == 's') squareSelected = true; // select SQUARE else if (tolower(key) == 'c') squareSelected = false; // select CIRCLE

  26. Program using ezXwindow(cont) sq.getPositX(px,py); // get current position, both Shapes same if ((py-sq.getSizeX()/2 <= 0) || (py+sq.getSizeX()/2 >= height)) dy= -dy; // hit top or bottom wall so reverse y direction if ((px-sq.getSizeX()/2 <= 0) || (px+sq.getSizeX()/2 >= width)) dx = -dx; // hit side wall so reverse x direction sq.movePositX(px+dx, py+dy); // move SquareX circ.movePositX(px+dx, py+dy); // move CircleX } while ((key != 'q') && (key != 'Q')); // quit on input 'q' or 'Q' // clean up, and end curses I/O clear(); refresh(); endwin(); return 0; }

  27. An ezXwindow Program to display images // ezXimage.cpp -- JHS 10/24/2003 & 11/19/2006 #include <iostream> #include <fstream> #include "ezXwindow.h" using namespace std; #define ROWS 1024 #define COLUMNS 1024 bool load(string filename, unsigned char image[ROWS][COLUMNS][3], int &width, int &height); int main (void) { unsigned char image[ROWS][COLUMNS][3]; int width; int height; string filename; ezXwindow ezWin; // for the X11 display

  28. Program to display images system("clear"); system("ls *.ppm"); cout << "\nChoose file to display: “; cin >> filename; cin.get(); if (! load(filename, image, width, height)) return 1; // open window and draw image from the array ezWin.X_open_win(width,height); for (int r=0;r<height;r+=1) for (int c=0;c<width;c+=1) ezWin.X_draw_edge( c, r, c, r+1, ezWin.X_set_color( image[r][c][0], image[r][c][1], image[r][c][2])); ezWin.X_flush(); cout << "Hit ENTER to exit!"; cin.get(); return 0; }

  29. Program to display images(load function) bool load(string filename, unsigned char image[ROWS][COLUMNS][3], int &width, int &height) { int maxcolor; string comment, magicNum; cout << "Loading file \"" << filename << "\"\n"; ifstream infile(filename.c_str()); if (infile.fail()) { infile.clear(); cout << "ERROR: load failed.\n"; return false; } getline(infile,magicNum); if (magicNum != "P6") return false;

  30. Program to display images(load function) while(infile.peek() == '#') getline(infile, comment); infile >> width >> height >> maxcolor; infile.get(); for (int r=0;r<height;r++) for (int c=0;c<width;c++) { image[r][c][0] = infile.get(); image[r][c][1] = infile.get(); image[r][c][2] = infile.get(); } infile.clear(); infile.close(); return true; }

  31. More on Makefiles • MACRO • A name associated with a value to be used later • Useful macros • List of compiler options • List of libraries to be linked to program • Alternate locations for includes and libraries • $@ is the current target • $^ is a list of the current dependencies

  32. Sample Makefile # Makefile for ezXimage # JHS 2006 Notre Dame # here are some useful macros for COMMON items SOURCES = ezXimage.cpp ezXwindow.cpp OBJECTS = ezXimage.o ezXwindow.o HEADERS = ezXwindow.h LIBS = -L/usr/X11R6/lib -lX11 -lm INCLUDES = -I/usr/include/X11 ezXimage : $(OBJECTS) g++ -o $@ $^ $(LIBS) ezXimage.o : ezXimage.cpp ezXwindow.h g++ -c ezXimage.cpp $(INCLUDES) ezXwindow.o : ezXwindow.cpp ezXwindow.h g++ -c ezXwindow.cpp $(INCLUDES)

More Related