]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
fixed
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 10 Sep 2015 19:24:39 +0000 (15:24 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 10 Sep 2015 19:24:39 +0000 (15:24 -0400)
1  2 
include/World.h
include/common.h
src/main.cpp

diff --cc include/World.h
index 3b4b56d803cc00e74be8de496a983bad8bf975ec,dd133b9bba37ea7c41f32b17d58c6eed2629583e..d8440a07a8eb9ee7e69d0809d75f1eeeddfce653
@@@ -1,24 -1,42 +1,24 @@@
 -<<<<<<< HEAD
 -#ifndef WORLD_H
 -#define WORLD_H
 -
 -#include <common.h>
 -
 -class World {
 -private:
 -      struct line_t {
 -              // x = 2.0 (window width) / HLINES
 -              float start; // Where to change to dirt, going down (y)
 -      } *line;
 -      unsigned int lineCount;
 -public:
 -      World(float width);
 -      void draw(void);
 -};
 -
 +#ifndef WORLD_H\r
 +#define WORLD_H\r
 +\r
 +#include <common.h>\r
 +\r
 +#define goWorldLeft(w)  if(w->toLeft){w=w->toLeft;}\r
 +#define goWorldRight(w) if(w->toRight){w=w->toRight;}\r
 +\r
 +class World {\r
 +private:\r
 +      struct line_t {\r
 +              // x = 2.0 (window width) / HLINES\r
 +              double start; // Where to change to dirt, going down (y)\r
 +      } *line;\r
 +      unsigned int lineCount;\r
 +public:\r
 +      World *toLeft,*toRight;\r
 +      World(void);\r
 +      World(const float width,World *l,World *r);\r
 +      void draw(void);\r
 +      void detect(vec2 *v,const float width);\r
 +};\r
 +\r
- #endif // WORLD_H\r
+ #endif // WORLD_H
 -=======
 -#ifndef WORLD_H
 -#define WORLD_H
 -
 -#include <common.h>
 -
 -#define HLINE (2.0f/(SCREEN_WIDTH/4))
 -
 -class World {
 -private:
 -      struct line_t {
 -              // x = 2.0 (window width) / HLINES
 -              double start; // Where to change to dirt, going down (y)
 -      } *line;
 -      unsigned int lineCount;
 -public:
 -      World(float width);
 -      void draw(void);
 -      void detect(vec2 *v,const float width);
 -};
 -
 -#endif // WORLD_H
 ->>>>>>> origin/master
index 9ce1be08089f681c973b4d14b0cbdb36de6bb44e,48a068d950d99db0919c2e4bd61aee05e7ba8357..df5b6ac12410c0480d2208582dc631afef87605d
@@@ -1,31 -1,65 +1,31 @@@
- #ifndef COMMON_H\r
- #define COMMON_H\r
\r
- typedef struct{float x; float y;}vec2;\r
\r
- ///THIS FILE IS USED FOR VARIABLES THAT WILL BE ACCESED BY MULTIPLE CLASSES/FILES\r
\r
- #include <iostream>\r
- #include <cstdlib>\r
- #include <SDL2/SDL.h>\r
- #include <SDL2/SDL_image.h>\r
- #include <SDL2/SDL_opengl.h>\r
- #include <UIClass.h>\r
- #include <entities.h>\r
- #include <World.h>\r
\r
- #define SCREEN_WIDTH  1280\r
- #define SCREEN_HEIGHT 800\r
- #define FULLSCREEN\r
\r
- #define HLINE (2.0f/(SCREEN_WIDTH/4))\r
\r
- //SDL VARIABLES\r
- extern SDL_Window    *window;\r
- extern SDL_Surface   *renderSurface;\r
- extern SDL_GLContext  mainGLContext;\r
\r
- //WINODWS VARIABLES\r
- extern bool gameRunning;\r
\r
- #endif // COMMON_H\r
 -<<<<<<< HEAD
+ #ifndef COMMON_H
+ #define COMMON_H
+ typedef struct{float x; float y;}vec2;
+ ///THIS FILE IS USED FOR VARIABLES THAT WILL BE ACCESED BY MULTIPLE CLASSES/FILES
+ #include <iostream>
+ #include <cstdlib>
+ #include <SDL2/SDL.h>
+ #include <SDL2/SDL_image.h>
+ #include <SDL2/SDL_opengl.h>
+ #include <UIClass.h>
+ #include <entities.h>
+ #include <World.h>
+ #define SCREEN_WIDTH  1280
+ #define SCREEN_HEIGHT 720
+ //#define FULLSCREEN
+ #define HLINE (2.0f / (SCREEN_WIDTH / 4))
+ //SDL VARIABLES
+ extern SDL_Window    *window;
+ extern SDL_Surface   *renderSurface;
+ extern SDL_GLContext  mainGLContext;
+ //WINDOW VARIABLES
+ extern bool gameRunning;
+ #endif // COMMON_H
 -=======
 -#ifndef COMMON_H
 -#define COMMON_H
 -
 -typedef struct{float x; float y;}vec2;
 -
 -///THIS FILE IS USED FOR VARIABLES THAT WILL BE ACCESED BY MULTIPLE CLASSES/FILES
 -
 -#include <iostream>
 -#include <cstdlib>
 -#include <SDL2/SDL.h>
 -#include <SDL2/SDL_image.h>
 -#include <SDL2/SDL_opengl.h>
 -#include <UIClass.h>
 -#include <entities.h>
 -#include <World.h>
 -
 -#define SCREEN_WIDTH  1280
 -#define SCREEN_HEIGHT 800
 -#define FULLSCREEN
 -
 -
 -
 -//SDL VARIABLES
 -extern SDL_Window    *window;
 -extern SDL_Surface   *renderSurface;
 -extern SDL_GLContext  mainGLContext;
 -
 -//WINODWS VARIABLES
 -extern bool gameRunning;
 -
 -#endif // COMMON_H
 ->>>>>>> origin/master
diff --cc src/main.cpp
index fa3a9c403c8004b58055593c44759af2b0430fe5,e5c2339c2819564fe4ccb455c1ca389e0cf52e52..8bc01e2834cb6e3c5393b8cb3cf3dc1576408e43
- #include <common.h>\r
- #include <ctime>\r
\r
- SDL_Window    *window = NULL;\r
- SDL_Surface   *renderSurface = NULL;\r
- SDL_GLContext  mainGLContext = NULL;\r
\r
- bool gameRunning = true;\r
\r
- UIClass ui;\r
- Entities *entit1;\r
- Player player;\r
- World *currentWorld;\r
\r
- int main(int argc,char **argv){\r
-     // Initialize SDL\r
-     if(!SDL_Init(SDL_INIT_VIDEO)){\r
-       atexit(SDL_Quit);\r
-     }else{\r
-               std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl;\r
-               return -1;\r
-       }\r
-       // Initialize SDL_image\r
-       if((IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG)&(IMG_INIT_PNG|IMG_INIT_JPG))){\r
-               atexit(IMG_Quit);\r
-       }else{\r
-               std::cout<<"Could not init image libraries!\n"<<std::endl;\r
-               return -1;\r
-       }\r
-       // Create the window\r
-     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);\r
-     window = SDL_CreateWindow("Independent Study v.0.1 alpha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL\r
-                               #ifdef FULLSCREEN\r
-                               | SDL_WINDOW_FULLSCREEN\r
-                               #endif // FULLSCREEN\r
-                               );\r
-       if(!window){\r
-               std::cout << "The window failed to generate! Error: " << SDL_GetError() << std::endl;\r
-               return -1;\r
-       }\r
-       // Set OpenGL context\r
-       if((mainGLContext = SDL_GL_CreateContext(window))==NULL){\r
-               std::cout << "The OpenGL context failed to initialize! Error: " << SDL_GetError() << std::endl;\r
-       }\r
-       // Setup rand() and OpenGL\r
-       srand(time(NULL));\r
-       glClearColor(.3,.5,.8,0);\r
-       glEnable(GL_BLEND);\r
-       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);\r
-       \r
-       /**************************\r
-       ****     GAMELOOP      ****\r
-       **************************/\r
\r
-       entit1 = &player;\r
-       entit1->spawn(0,0);\r
\r
-       World *w=NULL;\r
-       World *w2=new World(4,w,NULL);\r
-       w=new World(2,NULL,w2);\r
-       currentWorld=w;\r
-       \r
-       while(gameRunning){\r
-               ui.handleEvents();                                                              // Handle events\r
-                                                                                                               //a matrix is a blank canvas for the computer to draw on, the matrices are stored in a "stack"\r
-                                                                                                               //GL_PROJECTION has 2 matrices\r
-                                                                                                               //GL_MODELVIEW has 32 matrices\r
-               glMatrixMode(GL_PROJECTION);                                    //set the matrix mode as projection so we can set the ortho size and the camera settings later on\r
-               glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack\r
-               glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_PROJECTION mode\r
-               glOrtho(-1,1,-1,1,-1,1);                                                //set the the size of the screen\r
-               glMatrixMode(GL_MODELVIEW);                                     //set the matrix to modelview so we can draw objects\r
-               glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack\r
-               glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_MODELVIEW mode\r
-               glPushMatrix();                                                                 //basically here we put a blank canvas (new matrix) on the screen to draw on\r
-               glClear(GL_COLOR_BUFFER_BIT);                                   //clear the matrix on the top of the stack\r
\r
-               /**************************\r
-               **** RENDER STUFF HERE ****\r
-               **************************/\r
-                \r
-               currentWorld->draw();\r
-               glColor3ub(0,0,0);\r
-               glRectf(player.loc.x, player.loc.y, player.loc.x + player.width, player.loc.y + player.height);\r
-               \r
-               /**************************\r
-               ****  CLOSE THE LOOP   ****\r
-               **************************/\r
\r
-               glPopMatrix();                                                                  //take the matrix(s) off the stack to pass them to the renderer\r
-               SDL_GL_SwapWindow(window);                                              //give the stack to SDL to render it\r
-       }\r
-       \r
-       /**************************\r
-       ****  CLOSE PROGRAM    ****\r
-       **************************/\r
-       \r
-     //closes the window and frees resources\r
-     SDL_GL_DeleteContext(mainGLContext);\r
-     SDL_DestroyWindow(window);\r
-     return 0;\r
- }\r
 -<<<<<<< HEAD
