]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Menus!
authordrumsetmonkey <abelleisle@roadrunner.com>
Mon, 25 Jan 2016 13:46:49 +0000 (08:46 -0500)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Mon, 25 Jan 2016 13:46:49 +0000 (08:46 -0500)
15 files changed:
assets/playerk.png
assets/playerk1.png [new file with mode: 0644]
assets/playerk2.png [new file with mode: 0644]
assets/playerk3.png [new file with mode: 0644]
assets/playerk4.png [new file with mode: 0644]
assets/playerk5.png [new file with mode: 0644]
assets/playerk6.png [new file with mode: 0644]
assets/playerk7.png [new file with mode: 0644]
assets/playerk8.png [new file with mode: 0644]
include/common.h
include/ui.h
main.cpp
src/entities.cpp
src/gameplay.cpp
src/ui.cpp

index b51415126af0da3b9140df39a46db6b2801f5210..7abf012dc74b06b722c296d4b4722becf05340ed 100644 (file)
Binary files a/assets/playerk.png and b/assets/playerk.png differ
diff --git a/assets/playerk1.png b/assets/playerk1.png
new file mode 100644 (file)
index 0000000..1c4fb49
Binary files /dev/null and b/assets/playerk1.png differ
diff --git a/assets/playerk2.png b/assets/playerk2.png
new file mode 100644 (file)
index 0000000..97f769b
Binary files /dev/null and b/assets/playerk2.png differ
diff --git a/assets/playerk3.png b/assets/playerk3.png
new file mode 100644 (file)
index 0000000..19b97c4
Binary files /dev/null and b/assets/playerk3.png differ
diff --git a/assets/playerk4.png b/assets/playerk4.png
new file mode 100644 (file)
index 0000000..c32a9b3
Binary files /dev/null and b/assets/playerk4.png differ
diff --git a/assets/playerk5.png b/assets/playerk5.png
new file mode 100644 (file)
index 0000000..fabfa60
Binary files /dev/null and b/assets/playerk5.png differ
diff --git a/assets/playerk6.png b/assets/playerk6.png
new file mode 100644 (file)
index 0000000..269fccb
Binary files /dev/null and b/assets/playerk6.png differ
diff --git a/assets/playerk7.png b/assets/playerk7.png
new file mode 100644 (file)
index 0000000..aeac120
Binary files /dev/null and b/assets/playerk7.png differ
diff --git a/assets/playerk8.png b/assets/playerk8.png
new file mode 100644 (file)
index 0000000..ff03a02
Binary files /dev/null and b/assets/playerk8.png differ
index 8e08d1dea985c5b274cf6b34a55c61860670a1da..d0f539beda9a7a9666ca0a24a60fbe809bf5e73b 100644 (file)
@@ -61,6 +61,8 @@ typedef struct {
        float z;
 } vec3;
 
+typedef vec2 dim2;
+
 /**
  * This structure contains two sets of coordinates for ray drawing.
  */
@@ -70,11 +72,25 @@ typedef struct {
        vec2 end;
 } Ray;
 
-typedef struct{
+struct col{
        float red;
        float green;
        float blue;
-} Color;
+       col operator-=(float a){
+               red-=a;
+               green-=a;
+               blue-=a;
+               return *this;
+       }
+       col operator+=(float a){
+               return{red+a,green+a,blue+a};
+       }
+       col operator=(float a){
+               return{red=a,green=a,blue=a};
+       }
+};
+
+typedef col Color;
 
 #include <Texture.h>
 
index c018008c6fba643ec9b27bfe376ba491a3099cb9..030c73dd728157d3b80b29ba9065f28323c770ec 100644 (file)
 
 #include <world.h>
 #include <ft2build.h>
+#include <SDL2/SDL_opengl.h>
 #include FT_FREETYPE_H
 
 #define DEBUG
 
+typedef void(*menuFunc)();
+
+
+struct menuItem{
+       int member;
+       union{
+
+               struct{
+                       vec2 loc;
+                       dim2 dim;
+                       Color color;
+                       const char* text;
+                       menuFunc func;
+               }button;
+
+               struct{
+                       vec2 loc;
+                       dim2 dim;
+                       Color color;
+                       float minValue;
+                       float maxValue;
+                       const char* text;
+                       void* var;
+               }slider;
+
+       };
+};
+
 namespace ui {
 
+       menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f);
        /**
         *      Contains the coordinates of the mouse inside the window.
         */
@@ -26,6 +56,8 @@ namespace ui {
         *      These flags are used elsewhere.
        */
 
+       extern bool pMenu;
+       extern bool menu;
        extern bool debug;
        extern bool posFlag;
        extern unsigned int fontSize;
@@ -83,6 +115,15 @@ namespace ui {
        
        void draw(void);
        
+
+       /*
+        *      Draw various menu items
+       */
+       void quitGame();
+       void quitMenu();
+       void drawMenu(std::vector<menuItem>mi);
+
+
        /*
         *      Handle keyboard/mouse events.
        */
index dcfc72227d572f695729bb7e6a2e69c29ed96076..9a71729ce68d0036f01859a4c22aa6e076e7303c 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -167,6 +167,8 @@ void mainLoop(void);
 
 vec2 offset;                                                                                                                                                   /*      OFFSET!!!!!!!!!!!!!!!!!!!! */
 
+std::vector<menuItem>pauseMenu;
+
 extern WEATHER weather;
 
 extern bool fadeEnable;
@@ -461,6 +463,8 @@ void mainLoop(void){
        prevTime        = currentTime;
        currentTime = millis();
        deltaTime       = currentTime - prevTime;
+
+       if(ui::menu)goto MENU;
        
        /*
         *      Run the logic handler if MSEC_PER_TICK milliseconds have passed.
@@ -484,6 +488,8 @@ void mainLoop(void){
        */
        
        currentWorld->update(player,deltaTime);
+
+       MENU:
        
        /*
         *      Update debug variables if necessary
@@ -596,9 +602,7 @@ void render(){
 
        player->near=true;                      // Draw the player's name
 
-       //glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 640,100);
        currentWorld->draw(player);
-       //glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 0,1.0f,0);
 
 
        /*
@@ -741,6 +745,10 @@ void render(){
 
        }
 
+       if(ui::menu){
+               if(ui::pMenu)ui::drawMenu(pauseMenu);
+       }
+
        /*
         *      Draw a white triangle as a replacement for the mouse's cursor.
        */
@@ -986,6 +994,10 @@ void logic(){
                else fadeIntensity = 0;
        }
 
+       if(ui::pMenu){
+               ui::drawMenu(pauseMenu);
+       }
+
        /*
         *      Increment a loop counter used for animating sprites.
        */
index 88ef6b9910b650ba998c072061a135336523c00f..8608aeb1cb5202abbcf6e0b22bc8d01b4e6cca8f 100644 (file)
@@ -100,7 +100,15 @@ Player::Player(){ //sets all of the player specific traits on object creation
        subtype = 0;    
        health = maxHealth = 100;
        speed = 1;
-       tex = new Texturec(3, "assets/player1.png", "assets/playerk.png", "assets/player2.png");
+       tex = new Texturec(9,   "assets/playerk.png",
+                                                       "assets/playerk1.png",
+                                                       "assets/playerk2.png",
+                                                       "assets/playerk3.png",
+                                                       "assets/playerk4.png",
+                                                       "assets/playerk5.png",
+                                                       "assets/playerk6.png",
+                                                       "assets/playerk7.png",
+                                                       "assets/playerk8.png");
        //tex = new Texturec(3, "assets/maybeplayer.png", "assets/maybeplayer.png", "assets/maybeplayer.png");
        inv = new Inventory(PLAYER_INV_SIZE);
 }
@@ -268,18 +276,11 @@ void Entity::draw(void){          //draws the entities
        switch(type){
        case PLAYERT:
                static int texState = 0;
-               static bool up = true;
-               if(speed && !(loops % (int)(4.0f/(float)speed))){
+               if(speed && !(loops % (int)(2.0f/(float)speed))){
                        //currentWorld->addParticle(loc.x,loc.y-HLINE,HLINE,HLINE,0,0,{0.0f,.17f,0.0f},1000);
-                       if(up){
-                               if(++texState==2)up=false;
-                               glActiveTexture(GL_TEXTURE0);
-                               tex->bindNext();
-                       }else{
-                               if(!--texState)up=true;
-                               glActiveTexture(GL_TEXTURE0);
-                               tex->bindPrev();
-                       }
+                       if(++texState==9)texState=1;
+                       glActiveTexture(GL_TEXTURE0);
+                       tex->bind(texState);
                }
                if(!ground){
                        glActiveTexture(GL_TEXTURE0 + 0);
@@ -289,7 +290,7 @@ void Entity::draw(void){            //draws the entities
                        tex->bind(texState);
                }else{
                        glActiveTexture(GL_TEXTURE0 + 0);
-                       tex->bind(1);
+                       tex->bind(0);
                }
                break;
        case MOBT:
index 7730cf693302c8d76dbff0c875380f124427baae..562791d6a676d0d49ab7fdcf1e6d4c1363985d42 100644 (file)
@@ -10,6 +10,10 @@ using namespace tinyxml2;
 extern World   *currentWorld;
 extern Player  *player;
 
+extern std::vector<menuItem>pauseMenu;
+
+extern void mainLoop(void);
+
 typedef struct {
        NPC *npc;
        unsigned int index;
@@ -211,6 +215,9 @@ void initEverything(void){
 
        currentWorld->bgmPlay(NULL);
        atexit(destroyEverything);
+
+       pauseMenu.push_back(ui::createButton({-256/2,0},{256,75},{0.0f,0.0f,0.0f}, (const char*)("Resume"), ui::quitMenu));
+       pauseMenu.push_back(ui::createButton({-256/2,-100},{256,75},{0.0f,0.0f,0.0f}, (const char*)("Save and Quit"), ui::quitGame));
 }
 
 extern std::vector<int (*)(NPC *)> AIpreload;
index e2cc39f0c28e90529abc92ad7d477346e694ff70..37c646bf03d107db406e550ceb74b60644d74304 100644 (file)
@@ -6,6 +6,8 @@
 
 #define SDL_KEY e.key.keysym.sym
 
+extern SDL_Window *window;
+
 /*
  *     External references for updating player coords / current world.
 */
@@ -76,11 +78,14 @@ namespace ui {
        */
        
        vec2 mouse;
+       static vec2 premouse={0,0};             
 
        /*
         *      Variety of keydown bools
        */
        bool edown;
+       bool pMenu = false;
+       bool menu = false;
        
        /*
         *      Debugging flags.
@@ -604,6 +609,91 @@ namespace ui {
                        }
                }
        }
+
+       void quitGame(){
+               if(pMenu)pMenu^=pMenu;
+               if(menu)menu^=menu;
+               gameRunning = false;
+       }
+
+       void quitMenu(){
+               if(pMenu)pMenu^=pMenu;
+               if(menu)menu^=menu;
+       }
+
+
+       menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f){
+               menuItem temp;
+               temp.member = 0;
+               temp.button.loc = l;
+               temp.button.dim = d;
+               temp.button.color = c;
+               temp.button.text = t;
+               temp.button.func = f;
+               return temp;
+       }
+
+       void drawMenu(std::vector<menuItem>mi){
+               SDL_Event e;
+                       
+               mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2);
+               mouse.y=(offset.y+SCREEN_HEIGHT/2)-premouse.y;
+
+               while(SDL_PollEvent(&e)){
+                       switch(e.type){
+                       case SDL_QUIT:
+                               gameRunning=false;
+                               return;
+                               break;
+                       case SDL_MOUSEMOTION:
+                               premouse.x=e.motion.x;
+                               premouse.y=e.motion.y;
+                               break;
+                       case SDL_KEYUP:
+                               if(SDL_KEY == SDLK_ESCAPE){
+                                       quitMenu();
+                                       return;
+                               }
+                               break;
+                       default:break;
+                       }
+               }
+
+               glColor4f(0.0f, 0.0f, 0.0f, .8f);
+               glRectf(offset.x-SCREEN_WIDTH/2,0,offset.x+SCREEN_WIDTH/2,SCREEN_HEIGHT);
+               for(auto &m : mi){
+                       if(m.member == 0){
+                               glColor3f(m.button.color.red,m.button.color.green,m.button.color.blue);
+                                       glRectf(offset.x+m.button.loc.x, 
+                                                       offset.y+m.button.loc.y, 
+                                                       offset.x+m.button.loc.x + m.button.dim.x, 
+                                                       offset.y+m.button.loc.y + m.button.dim.y);
+                               putStringCentered(offset.x + m.button.loc.x + (m.button.dim.x/2), (offset.y + m.button.loc.y + (m.button.dim.y/2)) - ui::fontSize/2, m.button.text);
+                               if(mouse.x >= offset.x+m.button.loc.x && mouse.x <= offset.x+m.button.loc.x + m.button.dim.x){
+                                       if(mouse.y >= offset.y+m.button.loc.y && mouse.y <= offset.y+m.button.loc.y + m.button.dim.y){
+
+                                               glColor3f(1.0f,1.0f,1.0f);
+                                               glBegin(GL_LINE_STRIP);
+                                                       glVertex2f(offset.x+m.button.loc.x,                                     offset.y+m.button.loc.y);
+                                                       glVertex2f(offset.x+m.button.loc.x+m.button.dim.x,              offset.y+m.button.loc.y);
+                                                       glVertex2f(offset.x+m.button.loc.x+m.button.dim.x,              offset.y+m.button.loc.y+m.button.dim.y);
+                                                       glVertex2f(offset.x+m.button.loc.x,                                     offset.y+m.button.loc.y+m.button.dim.y);
+                                                       glVertex2f(offset.x+m.button.loc.x,                                     offset.y+m.button.loc.y);
+                                               glEnd();
+
+                                               if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) m.button.func();
+                                       }
+                               }
+                       }
+               }
+
+               /*glColor3ub(255,255,255);
+               glBegin(GL_TRIANGLES);
+                       glVertex2i(mx              ,my            );
+                       glVertex2i(mx+HLINE*3.5,my                );
+                       glVertex2i(mx              ,my-HLINE*3.5);
+               glEnd();*/
+       }
        void dialogAdvance(void){
                unsigned char i;
                if(!typeOutDone){
@@ -630,7 +720,6 @@ DONE:
        }
        void handleEvents(void){
                static bool left=true,right=false;
-               static vec2 premouse={0,0};
                static int heyOhLetsGo = 0;
                World *tmp;
                vec2 oldpos,tmppos;
@@ -660,10 +749,11 @@ DONE:
                                KEYDOWN
                        */
                        case SDL_KEYDOWN:
-                               if(SDL_KEY == SDLK_ESCAPE){
-                                       gameRunning = false;
+                               /*if(SDL_KEY == SDLK_ESCAPE){
+                                       //gameRunning = false;
+                                       pMenu = true;
                                        return;
-                               }else if(SDL_KEY == SDLK_SPACE){
+                               }else */if(SDL_KEY == SDLK_SPACE){
                                        /*if(dialogBoxExists)
                                                dialogAdvance();
                                        else */if(player->ground){
@@ -794,6 +884,12 @@ DONE:
                        */
                        
                        case SDL_KEYUP:
+                               if(SDL_KEY == SDLK_ESCAPE){
+                                       //gameRunning = false;
+                                       pMenu = true;
+                                       menu = true;
+                                       return;
+                               }
                                switch(SDL_KEY){
                                case SDLK_a:
                                        left = false;