diff options
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | assets/items/ITEM_SWORD.png | bin | 0 -> 420 bytes | |||
-rw-r--r-- | assets/items/ITEM_TEST.png | bin | 0 -> 6585 bytes | |||
-rw-r--r-- | include/common.h | 5 | ||||
-rw-r--r-- | include/inventory.h | 7 | ||||
-rw-r--r-- | include/ui.h | 6 | ||||
-rw-r--r-- | include/world.h | 2 | ||||
-rw-r--r-- | main.cpp | 2 | ||||
-rw-r--r-- | out/Quest.o | bin | 32596 -> 32616 bytes | |||
-rw-r--r-- | out/common.o | bin | 2188 -> 2452 bytes | |||
-rw-r--r-- | out/entities.o | bin | 93600 -> 93736 bytes | |||
-rw-r--r-- | out/gameplay.o | bin | 43748 -> 44148 bytes | |||
-rw-r--r-- | out/inventory.o | bin | 3156 -> 4588 bytes | |||
-rw-r--r-- | out/ui.o | bin | 9844 -> 10012 bytes | |||
-rw-r--r-- | out/world.o | bin | 38716 -> 39432 bytes | |||
-rw-r--r-- | src/common.cpp | 54 | ||||
-rw-r--r-- | src/gameplay.cpp | 7 | ||||
-rw-r--r-- | src/inventory.cpp | 40 | ||||
-rw-r--r-- | src/ui.cpp | 17 | ||||
-rw-r--r-- | src/world.cpp | 14 |
20 files changed, 120 insertions, 42 deletions
@@ -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 Binary files differnew file mode 100644 index 0000000..306f58c --- /dev/null +++ b/assets/items/ITEM_SWORD.png diff --git a/assets/items/ITEM_TEST.png b/assets/items/ITEM_TEST.png Binary files differnew file mode 100644 index 0000000..f3e6c97 --- /dev/null +++ b/assets/items/ITEM_TEST.png 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<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 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 <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 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 <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 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> 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. @@ -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 Binary files differindex 50fd936..7e52667 100644 --- a/out/Quest.o +++ b/out/Quest.o diff --git a/out/common.o b/out/common.o Binary files differindex 044750a..9153094 100644 --- a/out/common.o +++ b/out/common.o diff --git a/out/entities.o b/out/entities.o Binary files differindex 69472f4..baec147 100644 --- a/out/entities.o +++ b/out/entities.o diff --git a/out/gameplay.o b/out/gameplay.o Binary files differindex 6894853..f976e64 100644 --- a/out/gameplay.o +++ b/out/gameplay.o diff --git a/out/inventory.o b/out/inventory.o Binary files differBinary files differindex d88bf53..85313ce 100644 --- a/out/inventory.o +++ b/out/inventory.o diff --git a/out/world.o b/out/world.o Binary files differindex 4997e5b..be84bdf 100644 --- a/out/world.o +++ b/out/world.o 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 <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); } 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;i<entity.size()+1;i++){ entity[i]->inWorld=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 <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++; } } @@ -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; diff --git a/src/world.cpp b/src/world.cpp index 18a0725..72287be 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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; |