How to use Allocate

Source-code only - please, don't post questions here.
mrminecrafttnt
Posts: 42
Joined: Feb 11, 2013 12:23

How to use Allocate

Postby mrminecrafttnt » Apr 14, 2017 21:15

This example uses Allocate for an image on an screen without errors. Usefull to replace the shared command. :)

Code: Select all

dim as integer pixel_color

dim as uinteger ptr image = allocate (640*480*sizeof(uinteger))
print image
dim as uinteger ptr pixel_ptr = image
for x as integer = 0 to 639
    pixel_color+=1
    for y as integer = 0 to 479
        pixel_color xor=y
        *pixel_ptr=pixel_color
        pixel_ptr+=1
    next
next

screenres 640,480,32
pixel_ptr = image
for x as integer = 0 to 639
    for y as integer = 0 to 479
        pset (x,y),*pixel_ptr
        pixel_ptr+=1
    next
next


deallocate(image)
sleep
MrSwiss
Posts: 1928
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to use Allocate

Postby MrSwiss » Apr 14, 2017 21:25

Sorry, but why don't you stop, posting sensless code ... (for Tips & Tricks, use Begin-
ners for that sort of thing)
Why *sensless* you might ask? Until you understand the basics (e.g. correct variable
types etc.) of programming in FB, I consider any sort of code as beeing sensless.
Use of Allocate in this context is simply: *Not The Way* to do it.

Code: Select all

dim as uinteger ptr image = allocate (640*480*sizeof(uinteger))  ' sensless
' below: at least correct type var, but still useless! (no image header)
Dim As ULong Ptr image = allocate(640 * 480 * SizeOf(ULong))  ' it's Color = 32bit always !!!
' correct:
Dim As Any Ptr image = ImageCreate(640, 480,, 32)
fxm
Posts: 7274
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to use Allocate

Postby fxm » Apr 15, 2017 5:26

'ImageCreate' easily allocates a formatted image buffer but only if a graphic screen is defined.
Otherwise a null pointer is returned.

In his example, Mrminecrafttnt does not use a formatted image buffer for Get/Put (Graphics) instructions, but only simple memory of 'UIntegers' to make its own backup of pixel colors.
In addition, for a 64bit processor, accessing a UInteger memory can be a little faster than accessing a ULong memory.
(You may need to read a bit better the posted code before reacting strongly!)
MrSwiss
Posts: 1928
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to use Allocate

Postby MrSwiss » Apr 15, 2017 10:10

fxm wrote:In addition, for a 64bit processor, accessing a UInteger memory can be a little faster than accessing a ULong memory.

Yep, and paying for that, by allocating double the memory, that's really needed ... (50% pure waste).
fxm
Posts: 7274
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to use Allocate

Postby fxm » Apr 15, 2017 10:37

Your 50% corresponds to about 1 MB in front of at least 1 GB available!
(obviously, the problem arises differently when that impacts large sizes of memory)
MrSwiss
Posts: 1928
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to use Allocate

Postby MrSwiss » Apr 15, 2017 12:09

You are obviously, living in a World of PC's only ...

There are also small Devices, Arduino's and the like, where Memory is a premium asset.
We are talking a few KB's, top most. (it's about programming skills, not exclusively FB)

If you've not learned, how to conserve Memory-use, you're going to be *lost* there.
fxm
Posts: 7274
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to use Allocate

Postby fxm » Apr 15, 2017 13:04

It is true that when I worked on DOS 360 Ko (more than 30 years ago), I saved both on the size of the compiled program and on the size of the variables used, but it was another era for me.
Stonemonkey
Posts: 526
Joined: Jun 09, 2005 0:08

Re: How to use Allocate

Postby Stonemonkey » Apr 18, 2017 22:26

By changing it to ulong instead of uinteger keeps it as 32 bits per pixel, i was using uinteger for long enough until someone pointed that out. Still, it's a useful way imo of creating buffers that can be manipulated fast with your own routines, making a gfx_buffer type lets you give the buffer custom properties too (I usually use 'new' instead of allocate though).

typically I start off with something like:

Code: Select all

type gfx_buffer
  as ulong ptr pixels         'points to bitmap in memory
  as long wwidth,height    'properties of image
  as ulong flags                '32 bit flags which can indicate masked, blended, or other properties
  'add anything else you want
end type


as well as that, you can create that type and point it to the freebasic screenbuffer or to image buffer bitmaps and manipulate them using your custom routines with calls to the same functions/subs.

This is also pretty much the way I'm working with SDL in c++ on android and makes it pretty easy to port code between different graphics libs and between FB and C++
sean_vn
Posts: 255
Joined: Aug 06, 2012 8:26

Re: How to use Allocate

Postby sean_vn » Apr 23, 2017 2:49

People who are quite happy to max out all N cores on their CPU are ultra conservative with memory usage. If you can gain anything from it then max out memory usage too. It's not 1979 when 64 Kbytes of RAM cost $639.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest