English 中文(简体)
将主(主)职能划分为分离职能
原标题:Dividing main() function into separated functions

我刚刚在YouTube上复制了一部非常简单的动画法,以了解如何在射线上做一im。 但整个代码在<条码>main(功能>内写。 我希望这能够组织和分离。

Here is the code:

#include "include/raylib.h"

int main()
{
    const int screenWidth = 1200;
    const int screenHeight = 800;

    InitWindow(screenWidth, screenHeight, "2D Platformer");
    SetTargetFPS(60);

    Texture2D run_sprite = LoadTexture("sprites/Fighter/Run.png");
    Rectangle source = {0.f, 0.f, (float)run_sprite.width / 8.0f, (float)run_sprite.height};
    Vector2 position = {0, screenHeight / 2};

    int frame = 0;
    float runningTime{};
    const float updateTime{1.f/12.f};

    while (WindowShouldClose() == false)
    {
        //UPDATE VARIABLES
        float deltaTime = GetFrameTime();
        runningTime += deltaTime;
        if (runningTime >= updateTime)
        {
            runningTime = 0.f;
            source.x = (float)frame * source.width;
            frame++;
            if (frame > 8)
            {
                frame = 0;
            }
        }


        //START DRAWING
        BeginDrawing();
            ClearBackground(RAYWHITE);
            DrawTextureRec(run_sprite, source, position, WHITE);
        EndDrawing();
    }

    CloseWindow();
}

I tried to divide it into these functions:

Texture2D run_sprite;

void load_textures()
{
    run_sprite = LoadTexture("sprites/Fighter/Run.png");
}

int frame = 0;
float runningTime{};
const float updateTime{1.f/12.f};

Rectangle source = {0.f, 0.f, (float)run_sprite.width / 8.0f, (float)run_sprite.height};
void update_run_animation()
{
    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        runningTime = 0.f;
        source.x = (float)frame * source.width;
        frame++;
        if (frame > 8)
        {
            frame = 0;
        }
    }
}

Vector2 position = {0, screenHeight / 2};
void render_run_animation()
{
    DrawTextureRec(run_sprite, source, position, WHITE);
}

此处为<代码>main(功能>内的最新代码:

int main()
{
    const int screenWidth = 1200;
    const int screenHeight = 800;

    InitWindow(screenWidth, screenHeight, "2D Platformer");
    SetTargetFPS(60);

    load_textures();

    while (WindowShouldClose() == false)
    {
        update_run_animation();

        BeginDrawing();
            ClearBackground(RAYWHITE);
            render_run_animation();
        EndDrawing();
    }

    CloseWindow();
}

But when I run this, it s just creating a window and not drawing the texture.

我试图从每个功能的变数中获取产出,以加以缩减。 结论是<代码>update_run_animation( function,source.width。 变量给出的违约值为0。

I don t know what the problem is exactly, so I posted the whole code.

最佳回答

The initialization of global variables happens before the main function even runs. So the values you use in the initialization of source (and position) will not be what you expect.

在知道这些变量所依赖的数值时,你需要明确地将<代码>main中的这些变量初步化(签字)。

问题回答

One problem is here

Rectangle source = {0.f, 0.f, (float)run_sprite.width / 8.0f, (float)run_sprite.height};

由于<代码> 是全球变量,该代码在<代码>前执行,在文本上载<>/代码>之后执行(如原方案)。

The simplest fix would seem to be to move the declaration to main

int main()
{
    ...
    load_textures();
    Rectangle source = {0.f, 0.f, (float)run_sprite.width / 8.0f, (float)run_sprite.height};
    ...
}

然后将变量作为参考参数通过<代码>更新日期_run_animation功能,必要时

update_run_animation(source);

并相应修改这一职能

void render_run_animation(Rectangle& source)
{
    DrawTextureRec(run_sprite, source, position, WHITE);
}

我认为,还需要作出许多其他改变。 一般不使用全球变量。

This is all completely untested.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签