All pastes #20885 Raw Edit

Anonymous

public c v1 · immutable
#20885 ·published 2005-08-23 18:53 UTC
rendered paste body
//Let's get our libaries...#include <stdio.h>#include <stdlib.h>#include <ncurses.h>#include <unistd.h>#define MAX_LINE 150#define OFF_END 148int main(){	FILE * file;	initscr();	file = fopen("map01.map","r"); //Get the file.	if (file!=NULL) {		char *s,*p; 		int x,y,i,j,x0,y0,x1,y1,winx,winy,realx,realy; //Declare useful variables...		s=p=(char *)malloc(MAX_LINE); //Set the limit for amount of characters ( when maps become large, the constant will probably be increased ).		while( ((*p=getc(file))!='\n') && (p<(s+OFF_END))) {			p++;		} //get first line		*p='\0';		if ((s[0]!='x') || (s[1]!='=')) { //check if it complies			endwin();			exit(1);		}		else {						x = atoi(&s[2]);		} //get the data from the line.		p=s;		while( ((*p=getc(file))!='\n') && (p<(s+OFF_END))) { //get the second line			p++;		}		if ((s[0]!='y') || (s[1]!='=')) { //check if it complies			endwin();			exit(1);		}		else {						y = atoi(&s[2]);		} //and get the data from this data aswell.		p=s;		while( ((*p=getc(file))!='\n') && (p<(s+OFF_END))) { //ignore the following line as we assume it's [map] ( is to be fixed )			p++;		}		getyx(stdscr,realy,realx); //get the window size ( I guess )		winx=30; //Set a window size for x and y, which is the window you see, it should be relative to the realx,realy.		winy=10;		x0=5; //Make offset ( e.g. when you will be able to move the map around, these numbers are going to decrease/increase ).		y0=0;		x1=winx+x0; //Get the other end		y1=winy+y0;		addstr("+"); //start the box		for(i=x0;i<=x1;i++) { //The upper side of it			addstr("-");		}		addstr("+\n"); //and the end of the upper part of the box.		for(i=y0*3;i<=y1*3;i+=3) { //start the drawning map loop.			p=s;			while(((*p=getc(file))!='\n') && (p<(s+OFF_END))) {				p++;			} //get the line we need.			*p='\0';			if (s[0]=='#') { //See if it's a comment, then the line should be ignored.				i--;			}			else {				addstr("|"); //Start the left side of the border.				for (j=x0*3;j<=x1*3;j+=3) {					switch (s[j]) { //Check what type of tile we are talking about						case 'G': //Ground?							addstr(":");							break;						case 'R': //Road?							if (s[j+1]=='V') addstr("¦"); //Vertical?							else if (s[j+1]=='H') addstr("="); //Horizontal?							else if (s[j+1]=='C') addstr("X"); //A cross?							break;						case 'W': //Water?							if (s[j+1]=='-') addstr("~"); //Just water?							else if (s[j+1]=='C') addstr("!"); //Or a coast line?							break;					}				}				addstr("|\n"); //Add the other border, and line feed ( should this be fixed? )			}		}		addstr("+"); //Start the bottom of the box.		for(i=x0;i<=x1;i++) {			addstr("-"); //The long side of the bottom.		}		addstr("+\n"); //The last character.		refresh(); //Send it to the terminal		sleep(15); //Let's see it for 15 seconds.		endwin(); //Restore the window. 		fclose(file); //Close the file ( probably should be before ).		return 0; //Clean it up...	}}