www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GLchar** problem

reply "Saaa" <empty needmail.com> writes:
glshaderSource needs a GLchar** and all I get from cast(char[]) 
read(filename) is a single *

How do I get this extra pointer ? :D

The C code is:

char *file;
shader = glCreateShader(GL_FRAGMENT_SHADER);
file = textFileRead("program.frag");
const char * filep = file;
glShaderSource(f, 1, &filep,NULL);
free(filep);

my D attempt:

char[] file;
GLuint shader;
shader=glCreateShader(GL_FRAGMENT_SHADER);
file=cast(char[])read(`program.frag`);
glShaderSource(f, 1, cast(char **) toStringz(file),null);
//let gc collect file

Error: Access Violation :)
Oct 30 2008
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 30 Oct 2008 20:44:06 +0100, Saaa <empty needmail.com> wrote:

 glshaderSource needs a GLchar** and all I get from cast(char[])
 read(filename) is a single *

 How do I get this extra pointer ? :D

 The C code is:

 char *file;
 shader = glCreateShader(GL_FRAGMENT_SHADER);
 file = textFileRead("program.frag");
 const char * filep = file;
 glShaderSource(f, 1, &filep,NULL);
 free(filep);

 my D attempt:

 char[] file;
 GLuint shader;
 shader=glCreateShader(GL_FRAGMENT_SHADER);
 file=cast(char[])read(`program.frag`);
 glShaderSource(f, 1, cast(char **) toStringz(file),null);
 //let gc collect file

 Error: Access Violation :)

I believe this should work: char[] file; file = toStringz(read("program.frag")); glShaderSource(f, 1, &file, null); What your program does, is treat the first chars of toStringz(file) as a pointer, then wander off wildly in that direction. A more D-like way of doing it would possibly be: char[] file; file = read("program.frag"); glShaderSource(f, 1, &file.ptr, &file.length); -- Simen
Oct 30 2008
parent "Saaa" <empty needmail.com> writes:
Still D1 here.. :)


BTW, shouldn't these be reference return values now (dmd v2.020)? -- Simen

Oct 30 2008
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 30 Oct 2008 20:57:56 +0100, Simen Kjaeraas  
<simen.kjaras gmail.com> wrote:

 A more D-like way of doing it would possibly be:

 	char[] file;
 	file = read("program.frag");
 	glShaderSource(f, 1, &file.ptr, &file.length);

Scratch this last part. I'd hoped it worked, but ptr and length aren't lvalues, so no can do. -- Simen
Oct 30 2008
prev sibling next sibling parent reply torhu <no spam.invalid> writes:
Saaa wrote:
 glshaderSource needs a GLchar** and all I get from cast(char[]) 
 read(filename) is a single *
 
 How do I get this extra pointer ? :D
 
 The C code is:
 
 char *file;
 shader = glCreateShader(GL_FRAGMENT_SHADER);
 file = textFileRead("program.frag");
 const char * filep = file;
 glShaderSource(f, 1, &filep,NULL);
 free(filep);
 
 my D attempt:
 
 char[] file;
 GLuint shader;
 shader=glCreateShader(GL_FRAGMENT_SHADER);
 file=cast(char[])read(`program.frag`);
 glShaderSource(f, 1, cast(char **) toStringz(file),null);
 //let gc collect file
 
 Error: Access Violation :)
 
 

Assuming the C code works, here's what you do in D. GLuint shader; shader=glCreateShader(GL_FRAGMENT_SHADER); char[] file=cast(char[])read(`program.frag`); char* filep = toStringz(file); glShaderSource(f, 1, &filep,null);
Oct 30 2008
next sibling parent reply "Saaa" <empty needmail.com> writes:
 Assuming the C code works, here's what you do in D.

 GLuint shader;
 shader=glCreateShader(GL_FRAGMENT_SHADER);
 char[] file=cast(char[])read(`program.frag`);
 char* filep = toStringz(file);
 glShaderSource(f, 1, &filep,null);

erm.. ok, thanks :) Thought I tried this already. But, how is filep now an char** ?
Oct 30 2008
parent reply torhu <no spam.invalid> writes:
Saaa wrote:
 Assuming the C code works, here's what you do in D.

 GLuint shader;
 shader=glCreateShader(GL_FRAGMENT_SHADER);
 char[] file=cast(char[])read(`program.frag`);
 char* filep = toStringz(file);
 glShaderSource(f, 1, &filep,null);

erm.. ok, thanks :) Thought I tried this already. But, how is filep now an char** ?

It's not filep that's a char**, it's &filep. You're taking the address of a pointer, so you end up with a pointer to a pointer. If this code doesn't work, then something else is the matter.
Oct 30 2008
parent "Saaa" <empty needmail.com> writes:
No no, it works.
I always read &x as the location of x in memory, which as a whole is 
ofcourse a pointer :/
My bad.. too long since using them.

 It's not filep that's a char**, it's &filep. You're taking the address of 
 a pointer, so you end up with a pointer to a pointer.

 If this code doesn't work, then something else is the matter. 

Oct 30 2008
prev sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
On Fri, Oct 31, 2008 at 5:20 AM, Saaa <empty needmail.com> wrote:
 Assuming the C code works, here's what you do in D.

 GLuint shader;
 shader=glCreateShader(GL_FRAGMENT_SHADER);
 char[] file=cast(char[])read(`program.frag`);
 char* filep = toStringz(file);
 glShaderSource(f, 1, &filep,null);

erm.. ok, thanks :) Thought I tried this already. But, how is filep now an char** ?

It isn't filep is still char*. But &filep is a pointer to filep, which is a (char*)*. You realize that if you have TypeX x, then typeof(&x) is TypeX*, right? --bb
Oct 30 2008
prev sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 30 Oct 2008 21:02:41 +0100, Simen Kjaeraas  
<simen.kjaras gmail.com> wrote:

 On Thu, 30 Oct 2008 20:57:56 +0100, Simen Kjaeraas  
 <simen.kjaras gmail.com> wrote:

 A more D-like way of doing it would possibly be:

 	char[] file;
 	file = read("program.frag");
 	glShaderSource(f, 1, &file.ptr, &file.length);

Scratch this last part. I'd hoped it worked, but ptr and length aren't lvalues, so no can do.

BTW, shouldn't these be reference return values now (dmd v2.020)? -- Simen
Oct 30 2008