Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi to all
I am student in university of ioannina and i have a project in OpenGL.
I have to create a game like tetris and i diceded to make it with subwindows.
I create subwindows succesfully and i can print a text in subwindows method(glutDisplayFunc()) succesfully, but when i try to print a text outside of subwindows method its IMPOSSIBLE!!!!! TAKE A LOOK AT THE TEXT IN MAIN AND THE OTHER TEXT!!!!!!

Can someone help me???
Here is my code:
C++
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sstream>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#define WIDTH 302
#define HEIGHT 522

using namespace std;

int mainWin, subWin1, subWin2;

void processKeyboardKeys(unsigned char key, int x, int y)
{
	switch(key)
	{
		case 27:
			glutSetWindow(mainWin);
			exit(0);
		break;
	}
}

void printString(string s, int length, double x, double y, int subwintoprint)
{
	glutSetWindow(subwintoprint);
	glRasterPos2i(x,y);
	for(int i=0; i<length; i++)
	{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, s[i]);
	}
	glFlush();
}

void mainDisplay(void)
{
	glutSetWindow(mainWin);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_LINE_LOOP);
		glVertex2f(0, 61);
		glVertex2f(WIDTH, 61);
		glVertex2f(0, 62);
		glVertex2f(WIDTH, 62);
	glEnd();
	glBegin(GL_LINE_LOOP);
		glVertex2f(WIDTH-61, 63);
		glVertex2f(WIDTH-61, HEIGHT);
		glVertex2f(WIDTH-60, 63);
		glVertex2f(WIDTH-60, HEIGHT);
	glEnd();
	
	string score = "mainWin";
	glColor3f(1.0, 1.0, 1.0);
	printString(score.data(), score.size(), 40, 250, mainWin);

	
	glutKeyboardFunc(processKeyboardKeys); // Main window will close with ESC(escape button) character
	
	glFlush();
}

void subWin1Display(void)
{
	glutSetWindow(subWin1);
	glClearColor(0.0, 0.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluOrtho2D(0, WIDTH, 0, 60);
	
	string score = "subWin1";
	glColor3f(1.0, 1.0, 1.0);
	printString(score.data(), score.size(), 40, 25, subWin1);

	glFlush();
}

void subWin2Display(void)
{
	glutSetWindow(subWin2);
	glClearColor(0.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluOrtho2D(0, 60, 0, HEIGHT-62);
	
	string f = "sub";
	glColor3f(0.0, 0.0, 0.0);
	printString(f.data(), f.size(), 0, 200, subWin2);
	
	string g = "Win2";
	glColor3f(0.0, 0.0, 0.0);
	printString(g.data(), g.size(), 0, 180, subWin2);
	
	glFlush();
}

void SubWindows(void)
{
	//Sub window 1
	subWin1 = glutCreateSubWindow(mainWin, 0, HEIGHT-60, WIDTH, 60);
	glutDisplayFunc(subWin1Display);
	
	//Sub window 2
	subWin2 = glutCreateSubWindow(mainWin, WIDTH-60, 0, 60, (HEIGHT-63));
	glutDisplayFunc(subWin2Display);
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

	glutInitWindowPosition (300, 300);
	glutInitWindowSize (WIDTH, HEIGHT);
	
	// Main window
	mainWin = glutCreateWindow ("BRAXAPSA");
	gluOrtho2D(0, WIDTH, 0, HEIGHT);
	glutDisplayFunc(mainDisplay);
	
	
	SubWindows();
	
        //THE FOLLOWING TEXT WILL NOT BE PRINTED IN mainWin
	string g = "HELP ME!!!";
	glColor3f(1.0, 1.0, 1.0);
	printString(g.data(), g.size(), 0, 180, mainWin);

	glutMainLoop();

	return 0;
}
Posted

In GLUT, control function is responsible to draw the frame. Other threads can't draw with OpenGL commands. You can create a global variable to hold the data. In mainDisplay you can display it with OpenGL commands. When you want change the string, update the string from other function and call glutPostRedisplay() to redraw the frame again. Now entire frame will be redrawn with new text. Please take care to synchronize the access of string between two the threads.

C#
void mainDisplay(void)
{
.....
// draw the string with strText global variable
    glColor3f(1.0, 1.0, 1.0);
    printString(strText.data(), strText.size(), 0, 180, mainWin);
}


C#
int main(int argc, char *argv[])
{
....// change the text and request redraw..
strText = "HELP ME!!!"
glutPostRedisplay();
}
 
Share this answer
 
Comments
Snk Tay 26-Nov-12 11:27am    
You were right wiht glutPostRedesplay()!!!
THANK YOU :)
In void subWin2Display(void) you're setting your window to subWin2 and never changing it back.

Use glutGetWindow to get the original window first, then restore that with glutSetWindow before the function exits.

Do this for any function that calls glutSetWindow.

C++
void subWin2Display(void)
{
        int OldWindow = glutGetWindow();
	glutSetWindow(subWin2);
	glClearColor(0.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluOrtho2D(0, 60, 0, HEIGHT-62);
	
	string f = "sub";
	glColor3f(0.0, 0.0, 0.0);
	printString(f.data(), f.size(), 0, 200, subWin2);
	
	string g = "Win2";
	glColor3f(0.0, 0.0, 0.0);
	printString(g.data(), g.size(), 0, 180, subWin2);
	
	glFlush();
	glutSetWindow(OldWindow);

}


Note: You'll need to update void subWin1Display(void) also.
 
Share this answer
 
v4
Comments
Snk Tay 21-Nov-12 13:00pm    
I call glutSetWindow in every subwindows method. The text which is in subWintoDisplay is printing succesfully but the text which is in the main is not printting.

IF YOU CAN COMPILE THE CODE AND YOU WILL UNDERSTAND WHAT I MEAN!!!!
JackDingler 21-Nov-12 13:11pm    
Right, you told it not to display text in the main window when you called glutSetWindow.

It's doing exactly what you told it to do.
Snk Tay 21-Nov-12 15:24pm    
I tried it and its not working :(
JackDingler 21-Nov-12 15:28pm    
I see what rswapna28531 was getting at.

You shouldn't be writing to the OpenGL in main() when you're specifying display functions.
Snk Tay 21-Nov-12 15:39pm    
I cant understand what do you mean :(
I am not that good at OpenGL. But looking at your I felt I give a try.


If you write the below snippet in mainDisplay() function then It might work.
//THE FOLLOWING TEXT WILL NOT BE PRINTED IN mainWin
string g = "HELP ME!!!";
glColor3f(1.0, 1.0, 1.0);
printString(g.data(), g.size(), 0, 180, mainWin);
 
Share this answer
 
Comments
Snk Tay 21-Nov-12 7:02am    
I appreciate your answer. I know that if i put the code in mainDisplay() function it will work. But i want to print text in mainWin from outside of this function!!!!!
I know i want a lot!!!!! :|
JackDingler 21-Nov-12 15:43pm    
Yes, you want to use it in an unsupported manner.

You'll have to get the source code for glut. Modify it to support what you're doing and provide your special modification to anyone who wishes to use your code.

It's going to take a bit of work and effort on your part, to make glut work this way.
rswapna28 22-Nov-12 1:06am    
Try printing the text before creating the subwindows.
You might have tried it but give a try before if you haven't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900