1 /**
2  ** modetest.c ---- test all available graphics modes
3  **
4  ** Copyright (c) 1995 Csaba Biegl, 820 Stirrup Dr, Nashville, TN 37221
5  ** [e-mail: csaba@vuse.vanderbilt.edu]
6  **
7  ** This file is part of the GRX graphics library.
8  **
9  ** The GRX graphics library is free software; you can redistribute it
10  ** and/or modify it under some conditions; see the "copying.grx" file
11  ** for details.
12  **
13  ** This library is distributed in the hope that it will be useful,
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  **
17  **/
18 
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 
24 #include "grx20.h"
25 #include "grxkeys.h"
26 
27 #include "../../test/drawing.h"
28 
PrintInfo(void)29 static void PrintInfo(void)
30 {
31     char aux[81];
32     int x, y;
33 
34     sprintf(aux, " Mode: %dx%d %d bpp ", GrCurrentVideoMode()->width,
35     GrCurrentVideoMode()->height, GrCurrentVideoMode()->bpp);
36     x = (GrMaxX() -
37         GrFontStringWidth(&GrDefaultFont, aux, strlen(aux), GR_BYTE_TEXT)) / 2;
38     y = (GrMaxY() -
39         GrFontStringHeight(&GrDefaultFont, aux, strlen(aux), GR_BYTE_TEXT)) / 2;
40     GrTextXY(x, y, aux, GrWhite(), GrBlack());
41 }
42 
43 typedef struct {
44     int  w,h,bpp;
45 } gvmode;
46 
47 gvmode grmodes[200];
48 int  nmodes = 0;
49 
collectmodes(const GrVideoDriver * drv,gvmode * gp)50 gvmode *collectmodes(const GrVideoDriver *drv,gvmode *gp)
51 {
52 	GrFrameMode fm;
53 	const GrVideoMode *mp;
54 	for(fm =GR_firstGraphicsFrameMode;
55 	      fm <= GR_lastGraphicsFrameMode; fm++) {
56 	    for(mp = GrFirstVideoMode(fm); mp; mp = GrNextVideoMode(mp)) {
57 		gp->w   = mp->width;
58 		gp->h   = mp->height;
59 		gp->bpp = mp->bpp;
60 		gp++;
61 	    }
62 	}
63 	return(gp);
64 }
65 
vmcmp(const void * m1,const void * m2)66 int vmcmp(const void *m1,const void *m2)
67 {
68 	gvmode *md1 = (gvmode *)m1;
69 	gvmode *md2 = (gvmode *)m2;
70 	if(md1->bpp != md2->bpp) return(md1->bpp - md2->bpp);
71 	if(md1->w   != md2->w  ) return(md1->w   - md2->w  );
72 	if(md1->h   != md2->h  ) return(md1->h   - md2->h  );
73 	return(0);
74 }
75 
76 #define LINES   18
77 #define COLUMNS 80
ModeText(int i,int shrt,char * mdtxt)78 void ModeText(int i, int shrt,char *mdtxt) {
79 	switch (shrt) {
80 	  case 2 : sprintf(mdtxt,"%2d) %dx%d ", i+1, grmodes[i].w, grmodes[i].h);
81 		   break;
82 	  case 1 : sprintf(mdtxt,"%2d) %4dx%-4d ", i+1, grmodes[i].w, grmodes[i].h);
83 		   break;
84 	  default: sprintf(mdtxt,"  %2d)  %4dx%-4d ", i+1, grmodes[i].w, grmodes[i].h);
85 		   break;
86 	}
87 	mdtxt += strlen(mdtxt);
88 
89 	if (grmodes[i].bpp > 20)
90 	  sprintf(mdtxt, "%ldM", 1L << (grmodes[i].bpp-20));
91 	else  if (grmodes[i].bpp > 10)
92 	  sprintf(mdtxt, "%ldk", 1L << (grmodes[i].bpp-10));
93 	else
94 	  sprintf(mdtxt, "%ld", 1L << grmodes[i].bpp);
95 	switch (shrt) {
96 	  case 2 : break;
97 	  case 1 : strcat(mdtxt, " col"); break;
98 	  default: strcat(mdtxt, " colors"); break;
99 	}
100 }
101 
ColsCheck(int cols,int ml,int sep)102 int ColsCheck(int cols, int ml, int sep) {
103   int len;
104 
105   len = ml * cols + (cols-1) * sep + 1;
106   return len <= COLUMNS;
107 }
108 
PrintModes(void)109 void PrintModes(void) {
110 	char mdtxt[100];
111 	unsigned int maxlen;
112 	int i, n, shrt, c, cols;
113 
114 	cols = (nmodes+LINES-1) / LINES;
115 	do {
116 	  for (shrt = 0; shrt <= 2; ++shrt) {
117 	    maxlen = 0;
118 	    for (i = 0; i < nmodes; ++i) {
119 	      ModeText(i,shrt,mdtxt);
120 	      if (strlen(mdtxt) > maxlen) maxlen = strlen(mdtxt);
121 	    }
122 	    n = 2;
123 	    if (cols>1 || shrt<2) {
124 	      if (!ColsCheck(cols, maxlen, n)) continue;
125 	      while (ColsCheck(cols, maxlen, n+1) && n < 4) ++n;
126 	    }
127 	    c = 0;
128 	    for (i = 0; i < nmodes; ++i) {
129 	      if (++c == cols) c = 0;
130 	      ModeText(i,shrt,mdtxt);
131 	      printf("%*s%s", (c ? -((int)(maxlen+n)) : -((int)maxlen)), mdtxt, (c ? "" : "\n") );
132 	    }
133 	    if (!c) printf("\n");
134 	    return;
135 	  }
136 	  --cols;
137 	} while (1);
138 }
139 
main(void)140 int main(void)
141 {
142 	static int firstgr = 1;
143 	GrSetDriver(NULL);
144 	if(GrCurrentVideoDriver() == NULL) {
145 	    printf("No graphics driver found\n");
146 	    exit(1);
147 	}
148 	for( ; ; ) {
149 	    int  i,w,h,px,py;
150 	    char m1[41];
151 	    nmodes = (int)(collectmodes(GrCurrentVideoDriver(),grmodes) - grmodes);
152 	    GrSetMode(GR_default_text);
153 	    if(nmodes == 0) {
154 		printf("No graphics modes found\n");
155 		exit(1);
156 	    }
157 	    qsort(grmodes,nmodes,sizeof(grmodes[0]),vmcmp);
158 	    printf(
159 		"Graphics driver: \"%s\"\n"
160 		"  graphics defaults: %dx%d %ld colors\n"
161 		"  text defaults: %dx%d %ld colors\n\n",
162 		GrCurrentVideoDriver()->name,
163 		GrDriverInfo->defgw,
164 		GrDriverInfo->defgh,
165 		(long)GrDriverInfo->defgc,
166 		GrDriverInfo->deftw,
167 		GrDriverInfo->defth,
168 		(long)GrDriverInfo->deftc
169 	    );
170 	    PrintModes();
171 	    printf("\nEnter choice #, or anything else to quit> ");
172 	    fflush(stdout);
173 	    if(!fgets(m1,40,stdin) ||
174 	    (sscanf(m1,"%d",&i) != 1) || (i < 1) || (i > nmodes)) {
175 		exit(0);
176 	    }
177 	    if(firstgr) {
178 		printf(
179                     "When in graphics mode, press any key to return to menu.\n"
180 		    "Now press <CR> to continue..."
181 		);
182 		fflush(stdout);
183 		fgets(m1,40,stdin);
184 		firstgr = 0;
185 	    }
186 	    i--;
187 	    GrSetMode(
188 		GR_width_height_bpp_graphics,
189 		grmodes[i].w,
190 		grmodes[i].h,
191 		grmodes[i].bpp
192 	    );
193 	    if(grmodes[i].bpp<15) {
194 		w = GrScreenX() >> 1;
195 		h = GrScreenY() >> 1;
196 		px = w + 5;
197 		py = h + 5;
198 		w -= 10;
199 		h -= 10;
200 		drawing(
201 		    5,5,w,h,
202 		    GrBlack(),
203 		    GrWhite()
204 		);
205 		drawing(
206 		    px,5,w,h,
207 		    GrAllocColor(255,0,0),
208 		    GrAllocColor(0,255,0)
209 		);
210 		drawing(
211 		    5,py,w,h,
212 		    GrAllocColor(0,0,255),
213 		    GrAllocColor(255,255,0)
214 		);
215 		drawing(
216 		    px,py,w,h,
217 		    GrAllocColor(255,0,255),
218 		    GrAllocColor(0,255,255)
219 		);
220 	    } else {
221 		int y,sx;
222 		sx=GrScreenX()>>2;
223 		for(y=0;y<GrScreenY();y++) {
224 		    int yy = y & 255;
225 		    GrHLine(0,sx-1,y,GrBuildRGBcolorT(yy,0,0));
226 		    GrHLine(sx,2*sx-1,y,GrBuildRGBcolorT(0,yy,0));
227 		    GrHLine(2*sx,3*sx-1,y,GrBuildRGBcolorT(0,0,yy));
228 		    GrHLine(3*sx,4*sx-1,y,GrBuildRGBcolorT(yy,yy,yy));
229 		}
230 	    }
231             PrintInfo();
232             GrKeyRead();
233 	}
234 	return 0;
235 }
236