Mint_21.3 drawing-area和goocanvas的FB笔记(七)

FreeBASIC gfx 基本 graphics 绘图

8、ScreenControl与屏幕窗口位置设置

FreeBASIC通过自建屏幕窗口摆脱了原来的屏幕模式限制,既然是窗口,在屏幕坐标中就有它的位置。ScreenControl GET_WINDOW_POS x, y 获取窗口左上角的x, y位置;ScreenControl SET_WINDOW_POS x, y则将窗口放在屏幕坐标的 x, y处,配合 ScreenEvent(@e),下面的程序在获取快速鼠标点击窗口绘图区时快速变动窗口位置并回到原处,看上去是在抖动。当判断鼠标点击的关闭窗口时,退出程序。

在Do与Loop的尾部,有一个sleep 5 毫秒的语句,让CPU更好地处理其它事件。

'' examples/manual/gfx/screencontrol.bas
''
'' Example extracted from the FreeBASIC Manual
'' from topic 'SCREENCONTROL'
''
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgScreencontrol
'' --------'' include fbgfx.bi for some useful definitions
#include "fbgfx.bi"'' use FB namespace for easy access to types/constants
Using FBDim e As Event
Dim As Long x0, y0, x, y
Dim As Integer shakes = 0
Dim As Any Ptr imgScreenRes 320, 200, 32
Print "Click to shake window"'' find window coordinates
ScreenControl GET_WINDOW_POS, x0, y0DoIf (shakes > 0) Then'' do a shake of the windowIf (shakes > 1) Then'' move window to a random position near its original coordinatesx = x0 + Int(32 * (Rnd() - 0.5))y = y0 + Int(32 * (Rnd() - 0.5))ScreenControl SET_WINDOW_POS, x, yElse'' move window back to its original coordinatesScreenControl SET_WINDOW_POS, x0, y0End Ifshakes -= 1End IfIf (ScreenEvent(@e)) ThenSelect Case e.type'' user pressed the mouse buttonCase EVENT_MOUSE_BUTTON_PRESSIf (shakes = 0) Then'' set to do 20 shakesshakes = 20'' find current window coordinates to shake aroundScreenControl GET_WINDOW_POS, x0, y0End If'' user closed the window or pressed a keyCase EVENT_WINDOW_CLOSE, EVENT_KEY_PRESS'' exit to end of programExit DoEnd SelectEnd If'' free up CPU for other programsSleep 5Loop

9、图像的透明度处理

程序通过imagecreate创建一块像素存储内存,然后即可用circle和line在image绘RGBA格式的圆和线,它们的 A = alpha 值不同。左侧用put带参数Pset, 右侧用put 带参数alpha, 左右图的透明度发生了变化。

'' examples/manual/gfx/rgba.bas
''
'' Example extracted from the FreeBASIC Manual
'' from topic 'RGBA'
''
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgRgba
'' --------'open a graphics screen (320 * 240, 32-bit)
ScreenRes 320, 240, 32Dim As Any Ptr img
Dim As Integer x, y'make an image that varies in transparency and color
img = ImageCreate(64, 64)
For x = 0 To 63For y = 0 To 63PSet img, (x, y), RGBA(x * 4, 0, y * 4, (x + y) * 2)Next y
Next x
Circle img, (31, 31), 25,      RGBA(0, 127, 192, 192), ,,, F 'semi-transparent blue circle
Line   img, (26, 20)-(38, 44), RGBA(255, 255, 255, 0),    BF 'transparent white rectangle'draw a background (diagonal white lines)
For x = -240 To 319 Step 10Line (x, 0)-Step(240, 240), RGB(255, 255, 255)
NextLine (10,  10)-(310,  37), RGB(127, 0, 0), BF 'red box for text
Line (10, 146)-(310, 229), RGB(0, 127, 0), BF 'green box for Putting onto'draw the image and some text with PSET
Draw String(64, 20), "PSet"
Put(48,  48), img, PSet
Put(48, 156), img, PSet'draw the image and some text with ALPHA
Draw String (220, 20), "Alpha"
Put(208,  48), img, Alpha
Put(208, 156), img, Alpha'Free the image memory
ImageDestroy img'Keep the window open until the user presses a key
Sleep

put 最后的参数如果是 xor 则两个白色图叠加时共有部份就变成黑色。

