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.

DarkBASIC Discussion / What is a memblock???

Author
Message
HomerS
13
Years of Service
User Offline
Joined: 8th Apr 2007
Location:
Posted: 13th Nov 2013 22:42
Hi all,

I see you can use memblocks in dbc but never used it.

But why should i use that (the benefit?) and what are they anyway?

I realy dont understand the help of dbc (i am more like basic-basic).

Is it like a variable?? Why nog use standard variables??

Can someone help me out?

Greetings,
Jeroen

Toedeledoki
Van B
Moderator
17
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Nov 2013 23:35
A memblock is a block of memory, the size varies depending on what the memory contains.

A common use would be a memblock image, which would be 32-bit, and start with identifying data, like the size of the resulting image. I'm not too familiar with DBC memblocks, I've only really used them in DBPro and AppGameKit, but in them:

wid=100
hig=100
dep=4

size=12+(hig*wid*dep)

create memblock 1,size
write memblock byte 1,0,wid
write memblock byte 1,4,hig
write memblock byte 1,8,dep*8


Then, you could change the data after position 12, to change the colour of the pixels in the memblock image before making an image from it.

Memblocks can contain data for an image, a sound, a mesh, or just whatever data you want to fill it with. They are a convenient way to access and edit the raw data of media, that's really their basic function, and they are well worth learning about - you can do some really clever things with memblocks.

I am the one who knocks...
Kevin Picone
17
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 14th Nov 2013 03:31 Edited at: 23rd Nov 2013 03:45
Quote: "Is it like a variable?? Why nog use standard variables??"


Nah, they're more like arrays.

Array's are nothing more than a chunk of memory we can access (read from, write to) via it's name and a position index within that array.

eg,




Above we're created an Integer array called MyData() and storing a single integer value inside it at position 50. It's not the only thing in the array though, all the positions (legal indexes) are initialized to zero.. So if we printed out the contents of index 49, it'd be zero at this point.

But what does it do ? Nothing really, arrays are nothing more than a user friendly interface that your programming language provides us with to interact with your computers memory. The programmer associates all meaning to what the data within any array represents.

eg,




In this example, we're using an array to store our games high score table. But really the computer doesn't have a clue what a high score is and why we're using this array in our program. To it, this is just another chunk of data. Internally the array is really just a lump of system memory. The language is just giving us a nice safe and organized way to access the information stored within it that chunk of memory.

Mem blocks are an alternative approach that we programmers can ask the language to give us a chunk of raw system memory. Computer memory is completely ambiguous. You can store anything you like in the chunk, from say integer, floats, string s through to picture data, sounds, text files through to machine code even. There's absolutely nothing special about it.

In DarkBASIC and DBPRO they've included mechanisms where the programmer can set out the data in the created mem Block that represents some internal media. VanB's example above shows how you can manually build an image via the mem block system for example.

When we do this, we've peek and poking the raw image data into the chunk of memory in fashion that some built in conversion function understands. So you'll find commands that convert mem blocks to images and vice versa. Those commands look at the data in a membock and attempt to create a drawable image from it. They require the programmer set out the data in the mem block in particular way. Generally there's a header with a group of control variables followed by the raw data.


One fundamental difference between the Array() and memblock interfaces is that Integer Arrays house integer values, where mem blocks house raw bytes. An integer is made up of 4 bytes, so an array of 100 elements is actually 100*4 bytes (plus whatever header data they store in front of that). The programming language limits what types of data a programmer can be write/read from any array, based upon that arrays type. So a floating point array can only store floating point values in it for example. Where a mem block has no such limit.

To emulate the high scores table in a mem block, we get something like this..




The Memblocks commands are a high level approach to memory access. The language hides the physical location of the memory from you (you can get those if you really need) and gives you a safe way to read and write information into them. By safe, I mean that if you attempt to access memory outside of the chunks defined size, the language will give you a run time error. Such protections aren't possible with Pointer styled access. Which are at at lower level.


Related Articles:

