From: Andy Date: Fri, 23 Dec 2016 13:42:15 +0000 (-0500) Subject: Sprites are good. Merged. X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=d7d1b397197893f0ce49b28f762711b7a9ef1087;p=clyne%2Fgamedev.git Sprites are good. Merged. --- d7d1b397197893f0ce49b28f762711b7a9ef1087 diff --cc include/components.hpp index 2a8680d,bbf153a..5c067dd --- a/include/components.hpp +++ b/include/components.hpp @@@ -125,33 -125,12 +125,36 @@@ struct Solid }; struct SpriteData { - - SpriteData(std::string path, vec2 offset): - offset(offset) { - pic = Texture::loadTexture(path); - size = Texture::imageDim(path); ++ ++ SpriteData(std::string path, vec2 off): ++ offset(off) { ++ tex = Texture(path); ++ size = tex.getDim(); ++ + size_tex = vec2(1.0, 1.0); + + offset_tex.x = offset.x/size.x; + offset_tex.y = offset.y/size.y; + } + - SpriteData(std::string path, vec2 offset, vec2 size): - offset(offset), size(size) { - pic = Texture::loadTexture(path); - vec2 tmpsize = Texture::imageDim(path); ++ SpriteData(std::string path, vec2 off, vec2 si): ++ size(si), offset(off) { ++ tex = Texture(path); ++ vec2 tmpsize = tex.getDim(); - SpriteData(std::string path, vec2 offset) - : tex(path), offset(offset) {} + size_tex.x = size.x/tmpsize.x; + size_tex.y = size.y/tmpsize.y; ++ + offset_tex.x = offset.x/tmpsize.x; + offset_tex.y = offset.y/tmpsize.y; + } - GLuint pic; + Texture tex; ++ vec2 size; vec2 offset; ++ + vec2 offset_tex; - vec2 size; + vec2 size_tex; }; using Frame = std::vector>; diff --cc include/engine.hpp index 1c5f510,52569e7..2b03696 --- a/include/engine.hpp +++ b/include/engine.hpp @@@ -18,11 -34,27 +34,28 @@@ public explicit Engine(void); + /** + * Initializes the game engine, and all systems used within it. + */ void init(void); + + /** + * Updates all rendering systems. + * @param dt the delta time + */ void render(entityx::TimeDelta dt); + void resetRender(entityx::TimeDelta dt); + + /** + * Updates all logic systems. + * @param dt the delta time + */ void update(entityx::TimeDelta dt); + /** + * A shortcut to get a system, for calling system-specific functions. + * Takes the type of the desired system. + */ template inline T* getSystem(void) { return dynamic_cast(systems.system().get()); diff --cc main.cpp index 6631736,7fc1658..17499f7 --- a/main.cpp +++ b/main.cpp @@@ -124,14 -131,10 +131,12 @@@ int main(int argc, char *argv[] game::briceLoad(); game::briceUpdate(); - // + // load sprites used in the inventory menu. See src/inventory.cpp + //initInventorySprites(); + - // load mouse texture, and other inventory textures - mouseTex = Texture::loadTexture("assets/goodmouse.png"); - // get a world + // + if (xmlFolder.empty()) xmlFolder = "xml/"; @@@ -227,18 -218,26 +220,26 @@@ const bool &run = game::engine.shouldRun; while (run) { render(); - game::engine.render(0); + game::engine.resetRender(0); } + // on game end, get back together thMain.join(); thDebug.join(); - //game::engine.getSystem()->thAmbient.join(); + //game::engine.getSystem()->thAmbient.join(); // segfault or something } - // put away the brice for later + // + // put away the brice for later, save world + // + game::briceSave(); + worldSys->save(); + + // + // close things, free stuff, yada yada + // - // free library resources Mix_HaltMusic(); Mix_CloseAudio(); @@@ -255,8 -254,19 +256,19 @@@ } void render() { - const auto SCREEN_WIDTH = game::SCREEN_WIDTH; - const auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; - static const Texture mouseTex ("assets/mouse.png"); ++ static const Texture mouseTex ("assets/goodmouse.png"); + static const glm::mat4 view = glm::lookAt( + glm::vec3(0.0f, 0.0f, 0.0f), // pos + glm::vec3(0.0f, 0.0f, -10.0f), // looking at + glm::vec3(0.0f, 1.0f, 0.0f) // up vector + ); + + static const auto& SCREEN_WIDTH2 = game::SCREEN_WIDTH / 2.0f; + static const auto& SCREEN_HEIGHT2 = game::SCREEN_HEIGHT / 2.0f; + + // + // set the ortho + // auto ps = game::engine.getSystem(); auto ploc = ps->getPosition(); @@@ -351,108 -335,8 +339,8 @@@ // draw the mouse Render::textShader.use(); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, mouseTex); + mouseTex.use(); Render::useShader(&Render::textShader); - Render::drawRect(ui::mouse, ui::mouse + 15, -9.9); + Render::drawRect(vec2(ui::mouse.x, ui::mouse.y - 64), vec2(ui::mouse.x + 64, ui::mouse.y), -9.9); Render::textShader.unuse(); } - - void logic(){ - // static bool NPCSelected = false; - // static bool ObjectSelected = false; - - // exit the game if the player falls out of the world - /*if (player->loc.y < 0) - game::endGame();*/ - - /*if (player->inv->usingi) { - for (auto &e : currentWorld->entity) { - if (player->inv->usingi && !e->isHit() && - player->inv->detectCollision(vec2 { e->loc.x, e->loc.y }, vec2 { e->loc.x + e->width, e->loc.y + e->height})) { - e->takeHit(25, 10); - break; - } - } - player->inv->usingi = false; - }*/ - - /*for (auto &e : currentWorld->entity) { - if (e->isAlive() && ((e->type == NPCT) || (e->type == MERCHT) || (e->type == OBJECTT))) { - if (e->type == OBJECTT && ObjectSelected) { - e->near = false; - continue; - } else if (e->canMove) { - if (!currentWorld->goWorldLeft(dynamic_cast(e))) - currentWorld->goWorldRight(dynamic_cast(e)); - e->wander((rand() % 120 + 30)); - if (NPCSelected) { - e->near = false; - continue; - } - } - - if(e->isInside(ui::mouse) && player->isNear(e)) { - e->near = true; - if (e->type == OBJECTT) - ObjectSelected = true; - else - NPCSelected = true; - - if ((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)) && !ui::dialogBoxExists) { - if (ui::mouse.x < player->loc.x && player->right) - player->left = true, player->right = false; - else if(ui::mouse.x > player->loc.x && player->left) - player->right = true, player->left = false; - e->interact(); - } - } else { - e->near = false; - } - } else if (e->type == MOBT) { - e->near = player->isNear(e); - e->wander(); - } - }*/ - - // calculate the world shading value - worldShade = 50 * sin((game::time::getTickCount() + (DAY_CYCLE / 2)) / (DAY_CYCLE / PI)); - - // update fades - ui::fadeUpdate(); - - // create weather particles if necessary - /*auto weather = game::engine.getSystem()->getWeatherId(); - auto worldWidth = game::engine.getSystem()->getWidth(); - if (weather == WorldWeather::Rain) { - for (unsigned int r = (randGet() % 25) + 11; r--;) { - currentWorld->addParticle(randGet() % worldWidth - (worldWidth / 2), - offset.y + game::SCREEN_HEIGHT / 2, - HLINES(1.25), // width - HLINES(1.25), // height - randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x - (4 + randGet() % 6) * .05, // vel.y - { 0, 0, 255 }, // RGB color - 2500, // duration (ms) - (1 << 0) | (1 << 1) // gravity and bounce - ); - } - } else if (weather == WorldWeather::Snowy) { - for (unsigned int r = (randGet() % 25) + 11; r--;) { - currentWorld->addParticle(randGet() % worldWidth - (worldWidth / 2), - offset.y + game::SCREEN_HEIGHT / 2, - HLINES(1.25), // width - HLINES(1.25), // height - .0001 + randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x - (4 + randGet() % 6) * -.03, // vel.y - { 255, 255, 255 }, // RGB color - 5000, // duration (ms) - 0 // no gravity, no bounce - ); - } - }*/ - - // increment game ticker - game::time::tick(); - //NPCSelected = false; - //ObjectSelected = false; - } diff --cc src/components.cpp index 323dfc8,f95f46f..7322208 --- a/src/components.cpp +++ b/src/components.cpp @@@ -19,13 -21,11 +21,11 @@@ void MovementSystem::update(entityx::En position.x += direction.x * dt; position.y += direction.y * dt; -- if (entity.has_component() && entity.has_component()) { - if (direction.x) { - entity.component()->nextFrame(entity.component()->sprite); - } else { - entity.component()->firstFrame(entity.component()->sprite); - } - } ++ /*if (entity.has_component() && entity.has_component()) { + auto animate = entity.component(); + entity.component()->sprite = + (direction.x != 0) ? animate->nextFrame() : animate->firstFrame(); - } ++ }*/ if (entity.has_component() && entity.component()->talking) { direction.x = 0; } else { @@@ -82,41 -82,34 +82,36 @@@ void RenderSystem::update(entityx::Enti en.each([dt](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) { (void)entity; // Verticies and shit - GLfloat tex_coord[] = {0.0, 0.0, - 1.0, 0.0, - 1.0, 1.0, - - 1.0, 1.0, - 0.0, 1.0, - 0.0, 0.0}; + float its = 0; + for (auto &S : sprite.sprite) { + auto sp = S.first; - float width = HLINES(S.first.size.x); - float height = HLINES(S.first.size.y); ++ auto size = sp.size * game::HLINE; + vec2 drawOffset(HLINES(S.second.x), HLINES(S.second.y)); + vec2 loc(pos.x + drawOffset.x, pos.y + drawOffset.y); - GLfloat tex_coordL[] = {1.0, 0.0, - 0.0, 0.0, - 0.0, 1.0, + GLfloat tex_coord[] = {sp.offset_tex.x, sp.offset_tex.y, + sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y, + sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y, - 0.0, 1.0, - 1.0, 1.0, - 1.0, 0.0}; + sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y, + sp.offset_tex.x, sp.offset_tex.y + sp.size_tex.y, + sp.offset_tex.x, sp.offset_tex.y}; - for (auto &S : sprite.sprite) { - const auto& size = S.first.tex.getDim() * game::HLINE; + if(sprite.faceLeft) { - + glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(-1.0f,1.0f,1.0f)); - glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(0-width-pos.x*2, 0.0f, 0.0f)); ++ glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f - size.x - pos.x * 2.0f, 0.0f, 0.0f)); - vec2 loc = vec2(pos.x + S.first.offset.x, pos.y + S.first.offset.y); + glm::mat4 mov = scale * translate; + glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(mov)); - - } else { - + } - GLfloat coords[] = {loc.x, loc.y, visible.z, - loc.x + size.x, loc.y, visible.z, - loc.x + size.x, loc.y + size.y, visible.z, + GLfloat coords[] = {loc.x, loc.y, visible.z + its, - loc.x + width, loc.y, visible.z + its, - loc.x + width, loc.y + height, visible.z + its, ++ loc.x + size.x, loc.y, visible.z + its, ++ loc.x + size.x, loc.y + size.y, visible.z + its, - loc.x + width, loc.y + height, visible.z + its, - loc.x, loc.y + height, visible.z + its, - loc.x + size.x, loc.y + size.y, visible.z, - loc.x, loc.y + size.y, visible.z, - loc.x, loc.y, visible.z}; ++ loc.x + size.x, loc.y + size.y, visible.z + its, ++ loc.x, loc.y + size.y, visible.z + its, + loc.x, loc.y, visible.z + its}; // make the entity hit flash red @@@ -126,7 -119,7 +121,7 @@@ glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, flashAmt, flashAmt, 1.0); }*/ - glBindTexture(GL_TEXTURE_2D, S.first.pic); - S.first.tex.use(); ++ sp.tex.use(); glUniform1i(Render::worldShader.uniform[WU_texture], 0); Render::worldShader.enable(); diff --cc src/ui.cpp index 84bb55f,9f0e014..6383cca --- a/src/ui.cpp +++ b/src/ui.cpp @@@ -739,56 -675,56 +675,56 @@@ namespace ui c2.x, c2.y, z, 1.0f, 1.0f, c2.x, c2.y, z, 1.0f, 1.0f, - c2.x - box_corner_dim.x, c2.y, z, 0.5f, 1.0f, - c2.x - box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 0.5f}; + c2.x - boxCornerDim2.x, c2.y, z, 0.5f, 1.0f, + c2.x - boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 0.5f}; - GLfloat box_lr[] = {c2.x - box_corner_dim.x, c1.y, z, 0.5f, 0.0f, + GLfloat box_lr[] = {c2.x - boxCornerDim2.x, c1.y, z, 0.5f, 0.0f, c2.x, c1.y, z, 1.0f, 0.0f, - c2.x, c1.y + box_corner_dim.y, z, 1.0f, 0.5f, + c2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.5f, - c2.x, c1.y + box_corner_dim.y, z, 1.0f, 0.5f, - c2.x - box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.5f, - c2.x - box_corner_dim.x, c1.y, z, 0.5f, 0.0f}; + c2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.5f, - c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.0f, 0.5f, ++ c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.5f, + c2.x - boxCornerDim2.x, c1.y, z, 0.5f, 0.0f}; - GLfloat box_l[] = {c1.x, c1.y + box_corner_dim.y, z, 0.0f, 0.0f, - c1.x + box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.0f, - c1.x + box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 1.0f, + GLfloat box_l[] = {c1.x, c1.y + boxCornerDim2.y, z, 0.0f, 0.0f, + c1.x + boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.0f, + c1.x + boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 1.0f, - c1.x + box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 1.0f, - c1.x, c2.y - box_corner_dim.y, z, 0.0f, 1.0f, - c1.x, c1.y + box_corner_dim.y, z, 0.0f, 0.0f}; + c1.x + boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 1.0f, + c1.x, c2.y - boxCornerDim2.y, z, 0.0f, 1.0f, + c1.x, c1.y + boxCornerDim2.y, z, 0.0f, 0.0f}; - GLfloat box_r[] = {c2.x - box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.0f, - c2.x, c1.y + box_corner_dim.y, z, 1.0f, 0.0f, - c2.x, c2.y - box_corner_dim.y, z, 1.0f, 1.0f, + GLfloat box_r[] = {c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.0f, + c2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.0f, + c2.x, c2.y - boxCornerDim2.y, z, 1.0f, 1.0f, - c2.x, c2.y - box_corner_dim.y, z, 1.0f, 1.0f, - c2.x - box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 1.0f, - c2.x - box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.0f}; + c2.x, c2.y - boxCornerDim2.y, z, 1.0f, 1.0f, + c2.x - boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 1.0f, + c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.0f}; - GLfloat box_b[] = {c1.x + box_corner_dim.x, c1.y, z, 0.0f, 0.0f, - c2.x - box_corner_dim.x, c1.y, z, 1.0f, 0.0f, - c2.x - box_corner_dim.x, c1.y + box_corner_dim.y, z, 1.0f, 0.5f, + GLfloat box_b[] = {c1.x + boxCornerDim2.x, c1.y, z, 0.0f, 0.0f, + c2.x - boxCornerDim2.x, c1.y, z, 1.0f, 0.0f, + c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.5f, - c2.x - box_corner_dim.x, c1.y + box_corner_dim.y, z, 1.0f, 0.5f, - c1.x + box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.0f, 0.5f, - c1.x + box_corner_dim.x, c1.y, z, 0.0f, 0.0f}; + c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.5f, + c1.x + boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.0f, 0.5f, + c1.x + boxCornerDim2.x, c1.y, z, 0.0f, 0.0f}; - GLfloat box_t[] = {c1.x + box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.0f, 0.5f, - c2.x - box_corner_dim.x, c2.y - box_corner_dim.y, z, 1.0f, 0.5f, - c2.x - box_corner_dim.x, c2.y, z, 1.0f, 1.0f, + GLfloat box_t[] = {c1.x + boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.0f, 0.5f, + c2.x - boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 1.0f, 0.5f, + c2.x - boxCornerDim2.x, c2.y, z, 1.0f, 1.0f, - c2.x - box_corner_dim.x, c2.y, z, 1.0f, 1.0f, - c1.x + box_corner_dim.x, c2.y, z, 0.0f, 1.0f, - c1.x + box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.0f, 0.5f}; + c2.x - boxCornerDim2.x, c2.y, z, 1.0f, 1.0f, + c1.x + boxCornerDim2.x, c2.y, z, 0.0f, 1.0f, + c1.x + boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.0f, 0.5f}; - GLfloat box_f[] = {c1.x + box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.5f, - c2.x - box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.5f, - c2.x - box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 0.5f, + GLfloat box_f[] = {c1.x + boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.5f, + c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.5f, + c2.x - boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 0.5f, - c2.x - box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 0.5f, - c1.x + box_corner_dim.x, c2.y - box_corner_dim.y, z, 0.5f, 0.5f, - c1.x + box_corner_dim.x, c1.y + box_corner_dim.y, z, 0.5f, 0.5f}; + c2.x - boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 0.5f, + c1.x + boxCornerDim2.x, c2.y - boxCornerDim2.y, z, 0.5f, 0.5f, + c1.x + boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.5f}; glActiveTexture(GL_TEXTURE0); glUniform1f(Render::textShader.uniform[WU_texture], 0);