コード例 #1
0
ファイル: string_utils.c プロジェクト: bak724/rlwrap
char *
colour_name_to_ansi_code(const char *colour_name) {
  if (colour_name  && *colour_name && isalpha(*colour_name)) {
    char *lc_colour_name = mysavestring(lowercase(colour_name));
    char *bold_code = (isupper(*colour_name) ? "1" : "0");

#define isit(c) (strcmp(c,lc_colour_name)==0)
    char *colour_code =
      isit("black")   ? "30" :
      isit("red")     ? "31" :
      isit("green")   ? "32" :
      isit("yellow")  ? "33" :
      isit("blue")    ? "34" :
      isit("magenta") ? "35" :
      isit("purple")  ? "35" :
      isit("cyan")    ? "36" :
      isit("white")   ? "37" :
      NULL ;
      
#undef isit
    if (colour_code)
      return add3strings(bold_code,";",colour_code);
    else
      myerror("unrecognised colour name '%s'. Use e.g. 'yellow' or 'Blue'.", colour_name);
  }
  return mysavestring(colour_name);
}       
コード例 #2
0
void IOStreamIterator(const std::string& fin, const std::string& foutOdd, const std::string& foutEven)
{
    std::ifstream ifs(fin);
    if (!ifs)
    {
        std::cerr << "cannot open file " << fin << std::endl;
    }

    std::ofstream ofsOdd(foutOdd);
    if (!ofsOdd)
    {
        std::cerr << "cannot open file " << foutOdd << std::endl;
    }

    std::ofstream ofsEven(foutEven);
    if (!ofsEven)
    {
        std::cerr << "cannot open file " << foutEven << std::endl;
    }

    std::istream_iterator<int> isit(ifs);
    std::istream_iterator<int> eof;
    std::ostream_iterator<int> ositOdd(ofsOdd, " ");
    std::ostream_iterator<int> ositEven(ofsEven, "\n");

    while (isit != eof)
    {
        if (*isit % 2)
        {
            *ositOdd++ = *isit;
        }
        else
        {
            *ositEven++ = *isit;
        }

        ++isit; // µÝÔö
    }
}
コード例 #3
0
ファイル: sst.c プロジェクト: TimeDoctor/Super-Star-Trek
void debugme(void) {
	proutn("Reset levels? ");
	if (ja() != 0) {
		if (energy < inenrg) energy = inenrg;
		shield = inshld;
		torps = intorps;
		lsupres = inlsr;
	}
	proutn("Reset damage? ");
	if (ja() != 0) {
		int i;
		for (i=0; i <= ndevice; i++) if (damage[i] > 0.0) damage[i] = 0.0;
		stdamtim = 1e30;
	}
	proutn("Toggle idebug? ");
	if (ja() != 0) {
		idebug = !idebug;
		if (idebug) prout("Debug output ON");
		else prout("Debug output OFF");
	}
	proutn("Cause selective damage? ");
	if (ja() != 0) {
		int i, key;
		for (i=1; i <= ndevice; i++) {
			proutn("Kill ");
			proutn(device[i]);
			proutn("? ");
			chew();
			key = scan();
			if (key == IHALPHA &&  isit("y")) {
				damage[i] = 10.0;
				if (i == DRADIO) stdamtim = d.date;
			}
		}
	}
	proutn("Examine/change events? ");
	if (ja() != 0) {
		int i;
		for (i = 1; i < NEVENTS; i++) {
			int key;
			if (future[i] == 1e30) continue;
			switch (i) {
				case FSNOVA:  proutn("Supernova       "); break;
				case FTBEAM:  proutn("T Beam          "); break;
				case FSNAP:   proutn("Snapshot        "); break;
				case FBATTAK: proutn("Base Attack     "); break;
				case FCDBAS:  proutn("Base Destroy    "); break;
				case FSCMOVE: proutn("SC Move         "); break;
				case FSCDBAS: proutn("SC Base Destroy "); break;
			}
			cramf(future[i]-d.date, 8, 2);
			chew();
			proutn("  ?");
			key = scan();
			if (key == IHREAL) {
				future[i] = d.date + aaitem;
			}
		}
		chew();
	}
	proutn("Make universe visible? ");
	if (ja() != 0) {
		int i, j;
		for (i = 1; i < 9; i++) 
		{
			for (j = 1; j < 9; j++)
			{
				starch[i][j] = 1;
			}
		}
	}
}
コード例 #4
0
ファイル: sst.c プロジェクト: TimeDoctor/Super-Star-Trek
static void makemoves(void) {
	int i, hitme;
	char ch;
	while (TRUE) { /* command loop */
		hitme = FALSE;
		justin = 0;
		Time = 0.0;
		i = -1;
		while (TRUE)  { /* get a command */
			chew();
			skip(1);
			proutn("COMMAND> ");
			if (scan() == IHEOL) continue;
			for (i=0; i < 29; i++) // Abbreviations allowed for the first 29 commands, only.
				if (isit(commands[i]))
					break;
			if (i < 29) break;
			for (; i < NUMCOMMANDS; i++)
				if (strcmp(commands[i], citem) == 0) break;
			if (i < NUMCOMMANDS
#ifndef CLOAKING
			    && i != 26 // ignore the CLOAK command
#endif
#ifndef CAPTURE
			    && i != 27 // ignore the CAPTURE command
#endif
#ifndef SCORE
			    && i != 28 // ignore the SCORE command
#endif
#ifndef DEBUG
			    && i != 33 // ignore the DEBUG command
#endif
			   ) break;

			if (skill <= SFAIR)  {
				prout("UNRECOGNIZED COMMAND. LEGAL COMMANDS ARE:");
				listCommands(TRUE);
			}
			else prout("UNRECOGNIZED COMMAND.");
		}
		switch (i) { /* command switch */
			case 0:			// srscan
				srscan(1);
				break;
			case 1:			// lrscan
				lrscan();
				break;
			case 2:			// phasers
				phasers();
                if (ididit) {
#ifdef CLOAKING
                    if (irhere && d.date >= ALGERON && !isviolreported && iscloaked) {
                        prout("The Romulan ship discovers you are breaking the Treaty of Algeron!");
                        ncviol++;
                        isviolreported = TRUE;
                    }
#endif
                    hitme = TRUE;
                }
				break;
			case 3:			// photons
				photon();
                if (ididit) {
#ifdef CLOAKING
                    if (irhere && d.date >= ALGERON && !isviolreported && iscloaked) {
                        prout("The Romulan ship discovers you are breaking the Treaty of Algeron!");
                        ncviol++;
                        isviolreported = TRUE;
                    }
#endif
                    hitme = TRUE;
                }
				break;
			case 4:			// move
				warp(1);
				break;
			case 5:			// shields
				sheild(1);
				if (ididit) {
					attack(2);
					shldchg = 0;
				}
				break;
			case 6:			// dock
				dock();
				break;
			case 7:			// damages
				dreprt();
				break;
			case 8:			// chart
				chart(0);
				break;
			case 9:			// impulse
				impuls();
				break;
			case 10:		// rest
				waiting();
				if (ididit) hitme = TRUE;
				break;
			case 11:		// warp
				setwrp();
				break;
			case 12:		// status
				srscan(3);
				break;
			case 13:			// sensors
				sensor();
				break;
			case 14:			// orbit
				orbit();
				if (ididit) hitme = TRUE;
				break;
			case 15:			// transport "beam"
				beam();
				break;
			case 16:			// mine
				mine();
				if (ididit) hitme = TRUE;
				break;
			case 17:			// crystals
				usecrystals();
				break;
			case 18:			// shuttle
				shuttle();
				if (ididit) hitme = TRUE;
				break;
			case 19:			// Planet list
				preport();
				break;
			case 20:			// Status information
				srscan(2);
				break;
			case 21:			// Game Report 
				report(0);
				break;
			case 22:			// use COMPUTER!
				eta();
				break;
			case 23:
				listCommands(TRUE);
				break;
			case 24:		// Emergency exit
				clearscreen();	// Hide screen
				freeze(TRUE);	// forced save
				exit(1);		// And quick exit
				break;
			case 25:
				probe();		// Launch probe
				break;
#ifdef CLOAKING
			case 26:
				cloak();        // turn on/off cloaking
				if (iscloaking) {
					attack(2); // We will be seen while we cloak
					iscloaking = FALSE;
					iscloaked = TRUE;
				}
				break;
#endif
#ifdef CAPTURE
			case 27:
				capture();      // Attempt to get Klingon ship to surrender
				if (ididit) hitme = TRUE;
				break;
#endif
#ifdef SCORE
			case 28:
				score(1);    // get the score
				break;
#endif
			case 29:			// Abandon Ship
				abandn();
				break;
			case 30:			// Self Destruct
				dstrct();
				break;
			case 31:			// Save Game
				freeze(FALSE);
				if (skill > SGOOD)
					prout("WARNING--Frozen games produce no plaques!");
				break;
			case 32:			// Try a desparation measure
				deathray();
				if (ididit) hitme = TRUE;
				break;
#ifdef DEBUG
			case 33:			// What do we want for debug???
				debugme();
				break;
#endif
			case 34:		// Call for help
				help();
				break;
			case 35:
				alldone = 1;	// quit the game
#ifdef DEBUG
				if (idebug) score(0);
#endif
				break;
			case 36:
				helpme();	// get help
				break;
		}
		for (;;) {
			if (alldone) break;		// Game has ended
#ifdef DEBUG
			if (idebug) prout("2500");
#endif
			if (Time != 0.0) {
				events();
				if (alldone) break;		// Events did us in
			}
			if (d.galaxy[quadx][quady] == 1000) { // Galaxy went Nova!
				atover(0);
				continue;
			}
			if (nenhere == 0) movetho();
			if (hitme && justin==0) {
				attack(2);
				if (alldone) break;
				if (d.galaxy[quadx][quady] == 1000) {	// went NOVA! 
					atover(0);
					hitme = TRUE;
					continue;
				}
			}
			break;
		}
		if (alldone) break;
	}
}
コード例 #5
0
ファイル: ifinfo.c プロジェクト: 2asoft/freebsd
int
main(int argc, char **argv)
{
	int i, maxifno, retval;
	struct ifmibdata ifmd;
	int name[6];
	size_t len;
	int c;
	int dolink = 0;
	void *linkmib;
	size_t linkmiblen;
	printfcn pf;
	char *dname;

	while ((c = getopt(argc, argv, "l")) != -1) {
		switch(c) {
		case 'l':
			dolink = 1;
			break;
		default:
			usage(argv[0]);
		}
	}
	
	retval = 1;

	name[0] = CTL_NET;
	name[1] = PF_LINK;
	name[2] = NETLINK_GENERIC;
	name[3] = IFMIB_SYSTEM;
	name[4] = IFMIB_IFCOUNT;

	len = sizeof maxifno;
	if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0)
		err(EX_OSERR, "sysctl(net.link.generic.system.ifcount)");

	for (i = 1; i <= maxifno; i++) {
		len = sizeof ifmd;
		name[3] = IFMIB_IFDATA;
		name[4] = i;
		name[5] = IFDATA_GENERAL;
		if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
			if (errno == ENOENT)
				continue;

			err(EX_OSERR, "sysctl(net.link.ifdata.%d.general)",
			    i);
		}

		if (!isit(argc - optind, argv + optind, ifmd.ifmd_name))
			continue;

		dname = NULL;
		len = 0;
		name[5] = IFDATA_DRIVERNAME;
		if (sysctl(name, 6, NULL, &len, 0, 0) < 0) {
			warn("sysctl(net.link.ifdata.%d.drivername)", i);
		} else {
			if ((dname = malloc(len)) == NULL)
				err(EX_OSERR, NULL);
			if (sysctl(name, 6, dname, &len, 0, 0) < 0) {
				warn("sysctl(net.link.ifdata.%d.drivername)",
				    i);
				free(dname);
				dname = NULL;
			}
		}
		printit(&ifmd, dname);
		free(dname);
		if (dolink && (pf = findlink(ifmd.ifmd_data.ifi_type))) {
			name[5] = IFDATA_LINKSPECIFIC;
			if (sysctl(name, 6, 0, &linkmiblen, 0, 0) < 0)
				err(EX_OSERR, 
				    "sysctl(net.link.ifdata.%d.linkspec) size",
				    i);
			linkmib = malloc(linkmiblen);
			if (!linkmib)
				err(EX_OSERR, "malloc(%lu)", 
				    (u_long)linkmiblen);
			if (sysctl(name, 6, linkmib, &linkmiblen, 0, 0) < 0)
				err(EX_OSERR, 
				    "sysctl(net.link.ifdata.%d.linkspec)",
				    i);
			pf(linkmib, linkmiblen);
			free(linkmib);
		}
		retval = 0;
	}

	return retval;
}
コード例 #6
0
ファイル: sst.c プロジェクト: aichao/sst
static void makemoves(void) {
	int i, hitme;
	char ch;
	while (TRUE) { /* command loop */
		hitme = FALSE;
		justin = 0;
		Time = 0.0;
		i = -1;
		while (TRUE)  { /* get a command */
			chew();
			skip(1);
			proutn("COMMAND> ");
      // Use of scan() here (after chew) will get a new line of input
      // and will return IHEOL iff new line of input contains nothing
      // or a numeric input is detected but conversion fails.
			if (scan() == IHEOL) continue;
			for (i=0; i < 26; i++)
				if (isit(commands[i]))
					break;
			if (i < 26) break;
			for (; i < NUMCOMMANDS; i++)
				if (strcmp(commands[i], citem) == 0) break;
			if (i < NUMCOMMANDS) break;
      // we get here iff the first parsed input from the line does not 
      // match one of the commands. In this case, the rest of the line
      // is discarded, the below message is printed, and we go back to 
      // get a new command.
			if (skill <= 2)  {
				prout("UNRECOGNIZED COMMAND. LEGAL COMMANDS ARE:");
				listCommands(TRUE);
			}
			else prout("UNRECOGNIZED COMMAND.");
		} // end get command loop
    // we get here iff the first parsed input from the line matches one
    // of the commands (i.e., command i). We use i to dispatch the 
    // handling of the command. The line may still contain additional
    // inputs (i.e., parameters of the command) that is to be parsed by
    // the dispatched command handler. If the line does not contain
    // all the necessary parameters, the dispatched command handler is 
    // responsible to get additional input(s) interactively using scan().
    // The dispatched command handler is also responsible to handle any 
    // input errors.
		switch (i) { /* command switch */
			case 0:			// srscan
				srscan(1);
				break;
			case 1:			// lrscan
				lrscan();
				break;
			case 2:			// phasers
				phasers();
				if (ididit) hitme = TRUE;
				break;
			case 3:			// photons
				photon();
				if (ididit) hitme = TRUE;
				break;
			case 4:			// move
				warp(1);
				break;
			case 5:			// shields
				sheild(1);
				if (ididit) {
					attack(2);
					shldchg = 0;
				}
				break;
			case 6:			// dock
				dock();
				break;
			case 7:			// damages
				dreprt();
				break;
			case 8:			// chart
				chart(0);
				break;
			case 9:			// impulse
				impuls();
				break;
			case 10:		// rest
				waiting();
				if (ididit) hitme = TRUE;
				break;
			case 11:		// warp
				setwrp();
				break;
			case 12:		// status
				srscan(3);
				break;
			case 13:			// sensors
				sensor();
				break;
			case 14:			// orbit
				orbit();
				if (ididit) hitme = TRUE;
				break;
			case 15:			// transport "beam"
				beam();
				break;
			case 16:			// mine
				mine();
				if (ididit) hitme = TRUE;
				break;
			case 17:			// crystals
				usecrystals();
				break;
			case 18:			// shuttle
				shuttle();
				if (ididit) hitme = TRUE;
				break;
			case 19:			// Planet list
				preport();
				break;
			case 20:			// Status information
				srscan(2);
				break;
			case 21:			// Game Report 
				report(0);
				break;
			case 22:			// use COMPUTER!
				eta();
				break;
			case 23:
				listCommands(TRUE);
				break;
			case 24:		// Emergency exit
				clearscreen();	// Hide screen
				freeze(TRUE);	// forced save
				exit(1);		// And quick exit
				break;
			case 25:
				probe();		// Launch probe
				break;
			case 26:			// Abandon Ship
				abandn();
				break;
			case 27:			// Self Destruct
				dstrct();
				break;
			case 28:			// Save Game
				freeze(FALSE);
				if (skill > 3)
					prout("WARNING--Frozen games produce no plaques!");
				break;
			case 29:			// Try a desparation measure
				deathray();
				if (ididit) hitme = TRUE;
				break;
			case 30:			// What do we want for debug???
#ifdef DEBUG
				debugme();
#endif
				break;
			case 31:		// Call for help
				help();
				break;
			case 32:
				alldone = 1;	// quit the game
#ifdef DEBUG
				if (idebug) score();
#endif
				break;
			case 33:
				helpme();	// get help
				break;
		} // end command switch
		for (;;) {
			if (alldone) break;		// Game has ended
#ifdef DEBUG
			if (idebug) prout("2500");
#endif
			if (Time != 0.0) {
				events();
				if (alldone) break;		// Events did us in
			}
			if (d.galaxy[quadx][quady] == 1000) { // Galaxy went Nova!
				atover(0);
				continue;
			}
			if (nenhere == 0) movetho();
			if (hitme && justin==0) {
				attack(2);
				if (alldone) break;
				if (d.galaxy[quadx][quady] == 1000) {	// went NOVA! 
					atover(0);
					hitme = TRUE;
					continue;
				}
			}
			break;
		} // end event loop
		if (alldone) break;
	} // end command loop
}
コード例 #7
0
ファイル: reports.c プロジェクト: aichao/sst
void srscan(int l) {
	static char requests[][3] =
		{"","da","co","po","ls","wa","en","to","sh","kl","ti"};
	char *cp;
	int leftside=TRUE, rightside=TRUE, i, j, jj, k=0, nn=FALSE;
	int goodScan=TRUE;
	switch (l) {
		case 1: // SRSCAN
			if (damage[DSRSENS] != 0) {
				/* Allow base's sensors if docked */
				if (condit != IHDOCKED) {
					prout("SHORT-RANGE SENSORS DAMAGED");
					goodScan=FALSE;
				}
				else
					prout("[Using starbase's sensors]");
			}
			if (goodScan)
				starch[quadx][quady] = damage[DRADIO]>0.0 ?
									   d.galaxy[quadx][quady]+1000:1;
			scan();
			if (isit("chart")) nn = TRUE;
			if (isit("no")) rightside = FALSE;
			chew();
			prout("\n    1 2 3 4 5 6 7 8 9 10");
			break;
		case 2: // REQUEST
			while (scan() == IHEOL)
				printf("Information desired? ");
			chew();
			for (k = 1; k <= 10; k++)
				if (strncmp(citem,requests[k],min(2,strlen(citem)))==0)
					break;
			if (k > 10) {
				prout("UNRECOGNIZED REQUEST. Legal requests are:\n"
					 "  date, condition, position, lsupport, warpfactor,\n"
					 "  energy, torpedoes, shields, klingons, time.");
				return;
			}
			// no "break"
		case 3: // STATUS
			chew();
			leftside = FALSE;
			skip(1);
	}
	for (i = 1; i <= 10; i++) {
		int jj = (k!=0 ? k : i);
		if (leftside) {
			printf("%2d  ", i);
			for (j = 1; j <= 10; j++) {
				if (goodScan || (abs(i-sectx)<= 1 && abs(j-secty) <= 1))
					printf("%c ",quad[i][j]);
				else
					printf("- ");
			}
		}
		if (rightside) {
			switch (jj) {
				case 1:
					printf(" Stardate      %.1f", d.date);
					break;
				case 2:
					if (condit != IHDOCKED) newcnd();
					switch (condit) {
						case IHRED: cp = "RED"; break;
						case IHGREEN: cp = "GREEN"; break;
						case IHYELLOW: cp = "YELLOW"; break;
						case IHDOCKED: cp = "DOCKED"; break;
					}
					printf(" Condition     %s", cp);
					break;
				case 3:
					printf(" Position     ");
					cramlc(0, quadx, quady);
					putchar(',');
					cramlc(0, sectx, secty);
					break;
				case 4:
					printf(" Life Support  ");
					if (damage[DLIFSUP] != 0.0) {
						if (condit == IHDOCKED)
							printf("DAMAGED, supported by starbase");
						else
							printf("DAMAGED, reserves=%4.2f", lsupres);
					}
					else
						printf("ACTIVE");
					break;
				case 5:
					printf(" Warp Factor   %.1f", warpfac);
					break;
				case 6:
					printf(" Energy        %.2f", energy);
					break;
				case 7:
					printf(" Torpedoes     %d", torps);
					break;
				case 8:
					printf(" Shields       ");
					if (damage[DSHIELD] != 0)
						printf("DAMAGED,");
					else if (shldup)
						printf("UP,");
					else
						printf("DOWN,");
					printf(" %d%% %.1f units",
						   (int)((100.0*shield)/inshld + 0.5), shield);
					break;
				case 9:
					printf(" Klingons Left %d", d.remkl);
					break;
				case 10:
					printf(" Time Left     %.2f", d.remtime);
					break;
			}
					
		}
		skip(1);
		if (k!=0) return;
	}
	if (nn) chart(1);
}
コード例 #8
0
ファイル: vrpn_Sound_A3D.cpp プロジェクト: ASPePeX/vrpn
void vrpn_Sound_Server_A3D::loadModelLocal(const char * filename) {
  FILE  * model_file;
  float   x,y,z;
  int     num = 0;
  char    tempbuf[1024], tempbuf2[1024];
  
  int       valid_num = 0;
  int       mynum = 0;
  int       curnum = 0;
  char      material_name[MAX_MATERIAL_NAME_LENGTH];
  
  float     trans1, trans2, refl1, refl2;
  float		  subopeningval;
  // open the file
		
  try
  {
    sprintf(tempbuf, "Working from model file: %s", filename);
    printf("%s\n",tempbuf);
    send_message((const char *) tempbuf,vrpn_TEXT_NORMAL,0);
   
    if ( (model_file = fopen(filename, "r")) == NULL) {
      throw "Cannot open model file";
    }
  
  char	line[512];	// Line read from the input file
  char  * pch;
	char    scrap[512];
	
	// Read lines from the file until we run out
	while ( fgets(line, sizeof(line), model_file) != NULL ) 
		{
			
			// Make sure the line wasn't too long
    if (strlen(line) >= sizeof(line)-1) {
        sprintf(tempbuf, "Line too long in config file: %s",line);
				throw tempbuf;
			}
			
			if ((strlen(line)<3)||(line[0]=='#')) {
				continue;
			}
			
			// copy for strtok work
			strncpy(scrap, line, sizeof(line) - 1);
			
#define isit(s) !strcmp(pch=strtok(scrap," \t"),s)
#define next() pch += strlen(pch) + 1
			
			// for now just have 10 materials
			
			if(isit("MATERIAL"))
			{	
			// material name should be first followed by trans and reflect values
				next();
				
				if (sscanf(pch,"%511s\t%f\t%f\t%f\t%f\n",material_name, &trans1, &trans2, &refl1, &refl2) != 5) {
					sprintf(tempbuf, "PROBLEM in material list with material: %s", material_name);
          throw tempbuf;
				}
				else {
					// make sure name is not already in the list
					for (int i(0); i < MAX_NUMBER_MATERIALS; i++) {
						if (strcmp(material_name, mat_names[i]) == 0)
						{
							sprintf(tempbuf, "Material %s already exists in the list.. replacing with new definition", material_name);
              send_message((const char *) tempbuf,vrpn_TEXT_WARNING,0);
						}
					}

					// copy the name to the name array
					strcpy(mat_names[curnum], material_name);
					a3dgeom->NewMaterial(&materials[curnum]);
					materials[curnum]->SetTransmittance(trans1, trans2);
					materials[curnum]->SetReflectance(refl1, refl2);
					curnum++;
          sprintf(tempbuf, "Material %d is %s", curnum-1, mat_names[curnum-1]);
          send_message((const char *) tempbuf,vrpn_TEXT_NORMAL,0);
					valid_num = curnum;
				}
			}
	
			else if(isit("A3D_QUAD"))
			{

				next();

				if (sscanf(pch,"%d\t%512s",&num, material_name) != 2) {
 							sprintf(tempbuf, "PROBLEM in A3D_QUAD %d\n", num);
              throw tempbuf;
				}
				else {
					// should look up the materials
					mynum = -1;
					
					for (int i(0); i < valid_num; i++) {
					
						if (strcmp(material_name, mat_names[i]) == 0) {
								mynum = i;
						}
					}
					
					if (mynum >= 0)
					  a3dgeom->BindMaterial(materials[mynum]);
				}

				a3dgeom->PushMatrix();
				a3dgeom->Begin(A3D_QUADS);
				a3dgeom->Tag(num);

				// read in 4 vertices
				for (int i(0); i<4; i++) {
					if (fgets(line, sizeof(line), model_file) != NULL) {
				  if (strlen(line) >= sizeof(line)-1) {
 							sprintf(tempbuf, "Quad (#%d) line too long in model file: %s",num, line);
              throw tempbuf;
				  }
			      if ((strlen(line)<3)||(line[0]=='#')) 
			   	  continue; // skip this line

				  // read in the vertices
				  if (sscanf(line,"%f\t%f\t%f",&x, &y, &z) != 3) 
					  return;
				  else {
					  a3dgeom->Vertex3f(x,y,z);
				  }
				 
					}
					else return;
				}
				a3dgeom->End();
				a3dgeom->PopMatrix();
				
			}

			else if(isit("A3D_SUB_QUAD"))
			{

				next();

				if (sscanf(pch,"%d\t%511s\t%f",&num, material_name,&subopeningval) != 3) {
					throw "PROBLEM in A3D_QUAD";
				}
				else {
					// should look up the materials
					mynum = -1;
					for (int i(0); i < valid_num; i++) {
						if (strcmp(material_name, mat_names[i]) == 0)
							mynum = i;
					}

					if (mynum >= 0)
					  a3dgeom->BindMaterial(materials[mynum]); 
				}

				a3dgeom->PushMatrix();
				a3dgeom->Begin(A3D_SUB_QUADS);
				a3dgeom->Tag(num);
				a3dgeom->SetOpeningFactorf(subopeningval); // make sure to call this before specifying vertices

				// read in 4 vertices
				for (int i(0); i<4; i++) {
				  if (fgets(line, sizeof(line), model_file) != NULL) {
				    if (strlen(line) >= sizeof(line)-1) {
 							sprintf(tempbuf, "subQuad (#%d) line too long in model file: %s",num, line);
              throw tempbuf;
					}
			        if ((strlen(line)<3)||(line[0]=='#')) 
			   	    continue; // skip this line

				    // read in the vertices
				    if (sscanf(line,"%f\t%f\t%f",&x, &y, &z) != 3) 
			 		  return;
				    else {
					    a3dgeom->Vertex3f(x,y,z);
					}
				  }
				 else return;
				}
				a3dgeom->End();
				a3dgeom->PopMatrix();
				
			}

			else if(isit("A3D_TRIANGLE"))
			{

				next();

				if (sscanf(pch,"%d\t%511s\t%f",&num, material_name) != 2) {
					throw "PROBLEM in A3D_TRI";
				}
				else {
					// should look up the materials
					mynum = -1;
					for (int i(0); i < valid_num; i++) {
						if (strcmp(material_name, mat_names[i]) == 0)
							mynum = i;
					}
					if (mynum >= 0)
					  a3dgeom->BindMaterial(materials[mynum]); 				
				}

				a3dgeom->PushMatrix();
				a3dgeom->Begin(A3D_TRIANGLES);
				a3dgeom->Tag(num);

				// read in 3 vertices
				for (int i(0); i<3; i++) {
					if (fgets(line, sizeof(line), model_file) != NULL) {
				  if (strlen(line) >= sizeof(line)-1) {
             	sprintf(tempbuf, "Tri (#%d) line too long in model file: %s",num, line);
              throw tempbuf;
				  }
			      if ((strlen(line)<3)||(line[0]=='#')) 
			   	  continue; // skip this line

				  // read in the vertices
				  if (sscanf(line,"%f\t%f\t%f",&x, &y, &z) != 3) 
					  return;
				  else {
					  a3dgeom->Vertex3f(x,y,z);
				  }
				  
					}
					else return;
				}
				a3dgeom->End();
				a3dgeom->PopMatrix();
			}

			else if(isit("A3D_SUB_TRIANGLE"))
			{
				next();
				if (sscanf(pch,"%d\t%511s\t%f",&num, material_name,&subopeningval) != 3) {
					throw "PROBLEM in A3D_SUB_TRI";
				}
				else {
					// should look up the materials
					mynum = -1;
					for (int i(0); i < valid_num; i++) {
						if (strcmp(material_name, mat_names[i]) == 0)
							mynum = i;
					}
					if (mynum > 0)
					  a3dgeom->BindMaterial(materials[mynum]); 					
				}

				a3dgeom->PushMatrix();
				a3dgeom->Begin(A3D_SUB_TRIANGLES);
				a3dgeom->Tag(num);
				a3dgeom->SetOpeningFactorf(subopeningval); // make sure to call this before specifying vertices

				// read in 3 vertices
				for (int i(0); i<3; i++) {
					if (fgets(line, sizeof(line), model_file) != NULL) {
				  if (strlen(line) >= sizeof(line)-1) {
 							sprintf(tempbuf, "subTri (#%d) line too long in model file: %s",num, line);
              throw tempbuf;
				  }
			      if ((strlen(line)<3)||(line[0]=='#')) 
			   	  continue; // skip this line

				  // read in the vertices
				  if (sscanf(line,"%f\t%f\t%f",&x, &y, &z) != 3) 
					  return;
				  else {
					  a3dgeom->Vertex3f(x,y,z);
				  }
				  					}
					else return;
				}
				a3dgeom->End();
				a3dgeom->PopMatrix();
			}
	}

	fclose(model_file);
}
catch (char* szError) {
  sprintf(tempbuf2,"Error: Loading model file (%s)", szError);
  printf("%s\n", tempbuf2);
  send_message((const char *) tempbuf2,vrpn_TEXT_ERROR,0);
}
return;
}
コード例 #9
0
ファイル: vrpn_Sound_A3D.cpp プロジェクト: ASPePeX/vrpn
void main(int argc, char **argv) {

HWND hWin;
vrpn_Sound_Server_A3D * soundServer = NULL;
vrpn_Tracker_Remote   * tracker_connection;
char                    tracker_device[512];
char                    tracker_name[512];
vrpn_Connection       * connection;
vrpn_Connection       * trackerCon;
int                     got_report;

int USE_TRACKER;

	char	* config_file_name = "vrpn.cfg";
	FILE	* config_file;
	char 	* client_name   = NULL;
	int	    client_port   = 4150;
	int	    bail_on_error = 1;
	int	    verbose       = 1;
	int	    auto_quit     = 0;
	int	    realparams    = 0;
	int 	  loop          = 0;
	int	    port          = vrpn_DEFAULT_LISTEN_PORT_NO;

	connection = new vrpn_Synchronized_Connection (port);
	
	// Open the configuration file
	if (verbose) printf("Reading from config file %s\n", config_file_name);
	
	if ( (config_file = fopen(config_file_name, "r")) == NULL) 
	{
		perror("Cannot open config file");
		printf("  (filename %s)\n", config_file_name);
		return;
	}
		
	// Read the configuration file, creating a device for each entry.
	// Each entry is on one line, which starts with the name of the
	//   class of the object that is to be created.
	// If we fail to open a certain device, print a message and decide
	//  whether we should bail.
	{	
		char	line[512];	// Line read from the input file
		char *pch;
		char    scrap[512], s2[512];
	
		// Read lines from the file until we run out
		while ( fgets(line, sizeof(line), config_file) != NULL ) 
		{
			
			// Make sure the line wasn't too long
			if (strlen(line) >= sizeof(line)-1) 
			{
				printf("Line too long in config file: %s\n",line);
				if (bail_on_error) { return; }
				else { continue; }	// Skip this line
			}
			
			if ((strlen(line)<3)||(line[0]=='#')) 
			{
				// comment or empty line -- ignore
				continue;
			}
			
			// copy for strtok work
			strncpy(scrap, line, sizeof(line) - 1);
			// Figure out the device from the name and handle appropriately
			
			// WARNING: SUBSTRINGS WILL MATCH THE EARLIER STRING, SO 
			// ADD AN EMPTY SPACE TO THE END OF STATIC STRINGS!!!!
			
			//	  #define isit(s) !strncmp(line,s,strlen(s))
#define isit(s) !strcmp(pch=strtok(scrap," \t"),s)
#define next() pch += strlen(pch) + 1
	
			#ifdef _WIN32

			if(isit("vrpn_Sound_Server"))
			{
				printf("%s\n",pch); 
				next();
				printf("%s\n",pch);
				if (sscanf(pch,"%511s\t%d\t%511s\t%511s",s2,&USE_TRACKER,tracker_name, tracker_device) != 4) 
				{
					printf("Bad vrpn_Server_Sound line: %s\n",line);
					if (bail_on_error) 
					{ 
						return; 
					}
					else 
					{ 
						continue; 
					}	// Skip this line
				}

				hWin = GetConsoleHwnd();

				printf("Begin initializing A3D Sound Server\n");	
				soundServer = NULL;
				soundServer = new vrpn_Sound_Server_A3D(s2, connection,hWin);
				if (soundServer == NULL) 
					printf("Can't create sound server\n");
        printf("End A3D Sound Server initialization\n");	
				
			}
#endif
		}
	}

	fclose(config_file);

	// Open remote tracker if we are to use one

	if (USE_TRACKER) {
		
		char newname[1024];
		sprintf(newname,"%s@%s",(const char*)tracker_device, (const char*)tracker_name);
		printf("Using tracker: %s\n",newname);
		trackerCon = vrpn_get_connection_by_name(tracker_name);
		tracker_connection = new vrpn_Tracker_Remote((const char *) newname);
		// SET UP TRACKER HANDLER
		if (trackerCon->doing_okay()) {
			printf( "TC OK.\n");
		} else {
			printf( "TC Not OK.\n");
		}
	}
	else printf("Not using tracker\n");

	loop = 0;
	
	if (client_name) 
	{
		printf( "vrpn_serv: connecting to client: %s:%d\n",
			client_name, client_port);
		if (connection->connect_to_client(client_name, client_port))
		{
			printf( "server: could not connect to client %s:%d\n", client_name, client_port);
		}
	}
	
		
// ********************************************************************
// **                                                                **
// **                MAIN LOOP                                       **
// **                                                                **
// ********************************************************************
float fPrevTime = 0.0f;
float fFrameTime;
float fTime;
int counter = 0;
int stopNow = 0;
int numconnections = 0;
char buf[1024];

	printf("Begin main loop\n");

	while (!stopNow && 	!_kbhit()) {

    soundServer->GetLastError(buf);

    if (!strncmp(buf,"ERROR",5)) {
		  printf("%s", buf);
    }
      counter++;

  	  // record time since last frame 
      if (counter==NUM_SPIN) {
	    fTime = (float)timeGetTime();
	    counter = 0;

	    fFrameTime = (fTime - fPrevTime) * 0.001f;
	  
	    printf("Running at %4.2f Hz\n", (float) NUM_SPIN/fFrameTime);

        fPrevTime = fTime;
      }

	  soundServer->mainloop();
						
		// ensure we get a new report!
		if (USE_TRACKER) {
		  tracker_connection->mainloop();
		  got_report = 0;
		  if (trackerCon->doing_okay())
		    while (!got_report) 
			  tracker_connection->mainloop(); 
		}

		// Send and receive all messages
		connection->mainloop();
		if (numconnections==0 && connection->connected())
           numconnections++;
	
		if (((numconnections!=0) & (!connection->connected())) | !connection->doing_okay())  {
			soundServer->shutDown();
		  numconnections=0;
		}
	}

	printf("about to shutdown\n");
//	delete connection;
   delete soundServer;
}