]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
shitty bug fixes
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 16 Sep 2015 15:33:02 +0000 (11:33 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 16 Sep 2015 15:33:02 +0000 (11:33 -0400)
Makefile
include/UIClass.h
include/World.h
src/UIClass.cpp
src/World.cpp
src/entities.cpp
src/main.cpp

index 7b8b834353f327173311f1114f68ae7e0539c932..66b1c7f7a95cdfe2ce70a62c73c65f6c7c51979e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 FLAGS_LINUX = -lGL                 -lSDL2_image -lfreetype\r
 FLAGS_WIN32 = -lopengl32 -lmingw32 -lSDL2_Image -lfreetype\r
-FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -Wall -Werror -lSDL2main -lSDL2\r
+FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -Wall -Werror -Wextra -lSDL2main -lSDL2\r
 \r
 all:\r
        @g++ src/*.cpp -o main $(FLAGS_LINUX) $(FLAGS)\r
index e6f48256dbd62e1819d94cfb01b2a4ef98bc8d19..745dec34cf15e0a71d311cfbd7f358c5d66f10ac 100644 (file)
@@ -5,17 +5,14 @@
 #include <cstdarg>\r
 #include <cstdio>\r
 \r
-class UIClass {\r
-private:\r
-       unsigned int fontSize;\r
-public:\r
+namespace ui {\r
+       extern int mousex,mousey;\r
        void init(const char *ttf);\r
        void setFontSize(unsigned int fs);\r
        void putText(const float x,const float y,const char *s,...);\r
        void putString(const float x,const float y,const char *s);\r
        void msgBox(const char *str,...);\r
        void handleEvents();\r
-       int mousex, mousey;\r
-};\r
+}\r
 \r
 #endif // UICLASS_H\r
index fca108d52cf3264b6504a270cb5e754a0f138d8e..f6ddbfe5eac1fc71717835d0cd9e7b87794b90d7 100644 (file)
@@ -18,7 +18,7 @@ private:
        } __attribute__ ((packed)) *line;\r
        unsigned int lineCount;                   // Size of line array, calculated in the constructor\r
        unsigned int entCount;                    // Count of currently bound entities\r
-       void *entity[MAX_ENTITIES];\r
+       void **entity;\r
 public:\r
        World *behind,*infront;                                                   // As in layers\r
        World *toLeft,*toRight;                                                   // 'new' worlds (off screen)\r
index 04975756e7df87b97a4a27d5ec69b894922cd2e9..86a7be61e0d54be3b8d26f83418c58792b33d216 100644 (file)
@@ -9,151 +9,159 @@ extern World *currentWorld;
 static FT_Library ftl;
 static FT_Face ftf;
 static GLuint ftex;
+static unsigned int fontSize;
 
-void UIClass::init(const char *ttf){
-       if(FT_Init_FreeType(&ftl)){
-               std::cout<<"Error! Couldn't initialize freetype."<<std::endl;
-               abort();
-       }
-       if(FT_New_Face(ftl,ttf,0,&ftf)){
-               std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl;
-               abort();
-       }
+namespace ui {
+       int mousex, mousey;
        
-}
-void UIClass::setFontSize(unsigned int fs){
-       fontSize=fs;
-       FT_Set_Pixel_Sizes(ftf,0,fontSize);
-}
-void UIClass::putString(const float x,const float y,const char *s){
-       unsigned int i=0,j;
-       float xo=x,yo=y,w,h;
-       char *buf;
-       do{
-               if(s[i]=='\n'){
-                       xo=x;
-                       yo-=fontSize*.0022;
-               }else{
-                       FT_Load_Char(ftf,s[i],FT_LOAD_RENDER);
-                       glGenTextures(1,&ftex);
-                       glBindTexture(GL_TEXTURE_2D,ftex);
-                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-                       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-                       buf=(char *)malloc(ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows*4);
-                       for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){
-                               buf[j*4]=255;
-                               buf[j*4+1]=255;
-                               buf[j*4+2]=255;
-                               buf[j*4+3]=ftf->glyph->bitmap.buffer[j]?255:0;
-                       }
-                       glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
-                       w=ftf->glyph->bitmap.width*(2.0/SCREEN_WIDTH);
-                       h=ftf->glyph->bitmap.rows *(2.0/SCREEN_HEIGHT); 
-                       glEnable(GL_TEXTURE_2D);
-                       glBindTexture(GL_TEXTURE_2D,ftex);
-                       if(s[i]=='\''||
-                          s[i]=='\"'||
-                          s[i]=='-'||
-                          s[i]=='*'){
-                               yo+=fontSize*.001;
-                       }
-                       glBegin(GL_QUADS);
-                               glColor3ub(255,255,255);
-                               glTexCoord2f(0,1);glVertex2f(xo,yo);
-                               glTexCoord2f(1,1);glVertex2f(xo+w,yo);
-                               glTexCoord2f(1,0);glVertex2f(xo+w,yo+h);
-                               glTexCoord2f(0,0);glVertex2f(xo,yo+h);
-                       glEnd();
-                       if(s[i]=='\''||
-                          s[i]=='\"'||
-                          s[i]=='-'||
-                          s[i]=='*'){
-                               yo-=fontSize*.001;
-                       }
-                       glDisable(GL_TEXTURE_2D);
-                       xo+=w+(fontSize*.0002);
-                       free(buf);
+       void init(const char *ttf){
+               if(FT_Init_FreeType(&ftl)){
+                       std::cout<<"Error! Couldn't initialize freetype."<<std::endl;
+                       abort();
                }
-       }while(s[i++]);
-}
-void UIClass::putText(const float x,const float y,const char *str,...){
-       va_list args;
-       char *buf;
-       buf=(char *)calloc(128,sizeof(char));
-       va_start(args,str);
-       vsnprintf(buf,128,str,args);
-       va_end(args);
-       putString(x,y,buf);
-       free(buf);
-}
-void UIClass::msgBox(const char *str,...){
-       va_list args;
-       va_start(args,str);
-       glColor3ub(0,0,0);
-       glRectf(-1,.6,1,1);
-       setFontSize(24);
-       putText(-1,1-24*.0022,str,args);
-       va_end(args);
-}
+               if(FT_New_Face(ftl,ttf,0,&ftf)){
+                       std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl;
+                       abort();
+               }       
+       }       
+       void setFontSize(unsigned int fs){
+               fontSize=fs;
+               FT_Set_Pixel_Sizes(ftf,0,fontSize);
+       }
+       void putString(const float x,const float y,const char *s){
+               unsigned int i=0,j;
+               float xo=x,yo=y,w,h;
+               char *buf;
+               do{
+                       if(s[i]=='\n'){
+                               xo=x;
+                               yo-=fontSize*.0022;
+                       }else{
+                               if(FT_Load_Char(ftf,s[i],FT_LOAD_RENDER)){
+                                       std::cout<<"Error! Invalid character."<<std::endl;
+                                       return;
+                               }
+                               glActiveTexture(GL_TEXTURE0);
+                               glGenTextures(1,&ftex);
+                               glBindTexture(GL_TEXTURE_2D,ftex);
+                               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+                               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+                               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+                               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+                               glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+                               buf=(char *)malloc(ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows*4);
+                               for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){
+                                       buf[j*4]=255;
+                                       buf[j*4+1]=255;
+                                       buf[j*4+2]=255;
+                                       buf[j*4+3]=ftf->glyph->bitmap.buffer[j]?255:0;
+                               }
+                               glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
+                               w=ftf->glyph->bitmap.width*(2.0/SCREEN_WIDTH);
+                               h=ftf->glyph->bitmap.rows *(2.0/SCREEN_HEIGHT); 
+                               if(s[i]=='\''||
+                                  s[i]=='\"'||
+                                  s[i]=='-'||
+                                  s[i]=='*'){
+                                       yo+=fontSize*.001;
+                               }
+                               glEnable(GL_TEXTURE_2D);
+                               glBindTexture(GL_TEXTURE_2D,ftex);
+                               glBegin(GL_QUADS);
+                                       glColor3ub(255,255,255);
+                                       glTexCoord2f(0,1);glVertex2f(xo,yo);
+                                       glTexCoord2f(1,1);glVertex2f(xo+w,yo);
+                                       glTexCoord2f(1,0);glVertex2f(xo+w,yo+h);
+                                       glTexCoord2f(0,0);glVertex2f(xo,yo+h);
+                               glEnd();
+                               glDisable(GL_TEXTURE_2D);
+                               if(s[i]=='\''||
+                                  s[i]=='\"'||
+                                  s[i]=='-'||
+                                  s[i]=='*'){
+                                       yo-=fontSize*.001;
+                               }
+                               xo+=w+(fontSize*.0002);
+                               free(buf);
+                       }
+               }while(s[i++]);
+       }
+       void putText(const float x,const float y,const char *str,...){
+               va_list args;
+               char *buf;
+               buf=(char *)calloc(128,sizeof(char));
+               va_start(args,str);
+               vsnprintf(buf,128,str,args);
+               va_end(args);
+               putString(x,y,buf);
+               free(buf);
+       }
+       void msgBox(const char *str,...){
+               va_list args;
+               va_start(args,str);
+               glColor3ub(0,0,0);
+               glRectf(-1,.6,1,1);
+               setFontSize(24);
+               putText(-1,1-24*.0022,str,args);
+               va_end(args);
+       }
 
-void UIClass::handleEvents(){
-       static bool space=false;
-       float thing;
-       SDL_Event e;
-       while(SDL_PollEvent(&e)){
-               switch(e.type){
-               case SDL_MOUSEMOTION:
-                       mousex=e.motion.x;
-                       mousey=e.motion.y;
-                       break;
-               case SDL_WINDOWEVENT:
-                       switch(e.window.event){
-                               case SDL_WINDOWEVENT_CLOSE:
-                                       gameRunning = false;
+       void handleEvents(){
+               static bool space=false;
+               float thing;
+               SDL_Event e;
+               while(SDL_PollEvent(&e)){
+                       switch(e.type){
+                       case SDL_MOUSEMOTION:
+                               mousex=e.motion.x;
+                               mousey=e.motion.y;
                                break;
-                       }
-               case SDL_KEYDOWN:
-                       if(e.key.keysym.sym == SDLK_d) player.right = true;
-                       if(e.key.keysym.sym == SDLK_a) player.left = true;
-                       if(e.key.keysym.sym == SDLK_LSHIFT) player.speed = 3;
-                       if(e.key.keysym.sym == SDLK_SPACE){
-                               if(!space&&player.vel.y<=0){
-                                       space=true;
-                                       player.loc.y += HLINE*1.2;
-                                       player.vel.y += .003;
+                       case SDL_WINDOWEVENT:
+                               switch(e.window.event){
+                                       case SDL_WINDOWEVENT_CLOSE:
+                                               gameRunning = false;
+                                       break;
                                }
-                       }
-                       if(e.key.keysym.sym == SDLK_i){
-                               if(currentWorld->behind){
-                                       thing=(currentWorld->getWidth()-currentWorld->behind->getWidth())/2;
-                                       if(player.loc.x>thing-1&&
-                                          player.loc.x<thing-1+currentWorld->behind->getWidth()){
-                                               player.loc.x-=thing;
-                                               memset(&player.vel,0,sizeof(vec2));
-                                               currentWorld=currentWorld->behind;
+                       case SDL_KEYDOWN:
+                               if(e.key.keysym.sym == SDLK_d) player.right = true;
+                               if(e.key.keysym.sym == SDLK_a) player.left = true;
+                               if(e.key.keysym.sym == SDLK_LSHIFT) player.speed = 3;
+                               if(e.key.keysym.sym == SDLK_SPACE){
+                                       if(!space&&player.vel.y<=0){
+                                               space=true;
+                                               player.loc.y += HLINE*1.2;
+                                               player.vel.y += .003;
                                        }
                                }
-                       }
-                       if(e.key.keysym.sym == SDLK_k){
-                               if(currentWorld->infront){
-                                       player.loc.x+=(currentWorld->infront->getWidth()-currentWorld->getWidth())/2;
-                                       memset(&player.vel,0,sizeof(vec2));
-                                       currentWorld=currentWorld->infront;
+                               if(e.key.keysym.sym == SDLK_i){
+                                       if(currentWorld->behind){
+                                               thing=(currentWorld->getWidth()-currentWorld->behind->getWidth())/2;
+                                               if(player.loc.x>thing-1&&
+                                                  player.loc.x<thing-1+currentWorld->behind->getWidth()){
+                                                       player.loc.x-=thing;
+                                                       memset(&player.vel,0,sizeof(vec2));
+                                                       currentWorld=currentWorld->behind;
+                                               }
+                                       }
                                }
-                       }
-                       break;
-               case SDL_KEYUP:
-                       if(e.key.keysym.sym == SDLK_d) player.right = false;
-                       if(e.key.keysym.sym == SDLK_a) player.left = false;
-                       if(e.key.keysym.sym == SDLK_LSHIFT) player.speed = 1.0;
-                       if(e.key.keysym.sym == SDLK_SPACE)
-                               if(player.vel.y<=.001)space=false;
-               
-                       if(e.key.keysym.sym == SDLK_ESCAPE) gameRunning = false;
-                       break;
-               }       
+                               if(e.key.keysym.sym == SDLK_k){
+                                       if(currentWorld->infront){
+                                               player.loc.x+=(currentWorld->infront->getWidth()-currentWorld->getWidth())/2;
+                                               memset(&player.vel,0,sizeof(vec2));
+                                               currentWorld=currentWorld->infront;
+                                       }
+                               }
+                               break;
+                       case SDL_KEYUP:
+                               if(e.key.keysym.sym == SDLK_d) player.right = false;
+                               if(e.key.keysym.sym == SDLK_a) player.left = false;
+                               if(e.key.keysym.sym == SDLK_LSHIFT) player.speed = 1.0;
+                               if(e.key.keysym.sym == SDLK_SPACE)
+                                       if(player.vel.y<=.001)space=false;
+                       
+                               if(e.key.keysym.sym == SDLK_ESCAPE) gameRunning = false;
+                               break;
+                       }       
+               }
        }
 }
index bde35de3dd27264cc1676e5efdd9c7421fdee00b..49bdf0d011664a387dba6d650d8067ccda46c707 100644 (file)
@@ -24,6 +24,7 @@ World::World(const float width,World *l,World *r){
        toRight=r;\r
        behind=infront=NULL;\r
        entCount=0;\r
+       entity=(void **)calloc(MAX_ENTITIES,sizeof(void *));\r
        if(toLeft){                                                                                                                                     // Make sure linked worlds link back\r
                if(toLeft->toRight){\r
                        std::cout<<"There's already a world to the left!"<<std::endl;\r
@@ -100,7 +101,8 @@ LOOP2:                                                                                                                              // Should be in furthest back layer once this is first rea
        }else{\r
                drawOffsetX=drawOffsetY=0;                                                                      // Reset for next draw() call\r
                for(i=0;i<entCount;i++){                                                                        // Draw any bound entities\r
-                       ((Entity **)entity)[i]->draw();\r
+                       if(entity[i])\r
+                               ((Entity **)entity)[i]->draw();\r
                }\r
        }\r
 }\r
@@ -134,6 +136,6 @@ void World::addLayer(const float width){
                behind->infront=this;\r
        }\r
 }\r
-void World::addEntity(void *e){\r
-       entity[entCount++]=e;           // duh\r
+void World::addEntity(void *addr){\r
+       entity[entCount++]=addr;\r
 }\r
index b418534b9038fa3e34dd9ea46afc3f4479fa26a0..896e02de0d1dba82a7130e4f99012e055f3ab561 100644 (file)
@@ -17,10 +17,11 @@ void Entity::draw(void){
 }
 
 void Entity::wander(int timeRun, vec2 *v){
+       static int hey;
        if(ticksToUse == 0){
                ticksToUse = timeRun;
                v->x = .00010;
-               int hey = (grand() % 3 - 1);
+               hey = (grand() % 3 - 1);
                v->x *= hey;
        }
        ticksToUse--;
index 3ae4206edbe75f82062a9c70acc54e23ffe2b76f..eab1201b105a92fa0898c09dfbc09b21440e0e57 100644 (file)
@@ -3,7 +3,7 @@
 #include <chrono>
 
 #define TICKS_PER_SEC 20
-#define MSEC_PER_TICK 500//(1000/TICKS_PER_SEC)
+#define MSEC_PER_TICK (1000/TICKS_PER_SEC)
 
 SDL_Window    *window = NULL;
 SDL_Surface   *renderSurface = NULL;
@@ -22,7 +22,6 @@ Entity *entnpc[32];   //The NPC base
 Player player;         //The actual player object
 NPC npc[32];
 Structures build;
-UIClass ui;                    //Yep
 World *currentWorld;//u-huh
 
 World *spawn;
@@ -32,10 +31,10 @@ void render();
 
 unsigned int millis(void){
        std::chrono::system_clock::time_point now=std::chrono::system_clock::now();
-       return std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
+       return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
 }
 
-int main(int argc,char **argv){
+int main(/*int argc,char **argv*/){
        //runs start-up procedures
     if(!SDL_Init(SDL_INIT_VIDEO)){
        atexit(SDL_Quit);
@@ -77,7 +76,7 @@ int main(int argc,char **argv){
        ****     GAMELOOP      ****
        **************************/
 
-       ui.init("ttf/VCR_OSD_MONO_1.001.ttf");
+       ui::init("ttf/VCR_OSD_MONO_1.001.ttf");
 
        irand(time(NULL));
        entPlay = &player;
@@ -116,6 +115,7 @@ int main(int argc,char **argv){
                                npc[i].loc.x += npc[i].vel.x * deltaTime;
                        }
            }
+           
                render();
                
                if(prevTime + MSEC_PER_TICK >= millis()){                                               //the logic loop to run at a dedicated time
@@ -170,15 +170,15 @@ void render(){
        glRectf(build.loc.x, build.loc.y, build.loc.x + build.width, build.loc.y + build.height);
        ///BWAHHHHHHHHHHHH
 
-       ui.setFontSize(16);
+       ui::setFontSize(16);
        if(++div==20){
                div=0;
                d=deltaTime;
                fps=(1000/d);
        }
-       ui.putText(-.98 + player.loc.x, .94, "FPS: %1.0f\nDT: %1.0f",fps);
+       ui::putText(-.98 + player.loc.x, .94, "FPS: %1.0f\nDT: %1.0f",fps,d);
        //ui.putText(-.98 + player.loc.x, .88, "DT: %1.0f",d);
-       ui.putText(player.loc.x,player.loc.y-(HLINE*10),"(%+1.3f,%+1.3f)",player.loc.x,player.loc.y);
+       ui::putText(player.loc.x,player.loc.y-(HLINE*10),"(%+1.3f,%+1.3f)",player.loc.x,player.loc.y);
        
        /**************************
        ****  CLOSE THE LOOP   ****
@@ -186,8 +186,8 @@ void render(){
 
        //DRAW MOUSE HERE!!!!!W
        glColor3ub(255,0,0);
-       mx=(ui.mousex/(float)SCREEN_WIDTH)*2.0f-1.0f;
-       my=((SCREEN_HEIGHT-ui.mousey)/(float)SCREEN_HEIGHT)*2.0f-1.0f;
+       mx=(ui::mousex/(float)SCREEN_WIDTH)*2.0f-1.0f;
+       my=((SCREEN_HEIGHT-ui::mousey)/(float)SCREEN_HEIGHT)*2.0f-1.0f;
        if(player.loc.x-1>-1)mx+=player.loc.x;
        glRectf(mx,my,mx+HLINE*4,my+HLINE*4);
 
@@ -197,7 +197,8 @@ void render(){
 
 void logic(){
        float gw;
-       ui.handleEvents();                                                              // Handle events
+       
+       ui::handleEvents();                                                             // Handle events
 
        if(player.right)player.vel.x=.00075;
        else if(player.left)player.vel.x=-.00075;
@@ -221,13 +222,17 @@ void logic(){
                        player.loc.x=-1+HLINE;
                }
        }
-
+       
        currentWorld->detect(&build.loc,&build.vel,build.width);
-       for(int i = 0; i < eAmt(entnpc); i++){
-               if(npc[i].alive == true){
-                       currentWorld->detect(&npc[i].loc,&npc[i].vel,npc[i].width);
-                       entnpc[i]->wander((grand()%181 + 1), &npc[i].vel);
+       /*for(int i = 0; i < 32; i++){
+               if(entnpc[i]->alive){
+                       currentWorld->detect(&entnpc[i]->loc,&entnpc[i]->vel,entnpc[i]->width);
+                       std::cout<<(void *)entnpc[i]<<" "<<i<<" "<<entnpc<<" "<<eAmt(entnpc)<<std::endl;
+                       entnpc[i]->wander(30, &entnpc[i]->vel);
                }
-       }
+       }*/
+       //////////////////////
+       std::cout<<"shit";
+       
        tickCount++;
 }