1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <stdio.h>
#include <stdlib.h>
#include "fssimplewindow.h"
int ParseString(int &nWord,int wordTop[],int wordLength[],char str[],int maxNumWords)
{
int i,state;
state=0;
nWord=0;
for(i=0; str[i]!=0; i++)
{
switch(state)
{
case 0:
if(str[i]!=' ' &&
str[i]!='\t' &&
str[i]!=',')
{
state=1;
wordTop[nWord]=i;
wordLength[nWord]=1;
nWord++;
}
break;
case 1:
if(str[i]!=' ' &&
str[i]!='\t' &&
str[i]!=',')
{
wordLength[nWord-1]++;
}
else
{
state=0;
if(nWord==maxNumWords)
{
goto LOOPOUT;
}
}
break;
}
}
LOOPOUT:
return nWord;
}
class Vec
{
public:
int x1,y1,x2,y2;
int r,g,b;
};
class Polyg
{
protected:
int nVertex;
Vec *vertex;
public:
Polyg();
~Polyg();
void ReadFromFile(char fn[]);
void Clear();
void DrawWireframe();
void Move();
};
void Polyg::Clear()
{
if(vertex!=NULL)
{
delete [] vertex; //de-allocation
vertex=NULL;
nVertex=0;
}
}
Polyg::Polyg()
{
nVertex=0;
vertex=NULL;
}
Polyg::~Polyg()
{
Clear(); //de-allocation
}
void Polyg::ReadFromFile(char fn[])
{
if(vertex!=NULL)
{
Clear();
}
FILE *fp;
char str[256];
fp=fopen(fn,"r");
if(fp!=NULL && fgets(str,255,fp)!=NULL)
{
int nRead;
nVertex=atoi(str);
vertex=new Vec[nVertex]; //allocation
if(vertex!=NULL)
{
nRead=0;
while(fgets(str,255,fp)!=NULL && nRead<nVertex)
{
int nWord,wordTop[16],wordLength[16];
ParseString(nWord,wordTop,wordLength,str,16);
if(nWord>=2)
{
vertex[nRead].x1=atoi(str+wordTop[0]);
vertex[nRead].y1=atoi(str+wordTop[1]);
vertex[nRead].x2=atoi(str+wordTop[2]);
vertex[nRead].y2=atoi(str+wordTop[3]);
vertex[nRead].r=atoi(str+wordTop[4]);
vertex[nRead].g=atoi(str+wordTop[5]);
vertex[nRead].b=atoi(str+wordTop[6]);
nRead++;
}
}
fclose(fp);
}
}
if(fp!=NULL)
{
fclose(fp);
}
}
void Polyg::DrawWireframe()
{
for(int i=0; i<nVertex; i++)
{
glColor3ub(vertex[i].r,vertex[i].g,vertex[i].b);
glBegin(GL_LINES);
glVertex2i(vertex[i].x1,600-vertex[i].y1);
glVertex2i(vertex[i].x2,600-vertex[i].y2);
glEnd();
}
}
void Polyg::Move()
{
int lb,mb,rb,mx,my;
int mx0,my0;
int evt=FsGetMouseEvent(lb,mb,rb,mx,my);
if (evt==FSMOUSEEVENT_LBUTTONDOWN)
{
mx0=mx; //seems the assignment fails
my0=my; //seems the assignment fails
}
for(int i=0; i<nVertex; i++)
{
if(0!=lb) // (FSMOUSEEVENT_LBUTTONDOWN==evt)
{
vertex[i].x1=vertex[i].x1+(mx-mx0);
vertex[i].y1=vertex[i].y1+(my-my0);
vertex[i].x2=vertex[i].x2+(mx-mx0);
vertex[i].y2=vertex[i].y2+(my-my0);
}
}
}
int main(void)
{
Polyg plg;
char fn[256];
printf("Enter input file name>");
gets(fn);
plg.ReadFromFile(fn);
FsOpenWindow(16,16,800,600,1);
while(FSKEY_ESC!=FsInkey())
{
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
plg.Move();
plg.DrawWireframe();
FsPollDevice();
if (FSKEY_SPACE==FsInkey())
{
plg.ReadFromFile(fn);
}
FsSwapBuffers();
FsSleep(10);
}
return 0;
}
|