2023年5月4日 星期四

week12 zhou

 Week12

首先開啟codeblock 新增一個新的檔案 .cpp
#include <stdio.h>
int main()
{
    FILE * fout = fopen("file.txt","w");
    printf("Hello World\n");
    fprintf(fout, "Hello World在檔案裡\n");
}
打上以上程式碼 執行
結果就會如下圖



開啟一個.cpp檔案
#include <stdio.h>
int main()
{
    char line[20];
    FILE * fin = fopen("file.txt","r");
    fscanf(fin, "%s", line);
    printf("讀到了:%s\n",line);
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);
}
打上以上程式碼 並且執行
執行結果如下圖



新增一個新的.cpp檔案
#include <stdio.h>
int main()
{

    int a[10] = {10,20,30,40,50,60,70,80,90,100};
    FILE * fout = fopen("file3.txt","w");
    for(int i=0; i<10; i++){
        fprintf(fout,"%d ", a[i]);
        printf("%d ",a[i]);
    }
    fclose(fout);

    int b[10];
    FILE * fin = fopen("file3.txt","r");
    for(int i=0; i<10; i++){
        fprintf(fin,"%d", b[i]);
        printf("%d ",b[i]);
    }
    fclose(fin);
}
打上以上程式碼並且執行
執行結果如下



新增一個新的GLUT專案
檔案名為week12-4_keyboard_mouse
用程式製作出一個可以隨著滑鼠點到哪裡
茶壺就到哪裡的程式

#include <stdio.h>
#include <GL/glut.h>
float teapotX = 0, teapotY = 0;
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();
}


執行程式碼就會出現以下畫面囉


將剛剛的程式碼新增且修改為以下程式碼
紅色的部分為新增的程式碼

#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();
}
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();
}

執行結果如下圖




接著再將程式碼修改為以下程式碼


#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;
    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();
}


接著照著上周的步驟將執行工作的路徑
改成在自己的資料夾不是在freeglut裡執行
先到project  properties裡面


接著到下圖紅框的地方的路徑改成
一個 . 
再把 freeglut.dll移動到12-4的資料夾











沒有留言:

張貼留言