-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
@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
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;
extern Mix_Music *music;
extern Mix_Chunk *horn;
+GLuint loadTexture(const char *fileName);
+void DEBUG_printf(const char *s,...);
+
#endif // COMMON_H
#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
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
#include <common.h>
#include <cstdarg> // For putText()
+#define DEBUG
+
namespace ui { // Functions are kept in a namespace simply
// for organization
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
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.
bgImage=loadTexture("assets/bg.png");
+ initInventorySprites();
+
while(gameRunning){
mainLoop();
}
#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);
}
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;
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);
}
}
#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));
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++;
}
}
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;
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{
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;
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))){
}
}
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;