Arrays() like C/C++?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Arrays() like C/C++?

Post by Mijikai »

In PureBasic i need to do this:

Code: Select all

;x y z
Dim vertices.f(6)
vertices(0) = 1:vertices(1) = 1:vertices(2) = 1
vertices(3) = 1:vertices(4) = 1:vertices(5) = 1
While in C you can do this:

Code: Select all

;x y z
float vertices[6] = {
  1,1,1
  1,1,1
} 
Obviously C in this case is way more easy to write, read and understand.
Is there some kind of clever Macro, assembler code or other trick i could use to get the same or similar behaviour?
Iirc there was a Macro for UUIDS that did something similar (it (ab)used DataSection) but i cant find it anymore.

Any Ideas?
:)
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Arrays() like C/C++?

Post by Olliv »

Not so quick as C syntax, sadly...

However, a member published a solution in 2015 on this forum.

See the way of ASM syntax. I do not remember exactly the right syntax for this day. It seems like this :

Code: Select all

! ALabel:
! DB 0,1,2,3,4,5,6,7,8
! DB 3,2,1
And after, you use CopyMemory() to store your datas to an array.

DB = Data Byte (8 bits)
DW = Data Word (16 bits)
DD = Data Double word (32 bits)
DQ = Data Quad word (64 bits)

ASM hexadecimal numbers differ from PB syntax : I think this is 0xFh, 0Fh or Fh for $F (15).
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Arrays() like C/C++?

Post by mk-soft »

Code: Select all


; ;x y z
; float vertices[6] = {
;   1,1,1
;   1,1,1
; } 

;x y z
Dim vertices.f(5) ; Index 0..5 

; Validation

size1 = (ArraySize(vertices()) + 1) * SizeOf(float)
size2 = ?end_vertices - ?begin_vertices

; Copy Datasection to Array
If size1 = size2
  CopyMemory(?begin_vertices, @vertices(), size1)
  For index = 0 To 5
    Debug StrD(vertices(index), 2)
  Next
Else
  Debug "Invalid Data Size"
EndIf

DataSection
  begin_vertices:
  Data.f 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
  end_vertices:
EndDataSection

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Arrays() like C/C++?

Post by Mijikai »

@Olliv that is a nice idea :)
We can place the assembly in a function after the return or anywhere (using a jmp).

Code: Select all

vertices:
!dd 1.0,1.0,1.0
!dd 1.0,1.0,1.0
;access -> ?vertices / we dont need CopyMemory()
This will be fine for static arrays but not for dynamic arrays.
So this is not really a (full) solution (and the DataSection cant grow).
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Arrays() like C/C++?

Post by mk-soft »

With Macro for Arrays

Edit

Code: Select all


Macro DimWithData(ArrayName, Type, Elements)
  Dim ArrayName#.Type(Elements - 1)
  DataSection
  __Begin_#ArrayName:
EndMacro
  
