From cb5b590f27da9567d0de1ea80c7539a6d3ddf171 Mon Sep 17 00:00:00 2001 From: Ian C Date: Mon, 18 May 2020 21:14:00 +0000 Subject: Fixed up spred so it works on modern displays. --- spred.c | 328 +++++++++++++++++++++++++++++----------------------------------- 1 file changed, 149 insertions(+), 179 deletions(-) (limited to 'spred.c') diff --git a/spred.c b/spred.c index b1f36c8..b43ec25 100644 --- a/spred.c +++ b/spred.c @@ -4,11 +4,16 @@ #include #include +#include #include #include -#include #include +#include +#include + +#include + #include "Xbit.h" #define WINX 270 @@ -23,12 +28,12 @@ #define ABS(x) ((x)<0 ? (-(x)) : (x)) #define SGN(x) ((x) ? ((x)/ABS(x)) : (0)) -XSizeHints size_hints; -int black,white; -Display *disp; -Window window; -Colormap cm; -XFontStruct *font; +static XSizeHints size_hints; +static ulong black,white; +static Display *disp; +static Window window; +static Colormap cm; +static XFontStruct *font; #define CMAP_R 4 @@ -86,28 +91,39 @@ XFontStruct *font; #define RND_MIN 255 -typedef struct +typedef struct Button { int x,y,w,h; char *text; - int (*redraw)(); - int (*callback)(); + void (*redraw)(struct Button *b); + void (*callback)(void); } Button; -int RdCmapBut(),CmapBut(); -int RdSprSaveBut(),SprSaveBut(); -int RdSprLoadBut(),SprLoadBut(); -int RdSetCol(),SetCol(); -int RdSpreadBut(),SpreadBut(); -int RdLoadCmapBut(),LoadCmapBut(); -int RdDrawMode(),DrawMode(); -int RdClearToBut(),ClearToBut(); -int RdClearBut(),ClearBut(); -int RdSetSprBut(),SetSprBut(); -int RdQuitBut(),QuitBut(); +static void RdCmapBut(Button *b); +static void CmapBut(); +static void RdSprSaveBut(Button *b); +static void SprSaveBut(); +static void RdSprLoadBut(Button *b); +static void SprLoadBut(); +static void RdSetCol(Button *b); +static void SetCol(); +static void RdSpreadBut(Button *b); +static void SpreadBut(); +static void RdLoadCmapBut(Button *b); +static void LoadCmapBut(); +static void RdDrawMode(Button *b); +static void DrawMode(); +static void RdClearToBut(Button *b); +static void ClearToBut(); +static void RdClearBut(Button *b); +static void ClearBut(); +static void RdSetSprBut(Button *b); +static void SetSprBut(); +static void RdQuitBut(Button *b); +static void QuitBut(); #define NO_BUT 11 -Button butlist[NO_BUT]= +static Button butlist[NO_BUT]= { {1,10,90,20, "Save Cmap", @@ -155,27 +171,45 @@ Button butlist[NO_BUT]= QuitBut} }; -#define MAXSTRLEN 64 +static void SetCmFade(int c1, int c2, XColor xc[], int fl); +static void MainLoop(int argc, char *argv[]); +static void NullStr(void); +static void RedrawAll(void); +static void RedrawButs(void); +static void RedrawCmap(void); +static void RedrawSel(void); +static void RedrawStr(void); +static void RedrawSpr(void); +static void SpritePress(int x, int y, int b); +static void SprCircle(int x, int y, int rx, int ry, int c); +static void SprLine(int p1x,int p1y,int p2x,int p2y,int c); +static void SprSet(int x,int y,int c); +static void SprPlot(int x,int y,int c); +static void SprFill(int x, int y, int c, int bc); +static void RangeCols(int a, int b); +static void ButtonPoll(Button *b, int n, int x, int y); +static void ButtonText(const Button *b); +static void ButtonRedraw(Button *b); +static int ColPress(int x,int y, int *c); -int quit=False; -int selcol; -int range=False; -int width=1; -int drawmode=PLOT; -int last_x=-1,last_y=-1; -XColor xc[256]; -int namelen=0; -char name[MAXSTRLEN]; -int SPR_W=MAX_SPR_W; -int SPR_H=MAX_SPR_H; -int GRID_W=SETGW(MAX_SPR_W,MAX_SPR_H); -unsigned char sprite[MAX_SPR_W][MAX_SPR_H]; - - -int main(argc,argv) -int argc; -char *argv[]; +#define MAXSTRLEN 64 +static int quit=False; +static int selcol; +static int range=False; +static int width=1; +static int drawmode=PLOT; +static int last_x=-1,last_y=-1; +static XColor xc[256]; +static int namelen=0; +static char name[MAXSTRLEN]; +static int SPR_W=MAX_SPR_W; +static int SPR_H=MAX_SPR_H; +static int GRID_W=SETGW(MAX_SPR_W,MAX_SPR_H); +static unsigned char sprite[MAX_SPR_W][MAX_SPR_H]; + + +int main(int argc, char *argv[]) { XEvent event; unsigned long evmask; @@ -201,11 +235,10 @@ char *argv[]; disp=GetDisplay(); - cm=XCreateColormap(disp,window,DefaultVisual(disp,0),AllocAll); - XSetWindowColormap(disp,window,cm); - font=XUseFont("fixed"); + cm = DefaultColormap(disp, DefaultScreen(disp)); + SetCmFade(W_MAX,W_MIN,xc,DoRed|DoBlue|DoGreen); SetCmFade(R_MAX,R_MIN,xc,DoRed); SetCmFade(B_MAX,B_MIN,xc,DoBlue); @@ -221,31 +254,26 @@ char *argv[]; xc[f].red=RND(0xff)*0xff; xc[f].green=RND(0xff)*0xff; xc[f].blue=RND(0xff)*0xff; + XAllocColor(disp, cm, &xc[f]); } - black=1; - white=0; - selcol=white; + selcol=1; - xc[black].pixel=black; - xc[black].flags=DoRed|DoBlue|DoGreen; - xc[black].red=xc[black].blue=xc[black].green=0; + xc[0].pixel=black; + xc[0].flags=DoRed|DoBlue|DoGreen; + xc[0].red=xc[0].blue=xc[0].green=0; - xc[white].pixel=white; - xc[white].flags=DoRed|DoBlue|DoGreen; - xc[white].red=xc[white].blue=xc[white].green=0xffff; + xc[1].pixel=white; + xc[1].flags=DoRed|DoBlue|DoGreen; + xc[1].red=xc[1].blue=xc[1].green=0xffff; - XStoreColors(disp,cm,xc,256); + /* XStoreColors(disp,cm,xc,256); */ MainLoop(argc,argv); } -SetCmFade(c1,c2,xc,fl) -int c1,c2; -XColor xc[]; -int fl; - +static void SetCmFade(int c1, int c2, XColor xc[], int fl) { int f,n,r; @@ -254,7 +282,6 @@ int fl; for(f=c1,r=0xff;f<=c2;f++,r-=n) { - xc[f].pixel=f; xc[f].flags=DoRed|DoBlue|DoGreen; if (fl&DoRed) @@ -272,22 +299,20 @@ int fl; else xc[f].blue=0; + XAllocColor(disp, cm, &xc[f]); } } /* Main Loop */ -MainLoop(argc,argv) -int argc; -char *argv[]; - +static void MainLoop(int argc, char *argv[]) { XEvent e; KeySym ks; char keybuf[10]; int f,x,y; - name[0]=NULL; + name[0]=0; for(x=0;x=SPR_W)||(y<0)||(y>=SPR_H)) return; @@ -783,22 +795,18 @@ int x,y,c; } -SprPlot(x,y,c) -int x,y,c; - +static void SprPlot(int x,int y,int c) { - XPlot(SPR_X+x,SPR_Y+y,c); + XPlot(SPR_X+x,SPR_Y+y,xc[c].pixel); x=GRID_X+(x*GRID_W)+1; y=GRID_Y+(y*GRID_W)+1; - XFillBox(x,y,GRID_W-1,GRID_W-1,c); + XFillBox(x,y,GRID_W-1,GRID_W-1,xc[c].pixel); } -SprFill(x,y,c,bc) -int x,y,c,bc; - +static void SprFill(int x, int y, int c, int bc) { if ((x<0)||(x>=SPR_W)||(y<0)||(y>=SPR_H)||(sprite[x][y]==c)|| (sprite[x][y]!=bc)) @@ -813,9 +821,7 @@ int x,y,c,bc; } -ColPress(x,y,c) -int x,y,*c; - +static int ColPress(int x,int y, int *c) { int lc; @@ -836,9 +842,7 @@ int x,y,*c; } -RangeCols(a,b) -int a,b; - +static void RangeCols(int a, int b) { int f,n,rc,gc,bc; @@ -860,16 +864,16 @@ int a,b; xc[a+f].red=xc[a].red+(f*rc); xc[a+f].green=xc[a].green+(f*gc); xc[a+f].blue=xc[a].blue+(f*bc); + XAllocColor(disp, cm, &xc[a+f]); } - XStoreColors(disp,cm,xc,256); + + /* XStoreColors(disp,cm,xc,256); */ } /* Button Routines */ -isin(x,y,bx,by,bw,bh) -int x,y,bx,by,bw,bh; - +static int isin(int x, int y, int bx, int by, int bw, int bh) { if ((x>=bx)&&(x<=bx+bw)&&(y>=by)&&(y<=by+bh)) return(True); @@ -877,11 +881,7 @@ int x,y,bx,by,bw,bh; return(False); } -ButtonPoll(b,n,x,y) -Button b[]; -int n; -int x,y; - +static void ButtonPoll(Button *b, int n, int x, int y) { int f; @@ -894,9 +894,7 @@ int x,y; } } -ButtonText(b) -Button *b; - +static void ButtonText(const Button *b) { XFillBox(b->x,b->y,b->w,b->h,black); XBox(b->x,b->y,b->w,b->h,white); @@ -907,9 +905,7 @@ Button *b; } -ButtonRedraw(b) -Button *b; - +static void ButtonRedraw(Button *b) { XBox(b->x,b->y,b->w,b->h,white); @@ -919,15 +915,13 @@ Button *b; /* Button Callbacks and redraws */ -int RdCmapBut(b) -Button *b; - +static void RdCmapBut(Button *b) { ButtonText(b); } -int CmapBut() +static void CmapBut(void) { unsigned short us; @@ -953,16 +947,13 @@ int CmapBut() } -int RdLoadCmapBut(b) -Button *b; - +static void RdLoadCmapBut(Button *b) { ButtonText(b); } -int LoadCmapBut() - +static void LoadCmapBut(void) { unsigned short us; int fd,f; @@ -981,24 +972,23 @@ int LoadCmapBut() xc[f].green=ntohs(us); read(fd,&us,sizeof(unsigned short)); xc[f].blue=ntohs(us); + XAllocColor(disp, cm, &xc[f]); } close(fd); - XStoreColors(disp,cm,xc,256); + /* XStoreColors(disp,cm,xc,256); */ + RedrawAll(); } -int RdSprSaveBut(b) -Button *b; - +static void RdSprSaveBut(Button *b) { ButtonText(b); } -int SprSaveBut() - +static void SprSaveBut(void) { int fd,x,y; unsigned short s; @@ -1025,16 +1015,13 @@ int SprSaveBut() } -int RdSprLoadBut(b) -Button *b; - +static void RdSprLoadBut(Button *b) { ButtonText(b); } -int SprLoadBut() - +static void SprLoadBut(void) { int fd,x,y; unsigned short s; @@ -1071,16 +1058,13 @@ int SprLoadBut() } -int RdSetCol(b) -Button *b; - +static void RdSetCol(Button *b) { ButtonText(b); } -int SetCol() - +static void SetCol(void) { static char *dum="0"; char *r,*g,*b,s[MAXSTRLEN]; @@ -1103,15 +1087,17 @@ int SetCol() xc[selcol].green=(int)strtol(g,NULL,0)*0xff; xc[selcol].blue=(int)strtol(b,NULL,0)*0xff; + XAllocColor(disp, cm, &xc[selcol]); + NullStr(); - XStoreColors(disp,cm,xc,256); -} + RedrawAll(); + /* XStoreColors(disp,cm,xc,256); */ +} -int RdDrawMode(b) -Button *b; +static void RdDrawMode(Button *b) { switch(drawmode) { @@ -1151,8 +1137,7 @@ Button *b; } -int DrawMode() - +static void DrawMode(void) { if(++drawmode==ENDMODE) drawmode=PLOT; @@ -1164,31 +1149,25 @@ int DrawMode() } -int RdSpreadBut(b) -Button *b; - +static void RdSpreadBut(Button *b) { ButtonText(b); } -int SpreadBut() - +static void SpreadBut(void) { range=True; } -int RdClearToBut(b) -Button *b; - +static void RdClearToBut(Button *b) { ButtonText(b); } -int ClearToBut() - +static void ClearToBut(void) { int x,y; @@ -1198,16 +1177,13 @@ int ClearToBut() } -int RdClearBut(b) -Button *b; - +static void RdClearBut(Button *b) { ButtonText(b); } -int ClearBut() - +static void ClearBut(void) { int x,y; @@ -1217,16 +1193,13 @@ int ClearBut() } -int RdSetSprBut(b) -Button *b; - +static void RdSetSprBut(Button *b) { ButtonText(b); } -int SetSprBut() - +static void SetSprBut(void) { int n; @@ -1249,16 +1222,13 @@ int SetSprBut() } -int RdQuitBut(b) -Button *b; - +static void RdQuitBut(Button *b) { ButtonText(b); } -int QuitBut() - +static void QuitBut(void) { quit=True; } -- cgit v1.2.3