1-1
開檔,寫檔,關檔
#include <stdio.h>
int main()
{
FILE*fout=fopen("file.txt","w+");
printf("Hello World\n");
fprintf(fout,"Hello World\n");
fclose(fout);
}
1-2
#include <stdio.h>
int main()
{
FILE*fout=fopen("file2.txt","w+");
fprintf(fout,"angle1 %d\n",999);
fclose(fout);
char line[200];
int a;
FILE*fin=fopen("file2.txt","r");
fscanf(fin,"%s %d",line,&a);
printf("讀到了字串:%s及整數%d\n",line,a);
fclose(fin);
}
1-3
File-New-Project,GLUT新專案,week14_TRT_angle_write
新增函式myWrite,並在motion中新增myWrite()
#include <GL/glut.h>
#include<stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE*fout=NULL;
void myWrite(){
if(fout==NULL)fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf( "%.2f", angle[i]);
fprintf(fout,"%.2f", angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void keyboard( unsigned char key ,int x,int y){
if( key=='0' ) angleID=0;
if( key=='1' ) angleID=1;
if( key=='2' ) angleID=2;
if( key=='3' ) angleID=3;
}
void mouse(int button, int state , int x, int y)///mouse按下去
{
oldX = x;
}
void motion(int x,int y)
{
angle[angleID] += (x-oldX);
myWrite();
oldX=x;
glutPostRedisplay();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 1, 1);
glRectf(0.3, 0.5, -0.3, -0.2);///四邊形Rectangle,表白色身體
glPushMatrix();
glTranslatef(0.3, 0.5, 0);///等下要掛到0.5,0.5
glRotatef(angle[0], 0, 0, 1);///Z軸,(2)旋轉45度
glTranslatef(-0.3, -0.4, 0);///(1)把旋轉中心放到正中心
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.8, 0.3);///畫手臂
glPushMatrix();
glTranslatef(0.8, 0.4, 0);///(3)把下手肘掛在關節上
glRotatef(angle[1], 0, 0, 1);///(2)旋轉
glTranslatef(-0.8, -0.4, 0 );///(1)把下手肘的旋轉中心,放正中心
glColor3f(0,1,0);
glRectf(0.8, 0.5, 1.1, 0.3);///畫下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3, 0.5, 0);///等下要掛到0.5,0.5
glRotatef(angle[2], 0, 0, 1);///Z軸,(2)旋轉45度
glTranslatef(0.3, -0.4, 0);///(1)把旋轉中心放到正中心
glColor3f(1, 0, 0);
glRectf(-0.3, 0.5, -0.8, 0.3);///畫手臂
glPushMatrix();
glTranslatef(-0.8, 0.4, 0);///(3)把下手肘掛在關節上
glRotatef(angle[3], 0, 0, 1);///(2)旋轉
glTranslatef(0.8, -0.4, 0 );///(1)把下手肘的旋轉中心,放正中心
glColor3f(0,1,0);
glRectf(-0.8, 0.5, -1.1, 0.3);///畫下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();///2被交換的buffer
}
int main(int argc,char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(400,400);
glutCreateWindow("Week14_TRT_angle_write");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
2-1
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout = NULL, * fin = NULL;
void myWrite()
{
if(fin != NULL)
{
fclose(fin);
fin=NULL;
}
if(fout == NULL) fout = fopen("file.txt","w+");
for(int i=0;i<20;i++)
{
printf( "%.2f ",angle[i]);
fprintf(fout,"%.2f ",angle[i]);
}
printf("\n");
fprintf(fout,"\n");///少了fclose,不想只印一行
}
void myRead()
{
if(fout!= NULL) {fclose(fout);fout =NULL;}
if(fin == NULL) fin = fopen("file.txt","r");
for(int i=0;i<20;i++){
fscanf(fin,"%f",&angle[i]);
}
glutPostRedisplay();
}
void keyboard( unsigned char key ,int x,int y){
if( key=='0' ) angleID=0;
if( key=='1' ) angleID=1;
if( key=='2' ) angleID=2;
if( key=='3' ) angleID=3;
if( key=='r' ){
myRead();
}
}
void mouse(int button, int state , int x, int y)///mouse按下去
{
oldX = x;
}
void motion(int x,int y)
{
angle[angleID] += (x-oldX);
myWrite();
oldX=x;
glutPostRedisplay();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 1, 1);
glRectf(0.3, 0.5, -0.3, -0.2);///四邊形Rectangle,表白色身體
glPushMatrix();
glTranslatef(0.3, 0.5, 0);///等下要掛到0.5,0.5
glRotatef(angle[0], 0, 0, 1);///Z軸,(2)旋轉45度
glTranslatef(-0.3, -0.4, 0);///(1)把旋轉中心放到正中心
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.8, 0.3);///畫手臂
glPushMatrix();
glTranslatef(0.8, 0.4, 0);///(3)把下手肘掛在關節上
glRotatef(angle[1], 0, 0, 1);///(2)旋轉
glTranslatef(-0.8, -0.4, 0 );///(1)把下手肘的旋轉中心,放正中心
glColor3f(0,1,0);
glRectf(0.8, 0.5, 1.1, 0.3);///畫下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3, 0.5, 0);///等下要掛到0.5,0.5
glRotatef(angle[2], 0, 0, 1);///Z軸,(2)旋轉45度
glTranslatef(0.3, -0.4, 0);///(1)把旋轉中心放到正中心
glColor3f(1, 0, 0);
glRectf(-0.3, 0.5, -0.8, 0.3);///畫手臂
glPushMatrix();
glTranslatef(-0.8, 0.4, 0);///(3)把下手肘掛在關節上
glRotatef(angle[3], 0, 0, 1);///(2)旋轉
glTranslatef(0.8, -0.4, 0 );///(1)把下手肘的旋轉中心,放正中心
glColor3f(0,1,0);
glRectf(-0.8, 0.5, -1.1, 0.3);///畫下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();///2被交換的buffer
}
int main(int argc,char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("Week14 rect TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
2-2
修正工作目錄
進入week14_TRT_angle_write_and_read.cdp檔案
利用notepad++來修正
將working_dir後方的路徑改成"."
最後到codeblock查看是否成功
3-1
File-New-Project,GLUT新專案,week14_timer
#include <stdio.h>
void timer (int t)
{ ///1000代表1秒,1500代表1.5秒
printf("鬧鐘%d, 我起床了\n", t);///起床做事
printf("設定下一個鬧鐘\n");
glutTimerFunc(1000, timer, t+1);
printf("設好鬧鐘再回去睡\n");
}
void display()
{
}
int main(int argc, char**argv)
{///進階的main函式
glutInit( &argc, argv);///初始化
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
// glutInitWindowSize(600, 600);
glutCreateWindow("week14_timer");///建視窗
glutTimerFunc(3000, timer, 0);
glutDisplayFunc( display );///畫圖函式
glutMainLoop();///主要迴圈
}
3-2
#include <GL/glut.h>
#include <stdio.h>
void timer (int t)
{ ///1000代表1秒,1500代表1.5秒
printf("鬧鐘%d, 我起床了\n", t);///起床做事
PlaySound("do.wav",NULL,SND_ASYNC);
printf("設定下一個鬧鐘\n");
glutTimerFunc(1000, timer, t+1);
printf("設好鬧鐘再回去睡\n");
}
void display()
{
}
int main(int argc, char**argv)
{///進階的main函式
glutInit( &argc, argv);///初始化
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
// glutInitWindowSize(600, 600);
glutCreateWindow("week14_timer");///建視窗
glutTimerFunc(3000, timer, 0);
glutDisplayFunc( display );///畫圖函式
glutMainLoop();///主要迴圈
}







沒有留言:
張貼留言