#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define COLS 80
#define ROWS 23
/**************************************************************/
/* Author: Daryle Niedermayer, I.S.P., ITCP, PMP */
/* Date: 2009-Nov-15 */
/* Version: 1.0 */
/* Title: CIT-154, Assignment 5 */
/* Purpose: To demonstrate how to read a graphical */
/* language from a text file and rasterize the */
/* graphical elements to a screen matrix. */
/* Copyright: 2009, Daryle Niedermayer, all rights reserved.*/
/**************************************************************/
/* Function prototypes */
int init(char);
int display();
int drawPt(char, int, int);
int drawRect(char, int, int, int, int);
int drawLine(char, int, int, int, int);
int fill(char, int, int);
int save(char* filename);
int loadFile(char* filename);
/* Global variables */
char screen[COLS][ROWS];
int main(int argc, char *argv[]) {
/*Author: Sean Skulmoski*/
/* Stu Num: s1234567 */
/* Date: 2009-Nov-30 */
/* Class: CIT 154-C01 */
/* Assignment: 5 */
/* File name: main.c */
/* Description: This program will take a file and convert its coords to a graphics file
Then hopefully, saves the graphics files under a new file name. */
/*Variables, Variables, and more Variables...*/
FILE *infile;
char *infilename, *d, c;
char *outfilename;
char t1[]= ",\n";
char *x1, *y1, *x2, *y2;
int row1, row2, col1, col2;
char *i;
char command;
char buffer[80];
infilename= "Final Assignment.txt";
/* Here we are uploading the file*/
infile= fopen(infilename,"r");
/*If the File isn't a valid file, or is Null, the program ends*/
if(infile == NULL){
printf("Warning, the File you have inputted is NULL\n");
system("PAUSE");
return 0;
}
/*For it the program works*/
else{
printf("%s\n", infile);
system("PAUSE");
while(fscanf(infile,"%c,%s",&command, buffer)>= 0 ){
switch(command){
case 'D':
/*Simple enough, just calls display*/
display(command);
break;
case 'R':
/*c==fustration... I will get this done!*/
/* We're currently trying to call command, break it into different tokens, use said tokens for the rectangle*/
/*cords, and print out the rectangle (which should happened in the drawRect function... if case R would work*/
d= strtok(buffer,t1);
c=d[0];
x1= strtok(NULL,t1);
y1= strtok(NULL,t1);
x2= strtok(NULL,t1);
y2= strtok(NULL,t1);
/*As said above, said tokens are being converted into integers to work with the drawRect Function*/
col1= atoi(x1);
row1= atoi(y1);
col2= atoi(x2);
row2= atoi(y2);
drawRect(c, col1, row1, col2, row2);
system("PAUSE");
break;
case 'P':
break;
case 'L':
break;
case 'I':
break;
case 'F':
d= strtok(buffer,t1);
c=d[0];
x1= strtok(NULL,t1);
y1= strtok(NULL,t1);
col1= atoi(x1);
row1= atoi(y1);
fill(c,col1,row1);
break;
case 'S':
i= strtok(buffer,t1);
saveFile(i);
break;
}
}
}
system("PAUSE");
return 0;
}
/**************************************************************/
/* init: Clears the screen by assigning all pixels the passed */
/* character. Receives a character to use in clearing the */
/* screen. */
/* Success returns a 0. */
/**************************************************************/
int init(char p) {
int x, y;
for (x = 0; x < COLS; x++) {
for (y = 0; y < ROWS; y++) {
screen[x][y] = p;
}
}
return 0;
}
/**************************************************************/
/* display: Prints the screen matrix to the console. */
/* Success returns a 0. */
/**************************************************************/
int display() {
int x, y;
/*The following line just puts a border above the screen */
for (x = 0; x < COLS; x++)
putchar('_');
/*Print out the screen matrix to standard output */
for (y = ROWS - 1; y >= 0; y--) {
for (x = 0; x < COLS; x++) {
putchar(screen[x][y]);
}
}
/*The following line just puts a border below the screen. */
for (x = 0; x < COLS; x++)
putchar('_');
return 0;
}
/**************************************************************/
/* drawPt: draws a point containing a border character using */
/* coordinates x,y. */
/* Returns -1 if the coordinates are out of bounds and 0 for */
/* success. */
/**************************************************************/
int drawPt(char b, int x, int y) {
/*Test for out of bounds */
if (x < 0 || x >= COLS) return -1;
if (y < 0 || y >= COLS) return -1;
/*Otherwise, draw the point */
screen[x][y] = (char) b;
return 0;
}
/**************************************************************/
/* drawRect: draws a rectangle with a border character using */
/* coordinates x1,y1 and x2,y2 to form opposite corners. */
/* Returns -1 if the coordinates are out of bounds and 0 for */
/* success. */
/**************************************************************/
int drawRect(char b, int x1, int y1, int x2, int y2) {
/*Test for out of bounds*/
if (x1 < 0 || x1 >= COLS) return -1;
if (x2 < 0 || x2 >= COLS) return -1;
if (y1 < 0 || y1 >= ROWS) return -1;
if (y2 < 0 || y2 >= ROWS) return -1;
int c = abs(x1 - x2); /* temporary counter variables */
int r = abs(y1 - y2); /* temporary counter variables */
int x = x1 >= x2 ? x2 : x1; /* Set x to the lowest coord. */
int y = y1 >= y2 ? y2 : y1; /* Set y to the lowest coord. */
while (c >= 0) { /* Draw the horizontal lines. */
screen[x + c][y1] = b;
screen[x + c][y2] = b;
c--;
}
while (r >= 0) { /* Draw the vertical lines. */
screen[x1][y + r] = b;
screen[x2][y + r] = b;
r--;
}
return 0;
}
/**************************************************************/
/* drawLine: draws a line with a border character using */
/* coordinates x1,y1 and x2,y2 to form opposite ends . */
/* A line can be expressed in the form y = mx + b */
/* Returns -1 if the coordinates are out of bounds and 0 for */
/* success. */
/**************************************************************/
int drawLine(char line_char, int x1, int y1, int x2, int y2) {
int horz, vert, horz1, vert1, low, high;
float m;
return 0;
}
/**************************************************************/
/* fill: fills an area with a supplied fill character, */
/* replacing all empty spaces (those pixels with a space */
/* character in a horizontal or vertical direction. */
/* This function uses recursion. */
/* Returns -1 if the coordinates are out of bounds, 1 if the */
/* coordinates are not a space and 0 for success. */
/**************************************************************/
int fill(char fill_char, int x, int y) {
/*The first half of the function should, if the requirements are correct, report that the program works.*/
/*The second if statement is the error check... if we @#$%@#ed up the code, the it returns a -1, resulting in a termination of program...*/
/**/
if(screen[COLS][ROWS]!=' '){
return 0;
}
if((x> COLS)||(y>ROWS)){
return -1;
}
return 0;
}
/**************************************************************/
/* saveFile: Prints the screen matrix to a file. */
/* Success returns a 0. */
/**************************************************************/
int saveFile(char *filename) {
int x, y;
FILE *infile;
infile= fopen(filename,"w");
/*This prints the data to the file we wish to save too.*/
for (y = ROWS - 1; y >= 0; y--) {
for (x = 0; x < COLS; x++) {
fputc(screen[x][y],infile);
}
fputc('\n',infile);
}
infile=fclose(infile);
return 0;
}
Alright, guess I'll start this off simple...
This code is to make a picture out of Coordinates from a txt file...
the problem I'm having right now though, is that my fill function isn't working...
What is fill supposed to do? It takes a type of character we wish to fill, along with coordinates, and puts it into the image instead of the ' ' characters that are between said coordinates...
However, I'm having an issue getting it to print out the characters along with the image (I got it to print out the characters, but it then decided to erase those when the image was printed.)
I know there's something I'm missing, but I don't know what... and my head is getting all bloody from how many times its hit the monitor...
This is the Txt file I'm using
the file name is Final Assignment.txt
I, ,
D
L,/,0,0,79,22
L,\,79,0,0,22
L,|,39,0,39,22
L,_,0,11,79,11
R,#,0,0,19,6
R,#,59,16,79,22
F,#,78,20
P,A,2,20,
P,S,3,20,
P,S,4,20,
P,I,5,20,
P,G,6,20,
P,N,7,20
P, ,8,20
P,5,9,20
P, ,10,20
P,#,11,20
P,1,12,20
L,*,39,16,80,20
L,*,39,16,0,25
L,*,39,16,-5,60
L,*,39,16,0,-10
R,O,60,5,85,10
P,!,65,65
D
S,assignment5-1.txt,
I, ,
R,o,5,18,11,14
F,o,7,16
R,o,4,17,12,15
R,o,7,19,9,13
D,
L,#,65,17,74,9
L,#,65,17,67,6
L,#,67,6,74,9
F,#,69,12
D
L,O,65,17,56,9
L,O,56,9,67,6
F,O,60,12
D
L,#,35,16,44,8
L,#,35,16,37,7
L,#,37,7,44,8
F,#,39,11
D
L,O,35,16,26,8
L,O,26,8,37,7
F,O,30,11
D
L,#,50,16,65,4
L,#,50,16,53,1
L,#,65,4,53,1
F,#,54,12
R,#,60,9,56,8
D,
L,O,50,16,35,4
L,O,35,4,53,1
F,O,46,8
R,O,44,6,41,8
R,O,42,9,44,9
P,O,40,7
D,
L,`,0,10,26,10
L,`,74,10,79,10
D
F,`,60,20
D,
F,.,1,1
D
P,#,66,15
P,#,36,14
P,#,51,14
D
S,assignment5-2.txt,
Any help would be greatly appreciated... also, sorry if my formatting is still "bad"... just I'm still getting the hang of it.
Frick, I fail... Meant to put this in Coding x.x
Tab Browsing got me x.x...