]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
mouse
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 16 Sep 2015 01:59:56 +0000 (21:59 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 16 Sep 2015 01:59:56 +0000 (21:59 -0400)
include/UIClass.h
src/UIClass.cpp
src/main.cpp

index 62d3e7e5a4b6613abcc88dc6f622af22ce57ed1f..e6f48256dbd62e1819d94cfb01b2a4ef98bc8d19 100644 (file)
@@ -13,9 +13,9 @@ public:
        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
-\r
-       float mousex, mousey;\r
+       int mousex, mousey;\r
 };\r
 \r
 #endif // UICLASS_H\r
index 68654c75e71219bf7007cc92ba0061f09c362230..04975756e7df87b97a4a27d5ec69b894922cd2e9 100644 (file)
@@ -30,36 +30,53 @@ void UIClass::putString(const float x,const float y,const char *s){
        float xo=x,yo=y,w,h;
        char *buf;
        do{
-               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;
+               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);
                }
-               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);
-               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);
-               xo+=w+(fontSize*.0001);
-               free(buf);
        }while(s[i++]);
 }
 void UIClass::putText(const float x,const float y,const char *str,...){
@@ -72,6 +89,15 @@ void UIClass::putText(const float x,const float y,const char *str,...){
        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);
+}
 
 void UIClass::handleEvents(){
        static bool space=false;
@@ -79,6 +105,10 @@ void UIClass::handleEvents(){
        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:
@@ -93,7 +123,7 @@ void UIClass::handleEvents(){
                                if(!space&&player.vel.y<=0){
                                        space=true;
                                        player.loc.y += HLINE*1.2;
-                                       player.vel.y += .004;
+                                       player.vel.y += .003;
                                }
                        }
                        if(e.key.keysym.sym == SDLK_i){
index e07e258c8e08d872b156ec2dc26fed0d2d777990..3ae4206edbe75f82062a9c70acc54e23ffe2b76f 100644 (file)
@@ -32,7 +32,7 @@ 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::milliseconds>(now.time_since_epoch()).count();
+       return std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
 }
 
 int main(int argc,char **argv){
@@ -108,13 +108,7 @@ int main(int argc,char **argv){
                currentTime = millis();
                deltaTime = currentTime - prevTime;
 
-               if(prevTime + MSEC_PER_TICK >= millis()){                                               //the logic loop to run at a dedicated time
-                       logic();
-                       prevTime = millis();
-               }
-
                player.loc.x += (player.vel.x * player.speed) * deltaTime;                                              //update the player's x based on 
-               //printf("%lf * %u\n",player.vel.y,deltaTime);
                player.loc.y += player.vel.y * deltaTime;
                for(int i = 0; i < eAmt(entnpc); i++){
                        if(npc[i].alive == true){
@@ -122,10 +116,12 @@ int main(int argc,char **argv){
                                npc[i].loc.x += npc[i].vel.x * deltaTime;
                        }
            }
-           
-           SDL_GetMouseState((int*)(&ui.mousex), (int*)(&ui.mousey));
-           ui.mousey = SCREEN_HEIGHT - ui.mousey;
                render();
+               
+               if(prevTime + MSEC_PER_TICK >= millis()){                                               //the logic loop to run at a dedicated time
+                       logic();
+                       prevTime = millis();
+               }
        }
        
        /**************************
@@ -139,59 +135,64 @@ int main(int argc,char **argv){
 }
 
 void render(){
-                                                                                                               //a matrix is a blank canvas for the computer to draw on, the matrices are stored in a "stack"
-                                                                                                               //GL_PROJECTION has 2 matrices
-                                                                                                               //GL_MODELVIEW has 32 matrices
-               glMatrixMode(GL_PROJECTION);                                    //set the matrix mode as projection so we can set the ortho size and the camera settings later on
-               glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack
-               glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_PROJECTION mode
-               //set the the size of the screen
-               if(player.loc.x-1<-1){
-                       glOrtho(-1,1, -1,1, -1,1);
-               }else if(player.loc.x+1>-1+currentWorld->getWidth()){
-                       glOrtho(-3+currentWorld->getWidth(),-1+currentWorld->getWidth(),-1,1,-1,1);
-               }else{
-                       glOrtho(-1 + player.loc.x, 1 + player.loc.x , -1, 1, -1,1);
-               }
-               glMatrixMode(GL_MODELVIEW);                                     //set the matrix to modelview so we can draw objects
-               glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack
-               glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_MODELVIEW mode
-               glPushMatrix();                                                                 //basically here we put a blank canvas (new matrix) on the screen to draw on
-               glClear(GL_COLOR_BUFFER_BIT);                                   //clear the matrix on the top of the stack
-
-               /**************************
-               **** RENDER STUFF HERE ****
-               **************************/
-               currentWorld->draw(); // layers dont scale x correctly...
-               glColor3ub(120,30,30);                                                  //render the player
-               glRectf(player.loc.x, player.loc.y, player.loc.x + player.width, player.loc.y + player.height);
+       static float mx,my;
+       static float d,fps;
+       static unsigned int div=0;
+                                                                                                       //a matrix is a blank canvas for the computer to draw on, the matrices are stored in a "stack"
+                                                                                                       //GL_PROJECTION has 2 matrices
+                                                                                                       //GL_MODELVIEW has 32 matrices
+       glMatrixMode(GL_PROJECTION);                                    //set the matrix mode as projection so we can set the ortho size and the camera settings later on
+       glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack
+       glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_PROJECTION mode
+       //set the the size of the screen
+       if(player.loc.x-1<-1){
+               glOrtho(-1,1, -1,1, -1,1);
+       }else if(player.loc.x+1>-1+currentWorld->getWidth()){
+               glOrtho(-3+currentWorld->getWidth(),-1+currentWorld->getWidth(),-1,1,-1,1);
+       }else{
+               glOrtho(-1 + player.loc.x, 1 + player.loc.x , -1, 1, -1,1);
+       }
+       glMatrixMode(GL_MODELVIEW);                                     //set the matrix to modelview so we can draw objects
+       glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack
+       glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_MODELVIEW mode
+       glPushMatrix();                                                                 //basically here we put a blank canvas (new matrix) on the screen to draw on
+       glClear(GL_COLOR_BUFFER_BIT);                                   //clear the matrix on the top of the stack
 
-               
-           glColor3ub(255,0,0);
-               glRectf(build.loc.x, build.loc.y, build.loc.x + build.width, build.loc.y + build.height);
-               ///BWAHHHHHHHHHHHH
-               
-               static float d,fps;
-               static unsigned int div=0;
-
-               ui.setFontSize(16);
-               if(++div==20){
-                       div=0;
-                       d=deltaTime;
-                       fps=(1000/d);
-               }
-               ui.putText(-.98 + player.loc.x, .94, "FPS: %1.0f",fps);
-               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);
-               
-               /**************************
-               ****  CLOSE THE LOOP   ****
-               **************************/
+       /**************************
+       **** RENDER STUFF HERE ****
+       **************************/
+       currentWorld->draw(); // layers dont scale x correctly...
+       glColor3ub(120,30,30);                                                  //render the player
+       glRectf(player.loc.x, player.loc.y, player.loc.x + player.width, player.loc.y + player.height);
+
+       
+       glColor3ub(255,0,0);
+       glRectf(build.loc.x, build.loc.y, build.loc.x + build.width, build.loc.y + build.height);
+       ///BWAHHHHHHHHHHHH
+
+       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, .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);
+       
+       /**************************
+       ****  CLOSE THE LOOP   ****
+       **************************/
 
-               //DRAW MOUSE HERE!!!!!W
+       //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;
+       if(player.loc.x-1>-1)mx+=player.loc.x;
+       glRectf(mx,my,mx+HLINE*4,my+HLINE*4);
 
-               glPopMatrix();                                                                  //take the matrix(s) off the stack to pass them to the renderer
-               SDL_GL_SwapWindow(window);                                              //give the stack to SDL to render it
+       glPopMatrix();                                                                  //take the matrix(s) off the stack to pass them to the renderer
+       SDL_GL_SwapWindow(window);                                              //give the stack to SDL to render it
 }
 
 void logic(){