]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Locked camera to world bounds and added basic mountain backdrop
authordrumsetmonkey <abelleisle@roadrunner.com>
Wed, 28 Oct 2015 15:55:21 +0000 (11:55 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Wed, 28 Oct 2015 15:55:21 +0000 (11:55 -0400)
include/common.h
include/world.h
main.cpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index 1da279c325ce56ed0d3318de3dd62fd3d80019db..6d11c57445626bb7a531e500913e7a66d46fb604 100644 (file)
@@ -49,8 +49,9 @@ typedef struct {
 
 #define GAME_NAME              "Independent Study v.0.2 alpha"
 
-#define SCREEN_WIDTH   1280
-#define SCREEN_HEIGHT  720
+#define SCREEN_WIDTH   1920
+#define SCREEN_HEIGHT  1080
+#define FULLSCREEN
 
 //#define FULLSCREEN
 
@@ -93,6 +94,11 @@ typedef struct {
 
 extern unsigned int deltaTime;
 
+/*
+ *     References the variable in main.cpp, used for drawing with the player
+*/
+extern vec2 offset;
+
 /*
  *     Loads an image from the given file path and attempts to make a texture out of it. The
  *     resulting GLuint is returned (used to recall the texture in glBindTexture).
index 18e1878c071ac5431147a23e29cef880ebe5ce3e..03b0d969b6208a6ffa4e7beda3ccae99412c0514 100644 (file)
@@ -36,6 +36,7 @@ protected:
        void singleDetect(Entity *e);   // Handles an individual entity (gravity n' stuff)
 public:
        unsigned int lineCount;         // Size of the array 'line' (aka the width of the world)
+       unsigned int worldWidth;
        World *toLeft,*toRight;         // Pointers to areas to the left and right of this world. These are made public
                                                                // so that they can easily be set without a function.
        World *infront;
index 4fb5a008877035044c35f873ed28620a2537382f..faa2be4fa5a564c464619cd7c576825282ad4b9c 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -49,7 +49,7 @@ SDL_GLContext  mainGLContext = NULL;
  * 
 */
 
-static GLuint  bgImage, bgTreesFirst;
+static GLuint  bgImage, bgMtn, bgTreesFirst;
 
 /*
  *     gameRunning
@@ -142,6 +142,9 @@ extern void initEverything(void);
 
 /*
  *     mainLoop is in fact the main loop, which runs 'infinitely' (as long as gameRunning
+
+
+
  *     is set). Each loop updates timing values (tickCount and deltaTime), runs logic()
  *     if MSEC_PER_TICK milliseconds have passed, and then runs render().
  * 
@@ -155,6 +158,12 @@ void logic(void);
 void render(void);
 void mainLoop(void);
 
+/* 
+ *     This offset is used as the player offset in the world drawing so
+ *     everything can be moved according to the player
+*/
+vec2 offset;
+
 /*
  *     millis
  * 
@@ -371,7 +380,8 @@ int main(int argc, char *argv[]){
         *      Load a temporary background image.
        */
        
-       bgImage=Texture::loadTexture("assets/bg.png");
+       bgImage=                Texture::loadTexture("assets/bg.png");
+       bgMtn=                  Texture::loadTexture("assets/bgFarMountain.png");
        bgTreesFirst =  Texture::loadTexture("assets/antree.png");
        
        /*
@@ -519,10 +529,24 @@ void render(){
         *      glLoadIdentity  This scales the current matrix back to the origin so the
         *                                      translations are seen normally on a stack.
        */
+
+        /*
+         *     This offset variable is what we use to move the camera and locked
+         *     objects on the screen so they always appear to be in the same relative area
+        */
+       offset = player->loc;
+       offset.x += player->width/2;
+
+       /*
+        *      If the camera will go off of the left  or right of the screen we want to lock it so we can't
+        *  see past the world render
+       */
+       if(player->loc.x - SCREEN_WIDTH/2 < SCREEN_WIDTH*-1.5)offset.x = ((SCREEN_WIDTH*-1.5) + SCREEN_WIDTH/2) + player->width/2;
+       if(player->loc.x + SCREEN_WIDTH/2 > SCREEN_WIDTH*1.5)offset.x = ((SCREEN_WIDTH*1.5) - SCREEN_WIDTH/2) + player->width/2;
        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(player->loc.x-SCREEN_WIDTH/2,player->loc.x+SCREEN_WIDTH/2,0,SCREEN_HEIGHT,-1,1);
+       glOrtho((offset.x-SCREEN_WIDTH/2),(offset.x+SCREEN_WIDTH/2),0,SCREEN_HEIGHT,-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
@@ -562,48 +586,65 @@ void render(){
 
        int base = 40 - (int)worldGetYBase(currentWorld);
 
+       glBindTexture(GL_TEXTURE_2D, bgMtn);
+
+       glColor4ub(150,150,150,220);
+       glBegin(GL_QUADS);
+               glTexCoord2i(0,1);glVertex2i(-1920+offset.x*.85,base);
+               glTexCoord2i(1,1);glVertex2i(      offset.x*.85,base);
+               glTexCoord2i(1,0);glVertex2i(      offset.x*.85,base+1080);
+               glTexCoord2i(0,0);glVertex2i(-1920+offset.x*.85,base+1080);
+       glEnd();
+       glColor4ub(150,150,150,220);
+       glBegin(GL_QUADS);
+               glTexCoord2i(0,1);glVertex2i(     offset.x*.85,base);
+               glTexCoord2i(1,1);glVertex2i(1920+offset.x*.85,base);
+               glTexCoord2i(1,0);glVertex2i(1920+offset.x*.85,base+1080);
+               glTexCoord2i(0,0);glVertex2i(     offset.x*.85,base+1080);
+       glEnd();
+
        glBindTexture(GL_TEXTURE_2D, bgTreesFirst);
 
        glPushMatrix();
-       glColor4ub(100,100,100,220);
+       glColor4ub(100,100,100,240);
        glBegin(GL_QUADS);
-               glTexCoord2i(0,1);glVertex2i(-1920+player->loc.x*.6,base);
-               glTexCoord2i(1,1);glVertex2i(      player->loc.x*.6,base);
-               glTexCoord2i(1,0);glVertex2i(      player->loc.x*.6,base+1080);
-               glTexCoord2i(0,0);glVertex2i(-1920+player->loc.x*.6,base+1080);
+               glTexCoord2i(0,1);glVertex2i(-1920+offset.x*.6,base);
+               glTexCoord2i(1,1);glVertex2i(      offset.x*.6,base);
+               glTexCoord2i(1,0);glVertex2i(      offset.x*.6,base+1080);
+               glTexCoord2i(0,0);glVertex2i(-1920+offset.x*.6,base+1080);
        glEnd();
 
-       glColor4ub(100,100,100,220);
+       glColor4ub(100,100,100,240);
        glBegin(GL_QUADS);
-               glTexCoord2i(0,1);glVertex2i(     player->loc.x*.6,base);
-               glTexCoord2i(1,1);glVertex2i(1920+player->loc.x*.6,base);
-               glTexCoord2i(1,0);glVertex2i(1920+player->loc.x*.6,base+1080);
-               glTexCoord2i(0,0);glVertex2i(     player->loc.x*.6,base+1080);
+               glTexCoord2i(0,1);glVertex2i(     offset.x*.6,base);
+               glTexCoord2i(1,1);glVertex2i(1920+offset.x*.6,base);
+               glTexCoord2i(1,0);glVertex2i(1920+offset.x*.6,base+1080);
+               glTexCoord2i(0,0);glVertex2i(     offset.x*.6,base+1080);
        glEnd();
        glPopMatrix();
 
-       glColor4ub(150,150,150,240);
+       glColor4ub(150,150,150,250);
        glBegin(GL_QUADS);
-               glTexCoord2i(0,1);glVertex2i(-1920+player->loc.x*.4,base);
-               glTexCoord2i(1,1);glVertex2i(      player->loc.x*.4,base);
-               glTexCoord2i(1,0);glVertex2i(      player->loc.x*.4,base+1080);
-               glTexCoord2i(0,0);glVertex2i(-1920+player->loc.x*.4,base+1080);
+               glTexCoord2i(0,1);glVertex2i(-1920+offset.x*.4,base);
+               glTexCoord2i(1,1);glVertex2i(      offset.x*.4,base);
+               glTexCoord2i(1,0);glVertex2i(      offset.x*.4,base+1080);
+               glTexCoord2i(0,0);glVertex2i(-1920+offset.x*.4,base+1080);
        glEnd();
 
-       glColor4ub(150,150,150,240);
+       glColor4ub(150,150,150,250);
        glBegin(GL_QUADS);
-               glTexCoord2i(0,1);glVertex2i(     player->loc.x*.4,base);
-               glTexCoord2i(1,1);glVertex2i(1920+player->loc.x*.4,base);
-               glTexCoord2i(1,0);glVertex2i(1920+player->loc.x*.4,base+1080);
-               glTexCoord2i(0,0);glVertex2i(     player->loc.x*.4,base+1080);
+               glTexCoord2i(0,1);glVertex2i(     offset.x*.4,base);
+               glTexCoord2i(1,1);glVertex2i(1920+offset.x*.4,base);
+               glTexCoord2i(1,0);glVertex2i(1920+offset.x*.4,base+1080);
+               glTexCoord2i(0,0);glVertex2i(     offset.x*.4,base+1080);
        glEnd();
 
        glColor4ub(255,255,255,255);
        glBegin(GL_QUADS);
-               glTexCoord2i(0,1);glVertex2i(-960+player->loc.x*.25,base);
-               glTexCoord2i(1,1);glVertex2i( 960+player->loc.x*.25,base);
-               glTexCoord2i(1,0);glVertex2i( 960+player->loc.x*.25,base+1080);
-               glTexCoord2i(0,0);glVertex2i(-960+player->loc.x*.25,base+1080);
+               glTexCoord2i(0,1);glVertex2i(-960+offset.x*.25,base);
+               glTexCoord2i(1,1);glVertex2i( 960+offset.x*.25,base);
+               glTexCoord2i(1,0);glVertex2i( 960+offset.x*.25,base+1080);
+               glTexCoord2i(0,0);glVertex2i(-960+offset.x*.25,base+1080);
        glEnd();
        glDisable(GL_TEXTURE_2D);
        
@@ -632,7 +673,7 @@ void render(){
                
                ui::setFontSize(16);
                
-               ui::putText(player->loc.x-SCREEN_WIDTH/2,
+               ui::putText(offset.x-SCREEN_WIDTH/2,
                                        SCREEN_HEIGHT-ui::fontSize,
                                        "FPS: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nQc: %u",
                                        fps,
@@ -654,9 +695,9 @@ void render(){
        glColor3ub(255,255,255);
        
        glBegin(GL_TRIANGLES);
-               glVertex2i(ui::mouse.x                  ,ui::mouse.y              );
+               glVertex2i(ui::mouse.x                   ,ui::mouse.y             );
                glVertex2i(ui::mouse.x+HLINE*3.5,ui::mouse.y              );
-               glVertex2i(ui::mouse.x                  ,ui::mouse.y-HLINE*3.5);
+               glVertex2i(ui::mouse.x                   ,ui::mouse.y-HLINE*3.5);
        glEnd();
 
        /**************************
index 1adcbab99b3bc5f51cfa4b8cb71abb707ca9115f..2eb008713af5a37a7da003a1dad806718db497aa 100644 (file)
@@ -86,10 +86,10 @@ int Inventory::takeItem(ITEM_ID id,unsigned char count){
 void Inventory::draw(void){
        unsigned int i=0;
        float y=SCREEN_HEIGHT/2,xoff;
-       ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"Inventory:");
+       ui::putText(offset.x-SCREEN_WIDTH/2,y,"Inventory:");
        while(item[i].count){
                y-=HLINE*12;
-               xoff=ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"%d x ",item[i].count);
+               xoff=ui::putText(offset.x-SCREEN_WIDTH/2,y,"%d x ",item[i].count);
                glEnable(GL_TEXTURE_2D);
                glBindTexture(GL_TEXTURE_2D,ITEM_TEX[item[i].id-1]);
                if(sel==i)glColor3ub(255,0,255);
@@ -101,7 +101,7 @@ void Inventory::draw(void){
                        glTexCoord2i(0,0);glVertex2i(xoff                 ,y+HLINE*10);
                glEnd();
                y-=ui::fontSize*1.15;
-               ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"%s",itemName[(unsigned)item[i].id]);
+               ui::putText(offset.x-SCREEN_WIDTH/2,y,"%s",itemName[(unsigned)item[i].id]);
                glDisable(GL_TEXTURE_2D);
                i++;
        }
index 9d7de8a2f88e52515a2f3d24d15fa0d7c63293de..8f19e3424ef9310d3ffba889ee7947424cc02532 100644 (file)
@@ -174,13 +174,13 @@ namespace ui {
                        putString(x+HLINE,y-fontSize-HLINE,dialogBoxText);
                }
                setFontSize(16);
-               putText(((SCREEN_WIDTH/2)+player->loc.x)-125,SCREEN_HEIGHT-fontSize,"Health: %u/%u",player->health>0?(unsigned)player->health:0,
+               putText(((SCREEN_WIDTH/2)+offset.x)-125,SCREEN_HEIGHT-fontSize,"Health: %u/%u",player->health>0?(unsigned)player->health:0,
                                                                                                                                                                                        (unsigned)player->maxHealth);
                if(player->alive){
                        glColor3ub(255,0,0);
-                       glRectf((SCREEN_WIDTH/2+player->loc.x)-125,
+                       glRectf((SCREEN_WIDTH/2+offset.x)-125,
                                        SCREEN_HEIGHT-32,
-                                       ((SCREEN_WIDTH/2+player->loc.x)-125)+((player->health/player->maxHealth)*100),
+                                       ((SCREEN_WIDTH/2+offset.x)-125)+((player->health/player->maxHealth)*100),
                                        SCREEN_HEIGHT-32+12);
                }
        }
@@ -188,7 +188,7 @@ namespace ui {
                static bool left=false,right=false;
                static vec2 premouse={0,0};
                SDL_Event e;
-               mouse.x=premouse.x+player->loc.x-(SCREEN_WIDTH/2);
+               mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2);
                mouse.y=SCREEN_HEIGHT-premouse.y;
                while(SDL_PollEvent(&e)){
                        switch(e.type){
index 168ac4c4d1e9a469bb448d6beeb611ffe0647169..9fe76b0a2bce44c2027fc53f2513425c7693eff5 100644 (file)
@@ -42,6 +42,7 @@ World::World(void){
 }
 
 void World::generate(unsigned int width){      // Generates the world and sets all variables contained in the World class.
+       worldWidth = width;
        unsigned int i;
        float inc;
        
@@ -199,7 +200,7 @@ LOOP2:
         *      the 'for' loop below that draws the layer.
        */
 
-       v_offset=(p->loc.x + p->width / 2 - current->x_start) / HLINE;
+       v_offset=(offset.x + p->width / 2 - current->x_start) / HLINE;
        
        // is -> i start
        
@@ -208,7 +209,7 @@ LOOP2:
        
        // ie -> i end
        
-       ie=v_offset + (SCREEN_WIDTH / 2 / HLINE) + GEN_INC; 
+       ie=v_offset + (SCREEN_WIDTH / 2 / HLINE) + GEN_INC + HLINE
        if(ie>current->lineCount)ie=current->lineCount;                         // Maximum bound
        
        /*