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

FreeBASIC SDL图形功能

SDL - Simple DirectMedia Layer 是完整的跨平台系统,有自己的窗口、直接捕获键盘、鼠标和游戏操纵杆的事件,直接操作音频和CDROM,在其surface上可使用gfx, openGL和direct3D绘图。Window3.0时代,各种应用程序在Pharlap、DJPP、4GW支持下均突破了常规内存进入了保护模式,因此那个时期是SDL突破性发展的时机,非常多的游戏程序用SDL做支撑(gtk/iup/libui有的功能sdl没有,而sdl有的能力其它的则没有),是开发游戏和工厂流程化应用绘图的优秀工具。C#, Lua, Rust, hollywood, beaflang, Ocamel, Python, 等众多语言有它的封装, Github上的最新稳定版是 2.30.1 , 三天前还在更新。

Github上各平台使用的SDL

游戏:wildfire 野火

DOSBOX: DOS simulator

Humble Bundle 各种游戏

valve 众多游戏

FreeBASIC 完美支持 SDL, 需要安装如下 .so 库文件(SDL, SDL2 二个版本)

#freebasic SDL
sudo apt install libsdl2-dev
sudo apt install libsdl2-ttf-dev
sudo apt install libsdl2-net-dev
sudo apt install libsdl2-mixer-dev
sudo apt install libsdl2-image-dev
sudo apt install libsdl2-gfx-dev
sudo apt install libsdl1.2-dev
sudo apt install libsdl-console-dev
sudo apt install libsdl-mixer1.2-dev
sudo apt install libsdl-gfx1.2-dev
sudo apt install libsdl-net1.2-dev
sudo apt install libsdl-pango-dev
sudo apt install libsdl-sge-dev
sudo apt install libsdl-sound1.2-dev

示例一:放三个图文件:free.jpg, basic.gif, horse.tga

SDL库可读取的的图形文件种类繁多,此示例是在原示例基础上修改的,读取 gif, tga, jpg三种格式的文件。程序首选获取SDL的版本号,初始化 sdl, 作为三个sdl surface装入图形文件并返回图形指针,然后在不同位置放置它们,最后flip显示它们。

' SDL_image example written by Edmond Leung (leung.edmond@gmail.com)
'
' free.jpg, basic.gif and horse.tga are taken from the official freeBasic
' website.#include  "SDL\SDL.bi"
#include  "SDL\SDL_image.bi"declare sub blitImage _(byval img as SDL_Surface ptr, byval x as integer, byval y as integer)dim shared video as SDL_Surface ptrdim freeImg as SDL_Surface ptr, basicImg as SDL_Surface ptr, horseImg as SDL_Surface ptrdim version as const SDL_version ptrversion = IMG_Linked_Version()' display the version number of the SDL_image being usedprint "Using SDL_image version number: "; SDL_VERSIONNUM(version->major, _version->minor, version->patch)' initialise sdl with video supportif (SDL_Init(SDL_INIT_VIDEO) < 0) thenprint "Couldn't initialise SDL: "; *SDL_GetError()end if' check to see if the images are in the correct formatsif (IMG_isJPG(SDL_RWFromFile("data/free.jpg", "rb")) = 0) thenprint "The image (free.jpg) is not a jpg file."end ifif (IMG_isGIF(SDL_RWFromFile("data/basic.gif", "rb")) = 0) thenprint "The image (basic.gif) is not a gif file."end if' set the video mode to 1024x768x32bppvideo = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE or SDL_DOUBLEBUF)if (video = NULL) thenprint "Couldn't set video mode: "; *SDL_GetError()end if' load the images into an SDL_RWops structuredim freeRw as SDL_RWops ptr, basicRw as SDL_RWops ptrfreeRw = SDL_RWFromFile("data/free.jpg", "rb")basicRw = SDL_RWFromFile("data/basic.gif", "rb")' load the images onto an SDL_Surface using three different functions available' in the SDL_image libraryfreeImg = IMG_LoadJPG_RW(freeRw)horseImg = IMG_Load("data/horse.tga")basicImg = IMG_LoadTyped_RW(basicRw, 1, "gif")dim done as integerdone = 0do while (done = 0)dim event as SDL_Eventdo while (SDL_PollEvent(@event))if (event.type = SDL_QUIT_) then done = 1if (event.type = SDL_KEYDOWN) thenif (event.key.keysym.sym = SDLK_ESCAPE) then done = 1      end ifloopdim destrect as SDL_Rectdestrect.w = video->wdestrect.h = video->h' clear the screen with the colour whiteSDL_FillRect(video, @destrect, SDL_MapRGB(video->format, 255, 255, 255))' draw the images onto the screenblitImage freeImg, 170, 205blitImage horseImg, 345, 330 blitImage freeImg, 445, 335 blitImage basicImg, 450, 360 blitImage freeImg, 650, 215 blitImage basicImg, 650, 240 		blitImage freeImg, 150, 455 blitImage basicImg, 150, 480 		SDL_Flip(video)loopSDL_Quit' sub-routine used to help with blitting the images onto the screen
sub blitImage _(byval img as SDL_Surface ptr, byval x as integer, byval y as integer)dim dest as SDL_Rectdest.x = xdest.y = ySDL_BlitSurface(img, NULL, video, @dest)
end sub

