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
#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
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
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
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);
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;
}
void logic(){
+ static unsigned int lastTime=0;
+ float gw;
ui.handleEvents(); // Handle events
if(player.right == true) {player.vel.x = .00075;}
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++){
entnpc[i]->wander((grand()%91 + 1), &npc[i].vel);
}
}
-
tickCount++;
}