Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
i have to submit this project in 3 days, please someone help me out..in the project im filling the stack block by a different colour when user presses "push"..and then asking the user to enter character..im accepting this character by keyboard function..then using "glutpostredisplay" to display the character in the block where i recently pushed a different coloured block..the problem is, only the character or the different coloured block is displayed at a time..i want both to be displayed at a time..pplease help me out..

C++
#include
#include<stdio.h>

GLfloat s_v[20][2]={{300,20},{440,20},{300,70},{440,70},{300,120},{440,120},{300,170},{440,170},{300,220},{440,220},{300,270},{440,270},{300,320},{440,320},{300,370},{440,370},{300,420},{440,420},{300,480},{440,480}};

GLfloat pu_v[4][2]={{30,370},{30,420},{170,420},{170,370}};

GLfloat po_v[4][2]={{30,70},{30,120},{170,120},{170,70}};

static int i=0,j,oper=0,flow=0,k=0;
static char chr_ary[9];

void draw_push()
{
	char push[5]="PUSH";
	int iter;
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0, 500, 0.0, 500);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glColor3f(0.0, 0.0, 0.0);
	glRasterPos2i(65, 385);
	void * font = GLUT_BITMAP_TIMES_ROMAN_24;
	for (iter=0;iter<5;iter++)
	{
		char c = push[iter];
		glutBitmapCharacter(font, c);
	}
}

void draw_pop()
{
	char pop[4]="POP";
	int iter;
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0, 500, 0.0, 500);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glColor3f(0.0, 0.0, 0.0);
	glRasterPos2i(75, 85);
	void * font = GLUT_BITMAP_TIMES_ROMAN_24;
	for (iter=0;iter<4;iter++)
	{
		char c = pop[iter];
		glutBitmapCharacter(font, c);
	}
}

void draw_push_char_prompt()
{
    char push_prompt[35]="Enter the character to be pushed";
    int iter;
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0.0, 500, 0.0, 500);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glColor3f(0.0, 0.0, 0.0);
    glRasterPos2i(80, 240);
    void * font = GLUT_BITMAP_TIMES_ROMAN_24;
    for (iter=0;iter<35;iter++)
    {
        char c = push_prompt[iter];
        glutBitmapCharacter(font, c);
    }
}

void draw_push_char()
{
	int iter;
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0.0, 500, 0.0, 500);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glColor3f(1.0, 0.0, 0.0);
    void * font = GLUT_BITMAP_TIMES_ROMAN_24;
    for (iter=0;iter<k;iter++)>
	{
		glRasterPos2i(365, (40+50*(iter)));
		char c = chr_ary[iter];
		glutBitmapCharacter(font, c);
	}
}

void draw_overflow()
{
	char overflow[35]="STACK OVERFLOW! press 'q' to exit";
	int iter;
	flow=1;
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0, 500, 0.0, 500);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glColor3f(0.0, 0.0, 0.0);
	glRasterPos2i(60, 240);
	void * font = GLUT_BITMAP_TIMES_ROMAN_24;
	for (iter=0;iter<35;iter++)
	{
		char c = overflow[iter];
		glutBitmapCharacter(font, c);
	}
}

void draw_underflow()
{
	char underflow[35]="STACK UNDERFLOW! press 'q' to exit";
	int iter;
	flow=1;
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0, 500, 0.0, 500);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glColor3f(0.0, 0.0, 0.0);
	glRasterPos2i(50, 240);
	void * font = GLUT_BITMAP_TIMES_ROMAN_24;
	for (iter=0;iter<35;iter++)
	{
		char c = underflow[iter];
		glutBitmapCharacter(font, c);
	}
}

void draw_top_ptr()
{
	char top[4]="top";
	int iter;
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0, 500, 0.0, 500);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glColor3f(1.0, 0.0, 0.0);
	glRasterPos2i(250, 0+(25*i));
	void * font = GLUT_BITMAP_TIMES_ROMAN_24;
	for (iter=0;iter<4;iter++)
	{
		char c = top[iter];
		glutBitmapCharacter(font, c);
	}
}

