]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
day/night cycling
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 30 Oct 2015 12:45:55 +0000 (08:45 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 30 Oct 2015 12:45:55 +0000 (08:45 -0400)
Changelog
assets/bgn.png [new file with mode: 0644]
config/quest_list.txt
include/common.h
include/inventory.h
include/world.h
main.cpp
src/common.cpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index 6283ad9477fe65a541c31c71de9ca7c09adbf800..0bb86d9b541aa8a5e261a046e3b91762f9b19207 100644 (file)
--- a/Changelog
+++ b/Changelog
 
        - the currently selected item is now drawn on the player
        - pressing q discards (w/ visuals) the currently selected item
+
+10/30/2015:
+===========
+
+       - fixed bug involving grass pressing and platforms
+       - added a day/night cycle, with shading on all drawn
+         objects except for entities
+       - added stars at night
+       - successfully enabled and loaded GLSL shaders
diff --git a/assets/bgn.png b/assets/bgn.png
new file mode 100644 (file)
index 0000000..2aa3995
Binary files /dev/null and b/assets/bgn.png differ
index 3014ede6895d44f0a795da2d8d319bb920f3dd42..d23743ecc0dc5a438eb42fcee64fd0fc4a5cce59 100644 (file)
@@ -1 +1 @@
-TITLE "Test" DESC "A test quest" REWARD 1 x TEST_ITEM END
+TITLE "Test" DESC "A test quest" REWARD 1 x SWORD_ITEM END
index c3b1aed5e3b367713894370c4c567aa54df1d7dd..ce176a6d6c96a5c036ebe2f49e8b724eed312204 100644 (file)
@@ -114,4 +114,7 @@ extern vec2 offset;
 
 void DEBUG_prints(const char* file, int line, const char *s,...);
 
+void safeSetColor(int r,int g,int b);
+void safeSetColorA(int r,int g,int b,int a);
+
 #endif // COMMON_H
index e27391a2751267e5782d912f5b1ccc4d72f8c597..d2b4c2858975972c0b318761a4bc5e9b4336da86 100644 (file)
@@ -30,7 +30,7 @@ public:
        
        int addItem(ITEM_ID id,unsigned char count);    // Add 'count' items with an id of 'id' to the inventory
        int takeItem(ITEM_ID id,unsigned char count);   // Take 'count' items with an id of 'id' from the inventory
-       int useItem(ITEM_ID id);
+       int useItem(void);
        
        bool tossd;
        int itemToss(void);
@@ -42,5 +42,6 @@ public:
 };
 
 unsigned int initInventorySprites(void);       // Loads as many inventory textures as it can find, returns count
+void itemUse(void *p);
 
 #endif // INVENTORY_H
index 5ec01aec873248397b7f3983c33928bca2167a77..75263e55e01e5413f00b57caebd6088df33e3e72 100644 (file)
@@ -154,4 +154,6 @@ public:
        void draw(Player *p);                           // Draws the world (ignores layers)
 };
 
+extern int worldShade;
+
 #endif // WORLD_H
index 929d1c8a98c933d37c50a2f94c0ee9ade75794a7..e7ecf37077c963fcadd1096a4a18bee0ffa2ef71 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -49,7 +49,7 @@ SDL_GLContext  mainGLContext = NULL;
  * 
 */
 
-static GLuint  bgImage, bgMtn, bgTreesFront, bgTreesMid, bgTreesFar;
+static GLuint  bgDay, bgNight, bgMtn, bgTreesFront, bgTreesMid, bgTreesFar;
 
 /*
  *     gameRunning
@@ -178,6 +178,18 @@ unsigned int millis(void){
        return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
 }
 
+typedef enum {
+       SUNNY = 0,
+       DARK,
+       RAIN
+} WEATHER;
+
+#define DAY_CYCLE 10000
+
+static WEATHER weather = SUNNY;
+static vec2 star[100];
+
+
 /*******************************************************************************
  * MAIN ************************************************************************
  *******************************************************************************/
