# PureBasic Forum

 It is currently Wed Jul 17, 2019 6:14 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 14 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Arrays() like C/C++?Posted: Sat Jun 15, 2019 9:53 am
 Enthusiast

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
In PureBasic i need to do this:
Code:
;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:
;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?

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 10:23 am
 Enthusiast

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 415
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:
! 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).

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 10:28 am

Joined: Fri May 12, 2006 6:51 pm
Posts: 1758
Location: Germany
Code:

; ;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 / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 10:40 am
 Enthusiast

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
@Olliv that is a nice idea
We can place the assembly in a function after the return or anywhere (using a jmp).

Code:
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).

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 10:54 am

Joined: Fri May 12, 2006 6:51 pm
Posts: 1758
Location: Germany
With Macro for Arrays

Edit
Code:

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 / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 11:05 am
 Enthusiast

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
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?

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 11:31 am

Joined: Fri May 12, 2006 6:51 pm
Posts: 1758
Location: Germany
This is only possible in the first initialization of the array.
The DataSection is Static

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 11:51 am

Joined: Fri May 12, 2006 6:51 pm
Posts: 1758
Location: Germany

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 4:26 pm
 Enthusiast

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 415
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...

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sat Jun 15, 2019 5:24 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3598
Location: Berlin, Germany
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

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups

Top

 Post subject: Re: Arrays() like C/C++?Posted: Sun Jun 16, 2019 7:56 pm

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4568
Location: Spa, relaxing and thinking, and learning...
Mijikai wrote:
In PureBasic i need to do this:
Code:
;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:
;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

_________________
http://www.zeitgeistmovie.com

Top

 Post subject: Re: Arrays() like C/C++?Posted: Mon Jun 17, 2019 12:32 pm
 Enthusiast

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
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:
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:
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

Top

 Post subject: Re: Arrays() like C/C++?Posted: Mon Jun 17, 2019 1:37 pm

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3559
Location: Utah, USA
@Mijikai: Wouldn't you want to use CompilerIf/CompilerEndIf instead of If/EndIf in Macro i()?

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

Use this:
Code:
Macro i()
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
!dd
CompilerElse
!dq
CompilerEndIf
EndMacro

_________________

Top

 Post subject: Re: Arrays() like C/C++?Posted: Mon Jun 17, 2019 1:48 pm
 Enthusiast

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
Demivec wrote:
@Mijikai: Wouldn't you want to use CompilerIf/CompilerEndIf instead of If/EndIf in Macro i()?

Thanks
Yes, i added the macro right in the post box as it was missing and made a mistake

Here are more Macros:
Code:
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:
;...
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)

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()

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()

;...

Just suit the macros to your need and enjoy

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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 14 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: oreopa and 12 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite