diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/Graphics/je_graphics.h | 1 | ||||
-rw-r--r-- | src/libjin/Graphics/je_sprite.cpp | 28 | ||||
-rw-r--r-- | src/libjin/Graphics/je_sprite.h | 7 | ||||
-rw-r--r-- | src/libjin/Graphics/je_sprite_batch.h | 17 | ||||
-rw-r--r-- | src/libjin/Graphics/je_sprite_sheet.cpp | 22 | ||||
-rw-r--r-- | src/libjin/Graphics/je_sprite_sheet.h | 19 | ||||
-rw-r--r-- | src/lua/common/je_lua_shared.hpp | 7 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_graphics.cpp | 23 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_sprite.cpp | 4 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_spritesheet.h | 17 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_texture_font.cpp | 2 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_ttf.cpp | 2 | ||||
-rw-r--r-- | src/lua/modules/types.h | 1 |
13 files changed, 125 insertions, 25 deletions
diff --git a/src/libjin/Graphics/je_graphics.h b/src/libjin/Graphics/je_graphics.h index a46e740..979d8f4 100644 --- a/src/libjin/Graphics/je_graphics.h +++ b/src/libjin/Graphics/je_graphics.h @@ -11,6 +11,7 @@ #include "je_bitmap.h" #include "je_image.h" #include "je_sprite.h" +#include "je_sprite_sheet.h" #include "shaders/je_shader.h" diff --git a/src/libjin/Graphics/je_sprite.cpp b/src/libjin/Graphics/je_sprite.cpp index 9ee4fc1..176447c 100644 --- a/src/libjin/Graphics/je_sprite.cpp +++ b/src/libjin/Graphics/je_sprite.cpp @@ -2,6 +2,7 @@ #include "je_sprite.h" +using namespace JinEngine::Math; using namespace JinEngine::Graphics::Shaders; namespace JinEngine @@ -14,6 +15,7 @@ namespace JinEngine , mGraphic(nullptr) , mScale(1, 1) , mColor(255, 255, 255, 255) + , mIsOriginEnum(false) { } @@ -21,6 +23,16 @@ namespace JinEngine { } + void Sprite::setQuad(int x, int y, int w, int h) + { + mQuad.x = x; + mQuad.y = y; + mQuad.w = w; + mQuad.h = h; + if (mIsOriginEnum) + setOrigin(mOriginEnum); + } + void Sprite::setRotation(float r) { mRotation = r; @@ -28,12 +40,12 @@ namespace JinEngine void Sprite::setOrigin(Origin origin) { + mIsOriginEnum = true; + mOriginEnum = origin; int l = 0, r = 0, t = 0, b = 0; - if (mGraphic != nullptr) - { - r = mGraphic->getWidth(); - b = mGraphic->getHeight(); - } + Vector2<int> size = getSize(); + r = size.w; + b = size.h; switch (origin) { case TopLeft: @@ -78,6 +90,7 @@ namespace JinEngine void Sprite::setOrigin(int x, int y) { mOrigin.set(x, y); + mIsOriginEnum = false; } void Sprite::setPosition(int x, int y) @@ -103,6 +116,9 @@ namespace JinEngine void Sprite::setGraphic(const Graphic* graphic) { mGraphic = graphic; + int w = mGraphic->getWidth(); + int h = mGraphic->getHeight(); + setQuad(0, 0, w, h); } void Sprite::render() @@ -113,7 +129,7 @@ namespace JinEngine if(mShader != nullptr) mShader->use(); if(mGraphic != nullptr) - mGraphic->render(mPosition.x, mPosition.y, mScale.x, mScale.y, mRotation, mOrigin.x, mOrigin.y); + mGraphic->render(mQuad, mPosition.x, mPosition.y, mScale.x, mScale.y, mRotation, mOrigin.x, mOrigin.y); shader->use(); gl.setColor(c); } diff --git a/src/libjin/Graphics/je_sprite.h b/src/libjin/Graphics/je_sprite.h index fad5b44..ad95aa4 100644 --- a/src/libjin/Graphics/je_sprite.h +++ b/src/libjin/Graphics/je_sprite.h @@ -34,6 +34,7 @@ namespace JinEngine BottomRight }; + void setQuad(int x, int y, int w, int h); void setRotation(float r); void setOrigin(Origin origin); void setOrigin(int x, int y); @@ -44,6 +45,8 @@ namespace JinEngine void setGraphic(const Graphic* graphic); float getRotation() { return mRotation; } + Math::Vector2<int> getSize() { return Math::Vector2<int>(mQuad.w, mQuad.h); } + const Math::Quad& getQuad() { return mQuad; } const Math::Vector2<float>& getPosition() { return mPosition; } const Math::Vector2<int>& getOrigin() { return mOrigin; } const Math::Vector2<float>& getScale() { return mScale; } @@ -61,6 +64,10 @@ namespace JinEngine Math::Vector2<float> mPosition; Math::Vector2<int> mOrigin; Math::Vector2<float> mScale; + Math::Quad mQuad; + + bool mIsOriginEnum; + Origin mOriginEnum; float mRotation; Color mColor; diff --git a/src/libjin/Graphics/je_sprite_batch.h b/src/libjin/Graphics/je_sprite_batch.h index e69de29..7c1098b 100644 --- a/src/libjin/Graphics/je_sprite_batch.h +++ b/src/libjin/Graphics/je_sprite_batch.h @@ -0,0 +1,17 @@ +#ifndef __JE_GRAPHICS_SPRITE_BATCH_H__ +#define __JE_GRAPHICS_SPRITE_BATCH_H__ + +namespace JinEngine +{ + namespace Graphics + { + + class SpriteBatch + { + + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/libjin/Graphics/je_sprite_sheet.cpp b/src/libjin/Graphics/je_sprite_sheet.cpp index e69de29..3a08751 100644 --- a/src/libjin/Graphics/je_sprite_sheet.cpp +++ b/src/libjin/Graphics/je_sprite_sheet.cpp @@ -0,0 +1,22 @@ +#include "je_sprite_sheet.h" + +namespace JinEngine +{ + namespace Graphics + { + + SpriteSheet::SpriteSheet(const Graphic* graphic) + : mGraphic(graphic) + { + } + + Sprite* SpriteSheet::createSprite(const Math::Quad& quad) + { + Sprite* spr = new Sprite(); + spr->setGraphic(mGraphic); + spr->setQuad(quad.x, quad.y, quad.w, quad.h); + return spr; + } + + } +}
\ No newline at end of file diff --git a/src/libjin/Graphics/je_sprite_sheet.h b/src/libjin/Graphics/je_sprite_sheet.h index 76bd022..8c56c51 100644 --- a/src/libjin/Graphics/je_sprite_sheet.h +++ b/src/libjin/Graphics/je_sprite_sheet.h @@ -11,7 +11,7 @@ namespace JinEngine { namespace Graphics { - + class SpriteSheet { public: @@ -20,21 +20,10 @@ namespace JinEngine /// Sprite* createSprite(const Math::Quad& quad); - private: - class SpriteInSheet : public Sprite - { - public: - - private: - /// - /// Quad in sprite sheet. - /// - Math::Quad quad; + SpriteSheet(const Graphic* graphic); - }; - - std::vector<SpriteInSheet> mSprites; - Graphic* mGraphic; + private: + const Graphic* const mGraphic; }; diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp index d6ea357..2c3e309 100644 --- a/src/lua/common/je_lua_shared.hpp +++ b/src/lua/common/je_lua_shared.hpp @@ -78,6 +78,13 @@ namespace JinEngine mDependencies.clear(); } + SharedBase* getDependency(int key) + { + if (!isDependOn(key)) + return nullptr; + return mDependencies.find(key)->second; + } + protected: using DepMap = std::map<int, SharedBase*>; diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 5255de7..d129e6a 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -5,6 +5,7 @@ #include "lua/modules/luax.h" #include "lua/modules/types.h" #include "lua/common/je_lua_common.h" +#include "je_lua_spritesheet.h" using namespace std; using namespace JinEngine; @@ -666,6 +667,27 @@ namespace JinEngine return 1; } + LUA_IMPLEMENT int l_newSpriteSheet(lua_State* L) + { + Proxy* pxyGraphic = nullptr; + if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) + pxyGraphic = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); + else if(luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + pxyGraphic = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + if (pxyGraphic != nullptr) + { + Proxy* pxySSheet = luax_newinstance(L, JIN_GRAPHICS_SPRITESHEET); + Graphic* graphic = pxyGraphic->getObject<Graphic>(); + Shared<SpriteSheet>& shrSSheet = pxySSheet->getShared<SpriteSheet>(); + Shared<Graphic>& shrGraphic = pxyGraphic->getShared<Graphic>(); + shrSSheet.setDependency(SpriteSheetDependency::DEP_GRAPHIC, &shrGraphic); + pxySSheet->bind(new Shared<SpriteSheet>(new SpriteSheet(graphic), JIN_GRAPHICS_SPRITESHEET)); + return 1; + } + else + return 0; + } + /* newTextureFont(bitmap, text, color | cellw, cellh) */ LUA_IMPLEMENT int l_newTextureFont(lua_State* L) { @@ -775,6 +797,7 @@ namespace JinEngine { "newText", l_newText }, { "newTextureFont", l_newTextureFont }, { "newSprite", l_newSprite }, + { "newSpriteSheet", l_newSpriteSheet }, /* render */ { "setClearColor", l_setClearColor }, { "clear", l_clear }, diff --git a/src/lua/modules/graphics/je_lua_sprite.cpp b/src/lua/modules/graphics/je_lua_sprite.cpp index b81b66b..af1ac60 100644 --- a/src/lua/modules/graphics/je_lua_sprite.cpp +++ b/src/lua/modules/graphics/je_lua_sprite.cpp @@ -98,7 +98,7 @@ namespace JinEngine Proxy* proxy = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_SHADER); Shader* shader = proxy->getObject<Shader>(); sprite->setShader(shader); - sprite.setDependency(DEP_SHADER, &proxy->getShared<Shader>()); + sprite.setDependency(SpriteDependency::DEP_SHADER, &proxy->getShared<Shader>()); return 0; } @@ -114,7 +114,7 @@ namespace JinEngine if (p != nullptr) { sprite->setGraphic(p->getObject<Graphic>()); - sprite.setDependency(DEP_GRAPHIC, &p->getShared<Graphic>()); + sprite.setDependency(SpriteDependency::DEP_GRAPHIC, &p->getShared<Graphic>()); } return 0; } diff --git a/src/lua/modules/graphics/je_lua_spritesheet.h b/src/lua/modules/graphics/je_lua_spritesheet.h new file mode 100644 index 0000000..b4e965d --- /dev/null +++ b/src/lua/modules/graphics/je_lua_spritesheet.h @@ -0,0 +1,17 @@ +#ifndef __JE_LUA_SPRITE_SHEET_H__ +#define __JE_LUA_SPRITE_SHEET_H__ + +namespace JinEngine +{ + namespace Lua + { + + enum SpriteSheetDependency + { + DEP_GRAPHIC = 1 + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_texture_font.cpp b/src/lua/modules/graphics/je_lua_texture_font.cpp index dc48ce4..0bd36e6 100644 --- a/src/lua/modules/graphics/je_lua_texture_font.cpp +++ b/src/lua/modules/graphics/je_lua_texture_font.cpp @@ -43,7 +43,7 @@ namespace JinEngine } Proxy* pxyPage = luax_newinstance(L, JIN_GRAPHICS_PAGE); Shared<Page>* shrPage = new Shared<Page>(page, JIN_GRAPHICS_PAGE); - shrPage->setDependency(DEP_TEXTURE_FONT, &shrTexFont); + shrPage->setDependency(PageDependency::DEP_TEXTURE_FONT, &shrTexFont); pxyPage->bind(shrPage); return 1; } diff --git a/src/lua/modules/graphics/je_lua_ttf.cpp b/src/lua/modules/graphics/je_lua_ttf.cpp index 6cade75..319fdd6 100644 --- a/src/lua/modules/graphics/je_lua_ttf.cpp +++ b/src/lua/modules/graphics/je_lua_ttf.cpp @@ -43,7 +43,7 @@ namespace JinEngine } Proxy* pxyPage = luax_newinstance(L, JIN_GRAPHICS_PAGE); Shared<Page>* refPage = new Shared<Page>(page, JIN_GRAPHICS_PAGE); - refPage->setDependency(DEP_TTF, &shrTTF); + refPage->setDependency(PageDependency::DEP_TTF, &shrTTF); pxyPage->bind(refPage); return 1; } diff --git a/src/lua/modules/types.h b/src/lua/modules/types.h index 326b5f2..4bd0943 100644 --- a/src/lua/modules/types.h +++ b/src/lua/modules/types.h @@ -12,6 +12,7 @@ #define JIN_GRAPHICS_PAGE "Page" #define JIN_GRAPHICS_BITMAP "Bitmap" #define JIN_GRAPHICS_SPRITE "Sprite" +#define JIN_GRAPHICS_SPRITESHEET "SpriteSheet" // audio module #define JIN_AUDIO_SOURCE "Source" |