Here's another tutorial for own gadgets, much in line with the other one. I'm bad with names, so I called this draw-object a M33P :)
Run the code below, and notice that when you put the whole lot of functions in includefiles, you don't need anything else to know. No declarations, no global variables (thank heavens!), no nothing. Just create it, use it, and release it when done.
In a nutshell the basics of this method:
* All variables, images, gadgets etc. go into banks.
* There is a 'create' function
* There is an 'update' function
* There is an 'event' function
* There is a 'free' function
* It's recommended to have banks in banks to allow for expansion without messing-up the bank-structure
* It's recommended to have special gadgetbanks, imagebanks etc.
* Images, gadgets, banks etc. are all 1 int
* Objects like these can be created and free'd locally
One might argue to use consts for the adresses.. that's up to you.. I'm comfy without them..
m33p ^_^
Run the code below, and notice that when you put the whole lot of functions in includefiles, you don't need anything else to know. No declarations, no global variables (thank heavens!), no nothing. Just create it, use it, and release it when done.
In a nutshell the basics of this method:
* All variables, images, gadgets etc. go into banks.
* There is a 'create' function
* There is an 'update' function
* There is an 'event' function
* There is a 'free' function
* It's recommended to have banks in banks to allow for expansion without messing-up the bank-structure
* It's recommended to have special gadgetbanks, imagebanks etc.
* Images, gadgets, banks etc. are all 1 int
* Objects like these can be created and free'd locally
One might argue to use consts for the adresses.. that's up to you.. I'm comfy without them..
; Bank tutorial, by CS_TBL ; ; purpose: Demonstrating how to create programs using banks for variable-storage ; app=CreateWindow("Bank tutorial 'M33P' - By CS_TBL",0,0,640,480) Global quit ; let's create some m33ps .. m33p1=CreateM33P(32,32,256,128,app) ; x,y,w,h,parent m33p2=CreateM33P(32,200,192,96,app) ; x,y,w,h,parent m33p3=CreateM33P(300,32,320,256,app) ; x,y,w,h,parent m33p4=CreateM33P(332,320,80,80,app) ; x,y,w,h,parent Repeat Delay 2 WaitEvent() If EventID()=$803 quit=True evM33P(m33p1) evM33P(m33p2) evM33P(m33p3) evM33P(m33p4) Until quit FreeM33P(m33p1) FreeM33P(m33p2) FreeM33P(m33p3) FreeM33P(m33p4) End Function evM33P(bank) ; security.. If Not bank Break "No bank given in evM33P()" ; first extract what we need first.. if the mouse is not on the canvas, there's no need to extract the rest yet panel=PeekInt(bank,0) gadgets=PeekInt(bank,4) canvas=PeekInt(gadgets,0) button=PeekInt(gadgets,8) images=PeekInt(bank,8) image=PeekInt(images,0) If EventSource()=canvas ; extract more variables titlecanvas=PeekInt(gadgets,4) r=PeekByte(bank,12) g=PeekByte(bank,13) b=PeekByte(bank,14) drawR=PeekByte(bank,21) drawG=PeekByte(bank,22) drawB=PeekByte(bank,23) w=GadgetWidth(canvas) h=GadgetHeight(canvas) LMD=PeekByte(bank,16) If EventID()=$205 ; canvasenter PokeByte bank,15,1 updateM33P(bank) EndIf If EventID()=$203 ; canvasmove PokeByte bank,15,1 updateM33P(bank) EndIf If EventID()=$206 ; canvasleave PokeByte bank,15,0 updateM33P(bank) EndIf If EventID()=$201 If EventData()=1 LMD=1 oldx=EventX():PokeShort bank,17,oldx oldy=EventY():PokeShort bank,19,oldy PokeByte bank,16,LMD EndIf EndIf If EventID()=$202 If EventData()=1 LMD=0 PokeByte bank,16,LMD EndIf EndIf If LMD x=EventX() y=EventY() If x<0 x=0 ; clip to canvas If x>w-1 x=w-1 ; If y<0 y=0 ; If y>h-1 y=h-1 ; oldx=PeekShort(bank,17) oldy=PeekShort(bank,19) SetBuffer ImageBuffer(image) Color drawR,drawG,drawB Line oldx,oldy,x,y SetBuffer DesktopBuffer() PokeShort bank,17,x PokeShort bank,19,y EndIf EndIf If EventSource()=button SetBuffer ImageBuffer(image) Cls SetBuffer DesktopBuffer() updateM33P(bank) ActivateGadget panel EndIf colorcanvas=PeekInt(gadgets,12) If EventSource()=colorcanvas If EventID()=$201 drawR=PeekByte(bank,21) drawG=PeekByte(bank,22) drawB=PeekByte(bank,23) RequestColor(drawR,drawG,drawB) PokeByte bank,21,RequestedRed() PokeByte bank,22,RequestedGreen() PokeByte bank,23,RequestedBlue() updateM33P(bank) EndIf EndIf End Function Function CreateM33P(x,y,w,h,parent) ; some security here first: If Not parent Break "No parent given in CreateM33P()" If w<64 w=64 If h<40 h=40 ; now we have proper dimensions for our panel ; We make a description of the bank here: ; --------------------------------------- ; adres size description ; ; 00 04 panel ; 04 04 gadgetbank (seperate bank, so it can grow with gadgets without messing up this M33P-bank) ; 08 04 imagebank (I just decided we need various images :) ; 12 01 title R ; 13 01 title G ; 14 01 title B ; 15 01 active ; 16 01 LMD (required for drawing) ; 17 02 oldx ; 19 02 oldy ; 21 01 draw R ; 22 01 draw G ; 23 01 draw B ; for the moment our bank has a size of 24 (adresses 0..23 when reading bytes) ; I could've chosen to put the panel inside the gadgetsbank, I didn't .. you can .., I figured I always need a ; panel anyway, and the seperate gadgetbank is here solely so it can expanded without ruining the m33p-bank.. bank=CreateBank(24) panel=CreatePanel(x,y,w,h,parent): PokeInt bank,0,panel images=CreateBank(4) : PokeInt bank,8,images PokeByte bank,12,160 ; title R PokeByte bank,13,160 ; title G PokeByte bank,14,255 ; title B PokeByte bank,15,0 ; active PokeByte bank,21,255 ; draw R PokeByte bank,22,255 ; draw G PokeByte bank,23,255 ; draw B ; let's create a new list, for the gadgets. ; --------------------------------------- ; adres size description ; ; 00 04 canvas ; 04 08 canvas (title) ; 08 04 clearbutton ; 12 04 colorcanvas ; so.. a size of 16 gadgets=CreateBank(16): PokeInt bank,4,gadgets PokeInt gadgets, 0,CreateCanvas(0,16,w-32,h-16,panel) PokeInt gadgets, 4,CreateCanvas(0,0,w-32,16,panel) PokeInt gadgets, 8,CreateButton("CLS",w-32,0,32,24,panel) PokeInt gadgets,12,CreateCanvas(2+w-32,2+24,32-4,24-4,panel) ; let's create 1 image (ok ok, so I decided M33P is a mini-drawtool :) PokeInt images,0,CreateImage(w-32,h-16) updateM33P(bank) ; draw it for the very first time! Return bank End Function Function updateM33P(bank) ; security.. If Not bank Break "No bank given in updateM33P()" ; extract the variables we need.. gadgets=PeekInt(bank,4) canvas=PeekInt(gadgets,0) colorcanvas=PeekInt(gadgets,12) titlecanvas=PeekInt(gadgets,4) r=PeekByte(bank,12) g=PeekByte(bank,13) b=PeekByte(bank,14) drawR=PeekByte(bank,21) drawG=PeekByte(bank,22) drawB=PeekByte(bank,23) active=PeekByte(bank,15) w=GadgetWidth(canvas) h=GadgetHeight(canvas) images=PeekInt(bank,8) image=PeekInt(images,0) SetBuffer CanvasBuffer(titlecanvas) If active ClsColor r,g,b Else ClsColor r/2,g/2,b/2 EndIf Cls Color r/3,g/3,b/3 For t=0 To 15 Step 2 Rect 0,t,w,1,False Next FlipCanvas titlecanvas SetBuffer CanvasBuffer(canvas) DrawBlock image,0,0 FlipCanvas canvas SetBuffer CanvasBuffer(colorcanvas) ClsColor drawR,drawG,drawB:Cls drawR=(drawR+128) Mod 256 drawG=(drawG+128) Mod 256 drawB=(drawB+128) Mod 256 Color drawR,drawG,drawB Rect 0,0,28,20,False FlipCanvas colorcanvas End Function Function FreeM33P(bank) ; security.. If Not bank Break "No bank given in FreeM33P()" gadgets=PeekInt(bank,4) b=BankSize(gadgets) ; only free gadgets if there are gadgets anyway.. If b For t=0 To b-1 Step 4 ; step 4, because gadgets, images, banks etc. are stored in INT values (= 4 bytes) g=PeekInt(gadgets,t) If g FreeGadget g ; perhaps we freed a gadget earlier? No need to free it now then... Next EndIf images=PeekInt(bank,8) b=BankSize(images) ; only free images if there are images anyway.. If b For t=0 To b-1 Step 4 i=PeekInt(images,t) If i FreeImage i ; perhaps we freed an image earlier? No need to free it now then... Next EndIf FreeGadget PeekInt(bank,0) ; let's not forget the panel.. FreeBank bank ; an last but not least... the m33p itself. End Function Function Break(s$) Notify s$ SetBuffer DesktopBuffer() End End Function
m33p ^_^