I want writting by Open gl

hi,

I want design program by open gl
when user set the psition by the mouse
I allowed for user enter s(he) text

I have some code for writting but not interactvie with user

#include <gl\glut.h>
#include<string.h>

void bitmap_output(int x,int y, char*string,void *font)

{int len,i;
glRasterPos2f(x,y);
len=(int)strlen(string);
for(i=0;i<len;i++)
{
glutBitmapCharacter(font,string[i]);}}

void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
bitmap_output(10,35,“First Attempt”,GLUT_BITMAP_TIMES_ROMAN_24);
bitmap_output(30,100,“Font with 9 by 15 pixels”,GLUT_BITMAP_9_BY_15);
bitmap_output(70,150,"Helvetica Font ",GLUT_BITMAP_HELVETICA_18);
glFlush();
}

void reshape (int w ,int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
}

int main (void)
{glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(350,250);

glutCreateWindow(“Font Program”);
glClearColor(1,1,1,0);
glColor3f(0,0,0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return(0);
}

how i can design this idea

I know must use key board and mouse function but I don’t know how (when user enter character by keyboard display in the same time in work area(the view port or windows I designed)

regardes

moon girl

I know Bob knows.

WHERE IS Bob??

So you are stuck on the text input?

If you’re talking about me, I’m here. Lucky for you I’m nice today, so I just give you some code. But I leave it up to you to figure out how it works though.

Two new functions needed, mouse and keyboard callback.

void keyboard(unsigned char key, int, int)
{
if(enteringText == true)
{
switch(key)
{
case 13: // enter
enteringText = false;
break;
case 8: // backspace
text.pop_back();
break;
default:
text.push_back(key);
break;
}

  glutPostRedisplay();

}
}

void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
textX = x;
textY = windowHeight - y - 1;
enteringText = true;
text = std::string();
}
}

Some global variables and a new header needed.

#include

bool enteringText = false;
std::string text;
int textX;
int textY;
int windowHeight;

Four functions need smaller modifications.
[ul][li] bitmap_output need to take a const char *string instead of a char *string.[] The reshape function must save the height if the window. Add this to the code: windowHeight = h;.[] The display function must display the string we type. Add this before flushing the output: bitmap_output(textX,textY,text.c_str(),GLUT_BITMAP_9_BY_15);.[*] In the main function, you need to register the mouse and keyboard callback. [/ul][/li]

glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);

You can use backspace to delete characters and enter will end the input so no more characters are accepted. I hope this is what you wanted.

[This message has been edited by Bob (edited 12-02-2002).]

HI EVERY BODY

Gavin thank you for posting but what means stuck??

=============
WELCOME Bob

thank you very much i use the code but I have problem with std

=============
this is my coding for all program

#include <gl\glut.h>
#include<string.h>

//using namespace std;

//gloabel varaible
bool enteringText = false;
std::string text;
int textX;
int textY;
int windowHeight;

void bitmap_output(int x,int y, const char*string,void *font)

{int len,i;
glRasterPos2f(x,y);
len=(int)strlen(string);
for(i=0;i<len;i++)
{
glutBitmapCharacter(font,string[i]);}
}

void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
bitmap_output(textX,textY,text.c_str(),GLUT_BITMAP_9_BY_15);

// bitmap_output(70,150,"Helvetica Font ",GLUT_BITMAP_HELVETICA_18);
glFlush();
}

void reshape (int w ,int h){
windowHeight=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
}

//function of key board
void keyboard(unsigned char key, int, int)
{ if(enteringText == true)
{ switch(key) { case 13: // enter
enteringText=false; break;

                            case 8: //backspace		         text.pop_back();			
			break;	
							default:		
							text.push	_(key);	
								break;		}	
            	glutPostRedisplay();	}}

//function of mouse
void mouse(int button, int state, int x, int y)
{ if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{ textX = x;
textY = windowHeight - y - 1;
enteringText = true;
text = std::string(); }}

int main (void)
{glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(350,250);
glutCreateWindow(“Font Program”);
glClearColor(1,1,1,0);
glColor3f(0,0,0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
return(0);
}

/// the compiler error

font.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects
n\font.cpp(10) : error C2653: ‘std’ : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects
n\font.cpp(10) : error C2146: syntax error : missing ‘;’ before identifier ‘text’
C:\Program Files\Microsoft Visual Studio\MyProjects
n\font.cpp(10) : error C2501: ‘string’ : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects
n\font.cpp(10) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

=============================
I sooooo sorry but its first time for me coding by open gl

and I have some qustion in this code
Can I ask you??

regards

moon_girl

hi;

I read some books

and I expect I must use class??

I try but I don’t Know how to use class correctly?

pleeeeeeeeeeeeeeese help me

MOON_GIRL

#include <string>

instead of

#include <string.h>

The '92 ANSI/ISO standards for C++ introduced STL, and put all the common C++ headers into a “std” namespace and used the headers w/o the .h. The headers with the .h like iostream, basically put everything into the std namespace. Using the .h form is noted as being deprecated.

Also, all of the standard C headers were given headers for C++ in the form c<headername> so instead of #include <stdlib.h>, you could do #include <cstdlib> and everything in that header is put into a std namespace.

Anyway, the C string.h (or cstring) is still use ful for things like strcpy, etc., but you can also do most of the same stuff with the string class in the string header (no .h).

hi,

welcome Deiussum

thank you

this problem is solved

================
I REPLACE text.pop_back(); BY
text = text.substr(0, text.length()-1);
IT IS O.K

BUT when replace text.push_back(key);
text.append(key);

I have error

font.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects
n\font.cpp(58) : error C2664: ‘class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &__thiscall std::basic_string<char,struct std::char_traits<char>,class std::a
llocator<char> >::append(const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)’ : cannot convert parameter 1 from ‘unsigned char’ to ‘const class std::basic_string<char,struct std::char_traits<char>,class s
td::allocator<char> > &’
Reason: cannot convert from ‘unsigned char’ to ‘const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >’
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

I want see member of std class how??

regards
MOON_GIRL

It’s actually the string class in the std namespace you want to see the members of.

Try change the append to this:

text += (char)key;

msdn.microsoft.com has some information on the STL classes like the string class, but they aren’t quite as well documented as most of their other stuff is.

HI;

WELCOME Deiussum

I USE IT

no error in compiler or run

but nothing appear in the windows

MOON_GIR

thank fot all

my program it work good now

moon_girl