Gino Gino - 2 months ago 7
C++ Question

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

How can I break the main method into smaller methods and than break the main file into three files: header file, client, and implementation in SFML in C programming.

There is no functions in the main file we have, only the main method, how can you break the main method into smaller ones, each specific for a certain task? What name are you going to use for these methods? what would be the input for the methods?
Based on how you are going to break the main into smaller methods, create your interface to include the method prototype
Remaining is the client. Connect your methods and call them in the correct order to obtain the same behavior we had before for the main.c file.
Things to consider while programming:

No global variables are allowed
For the method parameters, chose only the ones that need to be sent. If a parameter only needed by a single method, then it has to be a local variable to this method.

//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;
}

Answer

It sounds like you're coming from a Java background. In C, all "methods" are actually just called "functions." That being said, it sounds like you're trying to split this larger file into three smaller files that ultimately just performs the same task.

For this, I would actually suggest creating some sort of window header and client combination like such:

In a new file 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);

Now define these functions in a different class utils.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;
}

...

I trust you will be able to create the Circle utilities.

Then, after you create all of these utilities, just replace parts of your main file and boom! three files with the same functionality as the original.