⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 SDL-Ball这款经典的弹球游戏克隆自arkanoid、dxball、breakout
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  }    if(powerup == '4')  {    pMan.spawn(a,b,PO_GLUE);  }    if(powerup == 'B')  {    pMan.spawn(a,b,PO_EXPLOSIVE);  }  if(powerup == 'E')  {    pMan.spawn(a,b,PO_GRAVITY);  }    if(powerup == '5')  {    pMan.spawn(a,b,PO_MULTIBALL);  }    if(powerup == 'F')  {    pMan.spawn(a,b,PO_BIGBALL);  }    if(powerup == 'G')  {    pMan.spawn(a,b,PO_NORMALBALL);  }    if(powerup == 'H')  {    pMan.spawn(a,b,PO_SMALLBALL);  }    if(powerup == 'I')  {    pMan.spawn(a,b,PO_AIM);  }    if(powerup == 'P')  {    pMan.spawn(a,b,PO_GUN);  }    if(powerup == 'Q')  {    pMan.spawn(a,b,PO_THRU);  }    if(powerup == 'R')  {    pMan.spawn(a,b,PO_LASER);  }}SDL_Surface *screen = NULL;SDL_Event sdlevent;//sounds//Mix_Chunk *SND_playerbullet = NULL;/* function to reset our viewport after a window resize */void resizeWindow( int width, int height ){    /* Height / width ration */    GLfloat ratio;    /* Protect against a divide by zero */    if ( height == 0 )      height = 1;    ratio = ( GLfloat )width / ( GLfloat )height;    /* Setup our viewport. */    glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );    /* change to the projection matrix and set our viewing volume. */    glMatrixMode( GL_PROJECTION );    glLoadIdentity( );    /* Set our perspective */    gluPerspective( 45.0f, ratio, 0.1f, 10.0f );    /* Make sure we're chaning the model view and not the projection */    glMatrixMode( GL_MODELVIEW );    /* Reset The View */    glLoadIdentity();}void initGL() {    printf("GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));   // printf("GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));    printf("GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));    //printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));    /* Enable smooth shading */    glShadeModel( GL_SMOOTH );    /* Set the background black */    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );    /* Depth buffer setup */    glClearDepth( 1.0f );    /* Enables Depth Testing */  //  glEnable( GL_DEPTH_TEST );    /* The Type Of Depth Test To Do */    glDepthFunc( GL_LEQUAL );       /* Really Nice Perspective Calculations */    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );        glEnable(GL_TEXTURE_2D);    glEnable(GL_BLEND);    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);}void initSound(){  Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 1024 );}bool writeTxt(TTF_Font *font, SDL_Color textColor,const char text[], GLuint texture, bool center){  //cout << "Write text to texture:" << num << "Gl: " << tex[num] << endl;  SDL_Surface *temp,*tempb;  Uint32 rmask, gmask, bmask, amask;  #if SDL_BYTEORDER == SDL_BIG_ENDIAN    rmask = 0xff000000;    gmask = 0x00ff0000;    bmask = 0x0000ff00;    amask = 0x000000ff;  #else    rmask = 0x000000ff;    gmask = 0x0000ff00;    bmask = 0x00ff0000;    amask = 0xff000000;  #endif  if(font == NULL)  {    cout << SDL_GetError() << endl;;    SDL_FreeSurface(temp);    SDL_FreeSurface(tempb);    return(FALSE);  }  temp = TTF_RenderText_Blended(font, text, textColor);  SDL_SetAlpha(temp, 0, 0);  tempb = SDL_CreateRGBSurface(0, 512, 512, 32, rmask,gmask,bmask,amask);  int w,h;  TTF_SizeUTF8(font,text, &w,&h);    SDL_Rect src,dst;  src.x=0;  src.y=0;  src.w=w;  src.h=h;  if(center)  {    dst.x=(512.0-w)/2.0;  } else {    dst.x=0;  }  dst.y=0;  dst.w=w;  dst.h=h;  SDL_BlitSurface(temp, &src, tempb, &dst);  glBindTexture(GL_TEXTURE_2D, texture);  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tempb->w, tempb->h, GL_RGBA, GL_UNSIGNED_BYTE, tempb->pixels);  SDL_FreeSurface(temp);  SDL_FreeSurface(tempb);  return(TRUE);}float rndflt(float total, float negative){  return (rand()/(float(RAND_MAX)+1)*total)-negative;}void initScreen(){  int SDL_videomodeSettings = SDL_OPENGL|SDL_RESIZABLE|SDL_DOUBLEBUF;    if(setting.fullscreen)    SDL_videomodeSettings |= SDL_FULLSCREEN;  screen = SDL_SetVideoMode(setting.resx,setting.resy,32, SDL_videomodeSettings);  if( screen == NULL )  {    cout << "Error:" << SDL_GetError() << endl;  }  resizeWindow(setting.resx,setting.resy);    var.halfresx = setting.resx /2;  var.halfresy = setting.resy / 2;  var.glunits_per_pixel = 3.3 / setting.resx; //Der er ~3.3 glunits fra < til > }void resetPlayerPowerups(){  player.powerup[PO_GRAVITY] = 0;  player.powerup[PO_BIGBALL] = 0;  player.powerup[PO_NORMALBALL] = 0;  player.powerup[PO_SMALLBALL] = 0;  player.powerup[PO_EXPLOSIVE] = 0;    player.powerup[PO_SLOWBALL] = 0;  player.powerup[PO_SPEEDBALL] = 0;    player.powerup[PO_GLUE] = 0;  player.powerup[PO_MULTIBALL] = 0;    player.powerup[PO_GROWPADDLE] = 0;  player.powerup[PO_SHRINKPADDLE] = 0;    player.powerup[PO_AIM] = 0;  player.powerup[PO_LASER] = 0;  player.powerup[PO_GUN] = 0;  player.powerup[PO_THRU] = 0;  }void initNewGame(){  player.level=0;  player.score=0;  gVar.deadTime=0;    gVar.newLevel=1;  player.lives=5;  //Easy skal ikke clears af powerups  if(player.difficulty > 0)  {    player.lives -= 2;  }  resetPlayerPowerups();}void pauseGame(){  var.paused=1;  SDL_WM_GrabInput(SDL_GRAB_OFF);  SDL_ShowCursor(SDL_ENABLE);}void resumeGame(){  SDL_WM_GrabInput(SDL_GRAB_ON);  SDL_ShowCursor(SDL_DISABLE);  var.paused=0;  var.menu=0;}void mkDLscene(GLuint *dl,textureClass tex){  //Scenen  *dl = glGenLists(1);  glNewList(*dl,GL_COMPILE);    glLoadIdentity();    glTranslatef( 0.0f, 0.0f, -3.0 );    glColor4f(1.0, 1.0, 1.0, 1.0);    glEnable(GL_TEXTURE_2D);    glBindTexture(GL_TEXTURE_2D, tex.prop.texture);    glBegin( GL_QUADS );      //venstre kant      glTexCoord2f(0.0f,0.0f);glVertex3f( -1.66, 1.25, 0.0 );      glTexCoord2f(1.0f,0.0f);glVertex3f( -1.60, 1.25, 0.0 );      glTexCoord2f(1.0f,-1.0f);glVertex3f( -1.60,-1.25, 0.0 );      glTexCoord2f(0.0f,-1.0f);glVertex3f( -1.66,-1.25, 0.0 );      //højre kant      glTexCoord2f(0.0f,0.0f);glVertex3f( 1.66, 1.25, 0.0 );      glTexCoord2f(1.0f,0.0f);glVertex3f( 1.60, 1.25, 0.0 );      glTexCoord2f(1.0f,-1.0f);glVertex3f( 1.60,-1.25, 0.0 );      glTexCoord2f(0.0f,-1.0f);glVertex3f( 1.66,-1.25, 0.0 );        glEnd( );  glEndList();}void coldet(brick & br, ball &ba, pos & p, effectManager & fxMan){  GLfloat x,y;  int i=0;    int debug=0;  int points=0;  GLfloat px=0,py=0;  bool col=0;  bool dirfound=0;  //vi tager y først da der er mindst brikker   if(ba.posy <= br.posy+(br.height*2.0)+ba.height && ba.posy >= br.posy-(br.height*2.0)-ba.height)  {    //cout << " y " << endl;    if(ba.posx >= br.posx-(br.width*2.0)-ba.width && ba.posx <= br.posx+(br.width*2.0)+ba.width)    {      //cout << " x " << endl;      for(i=0; i < 32; i++) // 32 punkter præcis      {         x = ba.bsin[i];        y = ba.bcos[i];        #ifdef debugBall        glLoadIdentity();        glPointSize(1.0);        glColor3f(1.0, 0.0, 0.0);        glTranslatef(ba.posx, ba.posy, -3.0);        glBegin( GL_POINTS );          glVertex3f( x, y, 0.0 );        glEnd( );        #endif        if(ba.posx+x >= br.posx-br.width  &&  ba.posx+x <= br.posx+br.width)        {          if(ba.posy+y <= br.posy+br.height  && ba.posy+y >= br.posy-br.height)          {            //Vi har helt sikkert ramt            points++;            px += x;            py += y;            col=1;          } //y        } //x      } //32 punkters for loop                  if(col)      {        px /= (float)points;        py /= (float)points;        #ifdef debugBall        glLoadIdentity();        glPointSize(3.0);        glColor3f(1.0, 1.0, 1.0);        glTranslatef(ba.posx, ba.posy, -3.0);        glBegin( GL_POINTS );          glVertex3f( px, py, 0.0 );        glEnd( );        #endif        if(ba.lastX+px <= br.posx-br.width && !br.n(0)) //         {          dirfound=1;        //   cout << "På venstre"<<endl;          if(ba.xvel > 0.0 && !player.powerup[PO_THRU])            ba.xvel *=-1;                    debug++;        }                if(ba.lastX+px >= br.posx+br.width  && !br.n(1)) //Vi er kun på højre side hvis der ikke er en nabobrik der         {          dirfound=1;          // cout << "På højre"<<endl;          if(ba.xvel < 0 && !player.powerup[PO_THRU])            ba.xvel *=-1;          debug++;        }                if(ba.lastY+py <= br.posy-br.height && !br.n(3)) //         {          dirfound=1;      //    cout << "På bunden" << endl;          if(ba.yvel > 0 && !player.powerup[PO_THRU])            ba.yvel *=-1;                      debug++;        }                if(ba.lastY+py >= br.posy+br.height && !br.n(2)) // &&         {          dirfound=1;      //     cout << "På toppen"<< endl;          if(ba.yvel < 0 && !player.powerup[PO_THRU])            ba.yvel *=-1;          debug++;        }                  if(dirfound)          {            if(ba.explosive)            {              br.type='B';            }            p.x = ba.posx+px;            p.y = ba.posy+py;                        ba.hit(p, br.color);                        if(!player.powerup[PO_THRU])            {              if(player.difficulty == EASY)              {                ba.setspeed(ba.velocity + 0.0005);              } else if(player.difficulty == NORMAL)              {                ba.setspeed(ba.velocity + 0.001);              }            }                        //Her håndterer vi powerups            pos a,b;            a.x = br.posx;            a.y = br.posy;                        //Hastigheden en powerup blier sendt afsted med            if(player.difficulty == EASY)            {              b.x = ba.xvel/2.0;               b.y = ba.yvel/2.0;             } else if(player.difficulty == NORMAL)            {              b.x = ba.xvel/1.5;               b.y = ba.yvel/1.5;             }                        br.hit(fxMan, a,b);          } else {            cout << "Ball hit brick, but i dont know where...:" <<endl;            pos a,b;            a.x = br.posx;            a.y = br.posy;            b.x = ba.xvel;            b.y = ba.yvel;            br.hit(fxMan,a,b);          }      } //collision    } //x boxcol  } //y boxcol}void padcoldet(ball & b, paddle_class & p, pos & po){  int i,points=0;  GLfloat x,y,px=0,py=0;  bool col=0;  //Er bolden tæt nok på?  if(b.posy < p.posy+(p.height*2.0)+b.height && b.posy > (p.posy-p.height)-b.height)  {    if(b.posx > p.posx-(p.width*2.0)-b.width && b.posx < p.posx+(p.width*2.0)+b.width)    {      for(i=0; i < 32; i++)      {        x = b.bsin[i];        y = b.bcos[i];        #ifdef debugBall        glLoadIdentity();        glPointSize(2.0);        glColor3f(1.0, 0.0, 0.0);        glTranslatef(b.posx, b.posy, -3.0);        glBegin( GL_POINTS );          glVertex3f( x, y, 0.0 );        glEnd( );        #endif                //Find de punkter der er inden i padden        if(b.posx+x > p.posx-p.width  &&  b.posx+x < p.posx+p.width)        {          if(b.posy+y < p.posy+p.height  &&  b.posy+y > p.posy-p.height)          {            col=1;            px +=x;            py +=y;            points++;          }        }      } //For loop            if(col)      {        col=0;        gVar.deadTime=0;        px /= (float)points;        py /= (float)points;        #ifdef debugBall        glLoadIdentity();        glPointSize(3.0);        glColor3f(1.0, 1.0, 1.0);        glTranslatef(b.posx, b.posy, -3.0);        glBegin( GL_POINTS );          glVertex3f( px, py, 0.0 );        glEnd( );        #endif                px = b.posx+px;        //Vi kender nu præcist x for nedslaget        //Hvor mange procent skal den drejes?        //160 + 10 = max 170, 0+10 = min 10 grader                if(py>-0.01)        {          //detsværre, ikke til at redde..          b.xvel *= -1;     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -