Main Page | Data Structures | Directories | File List | Data Fields | Globals

lcsvga.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------- *
00002  * lcsvga.c
00003  * This file is part of lincity.
00004  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
00005  * ---------------------------------------------------------------------- */
00006 #include "lcconfig.h"
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <math.h>
00010 #include "lcstring.h"
00011 #include "common.h"
00012 #include "lctypes.h"
00013 #include "lin-city.h"
00014 #include "mouse.h"
00015 #include "engglobs.h"
00016 #include "engine.h"
00017 #include "screen.h"
00018 #include "lcintl.h"
00019 #include "fileutil.h"
00020 
00021 unsigned char mouse_pointer[] = {
00022     255, 255, 255, 255, 1, 1, 1, 1,
00023     255, 1, 1, 1, 255, 255, 1, 1,
00024     255, 1, 255, 255, 255, 1, 255, 1,
00025     255, 1, 255, 1, 1, 255, 1, 1,
00026     1, 255, 255, 1, 1, 1, 255, 1,
00027     1, 255, 1, 255, 1, 1, 1, 1,
00028     1, 1, 255, 1, 255, 1, 1, 1,
00029     1, 1, 1, 1, 1, 1, 1, 1
00030 };
00031 unsigned char under_mouse_pointer[8 * 8];
00032 
00033 static int vga_mode = -1;
00034 
00035 void lc_mouse_handler(int button, int dx, int dy, int dz,
00036                       int drx, int dry, int drz);
00037 #if defined (commentout)
00038 void lc_mouse_handler(int button, int dx, int dy);
00039 #endif
00040 
00041 /* ---------------------------------------------------------------------- *
00042  * Profiling routines
00043  * ---------------------------------------------------------------------- */
00044 #ifdef CS_PROFILE
00045 void
00046 FPgl_write (int a, int b, char *s)
00047 {
00048     gl_write (a, b, s);
00049 }
00050 void
00051 FPgl_getbox (int a, int b, int c, int d, void *e)
00052 {
00053     gl_getbox (a, b, c, d, e);
00054 }
00055 void
00056 FPgl_putbox (int a, int b, int c, int d, void *e)
00057 {
00058     gl_putbox (a, b, c, d, e);
00059 }
00060 void
00061 FPgl_fillbox (int a, int b, int c, int d, int e)
00062 {
00063     gl_fillbox (a, b, c, d, e);
00064 }
00065 void
00066 FPgl_hline (int a, int b, int c, int d)
00067 {
00068     gl_hline (a, b, c, d);
00069 }
00070 void
00071 FPgl_line (int a, int b, int c, int d, int e)
00072 {
00073     gl_line (a, b, c, d, e);
00074 }
00075 void
00076 FPgl_setpixel (int a, int b, int c)
00077 {
00078     gl_setpixel (a, b, c);
00079 }
00080 #endif
00081 
00082 /* ---------------------------------------------------------------------- *
00083  * Mouse routines
00084  * ---------------------------------------------------------------------- */
00085 void
00086 mouse_setup(void)
00087 {
00088     int m;
00089     m = mouse_init("/dev/mouse", vga_getmousetype(), MOUSE_DEFAULTSAMPLERATE);
00090     mouse_setxrange(0, 640 - 1);
00091     mouse_setyrange(0, 480 - 1);
00092     mouse_setwrap(MOUSE_NOWRAP);
00093     if (m != 0) {
00094         do_error("Can't initialise mouse");
00095     }
00096     cs_mouse_x = 0;
00097     cs_mouse_y = 0;
00098     cs_mouse_button = 0;
00099     cs_mouse_xmax = 640 - 1;
00100     cs_mouse_ymax = 480 - 1;
00101 
00102     /* GCS -- This is meant to fix the upper left cursor problem,
00103      *        but doesnt.  May 11, 2003                                 */
00104     mox = 1; moy = 1;
00105     Fgl_getbox(mox, moy, 8, 8, under_mouse_pointer);
00106 
00107     mouse_seteventhandler(lc_mouse_handler);
00108 }
00109 
00110 void
00111 mouse_set_range (int width, int height)
00112 {
00113     debug_printf ("setting mouse range: %d %d\n", width, height);
00114     mouse_setxrange(0, width - 1);
00115     mouse_setyrange(0, height - 1);
00116     cs_mouse_xmax = width - 1;
00117     cs_mouse_ymax = height - 1;
00118 }
00119 
00120 void
00121 set_vga_mode (void)
00122 {
00123   /* If vga_mode is not set by command line, use vga_getdefaultmode() 
00124      to get mode.  Supported modes are 10, 11, 12 or 13.
00125    */
00126   if (vga_mode == -1) {
00127     vga_mode = vga_getdefaultmode();
00128     if (vga_mode > 13 || vga_mode < 10)
00129       vga_mode = 10;
00130   }
00131   vga_setmode (vga_mode);
00132   gl_setcontextvga (vga_mode);
00133   init_mouse();
00134   switch (vga_mode) 
00135     {
00136     case 10:
00137       resize_geometry (640,480);
00138       break;
00139     case 11:
00140       resize_geometry (800,600);
00141       break;
00142     case 12:
00143       resize_geometry (1024,768);
00144       break;
00145     case 13:
00146       resize_geometry (1280,1024);
00147       break;
00148     default:
00149       do_error ("illegal vga mode");
00150     }
00151 }
00152 
00153 void
00154 lc_mouse_handler(int button, int dx, int dy, int dz,
00155                  int drx, int dry, int drz)
00156 {
00157     static int old_mouse_button = 0;
00158     int mouse_button_change = old_mouse_button ^ button;
00159 
00160     if (mouse_button_change & MOUSE_LEFTBUTTON) {
00161         if (button & MOUSE_LEFTBUTTON) {
00162             cs_mouse_handler(LC_MOUSE_LEFTBUTTON | LC_MOUSE_PRESS, dx, dy);
00163         } else {
00164             cs_mouse_handler(LC_MOUSE_LEFTBUTTON | LC_MOUSE_RELEASE, dx,
00165                              dy);
00166         }
00167         dx = dy = 0;
00168     }
00169     if (mouse_button_change & MOUSE_MIDDLEBUTTON) {
00170         if (button & MOUSE_MIDDLEBUTTON) {
00171             cs_mouse_handler(LC_MOUSE_MIDDLEBUTTON | LC_MOUSE_PRESS, dx,
00172                              dy);
00173         } else {
00174             cs_mouse_handler(LC_MOUSE_MIDDLEBUTTON | LC_MOUSE_RELEASE, dx,
00175                              dy);
00176         }
00177         dx = dy = 0;
00178     }
00179     if (mouse_button_change & MOUSE_RIGHTBUTTON) {
00180         if (button & MOUSE_RIGHTBUTTON) {
00181             cs_mouse_handler(LC_MOUSE_RIGHTBUTTON | LC_MOUSE_PRESS, dx,
00182                              dy);
00183         } else {
00184             cs_mouse_handler(LC_MOUSE_RIGHTBUTTON | LC_MOUSE_RELEASE, dx,
00185                              dy);
00186         }
00187         dx = dy = 0;
00188     }
00189     if (dx || dy) {
00190         cs_mouse_handler(0, dx, dy);
00191     }
00192     old_mouse_button = button;
00193 }
00194 
00195 int 
00196 lc_get_keystroke (void)
00197 {
00198     return vga_getkey ();
00199 }
00200 
00201 
00202 /* ---------------------------------------------------------------------- *
00203  * Other routines
00204  * ---------------------------------------------------------------------- */
00205 void
00206 parse_args (int argc, char **argv)
00207 {
00208   int option;
00209   extern char *optarg;
00210 
00211   /* GCS FIX:  Need to print usage and exit when illegal option spec'd */
00212   while ((option = getopt (argc, argv, "wR:G:B:m:")) != EOF)
00213     {
00214       switch (option)
00215         {
00216         case 'm':
00217           sscanf (optarg, "%d", &vga_mode);
00218           if (vga_mode > 13 || vga_mode < 10)
00219             vga_mode = -1;
00220           break;
00221         case 'w':
00222           gamma_correct_red = GAMMA_CORRECT_RED;
00223           gamma_correct_green = GAMMA_CORRECT_GREEN;
00224           gamma_correct_blue = GAMMA_CORRECT_BLUE;
00225           break;
00226         case 'R':
00227           sscanf (optarg, "%f", &gamma_correct_red);
00228           break;
00229         case 'G':
00230           sscanf (optarg, "%f", &gamma_correct_green);
00231           break;
00232         case 'B':
00233           sscanf (optarg, "%f", &gamma_correct_blue);
00234           break;
00235         }
00236     }
00237 }
00238 
00239 void
00240 HandleError (char *description, int degree)
00241 {
00242   fprintf (stderr,
00243            _("An error has occurred.  The description is below...\n"));
00244   fprintf (stderr, "%s\n", description);
00245 
00246   if (degree == FATAL) {
00247       fprintf (stderr, _("Program aborting...\n"));
00248       exit (-1);
00249     }
00250 }
00251 
00252 void
00253 init_mouse (void)
00254 {
00255     mouse_setup ();
00256 }
00257 
00258 void
00259 setcustompalette (void)
00260 {
00261     char s[100];
00262     int i, n, r, g, b, flag[256];
00263     FILE *inf;
00264     Palette pal;
00265     for (i = 0; i < 256; i++)
00266         flag[i] = 0;
00267     if ((inf = fopen (colour_pal_file, "r")) == 0)
00268     {
00269         printf ("The colour palette file <%s>... ", colour_pal_file);
00270         do_error ("Can't find it.");
00271     }
00272     while (feof (inf) == 0)
00273     {
00274         fgets (s, 99, inf);
00275         if (sscanf (s, "%d %d %d %d", &n, &r, &g, &b) == 4)
00276         {
00277             pal.color[n].red = r;
00278             pal.color[n].green = g;
00279             pal.color[n].blue = b;
00280             flag[n] = 1;
00281         }
00282     }
00283     fclose (inf);
00284     for (i = 0; i < 256; i++)
00285     {
00286         if (flag[i] == 0)
00287         {
00288             printf ("Colour %d not loaded\n", i);
00289             do_error ("Can't continue");
00290         }
00291         pal.color[i].red = (unsigned char) ((pal.color[i].red
00292                                              * (1 - gamma_correct_red)) + (64 * sin ((float) pal.color[i].red
00293                                                                                      * M_PI / 128)) * gamma_correct_red);
00294 
00295         pal.color[i].green = (unsigned char) ((pal.color[i].green
00296                                                * (1 - gamma_correct_green)) + (64 * sin ((float) pal.color[i].green
00297                                                                                          * M_PI / 128)) * gamma_correct_green);
00298 
00299         pal.color[i].blue = (unsigned char) ((pal.color[i].blue
00300                                               * (1 - gamma_correct_blue)) + (64 * sin ((float) pal.color[i].blue
00301                                                                                        * M_PI / 128)) * gamma_correct_blue);
00302     }
00303     gl_setpalette (&pal);
00304 }

Generated on Sun Dec 26 11:23:24 2004 for lincity by  doxygen 1.3.9.1