June 02, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Parrill | On Tuesday, 2 June 2015 at 17:03:32 UTC, Alex Parrill wrote:
> On Tuesday, 2 June 2015 at 16:41:38 UTC, Kyoji Klyden wrote:
>
>> src:
>>
>> string source = readText("test.glvert");
>>
>> const string sources = source.toStringz;
>> const int len = source.length;
>>
>> GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
>>
>> glShaderSource(vertShader, 1, &sources, &len);
>>
>> pt.d(26): Error: cannot implicitly convert expression (toStringz(source)) of type immutable(char)* to const(string)
>>
>> pt.d(34): Error: function pointer glShaderSource (uint, int, const(char*)*, const(int)*) is not callable using argument types (uint, int, const(string)*, const(int)*)
>>
>> -
>>
>> I also tried passing the char array instead but no go.. What am I missing? :\
>
> Oops, do `const immutable(char)* sources = source.toStringz` (or just use `auto sources = ...`).
OMG IT FINALLY WORKS :O
(%1 of the program complete!)
Thankyou very much, this was a huge help! It's such a small piece but I feel like a learned alot from this. :)
|
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyoji Klyden | On Tuesday, 2 June 2015 at 16:41:38 UTC, Kyoji Klyden wrote:
> src:
>
> string source = readText("test.glvert");
>
> const string sources = source.toStringz;
> const int len = source.length;
>
> GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
>
> glShaderSource(vertShader, 1, &sources, &len);
Judging by the docs, you don't need null-terminated strings and can use native D strings, just pass their lengths:
string source = readText("test.glvert");
const char* sources = source.ptr;
const GLint len = cast(GLint)source.length;
GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource(vertShader, 1, &sources, &len);
|
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Wednesday, 3 June 2015 at 08:11:16 UTC, Kagamin wrote:
> On Tuesday, 2 June 2015 at 16:41:38 UTC, Kyoji Klyden wrote:
>> src:
>>
>> string source = readText("test.glvert");
>>
>> const string sources = source.toStringz;
>> const int len = source.length;
>>
>> GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
>>
>> glShaderSource(vertShader, 1, &sources, &len);
>
> Judging by the docs, you don't need null-terminated strings and can use native D strings, just pass their lengths:
>
> string source = readText("test.glvert");
>
> const char* sources = source.ptr;
> const GLint len = cast(GLint)source.length;
>
> GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
>
> glShaderSource(vertShader, 1, &sources, &len);
Oh that also works quite well!
Is casting necessary there though? DerelictGL treats GL types as D types, and .length is size_t so wouldn't it just turn into an int regardless??
Also the one part I don't understand is with &sources. So is this passing sources as a reference, but sources itself is a pointer to a pointer? I'm just a tad confused on how this part works :S
|
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyoji Klyden | On Wednesday, 3 June 2015 at 10:21:20 UTC, Kyoji Klyden wrote: > On Wednesday, 3 June 2015 at 08:11:16 UTC, Kagamin wrote: >> On Tuesday, 2 June 2015 at 16:41:38 UTC, Kyoji Klyden wrote: >>> src: >>> >>> string source = readText("test.glvert"); >>> >>> const string sources = source.toStringz; >>> const int len = source.length; >>> >>> GLuint vertShader = glCreateShader( GL_VERTEX_SHADER ); >>> >>> glShaderSource(vertShader, 1, &sources, &len); >> >> Judging by the docs, you don't need null-terminated strings and can use native D strings, just pass their lengths: >> >> string source = readText("test.glvert"); >> >> const char* sources = source.ptr; >> const GLint len = cast(GLint)source.length; >> >> GLuint vertShader = glCreateShader( GL_VERTEX_SHADER ); >> >> glShaderSource(vertShader, 1, &sources, &len); > > Oh that also works quite well! > Is casting necessary there though? DerelictGL treats GL types as D types, and .length is size_t so wouldn't it just turn into an int regardless?? size_t can be 32bit or 64bit, depending on your platform. Don't know how large GLint is. Assigning 64bit to 32bit requires an explicit cast, because it can lose information. > > Also the one part I don't understand is with &sources. So is this passing sources as a reference, but sources itself is a pointer to a pointer? I'm just a tad confused on how this part works :S A string (or any other array slice for that matter) is internally the equivalent of: struct Slice(T) { T* ptr; size_t length; } (maybe the order of fields is different, I never remember that part) `&source` will give you the address of that structure. |
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Wednesday, 3 June 2015 at 10:28:50 UTC, Marc Schütz wrote:
>
> A string (or any other array slice for that matter) is internally the equivalent of:
>
> struct Slice(T) {
> T* ptr;
> size_t length;
> }
>
> (maybe the order of fields is different, I never remember that part)
>
> `&source` will give you the address of that structure.
hmm I still a bit confused..
So in "const char* sources = source.ptr;" sources is just turning the property ptr of source into a variable, and then in glShaderSource you're passing the memory address of sources (which is technically source.ptr) to the function?
Do I have that right? If I do, then I think this all makes sense
|
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyoji Klyden | On Wednesday, 3 June 2015 at 10:21:20 UTC, Kyoji Klyden wrote: > On Wednesday, 3 June 2015 at 08:11:16 UTC, Kagamin wrote: >> On Tuesday, 2 June 2015 at 16:41:38 UTC, Kyoji Klyden wrote: [...] >> string source = readText("test.glvert"); >> >> const char* sources = source.ptr; [...] >> glShaderSource(vertShader, 1, &sources, &len); [...] > Also the one part I don't understand is with &sources. So is this passing sources as a reference, but sources itself is a pointer to a pointer? I'm just a tad confused on how this part works :S `&sources` is a pointer to `sources`. `sources` itself is a pointer to a char (leaving const-ness aside). So `&sources` is a pointer to a pointer to a char. |
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyoji Klyden | On Wednesday, 3 June 2015 at 10:56:21 UTC, Kyoji Klyden wrote: > So in "const char* sources = source.ptr;" sources is just turning the property ptr of source into a variable, yes > and then in glShaderSource you're passing the memory address of sources yes > (which is technically source.ptr) No, the address of sources is not the same as `source.ptr`. > to the function? > > Do I have that right? If I do, then I think this all makes sense |
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | Ooooh okay, I'm starting to get it. I think this last question should clear it up for me: When a string is made, how is the struct Slice handled? What does ptr get assigned? |
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyoji Klyden | On Wednesday, 3 June 2015 at 11:23:09 UTC, Kyoji Klyden wrote:
> Ooooh okay, I'm starting to get it. I think this last question should clear it up for me: When a string is made, how is the struct Slice handled? What does ptr get assigned?
ptr is a pointer to the first char, in other words the address of the first char.
|
June 03, 2015 Re: string to char array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Wednesday, 3 June 2015 at 11:28:14 UTC, anonymous wrote:
> On Wednesday, 3 June 2015 at 11:23:09 UTC, Kyoji Klyden wrote:
>> Ooooh okay, I'm starting to get it. I think this last question should clear it up for me: When a string is made, how is the struct Slice handled? What does ptr get assigned?
>
> ptr is a pointer to the first char, in other words the address of the first char.
I think I get how it's all basically working now. I've realized that my confusing is all coming from not understanding how D handles arrays, so I'm going to go look into that for a while.
Thanks for the help!
|
Copyright © 1999-2021 by the D Language Foundation