void frame_block(int a,int b,int c,int d)
{
	glBegin(GL_LINE_LOOP);
	glColor3f(1.0,0.0,0.0);
	glVertex2fv(s_v[a]);
	glVertex2fv(s_v[b]);
	glVertex2fv(s_v[c]);
	glVertex2fv(s_v[d]);
	glEnd();
}

void stack_frame()
{
	frame_block(0,2,3,1);
	frame_block(2,4,5,3);
	frame_block(4,6,7,5);
	frame_block(6,8,9,7);
	frame_block(8,10,11,9);
	frame_block(10,12,13,11);
	frame_block(12,14,15,13);
	frame_block(14,16,17,15);
	frame_block(16,18,19,17);
}

void push_button()
{
	glBegin(GL_POLYGON);
	glColor3f(0.0,1.0,0.6);
	glVertex2fv(pu_v[0]);
	glVertex2fv(pu_v[1]);
	glVertex2fv(pu_v[2]);
	glVertex2fv(pu_v[3]);
	glEnd();
}

void pop_button()
{
	glBegin(GL_POLYGON);
	glColor3f(1.0,0.3,0.6);
	glVertex2fv(po_v[0]);
	glVertex2fv(po_v[1]);
	glVertex2fv(po_v[2]);
	glVertex2fv(po_v[3]);
	glEnd();
}

void push_char()
{
    glBegin(GL_POLYGON);
    glColor3f(1.0,1.0,1.0);
    glVertex2f(40,300);
    glVertex2f(40,200);
    glVertex2f(460,200);
    glVertex2f(460,300);
    glEnd();
    draw_push_char_prompt();
}

void stack_overflow()
{
	glBegin(GL_POLYGON);
	glColor3f(0.8,1.0,0.0);
	glVertex2f(300,20);
	glVertex2f(440,20);
	glVertex2f(440,480);
	glVertex2f(300,480);
	glEnd();
	stack_frame();
	glBegin(GL_POLYGON);
	glColor3f(1.0,1.0,1.0);
	glVertex2f(50,300);
	glVertex2f(50,200);
	glVertex2f(450,200);
	glVertex2f(450,300);
	glEnd();
	draw_overflow();
	draw_push_char();
}

void stack_underflow()
{
	glBegin(GL_POLYGON);
	glColor3f(1.0,1.0,1.0);
	glVertex2f(40,300);
	glVertex2f(40,200);
	glVertex2f(460,200);
	glVertex2f(460,300);
	glEnd();
	draw_underflow();
}

void push(int a,int b,int c,int d)
{
	if(oper==1&&i<=16)
	{
		i=i+2;
		glBegin(GL_POLYGON);
		glColor3f(0.8,1.0,0.0);
		glVertex2fv(s_v[a]);
		glVertex2fv(s_v[b]);
		glVertex2fv(s_v[c]);
		glVertex2fv(s_v[d]);
		glEnd();
		stack_frame();
		oper=0;
	}
	if(oper==1&&i>16)
	{
		oper=0;
		stack_overflow();
	}
}

void pop(int a,int b,int c,int d)
{
	if(oper==2&&j>=0)
	{
		k--;
		i=i-2;
		glBegin(GL_POLYGON);
		glColor3f(0.8,1.0,0.0);
		glVertex2fv(s_v[a]);
		glVertex2fv(s_v[b]);
		glVertex2fv(s_v[c]);
		glVertex2fv(s_v[d]);
		glEnd();
		stack_frame();
		oper=0;
	}
	if(oper==2&&j<0)
	{
		oper=0;
		k--;
		i=0;
		stack_underflow();
	}
}

