#ifdef __APPLE__
#include<GLUT/glut.h>
#include<openGL/openGL.h>
#else
#include<GL/glut.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include<GL/glut.h>
#include <stdio.h>
#define LIST_OBJECT 128
//tak dla szpanu niewiem co to robi chyba nic p.
#define molloc sizeof(*LIST_OBJECT) EOF
#define checkImageWidth 32
#define checkImageHeight 16
enum
{
PLAY,
PAUSE,
EXIT // wyjście
};
unsigned int i;
GLfloat d = 0.1;
struct kulka
{
GLfloat x;
GLfloat y ;
} kulka={0.0, -0.5} , wall= {1.0 , 1.0} , speed= {0.03, 0.04}, quad;
struct point
{
GLfloat x;
GLfloat y;
}point;
namespace glut {
//it's machine code (machinegun)
GLubyte checkImage[checkImageHeight][checkImageWidth][3];
static GLint height;
void makeCheckImage(void)
{
int i, j, c, d;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0xc0)==0)^((j&0xff))==0))*255;
checkImage[i][j][3] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) c;
}
}
}
bool CreateList()
{
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
makeCheckImage();
int c=127;
do
{
glNewList(LIST_OBJECT *c, GL_COMPILE);
{
c--;
glColor3f(1.0,1.0,.0);
glRasterPos2f(0.0, 0.0);
glDrawPixels(checkImageWidth, checkImageHeight, GL_RGB,
GL_UNSIGNED_BYTE, checkImage);
glFlush ();
glEndList();
}
}while (c > 0);
if(LIST_OBJECT *c>0)
return 1;
else
return 0;
}
bool enemy()
{
int a=0;
int i=113;
quad.x=0.9;
quad.y=0.9;
point.x=0.9;
point.y=0.9;
glBegin(GL_LINE);
glVertex2f(-quad.x, quad.y);
glVertex2f(quad.x, quad.y);
glVertex2f(quad.x, 0.0);
glVertex2f(-quad.x, 0.0);
glEnd();
do
{
if ( point.y<0.12)
{
point.y=quad.y;
point.x=point.x-0.12;
}
i--;
glPushMatrix();
glTranslatef(point.x ,point.y, 0.0);
glCallList(LIST_OBJECT *i);
glPopMatrix();
point.y=point.y-0.12;
if (((point.y-0.02)<kulka.y+d) && (kulka.x<d +(point.x-0.02) )&& ((point.x-0.02)<d+kulka.x) &&((kulka.y-0.02)<d+point.y) )
{
glDeleteLists(LIST_OBJECT *i, 1 );
#define delete (*LIST_OBJECT 128);
#define free (*LIST_OBJECT 128);
}
}
while(i>0);
glFlush ();
if ( a=113)
return 1;
else
return 0;
}
};
namespace glut {
const GLdouble left = - 10.0;
const GLdouble right = 10.0;
const GLdouble bottom = - 10.0;
const GLdouble top = 10.0;
const GLdouble near = 50.0;
const GLdouble far = 70.0;
GLfloat x2 , y2=-0.7;
int button_state = GLUT_UP;
int button_x, button_y;
void drawBall2(GLfloat a, GLfloat b)
{
glColor3f(1.0,0.0,.0);
glPushMatrix();
glTranslatef(x2, y2,0.0);
glutSolidSphere(d,20,20);
glPopMatrix();
}
void MouseButton( int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON )
{
// zapamiętanie stanu lewego przycisku myszki
button_state = state;
// zapamiętanie położenia kursora myszki
if( state == GLUT_DOWN )
{
button_x = x;
button_y = y;
}
}
}
void MouseMotion( int x, int y )
{
if( button_state == GLUT_DOWN )
{
x2 += 0.7 *( right - left ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
button_x = x;
y2 += 0.7 *( top - bottom ) / glutGet( GLUT_WINDOW_HEIGHT ) *( button_y - y );
button_y = y;
}
}
void drawBall1()
{
glColor3f(0.0,1.0,.0);
asm ("call glPushMatrix \n");
glTranslatef(kulka.x,kulka.y,0.0);
glutSolidSphere(d,20,20);
asm("call glPopMatrix \n");
}
void update()
{
kulka.x+=speed.x;
kulka.y+=speed.y;
quad.x+=speed.x;
quad.y+=speed.y;
if (kulka.x>wall.x)
{
speed.x=-speed.x;
}
if ( kulka.y>wall.y)
{
speed.y=-speed.y;
}
if(kulka.x<-wall.x)
{
speed.x=-speed.x;
}
if(( kulka.y<d + y2) && (kulka.x <d +x2 )&& (x2-kulka.x<d) )
{
speed.y=-speed.y;
}
}
};
/***********************************************************************************\
W I N D O W S D E F I N I T I O N S A N D V A R I A B L E S
\***********************************************************************************/
#define WIDTH 600 /* Frame Width is 600 pixels */
#define HEIGHT 600 /* Frame Height is 600 pixels */
#define TITLE "alfa arkanoid glut" /* Frame Title */
int winIdMain; /* Main Window handle */
int winIdSub; /* Sub-Window handle */
int menIdMain; /* Main menu handle */
int menIdSub; /* Sub-menu handle */
int menIdSubMain; /* Main menu for sub-window handle */
/***********************************************************************************\
A N I M A T I O N & S C E N E D E F I N I T I O N S A N D
V A R I A B L E S
\***********************************************************************************/
/* Small translation distance */
#define TIME_STEP 0.1
static double rotation_angle = 0.0; /* Current angle increment */
static double translation_distance = 0.0; /* Current translation increment */
static double time = 0.0; /* Current time */
static double spin = 0.0; /* Current Spin Angle */
static double center = 0.0; /* Current Center of Gravity */
static int object= PAUSE ; /* Object to use in scene */
/***********************************************************************************\
F U N C T I O N S & V A R I A B L E S F O R S T R I N G
R A S T E R I N G
\***********************************************************************************/
static char label[100]; /* Storage for current string */
/* Draw the string *s one character at a time using GLUT's bitmap
rendering utility */
void
drawString (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, s[i]);
};
/* Same as before but using a larger font bitmap */
void
drawStringBig (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, s[i]);
};
/***********************************************************************************\
W I N D O W S U P D A T I N G C A L L B A C K F U N C T I O N S
\***********************************************************************************/
void
mainDisplay (void)
{
/* Clean drawing board */
glutSetWindow (winIdMain);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glShadeModel(GL_SMOOTH);
glColor3f(1,0,0);
switch( object )
{
// kula
case PLAY:
glut:: drawBall1();
glut::update();
glut::enemy();
glut::drawBall2(glut::x2, glut::y2);
break;
// stożek
case PAUSE:
glutWireTorus( 0.2, 1, 10, 20 );
break;
case EXIT:
exit( 0) ;
}
glFlush();
glutSwapBuffers ();
};
/* Another display function, this one will be
used to update the graphic subwindow */
void
subDisplay ()
{
/* Clear subwindow */
glutSetWindow (winIdSub);
glClearColor (0.25, 0.25, 0.25, 0.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Draw border */
glColor3f (0.0F, 1.0F, 0.0F);
glBegin (GL_LINE_LOOP);
glVertex2f (0.0F, 0.0F);
glVertex2f (0.0F, 0.99F);
glVertex2f (0.999F, 0.99F);
glVertex2f (0.999F, 0.0F);
glEnd ();
/* Write State Variables */
glColor3f (1.0F, 1.0F, 1.0F);
sprintf (label, "Time = %8.3f ", time);
glRasterPos2f (0.05F, 0.75F);
drawString (label);
sprintf (label, "Rotation Angle = %8.3f ", spin);
glRasterPos2f (0.05F, 0.55F);
drawString (label);
sprintf (label, "notepadpl = %8.3f ", spin / time);
glRasterPos2f (0.05F, 0.35F);
drawString (label);
/* Write Banner and info */
glColor3f (1.0F, 0.0F, 1.0F);
sprintf (label, "arkanoid glut");
glRasterPos2f (0.40F, 0.70F);
drawStringBig (label);
sprintf (label, "notepadpl ");
glRasterPos2f (0.33F, 0.35F);
drawStringBig (label);
glutSwapBuffers ();
};
/* Callback function for reshaping the main window */
void
mainReshape (int w, int h)
{
/* Viewing and Projection transformations */
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-1.0F, 1.0F, -1.0F, 1.0F);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
/* Invoke subwindow reshape */
glutSetWindow (winIdSub);
glutReshapeWindow (w - 10, h / 10);
glutPositionWindow (5, 5);
glutSetWindow (winIdMain);
};
/* Callback function for reshaping the subwindow */
void
subReshape (int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0F, 1.0F, 0.0F, 1.0F);
};
/***********************************************************************************\
K E Y B O A R D I N P U T H A N D L I N G
Active Keys : i, I, q, Q
i, I switch on and off the info banner
q, Q quit the program
Note: parameters x and y correspond to the current location of the mouse
and they are not used in the program
\***********************************************************************************/
/* Now comes a function that processes keyboard events */
void
keyboard (unsigned char key, int x, int y)
{
static int info_banner = 1;
switch (key)
{
case 'i':
case 'I':
if (info_banner)
{
glutSetWindow (winIdSub);
glutHideWindow ();
}
else
{
glutSetWindow (winIdSub);
glutShowWindow ();
};
info_banner = !info_banner;
break;
case 'q':
case 'Q':
exit (0);
break;
};
};
/***********************************************************************************\
M E N U E V E N T S H A N D L I N G
\***********************************************************************************/
void
mainMenu(int value){
static int is_rotation = 0;
static int is_translation = 0;
switch( value )
{
case 1:
object = PLAY;
mainDisplay();
break;
// cylinder
case 2:
object = PAUSE;
mainDisplay();
break;
// wyjście
case 3:
exit( 0 );
}
};
void dummy(int value){};
/***********************************************************************************\
A N I M A T I O N
\***********************************************************************************/
/* There can be only one idle() callback function. In an
animation, this idle() function must update not only the
main window but also all derived subwindows */
void
idle (void)
{
/* Update state variables */
time += TIME_STEP;
spin += rotation_angle;
center += translation_distance;
if(center > 1.0) center -= 2.0;
/* Update main and sub window */
glutSetWindow (winIdMain);
glutPostRedisplay ();
glutSetWindow (winIdSub);
glutPostRedisplay ();
};
/***********************************************************************************\
M A I N : Initializes GLUT machine, opens frames, windows and menus and
runs GLUT's main loop
IMPORTANT NOTE: Pay attention to the order of the elements
\***********************************************************************************/
int
main (int argc, char *argv[])
{
/* Glut initializations */
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition (5, 5);
glutInitWindowSize (WIDTH, HEIGHT);
/* Main window creation and setup */
winIdMain = glutCreateWindow (TITLE);
glut::CreateList();
glutDisplayFunc (mainDisplay);
glutReshapeFunc (mainReshape);
glutKeyboardFunc (keyboard);
glutIdleFunc (idle);
glutMotionFunc( glut::MouseMotion );
glutPassiveMotionFunc( glut::MouseMotion );
glutMouseFunc( glut::MouseButton );
/* Create main menu */
menIdMain = glutCreateMenu(mainMenu);
glutAddMenuEntry("play", 1);
glutAddMenuEntry("pause", 2);
glutAddMenuEntry("Quit", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
/* Sub window creation and setup */
winIdSub = glutCreateSubWindow (winIdMain, 5, 5, WIDTH - 10, HEIGHT / 10);
glutDisplayFunc (subDisplay);
glutReshapeFunc (subReshape);
/* Create main menu for subwindow */
menIdSubMain = glutCreateMenu(dummy);
glutAddMenuEntry("This is a dummy menu", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop ();
return 0;
};
No comments:
Post a Comment