1 /* ---------------------------------------------------------------------- *
2  * lcsvga.c
3  * This file is part of lincity.
4  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
5  * ---------------------------------------------------------------------- */
6 #include "lcconfig.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include "lcstring.h"
11 #include "common.h"
12 #include "lctypes.h"
13 #include "lin-city.h"
14 #include "mouse.h"
15 #include "engglobs.h"
16 #include "engine.h"
17 #include "screen.h"
18 #include "lcintl.h"
19 #include "fileutil.h"
20 
21 unsigned char mouse_pointer[] = {
22     255, 255, 255, 255, 1, 1, 1, 1,
23     255, 1, 1, 1, 255, 255, 1, 1,
24     255, 1, 255, 255, 255, 1, 255, 1,
25     255, 1, 255, 1, 1, 255, 1, 1,
26     1, 255, 255, 1, 1, 1, 255, 1,
27     1, 255, 1, 255, 1, 1, 1, 1,
28     1, 1, 255, 1, 255, 1, 1, 1,
29     1, 1, 1, 1, 1, 1, 1, 1
30 };
31 unsigned char under_mouse_pointer[8 * 8];
32 
33 static int vga_mode = -1;
34 
35 void lc_mouse_handler(int button, int dx, int dy, int dz,
36 		      int drx, int dry, int drz);
37 #if defined (commentout)
38 void lc_mouse_handler(int button, int dx, int dy);
39 #endif
40 
41 /* ---------------------------------------------------------------------- *
42  * Profiling routines
43  * ---------------------------------------------------------------------- */
44 #ifdef CS_PROFILE
45 void
FPgl_write(int a,int b,char * s)46 FPgl_write (int a, int b, char *s)
47 {
48     gl_write (a, b, s);
49 }
50 void
FPgl_getbox(int a,int b,int c,int d,void * e)51 FPgl_getbox (int a, int b, int c, int d, void *e)
52 {
53     gl_getbox (a, b, c, d, e);
54 }
55 void
FPgl_putbox(int a,int b,int c,int d,void * e)56 FPgl_putbox (int a, int b, int c, int d, void *e)
57 {
58     gl_putbox (a, b, c, d, e);
59 }
60 void
FPgl_fillbox(int a,int b,int c,int d,int e)61 FPgl_fillbox (int a, int b, int c, int d, int e)
62 {
63     gl_fillbox (a, b, c, d, e);
64 }
65 void
FPgl_hline(int a,int b,int c,int d)66 FPgl_hline (int a, int b, int c, int d)
67 {
68     gl_hline (a, b, c, d);
69 }
70 void
FPgl_line(int a,int b,int c,int d,int e)71 FPgl_line (int a, int b, int c, int d, int e)
72 {
73     gl_line (a, b, c, d, e);
74 }
75 void
FPgl_setpixel(int a,int b,int c)76 FPgl_setpixel (int a, int b, int c)
77 {
78     gl_setpixel (a, b, c);
79 }
80 #endif
81 
82 /* ---------------------------------------------------------------------- *
83  * Mouse routines
84  * ---------------------------------------------------------------------- */
85 void
mouse_setup(void)86 mouse_setup(void)
87 {
88     int m;
89     m = mouse_init("/dev/mouse", vga_getmousetype(), MOUSE_DEFAULTSAMPLERATE);
90     mouse_setxrange(0, 640 - 1);
91     mouse_setyrange(0, 480 - 1);
92     mouse_setwrap(MOUSE_NOWRAP);
93     if (m != 0) {
94 	do_error("Can't initialise mouse");
95     }
96     cs_mouse_x = 0;
97     cs_mouse_y = 0;
98     cs_mouse_button = 0;
99     cs_mouse_xmax = 640 - 1;
100     cs_mouse_ymax = 480 - 1;
101 
102     /* GCS -- This is meant to fix the upper left cursor problem,
103      *        but doesnt.  May 11, 2003 				*/
104     mox = 1; moy = 1;
105     Fgl_getbox(mox, moy, 8, 8, under_mouse_pointer);
106 
107     mouse_seteventhandler(lc_mouse_handler);
108 }
109 
110 void
mouse_set_range(int width,int height)111 mouse_set_range (int width, int height)
112 {
113     debug_printf ("setting mouse range: %d %d\n", width, height);
114     mouse_setxrange(0, width - 1);
115     mouse_setyrange(0, height - 1);
116     cs_mouse_xmax = width - 1;
117     cs_mouse_ymax = height - 1;
118 }
119 
120 void
set_vga_mode(void)121 set_vga_mode (void)
122 {
123   /* If vga_mode is not set by command line, use vga_getdefaultmode()
124      to get mode.  Supported modes are 10, 11, 12 or 13.
125    */
126   if (vga_mode == -1) {
127     vga_mode = vga_getdefaultmode();
128     if (vga_mode > 13 || vga_mode < 10)
129       vga_mode = 10;
130   }
131   vga_setmode (vga_mode);
132   gl_setcontextvga (vga_mode);
133   init_mouse();
134   switch (vga_mode)
135     {
136     case 10:
137       resize_geometry (640,480);
138       break;
139     case 11:
140       resize_geometry (800,600);
141       break;
142     case 12:
143       resize_geometry (1024,768);
144       break;
145     case 13:
146       resize_geometry (1280,1024);
147       break;
148     default:
149       do_error ("illegal vga mode");
150     }
151 }
152 
153 void
lc_mouse_handler(int button,int dx,int dy,int dz,int drx,int dry,int drz)154 lc_mouse_handler(int button, int dx, int dy, int dz,
155 		 int drx, int dry, int drz)
156 {
157     static int old_mouse_button = 0;
158     int mouse_button_change = old_mouse_button ^ button;
159 
160     if (mouse_button_change & MOUSE_LEFTBUTTON) {
161 	if (button & MOUSE_LEFTBUTTON) {
162 	    cs_mouse_handler(LC_MOUSE_LEFTBUTTON | LC_MOUSE_PRESS, dx, dy);
163 	} else {
164 	    cs_mouse_handler(LC_MOUSE_LEFTBUTTON | LC_MOUSE_RELEASE, dx,
165 			     dy);
166 	}
167 	dx = dy = 0;
168     }
169     if (mouse_button_change & MOUSE_MIDDLEBUTTON) {
170 	if (button & MOUSE_MIDDLEBUTTON) {
171 	    cs_mouse_handler(LC_MOUSE_MIDDLEBUTTON | LC_MOUSE_PRESS, dx,
172 			     dy);
173 	} else {
174 	    cs_mouse_handler(LC_MOUSE_MIDDLEBUTTON | LC_MOUSE_RELEASE, dx,
175 			     dy);
176 	}
177 	dx = dy = 0;
178     }
179     if (mouse_button_change & MOUSE_RIGHTBUTTON) {
180 	if (button & MOUSE_RIGHTBUTTON) {
181 	    cs_mouse_handler(LC_MOUSE_RIGHTBUTTON | LC_MOUSE_PRESS, dx,
182 			     dy);
183 	} else {
184 	    cs_mouse_handler(LC_MOUSE_RIGHTBUTTON | LC_MOUSE_RELEASE, dx,
185 			     dy);
186 	}
187 	dx = dy = 0;
188     }
189     if (dx || dy) {
190 	cs_mouse_handler(0, dx, dy);
191     }
192     old_mouse_button = button;
193 }
194 
195 int
lc_get_keystroke(void)196 lc_get_keystroke (void)
197 {
198     return vga_getkey ();
199 }
200 
201 
202 /* ---------------------------------------------------------------------- *
203  * Other routines
204  * ---------------------------------------------------------------------- */
205 void
parse_args(int argc,char ** argv)206 parse_args (int argc, char **argv)
207 {
208   int option;
209   extern char *optarg;
210 
211   /* GCS FIX:  Need to print usage and exit when illegal option spec'd */
212   while ((option = getopt (argc, argv, "wR:G:B:m:")) != EOF)
213     {
214       switch (option)
215 	{
216 	case 'm':
217 	  sscanf (optarg, "%d", &vga_mode);
218 	  if (vga_mode > 13 || vga_mode < 10)
219 	    vga_mode = -1;
220 	  break;
221 	case 'w':
222 	  gamma_correct_red = GAMMA_CORRECT_RED;
223 	  gamma_correct_green = GAMMA_CORRECT_GREEN;
224 	  gamma_correct_blue = GAMMA_CORRECT_BLUE;
225 	  break;
226 	case 'R':
227 	  sscanf (optarg, "%f", &gamma_correct_red);
228 	  break;
229 	case 'G':
230 	  sscanf (optarg, "%f", &gamma_correct_green);
231 	  break;
232 	case 'B':
233 	  sscanf (optarg, "%f", &gamma_correct_blue);
234 	  break;
235 	}
236     }
237 }
238 
239 void
HandleError(char * description,int degree)240 HandleError (char *description, int degree)
241 {
242   fprintf (stderr,
243 	   _("An error has occurred.  The description is below...\n"));
244   fprintf (stderr, "%s\n", description);
245 
246   if (degree == FATAL) {
247       fprintf (stderr, _("Program aborting...\n"));
248       exit (-1);
249     }
250 }
251 
252 void
init_mouse(void)253 init_mouse (void)
254 {
255     mouse_setup ();
256 }
257 
258 void
setcustompalette(void)259 setcustompalette (void)
260 {
261     char s[100];
262     int i, n, r, g, b, flag[256];
263     FILE *inf;
264     Palette pal;
265     for (i = 0; i < 256; i++)
266 	flag[i] = 0;
267     if ((inf = fopen (colour_pal_file, "r")) == 0)
268     {
269 	printf ("The colour palette file <%s>... ", colour_pal_file);
270 	do_error ("Can't find it.");
271     }
272     while (feof (inf) == 0)
273     {
274 	fgets (s, 99, inf);
275 	if (sscanf (s, "%d %d %d %d", &n, &r, &g, &b) == 4)
276 	{
277 	    pal.color[n].red = r;
278 	    pal.color[n].green = g;
279 	    pal.color[n].blue = b;
280 	    flag[n] = 1;
281 	}
282     }
283     fclose (inf);
284     for (i = 0; i < 256; i++)
285     {
286 	if (flag[i] == 0)
287 	{
288 	    printf ("Colour %d not loaded\n", i);
289 	    do_error ("Can't continue");
290 	}
291 	pal.color[i].red = (unsigned char) ((pal.color[i].red
292 					     * (1 - gamma_correct_red)) + (64 * sin ((float) pal.color[i].red
293 										     * M_PI / 128)) * gamma_correct_red);
294 
295 	pal.color[i].green = (unsigned char) ((pal.color[i].green
296 					       * (1 - gamma_correct_green)) + (64 * sin ((float) pal.color[i].green
297 											 * M_PI / 128)) * gamma_correct_green);
298 
299 	pal.color[i].blue = (unsigned char) ((pal.color[i].blue
300 					      * (1 - gamma_correct_blue)) + (64 * sin ((float) pal.color[i].blue
301 										       * M_PI / 128)) * gamma_correct_blue);
302     }
303     gl_setpalette (&pal);
304 }
305