Version bumped to 1.0.0 for the initial release.
[wllib.git] / wllib-SVGA.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <math.h>
6 #include <vga.h>
7 #include "wllib.h"
8
9 #define SAFE_BORDER (4)
10 #define SIZE_MIN (16)
11
12 #define DEFAULT_MODE G320x200x256
13
14 extern void drawgfx(void);
15
16 long xmin,xmax,ymin,ymax;
17 int winx,winy,iniline,minimax=1,xcur,ycur;
18 float coefx,coefy;
19
20 char *pname;
21
22 void wl_line(long x,long y)
23 {
24         if (iniline) {
25                 iniline=0;
26                 if (minimax) { xmin=xmax=x; ymin=ymax=y; }
27                 else {
28                         xcur=rint(coefx*(x-xmin))+SAFE_BORDER;
29                         ycur=winy-SAFE_BORDER-rint(coefy*(y-ymin));
30                         }
31                 }
32         else {
33                 if (minimax) {
34                         if (x<xmin) xmin=x; if (x>xmax) xmax=x;
35                         if (y<ymin) ymin=y; if (y>ymax) ymax=y;
36                         }
37                 else {
38 int xcur2,ycur2;
39                         vga_drawline(xcur,ycur,
40                                         xcur2=rint(coefx*(x-xmin))+SAFE_BORDER,ycur2=winy-SAFE_BORDER-rint(coefy*(y-ymin)));
41                         xcur=xcur2; ycur=ycur2;
42                         }
43                 }
44 }
45
46
47 void wl_done(void)
48 {
49         iniline=1; drawgfx();
50         minimax=0;
51         if (xmax==xmin) xmax++;
52         if (ymax==ymin) ymax++;
53         if (iniline) return;
54         coefx=(float)(winx-2*SAFE_BORDER-1)/(xmax-xmin);
55         coefy=(float)(winy-2*SAFE_BORDER-1)/(ymax-ymin);
56         iniline=1; drawgfx();
57         vga_getch();
58 }
59
60 static void wl_cleanup(void)
61 {
62         vga_setmode(TEXT);
63 }
64
65 void wl_init(int *argcp,char **argv,const char *class)
66 {
67 int i,j;
68 char *s=NULL;
69 int mode=-1;
70         if ((pname=rindex(*argv,'/'))) pname++;
71         else pname=*argv;
72         for (j=i=1;i<*argcp;i++)
73                 if (!strcmp(argv[i],"-m")||!strcmp(argv[i],"--mode")) {
74                         s=argv[++i]; j++;
75                         }
76                 else argv[j++]=argv[i];
77         if (s) mode=vga_getmodenumber(s);
78         if (mode==-1) if ((mode=vga_getdefaultmode())==-1) mode=DEFAULT_MODE;
79         if (vga_setmode(mode)==-1) {
80                 fprintf(stderr,"%s: Mode \"%s\" [%d] not available!\n",pname,
81                                 vga_getmodename(mode),mode);
82                 exit(EXIT_FAILURE);
83                 }
84         atexit(wl_cleanup);
85         winx=vga_getxdim(); winy=vga_getydim();
86         vga_setcolor(vga_white());
87 }