diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-10-30 08:45:55 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-10-30 08:45:55 -0400 |
commit | a75a3b2d8f0848a05b09180d9517a8016cbafdde (patch) | |
tree | 7a70d92c90dab36deadef1e40a516c54231c116d | |
parent | 9ab6025a0cc3ab31c476f0b478ac69bfadd7f670 (diff) |
day/night cycling
-rw-r--r-- | Changelog | 9 | ||||
-rw-r--r-- | assets/bgn.png | bin | 0 -> 1438185 bytes | |||
-rw-r--r-- | config/quest_list.txt | 2 | ||||
-rw-r--r-- | include/common.h | 3 | ||||
-rw-r--r-- | include/inventory.h | 3 | ||||
-rw-r--r-- | include/world.h | 2 | ||||
-rw-r--r-- | main.cpp | 84 | ||||
-rw-r--r-- | src/common.cpp | 22 | ||||
-rw-r--r-- | src/inventory.cpp | 5 | ||||
-rw-r--r-- | src/ui.cpp | 3 | ||||
-rw-r--r-- | src/world.cpp | 17 |
11 files changed, 122 insertions, 28 deletions
@@ -206,3 +206,12 @@ - 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 Binary files differnew file mode 100644 index 0000000..2aa3995 --- /dev/null +++ b/assets/bgn.png diff --git a/config/quest_list.txt b/config/quest_list.txt index 3014ede..d23743e 100644 --- a/config/quest_list.txt +++ b/config/quest_list.txt @@ -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 diff --git a/include/common.h b/include/common.h index c3b1aed..ce176a6 100644 --- a/include/common.h +++ b/include/common.h @@ -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 diff --git a/include/inventory.h b/include/inventory.h index e27391a..d2b4c28 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -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 diff --git a/include/world.h b/include/world.h index 5ec01ae..75263e5 100644 --- a/include/world.h +++ b/include/world.h @@ -154,4 +154,6 @@ public: void draw(Player *p); // Draws the world (ignores layers) }; +extern int worldShade; + #endif // WORLD_H @@ -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; } diff --git a/src/common.cpp b/src/common.cpp index cd568ad..a8a964e 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -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); +} diff --git a/src/inventory.cpp b/src/inventory.cpp index 3043e80..33af0a8 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -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){ @@ -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."); } diff --git a/src/world.cpp b/src/world.cpp index 5ff1f0f..dccca21 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -18,16 +18,6 @@ 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 ) |