It is currently Tue Jul 23, 2019 12:42 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 9:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 441
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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 10:23 am 
Offline
Enthusiast
Enthusiast
User avatar

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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 10:28 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1770
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
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 10:40 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 441
@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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 10:54 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1770
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
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 11:05 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 441
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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 11:31 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1770
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
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 11:51 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1770
Location: Germany
Added LoadArrayData from DataSection (Without check ArraySize!)

Link: viewtopic.php?f=12&t=73027

_________________
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
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 4:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sat Jun 15, 2019 5:24 pm 
Offline
Addict
Addict
User avatar

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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Sun Jun 16, 2019 7:56 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4571
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 :?: :!: :shock:

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Top
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Mon Jun 17, 2019 12:32 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 441
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
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Mon Jun 17, 2019 1:37 pm 
Offline
Addict
Addict
User avatar

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

Instead of this:
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

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Arrays() like C/C++?
PostPosted: Mon Jun 17, 2019 1:48 pm 
Offline
Enthusiast
Enthusiast
User avatar

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

          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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Exabot [Bot] and 10 guests


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye