Results 1 to 10 of 10

Thread: output a variable to screen???

  1. #1
    Junior Member Newbie chuck norrrissss's Avatar
    Join Date
    Mar 2011
    Posts
    17

    output a variable to screen???

    so i can output text to the screen...
    renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'Y pos'");


    but how can i include a variable?
    thought it would be like this but it gives errors
    renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, VARIABLE);



    thanks for reading!
    am using win7 with vs2005
    am new to openGL, please be gentle [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  2. #2
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,214

    Re: output a variable to screen???

    It is not an OpenGL question.
    You are a beginner in programming, generally.
    Last parameter of the function is a string, so you have to print value of your variable into the string, and then pass the string as a parameter of the function.

  3. #3
    Junior Member Newbie chuck norrrissss's Avatar
    Join Date
    Mar 2011
    Posts
    17

    Re: output a variable to screen???

    sorry, thought it was opengl problem what problem is it then?
    yeah, not so good at programming

    let me phrase my question differently.
    i want to output a 'variable' onto the screen which i have created a bunch of graphics.
    normally in c++, i would use cout but in this case i want the 'variable' (which is actually a score of the game) to go in a certain position on the screen.

    and i thought the position part was opengl problem?

    any ideas?
    am using win7 with vs2005
    am new to openGL, please be gentle [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: output a variable to screen???

    You can use a function like sprintf to do that. Simply do something like this,
    Code :
    int num = 10;
    char buffer[10]={'\0'};
    sprintf(buffer, "%d", num); //%d is for integers 
    renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, buffer);
    Check the documentation for details of the funciton.
    Regards,
    Mobeen

  5. #5
    Junior Member Newbie chuck norrrissss's Avatar
    Join Date
    Mar 2011
    Posts
    17

    Re: output a variable to screen???

    perfect!!
    thank you
    am using win7 with vs2005
    am new to openGL, please be gentle [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  6. #6
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: output a variable to screen???

    sprintf(buffer, "%d", num); //%d is for integers
    NO! Bad! Never use sprintf. Ever. If you need to printf to a string, you use snprintf (or _snprintf if you're on Visual Studio). The unsized sprintf can cause many problems.

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    281

    Re: output a variable to screen???

    This cause a buffer overflow if the buffer is too little and the value of num too big or too little (cf. -9223372036854775808 to 9223372036854775807 in 64 bits) .
    (note that a buffer size of 33 bytes or more don't seem to cause the buffer overflow problem when the second parameter of sprintf is "%d")
    @+
    Yannoo

  8. #8
    Junior Member Newbie chuck norrrissss's Avatar
    Join Date
    Mar 2011
    Posts
    17

    Re: output a variable to screen???

    VS gave me a warning using 'sprintf' so i changed it to 'sprintf_s' and all is working well.

    is that ok?
    am using win7 with vs2005
    am new to openGL, please be gentle [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  9. #9
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: output a variable to screen???

    snprintf actually belongs to C99 standard, whereas sprintf_s is Microsoft only.

  10. #10
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: output a variable to screen???

    snprintf actually belongs to C99 standard, whereas sprintf_s is Microsoft only.
    True, but Visual Studio doesn't implement the C99 standard. Which is why I suggested _snprintf.

Similar Threads

  1. Black Screen output
    By hamdi.rakkez in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-24-2015, 09:12 AM
  2. Seeing just a black screen.. no output !!
    By rborgaonkar in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-30-2012, 06:09 PM
  3. rendering contents of a variable in the screen
    By mdog1234 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-02-2002, 07:29 PM
  4. Replies: 4
    Last Post: 06-06-2002, 10:52 PM
  5. Converting variable-interval input to fixed-interval output
    By imported_JML in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 03-24-2002, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean