Main Menu

Function Library Template

Started by kevin, January 28, 2015, 08:16:10 AM

Previous topic - Next topic

kevin

Function Library Template

    This is NOT a program, rather it's a basic library template project that you can use to create function libraries for your PlayBASIC programs.  


SETUP

   To set this code up all you do is customize it.   So you'd either rename the file "MyFunctionLibrary.pba" to whatever you want to call your library,or copy that file into your existing project and rename it.
   
   Next open the library up and rename the MYFUNCTIONLIBRARY to whatever you want to call your library.   The MYFUNCTIONLIBRARY tag is used throughout  the library code as unique keyword, so a relace will initialize them all. (NO SPACES)  
   
   Write or copy your functions between the [LIBRARY FUNCTIONS START]  and [LIBRARY FUNCTIONS END] comment blocks.  
   
   
   
 
WHAT IS A FUNCTION LIBRARY

    The library in this context is meant to be an umbrella term, for a group of functions / psubs that either all act upon a common data set, or have a similar purpose.   For example, if you have a set of custom string functions you could pull them all into library than just reused them between projects.  That's what I do, and you should too !
   

WHY MAKE A LIBRARY  

    They reduce development time and new projects complexity, as ideally that allow you to reuse work you or others have done in the past when  building new programs.
   

kevin

#1
  Memory Image Library V0.01

      This short example library show programmers a way of managing virtual image data in your own custom command set.   The library use a central list to keep the images data within and includes a few simple drawing functions to render stuff onto them.
 
      The point of library is not really what it does, but to shows you a way to build separate stand alone libraries that will plug into your   programs..  


PlayBASIC Code: [Select]
/*
*=---------------------------------------------------------------------=*

>> LIBRARY TEST CODE GOES HERE <<

*=---------------------------------------------------------------------=*

Here we're testing our function library.

*/


; clear the PB screen
Cls $304050


; create a memory image resource that's 600 * 200 pixels in size
MyImage1 =newmemoryimage(600,200)


; draw some colours boxes to the surface
Size=20
for ylp=0 to GetMemoryImageHeight(MyImage1)-1 step Size
for xlp=0 to GetMemoryImageWidth(MyImage1)-1 step Size
MI_BOX(xlp,ylp,xlp+size,ylp+size,rndrgb())
next
next


; draw some green dots accross and down surface from top to bottom
for lp=0 to 1000
MI_DOT(lp,lp,$00ff00)
next


; draw sine wave of dots of white dots across this surface
for lp=0 to GetMemoryImageWidth(MyImage1)
y#=GetMemoryImageHeight(MyImage1)/2
Y#+=sinradius(lp,y#)
MI_DOT(lp,Y#,$FFFFFF)
next

; render our memory image to the current PlayBASIC surface so we can see it
DrawMemoryImage(MyImage1)

; get some info back about this memory image and draw it to the screen
print GetMemoryImageStatus(MyImage1)
print GetMemoryImageWidth(MyImage1)
print GetMemoryImageHeight(MyImage1)


sync
waitkey





   This library was built using the library templates provided above, the actual functions show a way to manage multiple resources from a single interface.  In this case it's basically an emulation of the PlayBASIC drawing routines, written in PlayBASIC. 

  To build a DLL library all you would is prefix the command set functions with "DLL_" and when you're written your library, you then pass the library to PlayBASIC2DLL.  Personally I only ever bother building a DLL version when a library is ready for use.  There's really no point doing it every time you make a change !

  For big projects, the structure i generally use, is to host my DLL library project (just like the one attached) inside my projects folder.    Generally calling this folder DLL.  Then from my main project you either just include the "MyLibrary" Linkdll's statements or cut'n'paste them in..