2022年5月23日 星期一

Zfish week14

一:寫檔、關檔、讀檔
 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);///關閉檔案
}





二:使用week13_TRT,進行寫檔、讀檔

2-1:動作寫進檔

#include <GL/glut.h>
#include <stdio.h>///為了printf,fopen,fclose...
float angle[20], oldX=0;
int angleID=0;///0:第0個關節, 1:第1個關節...
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;///預設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){
    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);///身體瘦身(對比1)
    glPushMatrix();///右半邊
        glTranslatef(0.3, 0.5, 0);
        glRotatef(angle[0], 0, 0, 1);///旋轉
        glTranslatef(-0.3, -0.4, 0);///旋轉中心放正中心
        glColor3f(1, 0, 0);
        glRectf(0.3, 0.5, 0.8, 0.3);///右上手臂
        glPushMatrix();
            glTranslatef(0.8, 0.4, 0);///把下手肘掛在關節上
            glRotatef(angle[1], 0, 0, 1);///旋轉
            glTranslatef(-0.8, -0.4, 0);///把下手肘的旋轉中心放正中心
            glColor3f(0, 1, 0);
            glRectf(0.8, 0.5, 1.1, 0.3);///右下手肘
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左半邊
        glTranslatef(-0.3, 0.5, 0);
        glRotatef(angle[2], 0, 0, 1);///旋轉
        glTranslatef(+0.3, -0.4, 0);///旋轉中心放正中心
        glColor3f(1, 0, 0);
        glRectf(-0.3, 0.5, -0.8, 0.3);///左上手臂
        glPushMatrix();
            glTranslatef(-0.8, 0.4, 0);///把下手肘掛在關節上
            glRotatef(angle[3], 0, 0, 1);///旋轉
            glTranslatef(+0.8, -0.4, 0);///把下手肘的旋轉中心放正中心
            glColor3f(0, 1, 0);
            glRectf(-0.8, 0.5, -1.1, 0.3);///左下手肘
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
    //glutInitWindowSize(600, 600);
    glutCreateWindow("week14_rect_TRT");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2-2:增加讀檔

#include <GL/glut.h>
#include <stdio.h>///為了printf,fopen,fclose...
float angle[20], oldX=0;
int angleID=0;///0:第0個關節, 1:第1個關節...
FILE * fout=NULL, * fin=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 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;///預設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){
    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);///身體瘦身(對比1)
    glPushMatrix();///右半邊
        glTranslatef(0.3, 0.5, 0);
        glRotatef(angle[0], 0, 0, 1);///旋轉
        glTranslatef(-0.3, -0.4, 0);///旋轉中心放正中心
        glColor3f(1, 0, 0);
        glRectf(0.3, 0.5, 0.8, 0.3);///右上手臂
        glPushMatrix();
            glTranslatef(0.8, 0.4, 0);///把下手肘掛在關節上
            glRotatef(angle[1], 0, 0, 1);///旋轉
            glTranslatef(-0.8, -0.4, 0);///把下手肘的旋轉中心放正中心
            glColor3f(0, 1, 0);
            glRectf(0.8, 0.5, 1.1, 0.3);///右下手肘
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左半邊
        glTranslatef(-0.3, 0.5, 0);
        glRotatef(angle[2], 0, 0, 1);///旋轉
        glTranslatef(+0.3, -0.4, 0);///旋轉中心放正中心
        glColor3f(1, 0, 0);
        glRectf(-0.3, 0.5, -0.8, 0.3);///左上手臂
        glPushMatrix();
            glTranslatef(-0.8, 0.4, 0);///把下手肘掛在關節上
            glRotatef(angle[3], 0, 0, 1);///旋轉
            glTranslatef(+0.8, -0.4, 0);///把下手肘的旋轉中心放正中心
            glColor3f(0, 1, 0);
            glRectf(-0.8, 0.5, -1.1, 0.3);///左下手肘
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
    //glutInitWindowSize(600, 600);
    glutCreateWindow("week14_rect_TRT");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}






三:計時器、加聲音

3-1:計時器

#include <GL/glut.h>
#include <stdio.h>
void timer(int t){///t的單位是ms,->1000代表1秒
    printf("鬧鐘%d, 我起床了\n",t);
    printf("設定下一個鬧鐘\n");
    glutTimerFunc(1000,timer,t+1);
    printf("設好鬧鐘<再回去睡\n");
}
void display()
{

}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14_timer");

    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}



3-2:加聲音
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void timer(int t){///t的單位是ms,->1000代表1秒
    printf("鬧鐘%d, 我起床了\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    printf("設定下一個鬧鐘\n");
    glutTimerFunc(1000,timer,t+1);
}
void display()
{

}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14_timer");

    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}





沒有留言:

張貼留言