eck@eniac.seas.upenn.edu (Brian Eck) (10/10/90)
I am trying to write the contents of an array of integers to the screen.
The array is two dimensional; it is a representation of an electric field. I
would like to write the values at integer-valued points in the field at each
iteration of the field update. I have tried DrawString and DrawChar, but have
met with little success. I get output, but it's garbage. Here's the source:
#include <stdio.h>
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define MOVE_TO_FRONT -1L
#define REMOVE_ALL_EVENTS 0
#define GRIDX 40
#define GRIDY 40
#define POINTS 2
#define TOLERANCE 4
int grid [GRIDY] [GRIDX];
int oldgrid [GRIDY] [GRIDX];
struct coordinates
{
int x, y, val;
} field [POINTS];
int i, o, t;
int loop = 0;
int tolerance = TOLERANCE;
WindowPtr gPictureWindow;
/*** Main ***/
main()
{
ToolBoxInit();
WindowInit();
MainLoop();
while(!Button());
}
/*** ToolBoxInit ***/
ToolBoxInit()
{
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
InitWindows();
InitMenus();
TEInit();
InitDialogs(NIL_POINTER);
InitCursor();
}
/*** WindowInit ***/
WindowInit()
{
gPictureWindow = GetNewWindow(BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
ShowWindow(gPictureWindow);
SetPort(gPictureWindow);
}
/*** MainLoop ***/
MainLoop()
{
/*** FieldInit ***/
field[0].y = 19;
field[0].x = 25;
field[0].val = 100;
field[1].y = 19;
field[1].x = 15;
field[1].val = 100;
/*** GridInit ***/
for (i=0; i<GRIDY; i++)
for (o=0; o<GRIDX; o++)
grid [i] [o] = 1;
/*** Field Placement ***/
for (i=0; i<POINTS; i++)
grid[field[i].y] [field[i].x] = field[i].val;
/*** Main Loop ***/
while ((tolerance >= TOLERANCE) && (loop<100))
{
++loop;
tolerance = 0;
for (i = 0; i < GRIDY; i++) /* copy grid */
for (o = 0; o < GRIDX; o++)
oldgrid [i] [o] = grid [i] [o];
for (i = 1; i < GRIDY - 1; i++) /* recalculate */
{
for (o = 1; o < GRIDX - 1; o++)
{
if (NotInField(i,o))
{
grid[i][o] = (oldgrid [i-1] [o] + oldgrid [i+1] [o] +
oldgrid [i] [o-1] + oldgrid [i] [o+1])/4;
t = grid [i-1] [o] - grid [i] [o];
if (t>tolerance)
tolerance = t;
}
MoveTo ((o*10),(i*10));
TextSize(9);
DrawChar(grid[i][o]);
}
}
}
}
/*** Not In Field ***/
NotInField(y,x)
{
int i, o;
for (i = 0; i < POINTS; i++)
if ((field[i].x == x) && (field[i].y == y))
return(0);
return(1);
}
Please do not chastise me too severely if I am making a ridiculous error;
I kind of jumped into a project that is a bit over my head (crazy Toolbox...)
Brian
eck@eniac.seas.upenn.edurg2c+@andrew.cmu.edu (Robert Nelson Gasch) (10/10/90)
I have a think C problem. When I allocate the memory for the array
of pointers in allocatelandscape, it progressiveley slows down.
This is a real pain as MAXSIDE is 70 and after 50 rows or so, it
is *very* slow. The structure is too big for it to be declared as
an array, so I have to use pointers to allocate the memory.
If anybody knows a remedy to this, please let me know.
Thanx a million --> rob
Here's the source:
#include <stdio.h>
#define MAXSIDE 70
typedef struct {
short food;
short restorefood;
short org1, org2;
}squaretype;
squaretype *landscape[MAXSIDE][MAXSIDE]; /* global var */
void allocatelandscape ()
{
int x, y;
for (x = 0; x < MAXSIDE; x++){
for (y = 0; y < MAXSIDE; y++)
landscape[x][y] = (squaretype *) malloc (sizeof(squaretype));
printf (".");}
}
main ()
{
allocatelandscape ()
}