MEM: Difference between revisions
No edit summary |
No edit summary |
||
Line 67: | Line 67: | ||
''Example 1:'' Demonstration of .IMAGE to determine an image's dimensions, .TYPE to verify the type and [[_MEMEXISTS]] to check image has not been freed | ''Example 1:'' Demonstration of .IMAGE to determine an image's dimensions, .TYPE to verify the type and [[_MEMEXISTS]] to check image has not been freed | ||
{{CodeStart}} | {{CodeStart}} | ||
{{ | {{Text|<nowiki>'The $UNSTABLE command may not be necessary if HTTP integration has been fully accepted into QB64PE.</nowiki>|#919191}} | ||
{{Text|<nowiki>'Feel free to remark it out if the IDE flags the following line with an ERROR message.</nowiki>|#919191}} | |||
{{ | {{Text|<nowiki>'And kindly report the issue on our forums or Discord so that we can update this page to keep it as 100% relevant, as possible.</nowiki>|#919191}} | ||
{{Cl| | |||
{{Cm|$UNSTABLE}}:HTTP | |||
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}({{Text|500|#F580B1}}, {{Text|500|#F580B1}}, {{Text|32|#F580B1}}) | |||
{{ | Image$ = {{Text|Download$|#55FF55}}({{Text|<nowiki>"https://qb64phoenix.com/qb64wiki/resources/assets/peWikiLogo.png"</nowiki>|#FFB100}}, statusCode&) {{Text|<nowiki>'Let's try and download the QB64PE Logo from the web</nowiki>|#919191}} | ||
{{Cl|IF}} statusCode& = {{Text|200|#F580B1}} {{Cl|THEN}} {{Text|<nowiki>' 200 says a proper connection was made to the web page in question</nowiki>|#919191}} | |||
i = {{Cl|_LOADIMAGE}}(Image$, {{Text|32|#F580B1}}, {{Text|<nowiki>"memory"</nowiki>|#FFB100}}) {{Text|<nowiki>' and then we load it for use as a registered imange</nowiki>|#919191}} | |||
{{Cl|ELSE}} | |||
{{Cl|PRINT}} {{Text|<nowiki>"HTTP ERROR"</nowiki>|#FFB100}}; statusCode {{Text|<nowiki>' can't get a proper connection to our webpage, so we don't have an image to work with.</nowiki>|#919191}} | |||
{{Cl|PRINT}} | {{Cl|END}} {{Text|<nowiki>' end and go report the issue on the forums, if you'd be so kind, dear user.</nowiki>|#919191}} | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|_PUTIMAGE}} ({{Text|0|#F580B1}}, {{Text|0|#F580B1}})-({{Text|500|#F580B1}}, {{Text|500|#F580B1}}), i {{Text|<nowiki>' put the image on the screen so we can view it</nowiki>|#919191}} | |||
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}: m = {{Cl|_MEMIMAGE}}(i) {{Text|<nowiki>' make a memblock and point it towards our image</nowiki>|#919191}} | |||
{{Text|<nowiki>' **** try uncommenting the following line and see what happens ****</nowiki>|#919191}} | |||
{{Text|<nowiki>'_MEMFREE m</nowiki>|#919191}} | |||
{{Cl|IF}} m.TYPE {{Cl|AND (boolean)|AND}} {{Text|2048|#F580B1}} {{Cl|THEN}} | |||
{{Cl|PRINT}} {{Text|<nowiki>"this is/was an image"</nowiki>|#FFB100}} | |||
{{Cl|IF}} {{Cl|_MEMEXISTS}}(m) {{Cl|THEN}} {{Text|<nowiki>' check if memory m is still available</nowiki>|#919191}} | |||
{{Cl|PRINT}} t {{Cl|AND}} {{Text|7|#F580B1}}; {{Text|<nowiki>"bytes per pixel"</nowiki>|#FFB100}} | |||
{{Cl|PRINT}} {{Text|<nowiki>"image handle "</nowiki>|#FFB100}}; m.IMAGE | |||
{{Cl|PRINT}} {{Text|<nowiki>"image width"</nowiki>|#FFB100}}; {{Cl|_WIDTH (function)|_WIDTH}}(m.IMAGE) | |||
{{Cl|PRINT}} {{Text|<nowiki>"image height"</nowiki>|#FFB100}}; {{Cl|_HEIGHT}}(m.IMAGE) | |||
{{Cl|ELSE}} {{Text|<nowiki>' if we removed the remark from the _MEMFREE above, we'll see the following message</nowiki>|#919191}} | |||
{{Cl|PRINT}} {{Text|<nowiki>"Memory already freed!"</nowiki>|#FFB100}} | |||
{{Cl|END IF}} | |||
{{Cl|END IF}} | |||
{{Text|<nowiki>' Content of the HTTP response is returned.</nowiki>|#919191}} | |||
{{Text|<nowiki>' The statusCode is also assigned.</nowiki>|#919191}} | |||
{{Cl|FUNCTION}} {{Text|Download$|#55FF55}} (url {{Cl|AS}} {{Cl|STRING}}, statusCode {{Cl|AS}} {{Cl|LONG}}) | |||
{{Cl|DIM}} h {{Cl|AS}} {{Cl|LONG}}, content {{Cl|AS}} {{Cl|STRING}}, s {{Cl|AS}} {{Cl|STRING}} | |||
h = {{Cl|_OPENCLIENT}}({{Text|<nowiki>"HTTP:"</nowiki>|#FFB100}} + url) | |||
statusCode = {{Cl|_STATUSCODE}}(h) | |||
{{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(h) | |||
{{Cl|_LIMIT}} {{Text|60|#F580B1}} | |||
{{Cl|GET}} #h, , s | |||
content = content + s | |||
{{Cl|WEND}} | |||
{{Cl|CLOSE}} #h | |||
{{Text|Download$|#55FF55}} = content | |||
{{Cl|END FUNCTION}} | |||
{{CodeEnd}} | {{CodeEnd}} | ||
Revision as of 02:15, 23 September 2023
The _MEM variable type can be used when working with memory blocks. It has no variable type suffix.
Syntax
Description
Variable TYPE:
- Memory DOT values are actually part of the built in memory variable type in QB64. The following TYPE is built in:
TYPE memory_type OFFSET AS _OFFSET 'start location of block(changes with byte position) SIZE AS _OFFSET 'size of block remaining at offset(changes with position) TYPE AS _OFFSET 'type description of variable used(never changes) ELEMENTSIZE AS _OFFSET 'byte size of values inside the block(never changes) IMAGE AS LONG 'the image handle used when _MEMIMAGE(handle) is used SOUND AS LONG 'the sound handle used when _MEMSOUND(handle) is used END TYPE The above TYPE is for clarification purposes only. It doesn't need to be pasted in |
Usage
- The _MEM type contains the following read-only elements where name is the _MEM variable name:
.TYPE values (version 1.000 and up incl. all QB64-PE releases)
- [bit 0] 1* byte types (_BYTE)
- [bit 1] 2* byte types (INTEGER)
- [bit 2] 4* byte types (LONG or SINGLE)
- [bit 3] 8* byte types (DOUBLE or _INTEGER64)
- [bit 4] 16* byte types (reserved for future use)
- [bit 5] 32* byte types (_FLOAT)
- [bit 6] 64* byte types (reserved for future use)
- [bit 7] 128 = integer types (_BYTE, INTEGER, LONG, _INTEGER64) (added to *)
- [bit 8] 256 = floating point types (SINGLE, DOUBLE, _FLOAT) (added to *)
- [bit 9] 512 = STRING types (fixed length or variable length)
- [bit 10] 1024 = _UNSIGNED types (added to *+128)
- [bit 11] 2048 = pixel data usually from _MEMIMAGE (added to 1+128+1024 for 256 color screens, or 2+128+1024 for text screens, or 4+128+1024 for 32-bit color screens)
- [bit 12] 4096 = _MEM TYPE structure (NOT added to 32768)
- [bit 13] 8192 = _OFFSET type (added to 4+128+[1024] or 8+128+[1024] or future_size+128+[1024])
- [bit 14] 16384 = data created/defined by _MEMNEW(size) or _MEMNEW(offset,size)
- [bit 15] 32768 = a custom, user defined type (ie. created with TYPE name ... END TYPE)
- [bit 16] 65536 = an array of data (added to other type values defining the array's data type)
Note: If a future integer, float or other type doesn't have a size that is 1,2,4,8,16,32,64,128 or 256 it won't have a size-bit set.
Versions prior to 1.000 (never use with QB64-PE releases)
- 1 = Integer types such as _BYTE, INTEGER, LONG, _INTEGER64 or _OFFSET
- 2 = _UNSIGNED variable types. Value must be added to the variable type value.(2 cannot be used by itself)
- 3 = ALL _UNSIGNED INTEGER type values.(add 1 + 2)
- 4 = Floating point types such as SINGLE, DOUBLE or _FLOAT
- 8 = STRING
- 0 = unknown(eg. created with _MEMNEW) or user-defined-types
- Note: _OFFSET values cannot be cast to other variable types reliably. _MEM is a reserved custom variable type.
- _MEM cannot reference variable length STRING variable values. String values must be designated as a fixed-length string.
Examples
Example 1: Demonstration of .IMAGE to determine an image's dimensions, .TYPE to verify the type and _MEMEXISTS to check image has not been freed
'The $UNSTABLE command may not be necessary if HTTP integration has been fully accepted into QB64PE. 'Feel free to remark it out if the IDE flags the following line with an ERROR message. 'And kindly report the issue on our forums or Discord so that we can update this page to keep it as 100% relevant, as possible. $UNSTABLE:HTTP SCREEN _NEWIMAGE(500, 500, 32) Image$ = Download$("https://qb64phoenix.com/qb64wiki/resources/assets/peWikiLogo.png", statusCode&) 'Let's try and download the QB64PE Logo from the web IF statusCode& = 200 THEN ' 200 says a proper connection was made to the web page in question i = _LOADIMAGE(Image$, 32, "memory") ' and then we load it for use as a registered imange ELSE PRINT "HTTP ERROR"; statusCode ' can't get a proper connection to our webpage, so we don't have an image to work with. END ' end and go report the issue on the forums, if you'd be so kind, dear user. END IF _PUTIMAGE (0, 0)-(500, 500), i ' put the image on the screen so we can view it DIM m AS _MEM: m = _MEMIMAGE(i) ' make a memblock and point it towards our image ' **** try uncommenting the following line and see what happens **** '_MEMFREE m IF m.TYPE AND 2048 THEN PRINT "this is/was an image" IF _MEMEXISTS(m) THEN ' check if memory m is still available PRINT t AND 7; "bytes per pixel" PRINT "image handle "; m.IMAGE PRINT "image width"; _WIDTH(m.IMAGE) PRINT "image height"; _HEIGHT(m.IMAGE) ELSE ' if we removed the remark from the _MEMFREE above, we'll see the following message PRINT "Memory already freed!" END IF END IF ' Content of the HTTP response is returned. ' The statusCode is also assigned. FUNCTION Download$ (url AS STRING, statusCode AS LONG) DIM h AS LONG, content AS STRING, s AS STRING h = _OPENCLIENT("HTTP:" + url) statusCode = _STATUSCODE(h) WHILE NOT EOF(h) _LIMIT 60 GET #h, , s content = content + s WEND CLOSE #h Download$ = content END FUNCTION |
Example 2: Converts the current destination SCREEN 13 image memory altered by PSET to a STRING value. SCREEN 13 only.
SCREEN 13 PSET (0, 0), ASC("H") 'top left corner of screen PSET (1, 0), ASC("E") PSET (2, 0), ASC("L") PSET (3, 0), ASC("L") PSET (4, 0), ASC("O") DIM m AS _MEM m = _MEMIMAGE(0) 'copy the screen memory to m x1$ = _MEMGET(m, m.OFFSET, STRING * 5) 'get at block start position LOCATE 2, 1:PRINT LEN(x1$) 'prints 5 bytes as size is STRING * 5 PRINT x1$ 'prints HELLO as ASCII character values PRINT m.OFFSET; m.SIZE; m.ELEMENTSIZE _MEMFREE m |
5 HELLO 5448320 6400 1 |
- Explanation: When a numerical _BYTE value is converted to a STRING, each byte is converted to an ASCII character. The QB64 IDE will capitalize _MEM dot values.
m.SIZE = 320 * 200 = 6400 bytes m.ELEMENTSIZE = 1 byte |
Example 3: Using _MEM to convert _OFFSET to _INTEGER64.
DIM x AS INTEGER DIM m AS _MEM m = _MEM(x) PRINT m.OFFSET PRINT ConvertOffset(m.OFFSET) FUNCTION ConvertOffset&& (value AS _OFFSET) $CHECKING:OFF DIM m AS _MEM 'Define a memblock m = _MEM(value) 'Point it to use value $IF 64BIT THEN 'On 64 bit OSes, an OFFSET is 8 bytes in size. We can put it directly into an Integer64 _MEMGET m, m.OFFSET, ConvertOffset&& 'Get the contents of the memblock and put the values there directly into ConvertOffset&& $ELSE 'However, on 32 bit OSes, an OFFSET is only 4 bytes. We need to put it into a LONG variable first _MEMGET m, m.OFFSET, temp& 'Like this ConvertOffset&& = temp& 'And then assign that long value to ConvertOffset&& $END IF _MEMFREE m 'Free the memblock $CHECKING:ON END FUNCTION |
Explanation: The above will print two numbers which should match. These numbers will vary, as they're representations of where X is stored in memory, and that position is going to vary every time the program is run. What it should illustrate, however, is a way to convert _OFFSET to _INTEGER64 values, which can sometimes be useful when trying to run calculations involving mem.SIZE, mem.TYPE, or mem.ELEMENTSIZE.
See also
- _MEM (function), _MEMELEMENT
- _MEMNEW, _MEMCOPY, _MEMFREE
- _MEMGET, _MEMPUT, _MEMFILL
- _MEMIMAGE, _MEMSOUND