C++ help

Soldato
Joined
30 Aug 2009
Posts
8,099
Location
one nation under sony
Code:
// transform_XG.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "glut.h"

#define KEY_UP 101
#define KEY_DOWN 103	
#define KEY_LEFT 100
#define KEY_RIGHT 102


int tx=0, ty=0;
float sx=1.0, sy=1.0;

float theta=0.0;

void letter_A()
{
 	
  	//glColor3f (1.0, 0.0, 0.0);	       // set line colour red

	
	float red, green, blue;

glClear(GL_COLOR_BUFFER_BIT);

red = float(rand()%255)/255.0;
	green = float(rand()%255) / 255.0;
	blue = float(rand()%255) / 255.0;

	glColor3f(red, green, blue);


	glLineWidth(5.0);		// set line width
  
	 glBegin(GL_LINES);	// beginning of drawing
		glVertex2i(160, 500);
		glVertex2i(250, 75);		// left side line 
		glVertex2i(75,75);
		glVertex2i(160,500);		// right hand side line
				// finishing drawing.
		glVertex2i(95, 180);
		glVertex2i(225, 180);
	glEnd();

 

}

 
void letter_S()
{
 	
  	//glColor3f (0.0, 1.0, 0.0);	       // set line colour red
	
	float red, green, blue;

//glClear(GL_COLOR_BUFFER_BIT);

    red = float(rand()%255)/255.0;
	green = float(rand()%255) / 255.0;
	blue = float(rand()%255) / 255.0;

	glColor3f(red, green, blue);
	
	glLineWidth(5.0);		// set line width or add more line styles
  
	 glBegin(GL_LINE_STRIP);	
		glVertex2i(600, 500);	// link most lines together
		glVertex2i(400, 500);		
		glVertex2i(400, 300);
		glVertex2i(600,300);		
		glVertex2i(600,100); 	
	
	
	 
		glVertex2i(400, 100);		// last line	
		

		
		glEnd(); 


		
		
		
}

void display()
{
	glClear (GL_COLOR_BUFFER_BIT); // clear screen

	glPushMatrix();
		glTranslatef(tx,ty,0.0);
		letter_A();
	glPopMatrix();

	glPushMatrix();
		glScalef(sx,sy,1.0);
		letter_S();
	glPopMatrix();

	glPushMatrix();
		glRotatef(theta , 0.0, 0.0, 1.0); 	// rotate around origin
		letter_A();
		letter_S();
	glPopMatrix();

	glFlush();
}

void myKeyboardFunc (unsigned char key, int x, int y)
{
	switch (key) {

	case '8':
		tx = tx + 0;
		ty = ty + 100;				//Move A upward.
		glutPostRedisplay();
		break;

	case '4':
		tx = tx - 100;
		ty = ty + 0;				//Move A upward.
		glutPostRedisplay();
		break;

	case '6':
		tx = tx + 100;
		ty = ty + 0;				//Move A upward.
		glutPostRedisplay();
		break;

	case '2':
		tx = tx + 0;
		ty = ty - 100;				//Move A upward.
		glutPostRedisplay();
		break;

	case '9':
		tx = tx + 100;
		ty = ty + 100;				//Move A upward.
		glutPostRedisplay();
		break;

	case '7':
		tx = tx - 100;
		ty = ty + 100;				//Move A upward.
		glutPostRedisplay();
		break;

	case '1':
		tx = tx - 100;
		ty = ty - 100;				//Move A upward.
		glutPostRedisplay();
		break;

	case '3':
		tx = tx + 100;
		ty = ty - 100;				//Move A upward.
		glutPostRedisplay();
		break;

		
	case 't':
		sx = sx*0.90;
		sy = sy*0.90;			// scale S around origin (0,0)
		glutPostRedisplay();
		break;

	case 'g':
		sx = sx*1.1;
		sy = sy*1.1;			// scale S around origin (0,0)
		glutPostRedisplay();
		break;
	}
}

	 GLvoid window_special_key(int key, int x, int y) 
{    
  switch (key) {    

	case KEY_RIGHT: 
		theta=theta-1.57; 		//rotate right-hand-side by 45 degrees. 
		glutPostRedisplay();
		break;

	case KEY_LEFT: 
		theta=theta+1.57;		 //rotate left-hand-side by 45 degrees. 
		glutPostRedisplay();
    		break;
    
  	default:
    		printf ("Pressing %d doesn't do anything.\n", key);
    		break;
 

	
	
	case 27:			// Escape key
		exit(0);
		break;
	

 }
}



void init (void) 
{
/* select clearing color 	*/
   glClearColor (0.0, 0.0, 0.0, 0.0);

/* initialize viewing values  */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(0.0, 700.0, 0.0, 800.0);

}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (700, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("Drawing Letters AS");
   init ();
   glutDisplayFunc(display); 
   glutKeyboardFunc(myKeyboardFunc);
   glutSpecialFunc(& window_special_key);

   printf("Press \n's' to scale S around (0,0), \n't' to translate A.\n\n");
   glutMainLoop();
   return 0;   /* ANSI C requires main to return int. */
}

I'm trying to get the letter A to move in 8 directions and the letter S to scale

It worked but then I implemented rotation for both letters and now it doesn't.

don't know how to re-enable it

thanks in advance
EDIT:

lol I fixed it by commenting out

//glClear(GL_COLOR_BUFFER_BIT);

//glColor3f(red, green, blue);
 
Last edited:
Back
Top Bottom