]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
improved inventory, debug flags
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 8 Oct 2015 13:10:08 +0000 (09:10 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 8 Oct 2015 13:10:08 +0000 (09:10 -0400)
20 files changed:
Makefile
assets/items/ITEM_SWORD.png [new file with mode: 0644]
assets/items/ITEM_TEST.png [new file with mode: 0644]
include/common.h
include/inventory.h
include/ui.h
include/world.h
main.cpp
out/Quest.o
out/common.o
out/entities.o
out/gameplay.o
out/inventory.o
out/ui.o
out/world.o
src/common.cpp
src/gameplay.cpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index a8f2d8845629f1ca8732d82caa2f8ca39cb9e436..387f5f388e61a811983fc7d571cd9410326c6452 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
-LIBS = -lGL -lSDL2_image -lSDL2_mixer\r
+LIBS = -lGL\r
+WIN_LIBS = -lopengl32 -lmingw32\r
 \r
-FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -lSDL2main -lSDL2 -lfreetype\r
+FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -lSDL2main -lSDL2 -lfreetype -lSDL2_image -lSDL2_mixer\r
 \r
 all:
        @rm -f out/*.o
@@ -8,6 +9,9 @@ all:
        @echo "  CXX  main.cpp"\r
        @g++ $(FLAGS) -o main main.cpp out/*.o $(LIBS)\r
 \r
+win32:\r
+       @g++ $(FLAGS) -o main main.cpp src/*.cpp $(WIN_LIBS)\r
+\r
 clean:\r
        @echo "  RM main"\r
        @-rm -f main\r
diff --git a/assets/items/ITEM_SWORD.png b/assets/items/ITEM_SWORD.png
new file mode 100644 (file)
index 0000000..306f58c
Binary files /dev/null and b/assets/items/ITEM_SWORD.png differ
diff --git a/assets/items/ITEM_TEST.png b/assets/items/ITEM_TEST.png
new file mode 100644 (file)
index 0000000..f3e6c97
Binary files /dev/null and b/assets/items/ITEM_TEST.png differ
index 3015b11060c7d318df3c6e72f5ffae663e432c05..ed2ed9e3fd3af87586702fd4ee24f2274dc4e750 100644 (file)
@@ -41,8 +41,6 @@ enum GENDER{
 template<typename T, size_t N>                                         //this fuction returns the size of any array
 int eAmt(T (&)[N]){return N;}
 
-GLuint loadTexture(const char *fileName);
-
 extern bool gameRunning;
 extern unsigned int deltaTime;
 extern unsigned int loops;
@@ -53,4 +51,7 @@ extern FILE* names;
 extern Mix_Music *music;
 extern Mix_Chunk *horn;
 
+GLuint loadTexture(const char *fileName);
+void   DEBUG_printf(const char *s,...);
+
 #endif // COMMON_H
index 8f42cc5d46fdf3c1af0e05c4afeb6cf6f7140094..d68fed940d27cbbb73ec1e843af706f08ed1f500 100644 (file)
@@ -3,8 +3,11 @@
 
 #include <cstdlib>
 
+#define DEBUG
+
 enum ITEM_ID {         // Contains item IDs for every item in the game, this is how items are stored. IDs are also used to lookup item strings
-       TEST_ITEM = 1   // A test item (duh)
+       TEST_ITEM = 1,  // A test item (duh)
+       SWORD_ITEM      
 };
 
 struct item_t {                                // Used to define entries in an entity's inventory
@@ -26,4 +29,6 @@ public:
        void draw(void);        // Draws a text list of items in this inventory (should only be called for the player for now)
 };
 
+unsigned int initInventorySprites(void);       // Loads as many inventory textures as it can find, returns count
+
 #endif // INVENTORY_H
index 5304c1b6513aad52d53c38a8d75c1c02a5be3325..6f23a78b8f7153a0e5c5957358dcda0473b0cd5e 100644 (file)
@@ -4,6 +4,8 @@
 #include <common.h>
 #include <cstdarg> // For putText()
 
+#define DEBUG
+
 namespace ui { // Functions are kept in a namespace simply
                                // for organization
 
@@ -18,9 +20,9 @@ namespace ui {        // Functions are kept in a namespace simply
        void setFontFace(const char *ttf);              // Checks and unpacks the TTF file for use by putString() and putText()
        void setFontSize(unsigned int size);    // Sets the size of the currently loaded font to 'size' pixels
        
-       void putString(const float x,const float y,const char *s);              // Draws the string 's' to the coordinates ('x','y'). The height (and therefore the width)
+       float putString(const float x,const float y,const char *s);             // Draws the string 's' to the coordinates ('x','y'). The height (and therefore the width)
                                                                                                                                        // are determined by what's currently set by setFontSize()
-       void putText(const float x,const float y,const char *str,...);  // Draws the formatted string 'str' using putString()
+       float putText(const float x,const float y,const char *str,...); // Draws the formatted string 'str' using putString()
        
        void dialogBox(const char *name,const char *text,...);                  // Prepares a dialog box to be drawn (its drawn as a black background at the top of the
                                                                                                                                        // screen and then 'text' is putString()'d
index 1f8b7e10bae16eda074f38d055ea0395292bb224..4acb9ee4bf304ed31acda247cee6edbf05b5c2c3 100644 (file)
@@ -28,13 +28,13 @@ protected:
                float y,gh[2];
                unsigned char color;
        } __attribute__ ((packed)) *line;
-       unsigned int lineCount; // Size of the array 'line' (aka the width of the world)
        std::vector<Platform> platform; // An array (vector thing) of platforms
        int x_start;                    // Worlds are centered on the x axis (0,n), this contains
                                                        // where to start drawing the world to have it centered properly.
        World *behind,*infront; // Pointers to other areas of land that are behind or in front of this one, respectively.
        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)
        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.
                                                                
index 41f8e79a55cb9759deeea86327e0159318628f36..f0ca288a3a6876ab7f8701dc92c7b51b6737187f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -110,6 +110,8 @@ int main(int argc, char *argv[]){
        
        bgImage=loadTexture("assets/bg.png");
        
+       initInventorySprites();
+       
        while(gameRunning){
                mainLoop();
        }
index 50fd93650c5fcea92bbc2c9453dcce95e562249a..7e52667c35564515eb54085153039073d710ec55 100644 (file)
Binary files a/out/Quest.o and b/out/Quest.o differ
index 044750a5afc342e73ecdae7141f85241887f1df8..915309476f2bad0dba941be0df08fcf456b99499 100644 (file)
Binary files a/out/common.o and b/out/common.o differ
index 69472f4d15531e3d4470b354fcd64af3aea0ebff..baec14791f947addedcdfed7f00d6834f3dc03a2 100644 (file)
Binary files a/out/entities.o and b/out/entities.o differ
index 68948530d0f9d52cdcc983aef889e6080b3981df..f976e64a9e5b954a3f75c00fefff6f20d5a74ec3 100644 (file)
Binary files a/out/gameplay.o and b/out/gameplay.o differ
index d88bf538b0fc06609290034c570b2ea692c14977..85313ce24e53e0b4517ed1062d0f49d75d1d2873 100644 (file)
Binary files a/out/inventory.o and b/out/inventory.o differ
index e4cd1fd575ce2f875686efe98a2cbf6516609b9f..5cc559bc1b9085275def63e564a1bf40c841dff0 100644 (file)
Binary files a/out/ui.o and b/out/ui.o differ
index 4997e5b39d6d19317dcedac43dd7921cd3dccf3d..be84bdfc1052fb88d7223e933b9fcce32ab85d33 100644 (file)
Binary files a/out/world.o and b/out/world.o differ
index 1152be7214fc68b2ed3d18ac68b02a7f52443271..7a51b4e2c46932759293f2a3eee805272909b96e 100644 (file)
@@ -1,26 +1,36 @@
 #include <common.h>
 
 GLuint loadTexture(const char *fileName){
-  SDL_Surface *image = IMG_Load(fileName);
-  //SDL_DisplayFormatAlpha(image);
-
-  unsigned object(0);
-  glGenTextures(1, &object);
-  glBindTexture(GL_TEXTURE_2D, object);
-  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-   
-  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
-  //Free surface
-  SDL_FreeSurface(image);
-  return object;
+       SDL_Surface *image = IMG_Load(fileName);
+
+       if(!image)return 0;
+
+       //SDL_DisplayFormatAlpha(image);
+
+       unsigned object(0);
+
+       glGenTextures(1, &object);
+
+       glBindTexture(GL_TEXTURE_2D, object);
+
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
+
+       //Free surface
+       SDL_FreeSurface(image);
+
+       return object;
+}
+
+void DEBUG_printf(const char *s,...){
+       va_list args;
+       printf("%s:%u: ",__FILE__,__LINE__);
+       va_start(args,s);
+       vprintf(s,args);
+       va_end(args);
 }
index b6b939ab9a23dab0636d6f8f156c5c107b0fc74c..a69020330641cb0e6ae0fdd2f4abbf97ccff03c1 100644 (file)
@@ -31,6 +31,12 @@ int giveTestQuest(NPC *speaker){
        return 0;
 }
 
+int giveStuff(NPC *speaker){
+       ui::dialogBox(speaker->name,"Take my stuff you ugly whore");
+       player->inv->addItem(SWORD_ITEM,1);
+       return 0;
+}
+
 void initEverything(void){
        unsigned int i;
        
@@ -59,5 +65,6 @@ void initEverything(void){
        NPCp(entity[1])->addAIFunc(giveTestQuest);
        for(i=0;i<entity.size()+1;i++){
                entity[i]->inWorld=test;
+               if(entity[i]->type==NPCT&&i>1)NPCp(entity[i])->addAIFunc(giveStuff);
        }
 }
index b8905dd4d803dd1e05ac047b0f8839c171897e1a..3882fb099429649db04ed3396ef5bf3fe6a5112c 100644 (file)
@@ -1,11 +1,34 @@
 #include <inventory.h>
 #include <ui.h>
 
+#define ITEM_COUNT 2   // Total number of items that actually exist
+
 const char *itemName[]={
        "\0",
-       "Dank Maymay"
+       "Dank Maymay",
+       "Sword"
+};
+
+const char *ITEM_SPRITE[]={
+       "\0",                                                   // Null
+       "assets/items/ITEM_TEST.png",   // Dank maymay
+       "assets/items/ITEM_SWORD.png"
 };
 
+GLuint *ITEM_TEX;
+
+unsigned int initInventorySprites(void){
+       unsigned int i,loadCount=0;
+       ITEM_TEX=(GLuint *)calloc(ITEM_COUNT,sizeof(GLuint));
+       for(i=0;i<ITEM_COUNT;i++){
+               if((ITEM_TEX[i]=loadTexture(ITEM_SPRITE[i+1])))loadCount++;
+       }
+#ifdef DEBUG
+       DEBUG_printf("Loaded %u/%u item texture(s).\n",loadCount,ITEM_COUNT);
+#endif // DEBUG
+       return loadCount;
+}
+
 Inventory::Inventory(unsigned int s){
        size=s;
        item=(struct item_t *)calloc(size,sizeof(struct item_t));
@@ -48,11 +71,22 @@ extern Player *player;
 
 void Inventory::draw(void){
        unsigned int i=0;
-       float y=SCREEN_HEIGHT/2;
+       float y=SCREEN_HEIGHT/2,xoff;
        ui::putText(player->loc.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);
+               glEnable(GL_TEXTURE_2D);
+               glBindTexture(GL_TEXTURE_2D,ITEM_TEX[item[i].id-1]);
+               glBegin(GL_QUADS);
+                       glTexCoord2i(0,1);glVertex2i(xoff                 ,y);
+                       glTexCoord2i(1,1);glVertex2i(xoff+HLINE*10,y);
+                       glTexCoord2i(1,0);glVertex2i(xoff+HLINE*10,y+HLINE*10);
+                       glTexCoord2i(0,0);glVertex2i(xoff                 ,y+HLINE*10);
+               glEnd();
                y-=ui::fontSize*1.15;
-               ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"%d x %s",item[i].count,itemName[(unsigned)item[i].id]);
+               ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"%s",itemName[(unsigned)item[i].id]);
+               glDisable(GL_TEXTURE_2D);
                i++;
        }
 }
index e4af3e238c16712cf1c01f5fe82e5432a7b5f215..68601d80c09b195d506bcd4f22034e8369ba817b 100644 (file)
@@ -28,12 +28,18 @@ namespace ui {
                        abort();
                }
                fontSize=12; // to be safe
+#ifdef DEBUG
+               DEBUG_printf("Initialized FreeType2.\n");
+#endif // DEBUG
        }
        void setFontFace(const char *ttf){
                if(FT_New_Face(ftl,ttf,0,&ftf)){
                        std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl;
                        abort();
-               }       
+               }
+#ifdef DEBUG
+               DEBUG_printf("Using font %s\n",ttf);
+#endif // DEBUG
        }
        void setFontSize(unsigned int size){
                fontSize=size;
@@ -110,7 +116,7 @@ namespace ui {
                glDeleteTextures(1,&ftex);
                return w;
        }
-       void putString(const float x,const float y,const char *s){
+       float putString(const float x,const float y,const char *s){
                unsigned int i=0,j;
                float xo=x,yo=y;
                do{
@@ -123,16 +129,19 @@ namespace ui {
                                xo+=putChar(xo,yo,s[i])+fontSize*.1;
                        }
                }while(s[i++]);
+               return xo;
        }
-       void putText(const float x,const float y,const char *str,...){  // putText() simply runs 'str' and the extra arguments though
+       float putText(const float x,const float y,const char *str,...){ // putText() simply runs 'str' and the extra arguments though
                va_list args;                                                                                           // vsnprintf(), which'll store the complete string to a buffer
                char *buf;                                                                                                      // that's then passed to putString()
+               float width;
                buf=(char *)calloc(128,sizeof(char));
                va_start(args,str);
                vsnprintf(buf,128,str,args);
                va_end(args);
-               putString(x,y,buf);
+               width=putString(x,y,buf);
                free(buf);
+               return width;
        }
        void dialogBox(const char *name,const char *text,...){
                unsigned int name_len;
index 18a07259bb3e322146f87c175f87f1e7f9738c5f..72287be0b931553a57c367f43ebd225e4412b453 100644 (file)
@@ -158,7 +158,15 @@ void World::singleDetect(Entity *e){
        unsigned int i;
        if(e->alive){
                i=(e->loc.x+e->width/2-x_start)/HLINE;  // Calculate what line the player is currently on
-               if(e->loc.y>line[i].y-.002*deltaTime){  // Snap the player to the top of that line if the player is inside it
+               if(e->type==STRUCTURET||e->loc.y<line[i].y){
+                       e->vel.y=0;
+                       e->ground=true;
+                       e->loc.y=line[i].y-.001*deltaTime;      
+                       if(e->type==STRUCTURET){
+                               std::cout<<e->loc.x<<" "<<e->loc.y<<std::endl;
+                               return;
+                       }
+               }else if(e->loc.y>line[i].y-.002*deltaTime){    // Snap the player to the top of that line if the player is inside it
                        for(i=0;i<platform.size();i++){
                                if(((e->loc.x+e->width>platform[i].p1.x)&(e->loc.x+e->width<platform[i].p2.x))||
                                   ((e->loc.x<platform[i].p2.x)&(e->loc.x>platform[i].p1.x))){
@@ -173,10 +181,6 @@ void World::singleDetect(Entity *e){
                                }
                        }
                        e->vel.y-=.001*deltaTime;
-               }else if(e->loc.y<line[i].y){
-                       e->vel.y=0;
-                       e->ground=true;
-                       e->loc.y=line[i].y-.001*deltaTime;      
                }
                if(e->loc.x<x_start){                           // Keep the player inside world bounds (ui.cpp handles world jumping)
                        e->vel.x=0;