|
QBXL Technical Articles
Advanced Libraries in FreeBASIC By SJ Zero |
|
FreeBASICs
greatest strength is it's ability to seamlessly integrate with
a number of standard C libraries while maintaining the ease of use that
is QB. Even before FB had a built-in graphics library, intrepid coders
were using SDL to get graphics and sound routines working. Before the
current version included a SDL_NET and Winsock, a number of coders,
myself included, fought with the headers to get network support into
FreeBASIC. Today, I'm just going to cover how to get started with three
advanced libraries: SDL, fmod, and tinyPTC. After understanding the
fundamentals, you'll see that using C libraries is simple enough that
with few exceptions, C libraries are no more difficult to use in
FreeBASIC than QB libraries were to use.
These libs are particularly useful because they tend to
provide functions for games. SDL is a library with Graphics and Input support built in, and
a bunch of sublibraries for network, true type font support, and other
things. It can be used with OpenGL, but I won't be covering that today. TinyPTC is primarily a graphics lib, the simplest one
available. It does little more than give you a pointer to the graphics
reigon to fire at. Fmod is a 3d sound and music library. Though it's license is
strange, it works acceptably for playing sounds, and it nicely
encapsulates 3d sound. 1.
Including the library For SDL, it's simply '$INCLUDE: "SDL\SDL.bi" For FMOD, it's '$Include: 'fmod.bi' and for tinyPTC, you'll want '$INCLUDE:
'tinyptc.bi' 2. Initilizing the library, loading a file To initilize SDL and load a bitmap into memory, you must: CONST SCR_WIDTH = 640CONST SCR_HEIGHT = 480 DIM bitmap AS SDL_Surface ptr 'our bitmap DIM video AS SDL_Surface ptr 'our screen surface SDL_Init ( SDL_INIT_VIDEO ) video = SDL_SetVideoMode( SCR_WIDTH, SCR_HEIGHT, 32, 0 ) 'sets the video mode for 640x480x32 MenuScreen = SDL_LoadBMP("bitmap.bmp") To initilize FMOD and load a sound into
memory, you must: Finally, there's no data formats to load
with tinyPTC because it's so simple, but you initilize it by going: const
SCR_WIDTH = 320 3. Blitting, playing, or plotting SUB
BlitImage(x as integer,y as integer,image as sdl_surface ptr) For FMOD, the steps to play a sound aren't
that difficult either: FUNCTION
fModPlayWave( samp1 as integer ) AS INTEGER And tinyPTC, which is again, not a high
level lib like the other two, can plot pixels using the following code: SUB putd(BYREF buffer(), BYVAL x AS INTEGER, BYVAL y AS INTEGER, BYVAL colr as INTEGER) 4. Shutting down So you don't have to manage menory and do all the boring
mundane tasks, you must remember to shut down the library before your
program exits. Luckily, all three programs allow this with one line. If
you can't shut it down, the library no longer cares. It's beautiful. SDL: SDL_Quit () That's all there is to quitting! As you can see, there is nothing inherently more difficult in
using libraries in FreeBASIC compared to QuickBASIC. In fact, because
coders don't need to jump through hoops to get to memory, it's actually
much easier, even with the more modern OS and hardware. |
||
|
|||