From 3120be4f673c3e106c47ee250ca02179bacec52f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 8 Oct 2015 09:10:08 -0400 Subject: improved inventory, debug flags --- Makefile | 8 +++++-- assets/items/ITEM_SWORD.png | Bin 0 -> 420 bytes assets/items/ITEM_TEST.png | Bin 0 -> 6585 bytes include/common.h | 5 ++-- include/inventory.h | 7 +++++- include/ui.h | 6 +++-- include/world.h | 2 +- main.cpp | 2 ++ out/Quest.o | Bin 32596 -> 32616 bytes out/common.o | Bin 2188 -> 2452 bytes out/entities.o | Bin 93600 -> 93736 bytes out/gameplay.o | Bin 43748 -> 44148 bytes out/inventory.o | Bin 3156 -> 4588 bytes out/ui.o | Bin 9844 -> 10012 bytes out/world.o | Bin 38716 -> 39432 bytes src/common.cpp | 54 ++++++++++++++++++++++++++------------------ src/gameplay.cpp | 7 ++++++ src/inventory.cpp | 40 +++++++++++++++++++++++++++++--- src/ui.cpp | 17 ++++++++++---- src/world.cpp | 14 ++++++++---- 20 files changed, 120 insertions(+), 42 deletions(-) create mode 100644 assets/items/ITEM_SWORD.png create mode 100644 assets/items/ITEM_TEST.png diff --git a/Makefile b/Makefile index a8f2d88..387f5f3 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ -LIBS = -lGL -lSDL2_image -lSDL2_mixer +LIBS = -lGL +WIN_LIBS = -lopengl32 -lmingw32 -FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -lSDL2main -lSDL2 -lfreetype +FLAGS = -m32 -std=c++11 -Iinclude -Iinclude/freetype2 -lSDL2main -lSDL2 -lfreetype -lSDL2_image -lSDL2_mixer all: @rm -f out/*.o @@ -8,6 +9,9 @@ all: @echo " CXX main.cpp" @g++ $(FLAGS) -o main main.cpp out/*.o $(LIBS) +win32: + @g++ $(FLAGS) -o main main.cpp src/*.cpp $(WIN_LIBS) + clean: @echo " RM main" @-rm -f main diff --git a/assets/items/ITEM_SWORD.png b/assets/items/ITEM_SWORD.png new file mode 100644 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 index 0000000..f3e6c97 Binary files /dev/null and b/assets/items/ITEM_TEST.png differ diff --git a/include/common.h b/include/common.h index 3015b11..ed2ed9e 100644 --- a/include/common.h +++ b/include/common.h @@ -41,8 +41,6 @@ enum GENDER{ template //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 diff --git a/include/inventory.h b/include/inventory.h index 8f42cc5..d68fed9 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -3,8 +3,11 @@ #include +#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 diff --git a/include/ui.h b/include/ui.h index 5304c1b..6f23a78 100644 --- a/include/ui.h +++ b/include/ui.h @@ -4,6 +4,8 @@ #include #include // 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 diff --git a/include/world.h b/include/world.h index 1f8b7e1..4acb9ee 100644 --- a/include/world.h +++ b/include/world.h @@ -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; // 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. diff --git a/main.cpp b/main.cpp index 41f8e79..f0ca288 100644 --- 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(); } diff --git a/out/Quest.o b/out/Quest.o index 50fd936..7e52667 100644 Binary files a/out/Quest.o and b/out/Quest.o differ diff --git a/out/common.o b/out/common.o index 044750a..9153094 100644 Binary files a/out/common.o and b/out/common.o differ diff --git a/out/entities.o b/out/entities.o index 69472f4..baec147 100644 Binary files a/out/entities.o and b/out/entities.o differ diff --git a/out/gameplay.o b/out/gameplay.o index 6894853..f976e64 100644 Binary files a/out/gameplay.o and b/out/gameplay.o differ diff --git a/out/inventory.o b/out/inventory.o index d88bf53..85313ce 100644 Binary files a/out/inventory.o and b/out/inventory.o differ diff --git a/out/ui.o b/out/ui.o index e4cd1fd..5cc559b 100644 Binary files a/out/ui.o and b/out/ui.o differ diff --git a/out/world.o b/out/world.o index 4997e5b..be84bdf 100644 Binary files a/out/world.o and b/out/world.o differ diff --git a/src/common.cpp b/src/common.cpp index 1152be7..7a51b4e 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,26 +1,36 @@ #include 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); } diff --git a/src/gameplay.cpp b/src/gameplay.cpp index b6b939a..a690203 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -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;iinWorld=test; + if(entity[i]->type==NPCT&&i>1)NPCp(entity[i])->addAIFunc(giveStuff); } } diff --git a/src/inventory.cpp b/src/inventory.cpp index b8905dd..3882fb0 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -1,11 +1,34 @@ #include #include +#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;iloc.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++; } } diff --git a/src/ui.cpp b/src/ui.cpp index e4af3e2..68601d8 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -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 "<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.yvel.y=0; + e->ground=true; + e->loc.y=line[i].y-.001*deltaTime; + if(e->type==STRUCTURET){ + std::cout<loc.x<<" "<loc.y<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;iloc.x+e->width>platform[i].p1.x)&(e->loc.x+e->widthloc.xloc.x>platform[i].p1.x))){ @@ -173,10 +181,6 @@ void World::singleDetect(Entity *e){ } } e->vel.y-=.001*deltaTime; - }else if(e->loc.yvel.y=0; - e->ground=true; - e->loc.y=line[i].y-.001*deltaTime; } if(e->loc.xvel.x=0; -- cgit v1.2.3