💾 Archived View for spam.works › mirrors › textfiles › computers › weiskamp.lst captured on 2023-06-14 at 16:07:01.
-=-=-=-=-=-=-
_AN ICON EDITOR_ by Keith Weiskamp and Loren Heiny [LISTING ONE] /* iconed.c -- a special purpose icon editor. * * This program allows you to interactively edit icons that * can be used in a graphics program. You can create an icon, * edit an existing one, or save an icon pattern to a file. The * program requires a mouse. The icon files produced are of the * form: * ICONWIDTH ICONHEIGHT * one row of icon pattern * next row of icon pattern * . . . * last row of icon pattern * */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> #include <stdarg.h> #include <alloc.h> #include "mouse.h" /* mouse and keyboard routines */ #define BIGICONLEFT 20 /* left side of the big icon pattern */ #define BIGICONTOP 50 /* top side of the big icon pattern */ #define BIGBITSIZE 8 /* big bits are 8 pixels in size */ #define ICONWIDTH 16 /* an icon is a 16x16 pattern */ #define ICONHEIGHT 16 #define ICONLEFT 400 /* small icon pattern located here */ #define ESC 27 /* the value of the ESC key */ /* Here are the functions used in iconed.c: */ void draw_enlarged_icon(void); void toggle_bigbit(int x, int y); void toggle_icons_bit(int x, int y); void init_bigbit(void); void toggle_cursor(int x, int y); void save_icon(void); int read_icon(void); void init_graphics(void); void show_icon(void); /* The global variables */ void *bigbit; /* points to image of a big bit */ /* holds icon pattern */ unsigned char icon[ICONHEIGHT][ICONWIDTH]; void main() { int x, y; int c; read_icon(); /* read in an icon file */ init_graphics(); /* initialize mouse */ if (!initmouse()) { /* must have mouse */ restorecrtmode(); printf("\nMouse not installed"); printf("\nQuitting the Icon Editor"); exit(1); } draw_enlarged_icon(); /* an empty big icon pattern */ show_icon(); /* Draw the icon */ hidemouse(); /* mouse cursor must be turned off */ /* before writing to screen */ outtextxy(BIGICONLEFT, 10, "Press ESC when finished ..."); outtextxy(BIGICONLEFT, BIGICONTOP-20, "Enlarged Icon"); outtextxy(ICONLEFT, BIGICONTOP-20, "Actual Icon"); showmouse(); /* redisplay mouse cursor */ /* get input from mouse/keyboard */ while ((c=waitforinput(LEFT_BUTTON)) != ESC) { if (c < 0) { /* if true mouse button pressed */ getmousecoords(&x, &y); /* get current position */ toggle_bigbit(x, y); /* toggle big bit */ } } hidemouse(); /* turn mouse off and then get out */ closegraph(); /* of graphics mode */ printf("Do you want to save this icon to a file? (y) "); if (getch() != 'n') save_icon(); } void draw_enlarged_icon(void) { /* * This function draws an enlarged view of the icon pattern. * You can click a big bit in this pattern to toggle the * corresponding icons on and off in the actual icon. The * icon is drawn at BIGICONLEFT, BIGICONTOP, to right, bottom. */ int i; int right,bottom; setlinestyle(DOTTED_LINE, 0, NORM_WIDTH); right = 2 * (BIGICONLEFT + (ICONWIDTH-1) * (BIGBITSIZE + NORM_WIDTH)); bottom = BIGICONTOP + ICONHEIGHT * (BIGBITSIZE + NORM_WIDTH); hidemouse(); /* draw vertical and horizonatal dashed */ for (i=0; i<=ICONHEIGHT; i++) line(BIGICONLEFT, BIGICONTOP+i*(BIGBITSIZE+NORM_WIDTH), right, BIGICONTOP+i*(BIGBITSIZE+NORM_WIDTH)); for (i=0; i<=ICONWIDTH; i++) line(BIGICONLEFT+2*(i*(BIGBITSIZE+NORM_WIDTH)), BIGICONTOP, BIGICONLEFT+2*(i*(BIGBITSIZE+NORM_WIDTH)), bottom); showmouse(); init_bigbit(); /* create the big bit image */ } void init_bigbit(void) { /* This function creates the image of a single big bit. This * image is used to toggle the big bits whenever the user * clicks on the big icon pattern. */ int bbx, bby; int i,j; bbx = BIGICONLEFT; bby = BIGICONTOP; /* corner of the big icon */ hidemouse(); /* hide the mouse before drawing */ for (j=bby+1; j<=bby+BIGBITSIZE; j++) { for (i=bbx+1; i<=bbx+2*BIGBITSIZE; i++) { putpixel(i,j,getmaxcolor()); } } /* Set aside memory for the big bit image and then use getimage() to capture its image. */ bigbit = malloc(imagesize(bbx,bby,bbx+2*BIGBITSIZE, bby+BIGBITSIZE)); getimage(bbx+1,bby+1,bbx+2*BIGBITSIZE,bby+BIGBITSIZE, bigbit); /* Erase the big bit by exclusive ORing it with itself */ putimage(bbx+1, bby+1, bigbit, XOR_PUT); showmouse(); /* turn the mouse back on */ } void toggle_bigbit(int x, int y) { /* * This function toggles a big bit and the corresponding pixel * in the icon pattern. The x and y coordinates specify the * mouse position. */ int i, j; int line1, line2, col1, col2; for (j=0; j<ICONHEIGHT; j++) { line1 = BIGICONTOP+j*(BIGBITSIZE+NORM_WIDTH); line2 = BIGICONTOP+(j+1)*(BIGBITSIZE+NORM_WIDTH); if (line1 <= y && y < line2) { for (i=0; i<ICONWIDTH; i++) { col1 = BIGICONLEFT+2*(i*(BIGBITSIZE+NORM_WIDTH)); col2 = BIGICONLEFT+2*((i+1)* (BIGBITSIZE+NORM_WIDTH)); if (col1 <= x && x < col2) { hidemouse(); putimage(col1+1,line1+1,bigbit,XOR_PUT); showmouse(); toggle_icons_bit(i, j); return; } } } } } void toggle_icons_bit(int x, int y) { /* * This function toggles a single pixel in the icon pattern. * The pixel's color and value is changed in the icon array. * Arguments x and y are between 0 and ICONWIDTH or ICONHEIGHT. * The array icon saves the icon pattern. If a location is set * to 1, the corresponding pixel in the icon is displayed. */ hidemouse(); /* if pixel is not black, make it black */ if (getpixel(2*x+ICONLEFT,BIGICONTOP+y) != BLACK) { putpixel(2*x+ICONLEFT,BIGICONTOP+y,BLACK); putpixel(2*x+1+ICONLEFT,BIGICONTOP+y,BLACK); icon[y][x] = 0; } else { /* draw all pixels on with the max color */ putpixel(2*x+ICONLEFT,BIGICONTOP+y,getmaxcolor()); putpixel(2*x+1+ICONLEFT,BIGICONTOP+y,getmaxcolor()); icon[y][x] = 1; } showmouse(); } void save_icon(void) { /* This function writes the icon pattern to a file. The user * is prompted for the filename. The format of the file is * presented at the top of the iconed.c file. */ char filename[80]; FILE *iconfile; int i, j; printf("\nEnter the file name to store the icon in: "); scanf("%s",filename); if ((iconfile = fopen(filename,"w")) == NULL) { printf("Could not open file.\n"); return; } /* Write the header to the file */ fprintf(iconfile, "%d %d\n", ICONWIDTH, ICONHEIGHT); for (j=0; j<ICONHEIGHT; j++) { /* Write the icon */ for (i=0; i<ICONWIDTH; i++) /* pattern to a file */ fprintf(iconfile, "%x ", icon[j][i]); fprintf(iconfile, "\n"); } fclose(iconfile); } int read_icon(void) { /* This function reads an icon file into the icon array and * calls show_icon() to turn the appropriate pixels on. If the * file header doesn't match ICONWIDTH and ICONHEIGHT, the * the file is invalid and the icon is not read. The function * returns a 0 if a file is not read; otherwise 1 is returned. */ char filename[80]; FILE *iconfile; int i, j; int width, height; for (j=0; j<ICONHEIGHT; j++) { /* Initialize icon array */ for (i=0; i<ICONWIDTH; i++) /* to all blanks */ icon[j][i] = 0; } printf("\n\n----------- ICON EDITOR -------------\n\n"); printf("Do you want to edit an existing icon? (y) "); if (getch() == 'n') return(0); printf("\nEnter name of the file to read the icon from: "); scanf("%s",filename); if ((iconfile = fopen(filename,"r")) == NULL) { printf("Cannot open file.\n"); return(0); /* return a failure flag */ } /* Read first line of the icon file. It should contain two numbers that are equal to ICONWIDTH and ICONHEIGHT. */ fscanf(iconfile,"%d %d", &width, &height); if (width != ICONWIDTH || height != ICONHEIGHT) { printf("Incompatible icon file.\n"); return(0); /* return a failure flag */ } for (j=0; j<ICONHEIGHT; j++) { for (i=0; i<ICONWIDTH; i++) fscanf(iconfile, "%x", &icon[j][i]); } fclose(iconfile); return(1); /* return a success flag */ } void init_graphics(void) { /* This function initializes the graphics hardware.