2023年5月4日 星期四

Week12

 

Week12-1 Printf fprintf

1.先創建一個Empty file

2.寫入程式碼,可生成文字檔並寫出 hello world




Week12-2 


1.在開新empty file ,但其程式目錄要和week12-1要在同個目錄,因為接下來程式碼要讀取week12-1生成的文字檔


2.寫入程式,並執行測試是否能讀取 file.txt 裡的文字




Week12-3 


1.先直接創建檔案 Empty file,並寫入程式,要創建一個文字檔,裡面有程式給予的指定數字列。


2.

Week12-4 Keyboard-mouse


1.創立一個GLUT專案,並將main預設程式碼刪掉並修改

參考程式碼
:#include<stdio.h>
#include<GL/glut.h>
float teapotX=0,teapotY=0;
FILE * fout = NULL;
FILE * fin = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(teapotX,teapotY,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    teapotX=(x-150)/150.0;
    teapotY=(150-y)/150.0;
    display();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}


2.透過滑鼠的點擊來讓茶壺移動

3.接下來要多加程式碼鍵盤控制茶壺移動

程式碼:
void mouse(int button,int state,int x,int y)
{
    teapotX=(x-150)/150.0;
    teapotY=(150-y)/150.0;
    if(state==GLUT_DOWN)
    {
        if(fout==NULL) fout=fopen("file4.txt","w");
        fprintf(fout,"%f %f\n",teapotX,teapotY);
    }
    display();
}
void keyboard(unsigned char key,int x,int y)
{
    if(fin==NULL)
    {
        fclose(fout);
        fin = fopen("file4.txt","r");
    }
    fscanf(fin,"%f%f",&teapotX,&teapotY);
    display();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}

4.先使用滑鼠點擊,讓茶壺移動並記錄軌跡,接著按空白鍵或任一按鍵會讓茶壺回朔上一個軌跡座標


Week12-4-1 複習更改工作讀取路徑


1.在codeblocks左邊的專案點擊右鍵,並點選 Properties

2.在接下來的視窗點進 Build targets > Execution working dir
   將路徑改回" . " ,工作路徑就會以現在程式專案目錄所在路      徑為主


Week12-5 Final Project備份


1.複製week12-4的整個專案,並將裡面的 .cbp檔改名成Final project並開啟


2.對左邊的專案點擊右鍵選 Properties > Project setting
   修改Title列改名為 Final Project 並儲存
   接著整個專案的檔案名都會是Final Project










沒有留言:

張貼留言