From 84aea028f9955c9313fa14b62d39a3e8e80b84b7 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Nov 2018 08:12:17 +0800 Subject: =?UTF-8?q?*=E6=9B=B4=E6=96=B0sprite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/Jin.exe | Bin 2458624 -> 2458624 bytes bin/game/main.lua | 6 ++--- build/vc++/jin.vcxproj | 1 + build/vc++/jin.vcxproj.filters | 3 +++ src/libjin/Graphics/je_graphics.h | 1 + src/libjin/Graphics/je_sprite.cpp | 28 ++++++++++++++++++----- src/libjin/Graphics/je_sprite.h | 7 ++++++ src/libjin/Graphics/je_sprite_batch.h | 17 ++++++++++++++ src/libjin/Graphics/je_sprite_sheet.cpp | 22 ++++++++++++++++++ src/libjin/Graphics/je_sprite_sheet.h | 19 ++++----------- src/lua/common/je_lua_shared.hpp | 7 ++++++ src/lua/modules/graphics/je_lua_graphics.cpp | 23 +++++++++++++++++++ src/lua/modules/graphics/je_lua_sprite.cpp | 4 ++-- src/lua/modules/graphics/je_lua_spritesheet.h | 17 ++++++++++++++ src/lua/modules/graphics/je_lua_texture_font.cpp | 2 +- src/lua/modules/graphics/je_lua_ttf.cpp | 2 +- src/lua/modules/types.h | 1 + 17 files changed, 132 insertions(+), 28 deletions(-) create mode 100644 src/lua/modules/graphics/je_lua_spritesheet.h diff --git a/bin/Jin.exe b/bin/Jin.exe index 6f56539..2e5c505 100644 Binary files a/bin/Jin.exe and b/bin/Jin.exe differ diff --git a/bin/game/main.lua b/bin/game/main.lua index 2c9036f..5986073 100644 --- a/bin/game/main.lua +++ b/bin/game/main.lua @@ -46,12 +46,12 @@ function jin.core.onLoad() spr:setShader(shader_program2) spr:setGraphic(tex) tex = nil - spr:setScale(1, 1) - spr:setOrigin(jin.graphics.SpriteOrigin.MIDDLERIGHT) + spr:setScale(4, 4) + spr:setOrigin(jin.graphics.SpriteOrigin.MIDDLELEFT) music = jin.audio.newSource("forest.ogg") music:setVolume(0.5) music:setLoop(true) - music:play() + --music:play() jin.graphics.clear() jin.graphics.showWindow() timer = jin.time.newTimer() diff --git a/build/vc++/jin.vcxproj b/build/vc++/jin.vcxproj index 6b9449f..19d6585 100644 --- a/build/vc++/jin.vcxproj +++ b/build/vc++/jin.vcxproj @@ -199,6 +199,7 @@ + diff --git a/build/vc++/jin.vcxproj.filters b/build/vc++/jin.vcxproj.filters index 7b8a9a9..c510c64 100644 --- a/build/vc++/jin.vcxproj.filters +++ b/build/vc++/jin.vcxproj.filters @@ -257,6 +257,9 @@ source\modules\graphics + + source\modules\graphics + 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 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 getSize() { return Math::Vector2(mQuad.w, mQuad.h); } + const Math::Quad& getQuad() { return mQuad; } const Math::Vector2& getPosition() { return mPosition; } const Math::Vector2& getOrigin() { return mOrigin; } const Math::Vector2& getScale() { return mScale; } @@ -61,6 +64,10 @@ namespace JinEngine Math::Vector2 mPosition; Math::Vector2 mOrigin; Math::Vector2 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 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; 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(); + Shared& shrSSheet = pxySSheet->getShared(); + Shared& shrGraphic = pxyGraphic->getShared(); + shrSSheet.setDependency(SpriteSheetDependency::DEP_GRAPHIC, &shrGraphic); + pxySSheet->bind(new Shared(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(); sprite->setShader(shader); - sprite.setDependency(DEP_SHADER, &proxy->getShared()); + sprite.setDependency(SpriteDependency::DEP_SHADER, &proxy->getShared()); return 0; } @@ -114,7 +114,7 @@ namespace JinEngine if (p != nullptr) { sprite->setGraphic(p->getObject()); - sprite.setDependency(DEP_GRAPHIC, &p->getShared()); + sprite.setDependency(SpriteDependency::DEP_GRAPHIC, &p->getShared()); } 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* shrPage = new Shared(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* refPage = new Shared(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" -- cgit v1.1-26-g67d0