- 论坛徽章:
- 0
|
pang是什么游戏不用多言了,希望对有兴趣学习opengl和X窗口编程的同志有用:
编译命令:g++ -lX11 -lGLU -lpthread <你保存的源码文件名> -o <输出的文件名>
//++++++++++++++++++++++++++++++++++++++++
//++++++++++THIS IS FREE SOURCE+++++++++++++
//++++++++++2009-5-17 by Little_Cat++++++++++++++
//++++++++++++++++++++++++++++++++++++++++
#include<iostream>
#include<GL/glx.h>
#include<GL/glu.h>
#include<X11/Xlib.h>
#include<time.h>
#include<cstdlib>
#include<pthread.h>
using namespace std;
//++++++++++++++++THE CLASS+++++++++++++++++++++++++++++++
class timect{ //the class about the time
public:
clock_t l;
void init();
void mathl();
private:
clock_t b;
};
void timect::init(){
b=clock();
l=0;
}
void timect::mathl(){
l=clock()-b;
}
//-------------------------------------------------------
class player{ //the class about the player
public:
float v;
float y;
int s;
void init();
void move();
};
void player::init(){
v=0.0;
y=0.0;
s=0;
}
void player::move(){
if((y>10.0)||(y<-10.0)){
v=0.0-v;
if(y>10.0) y-=0.1;
if(y<-10.0) y+=0.1;
}
y+=v;
}
//--------------------------------------------------------
class ballt{ //the class about the ball
public:
float x,y;
float vx,vy;
void init();
void move();
void change();
void change_b(float dy,float dx);
};
void ballt::init(){
x=0.0;
y=0.0;
vx=0.2;
vy=0.12;
}
void ballt::move(){
x+=vx;
y+=vy;
}
void ballt::change(){
if((x>10.0)||(x<-10.0)) vx=0.0-vx;
if((y>10.0)||(y<-10.0)) vy=0.0-vy;
}
void ballt::change_b(float dy,float dx){
if((y<dy+0.5)&&(y>dy-0.5)&&(x<=dx+0.1)&&(x>=dx-0.1)) vx=0.0-vx;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
int work=0;
int gameover=1;
float def_v=0.2;
player leftb,rightb;
ballt ball;
timect timec;
int allover=0;
Display *dpy;
Window root,win;
//+++++++++++++++++++++++++++++++++++++++++++++++++
void keyboard(unsigned int);
void init();
void reshape(int, int);
void* idle(void* mes);
void* display(void* mes);
void draw_score(int);
//+++++++++++THE MAIN++++++++++++++++++++++++++++++
int main(){
pthread_t id,id_dis;
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
XWindowAttributes gwa;
XEvent xev;
GLXFBConfig *fc;
GLXWindow glw;
GLXContext glc;
int att[]={
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, True,
GLX_DEPTH_SIZE, 16,
None
};
int nelement;
//****************found the window*************************
dpy=XOpenDisplay(NULL);
fc=glXChooseFBConfig(dpy,0,att,&nelement);
vi=glXGetVisualFromFBConfig(dpy,*fc);
root=DefaultRootWindow(dpy);
cmap=XCreateColormap(dpy,root,vi->visual,AllocNone);
swa.colormap=cmap;
swa.event_mask=ExposureMask | KeyPressMask;
win=XCreateWindow(dpy,root,
100,100,500,500,
0,vi->depth,
InputOutput,vi->visual,
CWColormap | CWEventMask,&swa);
XMapWindow(dpy,win);
XStoreName(dpy,win,"Fuck pong!" ;
glw=glXCreateWindow(dpy,*fc,win,NULL);
glc=glXCreateNewContext(dpy,*fc,GLX_RGBA_TYPE,NULL,GL_TRUE);
glXMakeContextCurrent(dpy,win,win,glc);
//*********************************************************
timec.init();
ball.init();
leftb.init();
rightb.init();
init();
//****************the 2 thread*****************************
pthread_create(&id,NULL,idle,NULL);//used to manage data
pthread_create(&id_dis,NULL,display,NULL);//used to draw
while(1){
XNextEvent(dpy,&xev);
if(xev.type==Expose){//if expose event happen
XGetWindowAttributes(dpy,win,&gwa);
reshape(gwa.width,gwa.height);
}
else if(xev.type==KeyPress){//if any key is pressed
if(work==0)while(!work);//avoid modifying data with idle() at the same time
keyboard(xev.xkey.keycode);
}
}
//****************to end************************************
allover=1;
pthread_join(id,NULL);
pthread_join(id_dis,NULL);
glXMakeCurrent(dpy,None,NULL);
glXDestroyContext(dpy,glc);
XDestroyWindow(dpy,win);
XCloseDisplay(dpy);
return 0;
}
//+++++++++++END++++++++++++END++++++++++++++END++++++++++++
void init(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0,0.0,0.0,0.0);
}
void* idle(void* mes){//manage data
while(!allover){if(work==0){
double l;
timec.mathl();
l=(double)(timec.l)/CLOCKS_PER_SEC;
if ((gameover == 0)&&(l>=0.04)){
leftb.move();
rightb.move();
ball.move();
ball.change();
ball.change_b(leftb.y,-7.1);
ball.change_b(rightb.y,7.1);
if(ball.x<-10.0){
rightb.s+=1;
if(rightb.s>2) gameover=1;
}
else if(ball.x>10.0){
leftb.s+=1;
if(leftb.s>2) gameover=1;
}
if(gameover==1){
leftb.init();
rightb.init();
ball.init();
}
timec.init();
}
work=1;
}}
}
void* display(void* mes){//draw
while(!allover){if(work==1){
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
//the line
glBegin(GL_LINES);
glVertex2f(0.0,10.0);
glVertex2f(0.0,-10.0);
glEnd();
//the left board
glRectf(-7.2,leftb.y+0.5,-7.0,leftb.y-0.5);
//the right board
glRectf(7.2,rightb.y+0.5,7.0,rightb.y-0.5);
//the ball
glRectf(ball.x+0.1, ball.y+0.1, ball.x-0.1, ball.y-0.1);
//the score
glLoadIdentity();
glTranslatef(-2.0,8.0,0.0);
draw_score(leftb.s);
glLoadIdentity();
glTranslatef(1.0,8.0,0.0);
draw_score(rightb.s);
glLoadIdentity();
if(gameover){
glColor3f(1.0,0.0,0.0);
glRectf(5.0,5.0,-5.0,-5.0);
}
glXSwapBuffers(dpy,win);
work=0;
}}
}
void keyboard(unsigned int key){//if the key is pressed
if (key==25)/*w*/leftb.v=def_v;
else if(key==39)/*s*/leftb.v=0.0-def_v;
else if(key==31)/*i*/rightb.v=def_v;
else if(key==45)/*k*/rightb.v=0.0-def_v;
else if(key==33)/*p*/exit(0);
else if(key==2 /*t*/{
if(gameover==1) gameover=0;
}
}
void draw_score(int s){//draw the score of the player
if (s==0){
glColor3f(0.0,0.0,0.0);
glRectf(0.0,0.0,1.0,2.0);
glColor3f(1.0,1.0,1.0);
glRectf(0.2,0.2,0.8,1. ;
}
else if(s==1){
glColor3f(0.0,0.0,0.0);
glRectf(0.5,0.0,0.7,2.0);
}
else if(s==2){
glColor3f(0.0,0.0,0.0);
glRectf(0.0,0.0,1.0,2.0);
glColor3f(1.0,1.0,1.0);
glRectf(0.0,1.8,0.8,1.1);
glRectf(0.2,0.9,1.0,0.2);
}
}
void reshape(int w,int h){//reshape the window if it has been changed
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} |
|