* What's a Memory Bank

29 games
14
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 14th Nov 2013 23:38 Edited at: 14th Nov 2013 23:38
Memblocks also allow you to pass data to external dlls, such as the DBC version of Sparky's collision dll. I think they're also used to pass data for multiplayer games and the such but I can't be sure.

I used then quite a lot for making 3D models in DBC code:

http://forum.thegamecreators.com/?m=forum_view&t=164244&b=10

And even made an FPS game using object made from memblocks.

http://forum.thegamecreators.com/?m=forum_view&t=176499&b=10

As Van B says, you can do some cool stuff with memblocks and are worth learning.

one of these days I'll come up with a better signature
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 15th Nov 2013 19:28
There's some good information here so I'll just add a simple analogy to help you start.

A variable is a lump of memory of a specific size, "shape" and "colour".
Size and "shape" together make the data-type of the variable.

Size is the number of bytes occupied by the variable.

"Shape" defines what each bits represents. For example, a string of bits, "01011001", could represent a number from 0-255, an ASCII character or something else entirely; we need to know what the data-type is before we can interpret the bits as intended.
Reading a variable is like passing a shape through a hole cut specifically for that shape only. If we try to push a FLOAT through an INTEGER hole it wont fit properly, we could force it through but we'd get something mangled out the other end.

"Colour" is the value assigned to the variable.

An array is a bunch of lumps of the same size and shape arranged in a rectangle. Each lump can be coloured independently.

A memblock is a big lump from which we can cut smaller lumps of any shape and size, colour them and stick them all back together. When reading and writing from/to a memblock we have to be careful not to cut in the wrong place, or with the wrong shape, or we'll corrupt the data.

I feel like making a infographic of that.


Formerly OBese87.
HomerS
13
Years of Service
User Offline
Joined: 8th Apr 2007
Location:
Posted: 15th Nov 2013 19:43 Edited at: 16th Nov 2013 23:29
Thank you all for the help.

It is too complex for my brain to learn. Ik stick it with the standaard basic codes.

LOL

Toedeledoki
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 15th Nov 2013 23:11 Edited at: 15th Nov 2013 23:11
Here is my infogram. I hope the information is correct.




Formerly OBese87.

Attachments

Login to view attachments
HomerS
13
Years of Service
User Offline
Joined: 8th Apr 2007
Location:
Posted: 16th Nov 2013 20:34
Thank you for all the help.

I like to use simple coding, like basic. C++ like is to hard for me to learn. So for me I have to practice alot with memblocks to undertand. Mabey I Stick to the standard basic coding.

So the simple coding I prefer, dont matter if the graphics are having blocks, simple games are fun too.

But do I have to use memblocks when i start to make a NET-game? Or can it be done without it.

GReetings.

Toedeledoki
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 17th Nov 2013 00:10 Edited at: 17th Nov 2013 00:12
I don't know, I've never made a "net" game. If you want to learn about memblocks then I'd recommend playing around with bitmaps using memblocks. You will need to understand how colour values work but other than that they are easy. Try loading/creating a bitmap, convert it to a memblock, change a few values and convert it back to a bitmap and see the effects. First try drawing dots of different colours, then boxes then lines then circles then triangles. That's what I did anyway and it was fun for me.

However, if you are not comfortable writing your own functions yet then I would suggest you leave memblocks til later. Manipulating a memblock is really laborious if you don't know how to write functions that will make it easy for you.

It is good to dabble in things so at least you know they are there. One day you will be writing something and think, "HEY, A MEMBLOCK WOULD BE PERFECT FOR THIS!"


Formerly OBese87.
Phaelax
DBPro Master
17
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Jan 2014 18:27
Memblocks are useful for random access files, manipulating/creating images, or creating sound effects. I'd point you towards some tutorials but they're all for DBP and I believe the memblock structure is different in DBC.

Login to post a reply

Server time is: 2020-08-12 03:01:44
Your offset time is: 2020-08-12 03:01:44