gpt4 book ai didi

c++ - 如何将整个 C++ 过剩程序编写为可以在另一个程序中调用的函数?

转载 作者:行者123 更新时间:2023-11-28 01:34:27 25 4
gpt4 key购买 nike

我想在我的 OpenGL 程序中调用下面的代码作为函数。我的问题是:

  1. 是否可以在 main() 之外调用 glutIdleFunc() ?

  2. 我可以在我的主程序中将下面的代码作为函数调用作为 switch case 函数吗?

    #include<GL/glut.h>
    #include<math.h>
    #include<stdio.h>
    int bzco[4][2]={{0,0},{49,201},{201,99},{320,300}},c[4],n=3;
    int s1x,s1y,s2x,s2y;
    void bezierCoefficients(int n,int *c)
    {
    int k,i;
    for(k=0;k<=n;k++)
    {
    c[k]=1;

    for(i=n;i>=k+1;i--)
    c[k]*=i;

    for(i=n-k;i>=2;i--)
    c[k]/=i;
    }
    }

    void display(void)
    {
    int k;

    float x,y,u,blend;

    glClear(GL_COLOR_BUFFER_BIT);
    // To draw points
    glColor3f(0.0,1.0,0.0);
    glPointSize(3);
    glBegin(GL_POINTS);
    glVertex2f(80, 34);
    glVertex2f(85, 24);
    glVertex2f(78, 24);
    glVertex2f(46, 35);
    glVertex2f(67, 47);
    glVertex2f(85, 26);
    glVertex2f(78, 68);
    glVertex2f(86, 56);
    glVertex2f(82, 54);
    glVertex2f(56, 69);
    glEnd();
    glColor3f(1.0,0.0,0.0);
    glPointSize(3);
    glBegin(GL_POINTS);
    glVertex2f(34, 38);
    glVertex2f(46, 35);
    glVertex2f(56, 69);
    glVertex2f(43, 47);

    glEnd();
    glColor3f(0.0,0.0,1.0);
    glLineWidth(3.0);
    glBegin(GL_LINE_STRIP);

    for(u=0;u<1.0;u+=0.001)
    {x=0;y=0;
    for(k=0;k<4;k++)
    {
    blend=c[k]*pow(u,k)*pow(1-u,n-k);
    x+=bzco[k][0]*blend;
    y+=bzco[k][1]*blend;
    }
    glVertex2f(x,y);

    }
    glEnd();
    glFlush();
    glutSwapBuffers();
    }
    void myinit()
    {
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glPointSize(5.0);
    gluOrtho2D(0.0,320.0,0.0,300.0);
    }
    void motion(void)
    {
    bzco[1][0]+=s1x;
    bzco[1][1]+=s1y;
    bzco[2][0]+=s2x;
    bzco[2][1]+=s2y;
    if(bzco[1][0]<0||bzco[1][0]>320)
    {
    s1x=-s1x;
    }
    if(bzco[1][1]<0||bzco[1][1]>300)
    {
    s1y=-s1y;
    }
    if(bzco[2][0]<0||bzco[2][0]>320)
    {
    s2x=-s2x;
    }
    if(bzco[2][1]<0||bzco[2][1]>300)
    {
    s2y=-s2y;
    }
    glutPostRedisplay();
    }

主要功能:

    int main(int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(320,300);
glutCreateWindow("Logistic Function");
glutDisplayFunc(display);
glutDisplayFunc(display);
glutIdleFunc(motion);
myinit();
bezierCoefficients(n,c);
s1x=-1;s1y=-1;s2x=-1;s2y=1;
glutMainLoop();
return 0;
}

在我的主程序中,我在 display() 函数中使用了 switch case,因为我必须调用上面的代码。

最佳答案

您应该创建一个库,将您的函数放入其中,然后导出它们以便能够从另一个应用程序访问它们。要在 Visual C++ 编译器中导出函数,您必须像下面这样声明它们:

void __declspec(dllexport) motion();

稍后您必须将它导入到您的应用程序中才能像这样使用它:

void __declspec(dllimport) motion();

你可以像这样声明一些像 MY_API 的宏:

#if (defined(_WIN32) || defined(_WIN32_WCE)) && defined(MY_LIB_DYNAMIC)
# if defined(MY_LIB_SOURCE)
# define MY_API __declspec(dllexport)
# else
# define MY_API __declspec(dllimport)
# endif
#endif

#if !defined(MY_API)
# if defined (__GNUC__) && (__GNUC__ >= 4)
# define MY_API __attribute__((visibility("default")))
# else
# define MY_API
# endif
#endif

然后像这样声明你的函数:

void MY_API motion();

它允许您使用相同的代码在 visual c++ 和 gcc 编译器中创建静态或动态库。如果你想静态编译你的库,你可以在没有任何预处理器定义的情况下编译它和你的应用程序。如果您想动态编译您的库,您可以将 MY_LIB_DYNAMIC 和 MY_LIB_SOURCE 作为预处理器定义添加到您的编译器中,但只使用 MY_LIB_DYNAMIC 作为预处理器定义来编译您的应用程序。

关于c++ - 如何将整个 C++ 过剩程序编写为可以在另一个程序中调用的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49965708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com