我刚刚在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.