Author Topic: SET Library (Steve's Extended Toolset)  (Read 5090 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
SET Library (Steve's Extended Toolset)
« on: January 30, 2019, 11:56:57 am »
A collection of some of the most useful little library tools which I make use of on a regular basis, all collected and gathered together  for ease of inclusion into a program.

There's sort routines, search routines (a recent addition, tossed in and saved from the binary search topic), various text and graphic routines, extended timers, image save routines, safe font loading, spell checking, circle/ellipse filling, compression/decompression routines, conversion routines for various things...

A little bit of everything which I tend to have found useful to have at hand, from over my years of coding in QB64.

For now, there's not much written on HOW to make use of any of these, but they should be simple enough to sort out if you read the comments, like so:

Code: QB64: [Select]
  1. FUNCTION ConvertOffset&& (value AS _OFFSET)
  2.     DIM m AS _MEM 'Define a memblock
  3.     m = _MEM(value) 'Point it to use value
  4.     $IF 64BIT THEN
  5.         'On 64 bit OSes, an OFFSET is 8 bytes in size.  We can put it directly into an Integer64
  6.         _MEMGET m, m.OFFSET, ConvertOffset&& 'Get the contents of the memblock and put the values there directly into ConvertOffset&&
  7.     $ELSE
  8.         'However, on 32 bit OSes, an OFFSET is only 4 bytes.  We need to put it into a LONG variable first
  9.         _MEMGET m, m.OFFSET, temp& 'Like this
  10.         ConvertOffset&& = temp& 'And then assign that long value to ConvertOffset&&
  11.     $END IF
  12.     _MEMFREE m 'Free the memblock

A function to Convert Offsets to Integer values...  Even without a demo, folks should be able to figure out to do something like X = ConvertOffset(Y).

For the SpellChecker, we have the following:

Code: QB64: [Select]
  1. FUNCTION SpellCheck (TestWord$, CorrectWord$)
  2.     'IF Spellcheck = 100, we have a perfect 100% match for our words
  3.     'If the value is from -1 to -100, we have a percent match, so the routine "grades" how close the match is.
  4.     'If the value is 0, the words are just too different for the routine to think it's a match at all.
  5.     'Personally, I'd consider anything with an 80+ score to be a close enough match  usually.
  6.  
  7. ....more stuff

The comments should explain how it works well enough, I hope. 



As time (and motivation) allows, I'll post examples here of how to make use of these little routines one by one, but for the courageous who like to play around and figure things out themselves, feel free to grab it and use it in your own stuff.

If you have any questions about a routine (or set of routines), feel free to ask, and I'll explain/demo those first.

Secondary Dropbox link, in case the forum download doesn't work for everyone:

https://www.dropbox.com/s/bvyyl5i5xubrw9p/SET%20%28Steve%27s%20Extended%20Toolset%29.7z?dl=1
* SET (Steve's Extended Toolset) (02-03-2019).7z (Filesize: 121.4 KB, Downloads: 315)
« Last Edit: February 03, 2019, 06:38:47 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: SET Library (Steve's Extended Toolset)
« Reply #1 on: January 30, 2019, 04:20:02 pm »
Hi Steve, I am checking out your SET and am running into difficulty with both demos I try:
 
Cant find file.PNG


I changed the file name both by removing the "./" and also tried adding ".dll" extension, neither work. (I have since tried adding DYNAMIC and various combos of three mods, result: can't find file.

The file is in same place as source, the whole SET folder, is it suppose to be in QB64.exe location?
« Last Edit: January 30, 2019, 04:34:53 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SET Library (Steve's Extended Toolset)
« Reply #2 on: January 30, 2019, 04:35:09 pm »
That actually needs to be a DECLARE DYNAMIC LIBRARY there.  I noticed it a while ago and will be zipping up a fixed archive soon for it. (I'm also doing a little bug hunting on edge numbers with the search.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: SET Library (Steve's Extended Toolset)
« Reply #3 on: January 30, 2019, 04:36:27 pm »
Yes, I tried DYNAMIC too.

Wait, OK now tried just DYNAMIC with original code, OK now.
« Last Edit: January 30, 2019, 04:40:14 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SET Library (Steve's Extended Toolset)
« Reply #4 on: January 30, 2019, 05:18:32 pm »
Toolset patched.  I'm thinking it should be working as advertised now, without that error.

EDIT: Add a demo to showcase how we'd both SORT and SEARCH an array:

Code: QB64: [Select]
  1. '$INCLUDE:'SET.BI'
  2.  
  3. SCREEN _NEWIMAGE(800, 600, 32)
  4.  
  5. DIM NumberArray(0 TO 100) AS LONG
  6. DIM m AS _MEM: m = _MEM(NumberArray()) 'Get the memory location of our array so we can work with it via _MEM
  7.  
  8.  
  9.     CLS
  10.     'And now to randomize some numbers
  11.     FOR i = 0 TO 20
  12.         r = INT(RND * 100)
  13.         NumberArray(i) = r
  14.     NEXT
  15.     MemSortSome m, 0, 20 'sort those 20 numbers so we can search them
  16.     PRINT "Index", "Number"
  17.     FOR i = 0 TO 20
  18.         PRINT "      "; i, NumberArray(i)
  19.     NEXT
  20.     index = BinaryMemSearchSome(50, m, 0, 20) 'find 50 amongst the records from 1 to 20
  21.     IF index < 0 THEN
  22.         PRINT "50 was not found."
  23.         PRINT "Where it should appear is after Index"; LastIndex
  24.     ELSE
  25.         PRINT "50 Exists, at Index"; index
  26.     END IF
  27.     SLEEP
  28.  
  29. '$INCLUDE:'SET.BM'

MemSort was my routine to sort a whole array, of any variable type which _MEM works with.

MemSortSome is a new routine to sort a fragment of an array, as specified from StartIndex to EndIndex.  In this case, I'm actually only sorting the first 21 records (from 0 to 20).

index = BinaryMemSearchSome(50, m, 0, 20)

BinaryMemSearch searches for a value in a mem block which points to an array.  For the above, we're searching for 50, in memblock m...

BinaryMemSearchSome adds the extra 2 parameters so we can do like MemSortSome -- specify the range of record which we want to search.  In this case, the 0 and 20 indicate that we only actually want to search the memblock starting at record 0 and ending at record 20.   

If the result is -1, we *DON'T* have a match.  Matches will start at record 0 and go up to the max of our search record...  Should we get a result of -1, then you can check with LastIndex to see exactly where the value would go after, IF it existed.
« Last Edit: January 30, 2019, 05:31:54 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SET Library (Steve's Extended Toolset)
« Reply #5 on: February 03, 2019, 06:42:25 am »
A demo of the toolset command SafeLoadFont:

Code: QB64: [Select]
  1. '$INCLUDE:'SET.BI'
  2.  
  3. SCREEN _NEWIMAGE(1025, 720, 32)
  4.  
  5. C = _LOADFONT("cour.ttf", 16) 'courier
  6. CB = _LOADFONT("courbd.ttf", 16) 'bold
  7. CBI = _LOADFONT("courbi.ttf", 16) 'bold italic
  8. CI = _LOADFONT("couri.ttf", 16) 'italic
  9.  
  10. PRINT "First a demo of the difference in the behavior of _FONT, and the reason why I prefer SafeLoadFont."
  11. PRINT "To start with, watch where the text on your screen appears from when I try to swap to a BOLD font."
  12. PRINT "PRESS <ANY KEY> WHEN READY.";
  13. temp$ = "THIS IS WHERE I AM NOW PRINTING"
  14. COLOR Red, Blue
  15. FOR i = 1 TO LEN(temp$)
  16.     _DELAY .1
  17.     PRINT MID$(temp$, i, 1);
  18. PRINT "Notice what happened to our print location, just by changing the font?"
  19. CLS , Black
  20. COLOR White, Black
  21. PRINT "Now, let's try this same routine with SafeLoadFont: ";
  22. COLOR Red, Blue
  23. SafeLoadFont CB
  24. temp$ = "THIS IS WHERE I AM NOW PRINTING"
  25. COLOR Red, Blue
  26. FOR i = 1 TO LEN(temp$)
  27.     _DELAY .1
  28.     PRINT MID$(temp$, i, 1);
  29. COLOR White, Black
  30. SafeLoadFont C
  31. PRINT "Did you notice the difference in the two routines?"
  32. PRINT "_FONT swaps our fonts, but it also resets our print location to 0,0 (or 1,1 in text mode)"
  33. PRINT "Meaning we can't just swap fonts and keep typing with it, without using LOCATE or some"
  34. PRINT "other method to keep printing in the same place."
  35. PRINT "SafeLoadFont, however, loads our font ";
  36. SafeLoadFont CBI
  37. PRINT "without";
  38. SafeLoadFont C
  39. PRINT " destroying our print location."
  40. PRINT "This makes it ";
  41. SafeLoadFont CI
  42. PRINT "ideal";
  43. SafeLoadFont C
  44. PRINT " for quickly swapping from normal text to bold or italic text."
  45. PRINT "As you can easily see with the sentences above."
  46.  
  47.  
  48.  
  49.  
  50. '$INCLUDE:'SET.BM'
  51.  

One of the handiest little routines which I use when working with various fonts -- especially bold/italic/underline versions. 

Be certain to grab the latest archive from the attachment under the first post (the toolset has been tweaked, added to, and edited a bit), and give it a quick run to see the simple -- but elegant -- difference between _FONT and SafeLoadFont.  ;)

(More demos to come later, but I thought I'd just start with this one.  I have to start somewhere, anyway. :P)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: SET Library (Steve's Extended Toolset)
« Reply #6 on: February 03, 2019, 04:58:49 pm »
And the riddle for the day> How many little blue pills did it take to get Steve's toolset extended?

While you're pondering that, hey Steve... $CHECKING:OFF is used and I thought sine that's a meta command it will be invoked if you use this function, as is, in a routine. If so, and the routine hasn't been debugged, I think this could cause a problem for someone not familiar with its use... which includes me, as I have never coded with it. I thought it turned error checking off and could even cause a segfault, according to the wiki. If so, it might be a good idea to post a message about it. Anyway. throw your best pitch, and see if you can catch me off base on this one. Look at me, 90-minutes before kick off and I'm still quoting baseball :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SET Library (Steve's Extended Toolset)
« Reply #7 on: February 03, 2019, 05:32:48 pm »
$CHECKING:OFF/ON works a whole lot like DEFINT/DEFLNG/_DEFINE does --- only from that point downwards.

Try these two sets of code:

Code: QB64: [Select]

and

Code: QB64: [Select]

With the first, you see the error message, because it comes after the error checking is turned back on.  In the second, you end up in an endless loop as QB64 tries to pointless return to nowhere, and it can't inform you that there's an error in the program flow...

You'll see a lot of mem routines which contain $CHECKING:OFF and $CHECKING:ON in them, but checking should only be disabled once you're certain they work WITHOUT BUGS, and only in limited scope.  It should never really "bleed over" to affect any other part of the program.  ;)

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!