]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
millis stuff
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 14 Sep 2015 15:53:26 +0000 (11:53 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 14 Sep 2015 15:53:26 +0000 (11:53 -0400)
include/World.h
include/common.h
src/World.cpp
src/main.cpp

index 4d55c5dff3270110e0bae23f40cd6f4e92611df4..fca108d52cf3264b6504a270cb5e754a0f138d8e 100644 (file)
@@ -27,8 +27,6 @@ public:
        void draw(void);                                                                  // Draws the world as well as any bound entities\r
        void detect(vec2 *v,vec2 *vel,const float width); // Object / gravity detection\r
        float getWidth(void);                                                     // Get coordinate width of world\r
-       void saveToFile(FILE *f,World *parent);                   // WIP: Save the world (seed) to a file?\r
-       void loadFromFile(FILE *f,World *parent);                 // No\r
        void addLayer(const float width);                                 // Creates a layer of said width behind the current one\r
        void addEntity(void *e);                                                  // Adds (binds) an entity to the world\r
 };\r
index f32aad08bdc5fb318c6c1fe642687e63c6dfaeee..290e47a263b1ed34f565e66ab423e1d5c422cb7c 100644 (file)
@@ -16,10 +16,11 @@ typedef struct{float x; float y;}vec2;
 
 #define SCREEN_WIDTH  1280
 #define SCREEN_HEIGHT 720
-#define SCREEN_RATIO (float)SCREEN_WIDTH/(float)SCREEN_HEIGHT
+#define SCREEN_RATIO  ((float)SCREEN_WIDTH/(float)SCREEN_HEIGHT)
 //#define FULLSCREEN
 
 #define HLINE (2.0f / (SCREEN_WIDTH / 4))
+//#define HLINE (SCREEN_RATIO)
 
 #define irand srand
 #define grand rand