put 后带 Pset 与 put 后带 trans 参数的区别

put 后带参数 or 的叠加图

imagecrete 三个图,用and叠加的效果

几种效果放在一起的效果图,做图方式是一样的,还是创建像素内存块,画广场然后 put 上去。

'' examples/manual/gfx/put-all.bas
''
'' Example extracted from the FreeBASIC Manual
'' from topic 'PUT (GRAPHICS)'
''
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPutgraphics
'' --------Declare Function checkered_blend( ByVal src As ULong, ByVal dest As ULong, ByVal param As Any Ptr ) As ULongScreen 14, 32                                   '' set 320*240*32 gfx modeDim As Any Ptr spriteDim As Integer counter = 0sprite = ImageCreate( 32, 32 )                  '' allocate memory for 32x32 spriteLine sprite, ( 0, 0 )-( 31, 31 ), RGBA(255, 0, 0, 64), bf  '' draw a sprite ...Line sprite, ( 4, 4 )-( 27, 27 ), RGBA(255, 0, 0, 192), bfLine sprite, ( 0, 0 )-( 31, 31 ), RGB(0, 255, 0), bLine sprite, ( 8, 8 )-( 23, 23 ), RGBA(255, 0, 255, 64), bfLine sprite, ( 1, 1 )-( 30, 30 ), RGBA(0, 0, 255, 192)Line sprite, ( 30, 1 )-( 1, 30 ), RGBA(0, 0, 255, 192)ClsDim As Integer i : For i = 0 To 63              '' draw the backgroundLine( i,0 )-( i,240 ), RGB( i * 4, i * 4, i * 4 )Next i'' demonstrate all drawing methods ...Put( 8,14 ), sprite, PSetPut Step( 16,20 ), sprite, PResetPut Step( -16,20 ), sprite, AndPut Step( 16,20 ), sprite, OrPut Step( -16,20 ), sprite, XorPut Step( 16,20 ), sprite, TransPut Step( -16,20 ), sprite, Alpha, 96Put Step( 16,20 ), sprite, AlphaPut Step( -16,20 ), sprite, Add, 192Put Step( 16,20 ), sprite, Custom, @checkered_blend, @counter'' print a description near each demoDraw String (100, 26), "<- pset"Draw String Step (0, 20), "<- preset"Draw String Step (0, 20), "<- and"Draw String Step (0, 20), "<- or"Draw String Step (0, 20), "<- xor"Draw String Step (0, 20), "<- trans"Draw String Step (0, 20), "<- alpha (uniform)"Draw String Step (0, 20), "<- alpha (per pixel)"Draw String Step (0, 20), "<- add"Draw String Step (0, 20), "<- custom"ImageDestroy( sprite )                          '' free allocated memory for spriteSleep : End 0'' custom blender function: chequered put
Function checkered_blend( ByVal src As ULong, ByVal dest As ULong, ByVal param As Any Ptr ) As ULongDim As Integer Ptr counterDim As ULong pixelcounter = Cast(Integer Ptr, param)pixel = IIf(((*counter And 4) Shr 2) Xor ((*counter And 128) Shr 7), src, dest)*counter += 1Return pixel
End Function

用bload 将当前工作区(screenset n, 0 中的 n 区)的像素存成文件,用bsave 将像素文件装入工作区,用screencopy 将工作区的像素考贝到当前的显示区。

如果将一个位图文件装到一个工作区,在另一个工作工绘图并与位置异或,然后进行显示,往复进行异或显示,就是一幅动态的火焰列马图。