示例二:对网络的支持,访问百度站点并取得首页面前部分内容(对于现代编程,这好像是非常基本的能力)。

''
'' simple http get example using the SDL_net library
''#include once "SDL/SDL_net.bi"const RECVBUFFLEN = 8192
const NEWLINE = !"\r\n"
'const DEFAULT_HOST = "www.freebasic.net"
const DEFAULT_HOST = "www.baidu.com"declare sub gethostandpath( byref src as string, byref hostname as string, byref path as string )'' globalsdim hostname as stringdim path as stringgethostandpath command, hostname, pathif( len( hostname ) = 0 ) thenhostname = DEFAULT_HOSTend if'' initif( SDLNet_Init <> 0 ) thenprint "Error: SDLNet_Init failed"end 1end if'' resolvedim ip as IPAddressdim socket as TCPSocketif( SDLNet_ResolveHost( @ip, hostname, 80 ) <> 0 ) thenprint "Error: SDLNet_ResolveHost failed"end 1end if'' opensocket = SDLNet_TCP_Open( @ip )if( socket = 0 ) thenprint "Error: SDLNet_TCP_Open failed"end 1end if'' send HTTP requestdim sendbuffer as stringsendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _"Host: " + hostname + NEWLINE + _"Connection: close" + NEWLINE + _"User-Agent: GetHTTP 0.0" + NEWLINE + _NEWLINEif( SDLNet_TCP_Send( socket, strptr( sendbuffer ), len( sendbuffer ) ) < len( sendbuffer ) ) thenprint "Error: SDLNet_TCP_Send failed"end 1end if'' receive til connection is closeddim recvbuffer as zstring * RECVBUFFLEN+1dim bytes as integerdo bytes = SDLNet_TCP_Recv( socket, strptr( recvbuffer ), RECVBUFFLEN )if( bytes <= 0 ) thenexit doend if'' add the null-terminatorrecvbuffer[bytes] = 0'' print it as stringprint recvbuffer;loopprint'' close socketSDLNet_TCP_Close( socket )'' quitSDLNet_Quit'':::::
sub gethostandpath( byref src as string, byref hostname as string, byref path as string )dim p as integerp = instr( src, " " )if( p = 0 or p = len( src ) ) thenhostname = trim( src )path = ""elsehostname = trim( left( src, p-1 ) )path = trim( mid( src, p+1 ) )end ifend sub

示例三:SDL 对 gfx 的支持。画任意线条,非常经典的dos年代的一款demo

''
'' simple http get example using the SDL_net library
''#include once "SDL/SDL_net.bi"const RECVBUFFLEN = 8192
const NEWLINE = !"\r\n"
'const DEFAULT_HOST = "www.freebasic.net"
const DEFAULT_HOST = "www.baidu.com"declare sub gethostandpath( byref src as string, byref hostname as string, byref path as string )'' globalsdim hostname as stringdim path as stringgethostandpath command, hostname, pathif( len( hostname ) = 0 ) thenhostname = DEFAULT_HOSTend if'' initif( SDLNet_Init <> 0 ) thenprint "Error: SDLNet_Init failed"end 1end if'' resolvedim ip as IPAddressdim socket as TCPSocketif( SDLNet_ResolveHost( @ip, hostname, 80 ) <> 0 ) thenprint "Error: SDLNet_ResolveHost failed"end 1end if'' opensocket = SDLNet_TCP_Open( @ip )if( socket = 0 ) thenprint "Error: SDLNet_TCP_Open failed"end 1end if'' send HTTP requestdim sendbuffer as stringsendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _"Host: " + hostname + NEWLINE + _"Connection: close" + NEWLINE + _"User-Agent: GetHTTP 0.0" + NEWLINE + _NEWLINEif( SDLNet_TCP_Send( socket, strptr( sendbuffer ), len( sendbuffer ) ) < len( sendbuffer ) ) thenprint "Error: SDLNet_TCP_Send failed"end 1end if'' receive til connection is closeddim recvbuffer as zstring * RECVBUFFLEN+1dim bytes as integerdo bytes = SDLNet_TCP_Recv( socket, strptr( recvbuffer ), RECVBUFFLEN )if( bytes <= 0 ) thenexit doend if'' add the null-terminatorrecvbuffer[bytes] = 0'' print it as stringprint recvbuffer;loopprint'' close socketSDLNet_TCP_Close( socket )'' quitSDLNet_Quit'':::::
sub gethostandpath( byref src as string, byref hostname as string, byref path as string )dim p as integerp = instr( src, " " )if( p = 0 or p = len( src ) ) thenhostname = trim( src )path = ""elsehostname = trim( left( src, p-1 ) )path = trim( mid( src, p+1 ) )end ifend sub

示例四:SDL 对open_GL的支持。简单的三角形, 颜色glColor3f 后面带3个float参数; glVertext3f,  顶点描述后面带3个float参数。

''
'' gltest.bas - freeBASIC opengl example, using GLUT for simplicity
'' by Blitz
''
'' Opengl code ported from nehe's gl tutorials
''#include once "GL/gl.bi"
#include once "GL/glu.bi"
#include once "GL/glut.bi"''
declare sub         doMain           ( )
declare sub         doShutdown		 ( )'''' Entry point''doMain'' ::::::::::::
'' name: doRender
'' desc: Is called by glut to render scene
''
'' ::::::::::::
sub doRender cdeclstatic rtri as singlestatic rqud as singleglClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BITglPushMatrixglLoadIdentityglTranslatef -1.5, 0.0, -6.0glRotatef rtri, 0, 1, 0glBegin GL_TRIANGLESglColor3f   1.0, 0.0, 0.0			'' RedglVertex3f  0.0, 1.0, 0.0			'' Top Of Triangle  Front)glColor3f   0.0, 1.0, 0.0			'' GreenglVertex3f -1.0,-1.0, 1.0			'' Left Of Triangle  Front)glColor3f   0.0, 0.0, 1.0			'' BlueglVertex3f  1.0,-1.0, 1.0			'' Right Of Triangle  Front)glColor3f   1.0, 0.0, 0.0			'' RedglVertex3f  0.0, 1.0, 0.0			'' Top Of Triangle  Right)glColor3f   0.0, 0.0, 1.0			'' BlueglVertex3f  1.0,-1.0, 1.0			'' Left Of Triangle  Right)glColor3f   0.0, 1.0, 0.0			'' GreenglVertex3f  1.0,-1.0,-1.0			'' Right Of Triangle  Right)glColor3f   1.0, 0.0, 0.0			'' RedglVertex3f  0.0, 1.0, 0.0			'' Top Of Triangle  Back)glColor3f   0.0, 1.0, 0.0			'' GreenglVertex3f  1.0,-1.0,-1.0			'' Left Of Triangle  Back)glColor3f   0.0, 0.0, 1.0			'' BlueglVertex3f -1.0,-1.0,-1.0			'' Right Of Triangle  Back)glColor3f   1.0, 0.0, 0.0			'' RedglVertex3f  0.0, 1.0, 0.0			'' Top Of Triangle  Left)glColor3f   0.0, 0.0, 1.0			'' BlueglVertex3f -1.0,-1.0,-1.0			'' Left Of Triangle  Left)glColor3f   0.0, 1.0, 0.0			'' GreenglVertex3f -1.0,-1.0, 1.0			'' Right Of Triangle  Left)glEndglColor3f 0.5, 0.5, 1.0glLoadIdentity    glTranslatef -1.5, 0.0, -6.0glTranslatef 3.0,0.0,0.0	glRotatef rqud, 1.0, 0.0, 0.0glBegin GL_QUADSglVertex3f -1.0, 1.0, 0.0glVertex3f  1.0, 1.0, 0.0glVertex3f  1.0,-1.0, 0.0glVertex3f -1.0,-1.0, 0.0glEnd    glPopMatrix            glutSwapBuffersrtri = rtri + 2.0rqud = rqud + 1.5end sub'' ::::::::::::
'' name: doInput
'' desc: Handles input
''
'' ::::::::::::
sub doInput CDECL ( byval kbcode as unsigned byte, _byval mousex as integer, _byval mousey as integer )if ( kbcode = 27 ) thendoShutdownend 0end ifend sub'' ::::::::::::
'' name: doInitGL
'' desc: Inits OpenGL
''
'' ::::::::::::
sub doInitGLdim i as integerdim lightAmb(3) as singledim lightDif(3) as singledim lightPos(3) as single'''' Rendering stuff''glShadeModel GL_SMOOTHglClearColor 0.0, 0.0, 0.0, 0.5glClearDepth 1.0glEnable GL_DEPTH_TESTglDepthFunc GL_LEQUALglEnable GL_COLOR_MATERIALglHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST'''' Light setup ( not used at the moment )''for i = 0 to 3lightAmb(i) = 0.5lightDif(i) = 1.0lightPos(i) = 0.0next ilightAmb(3) = 1.0lightPos(2) = 2.0lightPos(3) = 1.0    glLightfv GL_LIGHT1, GL_AMBIENT, @lightAmb(0)glLightfv GL_LIGHT1, GL_DIFFUSE, @lightDif(0)glLightfv GL_LIGHT1, GL_POSITION,@lightPos(0)glEnable GL_LIGHT1'''' Blending ( not used at the moment )''glColor4f 1.0, 1.0, 1.0, 0.5glBlendFunc GL_SRC_ALPHA, GL_ONEend sub'' ::::::::::::
'' name: doReshapeGL
'' desc: Reshapes GL window
''
'' ::::::::::::
sub doReshapeGL CDECL ( byval w as integer, _byval h as integer )glViewport 0, 0, w, h glMatrixMode GL_PROJECTIONglLoadIdentityif ( h = 0 ) thengluPerspective  80/2, w, 1.0, 5000.0 elsegluPerspective  80/2, w / h, 1.0, 5000.0end ifglMatrixMode GL_MODELVIEWglLoadIdentityend sub'':::::
sub initGLUT'''' Setup glut''glutInit 1, strptr( " " )    glutInitWindowPosition 0, 0glutInitWindowSize 640, 480glutInitDisplayMode GLUT_RGBA or GLUT_DOUBLE or GLUT_DEPTHglutCreateWindow "FreeBASIC OpenGL example"doInitGLglutDisplayFunc  @doRenderglutIdleFunc     @doRenderglutReshapeFunc  CAST(Any PTR, @doReshapeGL)glutKeyboardFunc CAST(Any PTR, @doInput)end sub'':::::
sub doInit'''' Init GLUT''initGLUT    end sub'':::::
sub shutdownGLUT'' GLUT shutdown will be done automatically by atexit()end sub'':::::
sub doShutdown'''' GLUT''shutdownGLUTend sub'' ::::::::::::
'' name: doMain
'' desc: Main routine
''
'' ::::::::::::
sub doMain'''' ''doInit''''''glutMainLoopend sub

SDL 对鼠标、键盘、joysticker的响应都比较好。以前研究鼠标鉴相时拆过几个滚球鼠标,通过串口可以读取字节形式的位置信息,游戏操纵杆还没拆过,有时间了拆解一个研究一下它的button和位置实现,估计和早期原理图有很多不同。SDL内容太多,先熟悉它的功能和应用领域,没有做更深入的学习,以后用的话还要多补补课。

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

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

相关文章

Jenkins发送邮件、定时执行、持续部署

集成Allure报告只需要配置构建后操作即可。但如果是web自动化&#xff0c;或是用HTMLTestRunner生成报告&#xff0c;构建后操作要选择Publish HTML reports&#xff0c;而构建中还要添加Execute system Groovy script插件&#xff0c;内容&#xff1a; System.setProperty(&q…

如何快速分析OB集群日志,敏捷诊断工具obdiag分析能力实践——《OceanBase诊断系列》之四

1. 前言 obdiag是OceanBase的敏捷诊断工具。1.2版本中&#xff0c;obdiag支持快速收集诊断信息&#xff0c;但仅有收集能力是不够的&#xff0c;还需要有分析能力。因此在obdiag的1.3.0版本中&#xff0c;我们加入了OB集群的日志分析功能。用户可以一键进行集群的OB日志的分析…

运维知识点-Apache HTTP Server

Apache 介绍 介绍 Apache是一个开源的Web服务器软件&#xff0c;全称为Apache HTTP Server&#xff0c;由Apache软件基金会开发和维护。它是目前全球使用最广泛的Web服务器软件之一&#xff0c;占全球所有网络服务器的很大比例。Apache服务器具有跨平台的特性&#xff0c;可以…

软考高级:系统工程生命周期方法(计划驱动方法、渐进迭代式方法等)概念和例子

作者&#xff1a;明明如月学长&#xff0c; CSDN 博客专家&#xff0c;大厂高级 Java 工程师&#xff0c;《性能优化方法论》作者、《解锁大厂思维&#xff1a;剖析《阿里巴巴Java开发手册》》、《再学经典&#xff1a;《Effective Java》独家解析》专栏作者。 热门文章推荐&am…

【常见集合】Java 常见集合重点解析

Java 常见集合重点解析 1. 什么是算法时间复杂度&#xff1f; 时间复杂度表示了算法的 执行时间 和 数据规模 之间的增长关系&#xff1b; 什么是算法的空间复杂度&#xff1f; 表示了算法占用的额外 存储空间 与 数据规模 之间的增长关系&#xff1b; 常见的复杂度&#x…

防火墙配置实验

配置 配置IPSec FW1 FW3 NAT策略 FW1 FW3 安全策略 FW1 FW3 最后测试

数仓实战——京东数据指标体系的构建与实践

目录 一、如何理解指标体系 1.1 指标和指标体系的基本含义 1.2 指标和和标签的区别 1.3 指标体系在数据链路中的位置和作用 1.4 流量指标体系 1.5 指标体系如何向上支撑业务应用 1.6 指标体系背后的数据加工逻辑 二、如何搭建和应用指标体系 2.1 指标体系建设方法—OS…

分布式定时任务调度xxl-job

1. xxl-job基本介绍 1.1 Quartz的体系结构 Quartz中最重要的三个对象:Job&#xff08;作业&#xff09;、Trigger&#xff08;触发器&#xff09;、Scheduler&#xff08;调度器&#xff09;。 xxl-job的调度原理:调度线程在一个while循环中不断地获取一定数量的即将触发的Tr…

AIGC启示录:深度解析AIGC技术的现代性与系统性的奇幻旅程

✨✨ 欢迎大家来访Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭❤&#xff5e;✨✨ &#x1f31f;&#x1f31f; 欢迎各位亲爱的读者&#xff0c;感谢你们抽出宝贵的时间来阅读我的文章。 我是Srlua小谢&#xff0c;在这里我会分享我的知识和经验。&am…

政安晨:【深度学习处理实践】(三)—— 处理时间序列的数据准备

在深度学习中&#xff0c;对时间序列的处理主要涉及到以下几个方面&#xff1a; 序列建模&#xff1a;深度学习可以用于对时间序列进行建模。常用的模型包括循环神经网络&#xff08;Recurrent Neural Networks, RNN&#xff09;和长短期记忆网络&#xff08;Long Short-Term M…

【框架设计】MVC、MVP、MVVM对比图

1. MVC&#xff08;Model-View-Controller&#xff09; 2. MVP&#xff08;Model-View-Presenter&#xff09; 3. MVVM&#xff08;Model-View-ViewModel&#xff09;

ChatGPT Plus 支付出现「您的银行卡被拒绝/your card has been declined」怎么办?

ChatGPT Plus 支付出现「您的银行卡被拒绝/your card has been declined」怎么办&#xff1f; 在订阅 ChatGPT Plus 或者 OpenAI API 时&#xff0c;有时候会出现已下报错 &#xff1a; Your card has been declined. 您的银行卡被拒绝 出现这种错误&#xff0c;有以下几个解…

创邻科技获评环紫金港创新生态圈智源创新企业

3月1日&#xff0c;由杭州城西科创大走廊管理委员会指导&#xff0c;中共杭州市西湖区委员会、西湖区人民政府主办的“环紫金港创新生态圈”行动推进大会暨2024年紫金港科技城经济高质量发展大会在杭州举办。凭借重要的生态位置和创新业务成果&#xff0c;创邻科技受邀参会并被…

瑞芯微 | I2S-音频基础分享

1. 音频常用术语 名称含义ADC&#xff08;Analog to Digit Conversion&#xff09;模拟信号转换为数字信号AEC&#xff08;Acoustic Echo Cancellor&#xff09;回声消除AGC&#xff08;Automatic Gain Control&#xff09;自动增益补偿&#xff0c;调整MIC收音量ALSA&#xf…

深入探索Transformer时代下的NLP革新

《基于GPT-3、ChatGPT、GPT-4等Transformer架构的自然语言处理》主要聚焦于如何使用Python编程语言以及深度学习框架如PyTorch和TensorFlow来构建、训练和调整用于自然语言处理任务的深度神经网络架构&#xff0c;特别是以Transformer为核心模型的架构。 书中详细介绍了Transf…

07.axios封装实例

一.简易axios封装-获取省份列表 1. 需求&#xff1a;基于 Promise 和 XHR 封装 myAxios 函数&#xff0c;获取省份列表展示到页面 2. 核心语法&#xff1a; function myAxios(config) {return new Promise((resolve, reject) > {// XHR 请求// 调用成功/失败的处理程序}) …

【嵌入式高级C语言】9:万能型链表懒人手册

文章目录 序言单向不循环链表拼图框架搭建 - Necessary功能拼图块1 创建链表头信息结构体 - Necessary2 链表头部插入 - Optional3 链表的遍历 - Optional4 链表的销毁 - Necessary5 链表头信息结构体销毁 - Necessary6 获取链表中节点的个数 - Optional7 链表尾部插入 - Optio…

git克隆过程报错

设置 git config 来强制 git 使用 HTTP 1.1 git config --global http.version HTTP/1.1想将其设置回 HTTP2&#xff0c;你可以这样做 git config --global http.version HTTP/2

飞驰云联CEO朱旭光荣获“科技领军人才”称号

2024年2月29日&#xff0c;苏州工业园区“优化营商环境暨作风效能建设大会”成功举办&#xff0c;会上公布了2023年度苏州工业园区第十七届第一批金鸡湖科技领军人才名单&#xff0c;Ftrans飞驰云联创始人兼CEO朱旭光先生凭借在数据安全以及文件交换领域取得的突出成果&#xf…

Feign实现微服务间远程调用续;基于Redis实现消息队列用于延迟任务的处理,Redis分布式锁的实现;(黑马头条Day05)

目录 延迟任务和定时任务 使用Redis设计延迟队列原理 点评项目中选用list和zset两种数据结构进行实现 如何缓解Redis内存的压力同时保证Redis中任务能够被正确消费不丢失 系统流程设计 使用Feign实现微服务间的任务消费以及文章自动审核 系统微服务功能介绍 提交文章-&g…