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.

Newcomers AppGameKit Corner / Generating random Hex values for use in SetMemBlockInt()

Author
Message
Dead Pixel
4
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 17th Feb 2015 23:57 Edited at: 18th Feb 2015 00:20
Hello again,

I assume that using a single SetMemBlockInt() is going to be faster that using 4 seperate SetMemBlockByte()'s to set the r,g,b,alpha values of a pixel so I'm trying to figure out how I might randomly generate a 3 byte hex value for the r,g,b components and to which I then append a known value for the alpha channel as in 0x??????FF.

I think what I need to do is use Random2(0x00, 0xFFFFFFFF) to get a random 4 byte value to which I then apply some bitwise operations to set the bits in the alpha channel to the value that I want, but how then do I ensure that only the bits in the alpha channel are manipulated?

Maybe this approach is slower after all because we aren't using predetermined values, even so I'd be interested in seeing how it would be done.

Thanks for reading.

//******************************
// Coding In BASIC using AppGameKit V2
//******************************
Van B
Moderator
16
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 18th Feb 2015 09:40
I tested the speed a while ago in AGKv1, between setting a dword and 4 bytes and the difference was negligible - pretty much exactly the same time taken.

I'm not sure why your using hex though - is that a requirement of the data your using?, or are you deliberately using it?

If it's deliberate, then stop! - AppGameKit is not really ideal for more complex data types, it's good for byte values so I always stick to that. I mean a memblock is just a sequence of bytes at it's heart, so why obfuscate things... there's no speed benefit in using other data types - remember AppGameKit is compiled to an interpreted bytecode language, I'm sure that dealing in bytes is the fastest, and using hex might actually just slow things down (while hex strings are converted back and forth).

I am the one who knocks...
Dead Pixel
4
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 18th Feb 2015 22:12 Edited at: 18th Feb 2015 22:36
Vab B, thanks for your feedback. Good to know that there is practically no difference between writng an int and 4 seperate bytes, will file this away for future use

The reason I'm using Hex is that the author of "Hands On AppGameKit Basic" is using it in chapter 24 where he covers memblocks. With regards to the bytecode (and purely out of curiosity) is there a decompiler available so we could take a look at the code, just to see what optimisations the compiler implemented?

Earlier today it occured to me that I could use SetMemBlockInt(id, offset, Random2(0x00, 0xFFFFFFFF) to set my random colour, then use SetMemblockByte(id, offset+3, Known_Alpha_Value) as a solution to my first post.

//******************************
// Coding In BASIC using AppGameKit V2
//******************************
Mike Archer
4
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 20th Feb 2015 00:28
Using hexadecimal is no different to decimal as the strings are converted at the time of compiling.

However, writing Random2(0x00,0xFFFFFFFF) is the same as writing Random2(0,-1) because we are using signed integers ( -2,147,483,648 to 2,147,483,647).
Dead Pixel
4
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 20th Feb 2015 22:09
Thanks, Mike.

Could you elaborate on how Random2(0,-1) equates to using Random2(0x00,0xFFFFFFFF) as my understanding of its usage is Random2(min, max). How does using -1 achieve the same thing?

//******************************
// Coding In BASIC using AppGameKit V2
//******************************
Mike Archer
4
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 20th Feb 2015 23:52
The maximum value a signed integer can be is 2,147,483,647 which is 0x7FFFFFFF, any hexadecimal number you write higher than that will be taken to be a negative number, 0xFFFFFFFF is -1.

If you write Random2(0x00,0xFFFFFFFF) the interpreter will see this as Random2(0,-1) and you will only get the results 0 & -1 rather than the results you are expecting. Try Print(Random2(0x00,0xFFFFFFFF)) a few times if you want to see this.

I would suggest you use Random2(0x00,0xFFFFFF) and then shift it left 8 bits, as you are overwriting the last 8 bits with the alpha value anyway. In code that would read as (Random2(0x00,0xFFFFFF) << 8)

Login to post a reply

Server time is: 2019-08-23 13:28:34
Your offset time is: 2019-08-23 13:28:34