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.mzph.cn/news/731055.shtml

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

相关文章

Webpack常用配置及作用

一 、 二、 三、 四、 五、 六、 七、 八、

防御保护--IPSEC VPPN实验

实验拓扑图 实验背景&#xff1a;FW1和FW2是双机热备的状态。 实验要求&#xff1a;在FW5和FW3之间建立一条IPSEC通道&#xff0c;保证10.0.2.0/24网段可以正常访问到192.168.1.0/24 IPSEC VPPN实验配置&#xff08;由于是双机热备状态&#xff0c;所以FW1和FW2只需要配置FW1…

LLM 构建Data Muti-Agents 赋能数据分析平台的实践之①:数据采集

一、 概述 在推进产业数字化的过程中&#xff0c;数据作为最重要的资源是优化产业管控过程和提升产业数字化水平的基础一环&#xff0c;如何实现数据采集工作的便利化、高效化、智能化是降低数据分析体系运转成本以及推动数据价值挖掘体系的基础手段。随着数字化在产业端的推进…

UDP实现文件的发送、UDP实现全双工的聊天、TCP通信协议

我要成为嵌入式高手之3月7日Linux高编第十七天&#xff01;&#xff01; ———————————————————————————— 回顾 重要程序 1、UDP实现文件的发送 发端&#xff1a; #include "head.h"int main(void) {int sockfd 0;struct sockaddr_i…

LeetCode-102.题: 二叉树的层序遍历(原创)

【题目描述】 给你二叉树的根节点 root &#xff0c;返回其节点值的 层序遍历 。 &#xff08;即逐层地&#xff0c;从左到右访问所有节点&#xff09;。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;[[3],[9,20],[15,7]] 【题目链接…

Apache服务的搭建与配置

一、apache安装 systemctl stop firewalldsystemctl disable firewalldsetenforce 0yum -y install httpdsystemctl start httpdnetstat -ntlp | grep 80 二、认识主配置文件 # vim /etc/httpd/conf/httpd.conf ServerRoot "/etc/httpd" #定义工作目…

【CSP试题回顾】202109-1-数组推导

CSP-202109-1-数组推导 解题代码 #include<iostream> #include<vector> #include<algorithm> using namespace std;long long maxSum, minSum;int main() { int n;cin >> n;vector<int>B(n);for (auto& it : B){cin >> it;maxSum …

HNU-计算机网络-甘晴void学习感悟

前言 计算机网络其实我没太学懂&#xff0c; 仅从应试来说&#xff0c;考试成绩也不太好。 这也是为什么一直没有更新这一学科的学习感悟。 大三下还是有点闲&#xff0c;一周三天小长假&#xff0c;闲来无事还是给写了。 教材使用这本&#xff1a; 总领 期中考试 30% 期…

Vue 项目性能优化指南:提升应用速度与效率

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

基于ACM32 MCU的胎压监测仪方案介绍,提高驾驶安全性

胎压监测仪设计方案 基于ACM32F070的胎压监测仪是一种智能化的汽车安全装置&#xff0c;它可以实时监测汽车轮胎的胎压&#xff0c;提高驾驶安全性&#xff0c;降低事故风险。该方案采用ACM32F070微控制器作为核心控制芯片&#xff0c;结合压力传感器、无线通信模块等外围设备&…

Javaweb day13 day14 day15

spring boot 快速入门 写法 http协议 请求协议 响应协议 协议解析 Tomcat

学习人工智能:吴恩达《AI for everyone》2019 第4周:歧视,攻击,发展中国家,就业

吴恩达 Andrew Ng&#xff0c; 斯坦福大学前教授&#xff0c;Google Brain项目发起人、领导者。 Coursera 的联合创始人和联合主席&#xff0c;在 Coursera 上有十万用户的《机器学习》课程&#xff1b;斯坦福大学计算机科学前教授。百度前副总裁、前首席科学家&#xff1b;谷…

机器学习开源分子生成系列(1)-DeepFrag的本地部署及使用

欢迎浏览我的CSND博客&#xff01; Blockbuater_drug …进入 文章目录 前言一、DeepFrag是什么&#xff1f;二、conda中安装DeepFrag CLI环境1. 创建环境并激活2. 下载pre-trained model3. DeepFrag CLI 使用方法必需参数&#xff1a;可选参数&#xff1a; 4. DeepFrag CLI 使用…

了解开源可视化表单的主要优势

为什么可视化表单深受大家喜爱&#xff1f;这就需要了解开源可视化表单的优势和特点了。在流程化办公深入人心的今天&#xff0c;提高办公协作效率早已成为大家的发展目标&#xff0c;低代码技术平台、开源可视化表单是提升办公协作效率的得力助手&#xff0c;一起来看看它的优…

windows关闭copilot预览版

如果用户不想在windows系统当中启用Copilot&#xff0c;可以通过以下三种方式禁用。 第一种&#xff1a;隐藏Copilot 按钮 右键点击任务栏&#xff0c;取消勾选“显示 Copilot&#xff08;预览版&#xff09;按钮”&#xff0c;任务栏则不再显示&#xff0c;用户可以通过快捷键…

换手机后日记不见了怎么恢复?换手机日记内容同步方法

曾经&#xff0c;我使用的是一款苹果手机&#xff0c;这部手机陪伴了我整整3年。随着时间的推移&#xff0c;手机内存不够用成为了我面临的一个大问题&#xff0c;因此我决定更换一部新手机——这次我选择了OPPO品牌。在更换手机的过程中&#xff0c;我利用手机搬家软件一键同步…

Leetcode3070. 元素和小于等于 k 的子矩阵的数目

Every day a Leetcode 题目来源&#xff1a;3070. 元素和小于等于 k 的子矩阵的数目 解法1&#xff1a;二维前缀和 二维前缀和的模板题。 代码&#xff1a; /** lc appleetcode.cn id3070 langcpp** [3070] 元素和小于等于 k 的子矩阵的数目*/// lc codestart// 二维前缀和…

Python编程实验六:面向对象应用

目录 一、实验目的与要求 二、实验内容 三、主要程序清单和程序运行结果 第1题 第2题 四、实验结果分析与体会 一、实验目的与要求 &#xff08;1&#xff09;通过本次实验&#xff0c;学生应掌握类的定义与对象的创建、类的继承与方法的覆盖&#xff1b; &#xff08;2…

守护Web安全:了解Web攻击与防护策略

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

MKS真空规740B-742B-840B-852B-R700说明详细内容查看目录和图片目录

MKS真空规740B-742B-840B-852B-R700说明详细内容查看目录和图片目录