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 ptr
dim freeImg as SDL_Surface ptr, basicImg as SDL_Surface ptr, horseImg as SDL_Surface ptr
dim version as const SDL_version ptr
version = IMG_Linked_Version()
' display the version number of the SDL_image being used
print "Using SDL_image version number: "; SDL_VERSIONNUM(version->major, _
version->minor, version->patch)
' initialise sdl with video support
if (SDL_Init(SDL_INIT_VIDEO) < 0) then
print "Couldn't initialise SDL: "; *SDL_GetError()
end if
' check to see if the images are in the correct formats
if (IMG_isJPG(SDL_RWFromFile("data/free.jpg", "rb")) = 0) then
print "The image (free.jpg) is not a jpg file."
end if
if (IMG_isGIF(SDL_RWFromFile("data/basic.gif", "rb")) = 0) then
print "The image (basic.gif) is not a gif file."
end if
' set the video mode to 1024x768x32bpp
video = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE or SDL_DOUBLEBUF)
if (video = NULL) then
print "Couldn't set video mode: "; *SDL_GetError()
end if
' load the images into an SDL_RWops structure
dim freeRw as SDL_RWops ptr, basicRw as SDL_RWops ptr
freeRw = 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 library
freeImg = IMG_LoadJPG_RW(freeRw)
horseImg = IMG_Load("data/horse.tga")
basicImg = IMG_LoadTyped_RW(basicRw, 1, "gif")
dim done as integer
done = 0
do while (done = 0)
dim event as SDL_Event
do while (SDL_PollEvent(@event))
if (event.type = SDL_QUIT_) then done = 1
if (event.type = SDL_KEYDOWN) then
if (event.key.keysym.sym = SDLK_ESCAPE) then done = 1
end if
loop
dim destrect as SDL_Rect
destrect.w = video->w
destrect.h = video->h
' clear the screen with the colour white
SDL_FillRect(video, @destrect, SDL_MapRGB(video->format, 255, 255, 255))
' draw the images onto the screen
blitImage freeImg, 170, 205
blitImage 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)
loop
SDL_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_Rect
dest.x = x
dest.y = y
SDL_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 )
'' globals
dim hostname as string
dim path as string
gethostandpath command, hostname, path
if( len( hostname ) = 0 ) then
hostname = DEFAULT_HOST
end if
'' init
if( SDLNet_Init <> 0 ) then
print "Error: SDLNet_Init failed"
end 1
end if
'' resolve
dim ip as IPAddress
dim socket as TCPSocket
if( SDLNet_ResolveHost( @ip, hostname, 80 ) <> 0 ) then
print "Error: SDLNet_ResolveHost failed"
end 1
end if
'' open
socket = SDLNet_TCP_Open( @ip )
if( socket = 0 ) then
print "Error: SDLNet_TCP_Open failed"
end 1
end if
'' send HTTP request
dim sendbuffer as string
sendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _
"Host: " + hostname + NEWLINE + _
"Connection: close" + NEWLINE + _
"User-Agent: GetHTTP 0.0" + NEWLINE + _
NEWLINE
if( SDLNet_TCP_Send( socket, strptr( sendbuffer ), len( sendbuffer ) ) < len( sendbuffer ) ) then
print "Error: SDLNet_TCP_Send failed"
end 1
end if
'' receive til connection is closed
dim recvbuffer as zstring * RECVBUFFLEN+1
dim bytes as integer
do
bytes = SDLNet_TCP_Recv( socket, strptr( recvbuffer ), RECVBUFFLEN )
if( bytes <= 0 ) then
exit do
end if
'' add the null-terminator
recvbuffer[bytes] = 0
'' print it as string
print recvbuffer;
loop
print
'' close socket
SDLNet_TCP_Close( socket )
'' quit
SDLNet_Quit
'':::::
sub gethostandpath( byref src as string, byref hostname as string, byref path as string )
dim p as integer
p = instr( src, " " )
if( p = 0 or p = len( src ) ) then
hostname = trim( src )
path = ""
else
hostname = trim( left( src, p-1 ) )
path = trim( mid( src, p+1 ) )
end if
end 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 )
'' globals
dim hostname as string
dim path as string
gethostandpath command, hostname, path
if( len( hostname ) = 0 ) then
hostname = DEFAULT_HOST
end if
'' init
if( SDLNet_Init <> 0 ) then
print "Error: SDLNet_Init failed"
end 1
end if
'' resolve
dim ip as IPAddress
dim socket as TCPSocket
if( SDLNet_ResolveHost( @ip, hostname, 80 ) <> 0 ) then
print "Error: SDLNet_ResolveHost failed"
end 1
end if
'' open
socket = SDLNet_TCP_Open( @ip )
if( socket = 0 ) then
print "Error: SDLNet_TCP_Open failed"
end 1
end if
'' send HTTP request
dim sendbuffer as string
sendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _
"Host: " + hostname + NEWLINE + _
"Connection: close" + NEWLINE + _
"User-Agent: GetHTTP 0.0" + NEWLINE + _
NEWLINE
if( SDLNet_TCP_Send( socket, strptr( sendbuffer ), len( sendbuffer ) ) < len( sendbuffer ) ) then
print "Error: SDLNet_TCP_Send failed"
end 1
end if
'' receive til connection is closed
dim recvbuffer as zstring * RECVBUFFLEN+1
dim bytes as integer
do
bytes = SDLNet_TCP_Recv( socket, strptr( recvbuffer ), RECVBUFFLEN )
if( bytes <= 0 ) then
exit do
end if
'' add the null-terminator
recvbuffer[bytes] = 0
'' print it as string
print recvbuffer;
loop
print
'' close socket
SDLNet_TCP_Close( socket )
'' quit
SDLNet_Quit
'':::::
sub gethostandpath( byref src as string, byref hostname as string, byref path as string )
dim p as integer
p = instr( src, " " )
if( p = 0 or p = len( src ) ) then
hostname = trim( src )
path = ""
else
hostname = trim( left( src, p-1 ) )
path = trim( mid( src, p+1 ) )
end if
end 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 cdecl
static rtri as single
static rqud as single
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
glPushMatrix
glLoadIdentity
glTranslatef -1.5, 0.0, -6.0
glRotatef rtri, 0, 1, 0
glBegin GL_TRIANGLES
glColor3f 1.0, 0.0, 0.0 '' Red
glVertex3f 0.0, 1.0, 0.0 '' Top Of Triangle Front)
glColor3f 0.0, 1.0, 0.0 '' Green
glVertex3f -1.0,-1.0, 1.0 '' Left Of Triangle Front)
glColor3f 0.0, 0.0, 1.0 '' Blue
glVertex3f 1.0,-1.0, 1.0 '' Right Of Triangle Front)
glColor3f 1.0, 0.0, 0.0 '' Red
glVertex3f 0.0, 1.0, 0.0 '' Top Of Triangle Right)
glColor3f 0.0, 0.0, 1.0 '' Blue
glVertex3f 1.0,-1.0, 1.0 '' Left Of Triangle Right)
glColor3f 0.0, 1.0, 0.0 '' Green
glVertex3f 1.0,-1.0,-1.0 '' Right Of Triangle Right)
glColor3f 1.0, 0.0, 0.0 '' Red
glVertex3f 0.0, 1.0, 0.0 '' Top Of Triangle Back)
glColor3f 0.0, 1.0, 0.0 '' Green
glVertex3f 1.0,-1.0,-1.0 '' Left Of Triangle Back)
glColor3f 0.0, 0.0, 1.0 '' Blue
glVertex3f -1.0,-1.0,-1.0 '' Right Of Triangle Back)
glColor3f 1.0, 0.0, 0.0 '' Red
glVertex3f 0.0, 1.0, 0.0 '' Top Of Triangle Left)
glColor3f 0.0, 0.0, 1.0 '' Blue
glVertex3f -1.0,-1.0,-1.0 '' Left Of Triangle Left)
glColor3f 0.0, 1.0, 0.0 '' Green
glVertex3f -1.0,-1.0, 1.0 '' Right Of Triangle Left)
glEnd
glColor3f 0.5, 0.5, 1.0
glLoadIdentity
glTranslatef -1.5, 0.0, -6.0
glTranslatef 3.0,0.0,0.0
glRotatef rqud, 1.0, 0.0, 0.0
glBegin GL_QUADS
glVertex3f -1.0, 1.0, 0.0
glVertex3f 1.0, 1.0, 0.0
glVertex3f 1.0,-1.0, 0.0
glVertex3f -1.0,-1.0, 0.0
glEnd
glPopMatrix
glutSwapBuffers
rtri = rtri + 2.0
rqud = rqud + 1.5
end 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 ) then
doShutdown
end 0
end if
end sub
'' ::::::::::::
'' name: doInitGL
'' desc: Inits OpenGL
''
'' ::::::::::::
sub doInitGL
dim i as integer
dim lightAmb(3) as single
dim lightDif(3) as single
dim lightPos(3) as single
''
'' Rendering stuff
''
glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 0.5
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glEnable GL_COLOR_MATERIAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
''
'' Light setup ( not used at the moment )
''
for i = 0 to 3
lightAmb(i) = 0.5
lightDif(i) = 1.0
lightPos(i) = 0.0
next i
lightAmb(3) = 1.0
lightPos(2) = 2.0
lightPos(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.5
glBlendFunc GL_SRC_ALPHA, GL_ONE
end 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_PROJECTION
glLoadIdentity
if ( h = 0 ) then
gluPerspective 80/2, w, 1.0, 5000.0
else
gluPerspective 80/2, w / h, 1.0, 5000.0
end if
glMatrixMode GL_MODELVIEW
glLoadIdentity
end sub
'':::::
sub initGLUT
''
'' Setup glut
''
glutInit 1, strptr( " " )
glutInitWindowPosition 0, 0
glutInitWindowSize 640, 480
glutInitDisplayMode GLUT_RGBA or GLUT_DOUBLE or GLUT_DEPTH
glutCreateWindow "FreeBASIC OpenGL example"
doInitGL
glutDisplayFunc @doRender
glutIdleFunc @doRender
glutReshapeFunc 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
''
shutdownGLUT
end sub
'' ::::::::::::
'' name: doMain
'' desc: Main routine
''
'' ::::::::::::
sub doMain
''
''
''
doInit
''
''
''
glutMainLoop
end sub
SDL 对鼠标、键盘、joysticker的响应都比较好。以前研究鼠标鉴相时拆过几个滚球鼠标,通过串口可以读取字节形式的位置信息,游戏操纵杆还没拆过,有时间了拆解一个研究一下它的button和位置实现,估计和早期原理图有很多不同。SDL内容太多,先熟悉它的功能和应用领域,没有做更深入的学习,以后用的话还要多补补课。