@@ -381,11 +393,12 @@ int main(int argc, char *argv[]){
         *      Load a temporary background image.
        */
        
-       bgImage=                Texture::loadTexture("assets/bg.png");
-       bgMtn=                  Texture::loadTexture("assets/bgFarMountain.png");
-       bgTreesFront =  Texture::loadTexture("assets/bgFrontTree.png");
-       bgTreesMid =    Texture::loadTexture("assets/bgMidTree.png");
-       bgTreesFar =    Texture::loadTexture("assets/bgFarTree.png");
+       bgDay            =Texture::loadTexture("assets/bg.png"                   );
+       bgNight          =Texture::loadTexture("assets/bgn.png"                  );
+       bgMtn            =Texture::loadTexture("assets/bgFarMountain.png");
+       bgTreesFront =Texture::loadTexture("assets/bgFrontTree.png"      );
+       bgTreesMid       =Texture::loadTexture("assets/bgMidTree.png"    );
+       bgTreesFar       =Texture::loadTexture("assets/bgFarTree.png"    );
        
        /*
         *      Load sprites used in the inventory menu. See src/inventory.cpp
@@ -393,6 +406,12 @@ int main(int argc, char *argv[]){
        
        initInventorySprites();
        
+       unsigned int i;
+       for(i=0;i<100;i++){
+               star[i].x=getRand()%currentWorld->getTheWidth()-currentWorld->getTheWidth()/2;
+               star[i].y=getRand()%SCREEN_HEIGHT+100;
+       }
+       
        /**************************
        ****     GAMELOOP      ****
        **************************/
@@ -578,7 +597,16 @@ void render(){
        */
 
        glEnable(GL_TEXTURE_2D);
-       glBindTexture(GL_TEXTURE_2D,bgImage);
+       glBindTexture(GL_TEXTURE_2D,bgDay);
+       safeSetColorA(255,255,255,255-worldShade*4);
+       glBegin(GL_QUADS);
+               glTexCoord2i(0,1);glVertex2i(-SCREEN_WIDTH*2+offset.x,0);
+               glTexCoord2i(1,1);glVertex2i( SCREEN_WIDTH*2+offset.x,0);
+               glTexCoord2i(1,0);glVertex2i( SCREEN_WIDTH*2+offset.x,SCREEN_HEIGHT);
+               glTexCoord2i(0,0);glVertex2i(-SCREEN_WIDTH*2+offset.x,SCREEN_HEIGHT);
+       glEnd();
+       glBindTexture(GL_TEXTURE_2D,bgNight);
+       safeSetColorA(255,255,255,worldShade*4);
        glBegin(GL_QUADS);
                glTexCoord2i(0,1);glVertex2i(-SCREEN_WIDTH*2+offset.x,0);
                glTexCoord2i(1,1);glVertex2i( SCREEN_WIDTH*2+offset.x,0);
@@ -586,11 +614,27 @@ void render(){
                glTexCoord2i(0,0);glVertex2i(-SCREEN_WIDTH*2+offset.x,SCREEN_HEIGHT);
        glEnd();
 
+       glDisable(GL_TEXTURE_2D);
+
+       if(((weather==DARK )&(tickCount%DAY_CYCLE)<DAY_CYCLE/2)   ||
+          ((weather==SUNNY)&(tickCount%DAY_CYCLE)>DAY_CYCLE*.75) ){
+               if(tickCount%DAY_CYCLE){        
+                       glColor4ub(255,255,255,255);
+                       for(unsigned int i=0;i<100;i++){
+                               glRectf(star[i].x+offset.x*.9,star[i].y,star[i].x+offset.x*.9+HLINE,star[i].y+HLINE);
+                       }
+               }
+       }
+
        int base = 40 - (int)worldGetYBase(currentWorld);
+       int shade = worldShade*2;
+
+       glEnable(GL_TEXTURE_2D);
 
        glBindTexture(GL_TEXTURE_2D, bgMtn);
        glBegin(GL_QUADS);
-       glColor4ub(150,150,150,220);
+       safeSetColorA(150-shade,150-shade,150-shade,220);
+       //glColor4ub(150,150,150,220);
                for(int i = 0; i <= currentWorld->getTheWidth()/1920; i++){
                glTexCoord2i(0,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * i)+offset.x*.85,base);
                glTexCoord2i(1,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * (i+1))+offset.x*.85,base);
@@ -602,7 +646,8 @@ void render(){
 
        glBindTexture(GL_TEXTURE_2D, bgTreesFar);
        glBegin(GL_QUADS);
-       glColor4ub(100,100,100,240);
+       safeSetColorA(100-shade,100-shade,100-shade,240);
+       //glColor4ub(100,100,100,240);
        for(int i = 0; i <= currentWorld->getTheWidth()/1920; i++){
                glTexCoord2i(0,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * i)+offset.x*.6,base);
                glTexCoord2i(1,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * (i+1))+offset.x*.6,base);
@@ -613,7 +658,8 @@ void render(){
 
        glBindTexture(GL_TEXTURE_2D, bgTreesMid);
        glBegin(GL_QUADS);
-       glColor4ub(150,150,150,250);
+       safeSetColorA(150-shade,150-shade,150-shade,250);
+       //glColor4ub(150,150,150,250);
        for(int i = 0; i <= currentWorld->getTheWidth()/1920; i++){
                glTexCoord2i(0,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * i)+offset.x*.4,base);
                glTexCoord2i(1,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * (i+1))+offset.x*.4,base);
@@ -624,7 +670,8 @@ void render(){
 
        glBindTexture(GL_TEXTURE_2D, bgTreesFront);
        glBegin(GL_QUADS);
-       glColor4ub(255,255,255,255);
+       safeSetColorA(255-shade,255-shade,255-shade,255);
+       //glColor4ub(255,255,255,255);
        for(int i = 0; i <= currentWorld->getTheWidth()/1920; i++){
                glTexCoord2i(0,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * i)+offset.x*.25,base);
                glTexCoord2i(1,1);glVertex2i((currentWorld->getTheWidth()*-0.5f)+(1920 * (i+1))+offset.x*.25,base);
@@ -661,7 +708,7 @@ void render(){
                
                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: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nTc: %u\nQc: %u",
                                        fps,
                                        player->ground,
                                        SCREEN_WIDTH,                           // Window dimensions
@@ -669,6 +716,7 @@ void render(){
                                        entity.size(),                          // Size of entity array
                                        player->loc.x,                          // The player's x coordinate
                                        debugY,                                         // The player's y coordinate
+                                       tickCount,
                                        player->qh.current.size()       // Active quest count
                                        );
                if(ui::posFlag){
@@ -857,10 +905,22 @@ void logic(){
                }
        }
        
+       if(!(tickCount%DAY_CYCLE)||!tickCount){
+               if(weather==SUNNY){
+                       weather=DARK;
+               }else{
+                       weather=SUNNY;
+               }
+       }
+       
+       #define PI 3.1415926535
+       worldShade=50*sin((tickCount+(DAY_CYCLE/2))/(DAY_CYCLE/PI));
+       
        /*
         *      Increment a loop counter used for animating sprites.
        */
        
        loops++;
+       tickCount++;
        NPCSelected=false;
 }
index cd568add1863f8bb5a9c2d24fabde71fc0427665..a8a964e2301af30defecbf6809573473d289072e 100644 (file)
@@ -10,3 +10,25 @@ void DEBUG_prints(const char* file, int line, const char *s,...){
        vprintf(s,args);
        va_end(args);
 }
+
+void safeSetColor(int r,int g,int b){  // safeSetColor() is an alternative to directly using glColor3ub() to set
+       if(r>255)r=255;                                         // the color for OpenGL drawing. safeSetColor() checks for values that are
+       if(g>255)g=255;                                         // outside the range of an unsigned character and sets them to a safer value.
+       if(b>255)b=255;
+       if(r<0)r=0;
+       if(g<0)g=0;
+       if(b<0)b=0;
+       glColor3ub(r,g,b);
+}
+
+void safeSetColorA(int r,int g,int b,int a){
+       if(r>255)r=255;
+       if(g>255)g=255;
+       if(b>255)b=255;
+       if(a>255)a=255;
+       if(r<0)r=0;
+       if(g<0)g=0;
+       if(b<0)b=0;
+       if(a<0)a=0;
+       glColor4ub(r,g,b,a);
+}
index 3043e80b09e3d3281d755cfc552f9424811958b7..33af0a8a5e2188410bdbbcb733cf5e481b02538e 100644 (file)
@@ -131,7 +131,6 @@ void itemDraw(Player *p,ITEM_ID id){
                          p->loc.y+HLINE*3};
        }
        if(p->inv->tossd) yes=true;
-       std::cout<<item_coord.x<<" "<<item_coord.y<<std::endl;
        glBegin(GL_QUADS);
                glTexCoord2i(0,1);glVertex2f(item_coord.x+p1.x,item_coord.y+p2.y);
                glTexCoord2i(1,1);glVertex2f(item_coord.x+p2.x,item_coord.y+p2.y);
@@ -141,12 +140,14 @@ void itemDraw(Player *p,ITEM_ID id){
        glDisable(GL_TEXTURE_2D);
 }
 
-void itemUse(Player *p,ITEM_ID id){
+int Inventory::useItem(void){
+       ITEM_ID id = item[sel].id;
        switch(id){
        default:
                ui::dialogBox(itemName[id],"You cannot use this item.");
                break;
        }
+       return 0;
 }
 
 int Inventory::itemToss(void){
index 05b540e49a5114877c10c9ff0409672c1659d6f0..fa8cf9c9856c1020c92f305b0a5e0edc99862f1b 100644 (file)
@@ -265,6 +265,9 @@ namespace ui {
                                if(SDL_KEY==SDLK_q){
                                        player->inv->itemToss();
                                }
+                               if(SDL_KEY==SDLK_e){
+                                       player->inv->useItem();
+                               }
                                if(SDL_KEY==SDLK_c){
                                        dialogBox("","You pressed `c`, but nothing happened.");
                                }
index 5ff1f0fd886d2bfbd3a7d37518f3d49c8a34d3df..dccca2177bada5621ec60e50ba215f4b08fab2f9 100644 (file)
 extern std::vector<Entity        *> entity;
 extern std::vector<Structures *> build;
 
-void safeSetColor(int r,int g,int b){  // safeSetColor() is an alternative to directly using glColor3ub() to set
-       if(r>255)r=255;                                         // the color for OpenGL drawing. safeSetColor() checks for values that are
-       if(g>255)g=255;                                         // outside the range of an unsigned character and sets them to a safer value.
-       if(b>255)b=255;
-       if(r<0)r=0;
-       if(g<0)g=0;
-       if(b<0)b=0;
-       glColor3ub(r,g,b);
-}
-
 float worldGetYBase(World *w){
        float base = 0;
        World *ptr = w;
@@ -150,9 +140,11 @@ World::~World(void){
        free(line);
 }
 
+int worldShade = 0;
+
 void World::draw(Player *p){
        static float yoff=DRAW_Y_OFFSET;        // Initialize stuff
-       static int shade=0;
+       static int shade;
        static World *current;
        int i,is,ie,v_offset,cx_start;
        struct line_t *cline;
@@ -168,6 +160,7 @@ void World::draw(Player *p){
        */
        
        current=this;
+       shade=worldShade;
        
 LOOP1:
 
@@ -281,7 +274,7 @@ LOOP2:
                 *      by setting line.gs to false.
                */
                
-               if(p->ground){
+               if(p->ground==1){
                        for(i=0;i<lineCount-GEN_INC;i++){
                                if(i < ph + 6 && 
                                   i > ph - 6 )