]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
a
authorAndy Belle-Isle <abelleisle@roadrunner.com>
Thu, 10 Sep 2015 15:50:13 +0000 (11:50 -0400)
committerAndy Belle-Isle <abelleisle@roadrunner.com>
Thu, 10 Sep 2015 15:50:13 +0000 (11:50 -0400)
include/World.h
include/common.h
src/UIClass.cpp
src/World.cpp
src/main.cpp

index 212fe3877bef834c0abe680b52c73e1695354f18..6da0972a816740aee6d3c819e8ec661b4bf8d340 100644 (file)
@@ -3,8 +3,6 @@
 
 #include <common.h>
 
-#define HLINE (2.0f/ 200 )
-
 class World {
 private:
        struct line_t {
index 816de5ad2aeae49086e5e86e4ff595db7e3b9159..e4de2c5d5faefe42a60d8e1d1ed9badbd429db5c 100644 (file)
@@ -1,6 +1,8 @@
 #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 <SDL2/SDL_image.h>
 #include <SDL2/SDL_opengl.h>
 #include <UIClass.h>
+#include <entities.h>
 #include <World.h>
 
-#define SCREEN_WIDTH  640
-#define SCREEN_HEIGHT 480
+#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;
 
-//WINODWS VARIABLES
+//WINDOW VARIABLES
 extern bool gameRunning;
 
-#endif // COMMON_H
+#endif // COMMON_H
\ No newline at end of file
index 9cdf735c8c74e6d4eec170c56d5a80d716db5f50..6bb8aecc9cd39a0a56733e3cfa16ea5063495b68 100644 (file)
@@ -1,4 +1,5 @@
 #include <UIClass.h>
+extern Player player;
 
 void UIClass::handleEvents(){
        SDL_Event e;
@@ -9,9 +10,24 @@ void UIClass::handleEvents(){
                        break;
                case SDL_KEYDOWN:
                        switch(e.key.keysym.sym){
-                       case 27:
+                       case 27:                                                ///ESCAPE
                                gameRunning=false;
                                break;
+                       case SDLK_d:                                    ///D
+                               player.velg.x = 10;
+                               break;
+                       case SDLK_a:                                    ///A
+                               player.velg.x = -10;
+                               break;
+                       default:
+                               break;
+                       }
+               case SDL_KEYUP:
+                       switch(e.key.keysym.sym){
+                       /*case SDLK_d:                                  ///D
+                               break;
+                       case SDLK_a:                                    ///A
+                               break;*/
                        default:
                                break;
                        }
index 3d8cfb7e6b8d01ccb7b4d0cea84c9a46f6b30d7d..f38cfc0e14e5a650427b8b089e5c5d84ee68f6ed 100644 (file)
@@ -2,7 +2,7 @@
 
 World::World(float width){
        unsigned int i;
-       lineCount=width/HLINE;
+       lineCount=width/HLINE+1;
        if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){
                std::cout<<"Failed to allocate memory!"<<std::endl;
                abort();
@@ -19,11 +19,11 @@ void World::draw(void){
                        glColor3ub(0,255,0);
                        glVertex2f((HLINE*i)-1      ,line[i].start);
                        glVertex2f((HLINE*i)-1+HLINE,line[i].start);
-                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-0.02);
-                       glVertex2f((HLINE*i)-1      ,line[i].start-0.02);
+                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-HLINE*2);
+                       glVertex2f((HLINE*i)-1      ,line[i].start-HLINE*2);
                        glColor3ub(150,100,50);
-                       glVertex2f((HLINE*i)-1      ,line[i].start-0.02);
-                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-0.02);
+                       glVertex2f((HLINE*i)-1      ,line[i].start-HLINE*2);
+                       glVertex2f((HLINE*i)-1+HLINE,line[i].start-HLINE*2);
                        glVertex2f((HLINE*i)-1+HLINE,-1);
                        glVertex2f((HLINE*i)-1      ,-1);
                }
index cb93ff4c949ca358adbaa32dcb265c7b5b21e41a..f5d7e8b1e9062003ba41bf4c08bc957354c3f18d 100644 (file)
@@ -7,8 +7,29 @@ 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;
 
+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)){
@@ -48,18 +69,54 @@ int main(int argc,char **argv){
        /**************************
        ****     GAMELOOP      ****
        **************************/
-       
-       World *w=new World(2);
+
+       entit1 = &player;
+       entit1->spawn(4, 0);
        
        while(gameRunning){
-               ui.handleEvents();                                                              // Handle events
+               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(0,SCREEN_WIDTH, 0,SCREEN_HEIGHT, -1,1); //set the the size of the screen
+               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
@@ -69,15 +126,10 @@ int main(int argc,char **argv){
                /**************************
                **** RENDER STUFF HERE ****
                **************************/
-               
-               /*glColor3f(1.0f, 0.0f, 0.0f); //color to red
-               glRectf(0,0, 50,50); //draw a test rectangle
-               glColor3f(0.0f, 1.0f, 0.0f); //color to blue
-               glRectf(50,0, 100,50); //draw a test rectangle
-               glColor3f(0.0f, 0.0f, 1.0f); //color to green
-               glRectf(100,0,150,50); //draw a test rectangle*/
-               
+                
                w->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   ****
@@ -85,14 +137,4 @@ int main(int argc,char **argv){
 
                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;
-}
+}
\ No newline at end of file