Click here to Skip to main content
11,634,631 members (74,615 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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:
#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 20-Nov-12 13:22pm
Snk Tay376
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.

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.
  Permalink  
v4
Comments
Snk Tay at 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 at 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 at 21-Nov-12 15:24pm
   
I tried it and its not working :(
JackDingler at 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 at 21-Nov-12 15:39pm
   
I cant understand what do you mean :(
JackDingler at 21-Nov-12 15:45pm
   
You're doing it wrong.

You're trying to send stuff to the window, before glut has created the window.
Snk Tay at 21-Nov-12 16:02pm
   
But when i print the text which is in the main() all windows have been created!!!
JackDingler at 21-Nov-12 16:18pm
   
You've cued up the creation.

Glut does additional initialization in glutMainLoop().

One of the first things it will do is erase the window. and if your text was displayed, it's gone when you make that call.
Snk Tay at 21-Nov-12 17:07pm
   
I am using the glFlush() which prints permanently whatevers you want in a window.
JackDingler at 21-Nov-12 18:04pm
   
Perhaps you're right and the text is on the window for 0.0000msecs before being erased to call your drawing functions.

Are you watching very carefully?

Or are you expecting the text to remain after Glut erases the window?
Snk Tay at 21-Nov-12 19:47pm
   
Could you give me an example??
JackDingler at 22-Nov-12 11:24am
   
Glut redraws the window completely with every pass.

So anything you want displayed in the mainWin needs to be drawn in mainDisplay().
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.

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);
}

int main(int argc, char *argv[])
{
....// change the text and request redraw..
strText = "HELP ME!!!"
glutPostRedisplay();
}
  Permalink  
Comments
Snk Tay at 26-Nov-12 11:27am
   
You were right wiht glutPostRedesplay()!!!
THANK YOU :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
  Permalink  
Comments
Snk Tay at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 8,736
1 Sergey Alexandrovich Kryukov 8,359
2 Mika Wendelius 6,845
3 F-ES Sitecore 2,354
4 Suvendu Shekhar Giri 2,205


Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100