diff --git a/2DGame.cbp b/2DGame.cbp new file mode 100644 index 0000000..6cd97b5 --- /dev/null +++ b/2DGame.cbp @@ -0,0 +1,71 @@ + + + + + + diff --git a/2DGame.depend b/2DGame.depend new file mode 100644 index 0000000..21071bc --- /dev/null +++ b/2DGame.depend @@ -0,0 +1,347 @@ +# depslib dependency file v1.0 +1381068964 source:d:\cbprogram\2dgame\rgbpixmap.cpp + "RGBpixmap.h" + +1475583208 d:\cbprogram\2dgame\rgbpixmap.h + + + + + + + + + + +1301111162 c:\program files\codeblocks\mingw\include\windows.h + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1301111162 c:\program files\codeblocks\mingw\include\winresrc.h + + + + + + + +1301111164 c:\program files\codeblocks\mingw\include\winuser.h + +1301111162 c:\program files\codeblocks\mingw\include\winnt.h + + + + + + +1301111162 c:\program files\codeblocks\mingw\include\winerror.h + +1341010376 c:\program files\codeblocks\mingw\include\string.h + <_mingw.h> + + +1341010370 c:\program files\codeblocks\mingw\include\_mingw.h + +1301111148 c:\program files\codeblocks\mingw\include\basetsd.h + +1301111156 c:\program files\codeblocks\mingw\include\pshpack4.h + +1301111156 c:\program files\codeblocks\mingw\include\poppack.h + +1301111164 c:\program files\codeblocks\mingw\include\winver.h + +1301111148 c:\program files\codeblocks\mingw\include\dde.h + +1301111150 c:\program files\codeblocks\mingw\include\dlgs.h + +1301111148 c:\program files\codeblocks\mingw\include\commctrl.h + + +1301111156 c:\program files\codeblocks\mingw\include\prsht.h + +1301111162 c:\program files\codeblocks\mingw\include\windef.h + + +1301111162 c:\program files\codeblocks\mingw\include\wincon.h + +1301111160 c:\program files\codeblocks\mingw\include\winbase.h + +1301111162 c:\program files\codeblocks\mingw\include\wingdi.h + +1301111162 c:\program files\codeblocks\mingw\include\winnls.h + +1301111162 c:\program files\codeblocks\mingw\include\winnetwk.h + +1301111162 c:\program files\codeblocks\mingw\include\winreg.h + +1301111164 c:\program files\codeblocks\mingw\include\winsvc.h + +1301111148 c:\program files\codeblocks\mingw\include\cderr.h + +1301111148 c:\program files\codeblocks\mingw\include\ddeml.h + +1301111150 c:\program files\codeblocks\mingw\include\imm.h + +1301111154 c:\program files\codeblocks\mingw\include\lzexpand.h + +1301111154 c:\program files\codeblocks\mingw\include\mmsystem.h + +1301111154 c:\program files\codeblocks\mingw\include\nb30.h + +1301111158 c:\program files\codeblocks\mingw\include\rpc.h + + + + + + + +1301111158 c:\program files\codeblocks\mingw\include\rpcdce.h + + + +1301111148 c:\program files\codeblocks\mingw\include\basetyps.h + +1301111158 c:\program files\codeblocks\mingw\include\rpcdcep.h + +1301111158 c:\program files\codeblocks\mingw\include\rpcnsi.h + +1301111158 c:\program files\codeblocks\mingw\include\rpcnterr.h + +1341010372 c:\program files\codeblocks\mingw\include\excpt.h + <_mingw.h> + + +1301111160 c:\program files\codeblocks\mingw\include\shellapi.h + + + +1301111156 c:\program files\codeblocks\mingw\include\pshpack2.h + +1301111162 c:\program files\codeblocks\mingw\include\winperf.h + +1301111148 c:\program files\codeblocks\mingw\include\commdlg.h + + + +1301111160 c:\program files\codeblocks\mingw\include\unknwn.h + + + + +1301111156 c:\program files\codeblocks\mingw\include\objfwd.h + + +1301111164 c:\program files\codeblocks\mingw\include\wtypes.h + + + +1301111158 c:\program files\codeblocks\mingw\include\rpcndr.h + + + +1301111158 c:\program files\codeblocks\mingw\include\rpcnsip.h + +1301111162 c:\program files\codeblocks\mingw\include\winspool.h + +1301111162 c:\program files\codeblocks\mingw\include\winsock2.h + + +1301111162 c:\program files\codeblocks\mingw\include\winsock.h + + + +1301111154 c:\program files\codeblocks\mingw\include\mswsock.h + +1301111156 c:\program files\codeblocks\mingw\include\ole2.h + + + + + + +1301111154 c:\program files\codeblocks\mingw\include\objbase.h + + + + + + + + + +1341010376 c:\program files\codeblocks\mingw\include\stdlib.h + <_mingw.h> + + +1301111156 c:\program files\codeblocks\mingw\include\objidl.h + + +1301111148 c:\program files\codeblocks\mingw\include\cguid.h + +1301111156 c:\program files\codeblocks\mingw\include\olectlid.h + +1301111156 c:\program files\codeblocks\mingw\include\oleauto.h + + +1301111154 c:\program files\codeblocks\mingw\include\oaidl.h + + + +1301111156 c:\program files\codeblocks\mingw\include\oleidl.h + + +976692172 c:\program files\codeblocks\mingw\include\gl\glut.h + + + + +1301111164 c:\program files\codeblocks\mingw\include\gl\gl.h + +1301111164 c:\program files\codeblocks\mingw\include\gl\glu.h + + + +1341010370 c:\program files\codeblocks\mingw\include\assert.h + <_mingw.h> + +1341010374 c:\program files\codeblocks\mingw\include\math.h + <_mingw.h> + +1476104937 source:d:\cbprogram\2dgame\main.cpp + + + + + + + + "RGBpixmap.h" + "creature.h" + "player.h" + "enemy.h" + "bullet.h" + "power.h" + +1341010376 c:\program files\codeblocks\mingw\include\time.h + <_mingw.h> + + +1476037620 source:d:\cbprogram\2dgame\bullet.cpp + "bullet.h" + +1476037620 d:\cbprogram\2dgame\bullet.h + "creature.h" + +1476104896 source:d:\cbprogram\2dgame\enemy.cpp + "enemy.h" + +1476097036 d:\cbprogram\2dgame\enemy.h + "creature.h" + +1476035144 d:\cbprogram\2dgame\player.h + "creature.h" + +1475952840 source:d:\cbprogram\2dgame\player.cpp + + "player.h" + "RGBpixmap.h" + +1475933581 d:\cbprogram\2dgame\creature.h + + "RGBpixmap.h" + +1475930948 source:d:\cbprogram\2dgame\creature.cpp + "creature.h" + +1476096993 d:\cbprogram\2dgame\power.h + "creature.h" + +1476105092 source:d:\cbprogram\2dgame\power.cpp + "power.h" + +1476037620 source:d:\cbprogram\hw1\bullet.cpp + "bullet.h" + +1476037620 d:\cbprogram\hw1\bullet.h + "creature.h" + +1475933581 d:\cbprogram\hw1\creature.h + + "RGBpixmap.h" + +1475583208 d:\cbprogram\hw1\rgbpixmap.h + + + + + + + + + + +1476104896 source:d:\cbprogram\hw1\enemy.cpp + "enemy.h" + +1476097036 d:\cbprogram\hw1\enemy.h + "creature.h" + +1476109683 source:d:\cbprogram\hw1\main.cpp + + + + + + + + "RGBpixmap.h" + "creature.h" + "player.h" + "enemy.h" + "bullet.h" + "power.h" + +1476035144 d:\cbprogram\hw1\player.h + "creature.h" + +1476096993 d:\cbprogram\hw1\power.h + "creature.h" + +1475952840 source:d:\cbprogram\hw1\player.cpp + + "player.h" + "RGBpixmap.h" + +1381068964 source:d:\cbprogram\hw1\rgbpixmap.cpp + "RGBpixmap.h" + +1476105092 source:d:\cbprogram\hw1\power.cpp + "power.h" + diff --git a/2DGame.layout b/2DGame.layout new file mode 100644 index 0000000..358954b --- /dev/null +++ b/2DGame.layout @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RGBpixmap.cpp b/RGBpixmap.cpp index 77ee8f1..a2a8e4a 100644 --- a/RGBpixmap.cpp +++ b/RGBpixmap.cpp @@ -1,170 +1,170 @@ -// RGBpixmap.cpp - routines to read a BMP file -#include "RGBpixmap.h" -typedef unsigned short ushort; -typedef unsigned long ulong; - -//<<<<<<<<<<<<<<<<<<<<< getShort >>>>>>>>>>>>>>>>>>>> -inline ushort getShort(fstream &inf) //helper function -{ //BMP format uses little-endian integer types - // get a 2-byte integer stored in little-endian form - char ic; - ushort ip; - inf.get(ic); ip = ic; //first byte is little one - inf.get(ic); ip |= ((ushort)ic << 8); // or in high order byte - - return ip; -} -//<<<<<<<<<<<<<<<<<<<< getLong >>>>>>>>>>>>>>>>>>> -ulong getLong(fstream &inf) //helper function -{ //BMP format uses little-endian integer types - // get a 4-byte integer stored in little-endian form - ulong ip = 0; - char ic = 0; - unsigned char uc = ic; - inf.get(ic); uc = ic; ip = uc; - inf.get(ic); uc = ic; ip |=((ulong)uc << 8); - inf.get(ic); uc = ic; ip |=((ulong)uc << 16); - inf.get(ic); uc = ic; ip |=((ulong)uc << 24); - - return ip; -} - -/* -------------------------------------------------------------------- - RGBPixmap: - -------------------------------------------------------------------- */ - - -//<<<<<<<<<<<<<<<<<< RGBPixmap:: readBmpFile>>>>>>>>>>>>> -int RGBpixmap::readBMPFile(string fname) -{ // Read into memory an mRGB image from an uncompressed BMP file. - // return 0 on failure, 1 on success - fstream inf; - inf.open(fname.c_str(), ios::in|ios::binary); //read binary char's - if(!inf){ cout << " can't open file: " << fname << endl; return 0;} - int k, row, col, numPadBytes, nBytesInRow; - // read the file header information - char ch1, ch2; - inf.get(ch1); inf.get(ch2); //type: always 'BM' - ulong fileSize = getLong(inf); - ushort reserved1 = getShort(inf); // always 0 - ushort reserved2= getShort(inf); // always 0 - ulong offBits = getLong(inf); // offset to image - unreliable - ulong headerSize = getLong(inf); // always 40 - ulong numCols = getLong(inf); // number of columns in image - ulong numRows = getLong(inf); // number of rows in image - ushort planes= getShort(inf); // always 1 - ushort bitsPerPixel= getShort(inf); //8 or 24; allow 24 here - ulong compression = getLong(inf); // must be 0 for uncompressed - ulong imageSize = getLong(inf); // total bytes in image - ulong xPels = getLong(inf); // always 0 - ulong yPels = getLong(inf); // always 0 - ulong numLUTentries = getLong(inf); // 256 for 8 bit, otherwise 0 - ulong impColors = getLong(inf); // always 0 - if(bitsPerPixel != 24) - { // error - must be a 24 bit uncompressed image - cout << "not a 24 bit/pixelimage, or is compressed!\n"; - inf.close(); return 0; - } - //add bytes at end of each row so total # is a multiple of 4 - // round up 3*numCols to next mult. of 4 - nBytesInRow = ((3 * numCols + 3)/4) * 4; - numPadBytes = nBytesInRow - 3 * numCols; // need this many - nRows = numRows; // set class's data members - nCols = numCols; - pixel = new mRGB[nRows * nCols]; //make space for array - if(!pixel) return 0; // out of memory! - long count = 0; - char dum; - char *buffer= new char[nCols*3]; - for(row = 0; row < nRows; row++) // read pixel values - { - inf.read(buffer, nCols*3); - for(col = 0; col < nCols; col++) - { - //char r,g,b; - //inf.get(b); inf.get(g); inf.get(r); //read bytes - pixel[count].r = buffer[3*col+2]; //place them in colors - pixel[count].g = buffer[3*col+1]; - pixel[count++].b = buffer[3*col]; - } - for(k = 0; k < numPadBytes ; k++) //skip pad bytes at row's end - inf >> dum; - } - - delete [] buffer; - inf.close(); - return 1; // success -} - - -/* -------------------------------------------------------------------- - RGBAPixmap: - -------------------------------------------------------------------- */ - - -//RGBAPixmap:: readBmpFile -bool RGBApixmap:: readBMPFile(string fname) -{ // Read into memory an mRGB image from an uncompressed BMP file. - // return 0 on failure, 1 on success - fstream inf; - inf.open(fname.c_str(), ios::in|ios::binary); //read binary char's - if(!inf) { - cout << " can't open file: " << fname << endl; return 0; - } - - int k, row, col, numPadBytes, nBytesInRow; - // read the file header information - char ch1, ch2; - inf.get(ch1); inf.get(ch2); //type: always 'BM' - ulong fileSize = getLong(inf); - ushort reserved1 = getShort(inf); // always 0 - ushort reserved2= getShort(inf); // always 0 - ulong offBits = getLong(inf); // offset to image - unreliable - ulong headerSize = getLong(inf); // always 40 - ulong numCols = getLong(inf); // number of columns in image - ulong numRows = getLong(inf); // number of rows in image - ushort planes= getShort(inf); // always 1 - ushort bitsPerPixel= getShort(inf); //8 or 24; allow 24 here - ulong compression = getLong(inf); // must be 0 for uncompressed - ulong imageSize = getLong(inf); // total bytes in image - ulong xPels = getLong(inf); // always 0 - ulong yPels = getLong(inf); // always 0 - ulong numLUTentries = getLong(inf); // 256 for 8 bit, otherwise 0 - ulong impColors = getLong(inf); // always 0 - if(bitsPerPixel != 24) - { // error - must be a 24 bit uncompressed image - cout << "not a 24 bit/pixelimage, or is compressed!\n"; - inf.close(); return false; - } - //add bytes at end of each row so total # is a multiple of 4 - // round up 3*numCols to next mult. of 4 - nBytesInRow = ((3 * numCols + 3)/4) * 4; - numPadBytes = nBytesInRow - 3 * numCols; // need this many - nRows = numRows; // set class's data members - nCols = numCols; - pixel = new mRGBA[nRows * nCols]; //make space for array - if(!pixel) return 0; // out of memory! - long count = 0; - char dum; - char *buffer= new char[nCols*3]; - for(row = 0; row < nRows; row++) // read pixel values - { - inf.read(buffer, nCols*3); - for(col = 0; col < nCols; col++) - { - //char r,g,b; - //inf.get(b); inf.get(g); inf.get(r); //read bytes - pixel[count].r = buffer[3*col+2]; //place them in colors - pixel[count].g = buffer[3*col+1]; - pixel[count++].b = buffer[3*col]; - } - for(k = 0; k < numPadBytes ; k++) //skip pad bytes at row's end - inf >> dum; - } - - needUpdateTex = true; - - delete [] buffer; - inf.close(); - return true; // success +// RGBpixmap.cpp - routines to read a BMP file +#include "RGBpixmap.h" +typedef unsigned short ushort; +typedef unsigned long ulong; + +//<<<<<<<<<<<<<<<<<<<<< getShort >>>>>>>>>>>>>>>>>>>> +inline ushort getShort(fstream &inf) //helper function +{ //BMP format uses little-endian integer types + // get a 2-byte integer stored in little-endian form + char ic; + ushort ip; + inf.get(ic); ip = ic; //first byte is little one + inf.get(ic); ip |= ((ushort)ic << 8); // or in high order byte + + return ip; +} +//<<<<<<<<<<<<<<<<<<<< getLong >>>>>>>>>>>>>>>>>>> +ulong getLong(fstream &inf) //helper function +{ //BMP format uses little-endian integer types + // get a 4-byte integer stored in little-endian form + ulong ip = 0; + char ic = 0; + unsigned char uc = ic; + inf.get(ic); uc = ic; ip = uc; + inf.get(ic); uc = ic; ip |=((ulong)uc << 8); + inf.get(ic); uc = ic; ip |=((ulong)uc << 16); + inf.get(ic); uc = ic; ip |=((ulong)uc << 24); + + return ip; +} + +/* -------------------------------------------------------------------- + RGBPixmap: + -------------------------------------------------------------------- */ + + +//<<<<<<<<<<<<<<<<<< RGBPixmap:: readBmpFile>>>>>>>>>>>>> +int RGBpixmap::readBMPFile(string fname) +{ // Read into memory an mRGB image from an uncompressed BMP file. + // return 0 on failure, 1 on success + fstream inf; + inf.open(fname.c_str(), ios::in|ios::binary); //read binary char's + if(!inf){ cout << " can't open file: " << fname << endl; return 0;} + int k, row, col, numPadBytes, nBytesInRow; + // read the file header information + char ch1, ch2; + inf.get(ch1); inf.get(ch2); //type: always 'BM' + ulong fileSize = getLong(inf); + ushort reserved1 = getShort(inf); // always 0 + ushort reserved2= getShort(inf); // always 0 + ulong offBits = getLong(inf); // offset to image - unreliable + ulong headerSize = getLong(inf); // always 40 + ulong numCols = getLong(inf); // number of columns in image + ulong numRows = getLong(inf); // number of rows in image + ushort planes= getShort(inf); // always 1 + ushort bitsPerPixel= getShort(inf); //8 or 24; allow 24 here + ulong compression = getLong(inf); // must be 0 for uncompressed + ulong imageSize = getLong(inf); // total bytes in image + ulong xPels = getLong(inf); // always 0 + ulong yPels = getLong(inf); // always 0 + ulong numLUTentries = getLong(inf); // 256 for 8 bit, otherwise 0 + ulong impColors = getLong(inf); // always 0 + if(bitsPerPixel != 24) + { // error - must be a 24 bit uncompressed image + cout << "not a 24 bit/pixelimage, or is compressed!\n"; + inf.close(); return 0; + } + //add bytes at end of each row so total # is a multiple of 4 + // round up 3*numCols to next mult. of 4 + nBytesInRow = ((3 * numCols + 3)/4) * 4; + numPadBytes = nBytesInRow - 3 * numCols; // need this many + nRows = numRows; // set class's data members + nCols = numCols; + pixel = new mRGB[nRows * nCols]; //make space for array + if(!pixel) return 0; // out of memory! + long count = 0; + char dum; + char *buffer= new char[nCols*3]; + for(row = 0; row < nRows; row++) // read pixel values + { + inf.read(buffer, nCols*3); + for(col = 0; col < nCols; col++) + { + //char r,g,b; + //inf.get(b); inf.get(g); inf.get(r); //read bytes + pixel[count].r = buffer[3*col+2]; //place them in colors + pixel[count].g = buffer[3*col+1]; + pixel[count++].b = buffer[3*col]; + } + for(k = 0; k < numPadBytes ; k++) //skip pad bytes at row's end + inf >> dum; + } + + delete [] buffer; + inf.close(); + return 1; // success +} + + +/* -------------------------------------------------------------------- + RGBAPixmap: + -------------------------------------------------------------------- */ + + +//RGBAPixmap:: readBmpFile +bool RGBApixmap:: readBMPFile(string fname) +{ // Read into memory an mRGB image from an uncompressed BMP file. + // return 0 on failure, 1 on success + fstream inf; + inf.open(fname.c_str(), ios::in|ios::binary); //read binary char's + if(!inf) { + cout << " can't open file: " << fname << endl; return 0; + } + + int k, row, col, numPadBytes, nBytesInRow; + // read the file header information + char ch1, ch2; + inf.get(ch1); inf.get(ch2); //type: always 'BM' + ulong fileSize = getLong(inf); + ushort reserved1 = getShort(inf); // always 0 + ushort reserved2= getShort(inf); // always 0 + ulong offBits = getLong(inf); // offset to image - unreliable + ulong headerSize = getLong(inf); // always 40 + ulong numCols = getLong(inf); // number of columns in image + ulong numRows = getLong(inf); // number of rows in image + ushort planes= getShort(inf); // always 1 + ushort bitsPerPixel= getShort(inf); //8 or 24; allow 24 here + ulong compression = getLong(inf); // must be 0 for uncompressed + ulong imageSize = getLong(inf); // total bytes in image + ulong xPels = getLong(inf); // always 0 + ulong yPels = getLong(inf); // always 0 + ulong numLUTentries = getLong(inf); // 256 for 8 bit, otherwise 0 + ulong impColors = getLong(inf); // always 0 + if(bitsPerPixel != 24) + { // error - must be a 24 bit uncompressed image + cout << "not a 24 bit/pixelimage, or is compressed!\n"; + inf.close(); return false; + } + //add bytes at end of each row so total # is a multiple of 4 + // round up 3*numCols to next mult. of 4 + nBytesInRow = ((3 * numCols + 3)/4) * 4; + numPadBytes = nBytesInRow - 3 * numCols; // need this many + nRows = numRows; // set class's data members + nCols = numCols; + pixel = new mRGBA[nRows * nCols]; //make space for array + if(!pixel) return 0; // out of memory! + long count = 0; + char dum; + char *buffer= new char[nCols*3]; + for(row = 0; row < nRows; row++) // read pixel values + { + inf.read(buffer, nCols*3); + for(col = 0; col < nCols; col++) + { + //char r,g,b; + //inf.get(b); inf.get(g); inf.get(r); //read bytes + pixel[count].r = buffer[3*col+2]; //place them in colors + pixel[count].g = buffer[3*col+1]; + pixel[count++].b = buffer[3*col]; + } + for(k = 0; k < numPadBytes ; k++) //skip pad bytes at row's end + inf >> dum; + } + + needUpdateTex = true; + + delete [] buffer; + inf.close(); + return true; // success } \ No newline at end of file diff --git a/RGBpixmap.h b/RGBpixmap.h index 49fa015..2b1d5ae 100644 --- a/RGBpixmap.h +++ b/RGBpixmap.h @@ -1,367 +1,367 @@ -// RGBpixmap.h: a class to support working with RGB pixmaps. -#ifndef _RGBPIXMAP -#define _RGBPIXMAP - -#ifdef __APPLE__ -#include -#else -#include -#include -#endif - -#include -#include -#include - -using namespace std; - -#include -#include -#include - -typedef unsigned char uchar; - -class mRGB{ // the name RGB is already used by Windows -public: - uchar r, g, b; - - mRGB(){r = g = b = 0;} - mRGB(mRGB& p){r = p.r; g = p.g; b = p.b;} - mRGB(uchar rr, uchar gg, uchar bb){r = rr; g = gg; b = bb;} - - void set(uchar rr, uchar gg, uchar bb){r = rr; g = gg; b = bb;} -}; - -class mRGBA{ -public: - uchar r, g, b, a; - - mRGBA(){r = g = b = 0; a = 255;} - mRGBA(mRGBA& p){r = p.r; g = p.g; b = p.b; a = p.a;} - mRGBA(uchar rr, uchar gg, uchar bb, uchar aa){r = rr; g = gg; b = bb; a = aa;} - - void set(uchar rr, uchar gg, uchar bb, uchar aa){r = rr; g = gg; b = bb; a = aa;} -}; - -//@@@@@@@@@@@@@@@@@@ IntPoint class @@@@@@@@@@@@@@@@ -class IntPoint{ // for 2D points with integer coordinates -public: - int x, y; - - IntPoint(int xx, int yy){x = xx; y = yy;} - IntPoint(){ x = y = 0;} - - void set(int dx, int dy){x = dx; y = dy;} - void set(IntPoint& p){ x = p.x; y = p.y;} -}; - -// @@@@@@@@@@@@@@@@@@@@@@@@ IntRect class @@@@@@@@@@@@@@@@@@@@ -class IntRect{ // a rectangle with integer border values -public: - int left, top, right, bott; - - IntRect(){left = top = right = bott = 0;} - IntRect(int l, int t, int r, int b) {left = l; top = t; right = r; bott = b;} - - void set(int l, int t, int r, int b) {left = l; top = t; right = r; bott = b;} - void set(IntRect& r) {left = r.left; top = r.top; right = r.right; bott = r.bott;} -}; - -//$$$$$$$$$$$$$$$$$ RGBPixmap class $$$$$$$$$$$$$$$ -class RGBpixmap { -private: - mRGB* pixel; // array of pixels - -public: - int nRows, nCols; // dimensions of the pixmap. nCols: width, nRows: height - int w() const { return nCols;} - int h() const { return nRows;} - - RGBpixmap() {nRows = nCols = 0; pixel = 0;} - RGBpixmap(int rows, int cols) //constructor - { - nRows = rows; - nCols = cols; - pixel = new mRGB[rows*cols]; - } - int readBMPFile(string fname); // read BMP file into this pixmap - void freeIt() // give back memory for this pixmap - { - delete []pixel; nRows = nCols = 0; - } - //<<<<<<<<<<<<<<<<<< copy >>>>>>>>>>>>>>>>>>> - void copy(IntPoint from, IntPoint to, int x, int y, int width, int height) - { // copy a region of the display back onto the display - if(nRows == 0 || nCols == 0) return; - glCopyPixels(x, y, width, height,GL_COLOR); - } - //<<<<<<<<<<<<<<<<<<< draw >>>>>>>>>>>>>>>>> - void draw() - { // draw this pixmap at current raster position - if(nRows == 0 || nCols == 0) return; - //tell OpenGL: dont align pixels to 4 byte boundaries in memory - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glDrawPixels(nCols, nRows,GL_RGB, GL_UNSIGNED_BYTE,pixel); - } - - //<<<<<<<<<<<<<<<<< read >>>>>>>>>>>>>>>> - int read(int x, int y, int wid, int ht) - { // read a rectangle of pixels into this pixmap - nRows = ht; - nCols = wid; - delete []pixel; - pixel = new mRGB[nRows *nCols]; - if(!pixel) return -1; - //tell OpenGL: dont align pixels to 4 byte boundaries in memory - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(x, y, nCols, nRows, GL_RGB,GL_UNSIGNED_BYTE, pixel); - return 0; - } - //<<<<<<<<<<<<<<<<< read from IntRect >>>>>>>>>>>>>>>> - int read(IntRect r) - { // read a rectangle of pixels into this pixmap - nRows = r.top - r.bott; - nCols = r.right - r.left; - delete []pixel; - pixel = new mRGB[nRows *nCols]; if(!pixel) return -1; - //tell OpenGL: dont align pixels to 4 byte boundaries in memory - glPixelStorei(GL_PACK_ALIGNMENT,1); - glReadPixels(r.left,r.bott, nCols, nRows, GL_RGB, GL_UNSIGNED_BYTE, pixel); - return 0; - } - //<<<<<<<<<<<<<< setPixel >>>>>>>>>>>>> - void setPixel(int x, int y, mRGB color) - { - if(x>=0 && x =0 && y < nRows) - pixel[nCols * y + x] = color; - } - //<<<<<<<<<<<<<<<< getPixel >>>>>>>>>>> - mRGB getPixel(int x, int y) - { - mRGB bad(255,255,255); - assert(x >= 0 && x < nCols); - assert(y >= 0 && y < nRows); - return pixel[nCols * y + x]; - } -}; //end of class RGBpixmap - - -//RGBAPixmap class -class RGBApixmap { -private: - mRGBA* pixel; // array of pixels - GLuint textureId; - bool needUpdateTex; //Need update to texture? - -public: - int nRows, nCols; // dimensions of the pixmap. nCols: width, nRows: height - int w() const { return nCols;} - int h() const { return nRows;} - - - RGBApixmap() { nRows = nCols = 0; pixel = NULL; textureId = 0; needUpdateTex=false;} - RGBApixmap(int rows, int cols) //constructor - { - nRows = rows; - nCols = cols; - pixel = new mRGBA[rows*cols]; - - textureId = 0; - needUpdateTex = true; - } - - ~RGBApixmap() { - delete []pixel; - // should we clear texture object?? - if(!glIsTexture(textureId)) { - glDeleteTextures(1, &textureId); - } - } - - RGBApixmap(const RGBApixmap & pixmap) { - nRows = pixmap.nRows; - nCols = pixmap.nCols; - pixel = new mRGBA[nRows*nCols]; - memcpy(pixel, pixmap.pixel, sizeof(mRGBA) * nRows * nCols); - textureId = pixmap.textureId; - needUpdateTex = true; - } - - bool readBMPFile(string fname); // read BMP file into this pixmap - - void freeIt() // give back memory for this pixmap - { - delete []pixel; - nRows = nCols = 0; - } - - // copy a region of the display back onto the display - void copy(IntPoint from, IntPoint to, int x, int y, int width, int height) - { - if(nRows == 0 || nCols == 0) return; - glCopyPixels(x, y, width, height, GL_COLOR); - } - - void setTexture() { - if(!glIsTexture(textureId)) { - glGenTextures(1, &textureId); - } - glBindTexture(GL_TEXTURE_2D, textureId); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nCols, nRows, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel); - - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_NEAREST - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR - - needUpdateTex = false; - } - - //blend - void blend() - { - if(nRows == 0 || nCols == 0) return; - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - - //tell OpenGL: donot align pixels to 4 byte boundaries in memory - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - glDrawPixels(nCols, nRows,GL_RGBA, GL_UNSIGNED_BYTE, pixel); - - glDisable(GL_BLEND); - } - - void blendTex(int x, int y, float scalex=1.0f, float scaley=1.0f) - { - if(nRows == 0 || nCols == 0) return; - if(needUpdateTex) - setTexture(); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, textureId); - glPushMatrix(); - glTranslatef(x, y, 0); - glScalef(scalex, scaley, 1); - glBegin(GL_QUADS); - glTexCoord2f(0, 0); glVertex2f(0, 0); - glTexCoord2f(1, 0); glVertex2f(nCols, 0); - glTexCoord2f(1, 1); glVertex2f(nCols, nRows); - glTexCoord2f(0, 1); glVertex2f(0, nRows); - glEnd(); - glPopMatrix(); - glDisable(GL_TEXTURE_2D); - - glDisable(GL_BLEND); - } - - //rotate along the center of image - void blendTexRotate(int x, int y, float scalex=1.0f, float scaley=1.0f, float angle=0.0f) - { - if(nRows == 0 || nCols == 0) return; - - if(needUpdateTex) - setTexture(); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, textureId); - glPushMatrix(); - glTranslatef(x+0.5*w(), y+0.5*h(), 0); - glRotatef(angle, 0, 0, 1); - glTranslatef(-0.5*w(), -0.5*h(), 0); - glScalef(scalex, scaley, 1); - glBegin(GL_QUADS); - glTexCoord2f(0, 0); glVertex2f(0, 0); - glTexCoord2f(1, 0); glVertex2f(nCols, 0); - glTexCoord2f(1, 1); glVertex2f(nCols, nRows); - glTexCoord2f(0, 1); glVertex2f(0, nRows); - glEnd(); - glPopMatrix(); - glDisable(GL_TEXTURE_2D); - - glDisable(GL_BLEND); - } - - // draw this pixmap at current raster position - void draw() - { - if(nRows == 0 || nCols == 0) return; - - //tell OpenGL: donot align pixels to 4 byte boundaries in memory - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glDrawPixels(nCols, nRows,GL_RGBA, GL_UNSIGNED_BYTE, pixel); - } - - // read a rectangle of pixels into this pixmap - int read(int x, int y, int wid, int ht) - { - nRows = ht; - nCols = wid; - delete []pixel; - pixel = new mRGBA[nRows *nCols]; - if(!pixel) return -1; - //tell OpenGL: donot align pixels to 4 byte boundaries in memory - glPixelStorei(GL_PACK_ALIGNMENT,1); - glReadPixels(x, y, nCols, nRows, GL_RGBA,GL_UNSIGNED_BYTE,pixel); - needUpdateTex = true; - return 0; - } - - // read a rectangle of pixels into this pixmap - int read(IntRect r) - { - nRows = r.top - r.bott; - nCols = r.right - r.left; - delete []pixel; - pixel = new mRGBA[nRows *nCols]; if(!pixel) return -1; - //tell OpenGL: donot align pixels to 4 byte boundaries in memory - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(r.left,r.bott, nCols, nRows, GL_RGBA, GL_UNSIGNED_BYTE, pixel); - needUpdateTex = true; - return 0; - } - - // - void setChromaKey(uchar rr, uchar gg, uchar bb) - { - int count=0; - mRGBA temp; - for(int row=0; row>>>>>>>>>>>> - void setPixel(int x, int y, mRGBA color) - { - if(x>=0 && x =0 && y < nRows) { - pixel[nCols * y + x] = color; - } - - needUpdateTex = true; - } - //<<<<<<<<<<<<<<<< getPixel >>>>>>>>>>> - mRGBA getPixel(int x, int y) - { - mRGBA bad(255,255,255,255); - assert(x >= 0 && x < nCols); - assert(y >= 0 && y < nRows); - return pixel[nCols * y + x]; - } -}; //end of class RGBApixmap - -#endif +// RGBpixmap.h: a class to support working with RGB pixmaps. +#ifndef _RGBPIXMAP +#define _RGBPIXMAP + +#ifdef __APPLE__ +#include +#else +#include +#include +#endif + +#include +#include +#include + +using namespace std; + +#include +#include +#include + +typedef unsigned char uchar; + +class mRGB{ // the name RGB is already used by Windows +public: + uchar r, g, b; + + mRGB(){r = g = b = 0;} + mRGB(mRGB& p){r = p.r; g = p.g; b = p.b;} + mRGB(uchar rr, uchar gg, uchar bb){r = rr; g = gg; b = bb;} + + void set(uchar rr, uchar gg, uchar bb){r = rr; g = gg; b = bb;} +}; + +class mRGBA{ +public: + uchar r, g, b, a; + + mRGBA(){r = g = b = 0; a = 255;} + mRGBA(mRGBA& p){r = p.r; g = p.g; b = p.b; a = p.a;} + mRGBA(uchar rr, uchar gg, uchar bb, uchar aa){r = rr; g = gg; b = bb; a = aa;} + + void set(uchar rr, uchar gg, uchar bb, uchar aa){r = rr; g = gg; b = bb; a = aa;} +}; + +//@@@@@@@@@@@@@@@@@@ IntPoint class @@@@@@@@@@@@@@@@ +class IntPoint{ // for 2D points with integer coordinates +public: + int x, y; + + IntPoint(int xx, int yy){x = xx; y = yy;} + IntPoint(){ x = y = 0;} + + void set(int dx, int dy){x = dx; y = dy;} + void set(IntPoint& p){ x = p.x; y = p.y;} +}; + +// @@@@@@@@@@@@@@@@@@@@@@@@ IntRect class @@@@@@@@@@@@@@@@@@@@ +class IntRect{ // a rectangle with integer border values +public: + int left, top, right, bott; + + IntRect(){left = top = right = bott = 0;} + IntRect(int l, int t, int r, int b) {left = l; top = t; right = r; bott = b;} + + void set(int l, int t, int r, int b) {left = l; top = t; right = r; bott = b;} + void set(IntRect& r) {left = r.left; top = r.top; right = r.right; bott = r.bott;} +}; + +//$$$$$$$$$$$$$$$$$ RGBPixmap class $$$$$$$$$$$$$$$ +class RGBpixmap { +private: + mRGB* pixel; // array of pixels + +public: + int nRows, nCols; // dimensions of the pixmap. nCols: width, nRows: height + int w() const { return nCols;} + int h() const { return nRows;} + + RGBpixmap() {nRows = nCols = 0; pixel = 0;} + RGBpixmap(int rows, int cols) //constructor + { + nRows = rows; + nCols = cols; + pixel = new mRGB[rows*cols]; + } + int readBMPFile(string fname); // read BMP file into this pixmap + void freeIt() // give back memory for this pixmap + { + delete []pixel; nRows = nCols = 0; + } + //<<<<<<<<<<<<<<<<<< copy >>>>>>>>>>>>>>>>>>> + void copy(IntPoint from, IntPoint to, int x, int y, int width, int height) + { // copy a region of the display back onto the display + if(nRows == 0 || nCols == 0) return; + glCopyPixels(x, y, width, height,GL_COLOR); + } + //<<<<<<<<<<<<<<<<<<< draw >>>>>>>>>>>>>>>>> + void draw() + { // draw this pixmap at current raster position + if(nRows == 0 || nCols == 0) return; + //tell OpenGL: dont align pixels to 4 byte boundaries in memory + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glDrawPixels(nCols, nRows,GL_RGB, GL_UNSIGNED_BYTE,pixel); + } + + //<<<<<<<<<<<<<<<<< read >>>>>>>>>>>>>>>> + int read(int x, int y, int wid, int ht) + { // read a rectangle of pixels into this pixmap + nRows = ht; + nCols = wid; + delete []pixel; + pixel = new mRGB[nRows *nCols]; + if(!pixel) return -1; + //tell OpenGL: dont align pixels to 4 byte boundaries in memory + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(x, y, nCols, nRows, GL_RGB,GL_UNSIGNED_BYTE, pixel); + return 0; + } + //<<<<<<<<<<<<<<<<< read from IntRect >>>>>>>>>>>>>>>> + int read(IntRect r) + { // read a rectangle of pixels into this pixmap + nRows = r.top - r.bott; + nCols = r.right - r.left; + delete []pixel; + pixel = new mRGB[nRows *nCols]; if(!pixel) return -1; + //tell OpenGL: dont align pixels to 4 byte boundaries in memory + glPixelStorei(GL_PACK_ALIGNMENT,1); + glReadPixels(r.left,r.bott, nCols, nRows, GL_RGB, GL_UNSIGNED_BYTE, pixel); + return 0; + } + //<<<<<<<<<<<<<< setPixel >>>>>>>>>>>>> + void setPixel(int x, int y, mRGB color) + { + if(x>=0 && x =0 && y < nRows) + pixel[nCols * y + x] = color; + } + //<<<<<<<<<<<<<<<< getPixel >>>>>>>>>>> + mRGB getPixel(int x, int y) + { + mRGB bad(255,255,255); + assert(x >= 0 && x < nCols); + assert(y >= 0 && y < nRows); + return pixel[nCols * y + x]; + } +}; //end of class RGBpixmap + + +//RGBAPixmap class +class RGBApixmap { +private: + mRGBA* pixel; // array of pixels + GLuint textureId; + bool needUpdateTex; //Need update to texture? + +public: + int nRows, nCols; // dimensions of the pixmap. nCols: width, nRows: height + int w() const { return nCols;} + int h() const { return nRows;} + + + RGBApixmap() { nRows = nCols = 0; pixel = NULL; textureId = 0; needUpdateTex=false;} + RGBApixmap(int rows, int cols) //constructor + { + nRows = rows; + nCols = cols; + pixel = new mRGBA[rows*cols]; + + textureId = 0; + needUpdateTex = true; + } + + ~RGBApixmap() { + delete []pixel; + // should we clear texture object?? + if(!glIsTexture(textureId)) { + glDeleteTextures(1, &textureId); + } + } + + RGBApixmap(const RGBApixmap & pixmap) { + nRows = pixmap.nRows; + nCols = pixmap.nCols; + pixel = new mRGBA[nRows*nCols]; + memcpy(pixel, pixmap.pixel, sizeof(mRGBA) * nRows * nCols); + textureId = pixmap.textureId; + needUpdateTex = true; + } + + bool readBMPFile(string fname); // read BMP file into this pixmap + + void freeIt() // give back memory for this pixmap + { + delete []pixel; + nRows = nCols = 0; + } + + // copy a region of the display back onto the display + void copy(IntPoint from, IntPoint to, int x, int y, int width, int height) + { + if(nRows == 0 || nCols == 0) return; + glCopyPixels(x, y, width, height, GL_COLOR); + } + + void setTexture() { + if(!glIsTexture(textureId)) { + glGenTextures(1, &textureId); + } + glBindTexture(GL_TEXTURE_2D, textureId); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nCols, nRows, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel); + + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_NEAREST + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR + + needUpdateTex = false; + } + + //blend + void blend() + { + if(nRows == 0 || nCols == 0) return; + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + + //tell OpenGL: donot align pixels to 4 byte boundaries in memory + glPixelStorei(GL_UNPACK_ALIGNMENT,1); + glDrawPixels(nCols, nRows,GL_RGBA, GL_UNSIGNED_BYTE, pixel); + + glDisable(GL_BLEND); + } + + void blendTex(float x, float y, float scalex=1.0f, float scaley=1.0f) + { + if(nRows == 0 || nCols == 0) return; + if(needUpdateTex) + setTexture(); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, textureId); + glPushMatrix(); + glTranslatef(x, y, 0); + glScalef(scalex, scaley, 1); + glBegin(GL_QUADS); + glTexCoord2f(0, 0); glVertex2f(0, 0); + glTexCoord2f(1, 0); glVertex2f(nCols, 0); + glTexCoord2f(1, 1); glVertex2f(nCols, nRows); + glTexCoord2f(0, 1); glVertex2f(0, nRows); + glEnd(); + glPopMatrix(); + glDisable(GL_TEXTURE_2D); + + glDisable(GL_BLEND); + } + + //rotate along the center of image + void blendTexRotate(int x, int y, float scalex=1.0f, float scaley=1.0f, float angle=0.5f) + { + if(nRows == 0 || nCols == 0) return; + + if(needUpdateTex) + setTexture(); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, textureId); + glPushMatrix(); + glTranslatef(x+0.5*w(), y+0.5*h(), 0); + glRotatef(angle, 0, 0, 1); + glTranslatef(-0.5*w(), -0.5*h(), 0); + glScalef(scalex, scaley, 1); + glBegin(GL_QUADS); + glTexCoord2f(0, 0); glVertex2f(0, 0); + glTexCoord2f(1, 0); glVertex2f(nCols, 0); + glTexCoord2f(1, 1); glVertex2f(nCols, nRows); + glTexCoord2f(0, 1); glVertex2f(0, nRows); + glEnd(); + glPopMatrix(); + glDisable(GL_TEXTURE_2D); + + glDisable(GL_BLEND); + } + + // draw this pixmap at current raster position + void draw() + { + if(nRows == 0 || nCols == 0) return; + + //tell OpenGL: donot align pixels to 4 byte boundaries in memory + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glDrawPixels(nCols, nRows,GL_RGBA, GL_UNSIGNED_BYTE, pixel); + } + + // read a rectangle of pixels into this pixmap + int read(int x, int y, int wid, int ht) + { + nRows = ht; + nCols = wid; + delete []pixel; + pixel = new mRGBA[nRows *nCols]; + if(!pixel) return -1; + //tell OpenGL: donot align pixels to 4 byte boundaries in memory + glPixelStorei(GL_PACK_ALIGNMENT,1); + glReadPixels(x, y, nCols, nRows, GL_RGBA,GL_UNSIGNED_BYTE,pixel); + needUpdateTex = true; + return 0; + } + + // read a rectangle of pixels into this pixmap + int read(IntRect r) + { + nRows = r.top - r.bott; + nCols = r.right - r.left; + delete []pixel; + pixel = new mRGBA[nRows *nCols]; if(!pixel) return -1; + //tell OpenGL: donot align pixels to 4 byte boundaries in memory + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(r.left,r.bott, nCols, nRows, GL_RGBA, GL_UNSIGNED_BYTE, pixel); + needUpdateTex = true; + return 0; + } + + // + void setChromaKey(uchar rr, uchar gg, uchar bb) + { + int count=0; + mRGBA temp; + for(int row=0; row>>>>>>>>>>>> + void setPixel(int x, int y, mRGBA color) + { + if(x>=0 && x =0 && y < nRows) { + pixel[nCols * y + x] = color; + } + + needUpdateTex = true; + } + //<<<<<<<<<<<<<<<< getPixel >>>>>>>>>>> + mRGBA getPixel(int x, int y) + { + mRGBA bad(255,255,255,255); + assert(x >= 0 && x < nCols); + assert(y >= 0 && y < nRows); + return pixel[nCols * y + x]; + } +}; //end of class RGBApixmap + +#endif diff --git a/bullet.cpp b/bullet.cpp new file mode 100644 index 0000000..8f587a0 --- /dev/null +++ b/bullet.cpp @@ -0,0 +1,64 @@ +#include "bullet.h" + +bullet::bullet() +{ + setXY(-100,-100); + setPic(1,1); + powerFlag=1; +} +bullet::bullet(double xx,double yy) +{ + setXY(xx,yy); + setPic(1,1); + powerFlag=1; +} +void bullet::setPic(int p,int flag) +{ + if(p==1) + { + switch(flag) + { + case 1: + pic.readBMPFile("image/bullet1_1.bmp"); + break; + case 2: + pic.readBMPFile("image/bullet1_2.bmp"); + break; + } + } + else if (p==2) + { + switch(flag) + { + case 1: + pic.readBMPFile("image/bullet2_1.bmp"); + break; + case 2: + pic.readBMPFile("image/bullet2_2.bmp"); + break; + } + } +} + +void bullet::action(){if(this->x!=-100)this->y+=4;} +void bullet::die(){x=-100,y=-100;} + +//pcbullet +pcbullet::pcbullet() +{ + setXY(-100,-100); + setPic(); +} +pcbullet::pcbullet(double xx,double yy) +{ + setXY(xx,yy); + setPic(); +} +void pcbullet::setPic() +{ + pic.readBMPFile("image/pcbullet.bmp"); +} + +void pcbullet::action(){if(x!=-100) y-=1;} + +void pcbullet::die(){x=-100,y=-100;} diff --git a/bullet.h b/bullet.h new file mode 100644 index 0000000..325ce78 --- /dev/null +++ b/bullet.h @@ -0,0 +1,24 @@ + +#include "creature.h" +class bullet : public creature{ + public: + int powerFlag; + bullet(); + bullet(double xx,double yy); + void setPic(int,int); + RGBApixmap pic; + + void action(); + void die(); + +}; + +class pcbullet : public creature{ + public: + pcbullet(); + pcbullet(double xx,double yy); + void setPic(); + RGBApixmap pic; + void action(); + void die(); +}; diff --git a/creature.h b/creature.h new file mode 100644 index 0000000..5690823 --- /dev/null +++ b/creature.h @@ -0,0 +1,23 @@ +#ifndef CREATURE_H_INCLUDED +#define CREATURE_H_INCLUDED + +#include +#include "RGBpixmap.h" +class creature{ + public: + RGBApixmap pic; + virtual void setPic(){;} + + double getX(){return x;} + double getY(){return y;} + void setXY(double xx,double yy){x=xx,y=yy;} + protected: + double x; + double y; +}; + +#endif // CREATURE_H_INCLUDED + + + + diff --git a/enemy.cpp b/enemy.cpp new file mode 100644 index 0000000..a0a2719 --- /dev/null +++ b/enemy.cpp @@ -0,0 +1,40 @@ +#include "enemy.h" +enemy::enemy() +{ + setXY(-100,-100); + direct=0; + life=50; + setPic(); +} +enemy::enemy(double xx,double yy) +{ + setXY(xx,yy); + direct=0; + life=50; + setPic(); +} +void enemy::setPic() +{ + pic.readBMPFile("image/enemy.bmp"); +} + +void enemy::action() +{ + + /*int n=rand(); + if(rand()%10==0) direct=(direct==0)?1:0; + x=(direct==0)?x-2*( (n%1000)/200 +1):x+2*( (n%1000)/200 +1); + if(x<=50)x=0; + else if(x>=750)x=750; + setXY(x,y);*/ +} +void enemy::die() +{ + x=-100,y=-100; +} +void enemy::form() +{ + x=rand()%761; + y=rand()%201+360; + life=rand()%40; +} diff --git a/enemy.h b/enemy.h new file mode 100644 index 0000000..1f206a3 --- /dev/null +++ b/enemy.h @@ -0,0 +1,20 @@ + +#include "creature.h" +class enemy : public creature{ + public: + int life; + + void form(); + void die(); + enemy(); + enemy(double xx,double yy); + + RGBApixmap pic; + void setPic(); + + void action(); + int direct;//1:right 0:left + +}; + + diff --git a/main.cpp b/main.cpp index cfdad96..43e7406 100644 --- a/main.cpp +++ b/main.cpp @@ -1,369 +1,458 @@ -//3D game programming 2016 -//lab2: 2D - - -#ifdef __APPLE__ -#include -#else -#include -//#include -#include -#endif - -#include -#include -#include - -#include "RGBpixmap.h" - -// Global variables for measuring time (in milli-seconds) -int startTime; -int prevTime; - -// http://devmaster.net/forums/topic/7934-aabb-collision/ -bool AABBtest(float ax1, float ay1, float ax2, float ay2, float bx1, float by1, float bx2, float by2) -{ - return - ax1 > bx2 || ax2 < bx1 || - ay1 > by2 || ay2 < by1; -} - -static void CheckError(int line) -{ - GLenum err = glGetError(); - if (err) { - printf("GL Error %s (0x%x) at line %d\n", - gluErrorString(err), (int) err, line); - } -} - -//Set windows -int screenWidth = 800 , screenHeight = 600; - -int i=0; -RGBApixmap pic[3]; // create two (empty) global pixmaps -RGBApixmap bg; -int whichPic = 0; // which pixmap to display -int picX=100, picY=100; - -int rectX=300, rectY=100; - -float rotation_test=0; -float scale_test=0.3; - - -int jumpState=0; -int DirectState=0; //0:right 1:left -int Gamescore=0; - -void init(); - -//<<<<<<<<<<<<<<<<<<<<<<<<< myMouse >>>>>>>>>>>>>>>>>>>>>>>> -/*void myMouse(int button, int state, int mx, int my) -{ // set raster position with a left click - if(button == GLUT_LEFT_BUTTON ) - { - - - glutPostRedisplay(); - } - -}*/ -//<<<<<<<<<<<<<<<<<<<<<<<<< mouseMove >>>>>>>>>>>>>>>>> -/*void mouseMove(int x, int y) -{// set raster position with mouse motion - //rasterPos.x = x; rasterPos.y = screenHeight - y; - //glRasterPos2i(rasterPos.x, rasterPos.y); - glutPostRedisplay(); -}*/ - -//myReshape -void myReshape(int w, int h) -{ - /* Save the new width and height */ - screenWidth = w; - screenHeight = h; - - /* Reset the viewport... */ - glViewport(0, 0, screenWidth, screenHeight); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - glOrtho(0.0, (GLfloat)screenWidth, 0.0, (GLfloat)screenHeight, -1.0, 1.0); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - -//myDisplay -void myDisplay(void) -{ - // Measure the elapsed time - int currTime = glutGet(GLUT_ELAPSED_TIME); - int timeSincePrevFrame = currTime - prevTime; - int elapsedTime = currTime - startTime; - prevTime = currTime; - - char fpsmss[30]; - sprintf(fpsmss, "Fps %.1f", 1000.0/timeSincePrevFrame); - - - glClear(GL_COLOR_BUFFER_BIT); - - //draw background - glRasterPos2i(50, 50); - bg.blend(); - - - - - //change direction - if(DirectState==0) { - pic[whichPic].blendTex(picX, picY); - - }else { - int offset = pic[whichPic].nCols; - pic[whichPic].blendTex(picX+offset, picY, -1, 1); - } - - //rotation test - pic[whichPic].blendTexRotate(100, 250, 1, 1, rotation_test); - - //scale test - pic[whichPic].blendTexRotate(250, 250, scale_test, scale_test); - - //bouding box outside test - bool Hit = !AABBtest(picX, picY, picX + pic[whichPic].w(), picY + pic[whichPic].h(), rectX, rectY, rectX + 25, rectY + 25); - if(Hit) { - glColor3f(1.0f, 0.0f, 0.0f); - Gamescore -= 1; - }else { - glColor3f(0.0f, 1.0f, 0.0f); - } - glRectf(rectX, rectY, rectX + 25, rectY + 25); - - - //Font - char mss[30]; - sprintf(mss, "Score %d", Gamescore); - - glColor3f(1.0, 0.0, 0.0); //set font color - glRasterPos2i(10, 550); //set font start position - for(int i=0; i>>>>>>>>>>>>>>>>>>>>> -void myKeys(unsigned char key, int x, int y) -{ - switch(key) - { - case 'Q': - case 'q': - exit(0); - break; - - case 'm': - case ' ': - if(jumpState==0) { - jumpState=1; - Gamescore++; - jump(0); - } - break; - - case 'b': - bf(0); - break; - - case 'f': - fly(0); - break; - } //switch(key) - - glutPostRedisplay(); -} - - -void init() -{ - //GLenum err = glewInit(); - //if (GLEW_OK != err) - //{ - // // Problem: glewInit failed, something is seriously wrong. - // fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); - //} - - glutSpecialFunc(SpecialKeys); - glutKeyboardFunc(myKeys); -// glutMouseFunc(myMouse); -// glutMotionFunc(mouseMove); - glutDisplayFunc(myDisplay); - glutReshapeFunc(myReshape); - - glShadeModel(GL_SMOOTH); - //glEnable(GL_DEPTH_TEST); - - glClearColor(1.0f, 1.0f, 0.0f, 0.0f); //background color(1.0, 1.0, 1.0): white color -} - -int main(int argc, char **argv) -{ - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); - glutInitWindowSize(screenWidth, screenHeight); - glutInitWindowPosition(50, 30); - glutCreateWindow("2D World"); - - init(); - - srand(time(0)); //rand seed - - cout<<"Reading sprite"; - pic[0].readBMPFile("image/stand.bmp"); cout<<'.'; - pic[1].readBMPFile("image/walk.bmp"); cout<<'.'; - pic[2].readBMPFile("image/fly.bmp"); cout<<'.'< +#else +#include +//#include +#include +#endif + +#include +#include +#include +#include + +#include "RGBpixmap.h" +#include "creature.h" +#include "player.h" +#include "enemy.h" +#include "bullet.h" +#include "power.h" + +// Global variables for measuring time (in milli-seconds) +int startTime; +int prevTime; + +// http://devmaster.net/forums/topic/7934-aabb-collision/ + +bool AABBtest(float ax1, float ay1, float ax2, float ay2, float bx1, float by1, float bx2, float by2) +{ + return + ax1 > bx2 || ax2 < bx1 || + ay1 > by2 || ay2 < by1; +} + +static void CheckError(int line) +{ + GLenum err = glGetError(); + if (err) { + printf("GL Error %s (0x%x) at line %d\n", + gluErrorString(err), (int) err, line); + } +} + +//Set windows +int screenWidth = 1000 , screenHeight = 600; + +int i=0; +player p(380,4); +RGBApixmap playerHearts; + +enemy pc; + +int playerBullet=40; +int pcBullet=1000; +bullet pBulletList[40]; +pcbullet pcBulletList[1000]; +power powerCubeList[40]; +int pauseFlag=0;//record whether to pulse + +RGBApixmap bg; + +int flag=0; + + +void setSpecialKeyStateUp(int key,int x,int y) +{ + switch(key) + { + case GLUT_KEY_UP: + p.directXY[0]=0; + break; + case GLUT_KEY_DOWN: + p.directXY[1]=0; + break; + case GLUT_KEY_LEFT: + p.directXY[2]=0; + break; + case GLUT_KEY_RIGHT: + p.directXY[3]=0; + break; + } +} +void setSpecialKeyStateDown(int key,int x,int y) +{ + switch(key) + { + case GLUT_KEY_UP: + p.directXY[0]=1; + break; + case GLUT_KEY_DOWN: + p.directXY[1]=1; + break; + case GLUT_KEY_LEFT: + p.directXY[2]=1; + break; + case GLUT_KEY_RIGHT: + p.directXY[3]=1; + break; + } +} +//int rectX=380, rectY=550; + + +int DirectState=0; //0:right 1:left +int Gamescore=0; + +void init(); + +//myReshape +void myReshape(int w, int h) +{ + /* Save the new width and height */ + screenWidth = w; + screenHeight = h; + + /* Reset the viewport... */ + glViewport(0, 0, screenWidth, screenHeight); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0.0, (GLfloat)screenWidth, 0.0, (GLfloat)screenHeight, -1.0, 1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +//myDisplay +void myDisplay(void) +{ + // Measure the elapsed time + int currTime = glutGet(GLUT_ELAPSED_TIME); + int timeSincePrevFrame = currTime - prevTime; + int elapsedTime = currTime - startTime; + prevTime = currTime; + + char fpsmss[30]; + sprintf(fpsmss, "Fps %.1f", 1000.0/timeSincePrevFrame); + + + glClear(GL_COLOR_BUFFER_BIT); +// if gameover + + //draw background + glRasterPos2i(0, 0); + bg.blend(); + if(p.hearts==0) + { + + glRasterPos2i(0,0); + glColor3f(1.0,0.0,0.0); + char endmss[10]="Game Over"; + glRasterPos2f(340,300); + for(int i=0; i0) + { + for(int i=0;i792) + { + pBulletList[i].die(); + } + else + { + pBulletList[i].pic.blendTex(pBulletList[i].getX(),pBulletList[i].getY()); + } + } + } + } + else + { + for(int i=0;i<5;i++) + { + for(int j=0;j<40;j++) + { + if(powerCubeList[j].getX()==-100) + { + powerCubeList[j].form(pc.getX(),pc.getY()); + break; + } + } + } + pc.form(); + } + //bullets by pc + for(int i=0;i50&&pBulletList[i].powerFlag==1) + { + pBulletList[i].powerFlag=2; + pBulletList[i].setPic(p.no,pBulletList[i].powerFlag); + pBulletList[i].pic.setChromaKey(255,255,255); + } + if(pBulletList[i].getY()==-100) + { + pBulletList[i].setXY(p.getX() + 0.5*p.pic.w() - 0.5*pBulletList[i].pic.w(),p.getY()+p.pic.h()); + break; + } + } + } + if(i%10==0)//per 72ms a pcbullet appear + { + for(int i=0;i50)?2:1); + pBulletList[i].pic.setChromaKey(255,255,255); + } + } + break; + case '2': + if(p.no==1) + { + p.setPic(2); + p.pic.setChromaKey(0,0,0); + p.no=2; + for(int i=0;i50)?2:1); + pBulletList[i].pic.setChromaKey(255,255,255); + } + } + break; + case 'z':case 'Z': + p.slowMotion=1; + break; + case 'q':case 'Q': + exit(0); + break; + case ' ': + p.shooting=1; + break; + case 27: + pauseFlag=(pauseFlag==1)?0:1; + break; + } +} + + +void init() +{ + + glutSpecialFunc(setSpecialKeyStateDown); + glutSpecialUpFunc(setSpecialKeyStateUp); + glutKeyboardFunc(setKeyStateDown); + glutKeyboardUpFunc(setKeyStateUp); + + glutDisplayFunc(myDisplay); + glutReshapeFunc(myReshape); + + glShadeModel(GL_SMOOTH); + //glEnable(GL_DEPTH_TEST); + glClearColor(0.007f, 0.627f, 1.0f, 1.0f); //background color(1.0, 1.0, 1.0): white color + //Iw]Oq{z +} + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); + glutInitWindowSize(screenWidth, screenHeight); + glutInitWindowPosition(0, 0);//qW}l + //ڪùO1920*1080AMڬOj1.5AҥHO1280*720 + //(pGnNKkUܫhPosition(480,120) + glutCreateWindow("103703027HW1"); + + init(); + + srand(time(0)); //rand seed + bg.readBMPFile("image/bg.bmp"); + p.pic.setChromaKey(0,0,0); + playerHearts.readBMPFile("image/hearts.bmp"); + playerHearts.setChromaKey(255,255,255); + + pc.pic.setChromaKey(255,255,255); + pc.form(); + for(int i=0;i + +#include "player.h" +#include "RGBpixmap.h" + +player::player() +{ + setXY(-100,-100); + no=1; + slowMotion=0; + for(int i=0;i<4;i++) directXY[i]=0; + shooting=0; + setPic(no); + hearts=5; + power=0; +} +player::player(double xx,double yy) +{ + setXY(xx,yy); + no=1; + slowMotion=0; + for(int i=0;i<4;i++) directXY[i]=0; + shooting=0; + setPic(no); + hearts=5; + power=0; +} + +void player::setPic(int no) +{ + switch(no) + { + case 1: + pic.readBMPFile("image/player1.bmp"); cout<<'.'; + break; + case 2: + pic.readBMPFile("image/player2.bmp"); cout<<'.'; + break; + } +} + +void player::setPosition() +{ + + if(directXY[0]==1) + y+=(2-1*slowMotion); + if(directXY[1]==1) + y-=(2-1*slowMotion); + if(directXY[2]==1) + x-=(2-1*slowMotion); + if(directXY[3]==1) + x+=(2-1*slowMotion); + if(x<=4)x=4; + else if(x>=764)x=764; + + if(y<=0)y=0; + else if(y>536)y=536; +} + + diff --git a/player.h b/player.h new file mode 100644 index 0000000..408b28a --- /dev/null +++ b/player.h @@ -0,0 +1,23 @@ + +#include "creature.h" +class player : public creature{ + public: + player(); + player(double xx,double yy); + + int no; + void setPic(int n); + + + //move + int directXY[4];//up down left right + int slowMotion; + int shooting; + void setPosition(); + + int hearts; + int power; +}; + + + diff --git a/power.cpp b/power.cpp new file mode 100644 index 0000000..8b13bf8 --- /dev/null +++ b/power.cpp @@ -0,0 +1,39 @@ +#include"power.h" +power::power() +{ + x=-100,y=-100; + speedY=3; + setPic(); +} +power::power(double xx,double yy) +{ + x=xx,y=yy; + speedY=3; + setPic(); +} +void power::setPic() +{ + pic.readBMPFile("image/power.bmp"); +} +void power::action() +{ + if(x!=-100) + { + y+=(speedY+speedY-0.05)*0.008/2; + speedY-=0.8; + } +} +void power::die() +{ + x=-100,y=-100; +} +void power::form(double xx,double yy) +{ + speedY=3; + x=xx+rand()%201-100; + y=yy+rand()%201-100; + if(x>785)x=785; + if(x<5)x=5; + if(y>585)y=585; +} + diff --git a/power.h b/power.h new file mode 100644 index 0000000..19197c7 --- /dev/null +++ b/power.h @@ -0,0 +1,13 @@ +#include "creature.h" + +class power:public creature{ +public: + power(); + power(double,double); + double speedY; + void setPic(); + void action(); + void die(); + void form(double,double); + +}; diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..2128abe --- /dev/null +++ b/readme.txt @@ -0,0 +1,16 @@ +103703027_HW1 + +遊戲方式: +1.上下左右鍵控制角色移動 +2.數字鍵(是主鍵區的數字鍵而非tenkey)控制不同角色(現在只有兩個QAQ(1,2)))按3,4~0沒有效果,不同角色有不同子彈 +3.空白鍵控制射擊子彈 +4.Z鍵控制精準移動(移動速度放慢) +5.ESC鍵可將遊戲暫停,再按一次可繼續遊戲 + +關於遊戲 +1.角色血量(畫面右方愛心)歸零時,遊戲會暫停數秒後結束 +2.攻擊敵方數次(0~30)後,敵方會消失並產生數個力量方塊,撿取方塊可以增加POWER +3.角色力量(畫面右方POWER)超過50,玩家的子彈會升級 + + +