]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Sword swinging
authordrumsetmonkey <abelleisle@roadrunner.com>
Mon, 16 May 2016 02:52:44 +0000 (22:52 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Mon, 16 May 2016 02:52:44 +0000 (22:52 -0400)
include/common.hpp
main.cpp
shaders/world.vert
src/entities.cpp
src/inventory.cpp
src/ui.cpp

index fabf4c7b0f82f668f7411b8fcaeb730f2e2fe2f1..153c46e8d4193ffa2147753d280645f57c3549e4 100644 (file)
@@ -232,6 +232,7 @@ extern GLint worldShader_attribute_coord;
 extern GLint worldShader_attribute_tex;
 extern GLint worldShader_uniform_texture;
 extern GLint worldShader_uniform_color;
+extern GLint worldShader_uniform_transform;
 
 /**
  *     Prints a formatted debug message to the console, along with the callee's file and line
index 9ff36891a6b2ed7335e949e4f9fbeb889120ea8e..e61073a31041ef7c592542e6fc9ef47aa744f067 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -76,6 +76,7 @@ GLint worldShader_attribute_coord;
 GLint worldShader_attribute_tex;
 GLint worldShader_uniform_texture;
 GLint worldShader_uniform_transform;
+GLint worldShader_uniform_ortho;
 GLint worldShader_uniform_color;
 
 // keeps a simple palette of colors for single-color draws
@@ -102,7 +103,7 @@ void mainLoop(void);
 
 int main(int argc, char *argv[]){
        static SDL_GLContext mainGLContext = NULL;
-       
+
        // handle command line arguments
        if (argc > 1) {
                std::vector<std::string> args (argc, "");
@@ -114,7 +115,7 @@ int main(int argc, char *argv[]){
                                system("rm -f xml/*.dat");
                }
        }
-       
+
        // attempt to initialize SDL
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
                UserError(std::string("SDL was not able to initialize! Error: ") + SDL_GetError());
@@ -229,7 +230,8 @@ int main(int argc, char *argv[]){
        worldShader_attribute_coord =   get_attrib(worldShader, "coord2d");
        worldShader_attribute_tex =     get_attrib(worldShader, "tex_coord");
        worldShader_uniform_texture =   get_uniform(worldShader, "sampler");
-       worldShader_uniform_transform = get_uniform(worldShader, "ortho");
+       worldShader_uniform_transform = get_uniform(worldShader, "transform");
+       worldShader_uniform_ortho = get_uniform(worldShader, "ortho");
        worldShader_uniform_color =     get_uniform(worldShader, "tex_color");
 
        //glEnable(GL_MULTISAMPLE);
@@ -240,7 +242,7 @@ int main(int argc, char *argv[]){
 
        // load sprites used in the inventory menu. See src/inventory.cpp
        initInventorySprites();
-       
+
        // load mouse texture, and other inventory textures
        mouseTex = Texture::loadTexture("assets/mouse.png");
 
@@ -253,7 +255,7 @@ int main(int argc, char *argv[]){
 
        // alphabetically sort files
        strVectorSortAlpha(&xmlFiles);
-       
+
        // load the first valid XML file for the world
        for (const auto &xf : xmlFiles) {
                if (xf[0] != '.' && strcmp(&xf[xf.size() - 3], "dat")){
@@ -263,11 +265,11 @@ int main(int argc, char *argv[]){
                        break;
                }
        }
-       
+
        // make sure the world was made
        if (currentWorld == NULL)
                UserError("Plot twist: The world never existed...?");
-       
+
        // spawn the player
        player = new Player();
        player->sspawn(0,100);
@@ -292,7 +294,7 @@ int main(int argc, char *argv[]){
 
        // put away the brice for later
        game::briceSave();
-    
+
        // free library resources
     Mix_HaltMusic();
     Mix_CloseAudio();
@@ -388,7 +390,8 @@ void render() {
        glUniformMatrix4fv(textShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(ortho));
     glUniform4f(textShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
     glUseProgram(worldShader);
-       glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(ortho));
+       glUniformMatrix4fv(worldShader_uniform_ortho, 1, GL_FALSE, glm::value_ptr(ortho));
+       glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f)));
        glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
        /**************************
        **** RENDER STUFF HERE ****
index ce7fa5aa3df6e17078a964f33c1b7e72151c6f68..10869d68d2b9aa75528f1ef6383fc0f86e742fb8 100644 (file)
@@ -3,6 +3,7 @@ attribute vec2 tex_coord;
 
 uniform vec4 tex_color;
 uniform mat4 ortho;
+uniform mat4 transform;
 
 varying vec2 texCoord;
 varying vec4 color;
@@ -10,5 +11,5 @@ varying vec4 color;
 void main(){
        color = tex_color;
     texCoord = tex_coord;
-    gl_Position = ortho * vec4(coord2d.xyz, 1.0);
+    gl_Position = ortho * transform * vec4(coord2d.xyz, 1.0);
 }
index af9747dd49f573f71d03a48556fda54d66dc735e..73e7808781f3574f9fcd33ff7f6c1ca03a784ba9 100644 (file)
@@ -898,52 +898,52 @@ void Particles::draw(std::vector<GLfloat> &p) const
         z = 2.0;
 
        // lower left
-    p.push_back(loc.x);
-    p.push_back(loc.y);
-    p.push_back(z);
+    p.emplace_back(loc.x);
+    p.emplace_back(loc.y);
+    p.emplace_back(z);
 
-       p.push_back(tc.x);
-       p.push_back(tc.y);
+       p.emplace_back(tc.x);
+       p.emplace_back(tc.y);
 
        // lower right
-    p.push_back(loc.x + width);
-    p.push_back(loc.y);
-    p.push_back(z);
+    p.emplace_back(loc.x + width);
+    p.emplace_back(loc.y);
+    p.emplace_back(z);
 
-       p.push_back(tc.x);
-       p.push_back(tc.y);
+       p.emplace_back(tc.x);
+       p.emplace_back(tc.y);
 
        // upper right
-    p.push_back(loc.x + width);
-    p.push_back(loc.y + height);
-    p.push_back(z);
+    p.emplace_back(loc.x + width);
+    p.emplace_back(loc.y + height);
+    p.emplace_back(z);
 
-       p.push_back(tc.x);
-       p.push_back(tc.y);
+       p.emplace_back(tc.x);
+       p.emplace_back(tc.y);
 
        // upper right
-    p.push_back(loc.x + width);
-    p.push_back(loc.y + height);
-    p.push_back(z);
+    p.emplace_back(loc.x + width);
+    p.emplace_back(loc.y + height);
+    p.emplace_back(z);
 
-       p.push_back(tc.x);
-       p.push_back(tc.y);
+       p.emplace_back(tc.x);
+       p.emplace_back(tc.y);
 
        // upper left
-    p.push_back(loc.x);
-    p.push_back(loc.y + height);
-    p.push_back(z);
+    p.emplace_back(loc.x);
+    p.emplace_back(loc.y + height);
+    p.emplace_back(z);
 
-       p.push_back(tc.x);
-    p.push_back(tc.y);
+       p.emplace_back(tc.x);
+    p.emplace_back(tc.y);
 
        // lower left
-    p.push_back(loc.x);
-    p.push_back(loc.y);
-    p.push_back(z);
+    p.emplace_back(loc.x);
+    p.emplace_back(loc.y);
+    p.emplace_back(z);
 
-    p.push_back(tc.x);
-    p.push_back(tc.y);
+    p.emplace_back(tc.x);
+    p.emplace_back(tc.y);
 }
 
 void Particles::update(float _gravity, float ground_y)
index c64f62f65393f6f876374138566c366afb63b807..29e2dc9c92082533aa0bc962f767084d18cf9e96 100644 (file)
@@ -408,7 +408,7 @@ void Inventory::draw(void) {
             float t = (((float)massDfp[a]/(float)massRange)*.5f);
             glActiveTexture(GL_TEXTURE0);
             glUseProgram(textShader);
-            
+
                        glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(0.0f,0.0f,0.0f, t >= 0? 255*t : 0)));
             glUniform1i(textShader_uniform_texture, 0);
 
@@ -438,7 +438,7 @@ void Inventory::draw(void) {
                        curCurCoord[a].x -= float((curdfp[a]) * cos(-1));
                        curCurCoord[a].y += float((curdfp[a]) * sin(0));
                        cr.end = curCurCoord[a];
-            
+
             float curTrans = (((float)curdfp[a]/(float)(curRange?curRange:1))*0.5f);
 
             glUseProgram(textShader);
@@ -495,7 +495,7 @@ void Inventory::draw(void) {
                           &textShader_uniform_texture,
                           &textShader_attribute_coord,
                           &textShader_attribute_tex);
-                               
+
                 glUseProgram(textShader);
                 glUniform4f(textShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
 
@@ -503,7 +503,7 @@ void Inventory::draw(void) {
                 glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255, 255, 255, t >= 0 ? 255 * t : 0)));
                 drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09),
                          vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2));
+
                 // top
                 glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255, 255, 255, t  >= 0 ? 255 * t : 0)));
                 drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09),
@@ -617,14 +617,34 @@ void itemDraw(Player *p, Item *d) {
        itemLoc.y = p->loc.y+(p->height/3);
        itemLoc.x = p->left?p->loc.x-d->dim.x/2:p->loc.x+p->width-d->dim.x/2;
 
+       glUseProgram(worldShader);
+
        if (p->left) {
-               glTranslatef(itemLoc.x+d->dim.x/2,itemLoc.y,0);
-               glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
-               glTranslatef(-itemLoc.x-d->dim.x/2,-itemLoc.y,0);
+               // move to center of screen
+               glm::mat4 tro = glm::translate(glm::mat4(1.0f),
+                                                                          glm::vec3(itemLoc.x+d->dim.x/2, itemLoc.y, 0));
+               // rotate off center
+               glm::mat4 rot = glm::rotate(glm::mat4(1.0f),
+                                                                       static_cast<GLfloat>((d->rotation*3.14159)/180.0f),
+                                                                       glm::vec3(0.0f, 0.0f, 1.0f));
+               // move back to player
+               glm::mat4 trt = glm::translate(glm::mat4(1.0f),
+                                                                          glm::vec3(-itemLoc.x-d->dim.x/2, -itemLoc.y, 0));
+               // tell shader to translate the object using steps above
+               glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(tro * rot * trt));
        } else {
-               glTranslatef(itemLoc.x+d->dim.x/2,itemLoc.y,0);
-               glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
-               glTranslatef(-itemLoc.x-d->dim.x/2,-itemLoc.y,0);
+               // move to center of screen
+               glm::mat4 tro = glm::translate(glm::mat4(1.0f),
+                                                                          glm::vec3(itemLoc.x+d->dim.x/2,itemLoc.y,0));
+               // rotate off center
+               glm::mat4 rot = glm::rotate(glm::mat4(1.0f),
+                                                                       static_cast<GLfloat>((d->rotation*3.14159)/180.0f),
+                                                                       glm::vec3(0.0f, 0.0f, 1.0f));
+               // move back to player
+               glm::mat4 trt = glm::translate(glm::mat4(1.0f),
+                                                                          glm::vec3(-itemLoc.x-d->dim.x/2,-itemLoc.y,0));
+               // tell shader to translate the object using steps above
+               glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(tro * rot * trt));
        }
 
     GLfloat itemTex[12] = {0.0, 0.0,
@@ -650,7 +670,7 @@ void itemDraw(Player *p, Item *d) {
                             itemLoc.x+d->dim.x, itemLoc.y+d->dim.y, 1.0,
                             itemLoc.x,          itemLoc.y+d->dim.y, 1.0,
                             itemLoc.x,          itemLoc.y,          1.0};
-       glUseProgram(worldShader);
+
        glBindTexture(GL_TEXTURE_2D,d->tex->image[0]);
 
     glEnableVertexAttribArray(worldShader_attribute_coord);
index 838ac253638dc216c5d2602a0c3459faafd78519..a8d37d0cc139fdf72256d0176d73b6f2dd6122fe 100644 (file)
@@ -324,12 +324,12 @@ namespace ui {
                        c1.x,           c1.y+c2.y-c2.y, 1.0,    //top left
                        c1.x,           c1.y     -c2.y, 1.0,    //bottom left
                };
-        
-        glUniform4f(textShader_uniform_color, 
-                    static_cast<float>(fontColor[0]/255), 
-                    static_cast<float>(fontColor[1]/255), 
-                    static_cast<float>(fontColor[2]/255), 
-                    static_cast<float>(fontColor[3]/255)); 
+
+        glUniform4f(textShader_uniform_color,
+                    static_cast<float>(fontColor[0]/255),
+                    static_cast<float>(fontColor[1]/255),
+                    static_cast<float>(fontColor[2]/255),
+                    static_cast<float>(fontColor[3]/255));
 
                glVertexAttribPointer(textShader_attribute_coord,       3, GL_FLOAT, GL_FALSE, 0, text_vert);
                glVertexAttribPointer(textShader_attribute_tex,         2, GL_FLOAT, GL_FALSE, 0, tex_coord);
@@ -684,28 +684,28 @@ namespace ui {
         static GLuint boxT = Texture::genColor(Color(0,0,0));
         static GLuint lineT = Texture::genColor(Color(255,255,255));
 
-        glActiveTexture(GL_TEXTURE0); 
+        glActiveTexture(GL_TEXTURE0);
         glBindTexture(GL_TEXTURE_2D, boxT);
         glUniform1i(textShader_uniform_texture, 0);
         glUseProgram(textShader);
 
         glEnableVertexAttribArray(textShader_attribute_coord);
         glEnableVertexAttribArray(textShader_attribute_tex);
-   
+
         glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, box);
         glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, box_tex);
         glDrawArrays(GL_TRIANGLES, 0 ,6);
-       
+
         glBindTexture(GL_TEXTURE_2D, lineT);
         glUniform1i(textShader_uniform_texture, 0);
 
         glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, line_strip);
         glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, box_tex);
         glDrawArrays(GL_LINE_STRIP, 0 ,8);
-         
+
         glDisableVertexAttribArray(textShader_attribute_coord);
         glDisableVertexAttribArray(textShader_attribute_tex);
-        
+
         glUseProgram(0);
        }
 
@@ -738,21 +738,21 @@ namespace ui {
                                   0.0, 1.0,
                                   0.0, 0.0};
 
-            glActiveTexture(GL_TEXTURE0); 
+            glActiveTexture(GL_TEXTURE0);
             glBindTexture(GL_TEXTURE_2D, pageTex);
             glUniform1i(textShader_uniform_texture, 0);
             glUseProgram(textShader);
 
             glEnableVertexAttribArray(textShader_attribute_coord);
             glEnableVertexAttribArray(textShader_attribute_tex);
-       
+
             glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, page_loc);
             glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, page_tex);
             glDrawArrays(GL_TRIANGLES, 0 ,6);
-           
+
             glDisableVertexAttribArray(textShader_attribute_coord);
             glDisableVertexAttribArray(textShader_attribute_tex);
-            
+
             glUseProgram(0);
 
                } else if (dialogBoxExists) {
@@ -819,18 +819,18 @@ namespace ui {
                                         offset.x + (SCREEN_WIDTH / 10) - 40,    offset.y + (SCREEN_HEIGHT / 5) + 40,1.0,
                                         offset.x + (SCREEN_WIDTH / 10) - 40,    offset.y + (SCREEN_HEIGHT / 5),     1.0};
 
-                glActiveTexture(GL_TEXTURE0); 
+                glActiveTexture(GL_TEXTURE0);
                 glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[1]));
                 glUniform1i(textShader_uniform_texture, 0);
                 glUseProgram(textShader);
 
                 glEnableVertexAttribArray(textShader_attribute_coord);
                 glEnableVertexAttribArray(textShader_attribute_tex);
-           
+
                 glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, left_item);
                 glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex);
                 glDrawArrays(GL_TRIANGLES, 0 ,6);
-               
+
                 glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[0]));
 
                 glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, right_item);
@@ -839,7 +839,7 @@ namespace ui {
 
                 glDisableVertexAttribArray(textShader_attribute_coord);
                 glDisableVertexAttribArray(textShader_attribute_tex);
-                
+
                 glUseProgram(0);
 
                                merchArrowLoc[0].x = offset.x - (SCREEN_WIDTH / 8.5) - 16;
@@ -874,14 +874,14 @@ namespace ui {
 
                     glEnableVertexAttribArray(textShader_attribute_coord);
                     glEnableVertexAttribArray(textShader_attribute_tex);
-               
+
                     glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, tri_c);
                     glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tri_t);
                     glDrawArrays(GL_TRIANGLES, 0 ,6);
-                   
+
                     glDisableVertexAttribArray(textShader_attribute_coord);
                     glDisableVertexAttribArray(textShader_attribute_tex);
-                    
+
                     glUseProgram(0);
                                }
 
@@ -968,27 +968,27 @@ namespace ui {
                                    hub.x,       hub.y + 12, 1.0,
                                    hub.x + 150, hub.y + 12, 1.0};
 
-                
+
                 glUniform1i(textShader_uniform_texture, 0);
                 glUseProgram(textShader);
 
                 glEnableVertexAttribArray(textShader_attribute_coord);
                 glEnableVertexAttribArray(textShader_attribute_tex);
-           
+
                 glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(150,0,0)));
 
                 glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, front);
                 glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
-               
+
                 glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255,0,0)));
-                
+
                 glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, back);
                 glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
                 glDisableVertexAttribArray(textShader_attribute_coord);
-                glDisableVertexAttribArray(textShader_attribute_tex);             
+                glDisableVertexAttribArray(textShader_attribute_tex);
 
                 glUseProgram(0);
             }
@@ -1038,7 +1038,7 @@ namespace ui {
 
        void dialogAdvance(void) {
                unsigned char i;
-               
+
                dialogPassive = false;
                dialogPassiveTime = 0;
 
@@ -1123,7 +1123,7 @@ EXIT:
                        toggleBlackFast();
                        player->canMove = true;
                };
-               
+
                while(SDL_PollEvent(&e)) {
                        switch(e.type) {
 
@@ -1163,8 +1163,8 @@ EXIT:
                                        if (e.button.button & SDL_BUTTON_LEFT) {
                                                if ((en = currentWorld->getNearMob(*player)) != nullptr) {
                                                        player->inv->currentAddInteract(en);
-                                                       player->inv->useCurrent();
                                                }
+                                                       player->inv->useCurrent();
                                        }
 
                                }
@@ -1341,7 +1341,7 @@ EXIT:
                                        if (player->speed == 4)
                                                Mix_FadeOutChannel(1,2000);
 
-                                       // fall through                                 
+                                       // fall through
                                case SDLK_LCTRL:
                                        player->speed = 1;
                                        break;
@@ -1441,20 +1441,20 @@ EXIT:
                               offset.x + SCREEN_WIDTH / 2, offset.y - SCREEN_HEIGHT / 2, 1.0,
                               offset.x - SCREEN_WIDTH / 2, offset.y + SCREEN_HEIGHT / 2, 1.0,
                               offset.x + SCREEN_WIDTH / 2, offset.y + SCREEN_HEIGHT / 2, 1.0};
-           
+
         glUniform1i(textShader_uniform_texture, 0);
         glUseProgram(textShader);
 
         glEnableVertexAttribArray(textShader_attribute_coord);
         glEnableVertexAttribArray(textShader_attribute_tex);
-   
+
         glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, backdrop);
         glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
-       
+
         glDisableVertexAttribArray(textShader_attribute_coord);
         glDisableVertexAttribArray(textShader_attribute_tex);
-        
+
         glUseProgram(0);
 
     }