Macro EndDimWithData(ArrayName)
  __End_#ArrayName:
  EndDataSection
  CopyMemory(?__Begin_#ArrayName, @ArrayName(), ?__End_#ArrayName - ?__Begin_#ArrayName)
EndMacro

Global DimWithData(vertices,f, 6)
  Data.f 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
EndDimWithData(vertices)

For index = 0 To 5
  Debug StrF(vertices(index), 2)
Next

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Arrays() like C/C++?

Post by Mijikai »

That would be really neat @mk-soft but is that really dynamic?
I always thought that you can not change the size of the DataSection (runtime)?
Can i dynamically "allocate" more memory inside the DataSection?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Arrays() like C/C++?

Post by mk-soft »

This is only possible in the first initialization of the array.
The DataSection is Static
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Arrays() like C/C++?

Post by mk-soft »

Added LoadArrayData from DataSection (Without check ArraySize!)

Link: viewtopic.php?f=12&t=73027
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Arrays() like C/C++?

Post by Olliv »

What is finally sad, is C cannot protect our datas so strongly as we can protect them in PureBasic.

Do not hesitate to open a subject about it. I can be wrong...
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Arrays() like C/C++?

Post by Little John »

This topic has been discussed before, see e.g.
viewtopic.php?f=13&t=60372

And here is a 10 year old feature request:
viewtopic.php?f=3&t=37635
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Arrays() like C/C++?

Post by Psychophanta »

Mijikai wrote:In PureBasic i need to do this:

Code: Select all

;x y z
Dim vertices.f(6)
vertices(0) = 1:vertices(1) = 1:vertices(2) = 1
vertices(3) = 1:vertices(4) = 1:vertices(5) = 1
While in C you can do this:

Code: Select all

;x y z
float vertices[6] = {
  1,1,1
  1,1,1
} 
Obviously C in this case is way more easy to write, read and understand.
Where is the criteria to say such a think :?: :!: :shock:
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Arrays() like C/C++?

Post by Mijikai »

For dynamic stuff ill just keep using PureBasic functions.

For static things i came up with some simple macros (still using the inline fasm solution)
to create a section that then can be filled with (any) data.

Why? - Less code and more comfortable compared to a DataSection.
It can be placed anywhere in the program (the accessibility however is subject to the scope of the label).

How does it look:

Code: Select all

Section(Test)
  l() 123,456,789
  f() 2.456
  u() 'Hello!',0
EndSection()

Debug PeekL(?Test + 4)
Debug PeekF(?Test + 12)
Debug PeekS(?Test + 16)
Macros:

Code: Select all

Macro Section(a)
  !jmp @f
  a#:
EndMacro

Macro EndSection()
  !@@:
EndMacro

Macro q()
  !dq 
EndMacro

Macro d()
  !dq 
EndMacro

Macro l()
  !dd 
EndMacro

Macro i()
  If #PB_Compiler_Processor = #PB_Processor_x86
    !dd 
  Else
    !dq 
  EndIf
EndMacro

Macro f()
  !dd 
EndMacro

Macro b()
  !db 
EndMacro

Macro a()
  !db 
EndMacro

Macro u()
  !du 
EndMacro

Macro w()
  !dw 
EndMacro
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Arrays() like C/C++?

Post by Demivec »

@Mijikai: Wouldn't you want to use CompilerIf/CompilerEndIf instead of If/EndIf in Macro i()?

Instead of this:

Code: Select all

Macro i()
  If #PB_Compiler_Processor = #PB_Processor_x86
    !dd 
  Else
    !dq 
  EndIf
EndMacro
Use this:

Code: Select all

Macro i()
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    !dd 
  CompilerElse
    !dq 
  CompilerEndIf
EndMacro
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Arrays() like C/C++?

Post by Mijikai »

Demivec wrote:@Mijikai: Wouldn't you want to use CompilerIf/CompilerEndIf instead of If/EndIf in Macro i()?
Thanks :shock:
Yes, i added the macro right in the post box as it was missing and made a mistake :oops:

Here are more Macros:

Code: Select all

Macro sa(a);< ascii string + #CRLF$
  !db a#,0x0D,0x0A
EndMacro

Macro su(a);< unicode string + #CRLF$
  !du a#,0x0D,0x0A
EndMacro

;Note: replace the old EndSection() with this (adds a null terminator for strings)
Macro EndSection()
  !dw 0h
  !@@:
EndMacro
Snipped where i use the Macros :)

Code: Select all

;...
          Section(vertices)
            f()  1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0
            f() -1.0,  1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0
            f() -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0
            f()  1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0
            f()  1.0,  1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0
            f() -1.0,  1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0
          EndSection()
          
          *vbo = render_CreateVertexBuffer(?vertices,48 * 4,#GL_STATIC_DRAW)
          *vao = render_CreateVertexArray()
          *vao\Layout(3,#GL_FLOAT,#GL_FALSE)
          *vao\Layout(3,#GL_FLOAT,#GL_FALSE)
          *vao\Layout(2,#GL_FLOAT,#GL_FALSE)
          *vao\Buffer(*vbo)

          Section(vertex_shader)
            sa("#version 330 core")
            sa("layout (location = 0) in vec3 aPos;")
            sa("layout(location = 1) in vec3 aColor;")
            sa("layout(location = 2) in vec2 aUV;")
            sa("out vec3 vertexColor;")
            sa("out vec2 vertexUV;")
            sa("void main()")
            sa("{")
            sa("    vertexColor = aColor;")
            sa("    vertexUV = aUV;")
            sa("    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);")
            sa("}")
          EndSection()
          
          Section(fragment_shader)
            sa("#version 330 core")
            sa("out vec4 FragColor;")
            sa("in vec3 vertexColor;")
            sa("in vec2 vertexUV;")
            sa("uniform sampler2D myTexture;")
            sa("void main()")
            sa("{")
            sa("    vec2 ndc = vertexUV * 2.0 - 1.0;")
            sa("    //vec3 color = vertexColor * length(ndc);")
            sa("    vec3 color = texture(myTexture, vertexUV).rgb;")
            sa("    FragColor = vec4(color.rgb, 1.0f);")
            sa("}")
          EndSection()
          
          *sdr = render_NewShader()
          *sdr\Add(?vertex_shader,#Null$,#GL_VERTEX_SHADER)
          *sdr\Add(?fragment_shader,#Null$,#GL_FRAGMENT_SHADER)
          *sdr\Link()
;...
Just suit the macros to your need and enjoy :)

Note:
Sadly stacking different data-types horizontally is currently still not possible.
Post Reply