-
Notifications
You must be signed in to change notification settings - Fork 1
/
Untitled Document 1
538 lines (501 loc) · 8.49 KB
/
Untitled Document 1
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
1 0 1000.0 1500.0 1
2 1 1500.0 1500.0 1
3 1 1250.0 1800.0 1
4 1 1000.0 1500.0 1
1 0 1 1000.000000 1500.000000
2 1 1 1500.000000 1500.000000
3 1 1 1250.000000 1800.000000
4 1 1 1000.000000 1500.000000
1 0 1 1.000000e+03 1.500000e+03
2 1 1 1.500000e+03 1.500000e+03
3 1 1 1.250000e+03 1.800000e+03
4 1 1 1.000000e+03 1.500000e+03
0 500 1500 1
1 750 2000 1
1 1000 1500 1
1 500 1800 1
1 1000 1800 1
1 500 1500 1
0 3000 1500 1
1 3250 2000 1
1 3500 1500 1
1 3000 1800 1
1 3500 1800 1
1 3000 1500 1
0 1200 3500 1
2 200 2 1
0 2800 3500 1
2 200 2 1
0 1250 750 1
2 100 2 1
0 2450 1000 1
2 100 2 1
0 50 50 1
1 3950 50 1
1 3950 3950 1
1 50 3950 1
1 50 50 1
0 2000 3950 1
2 1950 1 1
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
#include<math.h>
#include </home/pruthvi/project/CG/new.h>
FILE *fp;
int numseg=0;
void * font = GLUT_BITMAP_9_BY_15;
static int addingmenu;
static int menuid;
static int animationmenu;
static int mouse_val = 0;
static int win=0,win_no=0,zx=0,window_no=0,tresx[10],tresy[10],xnew,ynew,sx=2,sy=2;
void createMenu(void);
void Init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,4000,0,4000);
}
struct segment{
int id;
int tx;
int ty;
int sx;
int sy;
int theta;
int start_add;
int end_add;
int visi;
struct segment * next;
};
typedef struct segment node;
node *head=NULL;
void create_segment()
{
int temp,opcode,line,vis;
node *p;
float x,y;
fp = fopen("disp2.txt","r");
while(!feof(fp))
{
if(numseg==0)
{
head=(node*)malloc(sizeof(node));
p=head;
numseg++;
}
fscanf(fp,"%d %d %d %d %d",&line,&opcode,&x,&y,&vis);
if(opcode==0)
{
if(numseg!=1)
{
p->end_add=line;
printf("%d %d\n",p->start_add,p->end_add);
}
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->id=numseg;
p->start_add=line;
p->visi=vis;
p->tx=0;
p->ty=0;
p->sx=0;
p->sy=0;
p->theta=0;
p->next=NULL;
numseg++;
}
}
p->end_add=line;
p->next=NULL;
fclose(fp);
}
void trans(int x,int y)
{
int a[3][3],b[3][1],ans[3][1];
int qa,j,k;
for(qa=0;qa<3;qa++)
{
for(j=0;j<3;j++)
{
if(qa==j)
a[qa][j]=1;
else
a[qa][j]=0;
}
}
a[0][2]=600;
a[1][2]=600;
b[0][0]=x;
b[1][0]=y;
b[2][0]=1;
for(qa=0;qa<3;qa++)
{
for(j=0;j<3;j++)
{
ans[qa][j]=0;
for(k=0;k<3;k++)
ans[qa][j]+=a[qa][k]*b[k][j];
}
}
xnew=ans[0][0];
ynew=ans[1][0];
}
void scale(int x,int y)
{
int a[3][3],b[3][1],ans[3][1];
int wi,j,k;
for(wi=0;wi<3;wi++)
{
for(j=0;j<3;j++)
{
if(wi==j && wi==0)
a[wi][j]=sx;
else if(wi==j && wi==1)
a[wi][j]=sy;
else
a[wi][j]=0;
}
}
a[2][2]=1;
b[0][0]=x;
b[1][0]=y;
b[2][0]=1;
for(wi=0;wi<3;wi++)
{
for(j=0;j<3;j++)
{
ans[wi][j]=0;
for(k=0;k<3;k++)
ans[wi][j]+=a[wi][k]*b[k][j];
}
}
xnew=ans[0][0];
ynew=ans[1][0];
}
void translate(int transx,int transy){
SetPixel(transx,transy);
}
void circle(int rd,int ch)
{
}
void show()
{
int n1=1,line,opcode,last_op,x,y,x1,y1,vis;
node *p;
p=head;
fp = fopen("disp1.txt","r");
while(!feof(fp))
{
fscanf(fp,"%d %d %d %d %d",&line,&opcode,&x,&y,&vis);
switch(opcode)
{
case 0: translate(x,y);
x1=x;y1=y;
break;
case 1: dda(x1,y1,x,y);
x1=x;y1=y;
break;
case 2: circle(x,y);
break;
case 3: SetPixel(x,y);
break;
}
}
fclose(fp);
}
void show1()
{
int n1=1,line,opcode,last_op,x,y,x1,y1,vis;
node *p;
p=head;
fp = fopen("disp2.txt","r");
while(!feof(fp))
{
fscanf(fp,"%d %d %d %d %d",&line,&opcode,&x,&y,&vis);
switch(opcode)
{
case 0: translate(x,y);
x1=x;y1=y;
break;
case 1: dda(x1,y1,x,y);
x1=x;y1=y;
break;
case 2: circle(x,y);
break;
case 3: SetPixel(x,y);
break;
}
}
fclose(fp);
}
void change_1()
{
int line,vis,opcode,x,y,x1,y1;
int a=0,c=0;
fp = fopen("disp2.txt","r+");
while(c!=4)
{
fscanf(fp,"%d %d ",&line,&opcode);
x=tresx[a];
y=tresy[a];
fprintf(fp,"%d %d",tresx[a],tresy[a]);
fscanf(fp,"%d",&vis);
printf("%d %d %d %d %d\n",line,opcode,tresx[a],tresy[a],vis);
a++;
if(a==3)
a=0;
switch(opcode)
{
case 0: translate(x,y);
x1=x;y1=y;
break;
case 1: dda(x1,y1,x,y);
x1=x;y1=y;
break;
case 2: circle(x,y);
break;
case 3: SetPixel(x,y);
break;
}
c++;
}
fclose(fp);
}
void menu2(int value2){
if(value2 == 12)
{
glutDestroyWindow(win);
}
else if (value2 == 11)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
display2();
glEnd();
glFlush();
window_no=3;
}
}
void createMenu2(void){
menuid = glutCreateMenu(menu2);
glutAddMenuEntry("START GAME",11);
glutAddMenuEntry("QUIT", 12);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void call1()
{
renderBitmapString(1600,3500,(void *)font1,"WELCOME :)");
}
void call2()
{
renderBitmapString(500,3500,(void *)font,"Welcome to the game! :)");
renderBitmapString(500,3000,(void *)font,"Instructions:");
renderBitmapString(600,2500,(void *)font,"-The goal of this game is to eat the food which is displayed on the screen at random points and make the snake grow longer.");
renderBitmapString(600,2000,(void *)font,"-Use the arrow keys to move the snake left,right,up and down.");
renderBitmapString(600,1500,(void *)font,"-The game will be over once the snake hits the boundary.");
renderBitmapString(2000,1000,(void *)font,"All the best!!");
createMenu2();
}
void rot(int x,int y)
{
}
void show3()
{
int n1=1,line,opcode,last_op,x,y,x1,y1,vis,u,o,arx[10],ary[10];
node *p;
p=head;
while(p!=NULL)
{
p=p->next;
fp = fopen("disp2.txt","r");
while(n1!=p->start_add)
{
fscanf(fp,"%d %d %d %d %d",&line,&opcode,&x,&y,&vis);
last_op=opcode;
n1++;
}
o=0;
while(n1!=p->end_add)
{
fscanf(fp,"%d %d %d %d %d",&line,&opcode,&x,&y,&vis);
arx[o]=x;
ary[o++]=y;
n1++;
}
fscanf(fp,"%d %d %d %d %d",&line,&opcode,&x,&y,&vis);
arx[o]=x;
ary[o++]=y;
fclose(fp);
p=p->next;
}
u=0;
switch(mouse_val)
{
case 1:
while(u<=3)
{ printf("translate\n");
trans(arx[u],ary[u]);
tresx[u]=xnew;
tresy[u]=ynew;
u++;
}
break;
case 2:
while(u<=3)
{ printf("rotate\n");
rot(arx[u],ary[u]);
tresx[u]=xnew;
tresy[u]=ynew;
u++;
}
break;
case 3:
while(u<=3)
{ printf("scale\n");
scale(arx[u],ary[u]);
tresx[u]=xnew;
tresy[u]=ynew;
u++;
}
break;
}
}
void change_tra()
{
node *p=head;
while(p!=NULL)
{
p->tx=600;
p->ty=600;
p=p->next;
}
}
void change_sca()
{
node *p=head;
while(p!=NULL)
{
p->sx=2;
p->sy=2;
p=p->next;
}
}
void menu1(int value){
if(value == 10)
{
glutDestroyWindow(win);
}
else
{
mouse_val=value;
}
if(mouse_val == 1)
{
glClear(GL_COLOR_BUFFER_BIT);
change_tra();
show();
call1();
show3();
change_1();
zx++;
}
else if(mouse_val == 2)
{
}
else if(mouse_val == 3)
{
glClear(GL_COLOR_BUFFER_BIT);
change_sca();
show();
call1();
show3();
change_1();
zx++;
}
else if(mouse_val == 4)
{
}
else if(mouse_val == 5)
{
int x,y,vis,opcode,line;
fp = fopen("disp2.txt","r+");
line =1;
while(line !=5)
{
fscanf(fp,"%d %d %d %d",&line,&opcode,&x,&y);
fprintf(fp," %d\n",1);
line++;
}
zx++;
fclose(fp);
show1();
}
else if(mouse_val == 6)
{
}
else if(mouse_val == 7)
{
win_no++;
glClear(GL_COLOR_BUFFER_BIT);
call2();
}
}
void createMenu1(void){
animationmenu = glutCreateMenu(menu1);
glutAddMenuEntry("Translation", 1);
glutAddMenuEntry("Rotation", 2);
glutAddMenuEntry("Scaling", 3);
glutAddMenuEntry("Rubber band", 4);
menuid = glutCreateMenu(menu1);
glutAddMenuEntry("Add shapes",5);
glutAddSubMenu("Transformations", animationmenu);
glutAddMenuEntry("Clipping",6);
glutAddMenuEntry("continue", 7);
glutAddMenuEntry("quit", 10);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void display()
{
if(win_no==0)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
show();
createMenu1();
if(zx!=0)
show1();
call1();
}
else if (window_no==3)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
display2();
if((xa>=inx+50) ||(xa<=iny-50)||(ya>=inx) ||(ya<=iny))
{
call3();
glutSwapBuffers();
}
glEnd();
glFlush();
}
else
{
createMenu2();
call2();
}
glEnd();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB );
win=glutCreateWindow("welcome window");
Init();
create_segment();
glutDisplayFunc(display);
glutMainLoop();
}