index 8083d479547cd0fa9a52765ff2584223ed545ab5..0e8d8fc790c56299eae3e45562ac082d11bbe47a 100644 (file)
@@ -110,10 +110,10 @@ void World::detect(vec2 *v,vec2 *vel,const float width){
                if(v->y<line[i].start){                                                                                         // If we're inside the line\r
                        if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){                                   // And we're inside it ;)\r
                                vel->y=0;v->y=line[i].start+HLINE/8;                                                    // Correct\r
-                               return; // :/\r
+//                             return; // :/\r
                        }else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){ // Same as above, but coming from right side instead of left\r
                                vel->y=0;v->y=line[i].start+HLINE/8;\r
-                               return; // ;)\r
+//                             return; // ;)\r
                        }\r
                }else if(v->y>line[i].start+HLINE){                                                                     // Trashy gravity handling\r
                        vel->y-=.0000001;\r
@@ -124,31 +124,6 @@ void World::detect(vec2 *v,vec2 *vel,const float width){
 float World::getWidth(void){\r
        return (lineCount-11)*HLINE;\r
 }\r
-// no\r
-void World::saveToFile(FILE *f,World *parent){\r
-       fwrite(&lineCount,sizeof(unsigned int) ,1        ,f);\r
-       fwrite(&line     ,sizeof(struct line_t),lineCount,f);\r
-       if(toLeft!=NULL&&toLeft!=parent->toLeft){\r
-               toLeft->saveToFile(f,toLeft);\r
-       }\r
-       if(toRight!=NULL&&toRight!=parent->toRight){\r
-               toRight->saveToFile(f,toRight);\r
-       }\r
-}\r
-// no\r
-void World::loadFromFile(FILE *f,World *parent){\r
-       fread(&lineCount,sizeof(unsigned int) ,1        ,f);\r
-       line=(struct line_t *)malloc(lineCount*sizeof(struct line_t *));\r
-       fread(&line     ,sizeof(struct line_t),lineCount,f);\r
-       if(toLeft!=NULL&&toLeft!=parent->toLeft){\r
-               toLeft->loadFromFile(f,toLeft);\r
-       }\r
-       std::cout<<toRight<<" "<<parent->toRight<<std::endl;\r
-       if(toRight!=NULL&&toRight!=parent->toRight){\r
-               puts("A");\r
-               toRight->loadFromFile(f,toRight);\r
-       }\r
-}\r
 void World::addLayer(const float width){\r
        if(behind){                                                                     // If there's already a layer behind us\r
                behind->addLayer(width);                                // Add it back there\r
index bed75c8e94203e848679ab514f8cf66d991c2c55..9f87c2b769ba7f56288e7a00afed5cc94aed319f 100644 (file)
@@ -28,7 +28,13 @@ World *currentWorld;//u-huh
 void logic();
 void render();
 
+static unsigned int initTime;
+unsigned int millis(void){
+       return ((float)(clock()-initTime)/(float)CLOCKS_PER_SEC)*2000;
+}
+
 int main(int argc,char **argv){
+       float gw;
     //runs start-up procedures
     if(!SDL_Init(SDL_INIT_VIDEO)){
        atexit(SDL_Quit);
@@ -93,23 +99,22 @@ int main(int argc,char **argv){
        for(jklasdf=0;jklasdf<npcAmt;jklasdf++){
                currentWorld->addEntity((void *)entnpc[jklasdf]);
        }
-
-       float gw;
        
+       initTime=clock();
+       currentTime=initTime;
        
        while(gameRunning){
                prevTime = currentTime;
-               currentTime = SDL_GetTicks();
+               currentTime = millis();
                deltaTime = currentTime - prevTime;
 
-               if(prevTime + MSEC_PER_TICK >= SDL_GetTicks()){                                         //the logic loop to run at a dedicated time
+               if(prevTime + MSEC_PER_TICK >= millis()){                                               //the logic loop to run at a dedicated time
                        logic();
-                       prevTime = SDL_GetTicks();
+                       prevTime = millis();
                }
 
                player.loc.x += (player.vel.x * player.speed) * deltaTime;                                              //update the player's x based on 
                player.loc.y += player.vel.y * deltaTime;
-
                for(int i = 0; i < eAmt(entnpc); i++){
                        if(npc[i].alive == true){
                                npc[i].loc.y += npc[i].vel.y * deltaTime;
@@ -194,6 +199,8 @@ void render(){
 }
 
 void logic(){
+       static unsigned int lastTime=0;
+       float gw;
        ui.handleEvents();                                                              // Handle events
 
        if(player.right == true) {player.vel.x = .00075;}
@@ -201,10 +208,27 @@ void logic(){
        if(player.right == false && player.left == false) {player.vel.x = 0;}
 
        //std::cout<<"\r("<<player.loc.x<<","<<player.loc.y<<")";
-       //std::cout << tickCount << std::endl;
-
+       std::cout<<millis()-lastTime<<std::endl;
+       lastTime=millis();
 
        currentWorld->detect(&player.loc,&player.vel,player.width);
+       gw=currentWorld->getWidth();
+       if(player.loc.x+player.width>-1+gw){
+               if(currentWorld->toRight){
+                       goWorldRight(currentWorld)
+                       player.loc.x=-1+HLINE;
+               }else{
+                       player.loc.x=gw-1-player.width-HLINE;
+               }
+       }
+       if(player.loc.x<-1){
+               if(currentWorld->toLeft){
+                       goWorldLeft(currentWorld);
+                       player.loc.x=currentWorld->getWidth()-1-player.width-HLINE;
+               }else{
+                       player.loc.x=-1+HLINE;
+               }
+       }
 
        currentWorld->detect(&build.loc,&build.vel,build.width);
        for(int i = 0; i < eAmt(entnpc); i++){
@@ -213,6 +237,5 @@ void logic(){
                        entnpc[i]->wander((grand()%91 + 1), &npc[i].vel);
                }
        }
-
        tickCount++;
 }