''
'' This fbgfx example deals with:
'' - palette
'' - multiple pages and double buffering
'' - direct access to the screen memory
'' - drawing to GET/PUT buffers
''#define MAX_EXPLOSIONS     32
#define MAX_EXPLOSION_SIZE 100#include "fbgfx.bi"'const FALSE = 0
'const TRUE = (-1)type EXPLOSION_TYPEsprite as ubyte ptrx as integery as integerused as integercount as integer
end typesub animate_fire(byval buffer as ubyte ptr, byval new_ as integer = 0)dim w as integer, h as integer, pitch as integerdim c0 as integer, c1 as integer, c2 as integer, c3 as integerdim header as FB.PUT_HEADER ptrheader = cast(FB.PUT_HEADER ptr, buffer)w = header->widthh = header->heightpitch = header->pitchif new_ thenline buffer, (0, 0)-(w-1, h-1), 0, bffor i as integer = 0 to 5circle buffer, ((w\4)+(rnd*(w\2)), (h\4)+(rnd*(h\2))), (w\6), 191,,,,Fnextelsefor y as integer = 1 to h-2for x as integer = 1 to w-2c0 = buffer[32 + (y * pitch) + x - 1]c1 = buffer[32 + (y * pitch) + x + 1]c2 = buffer[32 + ((y - 1) * pitch) + x]c3 = buffer[32 + ((y + 1) * pitch) + x]c0 = ((c0 + c1 + c2 + c3) \ 4) - rnd*2if (cint(c0) < 0) then c0 = 0buffer[32 + (y * pitch) + x] = c0nextnextend if
end subdim pal(256) as integer, r as integer, g as integer, b as integerdim explosion(MAX_EXPLOSIONS) as EXPLOSION_TYPEdim work_page as integerscreen 14, 8, 3randomize timer'' load image and get palettescreenset 2bload exepath() & "/../fblogo.bmp"palette get using pal'' image uses first 64 colors; since we need colors 0-191, we need to move'' these 64 colors into colors 192-255.screenlockdim as byte ptr pixel = screenptr()for i as integer = 0 to (320*240)-1pixel[i] = 192 + pixel[i]nextscreenunlockfor i as integer = 0 to 63pal(192+i) = pal(i)next'' create fire palettefor i as integer = 0 to 63pal(i) = ipal(64+i) = &h3F or (i shl 8)pal(128+i) = &h3F3F or (i shl 16)nextpalette using pal'' start demoscreenset 1, 0work_page = 1doscreencopy 2, work_pagefor i as integer = 0 to MAX_EXPLOSIONS-1if (explosion(i).used = FALSE) and ((rnd*50) < 1) thendim as integer size = (MAX_EXPLOSION_SIZE\4) + (rnd*((MAX_EXPLOSION_SIZE*3)/4))with explosion(i).sprite = imagecreate( size, size ).x = rnd*320.y = rnd*240.used = TRUE.count = 192end withanimate_fire( explosion(i).sprite, TRUE )end ifif explosion(i).used thenanimate_fire( explosion(i).sprite )put (explosion(i).x, explosion(i).y), explosion(i).sprite, transexplosion(i).count -= 1if explosion(i).count <= 0 thenexplosion(i).used = FALSEend ifend ifnextscreensyncwork_page xor= 1screenset work_page, work_page xor 1loop while inkey = ""

10、 gfx 与 cairo 并用

gfx 绘图方便, 它本身不能显示汉字,对少量的汉字可以做成图直接 put 上去。下面的方法是在 gfx 绘图尾部,调用 cairo 的绘图能力,将文本 “显示中文” show 到需要的位置。

'' examples/manual/gfx/put-or.bas
''
'' Example extracted from the FreeBASIC Manual
'' from topic 'OR'
''
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOrGfx
'' --------
'' Modified: Mongnewer 9, March 2024#INCLUDE ONCE "cairo/cairo.bi"''open a graphics window
ScreenRes 320, 200, 32''create 3 sprites containing red, green and blue circles
Const As Long r = 32
Dim As Any Ptr cr, cg, cb
cr = ImageCreate(r * 2 + 1, r * 2 + 1, RGBA(0, 0, 0, 0))
cg = ImageCreate(r * 2 + 1, r * 2 + 1, RGBA(0, 0, 0, 0))
cb = ImageCreate(r * 2 + 1, r * 2 + 1, RGBA(0, 0, 0, 0))
Circle cr, (r, r), r, RGB(255, 0, 0), , , 1, f
Circle cg, (r, r), r, RGB(0, 255, 0), , , 1, f
Circle cb, (r, r), r, RGB(0, 0, 255), , , 1, f''put the sprite at three different multipier
''levels, overlapping each other in the middle
Put (146 - r, 108 - r), cr, Or
Put (174 - r, 108 - r), cg, Or
Put (160 - r,  84 - r), cb, Or''free the memory used by the sprites
ImageDestroy cr
ImageDestroy cg
ImageDestroy cb''pause the program before closingVAR c_s_t = cairo_image_surface_create_for_data( _SCREENPTR, CAIRO_FORMAT_ARGB32, _320, 200, 320 * 4)VAR crx = cairo_create(c_s_t)cairo_set_source_rgb(crx, 1.0, 1.0, 1.0) '         white backgroundcairo_move_to(crx, 10, 20)  cairo_show_text(crx, "显示中文")Sleepcairo_destroy(crx)  cairo_surface_destroy(c_s_t)End 0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/733902.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++笔记之给枚举类型的变量赋值