void mouse(int btn,int state,int x,int y)
{
	if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN&&30<x&&x><170&&80><y&&y><130)>
	{
		oper=1;
		glutPostRedisplay();
	}
	if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN&&30<x&&x><170&&380><y&&y><430)>
	{	oper=2;
		glutPostRedisplay();
	}
}

void keys(unsigned char key, int x,int y)
{
	if((flow==1)&&(key=='q'||key=='Q'))
		exit(0);
	else
		if(k<9)
		{
			chr_ary[k++]=key;
			glutPostRedisplay();
		}
}

void display()
{
	j=i-4;
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	stack_frame();
	push_button();
	pop_button();
	draw_push();
	draw_pop();
	if(oper==1)
		push_char();
	push(0,2+i,3+i,1);
	pop(0,2+j,3+j,1);
	draw_push_char();
	draw_top_ptr();
	glutSwapBuffers();
}

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;
 
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,500.0,0.0,500.0);
	glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

void main(int argc, char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(0,0);
	glutCreateWindow("VISUALIZATION OF STACK");
	glutDisplayFunc(display);
	glutReshapeFunc(resize);
	glClearColor(0.0,0.0,0.0,1.0);
	glutMouseFunc(mouse);
	glRasterPos2i(100,120);
	glColor3f(1.0,0.0,0.0);
	glutKeyboardFunc(keys);
	glutMainLoop();
}
[Edit]Code block added[/Edit]
Posted
Updated 13-May-13 6:56am
v3
Comments
LaxmikantYadav 13-May-13 8:39am    
Please share your code so we can help u
nad.sushmita 13-May-13 10:49am    
sir, im unable to post the whole code..is there any way to attach files??
Santhosh G_ 13-May-13 11:55am    
Do you need to display the pushed char( new character) in different colored block, and all others in normal color( red) ?
nad.sushmita 13-May-13 12:02pm    
no no, not at all..i need all characters in red, but with yellow background..but (as u might have observed) when i push a new character, only character in red is displayed and not yellow background..i want that to be fixed..

1 solution

I observed problem in push() and pop(). These functions are responsible to draw the yellow block.
But it checks the oper value before drawing. I just modified push() and pop(), to draw yellow blocks regardless of oper value.

Now all characters will draw in red with yellow background..

C++
void push(int a,int b,int c,int d)
{
    if(oper==1&&i<=16)
    {
        i=i+2;
        oper=0;
    }
    if(i<=16)
    {
        glBegin(GL_POLYGON);
        glColor3f(0.8,1.0,0.0);
        glVertex2fv(s_v[a]);
        glVertex2fv(s_v[b]);
        glVertex2fv(s_v[c]);
        glVertex2fv(s_v[d]);
        glEnd();
        stack_frame();
    }
    if(oper==1&&i>16)
    {
        oper=0;
        stack_overflow();
    }

}

void pop(int a,int b,int c,int d)
{
    if(oper==2&&j>=0)
    {
        k--;
        i=i-2;
        oper=0;
    }
    if(j>=0)
    {
        glBegin(GL_POLYGON);
        glColor3f(0.8,1.0,0.0);
        glVertex2fv(s_v[a]);
        glVertex2fv(s_v[b]);
        glVertex2fv(s_v[c]);
        glVertex2fv(s_v[d]);
        glEnd();
        stack_frame();
    }
    if(oper==2&&j<0)
    {
        oper=0;
        k--;
        i=0;
        stack_underflow();
    }
}
 
Share this answer
 
v2
Comments
nad.sushmita 13-May-13 14:41pm    
yea, it works like that..but is not proper..im using "oper" value to check if user has clicked on push button or not..it is interactive stack..needs to push only when user clicks on "push" button with mouse..u can check the mouse function..u will know what im talking of..thanks for help anyways :)
Santhosh G_ 13-May-13 22:08pm    
Now what is the problem? i can see all stack items drawn in red color with yellow background. I just modified push() and pop() to draw the background color for every frame. The above code change will not push or pop items unconditionally. Could you please explain the problem.

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