Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Screen Buffers - Lab 21 - Fall 2007 | CPS 196, Lab Reports of Computer Science

Material Type: Lab; Class: Introduction to Computer Programming; Subject: Computational Science; University: Syracuse University; Term: Fall 2007;

Typology: Lab Reports

Pre 2010

Uploaded on 08/09/2009

koofers-user-ymv
koofers-user-ymv 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPS 196 Lab 21 Fall 2007
Screen Buffers
I. Screen Buffer
A screen buffer is a 2 dimensional array of chars. We make a drawing to this array and
then display the array to the monitor. Changes made to the screen buffer will not be
visible onscreen until we display the screenbuffer.
Enter this program:
/* drawing.c */
#include<stdio.h>
#include<string.h>
/********* defined constants **************/
#define WIDTH 40 /* width of buffer: only WIDTH-2 available
for drawing*/
#define HEIGHT 10 /* height of buffer */
/********* function prototypes ***************/
void clearbuffer(char sb[ ][WIDTH ]);
void display(char sb[ ][WIDTH],int labeled);
int getstar(int *rowp,int * colp);
void vertline(int col, int startrow, int endrow,
char sb[ ][WIDTH]);
void horline(int row, int startcol, int endcol,
char sb[ ][WIDTH]);
/*****************< main >********************/
int main(void)
{
/***** variable declarations ****/
char sb[HEIGHT] [WIDTH];
int row, col;
/* set the buffer to a clear screen */
clearbuffer(sb);
display(sb,1);
/* ask for points and plot them */
while(getstar(&row,&col))
{
sb[row][col]='*';
display(sb,1);
}
/* display all points */
pf3
pf4
pf5

Partial preview of the text

Download Screen Buffers - Lab 21 - Fall 2007 | CPS 196 and more Lab Reports Computer Science in PDF only on Docsity!

CPS 196 Lab 21 Fall 2007 Screen Buffers

I. Screen Buffer A screen buffer is a 2 dimensional array of chars. We make a drawing to this array and then display the array to the monitor. Changes made to the screen buffer will not be visible onscreen until we display the screenbuffer.

Enter this program:

/* drawing.c */

#include<stdio.h> #include<string.h>

/********* defined constants **************/ #define WIDTH 40 /* width of buffer: only WIDTH-2 available for drawing/ #define HEIGHT 10 / height of buffer */

/********* function prototypes ***************/ void clearbuffer(char sb[ ][WIDTH ]); void display(char sb[ ][WIDTH],int labeled); int getstar(int *rowp,int * colp); void vertline(int col, int startrow, int endrow, char sb[ ][WIDTH]); void horline(int row, int startcol, int endcol, char sb[ ][WIDTH]);

/*****************< main >********************/ int main(void) { /***** variable declarations ****/ char sb[HEIGHT] [WIDTH]; int row, col;

/* set the buffer to a clear screen */ clearbuffer(sb); display(sb,1);

/* ask for points and plot them / while(getstar(&row,&col)) { sb[row][col]=''; display(sb,1); } /* display all points */

display(sb,0);

/* clear screen and draw some lines */ clearbuffer(sb); vertline(15,2,8,sb); vertline(16,2,7,sb); vertline(17,2,6,sb); vertline(18,5,2,sb); display(sb,1);

return(0); }

/*****************< clearbuffer >********************/ /* make each row a string, with a newline character / / Note that with the '\n' and the '\0' at the end, each row has room for WIDTH-2 other characters. */

void clearbuffer(char sb[][WIDTH]) { int row,col; for (row = 0; row<HEIGHT; row++) { for (col = 0; col<WIDTH-2; col++) sb[row][col]=' '; /* end each row with a new line */ sb[row][WIDTH-2]='\n'; sb[row][WIDTH-1]='\0'; } }

/*****************< display >********************/ /* Display the screen buffer. There are two options: labeled = 1: prints WITH rows and columns numbered labeled = 0: prints WITHOUT row and column numbers */ void display(char sb[ ][WIDTH],int labeled) { int row,col;

if (!labeled) for (row=0; row<HEIGHT; row++) printf("%s",sb[row]);

int temp;

/* test if col is in screen */ if (col<0 || col>=WIDTH-2) return;

/* make startrow less than endrow */ if (startrow > endrow) { temp=startrow; startrow=endrow; endrow=temp; }

/* clip startrow and endrow to screen */ if (startrow<0) startrow=0; if (endrow>HEIGHT-1) endrow=HEIGHT-1;

/* draw visible part of line */ for (i=startrow; i<=endrow; i++) sb[i][col]='|'; }

/*****************< horline >********************/ /* draws a horizontal line in row row from column startcol to column endcol */ void horline(int row, int startcol, int endcol, char sb[ ][WIDTH]) { int i; int temp;

/* test if row is in the screen */

/* make startcol less than endcol */

/* clip startcol and endcol to the screen */

/* draw visible part of the line */

}

A) Read the program to understand how it works. Answer these questions based on reading the code.

For getstar, what happens if you enter a row or column that is out of bounds for the screen buffer?

In getstar, how do you get the program to stop asking for points?

For vertline, what happens if the column you give is a negative number?

For vertline, what happens if the column you give is beyond the edge of the screen buffer?

For vertline, if you use 1 for column, do you get a line in the first column or the second?

What happens if either endpoint is off the screen?

B) Run the program several times. Test the program and check your answers in part A. Compare with your neighbors. If your answer in part A is different, figure out how the program actually works.

C) Write the function horline and include code in main to test it. Notice that the algorithm is given in comments. Write code immediately below each comment. Follow the model of vertline. Here is an example of a horizontal line:


Notice it is NOT a vertical line made up of dashes!

D) Write a function to draw the letter of your choice using the vertline and horline functions. The function should not print the screen buffer. Have main clear the screen, call your new function to draw the letter to the screen buffer, and then call the display function to see the new letter on the screen.

Print out your program and show it to me.