C++笔记之给枚举类型的变量赋值 —— 杭州 2024-03-10 code review! 在C++中,你可以在结构体内部定义一个枚举类型,并在创建结构体变量时给枚举类型的变量赋值。下面是一个简单的例子展示了如何做到这一点: 代码 #include <iostream>// 定义结构体 struct MyStru…

如何在Windows系统使用固定tcp公网地址ssh远程Kali系统

文章目录 1. 启动kali ssh 服务2. kali 安装cpolar 内网穿透3. 配置kali ssh公网地址4. 远程连接5. 固定连接SSH公网地址6. SSH固定地址连接测试 简单几步通过[cpolar 内网穿透](cpolar官网-安全的内网穿透工具 | 无需公网ip | 远程访问 | 搭建网站)软件实现ssh 远程连接kali! …

在WSL2中安装多个Ubuntu教程

文章目录 前言一、前期准备1、WSL安装2、Docker安装 二、安装第二个Ubuntu系统1.切换为WSL22.获取Ubuntu16.04的tar文件从容器中导出tar 3. 将tar文件导入WSL4. 设置默认用户 总结 前言 适用于 Linux 的 Windows 子系统 (WSL) 是 Windows 的一项功能&#xff0c;可用于在 Wind…

详解Linux例行性工作

例行性工作&#xff08;计划任务&#xff09; 场景&#xff1a; 生活中&#xff0c;我们有太多场景需要使用到闹钟&#xff0c;比如早上7点起床&#xff0c;下午4点开会&#xff0c;晚上8点购物&#xff0c;等等。再Linux系统里&#xff0c;我们同样也有类似的需求。比如我们…

VS Code搭建windows+远程Linux上Docker的开发环境

在本地windows桌面系统远程Linux上Docker搭建开发环境主要步骤如下&#xff1a; 一、安装vs code和插件 在windows系统上安装vs code&#xff0c;并安装好remote-ssh、dev-container插件&#xff0c;也可以直接安装Remote Development&#xff0c;他会默认把vs code远程的几种…

【硬件基础】STM32F103C8T6芯片引脚定义及功能介绍

1、引脚图片 2、引脚定义图 3、引脚功能介绍 3.1引脚定义图解释 上表中&#xff0c;S表示电源&#xff0c;IO表示输入输出&#xff0c;FT表示容忍电压可达5V&#xff0c;没有FT的只能达3.3V。 VBAT&#xff0c;备用电源引脚&#xff0c;当系统断电时&#xff0c;备用电源可给…

GEE错误——Landsat9数据集进行去云操作后显示白板

问题 我遇到了一些有关 Landsat9 图像中的云遮蔽和图像处理的问题。我正在分享我所使用的代码以及我感兴趣的区域(资产)。请帮我解决这个问题。我是一名 GEE 学习者。问题:最终图像在大面积上有云状覆盖。 这里我们查看了搜索出的代码发现并不是没有数据集导致的,该区域有…

springcloud第3季 consul服务发现注册,配置中心2

一 consul的作用 1.1 为何使用注册中心 为何要用注册中心&#xff1f; 1.A服务调用B服务&#xff0c;使用ip和端口&#xff0c;如果B服务的ip或者端口发生变化&#xff0c;服务A需要进行改动&#xff1b; 2.如果在分布式集群中&#xff0c;部署多个服务B&#xff0c;多个服…

运动想象 (MI) 迁移学习系列 (3) : MSFT

运动想象迁移学习系列:MSFT 0. 引言1. 主要贡献2. 数据增强方法3. 基于度量的空间滤波转换器3.1 空间过滤3.2 脑电图ViT3.2.1 变压器编码器层3.2.2 基于度量的损失函数 4. 实验结果4.1 消融实验4.2 基线任务对比4.3 跨主题 5. 总结欢迎来稿 论文地址&#xff1a;https://www.s…

