如何将主文件分成三个文件:头文件、客户端和实现

How can I break the main file into three files: header file, client, and implementation

如何在 C 编程的 SFML 中将主要方法分解为更小的方法,而不是将主文件分解为三个文件:头文件、客户端和实现。

我们的主文件中没有函数,只有主方法,如何将主方法分解成更小的方法,每个方法都针对特定任务?您打算为这些方法使用什么名称?这些方法的输入是什么? 根据您打算如何将主要方法分解为更小的方法,创建您的接口以包含方法原型 剩下的就是客户端。连接您的方法并以正确的顺序调用它们以获得我们之前对 main.c 文件的相同行为。 编程时要考虑的事项:

不允许使用全局变量 对于方法参数,只选择需要发送的参数。如果一个参数只有一个方法需要,那么它必须是这个方法的局部变量。

//include directive
#include <stdio.h>
#include <SFML/Graphics.h>

//main function
int main(void)
{

    // change the size of the window
    sfVideoMode mode = {200, 300, 32};

    sfRenderWindow* window;
    sfCircleShape* circle;
    sfSprite* sprite;
    sfEvent event;

    // Create the main window with my name on the top
    window = sfRenderWindow_create(mode, "CSFML", sfResize | sfClose, NULL);
    //window is different than return 1.
    if (!window)
        return 1;

    //create a circle to display
    circle = sfCircleShape_create();
      if(!circle)
        return 1;

    //changes the radius of the circle
    sfCircleShape_setRadius(circle, 100);

    //changes the color of the cicrcle
    sfCircleShape_setFillColor(circle, sfRed);

    // Start the window loop
    while (sfRenderWindow_isOpen(window))
    {

        while (sfRenderWindow_pollEvent(window, &event))
        {
            // Close window: exit
            if (event.type == sfEvtClosed)

            sfRenderWindow_close(window);
        }

        // Clear screen and change the color of the window
        sfRenderWindow_clear(window, sfBlack);

        sfRenderWindow_drawCircleShape(window, circle, NULL);

        //display window
        sfRenderWindow_display(window);
    }
    //destroy the window and the circle
    sfCircleShape_destroy(circle);

    sfRenderWindow_destroy(window);

    return 0;
}

听起来您来自 Java 背景。在 C 中,所有 "methods" 实际上都被称为 "functions." 话虽这么说,听起来您正试图将这个较大的文件拆分为三个较小的文件,这些文件最终只执行相同的任务。

为此,我实际上建议创建某种 window header 和这样的客户端组合:

在新文件中utils.h:

/* This file will contain a series of sfml utilities. */
#include <SFML/Graphics.h>

// window stuff
int createWindow(sfRenderWindow* winToInit, sfVideoMode winMode, char* winName);
int clearWindow(sfRenderWindow* winToClear, sfColor clearColor);
int displayWindow(sfRenderWindow* winToDisplay);
int drawCircle(sfRenderWindow* winToDrawIn, sfCircleShape* circToDraw);
int isWinOpen(sfRenderWindow* winToCheck);
int destroyWindow(sfRenderWindow** winToDestroy);

// circle stuff
int createCircle(sfCircleShape* circToInit, int circRadius, sfColor circColor);
int destroyCircle(sfCircleShape);

现在用不同的方式定义这些函数classutils.c:

/* This file will define functions in utils.h */
#include "utils.h"

// window stuff
int createWindow(sfRenderWindow* winToInit, sfVideoMode winMode, char* winName) {
    // Create the main window with my name on the top
    winToInit = sfRenderWindow_create(winMode, winName, sfResize | sfClose, NULL);

    //window is different than return 1.
    if (!window)
        return -1;
    else
        return 0;
}

int clearWindow(sfRenderWindow* winToClear, sfColor clearColor) {
    // Clear screen and change the color of the window
    sfRenderWindow_clear(winToClear, clearColor);
    return 0;
}

int displayWindow(sfRenderWindow* winToDisplay) {
    //display window
    sfRenderWindow_display(winToDisplay);
    return 0;
}

int drawCircle(sfRenderWindow* winToDrawIn, sfCircleShape* circToDraw) {
    sfRenderWindow_drawCircleShape(winToDrawIn, circToDraw, NULL);
    return 0;
}

int isWinOpen(sfRenderWindow* winToCheck) {
    if (sfRenderWindow_isOpen(winToCheck))
        return 1;
    else
        return 0;
}

int destroyWindow(sfRenderWindow** winToDestroy) {
    sfRenderWindow_destroy(winToDestroy);
    return 0;
}

...

我相信您将能够创建 Circle 实用程序。

然后,在创建所有这些实用程序之后,只需替换主文件的部分内容即可!三个与原始文件功能相同的文件。