1 / 23

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. Coding and Decoding. Some ASCII basics

varuna
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. 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’);

  3. 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;

  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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);

  9. 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;

  10. 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);

  11. 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);

  12. 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 };

  13. 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;

  14. Program using Squares // ezXdrawShapes.cpp - JHS 2006 #include <iostream> #include <curses.h> #include <ctime> #include "shapes2006.h" #include "ezXwindow.h“ // new, for X11 window using namespace std; int main() { Square sq; time_t theTime; int key, width, height, px, py, dx, dy; // declare and open an ezXwindow (790 x 250) pixels ezXwindow ezWin; ezWin.X_open_win(790,250); // intitialize curses I/O initscr(); getmaxyx(stdscr, height, width); raw(); cbreak(); noecho(); nodelay(stdscr,true); srand((int)time(&theTime));

  15. Program using Squares sq.movePosit(width/2, height/2); // put sq in screen center sq.setSize(10); // set sq size dx = 2; dy = 1; // set initial movements do { sq.getPosit(px,py); // get current position of Shape // clear screen, show sq, position & size and wait 1/5 sec clear(); sq.draw(); // position ad draw the square in the ezXwindow int half = sq.getSize()/2; int Lx = 10*(px-half), Uy = 10*(py-half), Rx = 10*(px+half), Ly = 10*(py+half); int color = ezWin.X_set_color(rand()%256,rand()%256,rand()%256); ezWin.X_draw_edge(Lx,Uy,Lx,Ly, color); // left side ezWin.X_draw_edge(Lx,Ly,Rx,Ly, color); // bottom ezWin.X_draw_edge(Rx,Ly,Rx,Uy, color); // right side ezWin.X_draw_edge(Rx,Uy,Lx,Uy, color); // top ezWin.X_flush(); // show buffer

  16. Program using Squares // display location and prompt user inputs mvprintw(0,0,"Square: center(%3d,%3d) size(%3d)", px, py, sq.getSize()); mvprintw(0,width-29,"<< The Square (c) 2006 JHS >>"); mvprintw(height-1,0, "ENTER: (+) or (-) to change size or (q) to quit! "); refresh(); usleep(100000); // pause 1/10th second key = getch(); // check for keyboard input, but don't wait if ((key == '+') && (sq.getSize() < 20)) sq.setSize(sq.getSize()+2); // increase sq size if ((key == '-') && (sq.getSize() > 2)) sq.setSize(sq.getSize()-2); // decrease sq size if ((py-sq.getSize()/2 <= 0) || (py+sq.getSize()/2 >= height)) { dy= -dy; // hit top or bottom wall so reverse y direction sq.setDrawChar((char)(33+rand()%94)); // change draw char }

  17. Program using Squares if ((px-sq.getSize()/2 <= 0) || (px+sq.getSize()/2 >= width-1)) dx = -dx; // hit side wall so reverse x direction // erase the square in the ezXwindow color = ezWin.X_set_color_black(); // draw in black ezWin.X_draw_edge(Lx,Uy,Lx,Ly, color); ezWin.X_draw_edge(Lx,Ly,Rx,Ly, color); ezWin.X_draw_edge(Rx,Ly,Rx,Uy, color); ezWin.X_draw_edge(Rx,Uy,Lx,Uy, color); sq.movePosit(px+dx, py+dy); // move sq } while ((key != 'q') && (key != 'Q')); // quit on 'q' or 'Q‘ clear(); refresh(); endwin(); // clean up, and end curses I/O return 0; }

  18. 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

  19. 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; }

  20. 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;

  21. 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; }

  22. 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

  23. 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