+ #include <common.h>
+ #include <ctime>
+ SDL_Window    *window = NULL;
+ SDL_Surface   *renderSurface = NULL;
+ SDL_GLContext  mainGLContext = NULL;
+ bool gameRunning = true;
+ const float ticksPerSec = 20;
+ const float msecPerTick = 1000 / ticksPerSec;
+ int prevTime = GetTickCount();
+ int currentTime = 0;
+ float deltaTime = 0;
+ Entities *entit1;
+ Player player;
+ UIClass ui;
++World *currentWorld;
+ float interpolate(float goal, float current, float dt){
+       float difference = goal - current;
+       if(difference > dt){
+               return current + dt;}
+       if(difference < dt){
+               return current - dt;}
+       return goal;
+ }
 -
 -World *w=new World(2);
+ void render();
+ int main(int argc,char **argv){
+     //runs start-up procedures
+     if(!SDL_Init(SDL_INIT_VIDEO)){
+       atexit(SDL_Quit);
+       if(!(IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG)&(IMG_INIT_PNG|IMG_INIT_JPG))){
+                       std::cout<<"Could not init image libraries!\n"<<std::endl;
+                       return -1;
+               }
+               atexit(IMG_Quit);
+               //Turn on double Buffering
+         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+         //create the window
+         window = SDL_CreateWindow("Independent Study v.0.1 alpha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
+                                   #ifdef FULLSCREEN
+                                   | SDL_WINDOW_FULLSCREEN
+                                   #endif // FULLSCREEN
+                                   );
+         if(window){
+               //set OpenGL context
+             mainGLContext = SDL_GL_CreateContext(window);
+             if(mainGLContext == NULL){
+               std::cout << "The OpenGL context failed to initialize! Error: " << SDL_GetError() << std::endl;
+             }
+               }else{
+                       std::cout << "The window failed to generate! Error: " << SDL_GetError() << std::endl;
+               return -1;
+         }
+     }else{
+               std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl;
+               return -1;
+       }
+       srand(time(NULL));
+       glClearColor(.3,.5,.8,0);
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+       
+       /**************************
+       ****     GAMELOOP      ****
+       **************************/
+       entit1 = &player;
+       entit1->spawn(4, 0);
++
++      World *w=NULL;
++      World *w2=new World(4,w,NULL);
++      w=new World(2,NULL,w2);
++      currentWorld=w;
+       
+       while(gameRunning){
+               prevTime = currentTime;
+               currentTime = GetTickCount();
+               deltaTime = currentTime - prevTime;
+               if(prevTime + msecPerTick <= GetTickCount()){           //HANDLE ALL LOGIC STUFF HERE
+                       ui.handleEvents();                                                              // Handle events
+                       player.vel.x = 0;
+                       std::cout << player.vel.x << std::endl;
+                       std::cout << player.velg.y << std::endl;
+                       std::cout << "d:" << deltaTime << std::endl;
+                       prevTime = GetTickCount();
+               }
+                                                                                                                       //DO ALL RENDERING HERE         
+               player.vel.x = interpolate(player.velg.x, player.vel.x, deltaTime) * .001;
+               if(player.vel.x > .05) player.vel.x = .05;
+               if(player.vel.x < -.05) player.vel.x = -.05;
+               player.loci.x += player.vel.x;
+               render();
+       }
+       
+       /**************************
+       ****  CLOSE PROGRAM    ****
+       **************************/
+       
+     //closes the window and frees resources
+     SDL_GL_DeleteContext(mainGLContext);
+     SDL_DestroyWindow(window);
+     return 0;
+ }
+ 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
+               glOrtho(-1 + player.loci.x, 1 + player.loci.x , -1, 1, -1,1); //set the the size of the screen
+               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 ****
+               **************************/
+                
 -              w->draw();
++              currentWorld->draw();
+               glColor3ub(0,0,0);
+               glRectf(player.loci.x, player.loci.y, player.loci.x + player.width, player.loci.y + player.height);
+               
+               /**************************
+               ****  CLOSE THE LOOP   ****
+               **************************/
+               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
+ }
 -=======
 -#include <common.h>
 -#include <ctime>
 -
 -SDL_Window    *window = NULL;
 -SDL_Surface   *renderSurface = NULL;
 -SDL_GLContext  mainGLContext = NULL;
 -
 -bool gameRunning = true;
 -
 -UIClass ui;
 -Entities *entit1;
 -Player player;
 -
 -int main(int argc,char **argv){
 -    // Initialize SDL
 -    if(!SDL_Init(SDL_INIT_VIDEO)){
 -      atexit(SDL_Quit);
 -    }else{
 -              std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl;
 -              return -1;
 -      }
 -      // Initialize SDL_image
 -      if((IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG)&(IMG_INIT_PNG|IMG_INIT_JPG))){
 -              atexit(IMG_Quit);
 -      }else{
 -              std::cout<<"Could not init image libraries!\n"<<std::endl;
 -              return -1;
 -      }
 -      // Create the window
 -    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 -    window = SDL_CreateWindow("Independent Study v.0.1 alpha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
 -                              #ifdef FULLSCREEN
 -                              | SDL_WINDOW_FULLSCREEN
 -                              #endif // FULLSCREEN
 -                              );
 -      if(!window){
 -              std::cout << "The window failed to generate! Error: " << SDL_GetError() << std::endl;
 -              return -1;
 -      }
 -      // Set OpenGL context
 -      if((mainGLContext = SDL_GL_CreateContext(window))==NULL){
 -              std::cout << "The OpenGL context failed to initialize! Error: " << SDL_GetError() << std::endl;
 -      }
 -      // Setup rand() and OpenGL
 -      srand(time(NULL));
 -      glClearColor(.3,.5,.8,0);
 -      glEnable(GL_BLEND);
 -      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
 -      
 -      /**************************
 -      ****     GAMELOOP      ****
 -      **************************/
 -
 -      entit1 = &player;
 -      entit1->spawn(0,0);
 -
 -      World *w=new World(2);
 -      
 -      while(gameRunning){
 -              ui.handleEvents();                                                              // Handle events
 -                                                                                                              //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
 -              glOrtho(-1,1,-1,1,-1,1);                                                //set the the size of the screen
 -              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 ****
 -              **************************/
 -               
 -              w->draw();
 -              glColor3ub(0,0,0);
 -              glRectf(player.loc.x, player.loc.y, player.loc.x + player.width, player.loc.y + player.height);
 -              
 -              /**************************
 -              ****  CLOSE THE LOOP   ****
 -              **************************/
 -
 -              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
 -      }
 -      
 -      /**************************
 -      ****  CLOSE PROGRAM    ****
 -      **************************/
 -      
 -    //closes the window and frees resources
 -    SDL_GL_DeleteContext(mainGLContext);
 -    SDL_DestroyWindow(window);
 -    return 0;
 -}
 ->>>>>>> origin/master