用C语言执行SQLite3的gcc编译细节

错误信息&#xff1a; /tmp/cc3joSwp.o: In function main: execSqlite.c:(.text0x100): undefined reference to sqlite3_open execSqlite.c:(.text0x16c): undefined reference to sqlite3_exec execSqlite.c:(.text0x174): undefined reference to sqlite3_close execSqlit…

对中国境内所有地区KFC门店基本信息的统计(简略版)

我们要获取每个地区的kfc信息就要先获取中国一共有哪些地区 中国所有城市名称获取 import requests from lxml import etreewith open(f./省份.txt, w) as fp:fp.write() with open(f./城市.txt, w) as fp:fp.write()url1http://www.kfc.com.cn/kfccda/storelist/index.aspx#…

如何基于 esp-at 固件测试 TCP (UART 转 WiFi 透传)吞吐?

测试工具&#xff1a; windows/Ubuntu/Android&#xff08;电脑或手机与 ESP 开发板连接相同路由器&#xff09;iperf2 工具ESP 系列的开发板USB-TTL 串口调试工具路由器 测试固件&#xff1a; AT 固件 AT 固件硬件接线说明 不同环境下的 Iperf 工具安装说明 Iperf 工具用于…

云原生之容器编排实践-ruoyi-cloud项目部署到K8S:Nginx1.25.3

背景 前面搭建好了 Kubernetes 集群与私有镜像仓库&#xff0c;终于要进入服务编排的实践环节了。本系列拿 ruoyi-cloud 项目进行练手&#xff0c;按照 MySQL &#xff0c; Nacos &#xff0c; Redis &#xff0c; Nginx &#xff0c; Gateway &#xff0c; Auth &#xff0c;…

【数学】【组合数学】1830. 使字符串有序的最少操作次数

作者推荐 视频算法专题 本博文涉及知识点 数学 组合数学 LeetCode1830. 使字符串有序的最少操作次数 给你一个字符串 s &#xff08;下标从 0 开始&#xff09;。你需要对 s 执行以下操作直到它变为一个有序字符串&#xff1a; 找到 最大下标 i &#xff0c;使得 1 < i…

Android14之解决报错:No module named selinux(一百九十三)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

数据库 — 增删查改

一、操作数据库、表 显示 show databases;创建 create database xxx;使用 use xxx; 删除 drop database xxx;查看表&#xff1b; show tables; 查看表结构 desc 表名; 创建 create table 表名(字段1 类型1&#xff0c;字段2 类型2&#xff0c;.... ); 删除 drop table 表名; 二…

uniapp小程序获取位置权限(不允许拒绝)

需求 小程序上如果需要一些定位功能&#xff0c;那么我们需要提前获取定位权限。我们页面的所有功能后续都需要在用户同意的前提下进行&#xff0c;所以一旦用户点了拒绝&#xff0c;我们应该给予提示&#xff0c;并让用于修改为允许。 实现 1.打开手机GPS 经过测试发现即使…

【Java网络编程】TCP核心特性(下)

1. 拥塞控制 拥塞控制&#xff1a;是基于滑动窗口机制下的一大特性&#xff0c;与流量控制类似都是用来限制发送方的传送速率的 区别就在于&#xff1a;"流量控制"是从接收方的角度出发&#xff0c;根据接收方剩余接收缓冲区大小来动态调整发送窗口的&#xff1b;而…

深入分析Java线程池——ThreadPoolExecutor

文章目录 Java 线程池概述ThreadPoolExecutor 构造方法线程池拒绝策略工作流程并发库中的线程池CachedThreadPoolFixedThreadPoolSingleThreadExecutorScheduledThreadPool ThreadPoolExecutor 源码分析线程池状态表示获取 runState获取 workerCount生成 ctl 提交任务 execute(…

漫谈技术成长

引言 相信很多程序员在自己的技术成长之路上&#xff0c;总会遇到许许多多的难关&#xff0c;有些难关咬咬牙就过去了&#xff0c;而有点难关则需要有一定的能力&#xff0c;才能克服。因此&#xff0c;本文主要围绕“技术成长” 话题&#xff0c;为何会选择技术方向&#xff0…