diff options
author | drumsetmonkey <abelleisle@roadrunner.com> | 2016-02-11 08:25:57 -0500 |
---|---|---|
committer | drumsetmonkey <abelleisle@roadrunner.com> | 2016-02-11 08:25:57 -0500 |
commit | a0afc9a192016b2bb4c185895a4082cc965e1179 (patch) | |
tree | 949464ae46509c8689890f2d5d55da54860bf14f /src | |
parent | e88af920196476ffb5ae182e879f045692cd3ef4 (diff) |
Worlds can be themed
Diffstat (limited to 'src')
-rw-r--r-- | src/Texture.cpp | 24 | ||||
-rw-r--r-- | src/entities.cpp | 60 | ||||
-rw-r--r-- | src/ui.cpp | 15 | ||||
-rw-r--r-- | src/world.cpp | 110 |
4 files changed, 125 insertions, 84 deletions
diff --git a/src/Texture.cpp b/src/Texture.cpp index e715b1e..1487dcd 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -4,6 +4,7 @@ struct texture_t { char *name; GLuint tex; + dim2 dim; } __attribute__ ((packed)); struct index_t{ @@ -59,17 +60,28 @@ namespace Texture{ GL_UNSIGNED_BYTE, image->pixels ); - - SDL_FreeSurface(image); // Free the surface LoadedTexture[LoadedTextureCounter] = new struct texture_t; //(struct texture_t *)malloc(sizeof(struct texture_t)); LoadedTexture[LoadedTextureCounter]->name = new char[strlen(fileName)+1]; //(char *)malloc(safe_strlen(fileName)); LoadedTexture[LoadedTextureCounter]->tex = object; strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName); + LoadedTexture[LoadedTextureCounter]->dim.x = image->w; + LoadedTexture[LoadedTextureCounter]->dim.y = image->h; LoadedTextureCounter++; + SDL_FreeSurface(image); // Free the surface + return object; } + + dim2 imageDim(const char *fileName){ + for(unsigned int i=0;i<LoadedTextureCounter;i++){ + if(!strcmp(LoadedTexture[i]->name,fileName)){ + return LoadedTexture[i]->dim; + } + } + return {0,0}; + } void freeTextures(void){ for(unsigned int i=0;i<LoadedTextureCounter;i++){ @@ -158,6 +170,14 @@ Texturec::Texturec(uint amt, ...){ va_end(fNames); } +Texturec::Texturec(std::vector<std::string>v){ + texState = 0; + image = new GLuint[v.size()]; + for(unsigned int i = 0; i < v.size(); i++){ + image[i] = Texture::loadTexture(v[i].c_str()); + } +} + Texturec::Texturec(uint amt,const char **paths){ texState = 0; image = new GLuint[amt]; diff --git a/src/entities.cpp b/src/entities.cpp index 5a9c42a..0469ad2 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -12,15 +12,6 @@ extern Player *player; extern const char *itemName; -std::string sTexLoc[] = { "assets/townhall.png", - "assets/house1.png", - "assets/house2.png", - "assets/house1.png", - "assets/house1.png", - "assets/fountain1.png", - "assets/lampPost1.png", - "assets/brazzier.png"}; - GLuint waterTex; void initEntity(){ @@ -95,15 +86,15 @@ Player::Player(){ //sets all of the player specific traits on object creation subtype = 0; health = maxHealth = 100; speed = 1; - tex = new Texturec(9, "assets/playerk.png", - "assets/playerk1.png", - "assets/playerk2.png", - "assets/playerk3.png", - "assets/playerk4.png", - "assets/playerk5.png", - "assets/playerk6.png", - "assets/playerk7.png", - "assets/playerk8.png"); + tex = new Texturec(9, "assets/player/playerk.png", + "assets/player/playerk1.png", + "assets/player/playerk2.png", + "assets/player/playerk3.png", + "assets/player/playerk4.png", + "assets/player/playerk5.png", + "assets/player/playerk6.png", + "assets/player/playerk7.png", + "assets/player/playerk8.png"); inv = new Inventory(PLAYER_INV_SIZE); } Player::~Player(){ @@ -126,7 +117,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation tex = new Texturec(1,"assets/NPC.png"); inv = new Inventory(NPC_INV_SIZE); - randDialog = 6;//rand() % 12 - 1; + randDialog = rand() % 12 - 1; dialogIndex = 0; } NPC::~NPC(){ @@ -462,6 +453,7 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ alive = true; bsubtype = sub; + dim2 dim; /* * tempN is the amount of entities that will be spawned in the village. Currently the village @@ -470,33 +462,11 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ //unsigned int tempN = (getRand() % 5 + 2); switch(sub){ - case TOWN_HALL: - tex = new Texturec(1, sTexLoc[sub].c_str()); - width = 50 * HLINE; - height = 40 * HLINE; - - break; - case HOUSE: - tex = new Texturec(1, sTexLoc[sub].c_str()); - width = 50 * HLINE; - height = 40 * HLINE; - break; - case FOUNTAIN: - tex = new Texturec(1, sTexLoc[sub].c_str()); - width = 50 * HLINE; - height = 40 * HLINE; - break; - case LAMP_POST: - tex = new Texturec(1, sTexLoc[sub].c_str()); - width = 10 * HLINE; - height = 40 * HLINE; - break; - case FIRE_PIT: - tex = new Texturec(1, sTexLoc[sub].c_str()); - width = 12 * HLINE; - height = 12 * HLINE; - break; default: + tex = new Texturec(1, textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); + dim = Texture::imageDim(textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); + width = dim.x; + height = dim.y; break; } return 0; @@ -146,10 +146,10 @@ namespace ui { #ifdef DEBUG DEBUG_printf("Initialized FreeType2.\n",NULL); #endif // DEBUG - dialogClick = Mix_LoadWAV("assets/click.wav"); + dialogClick = Mix_LoadWAV("assets/sounds/click.wav"); battleStart = Mix_LoadWAV("assets/sounds/frig.wav"); sanic = Mix_LoadWAV("assets/sounds/sanic.wav"); - Mix_Volume(1,50); + //Mix_Volume(1,50); } void destroyFonts(void){ @@ -258,9 +258,11 @@ namespace ui { * Draws a character at the specified coordinates, aborting if the character is unknown. */ - vec2 putChar(float x,float y,char c){ + vec2 putChar(float xx,float yy,char c){ vec2 c1,c2; - + + int x = xx, y = yy; + /* * Get the width and height of the rendered character. */ @@ -725,8 +727,7 @@ namespace ui { */ void drawMenu(Menu *menu){ - - setFontSize(20); + setFontSize(18); SDL_Event e; mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2); @@ -1229,7 +1230,7 @@ DONE: //currentWorld->addVillage(player->loc.x, player->loc.y, 5, 10, 100, NULL); break; case SDLK_b: - currentWorld->addStructure(FIRE_PIT, player->loc.x, player->loc.y, NULL); + currentWorld->addStructure(FIRE_PIT, player->loc.x, player->loc.y, NULL, NULL); currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f}); break; case SDLK_F12: diff --git a/src/world.cpp b/src/world.cpp index 6118851..2012d32 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -17,23 +17,49 @@ bool worldInside = false; // True if player is inside a structure WEATHER weather = SUNNY; -const char *bgPaths[2][7]={ - {"assets/bg.png", // Daytime background - "assets/bgn.png", // Nighttime background - "assets/bgFarMountain.png", // Furthest layer - "assets/forestTileBack.png", // Closer layer - "assets/forestTileMid.png", // Near layer - "assets/forestTileFront.png", // Closest layer - "assets/dirt.png"}, // Dirt - {"assets/bgWoodTile.png", - NULL, - NULL, - NULL, - NULL, - NULL} +// const char *bgPaths[2][7]={ +// {"assets/bg.png", // Daytime background +// "assets/bgn.png", // Nighttime background +// "assets/bgFarMountain.png", // Furthest layer +// "assets/forestTileBack.png", // Closer layer +// "assets/forestTileMid.png", // Near layer +// "assets/forestTileFront.png", // Closest layer +// "assets/dirt.png"}, // Dirt +// {"assets/bgWoodTile.png", +// NULL, +// NULL, +// NULL, +// NULL, +// NULL} +// }; + +const std::string bgPaths[2][8]={ + {"bg.png", // Daytime background + "bgn.png", // Nighttime background + "bgFarMountain.png", // Furthest layer + "forestTileBack.png", // Closer layer + "forestTileMid.png", // Near layer + "forestTileFront.png", // Closest layer + "dirt.png", // Dirt + "grass.png"}, // Grass + {"bgWoodTile.png", + "bgWoodTile.png", + "bgWoodTile.png", + "bgWoodTile.png", + "bgWoodTile.png", + "bgWoodTile.png", + "bgWoodTile.png", + "bgWoodTile.png"} }; -Texturec *grassT; +const std::string buildPaths[] = { "townhall.png", + "house1.png", + "house2.png", + "house1.png", + "house1.png", + "fountain1.png", + "lampPost1.png", + "brazzier.png"}; const float bgDraw[3][3]={ {100,240,.6 }, @@ -56,14 +82,34 @@ void World::setBackground(WORLD_BG_TYPE bgt){ bgType = bgt; switch(bgt){ case BG_FOREST: - bgTex = new Texturec(7,bgPaths[0]); + bgTex = new Texturec(bgFiles); break; case BG_WOODHOUSE: - bgTex = new Texturec(1,bgPaths[1]); + bgTex = new Texturec(bgFilesIndoors); break; } } +void World::setStyle(const char* pre){ + std::string prefix = pre ? pre : "assets/style/classic/"; + for(uint i = 0; i < arrAmt(buildPaths);i++){ + sTexLoc.push_back(prefix); + sTexLoc.back() += buildPaths[i]; + std::cout << sTexLoc.back() << std::endl; + } + prefix += "bg/"; + for(uint i = 0; i < arrAmt(bgPaths[0]);i++){ + bgFiles.push_back(prefix); + bgFiles.back() += bgPaths[0][i]; + std::cout << bgFiles.back() << std::endl; + } + for(uint i = 0; i < arrAmt(bgPaths[1]);i++){ + bgFilesIndoors.push_back(prefix); + bgFilesIndoors.back() += bgPaths[1][i]; + std::cout << bgFilesIndoors.back() << std::endl; + } +} + World::World(void){ bgm = NULL; @@ -78,7 +124,6 @@ World::World(void){ star = new vec2[100]; memset(star,0,100 * sizeof(vec2)); - grassT = new Texturec(1,"assets/grass.png"); } void World::deleteEntities(void){ @@ -595,10 +640,10 @@ void World::draw(Player *p){ hey=true; glColor4ub(0,0,0,255); }else safeSetColorA(150+shade*2,150+shade*2,150+shade*2,255); - glTexCoord2i(0,0);glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT); - glTexCoord2i(1,0);glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT); + glTexCoord2i(0,0); glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT); + glTexCoord2i(1,0); glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT); glTexCoord2i(1,(int)(cline[i].y/64)+cline[i].color);glVertex2i(cx_start+i*HLINE+HLINE,0); - glTexCoord2i(0,(int)(cline[i].y/64)+cline[i].color);glVertex2i(cx_start+i*HLINE ,0); + glTexCoord2i(0,(int)(cline[i].y/64)+cline[i].color);glVertex2i(cx_start+i*HLINE ,0); cline[i].y-=(yoff-DRAW_Y_OFFSET); // Restore the line's y value if(hey){ hey=false; @@ -616,7 +661,7 @@ void World::draw(Player *p){ glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); - grassT->bind(0); + bgTex->bindNext(); glUseProgram(shaderProgram); glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //for the s direction @@ -948,10 +993,12 @@ void World::detect(Player *p){ goto LOOOOP; }*/ } -void World::addStructure(BUILD_SUB sub, float x,float y,const char *inside){ +void World::addStructure(BUILD_SUB sub, float x,float y, char *tex, const char *inside){ build.push_back(new Structures()); + build.back()->inWorld = this; + build.back()->textureLoc = tex; build.back()->spawn(sub,x,y); - + if(inside) strcpy((build.back()->inside = new char[1 + strlen(inside)]),inside); else @@ -1071,8 +1118,7 @@ char *World::setToRight(const char *file){ World *World::goWorldLeft(Player *p){ World *tmp; - - if(toLeft && p->loc.x < x_start + HLINE * 15){ + if(toLeft && p->loc.x < x_start + (int)HLINE * 15){ tmp = loadWorldFromXML(toLeft); p->loc.x = -tmp->x_start - HLINE * 10; @@ -1474,6 +1520,7 @@ World *loadWorldFromXML(const char *path){ tmp->setToRight(ptr); else abort(); }else if(!strcmp(name,"style")){ + tmp->setStyle(wxml->Attribute("folder")); tmp->setBackground((WORLD_BG_TYPE)wxml->UnsignedAttribute("background")); tmp->setBGM(wxml->Attribute("bgm")); }else if(!strcmp(name,"generation")){ @@ -1519,10 +1566,13 @@ World *loadWorldFromXML(const char *path){ }else if(!strcmp(name,"structure")){ ptr = wxml->Attribute("inside"); - if(wxml->QueryFloatAttribute("x",&spawnx) != XML_NO_ERROR) - tmp->addStructure((BUILD_SUB)wxml->UnsignedAttribute("type"),getRand() % tmp->getTheWidth() / 2.0f,100,ptr); - else - tmp->addStructure((BUILD_SUB)wxml->UnsignedAttribute("type"),spawnx,wxml->FloatAttribute("y"),ptr); + tmp->addStructure((BUILD_SUB)wxml->UnsignedAttribute("type"), + wxml->QueryFloatAttribute("x",&spawnx) != XML_NO_ERROR ? + getRand() % tmp->getTheWidth() / 2.0f : + spawnx, + 100, + (char*)wxml->Attribute("texture"), + ptr); }else if(!strcmp(name,"trigger")){ tmp->addMob(MS_TRIGGER,wxml->FloatAttribute("x"),0,commonTriggerFunc); tmp->mob.back()->heyid = wxml->Attribute("id"); |