C语言 循环彩色动画



我想要一种方法来循环切换白色和黑色,就像 C 中的动画一样

这是我以前使用的东西

static void ChangeWordColor()
{
if (color_cycle == 11)
{
color_cycle = 0;
}
switch (color_cycle)
{
case 0:
updatecolor(0xFFFFFF);
b4color = 0xFFFFFF;
break;
case 1:
updatecolor(0xDCDCDC);
break;
case 2:
updatecolor(0xD3D3D3);
break;
case 3:
updatecolor(0xC0C0C0);
break;
case 4:
updatecolor(0xA9A9A9);
break;
case 5:
updatecolor(0x808080);
break;
case 6:
updatecolor(0x696969);
break;
case 7:
updatecolor(0x808080);
break;
case 8:
updatecolor(0xA9A9A9);
break;
case 9:
updatecolor(0xC0C0C0);
break;
case 10:
updatecolor(0xD3D3D3);
break;
case 11:
updatecolor(0xDCDCDC);
break;
}
color_cycle++;
}
void FT_print_text_animate(uint dst_x, uint dst_y, int per, const char text)
{
if (frame % per == 0)
{
//every while(1) loop is "per" so every 2 loop cycles it changes color
ChangeWordColor();
}
FT_loop_text(dst_x, dst_y, text, 2);
}

我需要一种方法来制作动画,也许使用增量时间或更好的方法,也许是颜色的位移 此外,此代码使用 ARGB 和 updatecolor((;更改 FT 像素颜色。

我还需要任何解决方案才能在循环 ofc 中工作。

最好让颜色(或在这种情况下的灰度级别(更新负责自己的时间。 在下文中,只需要以比所需更新间隔稍快的间隔调用FT_print_text_animate()函数 - 它处理自己的计时,并且仅在需要时才更改颜色:

#include <time.h>
#include <stdint.h>
#define GREY_LEVEL_CHANGE_PERIOD (CLOCKS_PER_SEC / 5)    // Change five time per second (for example)
static void UpdateTextGreyLevel()
{
// Levels to cycle back-and forth through
static const uint32_t grey_levels[] = { 0xFF,0xDC,0xD3,0xC0,0xA9,0x80,0x69 } ;
// Current grey index and direction through cycle
static int cycle_index = 0 ;
static int direction = -1 ;
// Set current grey level
uint32_t grey_level = grey_levels[cycle_index] ; 
updatecolor( grey_level << 16 | grey_level << 8 | grey_level );
// If it is time to change grey level....
static clock_t last_change = 0 ;
clock_t now = clock() ;
if( now - last_change > GREY_LEVEL_CHANGE_PERIOD )
{
// Start new interval
last_change = now ;
// Change direction at either end of the cycle
if( cycle_index == sizeof(grey_levels) - 1 || cycle_index == 0 )
{
direction = -direction ;
}
// Increment/decrement cycle index
cycle_index += direction ;
}
}
void FT_print_text_animate( uint dst_x, uint dst_y, const char text )
{
UpdateTextGreyLevel() ;
FT_loop_text( dst_x, dst_y, text, 2 ) ;
}

例如,你可能有一个主循环:

for(;;)
{
FT_print_text_animate(0, 0, "Hello!" ) ;
} 

无论如何,灰度级别将以 200 毫秒(在本例中(的间隔变化。如果您也在"移动"文本,那么仓位的确定可能会使用类似的调用率独立时间来更新仓位。

本质上,您尽可能快地调用函数,并让函数确定是否该更新任何内容。 这样,您的所有计时都可以完全相互独立,并且不依赖于任何循环更新间隔。

要更改灰阶的数量和值,这里只需要更改grey_levels数组 - 其他所有内容都会自行排序。

最新更新