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

📄 main.cpp

📁 SDL-Ball这款经典的弹球游戏克隆自arkanoid、dxball、breakout
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* ************************************************************************* *     SDL-Ball - DX-Ball/Breakout remake with openGL and SDL for Linux     Copyright (C) 2008 DusteD.dk    This program is free software: you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation, either version 3 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program.  If not, see <http://www.gnu.org/licenses/>.  * ************************************************************************* *////FIXME: lav loops der adskiller gamelogikken og grafikken+partikkelsimulationer, kig alle "move" og "draw" funktioner igennem for bolden/paddlen/brikkerne og sørg for de er adskildt#include <iostream>#include <fstream>#include <stdio.h>#include <stdlib.h>#include <GL/gl.h>#include <GL/glu.h>#include <SDL/SDL.h>#include <SDL/SDL_ttf.h>#include <SDL/SDL_mixer.h>#include <SDL/SDL_image.h>#include <math.h>#include <string.h>#include <list>#include "declerations.h"#define PI 3.14159265#define RAD 6.28318531#define FALSE 0#define TRUE 1//#define debugBall 1//Disse påvirker aktive bolde#define PO_GRAVITY    0#define PO_BIGBALL    1#define PO_NORMALBALL 2#define PO_SMALLBALL  3#define PO_EXPLOSIVE  4#define PO_SPEEDBALL  5#define PO_SLOWBALL 6//Påvirker player#define PO_GLUE 7//Kaldes eksternt#define PO_MULTIBALL 8#define PO_GROWPADDLE 9#define PO_SHRINKPADDLE 10#define PO_AIM 11#define PO_GUN 12#define PO_THRU 13#define PO_LASER 14#define EASY 0#define NORMAL 1#define EASY_BALLSPEED 0.5f#define NORMAL_BALLSPEED 1.0f#define EASY_MAXBALLSPEED 3.0f#define NORMAL_MAXBALLSPEED 6.0fusing namespace std;void writeSettings();void initScreen();void initSound();void initNewGame();void pauseGame();void resumeGame();float rndflt(float total, float negative);bool writeTxt(TTF_Font *font, SDL_Color textColor,const char text[], GLuint texture, bool center);TTF_Font *fonts[4] = {NULL,NULL,NULL};int globalTicks;float globalMilliTicks;int nonpausingGlobalTicks;float nonpausingGlobalMilliTicks;struct pos {  GLfloat x;  GLfloat y;};struct settings {  int resx;  int resy;  int fps;  bool fullscreen;  bool showBg;  bool sound;  bool music;  bool eyeCandy;};struct vars {  int halfresx;  int halfresy;  float glunits_per_pixel;  bool paused;  int menu;  int menuItem;  bool menuPressed;  int menuNumItems;  bool quit;    int numlevels;  bool transition_half_done;  bool clearScreen;  bool idiotlock; //transition    int showHighScores;};//Ting der har med spillogik at gørestruct gameVars {  int deadTime; //I hvor mange millisekunder har bolden intet rørt  bool nextlevel;  bool gameOver;  bool newLife;   bool newLevel; //Start en ny level  int bricksleft; //hvor mange brikker er der tilbage};struct gameVars gVar;struct player_struct {    bool powerup[20];    int level;  int lives;  int difficulty;  uint score;};struct settings setting;struct player_struct player;struct vars var;typedef GLfloat texPos[8];struct texProp {  GLuint texture; //Den GLtexture der er loaded  GLfloat xoffset;/// Hvor stort er springet mellem hver subframe  GLfloat yoffset; ///  int cols,rows; //hvor mange rækker og kolonner er der i denne textur  int ticks;  uint frames; //This many frames in each se  bool bidir; //Går Looper den fra 0 -> X - 0 eller fra 0 -> X -> 0  bool playing;};class textureClass {  private:    float age; //Hvor gammel er den frame vi er ved?    bool dir; //hvis dette er en animation der går frem og tilbage hvilken retning        uint lastframe; //check om det er den samme frame som sidst, så vi kan vide om vis skal opdatere cords      public:    uint frame; //hvilken frame er vi nået til i texturen (den er public så vi kan lave offset)    bool playing; //spiller vi?    texPos pos; //Kordinater for den frame på texturen der er nu    texProp prop; //Properties for den textur som dette objekt har    textureClass() {      age=10000;      lastframe=1000;      frame=1;      dir=0;    }  void play()  {    int col=0,row=0;    if(prop.playing)    {      //Skal vi skifte frame?      age += globalTicks;      if(age >= prop.ticks) //Denne frame har været vist længe nok      {        age=0.0;        if(!dir)        {          if(frame == prop.frames)          {            if(prop.bidir)            {                dir=1;            } else {              frame=1;            }          } else {            frame++;          }        }                if(dir) {          if(frame == 1)          {            dir=0;            frame=2;          } else {            frame--;          }        }      }    }        uint f=0;    if(frame != lastframe)    {      lastframe=frame;                  //hvor mange kolonner er der på en række      for(row=0; row < prop.rows; row++)      {        for(col=0; col < prop.cols; col ++)        {          f++;          if(f == frame)          {            //Øverst Venstre            pos[0] =  (prop.xoffset*(float)col); //0.0;            pos[1] =  (prop.yoffset*(float)row);//0.0;                        //Øverst højre            pos[2] = (prop.xoffset*(float)col) + prop.xoffset;            pos[3] = (prop.yoffset*(float)row);//0.0;                        //Nederst højre            pos[4] =  (prop.xoffset*(float)col) + prop.xoffset; // 1            pos[5] =  (prop.yoffset*(float)row) + prop.yoffset; // 1                        //Nederst venstre            pos[6] = (prop.xoffset*(float)col);//0.0;            pos[7] = (prop.yoffset*(float)row) + prop.yoffset; //1          }        }      }          }  }};class textureManager {  private:    int num;  public:    GLuint tex[256];        textureManager()    {      num=0;    }        bool load(string file, textureClass & tex)    {      SDL_Surface *temp = NULL;      GLint maxTexSize;            glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);       temp = IMG_Load(file.data());      if(temp == NULL)      {        cout << "Texture manager ERROR: " << file << " : "<< SDL_GetError() << endl;        SDL_FreeSurface( temp );        return(FALSE);      }                        //Hvis større end tilladt:      if(temp->w > maxTexSize)      {        cout << "Texture manager ERROR: " << file << " texturesize too large." << endl;      }            glGenTextures(1, &tex.prop.texture);      glBindTexture(GL_TEXTURE_2D, tex.prop.texture);      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, temp->w, temp->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp->pixels);      SDL_FreeSurface( temp );      //cout << "Texture '" << file << "' loaded, gltexid: " << tex.prop.texture << endl;            return(TRUE);    }};#include "menu.cpp"#include "scoreboard.cpp"class object {  public:    GLfloat color[3];    GLfloat opacity;    GLfloat posx, posy;    GLfloat width,height;    GLuint dl; //opengl display list    bool active;    bool collide;    bool reflect;        textureClass tex;        object()    {      color[0]=1.0f;      color[1]=1.0f;      color[2]=1.0f;      opacity=1.0f;    }};class paddle_class : public object {  private:    GLfloat growspeed;    GLfloat destwidth;    GLfloat aspect; //så meget stiger højden i forhold til bredden    bool growing;      public:    textureClass *layerTex;    paddle_class ()    {      init();      growing=0;      growspeed=0.05f;      aspect = 0.2f;    }        void init()    {      width=0.1;      height=0.02;    }        void grow(GLfloat width)    {      destwidth = width;      growing = 1;    }        void draw()    {                if(growing)      {        GLfloat iy = growspeed * globalMilliTicks;        GLfloat ix = growspeed * (globalMilliTicks/aspect);                width += ix;        height += iy;                if(width >= destwidth)        {          width = destwidth;          height = aspect*destwidth;          growing=0;        }      }          glLoadIdentity();      glTranslatef( posx, posy, -3.0 );      glBindTexture(GL_TEXTURE_2D, tex.prop.texture);      glBegin( GL_QUADS );        glTexCoord2f(0.0f, 0.0f); glVertex3f(-width, height, 0.0f );        glTexCoord2f(1.0f, 0.0f); glVertex3f( width, height, 0.0f );        glTexCoord2f(1.0f, 0.99f); glVertex3f( width,-height, 0.0f );        glTexCoord2f(0.0f, 0.99f); glVertex3f(-width,-height, 0.0f );      glEnd( );                  //Hvis glue?      if(player.powerup[PO_GLUE])      {        glBindTexture(GL_TEXTURE_2D, layerTex[0].prop.texture);        glBegin( GL_QUADS );          glTexCoord2f(0.0f, 0.0f); glVertex3f(-width, height, 0.0f );          glTexCoord2f(1.0f, 0.0f); glVertex3f( width, height, 0.0f );          glTexCoord2f(1.0f, 0.99f); glVertex3f( width,-height, 0.0f );          glTexCoord2f(0.0f, 0.99f); glVertex3f(-width,-height, 0.0f );        glEnd( );      }      //Hvis aim      if(player.powerup[PO_AIM])      {        layerTex[2].play();        glBindTexture(GL_TEXTURE_2D, layerTex[2].prop.texture);        glBegin( GL_QUADS );          glTexCoord2f(layerTex[2].pos[0], layerTex[2].pos[1]); glVertex3f(-width,height, 0.0f );          glTexCoord2f(layerTex[2].pos[2], layerTex[2].pos[3]); glVertex3f( width,height, 0.0f );          glTexCoord2f(layerTex[2].pos[4], layerTex[2].pos[5]); glVertex3f( width,-height, 0.0f );          glTexCoord2f(layerTex[2].pos[6], layerTex[2].pos[7]); glVertex3f(-width,-height, 0.0f );        glEnd( );      }            //Hvis gun      if(player.powerup[PO_GUN])      {        layerTex[1].play();        glBindTexture(GL_TEXTURE_2D, layerTex[1].prop.texture);        glBegin( GL_QUADS );          glTexCoord2f(layerTex[1].pos[0], layerTex[1].pos[1]); glVertex3f(-width, height*4, 0.0f );          glTexCoord2f(layerTex[1].pos[2], layerTex[1].pos[3]); glVertex3f( width, height*4, 0.0f );          glTexCoord2f(layerTex[1].pos[4], layerTex[1].pos[5]-0.01); glVertex3f( width,height, 0.0f );          glTexCoord2f(layerTex[1].pos[6], layerTex[1].pos[7]-0.01); glVertex3f(-width,height, 0.0f );        glEnd( );      }                }};//nasty fix to a problemint nbrick[23][26];class brick : public object {  public:  int score; //Hvor meget gir den  bool destroytowin; // Skal den smadres for at man kan vinde?  char powerup;  char type;  GLfloat fade; //hvor meget brik   GLfloat fadespeed;  GLfloat zoomspeed;  GLfloat zoom;  bool isdyingnormally;  bool isexploding; //springer den i luften  int row; //what row is this brick in  int bricknum; //brick in this row  int hitsLeft; //Hvor mange gange skal denne brik rammes før den dør?    bool n(int dir)  {    switch(dir)    {      case 0: //Er der en brik til venstre for dig?        if(bricknum > 0)        {          if(nbrick[row][bricknum-1] != -1)            return(1);        }        break;      case 1: //Er der en brik til højre for dig?        if(bricknum < 25) //26        {          if(nbrick[row][bricknum+1] != -1)            return(1);        }        break;      case 2: //Er der en brik Ovenpå dig        if(row > 0)        {          if(nbrick[row-1][bricknum] != -1)            return(1);        }        break;      case 3: //Er der en brik nedenunder dig        if(row < 22) //23        {          if(nbrick[row+1][bricknum] != -1)            return(1);        }        break;    }     // cout << " O hai" <<endl;    return(0);  }   void hit(effectManager & fxMan, pos poSpawnPos, pos poSpawnVel);    void draw(brick bricks[], effectManager & fxMan) {    if(isdyingnormally)    {      fade -= fadespeed * globalMilliTicks;      opacity = fade;      zoom -= zoomspeed * globalMilliTicks;            if(fade < 0.0)          active=0;    }        if(isexploding && !var.paused)    {      fade -= 7.0 * globalMilliTicks;      opacity = fade;      if(fade<0.0)      {        active=0;                pos spos,svel;        spos.x=posx;        spos.y=posy;        if(bricknum > 0)        {          if(nbrick[row][bricknum-1] != -1)          {            svel.x=rndflt(2,0)/3.0;            svel.y=rndflt(2,0)/3.0;            bricks[nbrick[row][bricknum-1]].hit(fxMan,spos,svel);          }        }                        if(bricknum < 25)        {          if(nbrick[row][bricknum+1] != -1)

⌨️ 快捷键说明

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