Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <SDL_net.h>
- #include <SDL_opengl.h>
- #include <iostream>
- #include <fstream>
- #include <gl\glaux.h>
- #include <SDL_ttf.h> //arial.ttf
- #include <cmath>
- #include <sstream>
- #include <al.h>
- #include <alc.h>
- #include <alut.h>
- using namespace std;
- SDLNet_SocketSet set = NULL;
- SDL_Surface* screen = NULL;
- TCPsocket gameconnection;
- char buffer[512];
- GLuint texture[100];
- float scale = 0.5;
- SDL_Event event;
- TTF_Font *font = NULL;
- SDL_Color textcolor = {170,170,170};
- const char* windowcaption = "Syerjchep";
- const int screenwidth = 500;
- const int screenheight = 500;
- string username = "DrenDran";
- string password = "password";
- bool skup,skdown,skleft,skright,skpu,skpd;
- GLfloat xrot; // x rotation
- GLfloat yrot; // y rotation
- GLfloat xspeed; // x rotation speed
- GLfloat yspeed; // y rotation speed
- GLfloat walkbias = 0;
- GLfloat walkbiasangle = 0;
- GLfloat lookupdown = 0.0;
- const float piover180 = 0.0174532925f;
- float heading, xpos=0, ypos=10, zpos=0;
- float xvel = 0,yvel = 0,zvel = 0;
- GLfloat camx = 0, camy = 0, camz = 0; // camera location.
- GLfloat therotate;
- GLfloat z=0.0f; // depth into the screen.
- bool loggingin = true;
- bool takinginput = false;
- string tell = "";
- #include <SEDTL.h>
- void outpak(chalen in,string fname)
- {
- fstream tt(("logs/"+fname+".bin").c_str(),ios::out|ios::binary);
- char ttemp[in.length];
- for(int i = 0; i<in.length; i++)
- ttemp[i] = in.data[i];
- tt.write(ttemp,in.length);
- tt.close();
- }
- chalen compileauthpacket()
- {
- cerr<<"Logging in with username: "<<username<<"\n";
- char *cusername;
- char *cpassword;
- cusername = new char[username.length()];
- cpassword = new char[password.length()];
- strcpy(cusername,username.c_str());
- strcpy(cpassword,password.c_str());
- char authstageone[username.length()+password.length()+4];
- char authstagetwo[username.length()+password.length()+5];
- authstageone[0] = 0xA1;
- for(int i = 0; i<(int)username.length(); i++)
- authstageone[i+1] = cusername[i];
- authstageone[username.length()+1] = 0xFE;
- for(int i = 0; i<(int)password.length(); i++)
- authstageone[i+username.length()+2] = cpassword[i];
- authstageone[username.length()+password.length()+2] = 0xFE;
- authstageone[username.length()+password.length()+3] = 0xFF;
- authstagetwo[0] = (username.length()+password.length()+4);
- for(int i = 0; i<(int)(username.length()+password.length()+4); i++)
- authstagetwo[i+1] = authstageone[i];
- chalen ret;
- ret.allocspace(username.length()+password.length()+5);
- for(int i = 0; i<ret.length; i++)
- ret.data[i] = authstagetwo[i];
- cerr<<"Authentication packet formed.\n";
- return ret;
- }
- int authenticate()
- {
- cerr<<"Logging in...\n";
- chalen hi = compileauthpacket();
- if(SDLNet_TCP_Send(gameconnection,hi.data,hi.length) != hi.length)
- {
- cerr<<"Error sending authentication, probably because you had to disconnect.\n";
- return 7;
- }
- bool loggedin = false;
- for(int i = 0; i<10; i++)
- {
- int buflen = 0;
- if ((buflen = SDLNet_TCP_Recv(gameconnection, buffer, 512)) > 0)
- {
- fstream outl("packetinlog.bin",ios::binary|ios::app|ios::out);
- outl.write(buffer,buflen);
- outl.write(" ",5);
- outl.close();
- unsigned char test;
- test = buffer[0];
- if(test == username.length()+1)
- {
- char namecheck[username.length()];
- for(int a = 0; a<(int)username.length(); a++)
- namecheck[a] = buffer[a+1];
- if(string(namecheck).substr(0,username.length()) == username)
- {
- if(buffer[test] == 1)
- {
- loggedin = true;
- cerr<<"Logged in to game server!\n";
- break;
- }
- else
- {
- cerr<<"Invalid password, banned account, or bad connection.\n";
- return 10;
- }
- }
- else
- {
- cerr<<"Server client connection messed up username data.\n";
- return 9;
- }
- }
- else
- {
- chalen brf;
- brf.allocspace(buflen);
- for(int i = 0; i<buflen; i++)
- brf.data[i] = buffer[i];
- outpak(brf,"badloginresponse");
- cerr<<"Could not log in. Invalid packet size denomitator.\n";
- return 8;
- }
- }
- SDL_Delay(500);
- }
- if(!loggedin)
- {
- cerr<<"Log in confrimation timed out.\n";
- return 11;
- }
- return 4525;
- }
- int starttime = 0;
- void starttimer()
- {
- starttime = SDL_GetTicks();
- }
- void printtime()
- {
- int endtime = SDL_GetTicks();
- int timetake = endtime-starttime;
- cerr<<"Timer took "<<timetake<<"\n";
- starttimer();
- }
- int drawcube(float x,float y,float z,int tex,float sc,int id,bool ren)
- {
- if(ren || !(xpos - 5 > x || xpos + 5 < x || ypos - 5 > y || ypos + 5 < y || zpos - 5 > z || zpos + 5 < z))
- {
- if(id>0)
- glLoadName(id);
- float oldscale = scale;
- if(sc != -1)
- scale = sc;
- if(ren)
- glBindTexture(GL_TEXTURE_2D, texture[tex]); // Select Our Texture
- glBegin(GL_QUADS);
- // Front Face
- glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y, z); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale, y+scale, z); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+scale, z); // Top Left Of The Texture and Quad
- // Back Face
- glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z-scale); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+scale, z-scale); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale, y+scale, z-scale); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z-scale); // Bottom Left Of The Texture and Quad
- // Top Face
- glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+scale, z-scale); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y+scale, z); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y+scale, z); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale, y+scale, z-scale); // Top Right Of The Texture and Quad
- // Bottom Face
- glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z-scale); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale, y, z-scale); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z); // Bottom Right Of The Texture and Quad
- // Right face
- glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y, z-scale); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale, y+scale, z-scale); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale, y+scale, z); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z); // Bottom Left Of The Texture and Quad
- // Left Face
- glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z-scale); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+scale, z); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+scale, z-scale); // Top Left Of The Texture and Quad
- glEnd();
- scale = oldscale;
- }
- return true;
- }
- // Load Bitmaps And Convert To Textures
- bool createtexture(int position,char* filename)
- {
- // Load Texture
- SDL_Surface *image1;
- Uint8 *rowhi, *rowlo;
- Uint8 *tmpbuf, tmpch;
- SDL_Surface *image;
- int i, j;
- image = SDL_LoadBMP(filename);
- if ( image == NULL ) {
- cerr<<"Unable to load file "<<filename<<" SDL_ERROR: "<<SDL_GetError()<<"\n";
- return false;
- }
- /* GL surfaces are upsidedown and RGB, not BGR :-) */
- tmpbuf = (Uint8 *)malloc(image->pitch);
- if ( tmpbuf == NULL ) {
- fprintf(stderr, "Out of memory\n");
- return false;
- }
- rowhi = (Uint8 *)image->pixels;
- rowlo = rowhi + (image->h * image->pitch) - image->pitch;
- for ( i=0; i<image->h/2; ++i ) {
- for ( j=0; j<image->w; ++j ) {
- tmpch = rowhi[j*3];
- rowhi[j*3] = rowhi[j*3+2];
- rowhi[j*3+2] = tmpch;
- tmpch = rowlo[j*3];
- rowlo[j*3] = rowlo[j*3+2];
- rowlo[j*3+2] = tmpch;
- }
- memcpy(tmpbuf, rowhi, image->pitch);
- memcpy(rowhi, rowlo, image->pitch);
- memcpy(rowlo, tmpbuf, image->pitch);
- rowhi += image->pitch;
- rowlo -= image->pitch;
- }
- free(tmpbuf);
- image1 = image;
- // Create Texture
- glGenTextures(1, &texture[position]);
- glBindTexture(GL_TEXTURE_2D, texture[position]); // 2d texture (x and y size)
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
- // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
- // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
- glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->w, image1->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->pixels);
- return true;
- }
- bool loadalltextures()
- {
- bool status = true;
- status = createtexture(0,"media/exe.bmp")&&status;
- status = createtexture(1,"media/exe2.bmp")&&status;
- status = createtexture(2,"media/grass.bmp")&&status;
- status = createtexture(3,"media/soundbrick.bmp")&&status;
- status = createtexture(4,"media/hammer.bmp")&&status;
- status = createtexture(5,"media/noitem.bmp")&&status;
- status = createtexture(6,"media/corn.bmp")&&status;
- return status;
- }
- class cube
- {
- public:
- float x,y,z,sc;
- int text,id;
- bool used;
- void show(bool ren);
- };
- void cube::show(bool ren)
- {
- drawcube(x,y,z,text,sc,id,ren);
- }
- cube cubes[10000];
- int numberofcubes = 0;
- void addacube(float x,float y,float z,int text,int id,float sc,float l,float h,float w)
- {
- cube adder;
- adder.used = true;
- adder.x = x;
- adder.y = y;
- adder.z = z;
- adder.text = text;
- adder.id = id;
- adder.sc = sc;
- cubes[numberofcubes] = adder;
- numberofcubes++;
- }
- void cubemovebyid(int id,float x,float y,float z)
- {
- for(int i = 0; i<numberofcubes; i++)
- {
- if(cubes[i].id == id)
- {
- cubes[i].x = x;
- cubes[i].y = y;
- cubes[i].z = z;
- }
- }
- }
- bool idifcubeexists(int id)
- {
- for(int i = 0; i<numberofcubes; i++)
- {
- if(cubes[i].id == id)
- return true;
- }
- return false;
- }
- void moveorcreate(int id,float x,float y,float z)
- {
- if(idifcubeexists(id))
- {
- cubemovebyid(id,x,y,z);
- }
- else
- {
- addacube(x,y,z,0,id,-1,1,1,1);
- }
- }
- void showcubes(bool ren)
- {
- glInitNames();
- glPushName(0);
- for(int i = 0; i<numberofcubes; i++)
- cubes[i].show(ren);
- }
- int updateposition()
- {
- char pospak[18];
- pospak[0] = 17;
- pospak[1] = 0xA2;
- memcpy(pospak+2,&xpos,4);
- pospak[6] = 0xFE;
- memcpy(pospak+7,&ypos,4);
- pospak[11] = 0xFE;
- memcpy(pospak+12,&zpos,4);
- pospak[16] = 0xFE;
- pospak[17] = 0xFF;
- if(SDLNet_TCP_Send(gameconnection,pospak,18) != 18)
- {
- cerr<<"Error sending authauthentication, probably because you had to disconnect.\n";
- return 7;
- }
- return 4525;
- }
- int round(double x)
- {
- return (int)(x + 0.5);
- }
- int nextpoweroftwo(int x)
- {
- double logbase2 = log(x) / log(2);
- return round(pow(2,ceil(logbase2)));
- }
- int erc = 0;
- void eb()
- {
- erc++;
- cerr<<erc<<"\n";
- }
- int notdt = 0;
- SDL_Surface *tex2d[100];
- void loadatexture( string fnam )
- {
- tex2d[notdt] = SDL_LoadBMP(fnam.c_str());
- notdt++;
- }
- void SDL_GL_RenderPicture(int x,int y,int tex)
- {
- int h = 32;
- int w = 32;
- /* prepare to render our texture */
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture[tex]);
- glColor3f(1.0f, 1.0f, 1.0f);
- /* Draw a quad at location */
- glBegin(GL_QUADS);
- /* Recall that the origin is in the lower-left corner
- That is why the TexCoords specify different corners
- than the Vertex coors seem to. */
- glTexCoord2f(0.0f, 1.0f);
- glVertex2f(x, y);
- glTexCoord2f(1.0f, 1.0f);
- glVertex2f(x + w, y);
- glTexCoord2f(1.0f, 0.0f);
- glVertex2f(x + w, y + h);
- glTexCoord2f(0.0f, 0.0f);
- glVertex2f(x, y + h);
- glEnd();
- }
- void SDL_GL_RenderText(char *text,
- TTF_Font *font,
- SDL_Color color,
- SDL_Rect *location)
- {
- SDL_Surface *initial;
- SDL_Surface *intermediary;
- int w,h;
- GLuint textura;
- /* Use SDL_TTF to render our text */
- initial = TTF_RenderText_Blended(font, text, color);
- /* Convert the rendered text to a known format */
- w = nextpoweroftwo(initial->w);
- h = nextpoweroftwo(initial->h);
- intermediary = SDL_CreateRGBSurface(0, w, h, 32,
- 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
- SDL_BlitSurface(initial, 0, intermediary, 0);
- /* Tell GL about our new texture */
- glGenTextures(1, &textura);
- glBindTexture(GL_TEXTURE_2D, textura);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
- GL_UNSIGNED_BYTE, intermediary->pixels );
- /* GL_NEAREST looks horrible, if scaled... */
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- /* prepare to render our texture */
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, textura);
- glColor3f(1.0f, 1.0f, 1.0f);
- /* Draw a quad at location */
- glBegin(GL_QUADS);
- /* Recall that the origin is in the lower-left corner
- That is why the TexCoords specify different corners
- than the Vertex coors seem to. */
- glTexCoord2f(0.0f, 1.0f);
- glVertex2f(location->x , location->y);
- glTexCoord2f(1.0f, 1.0f);
- glVertex2f(location->x + w, location->y);
- glTexCoord2f(1.0f, 0.0f);
- glVertex2f(location->x + w, location->y + h);
- glTexCoord2f(0.0f, 0.0f);
- glVertex2f(location->x , location->y + h);
- glEnd();
- /* Bad things happen if we delete the texture before it finishes */
- glFinish();
- /* return the deltas in the unused w,h part of the rect */
- location->w = initial->w;
- location->h = initial->h;
- /* Clean up */
- SDL_FreeSurface(initial);
- SDL_FreeSurface(intermediary);
- glDeleteTextures(1, &textura);
- }
- void glEnable2D()
- {
- int vPort[4];
- glGetIntegerv(GL_VIEWPORT, vPort);
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glLoadIdentity();
- }
- void glDisable2D()
- {
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- glPopMatrix();
- }
- const int numlines = 9;
- string chats[numlines];
- void newchat(string cat)
- {
- for(int i = 1; i<numlines-2; i++)
- {
- chats[i-1] = chats[i];
- }
- chats[numlines-3] = cat;
- }
- void printchats()
- {
- for(int i = 0; i<numlines; i++)
- {
- SDL_Rect bob;
- bob.x = 10;
- bob.y = 10+(i*15);
- bob.h = 100;
- bob.w = 100;
- if(chats[i].length() > 1)
- SDL_GL_RenderText((char*)(chats[i].c_str()), font, textcolor, &bob);
- }
- }
- void sendchat(string in)
- {
- char out[4+in.length()];
- out[0] = 3+in.length();
- out[1] = 0xA3;
- out[2+in.length()] = 0xFE;
- out[3+in.length()] = 0xFF;
- cerr<<"Sending chat message.\n";
- int q = 0;
- while(q < (int)in.length())
- {
- int ra = q;
- char tmp[1];
- ra = q;
- string quick = in.substr(ra,1);
- ra = q;
- strcpy(tmp,quick.c_str());
- ra = q;
- out[ra+2] = tmp[0];
- ra = q;
- q++;
- }
- out[1] = 0xA3;
- SDLNet_TCP_Send(gameconnection,out,4+in.length());
- cerr<<"Chat message test.\n";
- }
- ALuint Source,Buffer;
- // Position of the source sound.
- ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
- // Velocity of the source sound.
- ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
- // Position of the listener.
- ALfloat ListenerPos[] = { 0,0,0 };
- // Velocity of the listener.
- ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
- // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
- ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
- void loadaudiofile(string fnam)
- {
- ALenum format;
- ALsizei size;
- ALvoid* data;
- ALsizei freq;
- ALboolean loop;
- alGenBuffers(1, &Buffer);
- alutLoadWAVFile((ALbyte*)fnam.c_str(), &format, &data, &size, &freq, &loop);
- alBufferData(Buffer, format, data, size, freq);
- alutUnloadWAV(format, data, size, freq);
- alGenSources(1, &Source);
- alSourcei (Source, AL_BUFFER, Buffer );
- alSourcef (Source, AL_PITCH, 1.0f );
- alSourcef (Source, AL_GAIN, 1.0f );
- alSourcefv(Source, AL_POSITION, SourcePos);
- alSourcefv(Source, AL_VELOCITY, SourceVel);
- alSourcei (Source, AL_LOOPING, loop );
- }
- void loadaudio()
- {
- loadaudiofile("media/ringout.wav");
- alListenerfv(AL_POSITION, ListenerPos);
- alListenerfv(AL_VELOCITY, ListenerVel);
- alListenerfv(AL_ORIENTATION, ListenerOri);
- }
- globjfaces models[100];
- int modelnum = 0;
- void loadmodel(string fnam)
- {
- globjfaces tmp;
- tmp.readin((char*)fnam.c_str());
- models[modelnum] = tmp;
- modelnum++;
- }
- void showmodel(float x,float y,float z,int pos)
- {
- models[pos].show(x,y,z,false,0,0,0);
- }
- class item_basic
- {
- public:
- int idbitmap;
- int model;
- string name;
- };
- class item_dropped
- {
- public:
- float x,y,z;
- item_basic type;
- int id;
- bool used;
- };
- class plant
- {
- public:
- int model,growth,id,maxgrowth;
- float x,y,z,height;
- bool used;
- };
- plant plants[100];
- void addplant(float x,float y,float z,int model,int growth,int id,int maxgrowth,float height)
- {
- for(int i = 0; i<100; i++)
- {
- if(plants[i].used == false)
- {
- plants[i].used = true;
- plants[i].x = x;
- plants[i].y = y;
- plants[i].z = z;
- plants[i].model = model;
- plants[i].growth = growth;
- plants[i].id = id;
- plants[i].maxgrowth = maxgrowth;
- plants[i].height = height;
- break;
- }
- }
- }
- void pickmodel(float x,float y,float z,int pos,int r,int g,int b)
- {
- models[pos].show(x,y,z,true,r,g,b);
- }
- void showplants(bool picking)
- {
- for(int i = 0; i<100; i++)
- {
- if(plants[i].used)
- {
- float plantmaxgrowthtime = plants[i].maxgrowth;
- float currentgrowthtime = plants[i].growth;
- float decimaldonegrowting = currentgrowthtime/plantmaxgrowthtime;
- float plantmaxheight = plants[i].height;
- float heightofplant = plantmaxheight*decimaldonegrowting;
- if(!picking)
- showmodel(plants[i].x,(plants[i].y-0.75-plantmaxheight)+heightofplant,plants[i].z,plants[i].model);
- else
- pickmodel(plants[i].x,(plants[i].y-0.75-plantmaxheight)+heightofplant,plants[i].z,plants[i].model,80,0,i);
- }
- }
- }
- void removeplantbyid(int id)
- {
- for(int i = 0; i<100; i++)
- {
- if(plants[i].id == id && plants[i].used)
- plants[i].used = false;
- }
- }
- item_basic items[100];
- int noi = 0;
- item_dropped things[100];
- void aaddnewitem(float x,float y,float z,int type,int id)
- {
- for(int i = 0; i<100; i++)
- {
- if(!things[i].used)
- {
- things[i].type = items[type];
- things[i].x = x;
- things[i].y = y;
- things[i].z = z;
- things[i].used = true;
- things[i].id = id;
- break;
- }
- }
- }
- void removeitem(int id)
- {
- for(int i = 0; i<100; i++)
- {
- if(things[i].id == id)
- things[i].used = false;
- }
- }
- void showgrounditems()
- {
- for(int i = 0; i<100; i++)
- {
- if(things[i].used)
- showmodel(things[i].x,things[i].y,things[i].z,things[i].type.model);
- }
- }
- void baddnewitem(string name,int text,int model)
- {
- items[noi].name = name;
- items[noi].idbitmap = text;
- items[noi].model = model;
- noi++;
- }
- int itemnametoid(string name)
- {
- for(int i = 0; i<noi; i++)
- if(items[i].name == name)
- return i;
- return -1;
- }
- int inventory[16];
- int iosx = 200;
- int iosy = 0;
- vec3 slottopos(int slot)
- {
- vec3 tmp;
- tmp.z = 0;
- tmp.x = 0;
- tmp.y = 0;
- switch(slot)
- {
- case 0: tmp.x = 0; tmp.y = 0; break;
- case 1: tmp.x = 40; tmp.y = 0; break;
- case 2: tmp.x = 80; tmp.y = 0; break;
- case 3: tmp.x = 120; tmp.y = 0; break;
- case 4: tmp.x = 0; tmp.y = 40; break;
- case 5: tmp.x = 40; tmp.y = 40; break;
- case 6: tmp.x = 80; tmp.y = 40; break;
- case 7: tmp.x = 120; tmp.y = 40; break;
- case 8: tmp.x = 0; tmp.y = 80; break;
- case 9: tmp.x = 40; tmp.y = 80; break;
- case 10: tmp.x = 80; tmp.y = 80; break;
- case 11: tmp.x = 120; tmp.y = 80; break;
- case 12: tmp.x = 0; tmp.y = 120; break;
- case 13: tmp.x = 40; tmp.y = 120; break;
- case 14: tmp.x = 80; tmp.y = 120; break;
- case 15: tmp.x = 120; tmp.y = 120; break;
- }
- tmp.x += iosx;
- tmp.y += iosy;
- return tmp;
- }
- void showitems()
- {
- for(int i = 0; i<16; i++)
- {
- vec3 tmp = slottopos(i);
- int x = (int)tmp.x;
- int y = (int)tmp.y;
- SDL_GL_RenderPicture(x,y,items[inventory[i]].idbitmap);
- }
- }
- bool pickupitem(unsigned char type)
- {
- for(int i = 0; i<16; i++)
- {
- if(inventory[i] == itemnametoid("nil"))
- {
- inventory[i] = type;
- return true;
- }
- }
- return false;
- }
- void inventorydrop(int slot)
- {
- char outpak[5];
- outpak[0] = 4;
- outpak[1] = 0xA4;
- outpak[2] = inventory[slot]-1;
- outpak[3] = 0xFE;
- outpak[4] = 0xFF;
- SDLNet_TCP_Send(gameconnection,outpak,5);
- inventory[slot] = itemnametoid("nil");
- }
- void inventoryuse(int slot)
- {
- char outpak[5];
- outpak[0] = 4;
- outpak[1] = 0xA6;
- outpak[2] = inventory[slot]-1;
- outpak[3] = 0xFE;
- outpak[4] = 0xFF;
- SDLNet_TCP_Send(gameconnection,outpak,5);
- inventory[slot] = itemnametoid("nil");
- }
- void pickplant(int a)
- {
- if(plants[a].used && !(plants[a].growth < plants[a].maxgrowth))
- {
- char outpak[5];
- outpak[0] = 4;
- outpak[1] = 0xA5;
- outpak[2] = plants[a].id;
- outpak[3] = 0xFE;
- outpak[4] = 0xFF;
- SDLNet_TCP_Send(gameconnection,outpak,5);
- }
- }
- int main ( int argc, char** argv )
- {
- baddnewitem("nil",5,0);
- baddnewitem("ball",4,0);
- baddnewitem("corn",6,2);
- for(int i = 0; i<16; i++)
- inventory[i] = itemnametoid("nil");
- loadatexture("media/exe.bmp");
- loadmodel("media/hammar.obj");
- loadmodel("media/test.obj");
- loadmodel("media/corn.obj");
- chats[numlines-1] = "";
- chats[numlines-2] = "";
- for(float i = -16; i<16; i+=2)
- {
- for(float y = -16; y<16; y+=2)
- {
- addacube(i,-2.75,y,2,-1,2,1,1,1);
- }
- }
- SourcePos[0] = 10;
- SourcePos[1] = -.75;
- SourcePos[2] = 10;
- alSourcefv(Source, AL_POSITION, SourcePos);
- string inip;
- ifstream prop("settings.ini");
- if(prop.is_open())
- {
- getline(prop,inip);
- cerr<<inip<<"\n";
- cerr<<"Properties loaded.\n";
- }
- else
- {
- cerr<<"Could not open properties file.\n";
- return 88;
- }
- alutInit(&argc, argv);
- cerr<<"Audio set up.\n";
- loadaudio();
- cerr<<"Audio files loaded.\n";
- if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
- {
- cerr<<"Could not start up SDL graphics and input system.\n";
- return 1;
- }
- else
- cerr<<"Started up SDL.\n";
- if(SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ) == -1)
- {
- cerr<<"Could not bind graphics to OpenGL.\n";
- return 2;
- }
- else
- cerr<<"Graphics bound to OpenGL.\n";
- if(SDLNet_Init() == -1)
- {
- cerr<<"Could not start up SDL_Net.\n";
- return 3;
- }
- else
- cerr<<"Started up SDL_Net.\n";
- if(TTF_Init() == -1)
- {
- cerr<<"SDL_TTF didn't initalize correctly.\n";
- return 63;
- }
- font = NULL;
- font = TTF_OpenFont( "media/arial.ttf", 12 );
- if(font == NULL)
- {
- cerr<<"Couldn't load font.\n";
- return 62;
- }
- FreeConsole();
- screen = SDL_SetVideoMode(screenwidth, screenheight, 0, SDL_OPENGL);
- if(!screen || screen == NULL)
- {
- cerr<<"Failed to create screen.\n";
- return 4;
- }
- else
- cerr<<"Screen created.\n";
- SDL_WM_SetCaption(windowcaption, NULL );
- cerr<<"Caption set.\n";
- glViewport(0, 0, screenwidth, screenheight);
- if(!loadalltextures())
- {
- cerr<<"Had errors when trying to load textures.\n";
- return 12;
- }
- else
- cerr<<"Textures loaded.\n";
- glEnable(GL_TEXTURE_2D); // Enable texture mapping.
- glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set the blending function for translucency (note off at init time)
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
- glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
- glDepthFunc(GL_LESS); // type of depth test to do.
- glEnable(GL_DEPTH_TEST); // enables depth testing.
- glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity(); // Reset The Projection Matrix
- gluPerspective(45.0f,(GLfloat)screenwidth/(GLfloat)screenheight,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
- glMatrixMode(GL_MODELVIEW);
- cerr<<"Screen properties set.\n";
- IPaddress ip;
- char *ipto = (char*)inip.c_str();
- if(SDLNet_ResolveHost(&ip, ipto, 4525) == -1)
- {
- cerr<<"Could not resolve host IP of server.\n";
- return 5;
- }
- else
- cerr<<"Host location found.\n";
- cerr<<"Using IP: "<<ipto<<"\n";
- if((gameconnection = SDLNet_TCP_Open(&ip)) == NULL)
- {
- cerr<<"Could not connect to server.\n";
- return 6;
- }
- else
- cerr<<"Connected to server.\n";
- int buflen = 0;
- SDL_EnableUNICODE( SDL_ENABLE );
- //SDL_ShowCursor(SDL_DISABLE);
- set = SDLNet_AllocSocketSet(2);
- SDLNet_TCP_AddSocket(set, gameconnection);
- cerr<<"Starting game loop.\n";
- bool done = false;
- bool firstround = true;
- int ticks = 0;
- int starttime = time(NULL);
- int roundcount = 0;
- int lastsecond = time(NULL);
- float spacebuf = 0.02;
- string usernametobe = "";
- int lastjumptime = 0;
- bool jumped = false;
- yvel = -0.05;
- int lft = 0;
- int ticksecond = SDL_GetTicks();
- while(!done)
- {
- if(ticksecond+1000 < (int)SDL_GetTicks())
- {
- ticksecond = SDL_GetTicks();
- for(int i = 0; i<100; i++)
- {
- if(plants[i].used && plants[i].growth < plants[i].maxgrowth)
- plants[i].growth++;
- }
- }
- if(lft < 20)
- SDL_Delay(20-lft);
- lft = SDL_GetTicks();
- ALfloat newListenerPos[] = {xpos,ypos,zpos};
- alListenerfv(AL_POSITION, newListenerPos);
- if(jumped && lastjumptime+500 < (int)SDL_GetTicks())
- {
- jumped = false;
- yvel -= 0.075;
- }
- if(loggingin)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
- glLoadIdentity(); // Reset The View
- glEnable2D();
- SDL_Rect hi;
- hi.x = (screenwidth/2)-100;
- hi.y = screenheight/2;
- hi.h = 0;
- hi.w = 0;
- SDL_GL_RenderPicture(10,10,0);
- SDL_GL_RenderText("Type in the name you wish to use: ", font, textcolor, &hi);
- hi.y -= 20;
- if(usernametobe.length() > 0)
- SDL_GL_RenderText((char*)usernametobe.c_str(),font,textcolor,&hi);
- glDisable2D();
- SDL_GL_SwapBuffers();
- while(SDL_PollEvent(&event))
- {
- if(event.type == SDL_QUIT)
- {
- done = true;
- break;
- }
- if(event.type == SDL_KEYDOWN)
- {
- if(usernametobe.length() < 20)
- {
- if( event.key.keysym.unicode == (Uint16)' ' )
- {
- usernametobe = usernametobe+" ";
- }
- else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
- {
- usernametobe += (char)event.key.keysym.unicode;
- }
- else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
- {
- usernametobe += (char)event.key.keysym.unicode;
- }
- else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
- {
- usernametobe += (char)event.key.keysym.unicode;
- }
- }
- if(event.key.keysym.sym == SDLK_BACKSPACE)
- {
- if(usernametobe.length() > 0)
- {
- usernametobe.erase(usernametobe.length()-1);
- }
- }
- if(event.key.keysym.sym == SDLK_RETURN)
- {
- if(usernametobe.length() > 0)
- {
- username = usernametobe;
- usernametobe = "";
- bool cah = true;
- while(cah)
- {
- if(authenticate() == 4525)
- {
- cah = false;
- cerr<<"Logged in as "<<username<<"\n";
- }
- else
- cerr<<"Failed to log in... trying again.\n";
- }
- loggingin = false;
- }
- }
- }
- }
- SDL_Delay(10);
- }
- else
- {
- roundcount++;
- ticks++;
- if(roundcount > 9)
- {
- updateposition();
- roundcount = 0;
- }
- if(roundcount == 5)
- {
- SDLNet_CheckSockets(set, 10);
- if(SDLNet_SocketReady(gameconnection))
- {
- chats[numlines-1] = tell;
- buflen = SDLNet_TCP_Recv(gameconnection, buffer, 1);
- if(buflen > 0)
- {
- unsigned int si = (unsigned int)buffer[0];
- buflen = SDLNet_TCP_Recv(gameconnection,buffer,si);
- fstream outl("packetinlog.bin",ios::binary|ios::app|ios::out);
- outl.write(buffer,buflen);
- outl.write(" ",5);
- outl.close();
- chalen newpak;
- newpak.allocspace(buflen);
- for(int i = 0; i<buflen; i++)
- newpak.data[i] = buffer[i];
- char tmp[newpak.length];
- for(int i = 0; i<newpak.length; i++)
- tmp[i] = newpak.data[i];
- unsigned char ptype = (unsigned char)buffer[0];
- newpak = cutdat(newpak,1);
- if(ptype == 0xB1)
- {
- chalen movid = readarg(newpak);
- chalen movx = readarg(newpak);
- chalen movy = readarg(newpak);
- chalen movz = readarg(newpak);
- float txpos,typos,tzpos;
- memcpy(&txpos, movx.data, 4);
- memcpy(&typos, movy.data, 4);
- memcpy(&tzpos, movz.data, 4);
- moveorcreate((int)movid.data[0],txpos,typos,tzpos);
- }
- else if(ptype == 0xB2)
- {
- cerr<<"Got a chat message.\n";
- chalen chat = readarg(newpak);
- char type[chat.length];
- for(int i = 0; i<chat.length; i++)
- type[i] = chat.data[i];
- cerr<<"Chat message is "<<chat.length<<" chars long and is: \n"<<string(type)<<"\n";
- newchat(string(type).substr(0,chat.length));
- }
- else if(ptype == 0xB3)
- {
- cerr<<"Got a remove item request.\n";
- chalen id = readarg(newpak);
- cerr<<"REMOVE ITEM "<<(unsigned int)id.data[0]<<"\n";
- removeitem((unsigned int)id.data[0]);
- }
- else if(ptype == 0xB4)
- {
- cerr<<"Got a add item request.\n";
- chalen movx = readarg(newpak);
- chalen movy = readarg(newpak);
- chalen movz = readarg(newpak);
- chalen type = readarg(newpak);
- chalen id = readarg(newpak);
- float txpos,typos,tzpos;
- memcpy(&txpos, movx.data, 4);
- memcpy(&typos, movy.data, 4);
- memcpy(&tzpos, movz.data, 4);
- aaddnewitem(txpos,typos,tzpos,(unsigned int)type.data[0]+1,(unsigned int)id.data[0]);
- cerr<<"ADD ITEM "<<(unsigned int)id.data[0]<<" - "<<txpos<<":"<<typos<<":"<<tzpos<<" - "<<(unsigned int)type.data[0]<<"\n";
- }
- else if(ptype == 0xB5)
- {
- cerr<<"Picked up an item.\n";
- chalen item = readarg(newpak);
- pickupitem(item.data[0]+1);
- }
- else if(ptype == 0xB6)
- {
- cerr<<"Adding a plant.\n";
- chalen movx = readarg(newpak);
- chalen movy = readarg(newpak);
- chalen movz = readarg(newpak);
- chalen type = readarg(newpak);
- chalen growth = readarg(newpak);
- chalen id = readarg(newpak);
- chalen amaxgrowth = readarg(newpak);
- chalen aheight = readarg(newpak);
- float txpos,typos,tzpos,height;
- unsigned short maxgrowth;
- memcpy(&txpos, movx.data, 4);
- memcpy(&typos, movy.data, 4);
- memcpy(&tzpos, movz.data, 4);
- memcpy(&height, aheight.data, 4);
- memcpy(&maxgrowth, amaxgrowth.data, 2);
- cerr<<"PLANT: "<<txpos<<":"<<typos<<":"<<tzpos<<":"<<(int)type.data[0]<<":"<<(int)growth.data[0]<<":"<<(int)id.data[0]<<":"<<maxgrowth<<":"<<height<<"\n";
- addplant(txpos,typos,tzpos,type.data[0],growth.data[0],id.data[0],maxgrowth,height);
- // float x,float y,float z,int model,int growth,int id,int maxgrowth,float height
- }
- else if(ptype == 0xB7)
- {
- cerr<<"Removing plant.\n";
- chalen id = readarg(newpak);
- removeplantbyid((unsigned char)id.data[0]);
- }
- else if(ptype == 0xB8)
- {
- chalen tx = readarg(newpak);
- chalen ty = readarg(newpak);
- chalen tz = readarg(newpak);
- chalen ttext = readarg(newpak);
- chalen tid = readarg(newpak);
- chalen tsc = readarg(newpak);
- chalen tl = readarg(newpak);
- chalen th = readarg(newpak);
- chalen tw = readarg(newpak);
- float cx,cy,cz;
- float cl,ch,cw;
- memcpy(&cx, tx.data, 4);
- memcpy(&cy, ty.data, 4);
- memcpy(&cz, tz.data, 4);
- memcpy(&cl, tl.data, 4);
- memcpy(&ch, th.data, 4);
- memcpy(&cw, tw.data, 4);
- addacube(cx,cy,cz,ttext.data[0],tid.data[0],tsc.data[0],cl,ch,cw);
- }
- else
- {
- fstream oest("logs/unreconizendpacketidpacket.bin",ios::binary|ios::out);
- oest.write(tmp,newpak.length);
- oest.close();
- cerr<<"Got a unreconized packet id.\n";
- return 99;
- }
- }
- }
- }
- if(lastsecond+1 < time(NULL))
- {
- lastsecond = time(NULL);
- }
- while(SDL_PollEvent(&event))
- {
- if(event.type == SDL_QUIT)
- {
- done = true;
- break;
- }
- if(event.type == SDL_MOUSEBUTTONDOWN)
- {
- int cursorX = event.button.x;
- int cursorY = event.button.y;
- int invselect = -1;
- for(int i = 0; i<16; i++)
- {
- int tmpcursory = screenheight-cursorY;
- vec3 test = slottopos(i);
- //cerr<<"Inventory position "<<i<<" X:"<<(int)test.x<<" Y:"<<(int)test.y<<" Mouse cursor... X:"<<cursorX<<" Y:"<<tmpcursory<<"\n";
- if(cursorX > test.x && cursorX < test.x+32)
- {
- if(tmpcursory > test.y && tmpcursory < test.y+32)
- {
- // cerr<<"Match!\n";
- invselect = i;
- break;
- }
- }
- }
- if(invselect == -1)
- {
- GLuint selectBuf[512];
- GLint viewport[4];
- GLubyte pixel[3];
- glSelectBuffer(512,selectBuf);
- glRenderMode(GL_SELECT);
- glGetIntegerv(GL_VIEWPORT,viewport);
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- gluPickMatrix(cursorX,500-cursorY,1,1,viewport);
- gluPerspective(45.0f,(GLfloat)screenwidth/(GLfloat)screenheight,0.1f,100.0f);
- showcubes(false);
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- GLuint hits = glRenderMode(GL_RENDER);
- cerr<<"Picking.\n";
- for (int qi = 0; qi < (int)hits; qi++)
- {
- if(selectBuf[qi*4 + 3] == 150)
- alSourcePlay(Source);
- }
- glMatrixMode(GL_MODELVIEW);
- GLfloat xtrans, ztrans, ytrans;
- GLfloat sceneroty;
- // calculate translations and rotations.
- xtrans = -xpos;
- ztrans = -zpos;
- ytrans = -(walkbias+ypos)-0.25f;
- sceneroty = 360.0f - yrot;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
- glLoadIdentity(); // Reset The View
- glRotatef(lookupdown, 1.0f, 0, 0);
- glRotatef(sceneroty, 0, 1.0f, 0);
- glTranslatef(xtrans, ytrans, ztrans);
- glPushMatrix();
- showcubes(true);
- glPopMatrix();
- glPushMatrix();
- showgrounditems();
- glPopMatrix();
- glPushMatrix();
- showplants(true);
- glPopMatrix();
- glEnable2D();
- printchats();
- showitems();
- glDisable2D();
- glReadPixels(cursorX,viewport[3]-cursorY,1,1,GL_RGB,GL_UNSIGNED_BYTE,(void*)pixel);
- cerr<<"Clicked pixel is "<<(int)pixel[0]<<":"<<(int)pixel[1]<<":"<<(int)pixel[2]<<"\n";
- if((int)pixel[0] == 80)
- pickplant((int)pixel[2]);
- }
- else
- {
- if(event.button.button == SDL_BUTTON_LEFT) inventorydrop(invselect);
- else inventoryuse(invselect);
- }
- }
- if(event.type == SDL_KEYDOWN)
- {
- if(event.key.keysym.sym == SDLK_ESCAPE)
- {
- done = true;
- break;
- }
- if(takinginput)
- {
- if(tell.length() < 40)
- {
- if( event.key.keysym.unicode == (Uint16)' ' )
- {
- tell = tell+" ";
- }
- else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
- {
- tell += (char)event.key.keysym.unicode;
- }
- else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
- {
- tell += (char)event.key.keysym.unicode;
- }
- else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
- {
- tell += (char)event.key.keysym.unicode;
- }
- }
- if(event.key.keysym.sym == SDLK_BACKSPACE)
- {
- if(tell.length() > 0)
- {
- tell.erase(tell.length()-1);
- }
- }
- if(event.key.keysym.sym == SDLK_RETURN)
- {
- if(tell.length() > 0)
- {
- sendchat(tell);
- tell = "";
- }
- }
- }
- if(event.key.keysym.sym == SDLK_F1)
- {
- takinginput = !takinginput;
- if(!takinginput)
- tell = "";
- }
- if(event.key.keysym.sym == SDLK_DOWN)
- skup = true;
- if(event.key.keysym.sym == SDLK_UP)
- skdown = true;
- if(event.key.keysym.sym == SDLK_LEFT)
- skleft = true;
- if(event.key.keysym.sym == SDLK_RIGHT)
- skright = true;
- if(event.key.keysym.sym == SDLK_PAGEUP)
- skpu = true;
- if(event.key.keysym.sym == SDLK_PAGEDOWN)
- skpd = true;
- if(event.key.keysym.sym == SDLK_SPACE && !jumped)
- {
- yvel += 0.075;
- jumped = true;
- lastjumptime = SDL_GetTicks();
- }
- }
- if(event.type == SDL_KEYUP)
- {
- if(event.key.keysym.sym == SDLK_DOWN)
- skup = false;
- if(event.key.keysym.sym == SDLK_UP)
- skdown = false;
- if(event.key.keysym.sym == SDLK_LEFT)
- skleft = false;
- if(event.key.keysym.sym == SDLK_RIGHT)
- skright = false;
- if(event.key.keysym.sym == SDLK_PAGEUP)
- skpu = false;
- if(event.key.keysym.sym == SDLK_PAGEDOWN)
- skpd = false;
- }
- if(event.type == SDL_MOUSEMOTION)
- {
- }
- }
- float lastx = xpos,lastz = zpos;
- Uint8 *keys = SDL_GetKeyState(NULL);
- if ( keys[SDLK_PAGEUP] == SDL_PRESSED ) {
- z -= 0.2f;
- lookupdown -= 0.2f;
- }
- if ( keys[SDLK_PAGEDOWN] == SDL_PRESSED ) {
- z += 0.2f;
- lookupdown += 0.2f;
- }
- if ( keys[SDLK_UP] == SDL_PRESSED ) {
- xpos -= (float)sin(yrot*piover180) * 0.05f;
- zpos -= (float)cos(yrot*piover180) * 0.05f;
- if (walkbiasangle >= 359.0f)
- walkbiasangle = 0.0f;
- else
- walkbiasangle+= 10;
- walkbias = (float)sin(walkbiasangle * piover180)/20.0f;
- }
- if ( keys[SDLK_DOWN] == SDL_PRESSED ) {
- xpos += (float)sin(yrot*piover180) * 0.05f;
- zpos += (float)cos(yrot*piover180) * 0.05f;
- if (walkbiasangle <= 1.0f)
- walkbiasangle = 359.0f;
- else
- walkbiasangle-= 10;
- walkbias = (float)sin(walkbiasangle * piover180)/20.0f;
- }
- if ( keys[SDLK_LEFT] == SDL_PRESSED ) {
- yrot += 1.5f;
- }
- if ( keys[SDLK_RIGHT] == SDL_PRESSED ) {
- yrot -= 1.5f;
- }
- bool doreset = false;
- for(int i = 0; i<numberofcubes; i++)
- {
- float oldscale = scale;
- if(cubes[i].sc > 0)
- scale = cubes[i].sc;
- if(xpos > cubes[i].x-spacebuf && xpos < cubes[i].x+scale+spacebuf)
- {
- if(ypos > cubes[i].y-spacebuf && ypos < cubes[i].y+spacebuf+scale)
- {
- if(zpos > cubes[i].z-scale-spacebuf && zpos < cubes[i].z+spacebuf)
- {
- ypos+=0.25;
- bool keephigh = true;
- if(xpos > cubes[i].x-spacebuf && xpos < cubes[i].x+scale+spacebuf)
- {
- if(ypos > cubes[i].y-spacebuf && ypos < cubes[i].y+spacebuf+scale)
- {
- if(zpos > cubes[i].z-scale-spacebuf && zpos < cubes[i].z+spacebuf)
- {
- keephigh = false;
- }
- }
- }
- if(!keephigh)
- {
- ypos-=0.25;
- doreset = true;
- break;
- }
- }
- }
- }
- scale = oldscale;
- }
- if(doreset)
- {
- xpos = lastx;
- zpos = lastz;
- }
- xpos += xvel;
- ypos += yvel;
- zpos += zvel;
- doreset = false;
- for(int i = 0; i<numberofcubes; i++)
- {
- float oldscale = scale;
- if(cubes[i].sc > 0)
- scale = cubes[i].sc;
- if(xpos > cubes[i].x-spacebuf && xpos < cubes[i].x+scale+spacebuf)
- {
- if(ypos > cubes[i].y-spacebuf && ypos < cubes[i].y+spacebuf+scale)
- {
- if(zpos > cubes[i].z-scale-spacebuf && zpos < cubes[i].z+spacebuf)
- {
- doreset = true;
- }
- }
- }
- scale = oldscale;
- }
- if(doreset)
- {
- ypos+=0.05;
- }
- GLfloat xtrans, ztrans, ytrans;
- GLfloat sceneroty;
- // calculate translations and rotations.
- xtrans = -xpos;
- ztrans = -zpos;
- ytrans = -(walkbias+ypos)-0.25f;
- sceneroty = 360.0f - yrot;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
- glLoadIdentity(); // Reset The View
- glRotatef(lookupdown, 1.0f, 0, 0);
- glRotatef(sceneroty, 0, 1.0f, 0);
- glTranslatef(xtrans, ytrans, ztrans);
- glPushMatrix();
- showcubes(true);
- glPopMatrix();
- glPushMatrix();
- showgrounditems();
- glPopMatrix();
- glPushMatrix();
- showplants(false);
- glPopMatrix();
- glEnable2D();
- printchats();
- showitems();
- glDisable2D();
- SDL_GL_SwapBuffers();
- SDL_Delay(10);
- if(firstround)
- firstround = false;
- }
- lft = SDL_GetTicks()-lft;
- }
- cerr<<"Main loop stopped.\n";
- int diff = time(NULL)-starttime;
- cerr<<"Ran for "<<diff<<" seconds.\n";
- int fps = ticks/diff;
- cerr<<"Avraged about "<<fps<<" frames per second.\n";
- cerr<<"Cleaning up and quiting.\n";
- cerr<<"OPENAL: "<<alGetError()<<"\n";
- cerr<<"OPENAL: "<<istr(alGetError())<<"\n";
- SDLNet_Quit();
- SDL_Quit();
- alDeleteBuffers(1, &Buffer);
- alDeleteSources(1, &Source);
- alutExit();
- cerr<<"Exited without any problems.\n";
- return 4525;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement