当前位置: 首页 > article >正文

FLTK - FLTK1.4.1 - demo - animated - v1

文章目录

    • FLTK - FLTK1.4.1 - demo - animated - v1
    • 概述
    • 笔记
    • END

FLTK - FLTK1.4.1 - demo - animated - v1

概述

知识点:

  • RGB图像的构造与绘制
  • 窗口类的封装
  • 超时回调的设置

笔记

// FLTK - FLTK1.4.1 - demo - animated - v1

#include "fltk_test.h"

// 如果要将fl demo的实现搬过来测试,就注释掉下面的宏
// #define DONT_USE_FL_DEMO

#ifdef DONT_USE_FL_DEMO
int fl_demo_main(int argc, char** argv)
{
	return 0;
}

#else

#endif // TEST_FL_DEMO

//
// Alpha rendering benchmark program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Image.H>
#include <FL/platform.H>
#include <FL/fl_draw.H>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// These constants define the image dimensions and
// the number of frames of the animation
const unsigned int dim = 256;
const unsigned int frames = 48;

static Fl_RGB_Image* img[frames];
static uchar curframe;

// 从这里能看到如何构造RGB图像数据
static void make_images() {

    unsigned i;
    for (i = 0; i < frames; i++) {
        const unsigned size = dim * dim * 4;
        uchar* data = new uchar[size];

        memset(data, 0, size);

        // First a black box, 10x10 pixels in the top-left corner
        int x, y;
        for (x = 0; x < 10; x++) {
            for (y = 0; y < 10; y++) {
                data[y * dim * 4 + x * 4 + 3] = 255;
            }
        }

        // A fading sphere
        uchar alpha = 255;
        if (i < frames / 2)
            alpha = uchar(255 * (i / ((float)frames / 2)));
        else
            alpha = uchar(255 * (((frames / 2) - (i - frames / 2)) / ((float)frames / 2)));

        const int spherew = 60;
        const int spherex = (dim - spherew) / 2;
        const int maxdist = (spherew / 2) * (spherew / 2);
        for (x = spherex; x < spherex + spherew; x++) {
            for (y = 20; y < 20 + spherew; y++) {

                float distx = x - (spherex + (float)spherew / 2);
                float disty = y - (20 + (float)spherew / 2);
                float dist = distx * distx + disty * disty;

                if (dist > maxdist)
                    continue;

                const float fill = dist / maxdist;
                const uchar grey = uchar(fill * 255);

                uchar myalpha = alpha;
                if (fill > 0.9)
                    myalpha *= uchar((1.0f - fill) * 10);

                data[y * dim * 4 + x * 4 + 0] = grey;
                data[y * dim * 4 + x * 4 + 1] = grey;
                data[y * dim * 4 + x * 4 + 2] = grey;
                data[y * dim * 4 + x * 4 + 3] = myalpha;
            }
        }

        // A moving blob
        const float pos = (i / (float)frames) * 2 - 0.5f;

        const int xoffset = int(pos * dim);
        const int yoffset = 2 * dim / 3;
        const int w = dim / 4;

        for (x = -w; x < w; x++) {
            if (x + xoffset < 0 || x + xoffset >= (int)dim)
                continue;
            for (y = yoffset - w; y < yoffset + w; y++) {
                const uchar grey = abs(y - yoffset);
                data[y * dim * 4 + (x + xoffset) * 4 + 2] = grey;
                data[y * dim * 4 + (x + xoffset) * 4 + 3] = 127;
            }
        }

        img[i] = new Fl_RGB_Image(data, dim, dim, 4);
    }
}

// 窗体一般是从双缓冲窗口Fl_Double_Window继承
class window : public Fl_Double_Window {
public:
    window(int X, int Y, const char* lbl) : Fl_Double_Window(X, Y, lbl) {}

    // 重载父类的虚函数
    void draw() FL_OVERRIDE {
        Fl_Double_Window::draw();

        // Test both cx/cy offset and clipping. Both borders should have a 5-pixel edge,
        // and the upper-left black box should not be visible.
        fl_push_clip(5, 5, w() - 5, h() - 5);
        img[curframe]->draw(0, 0, dim, dim, 5, 5);
        fl_pop_clip();
    }
};

static window* win;

static void cb(void*) {

    win->redraw();

    Fl::repeat_timeout(1.0f / 24, cb);

    curframe++;
    curframe %= frames;
}

int fl_demo_main(int argc, char** argv) {
    // 可以将窗体封装成类,可维护性好
    win = new window(256, 256, "Alpha rendering benchmark, watch CPU use");
    win->color(fl_rgb_color(142, 0, 0));

    make_images();

    win->end();
    win->show(argc, argv);

    // 设置超时回调
    Fl::add_timeout(1.0f / 24, cb);

    return Fl::run();
}

在这里插入图片描述

END


http://www.kler.cn/a/523555.html

相关文章:

  • Linux pkill 命令使用详解
  • SQL注入漏洞之高阶手法 宽字节注入以及编码解释 以及堆叠注入原理说明
  • 用 Scoop 优雅管理 Windows 软件:安装、配置与使用全指南
  • 【Julia】Julia预编译与外部库:从崩溃到完美集成
  • 【win11】解决msrdc.exe窗口启动导致周期性失去焦点
  • 【Pandas】pandas Series cov
  • Spring Boot 实现文件上传和下载
  • 【Go语言圣经】第四节:复合数据类型
  • 8622 哈希查找
  • LabVIEW纤维集合体微电流测试仪
  • 子2023
  • Linux(19)——使用正则表达式匹配文本
  • Linux 下注册分析(2)
  • 第31章 测试驱动开发中的设计模式与重构解析(Python 版)
  • .net 如何处理网页的Json请求?
  • LLM评估优化与新技术创新综述
  • 基于STM32的数字多重仪表教学
  • 编程题-最长的回文子串(中等)
  • 一文讲解CMS收集器的垃圾收集过程
  • vue3阻止事件冒泡到父元素
  • FLTK - FLTK1.4.1 - demo - animgifimage-play
  • FLTK - FLTK1.4.1 - demo - animgifimage
  • 漂亮数 (线性筛+前缀和)
  • 【小白学AI系列】NLP 核心知识点(五)Transformer介绍
  • 99.19 金融难点通俗解释:营业总收入vs归母净利润vs扣非净利润
  • 新鲜速递:DeepSeek-R1开源大模型本地部署实战—Ollama + MaxKB 搭建RAG检索增强生成应用