Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Starshyne Function Pack (require Matrix1Utils) - if someone could make a DBP plugin, I'd be eternally grateful

Author
Message
Emir Starshyne
3
Years of Service
User Offline
Joined: 4th Oct 2014
Location: Brazil
Posted: 30th May 2016 13:12
Ok, folks, now it's for real.
This community has given me so much knowledge and useful tips and now it's time for me to contribute with something.
It is not a big deal, no complex coding involved, but I think these functions are good enough to be helpful.

Since I use many functions that generate stuff for me, I was thinking "why not sharing them with the world?"

I'll put them in a bug code snippet in the end of this post, and will take some time explaining each one and how it is used and what it does, OK?

Enjoy!

IMPORTANT: some of these functions require Matrix1Utils plugin.

1. NAME GENERATOR FUNCTION KIT

return string = random_name(size as int,alphabet as string)

It returns a string that is a random name produced with the alphabet string provided. The alphabet string is a set of characters that are used to produce the name, obviously.
Size is not perfect, because it only says how many times it will iterate the generator, the final size can be greater than 1.5x the size in the worst of hypothesis.

To provide the alphabet, user have 2 choices:
1) write his/her own string, useful when you want to specify certain characters for the name generated
2) use the next function to do the dirty work

return string = alphabet_make(vowels as int, consonants as int)

What you see is what you'll get. Tell the function how many vowels you want for your alphabet, then how many consonants and it will give you a string containing what was randomly picked using your parameters.
This function has a fail-safe dumb-proof section in its code that randomly pick another vowel/consonant values if the given ones are not valid, to avoid someone from - for example - telling it that the alphabet has 6 or 7 vowels, something that is impossible.

2. PERCENTAGE FUNCTION:

If you want to know how many % of a value is some other value, this one will do it for you.

return float = percent(value as int, total as int)

Value and total are the ones you will use to find the percentage value, I think there is nothing else to explain since you all are smart enough to deduce how it work.

3. SAFE DELETION OF IMAGES:

In fact you could modify this to anything - sprites, matrices, objects...) but I made one for images and was too laze to make the others (someone can make them, it would be great).
It works in a way so simple that you will say "how did i never thought in something like that?"
All it does is check if the resource exists before trying to delete it. If it does, the function will delete it, but if it doesn't, nothing will be done, avoiding errors such "image does not exist".

safe_del_img(image_number as int)

4. CAPITALIZATION OF A STRING:

I know that there is the UPPER$ and LOWER$ functions that does the work of passing all chars of a string to upper or lower case.
It works great, but when you want to capitalize a string, you need to do more than just put it inside a function and wait for the work to be done.
This function does it for you in a easy way, quite like UPPER$ and LOWER$. Check out some examples:

STRING => String
STRING TO TRANSFORM => String To Transform
sTRING => String
sTrInG => String

In fact, it dosn't care if the string is upper or lower case, it will make it look like the examples above, no matter how large it is.

return string = caps(string)

5.PROCEDURAL SPACE INVADERS-LIKE SHIP:

A little thing I use for some variated purposes and now I am sharing with you. It does that: it makees an Space Invaders spaceship and store it in an image you can retrieve later.

space_invader(bitmap_number as int, image_number as int, rows as int, lines as int)

bitmap_number is the bitmap the function will use to draw the ship onto. Be sure of choosing one not in use, because the function will delete it after making its work.

image_number is the number of the image the function will use.

rows, lines are the x and y size of the space invaders ship in pixels.

Remember this: the generated image will be REALLY SMALL, since it will be drawn with few pixels. Useful to use with sprites, since it is small and eats little memory!

6. RANDOM COLOR GENERATOR:

Useful when you need to pick a color but don't want to lose your time thinking about that.
The only problem is that this function requires a little mental math (or the Windows calculator) to be fully used. I'll explain why in the function parameters' explanation.

random_color(color_step as int, no_black as boolean)

color_step is the one that'll need further calculation, since it is the number that will be divided by 255 to define how many shades of red, green and blue will used to make your color in the end.
For instance, a color_step of 2 will give you about 128 shades of each of the three colors, a 4 will give you around 64 and so on. A 128 color_step will give you similar color palletes you could get on old MS-DOS.
no_black is a boolean used to produce non-black colors - when you want colors that won't be transparent. If it is set to 1, if the color generated is black - rgb(0,0,0) - it will turn it in an almost-black - rgb(1,1,1) - that is not black but it is almost.

7. DISTANCE CALCULATION

For when you need to know how far 2 points are, there are 2 variables - 1D and 2D.

return integer = distance1d(x1,y1)

return integer = distance2d(x1,y1,x2,y2)

I think they are very self-explanative.

8. ANGULAR LINE DRAWING:

Easy as killing rabbits with an axe.

angleline(x as int,y as int,angle as int,lenght as int, clock as boolean)

x and y are the starting point of the line.

angle is, obviously, the angle of the line in degrees.

lenght is the lenght of the line in pixels (duh).

clock is a special parameter to allow you to use this line function to make a clock, if set to 0, the 0 degrees line will point to the right of the screen, but with a value of 1, it will point up like the clock hand at 12:00 o'clock.

9. LIFE BARS:

To make things easier, a little life bar function you can use anywhere and that doesn't require external media to work.

bar(x1,y1,x2,y2,color1 as dword,color2 as dword,current_value,max_value,mirror as boolean)

x1 and y1 are the top-left corner of the life bar.
x2 and y2 are the bottom right corner of the bar.
color1 is the color of the bar itself, a dword that stors an rgb value.
color2 is the background color
current_value is the current value - the current life your character has.
max_value is the total life points your character has.
mirror is a boolean, a 0 is normal, an 1 mirrors the life bar.

10. CLICKABLE BUTTON:

A function that tracks if mouse was clicked within a rectangular area. Useful for buttons or anything that requires you click on a certain spot.

return boolean = button_click(x1,y1,x2,y2)

x1,y1 are the top-left corner of the clickable area
x2 and y2 are the bottom-right.
It will return 1 if mouse was clicked and 0 if it hasn't click.
Easy, huh?

OK, now the huge snippet i promised. It is not very well indented or spaced, but I tried to make it readable enough to be edited and used easily by anyone.

+ Code Snippet

Now, if someone could transform these functions into a DLL for me, I would be eternally grateful.
<b><i>The mind is the compiler of dreams.</i></b>
Kevin Picone
15
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 4th Jun 2016 17:43 Edited at: 20th Apr 2017 17:00
Didn't have a close look but there seems to be some problems some functions.

In the Percent function it's going to do an integer division mult by integer 100 and convert the integer to a float. if you pass it params like percent(1,2) you'll get zero.. one of the terms has to float for it be cast a floating point operation.

eg..

+ Code Snippet

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code

Login to post a reply

Server time is: 2017-10-24 01:20:12
Your offset time is: 2017-10-24 01:20:12