├── targetver.h ├── Sources ├── Material.cpp ├── Entity.cpp ├── Light.cpp ├── SceneObject.cpp ├── BasicSprite.cpp ├── TextureRegion.cpp ├── Scene.cpp ├── TiledTextureRegion.cpp ├── Shader.cpp ├── DefaultShaderBase.cpp ├── EngineBase.cpp ├── Drawable2D.cpp ├── TextureAtlas.cpp ├── RenderTexture.cpp ├── Log.cpp ├── Engine.cpp ├── Camera.cpp ├── Window.cpp ├── BitmapFont.cpp ├── Mesh.cpp ├── Sprite.cpp ├── TiledSprite.cpp ├── SpriteGroup.cpp ├── TiledSpriteGroup.cpp ├── Text.cpp └── Graphics.cpp ├── Includes ├── _DllExport.h ├── Entity.h ├── Material.h ├── Light.h ├── SceneObject.h ├── TextureRegion.h ├── RenderTexture.h ├── TuxisEngine.h ├── BasicSprite.h ├── Scene.h ├── DefaultShaderBase.h ├── EngineDescription.h ├── TiledTextureRegion.h ├── Shader.h ├── SpriteGroup.h ├── Sprite.h ├── Mesh.h ├── TiledSpriteGroup.h ├── TextureAtlas.h ├── TiledSprite.h ├── Log.h ├── BitmapFont.h ├── Window.h ├── Drawable2D.h ├── Engine.h ├── Camera.h ├── Text.h ├── Graphics.h ├── EngineBase.h └── Types.h ├── stdafx.cpp ├── stdafx.h └── README.rdoc /targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | -------------------------------------------------------------------------------- /Sources/Material.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Material.h" 3 | 4 | namespace Tuxis 5 | { 6 | 7 | } -------------------------------------------------------------------------------- /Sources/Entity.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Entity.h" 3 | 4 | 5 | Entity::Entity(void) 6 | { 7 | } 8 | 9 | 10 | Entity::~Entity(void) 11 | { 12 | } 13 | -------------------------------------------------------------------------------- /Includes/_DllExport.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define TUXIS_EXPORTING 4 | 5 | #ifdef TUXIS_EXPORTING 6 | #define TUXIS_DECLSPEC __declspec(dllexport) 7 | #else 8 | #define TUXIS_DECLSPEC __declspec(dllimport) 9 | #endif -------------------------------------------------------------------------------- /stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // TuxisEngine.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 11 | // Windows Header Files: 12 | #include 13 | 14 | 15 | 16 | // TODO: reference additional headers your program requires here 17 | -------------------------------------------------------------------------------- /Sources/Light.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Light.h" 3 | 4 | namespace Tuxis 5 | { 6 | Light::Light() 7 | { 8 | ZeroMemory(this, sizeof(Light)); 9 | 10 | ambientColor = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f); 11 | diffuseColor = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 12 | lightDirection = XMFLOAT3(2.0f, -2.0f, 0.0f); 13 | } 14 | 15 | Light::~Light() 16 | { 17 | 18 | } 19 | 20 | void Light::Draw() 21 | { 22 | 23 | } 24 | 25 | void Light::Update() 26 | { 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Includes/Entity.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Entity super class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | 14 | class Entity 15 | { 16 | protected: 17 | int ID; 18 | 19 | public: 20 | Entity(void); 21 | ~Entity(void); 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /Includes/Material.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Graphics material class 6 | - Uncompleted 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | namespace Tuxis 14 | { 15 | class TUXIS_DECLSPEC Material 16 | { 17 | public: 18 | 19 | private: 20 | 21 | }; 22 | } -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | ==Tuxis-Engine 2 | Tuxis is a simple, 2D oriented game engine, based on DirectX11 graphics. 3 | Developed in order to study and does not strive for commercial indicators. 4 | It is written in C++, and has bindings only for C++ language. 5 | 6 | === Requirements 7 | * Windows 7 or later 8 | * DirectX SDK 9 | * DirectX11 hardware support 10 | * C++0X Compiler 11 | * Use "AngelCode Bitmap Font Generator" for generate .fnt files. 12 | 13 | ==== Some features 14 | http://www.youtube.com/watch?v=qfixC3GN0M0 15 | 16 | === Authors 17 | Andrew Tulay (Tulyakov) - main developer. (mhyhre@gmail.com) 18 | -------------------------------------------------------------------------------- /Sources/SceneObject.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "SceneObject.h" 3 | 4 | namespace Tuxis 5 | { 6 | SceneObject::SceneObject() 7 | { 8 | mVisible = true; 9 | mIgnored = false; 10 | mChanged = true; 11 | } 12 | 13 | 14 | 15 | 16 | void SceneObject::SetVisible(bool value) 17 | { 18 | mVisible=value; 19 | } 20 | 21 | void SceneObject::SetIgnoreUpdate(bool value) 22 | { 23 | mIgnored=value; 24 | } 25 | 26 | void SceneObject::SetChanged(bool value) 27 | { 28 | mChanged=value; 29 | } 30 | 31 | 32 | bool SceneObject::IsVisible() 33 | { 34 | return mVisible; 35 | } 36 | 37 | bool SceneObject::IsIgnoreUpdate() 38 | { 39 | return mIgnored; 40 | } 41 | 42 | bool SceneObject::IsChanged() 43 | { 44 | return mChanged; 45 | } 46 | } -------------------------------------------------------------------------------- /Includes/Light.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Light object 6 | -Uncompleted 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #define _XM_NO_INTRINSICS_ 14 | #pragma warning(disable:4251) 15 | 16 | #include 17 | #include 18 | 19 | #include "SceneObject.h" 20 | 21 | namespace Tuxis 22 | { 23 | class TUXIS_DECLSPEC Light : public SceneObject 24 | { 25 | public: 26 | Light(); 27 | ~Light(); 28 | 29 | void Draw(); 30 | void Update(); 31 | 32 | XMFLOAT4 ambientColor; 33 | XMFLOAT4 diffuseColor; 34 | XMFLOAT3 lightDirection; 35 | float padding; 36 | }; 37 | } -------------------------------------------------------------------------------- /Includes/SceneObject.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Scene Object 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | namespace Tuxis 14 | { 15 | 16 | class TUXIS_DECLSPEC SceneObject 17 | { 18 | protected: 19 | bool mChanged; 20 | bool mVisible; 21 | bool mIgnored; 22 | 23 | public: 24 | SceneObject(); 25 | 26 | void SetVisible(bool value); 27 | void SetIgnoreUpdate(bool value); 28 | void SetChanged(bool value); 29 | 30 | bool IsChanged(); 31 | bool IsVisible(); 32 | bool IsIgnoreUpdate(); 33 | 34 | virtual void Draw()=0; 35 | virtual void Update()=0; 36 | }; 37 | 38 | } -------------------------------------------------------------------------------- /Includes/TextureRegion.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include "TextureAtlas.h" 13 | #include "EngineBase.h" 14 | #include "Types.h" 15 | 16 | namespace Tuxis 17 | { 18 | class TUXIS_DECLSPEC TextureRegion 19 | { 20 | public: 21 | TextureRegion(); 22 | TextureRegion(TextureAtlas *pTextureAtlas,float x1,float y1,float x2,float y2); 23 | 24 | void SetRegion(float x1,float y1,float x2,float y2); 25 | void SetTextureAtlas(TextureAtlas *pTextureAtlas); 26 | void SetFullAtlasRegion(TextureAtlas *pTextureAtlas); 27 | 28 | TextureAtlas *mTexture; 29 | floatRect Region; 30 | }; 31 | } -------------------------------------------------------------------------------- /Includes/RenderTexture.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include 13 | #include "Log.h" 14 | #include "Engine.h" 15 | 16 | namespace Tuxis 17 | { 18 | class TUXIS_DECLSPEC RenderTexture 19 | { 20 | public: 21 | RenderTexture(); 22 | 23 | bool Initialize(int, int); 24 | void Release(); 25 | 26 | ID3D11RenderTargetView** GetRenderTargetView(); 27 | ID3D11ShaderResourceView* GetShaderResourceView(); 28 | 29 | private: 30 | ID3D11Texture2D* m_renderTargetTexture; 31 | ID3D11RenderTargetView* m_renderTargetView; 32 | ID3D11ShaderResourceView* m_shaderResourceView; 33 | 34 | }; 35 | } 36 | -------------------------------------------------------------------------------- /Includes/TuxisEngine.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Tuxis-Engine. 2011-2012. 4 | =============================================================================== 5 | Main Header File 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | 11 | #include "Log.h" 12 | 13 | #include "EngineBase.h" 14 | #include "Engine.h" 15 | #include "EngineDescription.h" 16 | 17 | #include "Window.h" 18 | 19 | #include "Camera.h" 20 | #include "Scene.h" 21 | #include "Shader.h" 22 | 23 | // Textures 24 | #include "TextureAtlas.h" 25 | #include "RenderTexture.h" 26 | 27 | // Regions 28 | #include "TextureRegion.h" 29 | #include "TiledTextureRegion.h" 30 | 31 | // Fonts 32 | #include "BitmapFont.h" 33 | 34 | // 2D objects 35 | #include "Sprite.h" 36 | #include "SpriteGroup.h" 37 | #include "TiledSprite.h" 38 | #include "TiledSpriteGroup.h" 39 | #include "Text.h" -------------------------------------------------------------------------------- /Includes/BasicSprite.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #pragma warning(disable:4251) 13 | 14 | #include "EngineBase.h" 15 | #include "Types.h" 16 | 17 | namespace Tuxis 18 | { 19 | class TUXIS_DECLSPEC BasicSprite 20 | { 21 | public: 22 | 23 | BasicSprite(); 24 | 25 | void SetRotation(float pRotation); 26 | void SetPosition(float px, float py); 27 | void SetFrame(int pFrame); 28 | void SetVisible(bool pVisible); 29 | void SetScale(float pScale); 30 | void SetColor(XMFLOAT4 pColor); 31 | 32 | 33 | bool Visible; 34 | int Frame; 35 | XMFLOAT3 Position; 36 | float Scale; 37 | float Rotation; 38 | XMFLOAT4 Color; 39 | }; 40 | } -------------------------------------------------------------------------------- /Includes/Scene.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Scene class 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include "SceneObject.h" 13 | #include "Log.h" 14 | 15 | #include 16 | using namespace std; 17 | 18 | namespace Tuxis 19 | { 20 | 21 | class TUXIS_DECLSPEC Scene: public SceneObject 22 | { 23 | public: 24 | Scene(); 25 | ~Scene(); 26 | 27 | void AttachChild(SceneObject *obj); 28 | void DetachChild(SceneObject *obj); 29 | void DetachAllChilds(); 30 | 31 | void On(); 32 | void Off(); 33 | 34 | virtual void Draw(); 35 | virtual void Update(); 36 | 37 | protected: 38 | 39 | vector ChildObjects; 40 | 41 | bool mVisible; 42 | bool mIgnored; 43 | }; 44 | 45 | } -------------------------------------------------------------------------------- /Sources/BasicSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "BasicSprite.h" 3 | 4 | namespace Tuxis 5 | { 6 | BasicSprite::BasicSprite() 7 | { 8 | Visible=true; 9 | Frame=0; 10 | 11 | Position.x=0.0f; 12 | Position.y=0.0f; 13 | Position.z=0.0f; 14 | 15 | Color.x=1.0f; 16 | Color.y=1.0f; 17 | Color.z=1.0f; 18 | Color.w=1.0f; 19 | 20 | Scale=1.0f; 21 | 22 | Rotation=0.0f; 23 | } 24 | 25 | void BasicSprite::SetPosition(float px, float py) 26 | { 27 | Position.x=px; 28 | Position.y=py; 29 | Position.z=0.0f; 30 | } 31 | 32 | void BasicSprite::SetRotation(float pRotation) 33 | { 34 | Rotation=pRotation; 35 | } 36 | 37 | void BasicSprite::SetScale(float pScale) 38 | { 39 | Scale=pScale; 40 | 41 | } 42 | 43 | void BasicSprite::SetFrame(int pFrame) 44 | { 45 | Frame=pFrame; 46 | } 47 | 48 | void BasicSprite::SetVisible(bool pVisible) 49 | { 50 | Visible=pVisible; 51 | } 52 | 53 | void BasicSprite::SetColor(XMFLOAT4 pColor) 54 | { 55 | Color=pColor; 56 | } 57 | } -------------------------------------------------------------------------------- /Includes/DefaultShaderBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Default shader base. (Mini-Manager) 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include "EngineBase.h" 13 | #include "Log.h" 14 | #include "Shader.h" 15 | 16 | 17 | namespace Tuxis 18 | { 19 | class TUXIS_DECLSPEC DefaultShaderBase 20 | { 21 | private: 22 | static DefaultShaderBase* mInstance; 23 | static bool Loaded; 24 | 25 | public: 26 | DefaultShaderBase(); 27 | ~DefaultShaderBase(); 28 | 29 | void LoadShaders(); 30 | void ReleaseShaders(); 31 | 32 | static DefaultShaderBase* Instance(); 33 | 34 | // Shaders 35 | Shader *mShaderSprite; 36 | Shader *mShaderSpriteGroup; 37 | Shader *mShaderText; 38 | Shader *mShaderTiledSprite; 39 | Shader *mShaderTiledSpriteGroup; 40 | }; 41 | } -------------------------------------------------------------------------------- /Includes/EngineDescription.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Engine Start Description 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include "Camera.h" 14 | #include "Window.h" 15 | #include "Scene.h" 16 | 17 | 18 | namespace Tuxis 19 | { 20 | 21 | class EngineDescription 22 | { 23 | 24 | public: 25 | 26 | bool _Windowed; 27 | bool _VSync; 28 | 29 | Camera *_Camera2D; 30 | Camera *_Camera3D; 31 | Window *_Window; 32 | 33 | 34 | 35 | EngineDescription() 36 | { 37 | _VSync = false; 38 | _Windowed = false; 39 | _Window = 0; 40 | _Camera2D = 0; 41 | _Camera3D = 0; 42 | } 43 | 44 | bool Test() 45 | { 46 | // Must have 1-2 start camera 47 | if( !(_Camera2D||_Camera3D)) 48 | return false; 49 | if(!_Window) return false; 50 | return true; 51 | } 52 | }; 53 | 54 | } -------------------------------------------------------------------------------- /Sources/TextureRegion.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "TextureRegion.h" 3 | 4 | namespace Tuxis 5 | { 6 | TextureRegion::TextureRegion() 7 | { 8 | mTexture=0; 9 | } 10 | 11 | TextureRegion::TextureRegion(TextureAtlas *pTextureAtlas,float x1,float y1,float x2,float y2) 12 | { 13 | mTexture=pTextureAtlas; 14 | Region(x1,y1,x2,y2); 15 | 16 | if(!pTextureAtlas) 17 | Log::Warning("TxTextureRegion::TxTextureRegion: Texture Atlas - null"); 18 | } 19 | 20 | 21 | void TextureRegion::SetRegion(float x1,float y1,float x2,float y2) 22 | { 23 | Region(x1,y1,x2,y2); 24 | } 25 | 26 | void TextureRegion::SetTextureAtlas(TextureAtlas *pTextureAtlas) 27 | { 28 | mTexture=pTextureAtlas; 29 | } 30 | 31 | void TextureRegion::SetFullAtlasRegion(TextureAtlas *pTextureAtlas) 32 | { 33 | if(pTextureAtlas) 34 | { 35 | mTexture=pTextureAtlas; 36 | Region.x1=0; 37 | Region.y1=0; 38 | Region.x2=(float)mTexture->GetWidth(); 39 | Region.y2=(float)mTexture->GetHeight(); 40 | } 41 | else 42 | Log::Warning("TxTextureRegion::SetFullAtlasRegion: Texture Atlas - null"); 43 | } 44 | } -------------------------------------------------------------------------------- /Includes/TiledTextureRegion.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #define _XM_NO_INTRINSICS_ 13 | 14 | #include "TextureAtlas.h" 15 | #include "EngineBase.h" 16 | #include "Log.h" 17 | #include "Types.h" 18 | #include "vector" 19 | 20 | namespace Tuxis 21 | { 22 | class TUXIS_DECLSPEC TiledTextureRegion 23 | { 24 | public: 25 | TiledTextureRegion(); 26 | ~TiledTextureRegion(); 27 | TiledTextureRegion(TextureAtlas *pTextureAtlas,float x1,float y1,float x2,float y2); 28 | 29 | void SetAutoTiling(TextureAtlas *pTextureAtlas,float TileWidth, float TileHeight); 30 | void SetTextureAtlas(TextureAtlas *pTextureAtlas,float x1,float y1,float x2,float y2); 31 | void AddRegion(float x1,float y1,float x2,float y2); 32 | 33 | int GetRegionCount(); 34 | 35 | void Release(); 36 | 37 | vector TileCoordinates; 38 | TextureAtlas *mTexture; 39 | 40 | }; 41 | } -------------------------------------------------------------------------------- /Includes/Shader.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Shader class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | using namespace std; 20 | 21 | #include "Log.h" 22 | #include "EngineBase.h" 23 | #include "Graphics.h" 24 | 25 | 26 | namespace Tuxis 27 | { 28 | class TUXIS_DECLSPEC Shader 29 | { 30 | public: 31 | 32 | ID3D10Blob *VS_Buffer; 33 | ID3D10Blob *PS_Buffer; 34 | ID3D11VertexShader *VertexShader; 35 | ID3D11PixelShader *PixelShader; 36 | ID3D11InputLayout *InputLayout; 37 | 38 | 39 | Shader(); 40 | ~Shader(); 41 | void Load(const WCHAR* FX_FileName); 42 | 43 | void CreateInputLayout(D3D11_INPUT_ELEMENT_DESC *pLayout,int pNumElements); 44 | void Release(); 45 | 46 | private: 47 | 48 | 49 | 50 | }; 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /Includes/SpriteGroup.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include "BasicSprite.h" 14 | #include "TextureRegion.h" 15 | #include "Drawable2D.h" 16 | #include "Log.h" 17 | #include "DefaultShaderBase.h" 18 | 19 | 20 | namespace Tuxis 21 | { 22 | class TUXIS_DECLSPEC SpriteGroup : public Drawable2D 23 | { 24 | public: 25 | SpriteGroup(); 26 | ~SpriteGroup(); 27 | 28 | void Create(int pSpriteCount,TextureRegion *pTextureRegion); 29 | void Clear(); 30 | BasicSprite* GetSprite(int i); 31 | void SetPosition2D( float tX, float tY ,bool centered); 32 | void Release(); 33 | 34 | void Draw(); 35 | void Update(); 36 | 37 | private: 38 | TextureRegion *mTextureRegion; 39 | 40 | floatRect AbsTexCoord; 41 | float2 HalfTexSize; 42 | 43 | Tuxis::Vertex::SpriteGroupVertex *vertices; 44 | BasicSprite *SpriteBase; 45 | 46 | 47 | int SpriteCount; 48 | }; 49 | } -------------------------------------------------------------------------------- /Includes/Sprite.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include "Drawable2D.h" 14 | #include "TextureAtlas.h" 15 | #include "EngineBase.h" 16 | #include "TextureRegion.h" 17 | #include "DefaultShaderBase.h" 18 | 19 | 20 | 21 | 22 | namespace Tuxis 23 | { 24 | 25 | class TUXIS_DECLSPEC Sprite : public Drawable2D 26 | { 27 | private: 28 | TextureAtlas *mTexture; 29 | Vertex::vtxSprite vertices[4]; 30 | 31 | floatRect AbsTexCoord; 32 | float2 HalfTexSize; 33 | CBSprite mCBValue; 34 | 35 | void Initialize(); 36 | 37 | public: 38 | 39 | Sprite(); 40 | 41 | 42 | void SetPosition2D( float tX, float tY ,bool centered); 43 | void SetTextureRegion(TextureRegion *pTextureRegion); 44 | void SetTextureAtlas(TextureAtlas *pTexture); 45 | void SetShader(Shader *pShader); 46 | 47 | void Draw(); 48 | void Update(); 49 | void Release(); 50 | }; 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Includes/Mesh.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include 13 | 14 | #include "TextureAtlas.h" 15 | #include "Drawable2D.h" 16 | #include "EngineBase.h" 17 | #include "TextureRegion.h" 18 | #include "Shader.h" 19 | #include "Light.h" 20 | 21 | 22 | namespace Tuxis 23 | { 24 | 25 | class TUXIS_DECLSPEC Mesh : public Drawable2D 26 | { 27 | struct CBStruct 28 | { 29 | XMMATRIX FinalMatrix; 30 | }; 31 | 32 | private: 33 | CBStruct mCBStruct; 34 | 35 | int VertexCount; 36 | Tuxis::Vertex::VertexTest *vertices; 37 | TextureAtlas *mTexture; 38 | 39 | public: 40 | Light *mLight; 41 | 42 | Mesh(); 43 | 44 | void SetTextureAuto(TextureAtlas *pTexture); 45 | bool LoadModel(const WCHAR* filename); 46 | void SetShader(Shader *pShader); 47 | 48 | void Update(); 49 | void Draw(); 50 | void Release(); 51 | 52 | 53 | }; 54 | } 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Includes/TiledSpriteGroup.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #define _XM_NO_INTRINSICS_ 14 | 15 | #include "BasicSprite.h" 16 | #include "TiledTextureRegion.h" 17 | #include "Drawable2D.h" 18 | #include "Log.h" 19 | #include "DefaultShaderBase.h" 20 | 21 | namespace Tuxis 22 | { 23 | class TUXIS_DECLSPEC TiledSpriteGroup : public Drawable2D 24 | { 25 | public: 26 | TiledSpriteGroup(); 27 | 28 | void Create(int countSize,TiledTextureRegion *pTextureRegion); 29 | 30 | BasicSprite* GetSprite(int i); 31 | void SetPosition2D( float PositionX, float PositionY ); 32 | void Release(); 33 | 34 | void Animate(int pTimeMillisecond); 35 | void Update(); 36 | void Draw(); 37 | 38 | private: 39 | int mFrameTime,mLastTime; 40 | TiledTextureRegion *mTiledTextureRegion; 41 | int CountOfSprites; 42 | 43 | floatRect AbsTextureCoordinate; 44 | Tuxis::Vertex::SpriteGroupVertex *vertices; 45 | float2 HalfTexSize; 46 | BasicSprite *SpriteBase; 47 | 48 | 49 | }; 50 | } -------------------------------------------------------------------------------- /Includes/TextureAtlas.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Texture Atlas class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include 14 | #include "Log.h" 15 | #include "Engine.h" 16 | 17 | 18 | 19 | namespace Tuxis 20 | { 21 | 22 | class TUXIS_DECLSPEC TextureAtlas 23 | { 24 | public: 25 | 26 | TextureAtlas(); 27 | ~TextureAtlas(); 28 | 29 | void Release(); 30 | 31 | int GetWidth(); 32 | int GetHeight(); 33 | 34 | ID3D11SamplerState** GetSamplerState(); 35 | ID3D11ShaderResourceView** GetShaderResourceView(); 36 | 37 | void SetShaderResourceView(ID3D11ShaderResourceView* pRes); 38 | 39 | void SetWidth(int pWidth); 40 | void SetHeight(int pHeight); 41 | 42 | 43 | 44 | bool LoadFromFile(const WCHAR* ImageName); 45 | void CreateSampler(); 46 | 47 | 48 | private: 49 | void LoadInfo(const WCHAR* ImageName); 50 | 51 | ID3D11ShaderResourceView *Resource; 52 | ID3D11SamplerState* SamplerState; 53 | D3DX11_IMAGE_LOAD_INFO ImageInfo; 54 | HRESULT hr; 55 | }; 56 | 57 | } -------------------------------------------------------------------------------- /Includes/TiledSprite.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include "Drawable2D.h" 13 | #include "TextureAtlas.h" 14 | #include "EngineBase.h" 15 | #include "TiledTextureRegion.h" 16 | #include "Engine.h" 17 | #include "DefaultShaderBase.h" 18 | 19 | 20 | 21 | namespace Tuxis 22 | { 23 | 24 | class TUXIS_DECLSPEC TiledSprite : public Drawable2D 25 | { 26 | private: 27 | TiledTextureRegion *mTiledTextureRegion; 28 | Vertex::vtxSprite vertices[4]; 29 | CBSprite mCBValue; 30 | 31 | int mFrameTime,mLastTime; 32 | int mFrame; 33 | floatRect AbsTextureCoordinate; 34 | float2 HalfTexSize; 35 | 36 | 37 | public: 38 | 39 | TiledSprite(); 40 | 41 | void Animate(int pTimeMillisecond); 42 | int GetFrameCount(); 43 | 44 | void SetFrame(int frame); 45 | void SetNextFrame(); 46 | void SetPosition2D( float tX, float tY ,bool centered); 47 | void SetTiledTextureRegion(TiledTextureRegion *pTiledTextureRegion); 48 | void Draw(); 49 | void Update(); 50 | void Release(); 51 | 52 | 53 | }; 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Includes/Log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * =============================================================================== 3 | * Tuxis Project 4 | * Copyright (C) 2011-2012, Andrew Tulay 5 | * =============================================================================== 6 | * Log class 7 | * =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #pragma warning( disable : 4251) 14 | 15 | 16 | #include "fstream" 17 | #include "string" 18 | #include 19 | using namespace std; 20 | 21 | namespace Tuxis 22 | { 23 | 24 | class TUXIS_DECLSPEC Log 25 | { 26 | private: 27 | static bool ConsoleLoging; 28 | static Log* Instance; 29 | char FileName[32]; 30 | FILE* mFile; 31 | 32 | void Init(); 33 | void Release(); 34 | void WriteAll( const char* Result); 35 | public: 36 | 37 | // Methods 38 | Log(); 39 | ~Log(); 40 | 41 | void EnableConsoleLoging(bool IsIt); 42 | 43 | void Separator(); 44 | 45 | static void Info( const char* Message ); 46 | static void Error( const char* Message ); 47 | static void Warning( const char* Message ); 48 | static void Success( const char* Message ); 49 | static void WriteText( const char* Message ); 50 | 51 | bool operator ! (); 52 | Log& operator << (int i); 53 | Log& operator << (float i); 54 | Log& operator << ( const char* Message); 55 | }; 56 | 57 | } -------------------------------------------------------------------------------- /Includes/BitmapFont.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | #include "EngineBase.h" 17 | #include "TextureAtlas.h" 18 | 19 | 20 | namespace Tuxis 21 | { 22 | 23 | struct CharDescriptor 24 | { 25 | short int x, y; 26 | short int Width, Height; 27 | short int XOffset, YOffset; 28 | short int XAdvance; 29 | short int Page; 30 | 31 | CharDescriptor() : x(), y(0), Width(0), Height(0), XOffset(0), YOffset(0), XAdvance(0), Page(0) {} 32 | }; 33 | 34 | struct sCharset 35 | { 36 | short int LineHeight; 37 | short int Base; 38 | short int Width, Height; 39 | short int Pages; 40 | CharDescriptor Chars[2048]; 41 | 42 | sCharset() : LineHeight(0), Base(0), Width(0), Height(0), Pages(0) {} 43 | }; 44 | 45 | 46 | class TUXIS_DECLSPEC BitmapFont 47 | { 48 | public: 49 | 50 | void Load( const WCHAR* FontName ); 51 | 52 | TextureAtlas* GetTextureAtlas(); 53 | sCharset* GetCharset(); 54 | void Release(); 55 | TextureAtlas mTexture; 56 | sCharset Charset; 57 | 58 | private: 59 | wstring TextureName; 60 | void LoadCharMap( const WCHAR* FileName ); 61 | 62 | }; 63 | } -------------------------------------------------------------------------------- /Includes/Window.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Window Module 6 | Required: dinput.h , dinput8.lib, dxguid.lib 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #define WIN32_LEAN_AND_MEAN 14 | #include 15 | #include "Log.h" 16 | 17 | #include "string" 18 | using namespace std; 19 | 20 | 21 | namespace Tuxis 22 | { 23 | class Window; 24 | 25 | typedef LRESULT (CALLBACK Window::*WNDPROC)(HWND, UINT, WPARAM, LPARAM); 26 | //typedef void (Window::*FunctionPointer)()const; 27 | 28 | 29 | class TUXIS_DECLSPEC Window 30 | { 31 | private: 32 | 33 | bool Created; 34 | 35 | int mWidth, mHeight; 36 | HWND mWindowHandler; 37 | 38 | wstring mCaption; 39 | wstring mClassName; 40 | 41 | HINSTANCE hInstance; 42 | 43 | public: 44 | 45 | Window(); 46 | ~Window(); 47 | 48 | void Create(int pWidth, int pHeight, const WCHAR* pCaption, bool EnableButtonBox); 49 | void SetWindow(HWND pHWND,int pWidth, int pHeight); 50 | 51 | bool IsRunning(); 52 | 53 | void Release(); 54 | 55 | HWND GetHWND(); 56 | int GetWidth(); 57 | int GetHeight(); 58 | }; 59 | 60 | 61 | 62 | TUXIS_DECLSPEC LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ); 63 | 64 | } 65 | 66 | 67 | -------------------------------------------------------------------------------- /Includes/Drawable2D.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Drawable superclass 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | #define _XM_NO_INTRINSICS_ 13 | 14 | #include "Engine.h" 15 | #include "EngineBase.h" 16 | #include "Shader.h" 17 | #include "SceneObject.h" 18 | 19 | 20 | namespace Tuxis 21 | { 22 | class TUXIS_DECLSPEC Drawable2D : public SceneObject 23 | { 24 | protected: 25 | 26 | // Shader 27 | Shader *mShader; 28 | ID3D11Buffer *ShaderConstantBuffer; 29 | 30 | // Vertex Buffer 31 | ID3D11Buffer *VertexBuffer; 32 | UINT VB_Stride; 33 | UINT VB_Offset; 34 | 35 | // Matrix 36 | float2 RotationCenter; 37 | XMMATRIX RotationMatrix; 38 | XMMATRIX ScaleMatrix; 39 | XMMATRIX TranslationMatrix; 40 | 41 | XMFLOAT4 ColorModulation; 42 | 43 | // Methods 44 | void InitDrawable(); 45 | void ReleaseDrawable(); 46 | 47 | 48 | public: 49 | void CreateConstantBuffer(int pBufferSize,D3D11_USAGE pD3D11_USAGE); 50 | void UpdateConstantBufferData(const void* Data); 51 | 52 | void SetColorModulation( float r, float g, float b ); 53 | void SetAlpha( float a ); 54 | 55 | void SetRotation(float Pitch,float Yaw,float Roll); 56 | void SetPosition(float x,float y,float z); 57 | void SetScale(float x,float y, float z); 58 | 59 | XMMATRIX CalculateWorldMatrix(); 60 | 61 | }; 62 | } -------------------------------------------------------------------------------- /Sources/Scene.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Scene.h" 3 | 4 | namespace Tuxis 5 | { 6 | Scene::Scene() 7 | { 8 | On(); 9 | } 10 | 11 | Scene::~Scene() 12 | { 13 | 14 | } 15 | 16 | 17 | void Scene::On() 18 | { 19 | mVisible=true; 20 | mIgnored=false; 21 | } 22 | 23 | void Scene::Off() 24 | { 25 | mVisible=false; 26 | mIgnored=true; 27 | } 28 | 29 | void Scene::AttachChild(SceneObject *obj) 30 | { 31 | if(!obj) 32 | Log::Error("Scene::AttachChild: obj - null"); 33 | ChildObjects.push_back(obj); 34 | } 35 | 36 | void Scene::DetachChild( SceneObject *obj ) 37 | { 38 | if(!obj) 39 | Log::Error("Scene::AttachChild: obj - null"); 40 | 41 | for(int i=0;iIsVisible()) 69 | { 70 | pObject->Draw(); 71 | } 72 | } 73 | } 74 | } 75 | 76 | void Scene::Update() 77 | { 78 | if(!mIgnored) 79 | { 80 | int NumberOfObjects=ChildObjects.size(); 81 | SceneObject *pObject; 82 | 83 | for(int i=0; iIsIgnoreUpdate()) 88 | { 89 | ChildObjects.at(i)->Update(); 90 | } 91 | } 92 | } 93 | } 94 | 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /Includes/Engine.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Engine class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #define _XM_NO_INTRINSICS_ 14 | 15 | #include "VertexBufferManager.h" 16 | 17 | #include "Camera.h" 18 | #include "EngineDescription.h" 19 | #include "Graphics.h" 20 | #include "DefaultShaderBase.h" 21 | #include "Window.h" 22 | #include "Log.h" 23 | 24 | 25 | namespace Tuxis 26 | { 27 | 28 | class TUXIS_DECLSPEC Engine 29 | { 30 | private: 31 | Window *mWindow; 32 | Camera *mCamera2D; 33 | Camera *mCamera3D; 34 | DefaultShaderBase mDefaultShaderBase; 35 | 36 | // Graphics 37 | Graphics *mGraphics; 38 | ID3D11Device *Device; 39 | ID3D11DeviceContext *Context; 40 | 41 | // Main scene 42 | Scene *mRootScene; 43 | 44 | static Engine* Me; 45 | 46 | 47 | public: 48 | 49 | static Engine* GetInstance(); 50 | 51 | Engine(); 52 | ~Engine(); 53 | 54 | void Initialize(EngineDescription pEngineOptions); 55 | void Release(); 56 | 57 | void SetRootScene(Scene* pScene); 58 | 59 | 60 | //static Graphics* GetGraphics(); 61 | static Camera* GetActive2DCamera(); 62 | static Camera* GetActive3DCamera(); 63 | static ID3D11Device* GetDevice(); 64 | static ID3D11DeviceContext* GetContext(); 65 | 66 | Window* GetWindow(); 67 | 68 | void SetActiveCamera2D(Camera *inCamera); 69 | void SetActiveCamera3D(Camera *inCamera); 70 | 71 | void SetScene(Scene *pScene); 72 | 73 | void Update(); 74 | void Draw(); 75 | }; 76 | 77 | 78 | } -------------------------------------------------------------------------------- /Includes/Camera.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Camera class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include "Log.h" 14 | 15 | #define _XM_NO_INTRINSICS_ 16 | #include 17 | #pragma warning(disable:4251) 18 | #include 19 | 20 | 21 | namespace Tuxis 22 | { 23 | 24 | class TUXIS_DECLSPEC Camera 25 | { 26 | private: 27 | float mMoveLeftRight, mMoveBackForward, mMoveTopDown; 28 | 29 | XMMATRIX mViewMatrix; 30 | XMMATRIX mProjectionMatrix; 31 | XMMATRIX mRotationMatrix; 32 | 33 | XMVECTOR mPosition; 34 | XMVECTOR mTarget; 35 | XMVECTOR mUp; 36 | XMVECTOR mRight; 37 | XMVECTOR mForward; 38 | 39 | XMVECTOR DefaultRight; 40 | XMVECTOR DefaultUp; 41 | XMVECTOR DefaultForward; 42 | 43 | float Roll, Pitch, Yaw; 44 | 45 | 46 | public: 47 | 48 | Camera(); 49 | 50 | void SetOrthoProjection(float X1, float X2, float Y1, float Y2, float ZNear, float ZFar); 51 | void SetPerspectiveProjection(float FovDEG, float pAspertRatio, float ZNear, float ZFar); 52 | 53 | void Turn(float pRoll, float pPitch, float pYaw); 54 | void Move(float pX, float pY, float pZ); 55 | 56 | void SetPosition(float pX, float pY, float pZ); 57 | void SetRotation(float pRoll, float pPitch, float pYaw); 58 | 59 | void UpdateViewMatrix(); 60 | void Update(); 61 | 62 | float GetRoll(); 63 | float GetPitch(); 64 | float GetYaw(); 65 | 66 | XMFLOAT3 GetPosition(); 67 | XMFLOAT3 GetRotation(); 68 | 69 | XMMATRIX GetFinalMatrix(); 70 | XMMATRIX GetViewMatrix(); 71 | XMMATRIX GetProjectionMatrix(); 72 | }; 73 | 74 | } -------------------------------------------------------------------------------- /Includes/Text.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Text drawable container 6 | =============================================================================== 7 | */ 8 | 9 | #pragma once 10 | #include "_DllExport.h" 11 | 12 | 13 | #define _XM_NO_INTRINSICS_ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | using namespace std; 21 | 22 | #include "TextureAtlas.h" 23 | #include "Drawable2D.h" 24 | #include "EngineBase.h" 25 | #include "SceneObject.h" 26 | #include "BitmapFont.h" 27 | #include "DefaultShaderBase.h" 28 | 29 | 30 | namespace Tuxis 31 | { 32 | #define TEXT_MAX_CHARS 512 33 | 34 | class TUXIS_DECLSPEC Text : public Drawable2D 35 | { 36 | public: 37 | 38 | Text(); 39 | ~Text(); 40 | 41 | 42 | void Release(); 43 | void Update(); 44 | void Draw(); 45 | void SetFont(BitmapFont* pFont); 46 | void SetRotate( float tR ); 47 | void SetText(const WCHAR* parText); 48 | void SetPosition(float xpos, float ypos); 49 | 50 | void SetHorizontalAlign(HorizontalAlign hAlign); 51 | void SetVerticalAlign(VerticalAlign vAlign); 52 | 53 | void SetMaxLineCount(int count); 54 | void SetLineSpacing(float space); 55 | 56 | private: 57 | void Initialize(); 58 | int MaxLineCount; 59 | BitmapFont *mFont; 60 | Tuxis::Vertex::SpriteVertex vertices[6*TEXT_MAX_CHARS]; 61 | XMFLOAT2 Position; 62 | 63 | HorizontalAlign hAlign; 64 | VerticalAlign vAlign; 65 | 66 | float mLineSpacing; 67 | 68 | std::vector SavedStrings; 69 | 70 | // Allignment offset's position 71 | float hAlignOffset; 72 | float vAlignOffset; 73 | 74 | void CalculateHorizontalAlignOffset(int LineNum); 75 | void CalculateVerticalAlignOffset(); 76 | void WriteStringToConstantBuffer(Vertex::SpriteVertex* pTempVertex,int prOffset,wstring prString,int LineNum); 77 | void DrawVertexBuffer(int prVertexCount); 78 | 79 | }; 80 | } -------------------------------------------------------------------------------- /Sources/TiledTextureRegion.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "TiledTextureRegion.h" 3 | 4 | namespace Tuxis 5 | { 6 | TiledTextureRegion::TiledTextureRegion() 7 | { 8 | mTexture=0; 9 | } 10 | 11 | TiledTextureRegion::~TiledTextureRegion() 12 | { 13 | Release(); 14 | } 15 | 16 | 17 | TiledTextureRegion::TiledTextureRegion(TextureAtlas *pTextureAtlas,float x1,float y1,float x2,float y2) 18 | { 19 | SetTextureAtlas(pTextureAtlas,x1,y1,x2,y2); 20 | } 21 | 22 | void TiledTextureRegion::SetTextureAtlas(TextureAtlas *pTextureAtlas,float x1,float y1,float x2,float y2) 23 | { 24 | if(mTexture) 25 | { 26 | if(mTexture==pTextureAtlas) 27 | { 28 | floatRect *temp=new floatRect( x1, y1, x2, y2 ); 29 | TileCoordinates.push_back(temp); 30 | } 31 | else 32 | Log::Warning("TiledTextureRegion::SetManualTiling: using different textures"); 33 | } 34 | else 35 | { 36 | mTexture=pTextureAtlas; 37 | } 38 | } 39 | 40 | 41 | int TiledTextureRegion::GetRegionCount() 42 | { 43 | return TileCoordinates.size(); 44 | } 45 | 46 | void TiledTextureRegion::AddRegion(float x1,float y1,float x2,float y2) 47 | { 48 | if(mTexture) 49 | { 50 | floatRect *temp=new floatRect( x1, y1, x2, y2 ); 51 | TileCoordinates.push_back(temp); 52 | } 53 | else 54 | { 55 | Log::Warning("TiledTextureRegion::AddTileCordinates: Set Atlas at first"); 56 | } 57 | } 58 | 59 | void TiledTextureRegion::Release() 60 | { 61 | for(int i=0; i<(int)TileCoordinates.size(); i++) 62 | delete TileCoordinates.at(i); 63 | TileCoordinates.clear(); 64 | } 65 | 66 | 67 | void TiledTextureRegion::SetAutoTiling(TextureAtlas *pTextureAtlas,float TileWidth, float TileHeight) 68 | { 69 | mTexture=pTextureAtlas; 70 | 71 | int TileCountX = (int)(mTexture->GetWidth() / TileWidth); 72 | int TileCountY = (int)(mTexture->GetHeight() / TileHeight); 73 | 74 | for(int y=0; yGetDevice()->CreateVertexShader( VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VertexShader ); 36 | if( FAILED(hr) ) 37 | Log::Error("TxShader::Load CreateVertexShader"); 38 | 39 | hr = Graphics::Instance()->GetDevice()->CreatePixelShader( PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PixelShader ); 40 | if( FAILED(hr) ) 41 | Log::Error("TxShader::Load CreatePixelShader"); 42 | } 43 | 44 | 45 | void Shader::CreateInputLayout(D3D11_INPUT_ELEMENT_DESC *pLayout,int pNumElements) 46 | { 47 | if(!VS_Buffer) 48 | Log::Error("TxShader::CreateInputLayout VS_Buffer - null"); 49 | 50 | hr = Graphics::Instance()->GetDevice()->CreateInputLayout( pLayout, pNumElements, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &InputLayout ); 51 | 52 | if(FAILED(hr)) 53 | Log::Error("TxShader::CreateInputLayout"); 54 | } 55 | 56 | 57 | void Shader::Release() 58 | { 59 | InputLayout->Release(); 60 | VertexShader->Release(); 61 | PixelShader->Release(); 62 | VS_Buffer->Release(); 63 | PS_Buffer->Release(); 64 | 65 | VS_Buffer = 0; 66 | PS_Buffer = 0; 67 | VertexShader = 0; 68 | PixelShader = 0; 69 | InputLayout = 0; 70 | } 71 | 72 | Shader::~Shader() 73 | { 74 | if(VertexShader||PixelShader) 75 | Release(); 76 | } 77 | 78 | } -------------------------------------------------------------------------------- /Sources/DefaultShaderBase.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "DefaultShaderBase.h" 3 | 4 | namespace Tuxis 5 | { 6 | DefaultShaderBase* DefaultShaderBase::mInstance = nullptr; 7 | bool DefaultShaderBase::Loaded = false; 8 | 9 | 10 | DefaultShaderBase::DefaultShaderBase() 11 | { 12 | if(mInstance == nullptr) 13 | { 14 | mInstance = this; 15 | } 16 | else 17 | Log::Warning("DefaultShaderBase::DefaultShaderBase already created."); 18 | 19 | mShaderSprite = nullptr; 20 | mShaderSpriteGroup = nullptr; 21 | mShaderText = nullptr; 22 | mShaderTiledSprite = nullptr; 23 | mShaderTiledSpriteGroup = nullptr; 24 | 25 | } 26 | 27 | void DefaultShaderBase::LoadShaders() 28 | { 29 | mShaderSprite = new Shader(); 30 | mShaderSpriteGroup = new Shader(); 31 | mShaderText = new Shader(); 32 | mShaderTiledSprite = new Shader(); 33 | mShaderTiledSpriteGroup = new Shader(); 34 | 35 | mShaderSprite ->Load(wstring(Path::DEFAULT_SHADERS+L"TxSprite.fx" ).c_str()); 36 | mShaderSpriteGroup ->Load(wstring(Path::DEFAULT_SHADERS+L"TxSpriteGroup.fx" ).c_str()); 37 | mShaderText ->Load(wstring(Path::DEFAULT_SHADERS+L"TxText.fx" ).c_str()); 38 | mShaderTiledSprite ->Load(wstring(Path::DEFAULT_SHADERS+L"TxTiledSprite.fx" ).c_str()); 39 | mShaderTiledSpriteGroup ->Load(wstring(Path::DEFAULT_SHADERS+L"TxTiledSpriteGroup.fx" ).c_str()); 40 | 41 | mShaderSprite ->CreateInputLayout(ShaderLayouts::SpriteLayout,2); 42 | mShaderSpriteGroup ->CreateInputLayout(ShaderLayouts::SpriteGroupLayout,5); 43 | mShaderText ->CreateInputLayout(ShaderLayouts::StandartLayout,3); 44 | mShaderTiledSprite ->CreateInputLayout(ShaderLayouts::SpriteLayout,2); 45 | mShaderTiledSpriteGroup ->CreateInputLayout(ShaderLayouts::TiledSpriteGroupLayout,5); 46 | 47 | Loaded = true; 48 | 49 | Log::Success("DefaultShaderBase::LoadShaders"); 50 | } 51 | 52 | void DefaultShaderBase::ReleaseShaders() 53 | { 54 | if(Loaded) 55 | { 56 | _RELEASE_DELETE(mShaderSprite) 57 | _RELEASE_DELETE(mShaderSpriteGroup) 58 | _RELEASE_DELETE(mShaderText) 59 | _RELEASE_DELETE(mShaderTiledSprite) 60 | _RELEASE_DELETE(mShaderTiledSpriteGroup) 61 | } 62 | Loaded = false; 63 | Log::Success("DefaultShaderBase::ReleaseShaders"); 64 | } 65 | 66 | DefaultShaderBase::~DefaultShaderBase() 67 | { 68 | if(mInstance) 69 | if(Loaded) 70 | { 71 | ReleaseShaders(); 72 | } 73 | mInstance = nullptr; 74 | } 75 | 76 | DefaultShaderBase* DefaultShaderBase::Instance() 77 | { 78 | if(mInstance == nullptr) 79 | { 80 | Log::Error("DefaultShaderBase::GetInstance: you must create object before."); 81 | return nullptr; 82 | } 83 | else 84 | return mInstance; 85 | } 86 | } -------------------------------------------------------------------------------- /Includes/Graphics.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Graphics class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "MMSystem.h" 18 | 19 | #include "Log.h" 20 | #include "Window.h" 21 | #include "EngineBase.h" 22 | #include "Types.h" 23 | 24 | 25 | namespace Tuxis 26 | { 27 | 28 | class TUXIS_DECLSPEC Graphics 29 | { 30 | public: 31 | 32 | Graphics(); 33 | ~Graphics(); 34 | 35 | ID3D11Device* GetDevice(); 36 | ID3D11DeviceContext* GetContext(); 37 | 38 | static Graphics* Instance(); 39 | static bool isReady(); 40 | 41 | Graphics( Window* pWindow, bool windowed,bool vsync); 42 | bool Initialize( Window* pWindow, bool windowed,bool vsync ); 43 | 44 | int GetWidth(); 45 | int GetHeight(); 46 | 47 | void SetRenderTarget(ID3D11RenderTargetView** pRenderTarget); 48 | void SetWireframeMode(bool pEnable); 49 | 50 | void EnableStencilBuffer(); 51 | void DisableStencilBuffer(); 52 | 53 | void SetClearColor(Color prColor); 54 | void Clear(); 55 | 56 | 57 | int GetFPS(); 58 | void Flip(); 59 | void CalculateFrameRate(); 60 | void Release(); 61 | 62 | void SetViewport( float pX, float pY, float pWidth, float pHeight ); 63 | 64 | ID3D11DepthStencilView* GetDepthStencilView(); 65 | 66 | private: 67 | static bool mReady; 68 | static Graphics* mInstance; 69 | Color mClearColor; 70 | 71 | 72 | void ZeroInit(); 73 | bool InitializeDirectX(); 74 | 75 | void CreateDeviceAndSwapChain(); 76 | void CreateBackBuffer(); 77 | void CreateWireframeState(); 78 | void CreateBlendState(); 79 | void CreateRasterState(); 80 | void CreateDepthStencilBuffer(); 81 | 82 | 83 | HWND WindowHandler; 84 | ID3D11Device *Device; 85 | ID3D11DeviceContext *Context; 86 | IDXGISwapChain *SwapChain; 87 | D3D_FEATURE_LEVEL FeatureLevel; 88 | 89 | ID3D11BlendState *BlendState; 90 | ID3D11RasterizerState *RasterState; 91 | ID3D11RasterizerState *WireFrameState; 92 | 93 | ID3D11RenderTargetView *BackBuffer; 94 | ID3D11RenderTargetView **CurrentRenderTarget; 95 | 96 | ID3D11DepthStencilView* DepthStencilView; 97 | ID3D11DepthStencilView* isDepthStencilView; 98 | ID3D11DepthStencilState* pDSState; 99 | 100 | long hr; 101 | //LPCTSTR Caption; 102 | int Width, Height, Depth; 103 | bool Windowed, VSync; 104 | int FPS; 105 | D3D11_VIEWPORT ViewPort; 106 | 107 | }; 108 | 109 | } -------------------------------------------------------------------------------- /Sources/EngineBase.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "EngineBase.h" 3 | 4 | namespace Tuxis 5 | { 6 | HRESULT hr; 7 | 8 | namespace Path 9 | { 10 | wstring DEFAULT_SHADERS=L"Data\\Shaders\\Default\\"; 11 | } 12 | 13 | const char* IntToString(int val) 14 | { 15 | static char buff[16]; 16 | sprintf(buff,"%d",val); 17 | return buff; 18 | } 19 | 20 | const char* FloatToString(float val) 21 | { 22 | static char buff[16]; 23 | sprintf(buff,"%f",val); 24 | return buff; 25 | } 26 | 27 | const wchar_t* IntToWString(int val) 28 | { 29 | static wchar_t buff[16]; 30 | swprintf(buff,16,L"%d",val); 31 | return buff; 32 | } 33 | 34 | const wchar_t* FloatToWString(float val) 35 | { 36 | static wchar_t buff[16]; 37 | swprintf(buff,16,L"%f",val); 38 | return buff; 39 | } 40 | 41 | namespace ShaderLayouts 42 | { 43 | D3D11_INPUT_ELEMENT_DESC SpriteLayout[] = 44 | { 45 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 46 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } 47 | }; 48 | 49 | D3D11_INPUT_ELEMENT_DESC StandartLayout[] = 50 | { 51 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 52 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 53 | { "RGBA", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 } 54 | }; 55 | 56 | 57 | D3D11_INPUT_ELEMENT_DESC SpriteGroupLayout[] = 58 | { 59 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 60 | { "ROTATION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 61 | { "SCALE", 0, DXGI_FORMAT_R32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 62 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 63 | { "RGBA", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0 } 64 | }; 65 | 66 | D3D11_INPUT_ELEMENT_DESC TiledSpriteGroupLayout[] = 67 | { 68 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 69 | { "ROTATION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 70 | { "SCALE", 0, DXGI_FORMAT_R32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 71 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 72 | { "RGBA", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0 } 73 | }; 74 | 75 | D3D11_INPUT_ELEMENT_DESC PrimitiveLayout[] = 76 | { 77 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 78 | { "RGBA", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } 79 | }; 80 | 81 | D3D11_INPUT_ELEMENT_DESC TestLayout[] = 82 | { 83 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 84 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } 85 | }; 86 | 87 | } 88 | } -------------------------------------------------------------------------------- /Sources/Drawable2D.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Drawable2D.h" 3 | 4 | namespace Tuxis 5 | { 6 | XMMATRIX Drawable2D::CalculateWorldMatrix() 7 | { 8 | return ScaleMatrix * RotationMatrix * TranslationMatrix; 9 | } 10 | 11 | void Drawable2D::SetScale(float sx,float sy, float sz=1.0f) 12 | { 13 | mChanged=true; 14 | ScaleMatrix = XMMatrixScaling( sx, sy, sz ); 15 | } 16 | 17 | void Drawable2D::SetRotation(float Pitch,float Yaw,float Roll) 18 | { 19 | mChanged=true; 20 | RotationMatrix = XMMatrixRotationRollPitchYaw(Pitch*DEGTORAD, Yaw*DEGTORAD, Roll*DEGTORAD); 21 | } 22 | 23 | 24 | void Drawable2D::SetPosition(float x,float y,float z=0.0f) 25 | { 26 | mChanged=true; 27 | TranslationMatrix = XMMatrixTranslation(x,y,z); 28 | } 29 | 30 | 31 | void Drawable2D::InitDrawable() 32 | { 33 | mChanged=true; 34 | VB_Stride=0; 35 | VB_Offset=0; 36 | mShader = nullptr; 37 | VertexBuffer = nullptr; 38 | ShaderConstantBuffer = nullptr; 39 | 40 | // Matrix Default Configuration 41 | ScaleMatrix = XMMatrixScaling( 1.0f, 1.0f, 1.0f ); 42 | TranslationMatrix = XMMatrixTranslation( 0.0f, 0.0f, 0.0f ); 43 | RotationMatrix = XMMatrixRotationRollPitchYaw(0.0f,0.0f,0.0f); 44 | 45 | ColorModulation = XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ); 46 | 47 | RotationCenter(0.0f,0.0f); 48 | } 49 | 50 | void Drawable2D::ReleaseDrawable() 51 | { 52 | _RELEASE(ShaderConstantBuffer); 53 | _RELEASE(VertexBuffer); 54 | mShader = nullptr; 55 | } 56 | 57 | void Drawable2D::SetColorModulation( float r, float g, float b ) 58 | { 59 | mChanged=true; 60 | ColorModulation.x = r; 61 | ColorModulation.y = g; 62 | ColorModulation.z = b; 63 | } 64 | 65 | void Drawable2D::SetAlpha( float a ) 66 | { 67 | mChanged=true; 68 | ColorModulation.w = a; 69 | } 70 | 71 | 72 | void Drawable2D::CreateConstantBuffer(int pBufferSize,D3D11_USAGE pD3D11_USAGE) 73 | { 74 | _RELEASE(ShaderConstantBuffer); 75 | 76 | //Create the buffer to send to the cbuffer in effect file 77 | D3D11_BUFFER_DESC ConstantBufferDescribes; 78 | ZeroMemory( &ConstantBufferDescribes, sizeof( D3D11_BUFFER_DESC ) ); 79 | ConstantBufferDescribes.ByteWidth = pBufferSize; 80 | ConstantBufferDescribes.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 81 | ConstantBufferDescribes.MiscFlags = 0; 82 | ConstantBufferDescribes.Usage = (D3D11_USAGE)pD3D11_USAGE; 83 | 84 | if(pD3D11_USAGE==D3D11_USAGE_DYNAMIC) 85 | ConstantBufferDescribes.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 86 | else 87 | ConstantBufferDescribes.CPUAccessFlags = 0; 88 | 89 | hr = Graphics::Instance()->GetDevice()->CreateBuffer( &ConstantBufferDescribes, NULL, &ShaderConstantBuffer ); 90 | if(FAILED(hr)) 91 | Log::Error("TxShader::CreateConstantBuffer CreateBuffer "); 92 | } 93 | 94 | 95 | void Drawable2D::UpdateConstantBufferData(const void* Data) 96 | { 97 | if(ShaderConstantBuffer == nullptr) 98 | Log::Warning("Drawable2D::UpdateConstantBufferData: Constant buffer not exist"); 99 | else 100 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, Data, 0, 0 ); 101 | } 102 | 103 | } -------------------------------------------------------------------------------- /Sources/TextureAtlas.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "TextureAtlas.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | 8 | TextureAtlas::TextureAtlas() 9 | { 10 | Resource=0; 11 | SamplerState=0; 12 | ZeroMemory(&ImageInfo, sizeof(D3DX11_IMAGE_LOAD_INFO)); 13 | } 14 | 15 | TextureAtlas::~TextureAtlas() 16 | { 17 | Resource=0; 18 | SamplerState=0; 19 | } 20 | 21 | 22 | bool TextureAtlas::LoadFromFile( const WCHAR* ImageName ) 23 | { 24 | Resource = 0; 25 | SamplerState = 0; 26 | 27 | CreateSampler(); 28 | LoadInfo( ImageName ); 29 | 30 | hr = D3DX11CreateShaderResourceViewFromFileW( Engine::GetDevice(), ImageName, &ImageInfo, NULL, &Resource, NULL ); 31 | if( FAILED( hr ) ) 32 | { 33 | Log::Error( "D3DX11CreateShaderResourceViewFromFile" ); 34 | } 35 | return true; 36 | } 37 | 38 | void TextureAtlas::LoadInfo( const WCHAR* ImageName ) 39 | { 40 | D3DX11_IMAGE_INFO TextureInfo; 41 | ZeroMemory(&TextureInfo, sizeof(D3DX11_IMAGE_INFO)); 42 | 43 | hr = D3DX11GetImageInfoFromFileW( ImageName, NULL, &TextureInfo, NULL ); 44 | if( FAILED( hr ) ) 45 | { 46 | Log::Error( "D3DX11GetImageInfoFromFile ImageName" ); 47 | } 48 | 49 | ImageInfo.Width = TextureInfo.Width; 50 | ImageInfo.Height = TextureInfo.Height; 51 | ImageInfo.Depth = TextureInfo.Depth; 52 | ImageInfo.FirstMipLevel = 0; 53 | ImageInfo.MipLevels = 1; 54 | ImageInfo.Usage = D3D11_USAGE_DEFAULT; 55 | ImageInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE; 56 | ImageInfo.CpuAccessFlags = 0; 57 | ImageInfo.MiscFlags = 0; 58 | ImageInfo.Format = TextureInfo.Format; 59 | ImageInfo.Filter = D3DX11_FILTER_LINEAR; 60 | ImageInfo.MipFilter = D3DX11_FILTER_LINEAR; 61 | } 62 | 63 | 64 | 65 | void TextureAtlas::CreateSampler() 66 | { 67 | if(SamplerState==0) 68 | { 69 | D3D11_SAMPLER_DESC sampDesc; 70 | ZeroMemory( &sampDesc, sizeof( sampDesc ) ); 71 | 72 | // Non filtering 73 | //sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; 74 | // Linear Filtering 75 | sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; 76 | 77 | sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; 78 | sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; 79 | sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; 80 | sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; 81 | sampDesc.MinLOD = 0; 82 | sampDesc.MaxLOD = D3D11_FLOAT32_MAX; 83 | 84 | hr = Engine::GetDevice()->CreateSamplerState( &sampDesc, &SamplerState ); 85 | if( FAILED( hr ) ) 86 | { 87 | Log::Error( "CreateSamplerState" ); 88 | } 89 | } 90 | } 91 | 92 | 93 | 94 | 95 | ID3D11ShaderResourceView** TextureAtlas::GetShaderResourceView() 96 | { 97 | return &Resource; 98 | } 99 | 100 | ID3D11SamplerState** TextureAtlas::GetSamplerState() 101 | { 102 | return &SamplerState; 103 | } 104 | 105 | 106 | void TextureAtlas::SetShaderResourceView(ID3D11ShaderResourceView* pRes) 107 | { 108 | Resource = pRes; 109 | } 110 | 111 | 112 | 113 | int TextureAtlas::GetWidth() 114 | { 115 | return ImageInfo.Width; 116 | } 117 | int TextureAtlas::GetHeight() 118 | { 119 | return ImageInfo.Height; 120 | } 121 | 122 | void TextureAtlas::SetWidth(int pWidth) 123 | { 124 | ImageInfo.Width=pWidth; 125 | } 126 | 127 | void TextureAtlas::SetHeight(int pHeight) 128 | { 129 | ImageInfo.Height=pHeight; 130 | } 131 | 132 | void TextureAtlas::Release() 133 | { 134 | _RELEASE(SamplerState); 135 | _RELEASE(Resource); 136 | 137 | ZeroMemory(&ImageInfo, sizeof(D3DX11_IMAGE_LOAD_INFO)); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /Sources/RenderTexture.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "RenderTexture.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | RenderTexture::RenderTexture() 8 | { 9 | m_renderTargetTexture = 0; 10 | m_renderTargetView = 0; 11 | m_shaderResourceView = 0; 12 | } 13 | 14 | 15 | bool RenderTexture::Initialize(int textureWidth, int textureHeight) 16 | { 17 | HRESULT result; 18 | D3D11_TEXTURE2D_DESC textureDesc; 19 | D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc; 20 | D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc; 21 | 22 | // Initialize the render target texture description. 23 | ZeroMemory(&textureDesc, sizeof(textureDesc)); 24 | ZeroMemory(&renderTargetViewDesc, sizeof(renderTargetViewDesc)); 25 | ZeroMemory(&shaderResourceViewDesc, sizeof(shaderResourceViewDesc)); 26 | 27 | // Setup the render target texture description. 28 | textureDesc.Width = textureWidth; 29 | textureDesc.Height = textureHeight; 30 | textureDesc.MipLevels = 1; 31 | textureDesc.ArraySize = 1; 32 | textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 33 | textureDesc.SampleDesc.Count = 1; 34 | textureDesc.Usage = D3D11_USAGE_DEFAULT; 35 | textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; 36 | textureDesc.CPUAccessFlags = 0; 37 | textureDesc.MiscFlags = 0; 38 | 39 | // Create the render target texture. 40 | result = Engine::GetDevice()->CreateTexture2D(&textureDesc, NULL, &m_renderTargetTexture); 41 | if(FAILED(result)) 42 | { 43 | Log::Error("RenderTextureClass::Initialize: CreateTexture2D"); 44 | } 45 | 46 | // Setup the description of the render target view. 47 | renderTargetViewDesc.Format = textureDesc.Format; 48 | renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; 49 | renderTargetViewDesc.Texture2D.MipSlice = 0; 50 | 51 | // Create the render target view. 52 | result = Engine::GetDevice()->CreateRenderTargetView(m_renderTargetTexture, &renderTargetViewDesc, &m_renderTargetView); 53 | if(FAILED(result)) 54 | { 55 | Log::Error("RenderTextureClass::Initialize: CreateRenderTargetView"); 56 | } 57 | 58 | // Setup the description of the shader resource view. 59 | shaderResourceViewDesc.Format = textureDesc.Format; 60 | shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; 61 | shaderResourceViewDesc.Texture2D.MostDetailedMip = 0; 62 | shaderResourceViewDesc.Texture2D.MipLevels = 1; 63 | 64 | // Create the shader resource view. 65 | result = Engine::GetDevice()->CreateShaderResourceView(m_renderTargetTexture, &shaderResourceViewDesc, &m_shaderResourceView); 66 | if(FAILED(result)) 67 | { 68 | Log::Error("RenderTextureClass::Initialize: CreateShaderResourceView"); 69 | } 70 | 71 | if(!m_shaderResourceView || !m_renderTargetView || !m_renderTargetTexture) 72 | { 73 | Log::Error("RenderTextureClass::Initialize: bad"); 74 | } 75 | 76 | return true; 77 | } 78 | 79 | 80 | void RenderTexture::Release() 81 | { 82 | if(m_shaderResourceView) 83 | { 84 | //m_shaderResourceView->Release(); 85 | m_shaderResourceView = 0; 86 | } 87 | 88 | 89 | if(m_renderTargetView) 90 | { 91 | m_renderTargetView->Release(); 92 | m_renderTargetView = 0; 93 | } 94 | 95 | 96 | if(m_renderTargetTexture) 97 | { 98 | m_renderTargetTexture->Release(); 99 | m_renderTargetTexture = 0; 100 | } 101 | } 102 | 103 | 104 | ID3D11RenderTargetView** RenderTexture::GetRenderTargetView() 105 | { 106 | return &m_renderTargetView; 107 | } 108 | 109 | ID3D11ShaderResourceView* RenderTexture::GetShaderResourceView() 110 | { 111 | return m_shaderResourceView; 112 | } 113 | } -------------------------------------------------------------------------------- /Sources/Log.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Log.h" 3 | #include "EngineBase.h" 4 | 5 | namespace Tuxis 6 | { 7 | Log* Log::Instance = NULL; 8 | bool Log::ConsoleLoging = true; 9 | 10 | Log::Log() 11 | { 12 | 13 | 14 | if(Instance==NULL) 15 | { 16 | strcpy(FileName,"Log.txt"); 17 | //FileName = "Log.txt"; 18 | Instance = this; 19 | Instance->Init(); 20 | } 21 | else 22 | { 23 | Warning("Log::Log: Log already exist"); 24 | } 25 | } 26 | 27 | Log::~Log() 28 | { 29 | Release(); 30 | } 31 | 32 | void Log::Release() 33 | { 34 | Separator(); 35 | Info( "Stop logging" ); 36 | Separator(); 37 | fclose(mFile); 38 | mFile = nullptr; 39 | Instance=NULL; 40 | } 41 | 42 | 43 | void Log::EnableConsoleLoging(bool IsIt) 44 | { 45 | ConsoleLoging = IsIt; 46 | } 47 | 48 | void Log::Init() 49 | { 50 | mFile = fopen(FileName,"w"); 51 | if(mFile != 0) 52 | { 53 | WriteText( "Tuxis Log System\n" ); 54 | #ifdef _DEBUG 55 | WriteText("Debug mode\n"); 56 | #else 57 | WriteText("Release mode\n"); 58 | #endif 59 | Separator(); 60 | Info("Start logging"); 61 | Separator(); 62 | } 63 | } 64 | 65 | 66 | void Log::Info( const char* pMessage ) 67 | { 68 | string Message = pMessage; 69 | if( Instance ) 70 | { 71 | Message.insert(0,"INFO: "); 72 | Message.append("\n"); 73 | Instance->WriteAll(Message.c_str()); 74 | } 75 | } 76 | 77 | 78 | void Log::WriteText( const char* Message ) 79 | { 80 | if( Instance ) 81 | { 82 | Instance->WriteAll(Message); 83 | } 84 | } 85 | 86 | 87 | void Log::Separator() 88 | { 89 | if( Instance ) 90 | { 91 | string Message="-------------------------------------------------------------------------------\n" ; 92 | if(mFile) 93 | fwrite(Message.c_str() , 1 , Message.size() , Instance->mFile ); 94 | if(ConsoleLoging) cout<WriteAll(Message.c_str()); 108 | Instance->Release(); 109 | } 110 | MessageBoxA(0,Message.c_str(),"Error",0); 111 | throw Message.c_str(); 112 | } 113 | 114 | 115 | void Log::Warning( const char* pMessage ) 116 | { 117 | string Message = pMessage; 118 | if( Instance ) 119 | { 120 | Message.insert(0,"WARNING: "); 121 | Message.append("\n"); 122 | Instance->WriteAll(Message.c_str()); 123 | } 124 | } 125 | 126 | 127 | void Log::Success( const char* pMessage ) 128 | { 129 | string Message = pMessage; 130 | if( Instance ) 131 | { 132 | Message.insert(0,"SUCCESS: "); 133 | Message.append("\n"); 134 | Instance->WriteAll(Message.c_str()); 135 | } 136 | } 137 | 138 | 139 | bool Log::operator ! () 140 | { 141 | return Instance==nullptr; 142 | } 143 | 144 | 145 | Log& Log::operator<<( int i ) 146 | { 147 | if(Instance) 148 | { 149 | string Message = IntToString(i); 150 | Instance->WriteAll(Message.c_str()); 151 | } 152 | return *this; 153 | } 154 | 155 | 156 | Log& Log::operator<<( float i ) 157 | { 158 | if(Instance) 159 | { 160 | string Message = FloatToString(i); 161 | Instance->WriteAll(Message.c_str()); 162 | } 163 | 164 | return *this; 165 | } 166 | 167 | 168 | Log& Log::operator<<( const char* Message ) 169 | { 170 | if(Instance) 171 | { 172 | Instance->WriteAll(Message); 173 | } 174 | return *this; 175 | } 176 | 177 | 178 | void Log::WriteAll( const char* ResultMessage ) 179 | { 180 | if(mFile) 181 | { 182 | fwrite(ResultMessage , 1 , strlen(ResultMessage) , mFile ); 183 | } 184 | if(ConsoleLoging) cout<Initialize( mWindow, pEngineOptions._Windowed, pEngineOptions._VSync); 61 | Context=mGraphics->GetContext(); 62 | Device=mGraphics->GetDevice(); 63 | 64 | mDefaultShaderBase.LoadShaders(); 65 | 66 | Log::Success("Engine::Initialized."); 67 | } 68 | 69 | Camera* Engine::GetActive2DCamera() 70 | { 71 | if(Me) 72 | { 73 | if(Me->mCamera2D) 74 | { 75 | return Me->mCamera2D; 76 | } 77 | else 78 | { 79 | Log::Error("Engine::GetActive2DCamera - Camera2D is null"); 80 | } 81 | } 82 | else 83 | Log::Error("Engine::GetActive2DCamera - engine is not created."); 84 | 85 | return NULL; 86 | } 87 | 88 | 89 | 90 | Camera* Engine::GetActive3DCamera() 91 | { 92 | if(Me) 93 | { 94 | if(Me->mCamera3D) 95 | { 96 | return Me->mCamera3D; 97 | } 98 | else 99 | { 100 | Log::Error("Engine::GetActive3DCamera - Camera3D is null"); 101 | } 102 | } 103 | else 104 | Log::Error("Engine::GetActive3DCamera - engine is not created."); 105 | 106 | return NULL; 107 | } 108 | 109 | ID3D11Device* Engine::GetDevice() 110 | { 111 | if(Me) 112 | { 113 | if(Me->Device) 114 | { 115 | return Me->Device; 116 | } 117 | else 118 | { 119 | Log::Error("Engine::GetDevice - Device is null"); 120 | } 121 | } 122 | else 123 | Log::Error("Engine::GetDevice - engine is not created."); 124 | 125 | return NULL; 126 | } 127 | 128 | 129 | 130 | ID3D11DeviceContext* Engine::GetContext() 131 | { 132 | if(Me) 133 | { 134 | if(Me->Context) 135 | { 136 | return Me->Context; 137 | } 138 | else 139 | { 140 | Log::Error("Engine::GetContext - Context is null"); 141 | } 142 | } 143 | else 144 | Log::Error("Engine::GetContext - engine is not created."); 145 | 146 | return NULL; 147 | } 148 | 149 | 150 | 151 | void Engine::SetActiveCamera2D(Camera *inCamera) 152 | { 153 | mCamera2D=inCamera; 154 | } 155 | 156 | 157 | void Engine::SetActiveCamera3D(Camera *inCamera) 158 | { 159 | mCamera3D=inCamera; 160 | } 161 | 162 | 163 | void Engine::Draw() 164 | { 165 | if(mCamera2D) 166 | mCamera2D->Update(); 167 | 168 | if(mCamera3D) 169 | mCamera3D->Update(); 170 | 171 | if(mRootScene) 172 | mRootScene->Draw(); 173 | } 174 | 175 | 176 | void Engine::Update() 177 | { 178 | if(mRootScene) 179 | mRootScene->Update(); 180 | } 181 | 182 | 183 | void Engine::Release() 184 | { 185 | 186 | mDefaultShaderBase.ReleaseShaders(); 187 | 188 | _RELEASE_DELETE(mGraphics); 189 | 190 | Context = nullptr; 191 | Device = nullptr; 192 | mWindow = nullptr; 193 | mCamera2D = nullptr; 194 | mCamera3D = nullptr; 195 | mRootScene = nullptr; 196 | 197 | Log::Success("Engine - Released"); 198 | } 199 | 200 | Engine::~Engine() 201 | { 202 | Me=nullptr; 203 | } 204 | 205 | void Engine::SetRootScene( Scene* pScene ) 206 | { 207 | mRootScene=pScene; 208 | } 209 | 210 | } -------------------------------------------------------------------------------- /Sources/Camera.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Camera.h" 3 | 4 | namespace Tuxis 5 | { 6 | 7 | #define DEGTORAD 0.0174532925199432957f 8 | 9 | Camera::Camera() 10 | { 11 | mRight = XMVectorSet( 1.0f, 0.0f, 0.0f, 0.0f ); 12 | mUp = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); 13 | mForward = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f ); 14 | mPosition = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f ); 15 | mTarget = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f ); 16 | 17 | mMoveLeftRight = 0.0f; 18 | mMoveBackForward = 0.0f; 19 | mMoveTopDown = 0.0f; 20 | 21 | Roll = 0.0f; 22 | Pitch = 0.0f; 23 | Yaw = 0.0f; 24 | 25 | DefaultRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f); 26 | DefaultUp = XMVectorSet(0.0f,1.0f,0.0f, 0.0f); 27 | DefaultForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f); 28 | 29 | UpdateViewMatrix(); 30 | } 31 | 32 | 33 | float Camera::GetRoll() 34 | { 35 | return Roll; 36 | } 37 | 38 | float Camera::GetPitch() 39 | { 40 | return Pitch; 41 | } 42 | 43 | float Camera::GetYaw() 44 | { 45 | return Yaw; 46 | } 47 | 48 | XMFLOAT3 Camera::GetPosition() 49 | { 50 | return XMFLOAT3( mPosition.x, mPosition.y, mPosition.z); 51 | } 52 | 53 | XMFLOAT3 Camera::GetRotation() 54 | { 55 | return XMFLOAT3( Roll, Pitch, Yaw); 56 | } 57 | 58 | 59 | void Camera::SetPosition(float pX, float pY, float pZ) 60 | { 61 | mPosition = XMVectorSet( pX, pY, pZ, 0.0f ); 62 | } 63 | 64 | void Camera::SetRotation(float pRoll, float pPitch, float pYaw) 65 | { 66 | Roll += pRoll; 67 | Pitch += pPitch; 68 | Yaw += pYaw; 69 | } 70 | 71 | void Camera::SetOrthoProjection(float X1, float X2, float Y1, float Y2, float ZNear, float ZFar) 72 | { 73 | if(X2>X1 && Y2>Y1 && ZFar>ZNear) 74 | mProjectionMatrix=XMMatrixOrthographicOffCenterLH(X1,X2,Y2,Y1,ZNear,ZFar); 75 | else 76 | Log::Error("Camera::SetOrthoProjection: check parametr's"); 77 | } 78 | 79 | void Camera::SetPerspectiveProjection(float FovDEG, float pAspertRatio, float ZNear, float ZFar) 80 | { 81 | if(ZNear==0.00f) 82 | { 83 | ZNear+=0.01f; 84 | Log::Warning("Camera::SetPerspectiveProjection: ZNear=0 is bad idea"); 85 | } 86 | 87 | if(FovDEG<1.0f) 88 | FovDEG=1.0f; 89 | 90 | if(FovDEG>179.0f) 91 | FovDEG=179.0f; 92 | 93 | mProjectionMatrix = XMMatrixPerspectiveFovLH( FovDEG*DEGTORAD, pAspertRatio, ZNear, ZFar ); 94 | } 95 | 96 | void Camera::UpdateViewMatrix() 97 | { 98 | mViewMatrix = XMMatrixLookAtLH( mPosition, mTarget, mUp ); 99 | } 100 | 101 | XMMATRIX Camera::GetFinalMatrix() 102 | { 103 | return mViewMatrix * mProjectionMatrix; 104 | } 105 | 106 | XMMATRIX Camera::GetProjectionMatrix() 107 | { 108 | return mProjectionMatrix; 109 | } 110 | 111 | XMMATRIX Camera::GetViewMatrix() 112 | { 113 | return mViewMatrix; 114 | } 115 | 116 | 117 | void Camera::Move(float pX, float pY, float pZ) 118 | { 119 | mMoveLeftRight += pX; 120 | mMoveTopDown += pY; 121 | mMoveBackForward += pZ; 122 | } 123 | 124 | void Camera::Turn(float pRoll, float pPitch, float pYaw) 125 | { 126 | Roll += pRoll; 127 | Pitch += pPitch; 128 | Yaw += pYaw; 129 | 130 | Roll = fmod(Roll, XM_2PI); 131 | 132 | if(Roll>XM_PI/2.0f) 133 | { 134 | Roll=XM_PI/2.0f; 135 | } 136 | 137 | if(Roll<-XM_PI/2.0f) 138 | { 139 | Roll=-XM_PI/2.0f; 140 | } 141 | } 142 | 143 | 144 | 145 | void Camera::Update() 146 | { 147 | mRotationMatrix = XMMatrixRotationRollPitchYaw(Roll, Pitch, Yaw); 148 | mTarget = XMVector3TransformCoord(DefaultForward, mRotationMatrix ); 149 | mTarget = XMVector3Normalize(mTarget); 150 | 151 | XMMATRIX RotateYTempMatrix; 152 | RotateYTempMatrix = XMMatrixRotationRollPitchYaw(Roll, Pitch, Yaw); 153 | 154 | mRight = XMVector3TransformCoord(DefaultRight, RotateYTempMatrix); 155 | mUp = XMVector3TransformCoord(DefaultUp, RotateYTempMatrix); 156 | mForward = XMVector3TransformCoord(DefaultForward, RotateYTempMatrix); 157 | 158 | mPosition += mMoveLeftRight*mRight; 159 | mPosition += mMoveTopDown*mUp; 160 | mPosition += mMoveBackForward*mForward; 161 | 162 | mMoveLeftRight = 0.0f; 163 | mMoveTopDown = 0.0f; 164 | mMoveBackForward = 0.0f; 165 | 166 | mTarget = mPosition + mTarget; 167 | 168 | UpdateViewMatrix(); 169 | } 170 | } -------------------------------------------------------------------------------- /Sources/Window.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Window.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | Window::Window() 8 | { 9 | Created=false; 10 | mClassName=L"TuxisWindowClass"; 11 | mWindowHandler = NULL; 12 | 13 | // Register window class 14 | hInstance = GetModuleHandle( NULL ); 15 | 16 | WNDCLASSEX wc; 17 | wc.cbSize = sizeof( WNDCLASSEX ); 18 | wc.style = CS_HREDRAW | CS_VREDRAW; 19 | wc.lpfnWndProc = WndProc; 20 | wc.cbClsExtra = 0; 21 | wc.cbWndExtra = 0; 22 | wc.hInstance = hInstance; 23 | wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); 24 | wc.hCursor = LoadCursor( NULL, IDC_ARROW ); 25 | wc.hbrBackground = ( HBRUSH )COLOR_GRAYTEXT; 26 | wc.lpszMenuName = NULL; 27 | wc.lpszClassName = mClassName.c_str(); 28 | wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); 29 | 30 | if( !RegisterClassEx( &wc ) ) 31 | { 32 | Log::Error( "Window::Window: RegisterClassEx(&wc)" ); 33 | } 34 | } 35 | 36 | Window::~Window() 37 | { 38 | Release(); 39 | // UnRegister window class 40 | if(!mClassName.empty() && !UnregisterClassW(mClassName.c_str(),hInstance)) 41 | { 42 | Log::Error( "Window::~Window: Could Not Unregister Class." ); 43 | } 44 | } 45 | 46 | void Window::Create(int pWidth,int pHeight, const WCHAR* pCaption, bool EnableButtonBox ) 47 | { 48 | if(Created) 49 | { 50 | Log::Error( "Window::Create: Window already created!" ); 51 | } 52 | 53 | mWindowHandler =NULL; 54 | mWidth =pWidth; 55 | mHeight =pHeight; 56 | mCaption =pCaption; 57 | 58 | 59 | DWORD WindowStyle; 60 | 61 | if(EnableButtonBox) 62 | WindowStyle = WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX; 63 | else 64 | WindowStyle = WS_BORDER; 65 | 66 | 67 | mWindowHandler = CreateWindowW( 68 | mClassName.c_str(), 69 | mCaption.c_str(), 70 | WindowStyle, 71 | CW_USEDEFAULT, CW_USEDEFAULT, 72 | mWidth + GetSystemMetrics( SM_CXFIXEDFRAME ) * 2, 73 | mHeight + GetSystemMetrics( SM_CYFIXEDFRAME ) * 2 + GetSystemMetrics( SM_CYCAPTION ), 74 | NULL,NULL, 75 | hInstance, NULL ); 76 | 77 | if( !mWindowHandler ) 78 | { 79 | Log::Error( "Window::Window: Window not created!" ); 80 | } 81 | 82 | ShowWindow( mWindowHandler, SW_SHOW ); 83 | SetForegroundWindow(mWindowHandler); 84 | SetFocus(mWindowHandler); 85 | UpdateWindow( mWindowHandler); 86 | 87 | Created=true; 88 | 89 | Log::Success("Window created."); 90 | } 91 | 92 | 93 | 94 | bool Window::IsRunning() 95 | { 96 | if(Created) 97 | { 98 | return IsWindow(mWindowHandler) ? true : false;; 99 | } 100 | return false; 101 | } 102 | 103 | 104 | 105 | void Window::SetWindow(HWND pHWND,int pWidth, int pHeight) 106 | { 107 | mWindowHandler =pHWND; 108 | mWidth =pWidth; 109 | mHeight =pHeight; 110 | mCaption =L""; 111 | 112 | Log::Success("Window: selected exist by HWND."); 113 | } 114 | 115 | 116 | void Window::Release() 117 | { 118 | if(IsWindow(mWindowHandler)) 119 | { 120 | if(!DestroyWindow(mWindowHandler)) 121 | { 122 | Log::Error( "Window::Release: Could Not Destroy Window." ); 123 | mWindowHandler=NULL; 124 | } 125 | 126 | Created=false; 127 | Log::Success("Window - Released"); 128 | } 129 | } 130 | 131 | 132 | HWND Window::GetHWND() 133 | { 134 | return mWindowHandler; 135 | } 136 | 137 | int Window::GetWidth() 138 | { 139 | return mWidth; 140 | } 141 | 142 | int Window::GetHeight() 143 | { 144 | return mHeight; 145 | } 146 | 147 | 148 | 149 | 150 | 151 | 152 | LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 153 | { 154 | switch (uMsg) 155 | { 156 | 157 | case WM_ACTIVATE: 158 | { 159 | return 0; 160 | } 161 | case WM_SYSCOMMAND: 162 | { 163 | switch (wParam) 164 | { 165 | case SC_SCREENSAVE: 166 | case SC_MONITORPOWER: 167 | return 0; 168 | } 169 | break; 170 | } 171 | case WM_CLOSE: 172 | { 173 | DestroyWindow(hWnd); 174 | Log::Success("Window - Released by WndProc"); 175 | return 0; 176 | } 177 | case WM_DESTROY: 178 | { 179 | PostQuitMessage(0); 180 | return 0; 181 | } 182 | } 183 | // Pass All Unhandled Messages To DefWindowProc 184 | return DefWindowProc(hWnd,uMsg,wParam,lParam); 185 | } 186 | 187 | } -------------------------------------------------------------------------------- /Includes/EngineBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | =============================================================================== 3 | Project Tuxis. 2011-2012. 4 | =============================================================================== 5 | Base class 6 | Required: 7 | =============================================================================== 8 | */ 9 | 10 | #pragma once 11 | #include "_DllExport.h" 12 | 13 | #define _XM_NO_INTRINSICS_ 14 | 15 | #include 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | 21 | #define _RELEASE(p) { if(p){(p)->Release(); (p)=nullptr;} } 22 | #define _RELEASE_DELETE(p) { if(p){(p)->Release(); delete (p); (p)=nullptr;} } 23 | 24 | namespace Tuxis 25 | { 26 | #define DEGTORAD 0.0174532925199432957f 27 | #define RADTODEG 57.295779513082320876f 28 | 29 | 30 | #pragma once 31 | 32 | #include 33 | using namespace std; 34 | 35 | /* 36 | ** Common math constants 37 | */ 38 | #define DEGTORAD 0.0174532925199432957f 39 | #define RADTODEG 57.295779513082320876f 40 | #ifndef M_PI 41 | #define M_PI 3.14159265358979323846f 42 | #define M_PI_2 1.57079632679489661923f 43 | #define M_PI_4 0.785398163397448309616f 44 | #define M_1_PI 0.318309886183790671538f 45 | #define M_2_PI 0.636619772367581343076f 46 | #endif 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | extern TUXIS_DECLSPEC HRESULT hr; 55 | 56 | namespace ShaderLayouts 57 | { 58 | extern TUXIS_DECLSPEC D3D11_INPUT_ELEMENT_DESC SpriteLayout[]; 59 | extern TUXIS_DECLSPEC D3D11_INPUT_ELEMENT_DESC StandartLayout[]; 60 | extern TUXIS_DECLSPEC D3D11_INPUT_ELEMENT_DESC SpriteGroupLayout[]; 61 | extern TUXIS_DECLSPEC D3D11_INPUT_ELEMENT_DESC TiledSpriteGroupLayout[]; 62 | extern TUXIS_DECLSPEC D3D11_INPUT_ELEMENT_DESC PrimitiveLayout[]; 63 | extern TUXIS_DECLSPEC D3D11_INPUT_ELEMENT_DESC TestLayout[]; 64 | } 65 | 66 | 67 | 68 | namespace Path 69 | { 70 | extern TUXIS_DECLSPEC wstring DEFAULT_SHADERS; 71 | } 72 | 73 | // Sprite constant buffer value 74 | struct CBSprite 75 | { 76 | CBSprite() 77 | { 78 | COLOR = XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ); 79 | } 80 | 81 | XMMATRIX FINAL_MATRIX; 82 | XMFLOAT4 COLOR; 83 | }; 84 | 85 | 86 | namespace Vertex 87 | { 88 | struct vtxSprite 89 | { 90 | vtxSprite() 91 | { 92 | Position =XMFLOAT3( 0.0f, 0.0f, 0.0f ); 93 | TexCoord =XMFLOAT2( 0.0f, 0.0f ); 94 | 95 | } 96 | 97 | XMFLOAT3 Position; 98 | XMFLOAT2 TexCoord; 99 | }; 100 | 101 | struct SpriteVertex 102 | { 103 | SpriteVertex() 104 | { 105 | Position =XMFLOAT3( 0.0f, 0.0f, 0.0f ); 106 | TexCoord =XMFLOAT2( 0.0f, 0.0f ); 107 | Color =XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f ); 108 | } 109 | 110 | XMFLOAT3 Position; 111 | XMFLOAT2 TexCoord; 112 | XMFLOAT4 Color; 113 | 114 | }; 115 | 116 | struct TiledSpriteVertex 117 | { 118 | TiledSpriteVertex() 119 | { 120 | Position = XMFLOAT3( 0.0f, 0.0f, 0.0f ); 121 | TexCoord = XMFLOAT2( 0.0f, 0.0f ); 122 | } 123 | 124 | XMFLOAT3 Position; 125 | XMFLOAT2 TexCoord; 126 | 127 | }; 128 | 129 | 130 | struct SpriteGroupVertex 131 | { 132 | SpriteGroupVertex() 133 | { 134 | Position = XMFLOAT3( 0.0f, 0.0f, 0.0f ); 135 | Rotation = XMFLOAT3( 0.0f, 0.0f, 0.0f ); 136 | Scale = 1.0f; 137 | TexCoord = XMFLOAT2( 0.0f, 0.0f ); 138 | Color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f ); 139 | } 140 | 141 | XMFLOAT3 Position; 142 | XMFLOAT3 Rotation; 143 | float Scale; 144 | XMFLOAT2 TexCoord; 145 | XMFLOAT4 Color; 146 | 147 | }; 148 | 149 | struct PrimitiveVertex 150 | { 151 | PrimitiveVertex() 152 | { 153 | Position =XMFLOAT3( 0.0f, 0.0f, 0.0f ); 154 | Color =XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f ); 155 | } 156 | 157 | XMFLOAT3 Position; 158 | XMFLOAT4 Color; 159 | 160 | }; 161 | 162 | struct VertexTest 163 | { 164 | XMFLOAT3 Position; 165 | XMFLOAT2 Texture; 166 | }; 167 | } 168 | 169 | 170 | 171 | 172 | /* 173 | =============================================================================== 174 | Functions 175 | =============================================================================== 176 | */ 177 | TUXIS_DECLSPEC const char* IntToString(int val); 178 | TUXIS_DECLSPEC const char* FloatToString(float val); 179 | 180 | TUXIS_DECLSPEC const wchar_t* IntToWString(int val); 181 | TUXIS_DECLSPEC const wchar_t* FloatToWString(float val); 182 | 183 | 184 | typedef 185 | enum HorizontalAlign 186 | { 187 | HLEFT, 188 | HCENTER, 189 | HRIGHT, 190 | } HorizontalAlign; 191 | 192 | 193 | typedef 194 | enum VerticalAlign 195 | { 196 | VTOP, 197 | VCENTER, 198 | VBOTTOM 199 | } VerticalAlign; 200 | 201 | 202 | 203 | } -------------------------------------------------------------------------------- /Sources/BitmapFont.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "BitmapFont.h" 3 | 4 | namespace Tuxis 5 | { 6 | sCharset* BitmapFont::GetCharset() 7 | { 8 | return &Charset; 9 | } 10 | 11 | TextureAtlas *BitmapFont::GetTextureAtlas() 12 | { 13 | return &mTexture; 14 | } 15 | 16 | 17 | void BitmapFont::LoadCharMap( const WCHAR* FileName ) 18 | { 19 | fstream Stream; 20 | Stream.open( FileName, ios::in | ios::binary ); 21 | 22 | if(Stream.fail()) 23 | { 24 | Log::Error("BitmapFont::Load: font not found"); 25 | } 26 | 27 | string Line; 28 | string Read, Key, Value; 29 | 30 | std::size_t i; 31 | 32 | while( !Stream.eof() ) 33 | { 34 | std::stringstream LineStream; 35 | std::getline( Stream, Line ); 36 | LineStream << Line; 37 | 38 | //read the line's type 39 | LineStream >> Read; 40 | 41 | if( Read == "common" ) 42 | { 43 | //this holds common data 44 | while( !LineStream.eof() ) 45 | { 46 | std::stringstream Converter; 47 | LineStream >> Read; 48 | i = Read.find( '=' ); 49 | Key = Read.substr( 0, i ); 50 | Value = Read.substr( i + 1 ); 51 | 52 | //assign the correct value 53 | Converter << Value; 54 | if( Key == "lineHeight" ) 55 | { 56 | Converter >> Charset.LineHeight; 57 | } 58 | else if( Key == "base" ) 59 | { 60 | Converter >> Charset.Base; 61 | } 62 | else if( Key == "scaleW" ) 63 | { 64 | Converter >> Charset.Width; 65 | } 66 | else if( Key == "scaleH" ) 67 | { 68 | Converter >> Charset.Height; 69 | } 70 | else if( Key == "pages" ) 71 | { 72 | Converter >> Charset.Pages; 73 | } 74 | } 75 | } 76 | else 77 | if( Read == "page" ) 78 | { 79 | 80 | //this holds common data 81 | while( !LineStream.eof() ) 82 | { 83 | std::stringstream Converter; 84 | LineStream >> Read; 85 | i = Read.find( '=' ); 86 | Key = Read.substr( 0, i ); 87 | Value = Read.substr( i + 1 ); 88 | 89 | //assign the correct value 90 | Converter << Value; 91 | 92 | if( Key == "file" ) 93 | { 94 | std::wstring wStr; 95 | std::string aStr; 96 | Converter >> aStr; 97 | if(!aStr.empty()) 98 | { 99 | wStr.assign(aStr.begin()+1,aStr.end()-1); 100 | } 101 | 102 | TextureName.append(wStr); 103 | 104 | break; 105 | } 106 | } 107 | } 108 | else 109 | if( Read == "char" ) 110 | { 111 | //this is data for a specific char 112 | unsigned short CharID = 0; 113 | 114 | while( !LineStream.eof() ) 115 | { 116 | std::stringstream Converter; 117 | LineStream >> Read; 118 | i = Read.find( '=' ); 119 | Key = Read.substr( 0, i ); 120 | Value = Read.substr( i + 1 ); 121 | 122 | //assign the correct value 123 | Converter << Value; 124 | if( Key == "id" ) 125 | { 126 | Converter >> CharID; 127 | } 128 | else if( Key == "x" ) 129 | { 130 | Converter >> Charset.Chars[CharID].x; 131 | } 132 | else if( Key == "y" ) 133 | { 134 | Converter >> Charset.Chars[CharID].y; 135 | } 136 | else if( Key == "width" ) 137 | { 138 | Converter >> Charset.Chars[CharID].Width; 139 | } 140 | else if( Key == "height" ) 141 | { 142 | Converter >> Charset.Chars[CharID].Height; 143 | } 144 | else if( Key == "xoffset" ) 145 | { 146 | Converter >> Charset.Chars[CharID].XOffset; 147 | } 148 | else if( Key == "yoffset" ) 149 | { 150 | Converter >> Charset.Chars[CharID].YOffset; 151 | } 152 | else if( Key == "xadvance" ) 153 | { 154 | Converter >> Charset.Chars[CharID].XAdvance; 155 | } 156 | else if( Key == "page" ) 157 | { 158 | Converter >> Charset.Chars[CharID].Page; 159 | } 160 | } 161 | } 162 | } 163 | 164 | } 165 | 166 | 167 | 168 | void BitmapFont::Load( const WCHAR* FontName ) 169 | { 170 | // Extract path for texture || FontPath.find_last_of(L'/') == -1 171 | wstring FontPath = FontName; 172 | 173 | int state=0; 174 | 175 | // if finded slash 176 | if(FontPath.find_last_of(L'\\') != -1) 177 | { 178 | TextureName = FontPath.substr(0,FontPath.find_last_of(L'\\')+1); 179 | } 180 | else 181 | { 182 | if(FontPath.find_last_of(L'/') != -1) 183 | TextureName = FontPath.substr(0,FontPath.find_last_of(L'/')+1); 184 | else 185 | TextureName = L""; 186 | } 187 | 188 | LoadCharMap( FontName ); 189 | mTexture.LoadFromFile(TextureName.c_str()); 190 | } 191 | 192 | 193 | 194 | void BitmapFont::Release() 195 | { 196 | 197 | mTexture.Release(); 198 | } 199 | } -------------------------------------------------------------------------------- /Sources/Mesh.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Mesh.h" 3 | 4 | namespace Tuxis 5 | { 6 | Mesh::Mesh() 7 | { 8 | InitDrawable(); 9 | 10 | mShader=new Shader(); 11 | wstring ResPath=Path::DEFAULT_SHADERS+L"TxMesh.fx"; 12 | mShader->Load(ResPath.c_str()); 13 | mShader->CreateInputLayout(ShaderLayouts::TestLayout,2); 14 | 15 | CreateConstantBuffer(sizeof(CBStruct),D3D11_USAGE_DEFAULT); 16 | } 17 | 18 | 19 | bool Mesh::LoadModel(const WCHAR* filename) 20 | { 21 | ifstream fin; 22 | char input; 23 | int i; 24 | 25 | 26 | // Open the model file. 27 | fin.open(filename); 28 | 29 | // If it could not open the file then exit. 30 | if(fin.fail()) 31 | { 32 | return false; 33 | } 34 | 35 | // Read up to the value of vertex count. 36 | fin.get(input); 37 | while(input != ':') 38 | { 39 | fin.get(input); 40 | } 41 | 42 | // Read in the vertex count. 43 | fin >> VertexCount; 44 | 45 | // Create the model using the vertex count that was read in. 46 | vertices = new Tuxis::Vertex::VertexTest[VertexCount]; 47 | if(!vertices) 48 | { 49 | return false; 50 | } 51 | 52 | // Read up to the beginning of the data. 53 | fin.get(input); 54 | while(input != ':') 55 | { 56 | fin.get(input); 57 | } 58 | fin.get(input); 59 | fin.get(input); 60 | 61 | // NOT USED 62 | XMFLOAT3 Normal; 63 | 64 | // Read in the vertex data. 65 | for(i=0; i> vertices[i].Position.x >> vertices[i].Position.y >> vertices[i].Position.z; 68 | fin >> vertices[i].Texture.x >> vertices[i].Texture.y; 69 | fin >> Normal.x >> Normal.y >> Normal.z; 70 | } 71 | 72 | // Close the model file. 73 | fin.close(); 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | VB_Stride = sizeof( Tuxis::Vertex::VertexTest ); 83 | VB_Offset = 0; 84 | 85 | // Vertex Buffer Creating 86 | D3D11_BUFFER_DESC VertexBufferDesc; 87 | ZeroMemory( &VertexBufferDesc, sizeof( D3D11_BUFFER_DESC ) ); 88 | VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 89 | VertexBufferDesc.ByteWidth = sizeof( Tuxis::Vertex::VertexTest ) * VertexCount; 90 | VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 91 | VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 92 | 93 | D3D11_SUBRESOURCE_DATA VertexBufferData; 94 | ZeroMemory( &VertexBufferData, sizeof( D3D11_SUBRESOURCE_DATA ) ); 95 | VertexBufferData.pSysMem = vertices; 96 | VertexBufferData.SysMemPitch = sizeof( vertices ); 97 | VertexBufferData.SysMemSlicePitch = sizeof( vertices ); 98 | hr = Engine::GetDevice()->CreateBuffer( &VertexBufferDesc, &VertexBufferData, &VertexBuffer ); 99 | if(FAILED(hr)) 100 | Log::Error("CreateBuffer vertexBufferDesc"); 101 | 102 | 103 | return true; 104 | } 105 | 106 | 107 | 108 | void Mesh::SetShader(Shader *pShader) 109 | { 110 | 111 | } 112 | 113 | void Mesh::SetTextureAuto(TextureAtlas *pTexture) 114 | { 115 | mTexture=pTexture; 116 | 117 | } 118 | 119 | 120 | 121 | void Mesh::Draw() 122 | { 123 | if(!mVisible) return; 124 | 125 | if(!VertexBuffer) Log::Error("Primitive::Draw VertexBuffer is null"); 126 | 127 | 128 | // Calculate matrices 129 | XMMATRIX WorldMatrix = CalculateWorldMatrix(); 130 | XMMATRIX FinalMatrix = WorldMatrix*(Engine::GetInstance()->GetActive3DCamera()->GetFinalMatrix() ); 131 | FinalMatrix = XMMatrixTranspose( FinalMatrix ); 132 | 133 | /* 134 | D3D11_MAPPED_SUBRESOURCE mappedResource; 135 | { 136 | HRESULT result; 137 | // Lock the constant buffer so it can be written to. 138 | result = Engine::Context->Map(mShader->ObjectBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 139 | if(FAILED(result)) 140 | { 141 | TxLog::Error("Primitive::Draw Engine::Context->Map(mShader->ObjectBuffer..."); 142 | } 143 | // Get a pointer to the data in the constant buffer. 144 | CBStruct *dataPtr = (CBStruct*)mappedResource.pData; 145 | *dataPtr=mCBStruct; 146 | 147 | // Unlock the constant buffer. 148 | Engine::Context->Unmap(mShader->ObjectBuffer, 0); 149 | } 150 | 151 | */ 152 | 153 | 154 | 155 | Graphics::Instance()->EnableStencilBuffer(); 156 | 157 | // Context configuration 158 | 159 | Engine::GetContext()->IASetVertexBuffers( 0, 1, &VertexBuffer, &VB_Stride, &VB_Offset ); 160 | Engine::GetContext()->VSSetShader( mShader->VertexShader, 0, 0 ); 161 | Engine::GetContext()->PSSetShader( mShader->PixelShader , 0, 0 ); 162 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, &FinalMatrix, 0, 0 ); 163 | Engine::GetContext()->VSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 164 | Engine::GetContext()->PSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 165 | 166 | Engine::GetContext()->PSSetShaderResources( 0, 1, mTexture->GetShaderResourceView() ); 167 | Engine::GetContext()->PSSetSamplers( 0, 1, mTexture->GetSamplerState() ); 168 | 169 | Engine::GetContext()->IASetInputLayout( mShader->InputLayout ); 170 | Engine::GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); 171 | 172 | 173 | 174 | // Drawing 175 | 176 | Engine::GetContext()->Draw(VertexCount,0); 177 | 178 | } 179 | 180 | void Mesh::Release() 181 | { 182 | if(vertices) 183 | { 184 | delete [] vertices; 185 | vertices = 0; 186 | } 187 | ReleaseDrawable(); 188 | } 189 | 190 | 191 | 192 | void Mesh::Update() 193 | { 194 | 195 | } 196 | } -------------------------------------------------------------------------------- /Sources/Sprite.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Sprite.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | Sprite::Sprite() 8 | { 9 | mChanged=true; 10 | InitDrawable(); 11 | 12 | AbsTexCoord(0.0f,0.0f,1.0f,1.0f); 13 | HalfTexSize(0,0); 14 | 15 | VB_Stride = sizeof(vertices[0]); 16 | VB_Offset = 0; 17 | 18 | Initialize(); 19 | } 20 | 21 | void Sprite::Initialize() 22 | { 23 | // Vertex Buffer Creating 24 | D3D11_BUFFER_DESC VertexBufferDesc; 25 | ZeroMemory( &VertexBufferDesc, sizeof( D3D11_BUFFER_DESC ) ); 26 | VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 27 | VertexBufferDesc.ByteWidth = VB_Stride * 4; 28 | VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 29 | VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 30 | 31 | D3D11_SUBRESOURCE_DATA VertexBufferData; 32 | ZeroMemory( &VertexBufferData, sizeof( D3D11_SUBRESOURCE_DATA ) ); 33 | VertexBufferData.pSysMem = vertices; 34 | VertexBufferData.SysMemPitch = sizeof( vertices ); 35 | VertexBufferData.SysMemSlicePitch = sizeof( vertices ); 36 | 37 | 38 | hr = Engine::GetDevice()->CreateBuffer( &VertexBufferDesc, &VertexBufferData, &VertexBuffer ); 39 | if(FAILED(hr)) 40 | Log::Error("TxSprite::TxSprite: CreateBuffer VertexBuffer"); 41 | 42 | mShader = DefaultShaderBase::Instance()->mShaderSprite; 43 | CreateConstantBuffer(sizeof(CBSprite),D3D11_USAGE_DEFAULT); 44 | } 45 | 46 | void Sprite::SetShader(Shader *pShader) 47 | { 48 | mChanged=true; 49 | mShader=pShader; 50 | } 51 | 52 | 53 | void Sprite::SetTextureRegion(TextureRegion *pTextureRegion) 54 | { 55 | mChanged=true; 56 | mTexture=pTextureRegion->mTexture; 57 | 58 | HalfTexSize.x=( (pTextureRegion->Region.x2-pTextureRegion->Region.x1) )/2.0f; 59 | HalfTexSize.y=( (pTextureRegion->Region.y2-pTextureRegion->Region.y1) )/2.0f; 60 | 61 | AbsTexCoord.x1 = pTextureRegion->Region.x1 / pTextureRegion->mTexture->GetWidth(); 62 | AbsTexCoord.x2 = pTextureRegion->Region.x2 / pTextureRegion->mTexture->GetWidth(); 63 | AbsTexCoord.y1 = pTextureRegion->Region.y1 / pTextureRegion->mTexture->GetHeight(); 64 | AbsTexCoord.y2 = pTextureRegion->Region.y2 / pTextureRegion->mTexture->GetHeight(); 65 | } 66 | 67 | void Sprite::SetTextureAtlas(TextureAtlas *pTexture) 68 | { 69 | mChanged=true; 70 | mTexture=pTexture; 71 | 72 | HalfTexSize.x=pTexture->GetWidth() / 2.0f; 73 | HalfTexSize.y=pTexture->GetHeight() / 2.0f; 74 | 75 | AbsTexCoord.x1 = 0.0f; 76 | AbsTexCoord.x2 = 1.0f; 77 | AbsTexCoord.y1 = 0.0f; 78 | AbsTexCoord.y2 = 1.0f; 79 | } 80 | 81 | void Sprite::SetPosition2D( float pX, float pY ,bool centered=false) 82 | { 83 | mChanged=true; 84 | 85 | if(centered) 86 | TranslationMatrix = XMMatrixTranslation( pX, pY, 0.0f ); 87 | else 88 | TranslationMatrix = XMMatrixTranslation( pX+HalfTexSize.x, pY+HalfTexSize.y, 0.0f ); 89 | } 90 | 91 | void Sprite::Draw() 92 | { 93 | 94 | if(!mVisible) return; 95 | 96 | if(!mTexture) 97 | Log::Error("Sprite::Draw - Not set Texture."); 98 | if(!mShader) 99 | Log::Error("Sprite::Draw - Not set Shader."); 100 | 101 | if(mChanged) 102 | { 103 | Log::Warning("Sprite::Class - Is changed between Update-Draw."); 104 | Update(); 105 | } 106 | 107 | Graphics::Instance()->DisableStencilBuffer(); 108 | 109 | // Context configuration 110 | Engine::GetContext()->IASetVertexBuffers( 0, 1, &VertexBuffer, &VB_Stride, &VB_Offset ); 111 | Engine::GetContext()->VSSetShader( mShader->VertexShader, 0, 0 ); 112 | Engine::GetContext()->PSSetShader( mShader->PixelShader , 0, 0 ); 113 | Engine::GetContext()->VSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 114 | Engine::GetContext()->PSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 115 | Engine::GetContext()->PSSetShaderResources( 0, 1, mTexture->GetShaderResourceView() ); 116 | Engine::GetContext()->PSSetSamplers( 0, 1, mTexture->GetSamplerState() ); 117 | Engine::GetContext()->IASetInputLayout( mShader->InputLayout ); 118 | Engine::GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP ); 119 | 120 | // Drawing 121 | Engine::GetContext()->Draw(4,0); 122 | } 123 | 124 | 125 | 126 | void Sprite::Update() 127 | { 128 | 129 | if(mChanged) 130 | { 131 | // Update Vertex Buffer 132 | D3D11_MAPPED_SUBRESOURCE mapResource; 133 | Engine::GetContext()->Map( VertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapResource ); 134 | { 135 | Vertex::vtxSprite* pTempVertex=(Vertex::vtxSprite*)mapResource.pData; 136 | 137 | pTempVertex[0].Position = XMFLOAT3(-HalfTexSize.x, HalfTexSize.y, 0.0f); 138 | pTempVertex[1].Position = XMFLOAT3(-HalfTexSize.x, -HalfTexSize.y, 0.0f); 139 | pTempVertex[2].Position = XMFLOAT3( HalfTexSize.x, HalfTexSize.y, 0.0f); 140 | pTempVertex[3].Position = XMFLOAT3( HalfTexSize.x, -HalfTexSize.y, 0.0f); 141 | 142 | pTempVertex[0].TexCoord = XMFLOAT2( AbsTexCoord.x1, AbsTexCoord.y2 ); 143 | pTempVertex[1].TexCoord = XMFLOAT2( AbsTexCoord.x1, AbsTexCoord.y1 ); 144 | pTempVertex[2].TexCoord = XMFLOAT2( AbsTexCoord.x2, AbsTexCoord.y2 ); 145 | pTempVertex[3].TexCoord = XMFLOAT2( AbsTexCoord.x2, AbsTexCoord.y1 ); 146 | 147 | } 148 | Engine::GetContext()->Unmap( VertexBuffer,0); 149 | 150 | // Calculate matrices 151 | XMMATRIX WorldMatrix = CalculateWorldMatrix(); 152 | XMMATRIX FinalMatrix = WorldMatrix*(Engine::GetInstance()->GetActive2DCamera()->GetFinalMatrix() ); 153 | 154 | mCBValue.FINAL_MATRIX = XMMatrixTranspose( FinalMatrix ); 155 | mCBValue.COLOR = ColorModulation; 156 | 157 | // Update Constant buffer 158 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, &mCBValue, 0, 0 ); 159 | 160 | mChanged=false; 161 | } 162 | } 163 | 164 | 165 | void Sprite::Release() 166 | { 167 | mShader = 0; 168 | ReleaseDrawable(); 169 | } 170 | 171 | } 172 | -------------------------------------------------------------------------------- /Sources/TiledSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "TiledSprite.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | TiledSprite::TiledSprite() 8 | { 9 | mChanged=true; 10 | InitDrawable(); 11 | 12 | mFrame=0; 13 | mFrameTime=0; 14 | mLastTime=0; 15 | mTiledTextureRegion=0; 16 | 17 | AbsTextureCoordinate(0.0f,0.0f,1.0f,1.0f); 18 | HalfTexSize(0,0); 19 | 20 | VB_Stride = sizeof( vertices[0] ); 21 | VB_Offset = 0; 22 | 23 | D3D11_BUFFER_DESC VertexBufferDesc; 24 | ZeroMemory( &VertexBufferDesc, sizeof( D3D11_BUFFER_DESC ) ); 25 | VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;//D3D11_USAGE_DEFAULT; 26 | VertexBufferDesc.ByteWidth = VB_Stride * 4; 27 | VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 28 | VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 29 | 30 | D3D11_SUBRESOURCE_DATA VertexBufferData; 31 | ZeroMemory( &VertexBufferData, sizeof( D3D11_SUBRESOURCE_DATA ) ); 32 | VertexBufferData.pSysMem = vertices; 33 | VertexBufferData.SysMemPitch = sizeof( vertices ); 34 | VertexBufferData.SysMemSlicePitch = sizeof( vertices ); 35 | 36 | hr = Engine::GetDevice()->CreateBuffer( &VertexBufferDesc, &VertexBufferData, &VertexBuffer ); 37 | if(FAILED(hr)) 38 | Log::Error("CreateBuffer vertexBufferDesc"); 39 | 40 | mShader = DefaultShaderBase::Instance()->mShaderTiledSprite; 41 | CreateConstantBuffer(sizeof(CBSprite),D3D11_USAGE_DEFAULT); 42 | } 43 | 44 | 45 | 46 | void TiledSprite::SetTiledTextureRegion(TiledTextureRegion *pTiledTextureRegion) 47 | { 48 | mTiledTextureRegion=pTiledTextureRegion; 49 | 50 | floatRect *Region=mTiledTextureRegion->TileCoordinates.at(mFrame); 51 | 52 | HalfTexSize.x=( (Region->x2-Region->x1) )/2.0f; 53 | HalfTexSize.y=( (Region->y2-Region->y1) )/2.0f; 54 | 55 | AbsTextureCoordinate.x1 = Region->x1 / mTiledTextureRegion->mTexture->GetWidth(); 56 | AbsTextureCoordinate.x2 = Region->x2 / mTiledTextureRegion->mTexture->GetWidth(); 57 | AbsTextureCoordinate.y1 = Region->y1 / mTiledTextureRegion->mTexture->GetHeight(); 58 | AbsTextureCoordinate.y2 = Region->y2 / mTiledTextureRegion->mTexture->GetHeight(); 59 | } 60 | 61 | 62 | void TiledSprite::SetFrame(int frame) 63 | { 64 | if(frame<0) frame=0; 65 | if((unsigned int)frame>=mTiledTextureRegion->TileCoordinates.size()) frame=0; 66 | mFrame=frame; 67 | } 68 | 69 | void TiledSprite::SetNextFrame() 70 | { 71 | SetFrame(mFrame+1); 72 | } 73 | 74 | 75 | int TiledSprite::GetFrameCount() 76 | { 77 | return mTiledTextureRegion->TileCoordinates.size(); 78 | } 79 | 80 | 81 | void TiledSprite::SetPosition2D( float tX, float tY ,bool centered=false) 82 | { 83 | if(centered) 84 | TranslationMatrix = XMMatrixTranslation( tX, tY, 0.0f ); 85 | else 86 | TranslationMatrix = XMMatrixTranslation( tX+HalfTexSize.x, tY+HalfTexSize.y, 0.0f ); 87 | } 88 | 89 | void TiledSprite::Animate(int pTimeMillisecond) 90 | { 91 | if(pTimeMillisecond>0) 92 | mFrameTime=pTimeMillisecond; 93 | else 94 | mFrameTime=0; 95 | } 96 | 97 | void TiledSprite::Draw() 98 | { 99 | if(!mVisible) return; 100 | 101 | Graphics::Instance()->DisableStencilBuffer(); 102 | 103 | // Frame calculation 104 | floatRect *Region=mTiledTextureRegion->TileCoordinates.at(mFrame); 105 | 106 | HalfTexSize.x=( (Region->x2-Region->x1) )/2.0f; 107 | HalfTexSize.y=( (Region->y2-Region->y1) )/2.0f; 108 | 109 | AbsTextureCoordinate.x1 = Region->x1 / mTiledTextureRegion->mTexture->GetWidth(); 110 | AbsTextureCoordinate.x2 = Region->x2 / mTiledTextureRegion->mTexture->GetWidth(); 111 | AbsTextureCoordinate.y1 = Region->y1 / mTiledTextureRegion->mTexture->GetHeight(); 112 | AbsTextureCoordinate.y2 = Region->y2 / mTiledTextureRegion->mTexture->GetHeight(); 113 | 114 | // Matrix calculation 115 | XMMATRIX WorldMatrix = CalculateWorldMatrix(); 116 | XMMATRIX FinalMatrix = WorldMatrix*(Engine::GetInstance()->GetActive2DCamera()->GetFinalMatrix() ); 117 | 118 | mCBValue.FINAL_MATRIX = XMMatrixTranspose( FinalMatrix ); 119 | mCBValue.COLOR = ColorModulation; 120 | 121 | D3D11_MAPPED_SUBRESOURCE mapResource; 122 | Engine::GetContext()->Map( VertexBuffer, 0,D3D11_MAP_WRITE_DISCARD, 0, &mapResource ); 123 | { 124 | Vertex::vtxSprite* pTempVertex=(Vertex::vtxSprite*)mapResource.pData; 125 | pTempVertex[0].Position = XMFLOAT3(-HalfTexSize.x, HalfTexSize.y, 0.0f); 126 | pTempVertex[1].Position = XMFLOAT3(-HalfTexSize.x, -HalfTexSize.y, 0.0f); 127 | pTempVertex[2].Position = XMFLOAT3( HalfTexSize.x, HalfTexSize.y, 0.0f); 128 | pTempVertex[3].Position = XMFLOAT3( HalfTexSize.x, -HalfTexSize.y, 0.0f); 129 | 130 | pTempVertex[0].TexCoord = XMFLOAT2( AbsTextureCoordinate.x1, AbsTextureCoordinate.y2 ); 131 | pTempVertex[1].TexCoord = XMFLOAT2( AbsTextureCoordinate.x1, AbsTextureCoordinate.y1 ); 132 | pTempVertex[2].TexCoord = XMFLOAT2( AbsTextureCoordinate.x2, AbsTextureCoordinate.y2 ); 133 | pTempVertex[3].TexCoord = XMFLOAT2( AbsTextureCoordinate.x2, AbsTextureCoordinate.y1 ); 134 | } 135 | Engine::GetContext()->Unmap( VertexBuffer,0); 136 | 137 | 138 | Engine::GetContext()->IASetVertexBuffers( 0, 1, &VertexBuffer, &VB_Stride, &VB_Offset ); 139 | Engine::GetContext()->VSSetShader( mShader->VertexShader, 0, 0 ); 140 | Engine::GetContext()->PSSetShader( mShader->PixelShader , 0, 0 ); 141 | 142 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, &mCBValue, 0, 0 ); 143 | Engine::GetContext()->VSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 144 | Engine::GetContext()->PSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 145 | 146 | Engine::GetContext()->PSSetShaderResources( 0, 1, mTiledTextureRegion->mTexture->GetShaderResourceView() ); 147 | Engine::GetContext()->PSSetSamplers( 0, 1, mTiledTextureRegion->mTexture->GetSamplerState() ); 148 | Engine::GetContext()->IASetInputLayout( mShader->InputLayout ); 149 | Engine::GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP ); 150 | 151 | 152 | Engine::GetContext()->Draw(4,0); 153 | 154 | } 155 | 156 | 157 | void TiledSprite::Update() 158 | { 159 | if(mFrameTime) 160 | { 161 | int tempTime=timeGetTime(); 162 | if(tempTime-mLastTime>mFrameTime) 163 | { 164 | mLastTime=tempTime; 165 | SetNextFrame(); 166 | } 167 | } 168 | } 169 | 170 | void TiledSprite::Release() 171 | { 172 | mShader = 0; 173 | ReleaseDrawable(); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /Includes/Types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * =============================================================================== 3 | * Tuxis Project 4 | * Copyright (C) 2011-2012, Andrew Tulay 5 | * =============================================================================== 6 | * Types 7 | * =============================================================================== 8 | */ 9 | #pragma once 10 | 11 | 12 | namespace Tuxis 13 | { 14 | /* 15 | *======================================================================= 16 | * Float types 17 | *======================================================================= 18 | */ 19 | 20 | struct float2 21 | { 22 | float x,y; 23 | 24 | inline float2() : x(0), y(0) {} 25 | inline float2(float px,float py) : x(px), y(py) {} 26 | inline float2(const float2& p) : x(p.x), y(p.y) {} 27 | 28 | inline void operator() (float px, float py) { x=px; y=py;} 29 | inline void operator() (const float2& p) { x=p.x; y=p.y;} 30 | 31 | void operator -= (const float2& pValue) 32 | { 33 | x-=pValue.x; 34 | y-=pValue.y; 35 | } 36 | 37 | void operator += (const float2& pValue) 38 | { 39 | x+=pValue.x; 40 | y+=pValue.y; 41 | } 42 | 43 | bool operator == (const float2& pValue) 44 | { 45 | return (x==pValue.x && y==pValue.y); 46 | } 47 | 48 | float2 operator + (const float2& pValue) 49 | { 50 | float2 t(x+pValue.x,y+pValue.y); 51 | return t; 52 | } 53 | 54 | float2 operator - (const float2& pValue) 55 | { 56 | float2 t(x-pValue.x,y-pValue.y); 57 | return t; 58 | } 59 | }; 60 | 61 | 62 | struct float4 63 | { 64 | float x,y,z,w; 65 | 66 | inline float4() : x(0), y(0), z(0), w(0) {} 67 | inline float4(float px,float py, float pz, float pw) : x(px), y(py), z(pz), w(pw) {} 68 | inline float4(const float4& p) : x(p.x), y(p.y), z(p.z), w(p.w) {} 69 | 70 | inline void operator() (float px, float py, float pz, float pw) { x=px; y=py; z=pz; w=pw;} 71 | inline void operator() (const float4& p) { x=p.x; y=p.y; z=p.z; w=p.w; } 72 | 73 | void operator -= (const float4& pValue) 74 | { 75 | x-=pValue.x; 76 | y-=pValue.y; 77 | z-=pValue.z; 78 | w-=pValue.w; 79 | } 80 | 81 | void operator += (const float4& pValue) 82 | { 83 | x+=pValue.x; 84 | y+=pValue.y; 85 | z+=pValue.z; 86 | w-=pValue.w; 87 | } 88 | 89 | bool operator == (const float4& pValue) 90 | { 91 | return ( 92 | x==pValue.x && 93 | y==pValue.y && 94 | z==pValue.z && 95 | w==pValue.w); 96 | } 97 | 98 | float4 operator + (const float4& pValue) 99 | { 100 | float4 t(x+pValue.x, y+pValue.y, z+pValue.z, w+pValue.w); 101 | return t; 102 | } 103 | 104 | float4 operator - (const float4& pValue) 105 | { 106 | float4 t(x-pValue.x, y-pValue.y, z-pValue.z, w-pValue.w); 107 | return t; 108 | } 109 | }; 110 | 111 | 112 | struct floatRect 113 | { 114 | float x1,y1,x2,y2; 115 | 116 | inline floatRect() : x1(0), y1(0), x2(0), y2(0) {} 117 | inline floatRect(float px1, float py1, float px2, float py2) 118 | : x1(px1), y1(py1), x2(px2), y2(py2) {} 119 | inline floatRect(float2 pPoint1, float2 pPoint2) 120 | : x1(pPoint1.x), y1(pPoint1.y), x2(pPoint2.x), y2(pPoint2.y) {} 121 | 122 | void operator() (float px1, float py1, float px2, float py2) 123 | { 124 | x1=px1; 125 | y1=py1; 126 | x2=px2; 127 | y2=py2; 128 | } 129 | 130 | void operator() (float2 pPoint1, float2 pPoint2) 131 | { 132 | x1=pPoint1.x; 133 | y1=pPoint1.y; 134 | x2=pPoint2.x; 135 | y2=pPoint2.y; 136 | } 137 | }; 138 | 139 | 140 | struct Color 141 | { 142 | float r,g,b,a; 143 | inline Color() : r(0), g(0), b(0), a(0) {} 144 | inline Color(float R, float G, float B,float A) : r(R), g(G), b(B), a(A) {} 145 | inline void operator () (float R, float G, float B,float A) { r=R; g=G; b=B; a=A; } 146 | 147 | void operator -= (const Color& pValue) 148 | { 149 | r-=pValue.r; 150 | g-=pValue.g; 151 | b-=pValue.b; 152 | a-=pValue.a; 153 | } 154 | 155 | void operator += (const Color& pValue) 156 | { 157 | r+=pValue.r; 158 | g+=pValue.g; 159 | b+=pValue.b; 160 | a-=pValue.a; 161 | } 162 | 163 | bool operator == (const Color& pValue) 164 | { 165 | return ( 166 | r==pValue.r && 167 | g==pValue.g && 168 | b==pValue.b && 169 | a==pValue.a); 170 | } 171 | 172 | Color operator + (const Color& pValue) 173 | { 174 | Color t(r+pValue.r, g+pValue.g, b+pValue.b, a+pValue.a); 175 | return t; 176 | } 177 | 178 | Color operator - (const Color& pValue) 179 | { 180 | Color t(r-pValue.r, g-pValue.g, b-pValue.b, a-pValue.a); 181 | return t; 182 | } 183 | }; 184 | 185 | 186 | /* 187 | *======================================================================= 188 | * Integer types 189 | *======================================================================= 190 | */ 191 | 192 | struct int2 193 | { 194 | int x,y; 195 | 196 | inline int2() : x(0), y(0) {} 197 | inline int2(int px,int py) : x(px), y(py) {} 198 | inline int2(const int2& p) : x(p.x), y(p.y) {} 199 | 200 | inline void operator() (int px, int py) { x=px; y=py;} 201 | inline void operator() (const int2& p) { x=p.x; y=p.y; } 202 | 203 | bool operator == (const int2& pValue) 204 | { 205 | return (x==pValue.x && y==pValue.y); 206 | } 207 | 208 | void operator -= (const int2& pValue) 209 | { 210 | x-=pValue.x; 211 | y-=pValue.y; 212 | } 213 | 214 | void operator += (const int2& pValue) 215 | { 216 | x+=pValue.x; 217 | y+=pValue.y; 218 | } 219 | 220 | int2 operator + (const int2& pValue) 221 | { 222 | int2 t(x+pValue.x,y+pValue.y); 223 | return t; 224 | } 225 | 226 | int2 operator - (const int2& pValue) 227 | { 228 | int2 t(x-pValue.x,y-pValue.y); 229 | return t; 230 | } 231 | }; 232 | 233 | struct int4 234 | { 235 | int x,y,z,w; 236 | 237 | inline int4() : x(0), y(0), z(0), w(0) {} 238 | inline int4(int px,int py, int pz, int pw) : x(px), y(py), z(pz), w(pw) {} 239 | inline int4(const int4& p) : x(p.x), y(p.y), z(p.z), w(p.w) {} 240 | 241 | inline void operator() (int px, int py, int pz, int pw) { x=px; y=py; z=pz; w=pw;} 242 | inline void operator() (const int2& p) { x=p.x; y=p.y; } 243 | 244 | bool operator == (const int4& pValue) 245 | { 246 | return ( 247 | x==pValue.x && 248 | y==pValue.y && 249 | z==pValue.z && 250 | w==pValue.w); 251 | } 252 | 253 | void operator -= (const int4& pValue) 254 | { 255 | x-=pValue.x; 256 | y-=pValue.y; 257 | z-=pValue.z; 258 | w-=pValue.w; 259 | } 260 | 261 | void operator += (const int4& pValue) 262 | { 263 | x+=pValue.x; 264 | y+=pValue.y; 265 | z+=pValue.z; 266 | w-=pValue.w; 267 | } 268 | 269 | int4 operator + (const int4& pValue) 270 | { 271 | int4 t(x+pValue.x, y+pValue.y, z+pValue.z, w+pValue.w); 272 | return t; 273 | } 274 | 275 | int4 operator - (const int4& pValue) 276 | { 277 | int4 t(x-pValue.x, y-pValue.y, z-pValue.z, w-pValue.w); 278 | return t; 279 | } 280 | }; 281 | 282 | } -------------------------------------------------------------------------------- /Sources/SpriteGroup.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "SpriteGroup.h" 3 | 4 | namespace Tuxis 5 | { 6 | SpriteGroup::SpriteGroup() 7 | { 8 | mChanged=true; 9 | InitDrawable(); 10 | 11 | SpriteBase=0; 12 | SpriteCount=0; 13 | 14 | mTextureRegion=0; 15 | 16 | AbsTexCoord(0.0f,0.0f,1.0f,1.0f); 17 | HalfTexSize(0,0); 18 | 19 | VB_Stride = sizeof( Tuxis::Vertex::SpriteGroupVertex ); 20 | VB_Offset = 0; 21 | 22 | mShader = DefaultShaderBase::Instance()->mShaderSpriteGroup; 23 | CreateConstantBuffer(sizeof(XMMATRIX),D3D11_USAGE_DEFAULT); 24 | } 25 | 26 | SpriteGroup::~SpriteGroup() 27 | { 28 | Release(); 29 | } 30 | 31 | 32 | void SpriteGroup::Create(int pSpriteCount,TextureRegion *pTextureRegion) 33 | { 34 | if(SpriteCount==0) 35 | { 36 | if(pSpriteCount>0 && pSpriteCount<=10000) 37 | { 38 | SpriteCount=pSpriteCount; 39 | SpriteBase=new BasicSprite[SpriteCount]; 40 | vertices=new Tuxis::Vertex::SpriteGroupVertex[6*SpriteCount]; 41 | 42 | // Vertex Buffer Creating 43 | D3D11_BUFFER_DESC VertexBufferDesc; 44 | ZeroMemory( &VertexBufferDesc, sizeof( D3D11_BUFFER_DESC ) ); 45 | VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 46 | VertexBufferDesc.ByteWidth = sizeof( Tuxis::Vertex::SpriteGroupVertex ) * 6 * SpriteCount; 47 | VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 48 | VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 49 | 50 | D3D11_SUBRESOURCE_DATA VertexBufferData; 51 | ZeroMemory( &VertexBufferData, sizeof( D3D11_SUBRESOURCE_DATA ) ); 52 | VertexBufferData.pSysMem = vertices; 53 | VertexBufferData.SysMemPitch = sizeof( vertices ); 54 | VertexBufferData.SysMemSlicePitch = sizeof( vertices ); 55 | 56 | hr = Engine::GetDevice()->CreateBuffer( &VertexBufferDesc, &VertexBufferData, &VertexBuffer ); 57 | if(FAILED(hr)) 58 | Log::Error("TxSpriteGroup:CreateBuffer vertexBufferDesc"); 59 | } 60 | else 61 | Log::Error("TxSpriteGroup::Create - strange count size (only >0 & <10001)"); 62 | } 63 | else 64 | Log::Error("TxSpriteGroup::Create - already created"); 65 | 66 | 67 | if(pTextureRegion) 68 | { 69 | mTextureRegion=pTextureRegion; 70 | 71 | HalfTexSize.x=( (mTextureRegion->Region.x2-mTextureRegion->Region.x1) )/2.0f; 72 | HalfTexSize.y=( (mTextureRegion->Region.y2-mTextureRegion->Region.y1) )/2.0f; 73 | 74 | AbsTexCoord.x1 = mTextureRegion->Region.x1 / mTextureRegion->mTexture->GetWidth(); 75 | AbsTexCoord.x2 = mTextureRegion->Region.x2 / mTextureRegion->mTexture->GetWidth(); 76 | AbsTexCoord.y1 = mTextureRegion->Region.y1 / mTextureRegion->mTexture->GetHeight(); 77 | AbsTexCoord.y2 = mTextureRegion->Region.y2 / mTextureRegion->mTexture->GetHeight(); 78 | } 79 | else 80 | Log::Error("SpriteGroup::Create - null region"); 81 | 82 | mChanged=true; 83 | } 84 | 85 | 86 | void SpriteGroup::Draw() 87 | { 88 | if(!mVisible) return; 89 | 90 | if(!mTextureRegion) 91 | Log::Error("Sprite::Draw - Not set Texture Region."); 92 | 93 | Graphics::Instance()->DisableStencilBuffer(); 94 | 95 | int RealDrawCount=SpriteCount; 96 | 97 | if(mChanged) 98 | { 99 | // Calculate matrices 100 | XMMATRIX WorldMatrix = CalculateWorldMatrix(); 101 | XMMATRIX FinalMatrix = WorldMatrix*(Engine::GetInstance()->GetActive2DCamera()->GetFinalMatrix() ); 102 | FinalMatrix = XMMatrixTranspose( FinalMatrix ); 103 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, &FinalMatrix, 0, 0 ); 104 | 105 | // Modify the vertex buffer 106 | D3D11_MAPPED_SUBRESOURCE mapResource; 107 | Engine::GetContext()->Map( VertexBuffer, 0,D3D11_MAP_WRITE_DISCARD, 0, &mapResource ); 108 | { 109 | Tuxis::Vertex::SpriteGroupVertex* pTempVertex=(Tuxis::Vertex::SpriteGroupVertex*)mapResource.pData; 110 | 111 | int BufferCounter=0; 112 | 113 | for(int i=0;iUnmap( VertexBuffer,0); 184 | } 185 | 186 | 187 | // Context configuration 188 | Engine::GetContext()->IASetVertexBuffers( 0, 1, &VertexBuffer, &VB_Stride, &VB_Offset ); 189 | Engine::GetContext()->VSSetShader( mShader->VertexShader, 0, 0 ); 190 | Engine::GetContext()->PSSetShader( mShader->PixelShader , 0, 0 ); 191 | 192 | Engine::GetContext()->VSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 193 | Engine::GetContext()->PSSetShaderResources( 0, 1, mTextureRegion->mTexture->GetShaderResourceView() ); 194 | Engine::GetContext()->PSSetSamplers( 0, 1, mTextureRegion->mTexture->GetSamplerState() ); 195 | Engine::GetContext()->IASetInputLayout( mShader->InputLayout ); 196 | Engine::GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); 197 | 198 | // Drawing RealDrawCount 199 | Engine::GetContext()->Draw(6*RealDrawCount,0); 200 | mChanged=false; 201 | } 202 | 203 | void SpriteGroup::Update() 204 | { 205 | 206 | } 207 | 208 | 209 | 210 | 211 | void SpriteGroup::SetPosition2D( float pX, float pY ,bool centered=false) 212 | { 213 | mChanged=true; 214 | if(centered) 215 | TranslationMatrix = XMMatrixTranslation( pX, pY, 0.0f ); 216 | else 217 | TranslationMatrix = XMMatrixTranslation( pX+HalfTexSize.x, pY+HalfTexSize.y, 0.0f ); 218 | } 219 | 220 | void SpriteGroup::Clear() 221 | { 222 | mChanged=true; 223 | SpriteCount=0; 224 | if(SpriteBase) 225 | delete[] SpriteBase; 226 | SpriteBase=0; 227 | } 228 | 229 | BasicSprite* SpriteGroup::GetSprite(int i) 230 | { 231 | mChanged=true; 232 | 233 | if(SpriteBase) 234 | if( i=0 ) 235 | return &SpriteBase[i]; 236 | else 237 | { 238 | Log::Error("SpriteGroup::GetSprite - parameter is out of range"); 239 | return 0; 240 | } 241 | else 242 | { 243 | Log::Error("SpriteGroup::GetSprite - group is empty"); 244 | return 0; 245 | } 246 | } 247 | 248 | void SpriteGroup::Release() 249 | { 250 | mShader = 0; 251 | mChanged=true; 252 | Clear(); 253 | ReleaseDrawable(); 254 | } 255 | } -------------------------------------------------------------------------------- /Sources/TiledSpriteGroup.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "TiledSpriteGroup.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | TiledSpriteGroup::TiledSpriteGroup() 8 | { 9 | 10 | InitDrawable(); 11 | 12 | CountOfSprites=0; 13 | SpriteBase=0; 14 | 15 | mTiledTextureRegion=0; 16 | 17 | vertices=0; 18 | 19 | AbsTextureCoordinate(0.0f,0.0f,1.0f,1.0f); 20 | HalfTexSize(0,0); 21 | 22 | VB_Stride = sizeof( Tuxis::Vertex::SpriteGroupVertex ); 23 | VB_Offset = 0; 24 | 25 | mShader = DefaultShaderBase::Instance()->mShaderTiledSpriteGroup; 26 | CreateConstantBuffer(sizeof(XMMATRIX),D3D11_USAGE_DEFAULT); 27 | } 28 | 29 | void TiledSpriteGroup::Update() 30 | { 31 | if(mFrameTime && mTiledTextureRegion) 32 | { 33 | int tempTime=timeGetTime(); 34 | if(tempTime-mLastTime>mFrameTime) 35 | { 36 | mLastTime=tempTime; 37 | int framesCount=mTiledTextureRegion->GetRegionCount(); 38 | 39 | for(int i=0; iFrame++; 42 | if(GetSprite(i)->Frame>=framesCount) 43 | { 44 | GetSprite(i)->Frame=0; 45 | } 46 | } 47 | 48 | } 49 | } 50 | } 51 | 52 | void TiledSpriteGroup::Create(int countSize,TiledTextureRegion *pTextureRegion) 53 | { 54 | mTiledTextureRegion = pTextureRegion; 55 | 56 | if(CountOfSprites==0) 57 | { 58 | if(countSize>0 && countSize<=10000) 59 | { 60 | 61 | CountOfSprites=countSize; 62 | SpriteBase=new BasicSprite[CountOfSprites]; 63 | vertices=new Tuxis::Vertex::SpriteGroupVertex[6*CountOfSprites]; 64 | 65 | // Vertex Buffer Creating 66 | D3D11_BUFFER_DESC VertexBufferDesc; 67 | ZeroMemory( &VertexBufferDesc, sizeof( D3D11_BUFFER_DESC ) ); 68 | VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;//D3D11_USAGE_DEFAULT; 69 | VertexBufferDesc.ByteWidth = sizeof( Tuxis::Vertex::SpriteGroupVertex ) * 6 * CountOfSprites; 70 | VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 71 | VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 72 | 73 | D3D11_SUBRESOURCE_DATA VertexBufferData; 74 | ZeroMemory( &VertexBufferData, sizeof( D3D11_SUBRESOURCE_DATA ) ); 75 | VertexBufferData.pSysMem = vertices; 76 | VertexBufferData.SysMemPitch = sizeof( vertices ); 77 | VertexBufferData.SysMemSlicePitch = sizeof( vertices ); 78 | 79 | 80 | hr = Engine::GetDevice()->CreateBuffer( &VertexBufferDesc, &VertexBufferData, &VertexBuffer ); 81 | if(FAILED(hr)) 82 | Log::Error("CreateBuffer vertexBufferDesc"); 83 | } 84 | else 85 | { 86 | Log::Error("TxTiledSpriteGroup::Create - strange count size (only >0 & <10001)"); 87 | } 88 | } 89 | else 90 | { 91 | Log::Error("TxTiledSpriteGroup::Create - already created"); 92 | } 93 | 94 | if(!pTextureRegion) 95 | { 96 | Log::Error("TxTiledSpriteGroup::Create - null region"); 97 | } 98 | 99 | } 100 | 101 | 102 | 103 | void TiledSpriteGroup::Draw() 104 | { 105 | if(!mVisible) return; 106 | 107 | 108 | if(!mTiledTextureRegion) 109 | Log::Error("TxTiledSpriteGroup::Draw - Not set Texture Region."); 110 | 111 | Graphics::Instance()->DisableStencilBuffer(); 112 | 113 | // Calculate matrices 114 | XMMATRIX WorldMatrix = CalculateWorldMatrix(); 115 | XMMATRIX FinalMatrix = WorldMatrix*(Engine::GetInstance()->GetActive2DCamera()->GetFinalMatrix() ); 116 | FinalMatrix = XMMatrixTranspose( FinalMatrix ); 117 | 118 | int RealDrawCount=CountOfSprites; 119 | 120 | 121 | // Modify the vertex buffer 122 | D3D11_MAPPED_SUBRESOURCE mapResource; 123 | Engine::GetContext()->Map( VertexBuffer, 0,D3D11_MAP_WRITE_DISCARD, 0, &mapResource ); 124 | { 125 | Tuxis::Vertex::SpriteGroupVertex* pTempVertex=(Tuxis::Vertex::SpriteGroupVertex*)mapResource.pData; 126 | 127 | int BufferCounter=0; 128 | 129 | for(int i=0;iTileCoordinates.at(SpriteBase[i/6].Frame); 138 | 139 | HalfTexSize.x=( (Region->x2 - Region->x1) )/2.0f; 140 | HalfTexSize.y=( (Region->y2 - Region->y1) )/2.0f; 141 | 142 | AbsTextureCoordinate( 143 | Region->x1 / (int)mTiledTextureRegion->mTexture->GetWidth(), 144 | Region->y1 / (int)mTiledTextureRegion->mTexture->GetHeight(), 145 | Region->x2 / (int)mTiledTextureRegion->mTexture->GetWidth(), 146 | Region->y2 / (int)mTiledTextureRegion->mTexture->GetHeight() 147 | ); 148 | 149 | float xPos=SpriteBase[i/6].Position.x; 150 | float yPos=SpriteBase[i/6].Position.y; 151 | 152 | // Vertex Position. 153 | pTempVertex[BufferCounter + 0].Position = XMFLOAT3( -HalfTexSize.x, -HalfTexSize.y, 0.0f ); 154 | pTempVertex[BufferCounter + 1].Position = XMFLOAT3( HalfTexSize.x, HalfTexSize.y, 0.0f ); 155 | pTempVertex[BufferCounter + 2].Position = XMFLOAT3( -HalfTexSize.x, HalfTexSize.y, 0.0f ); 156 | 157 | pTempVertex[BufferCounter + 3].Position = XMFLOAT3( HalfTexSize.x, -HalfTexSize.y, 0.0f ); 158 | pTempVertex[BufferCounter + 4].Position = XMFLOAT3( HalfTexSize.x, HalfTexSize.y, 0.0f ); 159 | pTempVertex[BufferCounter + 5].Position = XMFLOAT3( -HalfTexSize.x, -HalfTexSize.y, 0.0f ); 160 | 161 | // Texture Position. 162 | pTempVertex[BufferCounter + 0].TexCoord 163 | = XMFLOAT2( AbsTextureCoordinate.x1, AbsTextureCoordinate.y1 ); 164 | pTempVertex[BufferCounter + 1].TexCoord 165 | = XMFLOAT2( AbsTextureCoordinate.y2, AbsTextureCoordinate.y2 ); 166 | pTempVertex[BufferCounter + 2].TexCoord 167 | = XMFLOAT2( AbsTextureCoordinate.x1, AbsTextureCoordinate.y2 ); 168 | 169 | pTempVertex[BufferCounter + 3].TexCoord 170 | = XMFLOAT2( AbsTextureCoordinate.x2, AbsTextureCoordinate.y1 ); 171 | pTempVertex[BufferCounter + 4].TexCoord 172 | = XMFLOAT2( AbsTextureCoordinate.x2, AbsTextureCoordinate.y2 ); 173 | pTempVertex[BufferCounter + 5].TexCoord 174 | = XMFLOAT2( AbsTextureCoordinate.x1, AbsTextureCoordinate.y1 ); 175 | 176 | 177 | XMFLOAT4 CurrentColor=SpriteBase[i/6].Color; 178 | 179 | pTempVertex[BufferCounter + 0].Color = CurrentColor; 180 | pTempVertex[BufferCounter + 1].Color = CurrentColor; 181 | pTempVertex[BufferCounter + 2].Color = CurrentColor; 182 | pTempVertex[BufferCounter + 3].Color = CurrentColor; 183 | pTempVertex[BufferCounter + 4].Color = CurrentColor; 184 | pTempVertex[BufferCounter + 5].Color = CurrentColor; 185 | 186 | float Rotation=SpriteBase[i/6].Rotation; 187 | 188 | pTempVertex[BufferCounter + 0].Rotation = XMFLOAT3( xPos , yPos , Rotation ); 189 | pTempVertex[BufferCounter + 1].Rotation = XMFLOAT3( xPos , yPos , Rotation ); 190 | pTempVertex[BufferCounter + 2].Rotation = XMFLOAT3( xPos , yPos , Rotation ); 191 | pTempVertex[BufferCounter + 3].Rotation = XMFLOAT3( xPos , yPos , Rotation ); 192 | pTempVertex[BufferCounter + 4].Rotation = XMFLOAT3( xPos , yPos , Rotation ); 193 | pTempVertex[BufferCounter + 5].Rotation = XMFLOAT3( xPos , yPos , Rotation ); 194 | 195 | float Scale=SpriteBase[i/6].Scale; 196 | 197 | pTempVertex[BufferCounter + 0].Scale = Scale; 198 | pTempVertex[BufferCounter + 1].Scale = Scale; 199 | pTempVertex[BufferCounter + 2].Scale = Scale; 200 | pTempVertex[BufferCounter + 3].Scale = Scale; 201 | pTempVertex[BufferCounter + 4].Scale = Scale; 202 | pTempVertex[BufferCounter + 5].Scale = Scale; 203 | 204 | BufferCounter+=6; 205 | } 206 | } 207 | Engine::GetContext()->Unmap( VertexBuffer,0); 208 | 209 | //FinalMatrix=XMMatrixTranspose(FinalMatrix); 210 | 211 | // Context configuration 212 | Engine::GetContext()->IASetVertexBuffers( 0, 1, &VertexBuffer, &VB_Stride, &VB_Offset ); 213 | Engine::GetContext()->VSSetShader( mShader->VertexShader, 0, 0 ); 214 | Engine::GetContext()->PSSetShader( mShader->PixelShader , 0, 0 ); 215 | 216 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, &FinalMatrix, 0, 0 ); 217 | Engine::GetContext()->VSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 218 | 219 | Engine::GetContext()->PSSetShaderResources( 0, 1, mTiledTextureRegion->mTexture->GetShaderResourceView() ); 220 | Engine::GetContext()->PSSetSamplers( 0, 1, mTiledTextureRegion->mTexture->GetSamplerState() ); 221 | Engine::GetContext()->IASetInputLayout( mShader->InputLayout ); 222 | Engine::GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); 223 | 224 | // Drawing 225 | Engine::GetContext()->Draw(6*RealDrawCount,0); 226 | } 227 | 228 | 229 | void TiledSpriteGroup::SetPosition2D( float PositionX, float PositionY ) 230 | { 231 | TranslationMatrix = XMMatrixTranslation(PositionX, PositionY, 0.0f ); 232 | } 233 | 234 | 235 | void TiledSpriteGroup::Animate(int pTimeMillisecond) 236 | { 237 | if(pTimeMillisecond>0) 238 | mFrameTime=pTimeMillisecond; 239 | else 240 | mFrameTime=0; 241 | } 242 | 243 | BasicSprite* TiledSpriteGroup::GetSprite(int i) 244 | { 245 | if(SpriteBase) 246 | if( i>=CountOfSprites || i<0 ) 247 | { 248 | Log::Error("SpriteGroup::GetSprite - parameter is out of range"); 249 | return 0; 250 | } 251 | else 252 | { 253 | return &SpriteBase[i]; 254 | } 255 | else 256 | { 257 | Log::Error("SpriteGroup::GetSprite - group is empty"); 258 | return 0; 259 | } 260 | } 261 | 262 | void TiledSpriteGroup::Release() 263 | { 264 | 265 | CountOfSprites=0; 266 | if(SpriteBase) 267 | delete[] SpriteBase; 268 | SpriteBase=0; 269 | 270 | mShader = 0; 271 | ReleaseDrawable(); 272 | } 273 | } -------------------------------------------------------------------------------- /Sources/Text.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Text.h" 3 | 4 | 5 | namespace Tuxis 6 | { 7 | Text::Text() 8 | { 9 | mChanged=true; 10 | mFont=nullptr; 11 | VB_Stride = sizeof( Tuxis::Vertex::SpriteVertex ); 12 | VB_Offset = 0; 13 | 14 | InitDrawable(); 15 | 16 | MaxLineCount = 10; 17 | mLineSpacing = 28; 18 | 19 | hAlign = HLEFT; 20 | vAlign = VTOP; 21 | 22 | Position.x=0; 23 | Position.y=0; 24 | 25 | hAlignOffset=0.0f; 26 | vAlignOffset=0.0f; 27 | 28 | Initialize(); 29 | } 30 | 31 | Text::~Text() 32 | { 33 | Release(); 34 | } 35 | 36 | void Text::Initialize() 37 | { 38 | mShader = DefaultShaderBase::Instance()->mShaderText; 39 | CreateConstantBuffer(sizeof(XMMATRIX),D3D11_USAGE_DEFAULT); 40 | SavedStrings.clear(); 41 | } 42 | 43 | void Text::Update() 44 | { 45 | 46 | } 47 | 48 | void Text::SetFont(BitmapFont* pFont) 49 | { 50 | mChanged=true; 51 | mFont=pFont; 52 | mLineSpacing = mFont->GetCharset()->LineHeight; 53 | 54 | D3D11_BUFFER_DESC VertexBufferDesc; 55 | ZeroMemory( &VertexBufferDesc, sizeof( D3D11_BUFFER_DESC ) ); 56 | VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 57 | VertexBufferDesc.ByteWidth = sizeof( Tuxis::Vertex::SpriteVertex ) *6*TEXT_MAX_CHARS; 58 | VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 59 | VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 60 | 61 | D3D11_SUBRESOURCE_DATA VertexBufferData; 62 | ZeroMemory( &VertexBufferData, sizeof( D3D11_SUBRESOURCE_DATA ) ); 63 | VertexBufferData.pSysMem = vertices; 64 | VertexBufferData.SysMemPitch = sizeof( vertices ); 65 | VertexBufferData.SysMemSlicePitch = sizeof( vertices ); 66 | 67 | hr = Engine::GetDevice()->CreateBuffer( &VertexBufferDesc, &VertexBufferData, &VertexBuffer ); 68 | if(FAILED(hr)) Log::Error("CreateBuffer vertexBufferDesc"); 69 | 70 | 71 | } 72 | 73 | void Text::SetText(const WCHAR* parText) 74 | { 75 | wstring inText(parText); 76 | SavedStrings.clear(); 77 | 78 | if(!inText.empty()) 79 | { 80 | int s=0,e=0,l=0; 81 | 82 | while(e!=string::npos) 83 | { 84 | e=inText.find(L'\n',e+1); 85 | 86 | if(e==string::npos) 87 | { 88 | l = inText.size()-s; 89 | SavedStrings.push_back(inText.substr(s,l)); 90 | } 91 | else 92 | { 93 | l = (e-s); 94 | SavedStrings.push_back(inText.substr(s,l)); 95 | s=e+1; 96 | } 97 | } 98 | } 99 | 100 | 101 | mChanged=true; 102 | } 103 | 104 | void Text::SetPosition(float xpos, float ypos) 105 | { 106 | mChanged=true; 107 | 108 | Position.x=xpos; 109 | Position.y=ypos; 110 | } 111 | 112 | void Text::SetHorizontalAlign(HorizontalAlign hAlign) 113 | { 114 | mChanged=true; 115 | this->hAlign=hAlign; 116 | } 117 | 118 | void Text::SetVerticalAlign( VerticalAlign vAlign ) 119 | { 120 | mChanged=true; 121 | this->vAlign=vAlign; 122 | } 123 | 124 | void Text::CalculateHorizontalAlignOffset(int LineNum) 125 | { 126 | if(mFont==nullptr) 127 | { 128 | Log::Error("Text class: you must ::SetFont(...) before."); 129 | return; 130 | } 131 | sCharset* mCharset=mFont->GetCharset(); 132 | 133 | // Allignment offset calculations 134 | hAlignOffset=0.0f; 135 | int token; // Symbol 136 | 137 | int strLength; 138 | if(!SavedStrings.empty()) 139 | { 140 | strLength = SavedStrings.at(LineNum).length(); 141 | 142 | for( int i = 0; i < strLength; i++ ) 143 | { 144 | token = ( int )SavedStrings.at(LineNum).at( i ); 145 | hAlignOffset += ( float )mCharset->Chars[token].Width; 146 | } 147 | 148 | } 149 | else 150 | { 151 | strLength = 0; 152 | } 153 | 154 | 155 | 156 | switch(hAlign) 157 | { 158 | case HLEFT: 159 | hAlignOffset=0; 160 | break; 161 | 162 | case HCENTER: 163 | hAlignOffset=-hAlignOffset/2.0f; 164 | break; 165 | 166 | case HRIGHT: 167 | hAlignOffset=-hAlignOffset; 168 | break; 169 | 170 | } 171 | 172 | 173 | } 174 | 175 | void Text::CalculateVerticalAlignOffset() 176 | { 177 | if(mFont==nullptr) 178 | { 179 | Log::Error("Text class: you must ::SetFont(...) before."); 180 | return; 181 | } 182 | sCharset* mCharset=mFont->GetCharset(); 183 | float vSize= (float)((int)SavedStrings.size()*(int)mCharset->LineHeight); 184 | 185 | switch(vAlign) 186 | { 187 | case VTOP: 188 | vAlignOffset = 0; 189 | break; 190 | 191 | case VCENTER: 192 | vAlignOffset = -(vSize/2.0f); 193 | break; 194 | 195 | case VBOTTOM: 196 | vAlignOffset = -vSize; 197 | break; 198 | 199 | } 200 | 201 | } 202 | 203 | 204 | 205 | 206 | void Text::Draw() 207 | { 208 | 209 | if(!mVisible) return; 210 | 211 | if(mFont) 212 | { 213 | 214 | CalculateVerticalAlignOffset(); 215 | int DataSize=0; 216 | 217 | // String parser 218 | 219 | 220 | 221 | 222 | // Vertex Buffer Modification 223 | D3D11_MAPPED_SUBRESOURCE mapResource; 224 | hr = Engine::GetContext()->Map( VertexBuffer, 0,D3D11_MAP_WRITE_DISCARD, 0, &mapResource ); 225 | if(FAILED(hr)) 226 | Log::Error("Context->Map"); 227 | { 228 | Tuxis::Vertex::SpriteVertex* pTempVertex=(Tuxis::Vertex::SpriteVertex*)mapResource.pData; 229 | 230 | // Loop by text lines 231 | for(int i=0;i<(int)SavedStrings.size();i++) 232 | { 233 | CalculateHorizontalAlignOffset(i); 234 | WriteStringToConstantBuffer(pTempVertex,DataSize,SavedStrings.at(i),i); 235 | DataSize += SavedStrings.at(i).size(); 236 | } 237 | 238 | 239 | 240 | 241 | } 242 | Engine::GetContext()->Unmap( VertexBuffer,0); 243 | TranslationMatrix = XMMatrixTranslation( Position.x, Position.y, 0.0f ); 244 | 245 | 246 | 247 | DrawVertexBuffer(6 * DataSize); 248 | } 249 | } 250 | 251 | 252 | 253 | 254 | 255 | void Text::Release() 256 | { 257 | mShader = 0; 258 | ReleaseDrawable(); 259 | } 260 | 261 | 262 | void Text::SetMaxLineCount( int count ) 263 | { 264 | if(count>0) 265 | MaxLineCount = count; 266 | else 267 | throw L"Text::SetLineMaxCount: invalid argument"; 268 | } 269 | 270 | void Text::SetLineSpacing( float space ) 271 | { 272 | mLineSpacing = space; 273 | } 274 | 275 | void Text::WriteStringToConstantBuffer(Vertex::SpriteVertex* pTempVertex,int prOffset,wstring prString,int LineNum) 276 | { 277 | TextureAtlas *mTexture=mFont->GetTextureAtlas(); 278 | float TexW = ( float )mTexture->GetWidth(); 279 | float TexH = ( float )mTexture->GetHeight(); 280 | 281 | sCharset* mCharset=mFont->GetCharset(); 282 | 283 | int token; 284 | int index; 285 | 286 | float xOffset= 0.0f, xsOffset= 0.0f, yOffset, ysOffset; 287 | 288 | for( int i = 0; i < (int)prString.length(); i++ ) 289 | { 290 | token = ( int )prString.at( i ); 291 | 292 | yOffset = ( float )mCharset->Chars[token].YOffset; 293 | xsOffset = xOffset+hAlignOffset; 294 | ysOffset = yOffset+vAlignOffset+LineNum*mLineSpacing; 295 | 296 | index=(prOffset+i)*6; 297 | 298 | // Vertex Position. 299 | { 300 | // First triangle 301 | pTempVertex[index + 0].Position 302 | = XMFLOAT3( xsOffset, ysOffset + ( float )mCharset->Chars[token].Height, 0.0f ); 303 | 304 | pTempVertex[index + 1].Position 305 | = XMFLOAT3( xsOffset, ysOffset, 0.0f ); 306 | 307 | pTempVertex[index + 2].Position 308 | = XMFLOAT3( xsOffset + ( float )mCharset->Chars[token].Width, ysOffset, 0.0f ); 309 | 310 | // Second triangle 311 | pTempVertex[index + 3].Position 312 | = XMFLOAT3( xsOffset, ysOffset + ( float )mCharset->Chars[token].Height, 0.0f ); 313 | 314 | pTempVertex[index + 4].Position 315 | = XMFLOAT3( xsOffset + ( float )mCharset->Chars[token].Width, ysOffset, 0.0f ); 316 | 317 | pTempVertex[index + 5].Position 318 | = XMFLOAT3( xsOffset + ( float )mCharset->Chars[token].Width, ysOffset + ( float )mCharset->Chars[token].Height, 0.0f ); 319 | } 320 | 321 | // Texture Position. 322 | { 323 | // First triangle 324 | pTempVertex[index + 0].TexCoord = 325 | XMFLOAT2( ( float )mCharset->Chars[token].x / TexW, ( float )( mCharset->Chars[token].y + mCharset->Chars[token].Height ) / TexH ); 326 | pTempVertex[index + 1].TexCoord = 327 | XMFLOAT2( ( float )mCharset->Chars[token].x / TexW, ( float )mCharset->Chars[token].y / TexH ); 328 | pTempVertex[index + 2].TexCoord = 329 | XMFLOAT2( ( float )( mCharset->Chars[token].x + mCharset->Chars[token].Width ) / TexW, ( float )( mCharset->Chars[token].y ) / TexH ); 330 | 331 | // Second triangle 332 | pTempVertex[index + 3].TexCoord = 333 | XMFLOAT2( ( float )mCharset->Chars[token].x / TexW, ( float )( mCharset->Chars[token].y + mCharset->Chars[token].Height ) / TexH ); 334 | pTempVertex[index + 4].TexCoord = 335 | XMFLOAT2( ( float )( mCharset->Chars[token].x + mCharset->Chars[token].Width ) / TexW, ( float )( mCharset->Chars[token].y ) / TexH ); 336 | pTempVertex[index + 5].TexCoord = 337 | XMFLOAT2( ( float )( mCharset->Chars[token].x + mCharset->Chars[token].Width ) / TexW, ( float )( mCharset->Chars[token].y + mCharset->Chars[token].Height ) / TexH ); 338 | } 339 | 340 | // Vertex Color Modulation 341 | { 342 | pTempVertex[index + 0].Color=ColorModulation; 343 | pTempVertex[index + 1].Color=ColorModulation; 344 | pTempVertex[index + 2].Color=ColorModulation; 345 | pTempVertex[index + 3].Color=ColorModulation; 346 | pTempVertex[index + 4].Color=ColorModulation; 347 | pTempVertex[index + 5].Color=ColorModulation; 348 | } 349 | 350 | xOffset += ( float )mCharset->Chars[token].Width;// + 2.0f; 351 | } 352 | } 353 | 354 | 355 | void Text::DrawVertexBuffer( int prVertexCount ) 356 | { 357 | UINT stride = sizeof( Tuxis::Vertex::SpriteVertex ); 358 | UINT offset = 0; 359 | Engine::GetContext()->IASetVertexBuffers( 0, 1, &VertexBuffer, &stride, &offset ); 360 | XMMATRIX WorldMatrix = CalculateWorldMatrix(); 361 | XMMATRIX FinalMatrix = WorldMatrix * (Engine::GetInstance()->GetActive2DCamera()->GetFinalMatrix()) ; 362 | FinalMatrix = XMMatrixTranspose( FinalMatrix ); 363 | 364 | Engine::GetContext()->VSSetShader( mShader->VertexShader, 0, 0 ); 365 | Engine::GetContext()->PSSetShader( mShader->PixelShader , 0, 0 ); 366 | 367 | Engine::GetContext()->UpdateSubresource( ShaderConstantBuffer, 0, NULL, &FinalMatrix, 0, 0 ); 368 | Engine::GetContext()->VSSetConstantBuffers( 0, 1, &ShaderConstantBuffer ); 369 | 370 | Engine::GetContext()->PSSetShaderResources( 0, 1, mFont->GetTextureAtlas()->GetShaderResourceView() ); 371 | Engine::GetContext()->PSSetSamplers( 0, 1, mFont->GetTextureAtlas()->GetSamplerState() ); 372 | Engine::GetContext()->IASetInputLayout( mShader->InputLayout ); 373 | Engine::GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); 374 | 375 | Graphics::Instance()->DisableStencilBuffer(); 376 | Engine::GetContext()->Draw( prVertexCount, 0 ); 377 | } 378 | 379 | 380 | 381 | } -------------------------------------------------------------------------------- /Sources/Graphics.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Graphics.h" 3 | 4 | namespace Tuxis 5 | { 6 | bool Graphics::mReady=false; 7 | Graphics* Graphics::mInstance=nullptr; 8 | 9 | Graphics::Graphics() 10 | { 11 | 12 | if(mInstance == nullptr) 13 | { 14 | mInstance = this; 15 | } 16 | else 17 | Log::Warning("Graphics::Graphics object already created."); 18 | 19 | ZeroInit(); 20 | 21 | 22 | pDSState=0; 23 | FeatureLevel=D3D_FEATURE_LEVEL_10_0; 24 | 25 | mClearColor(0.5f,0.5f,0.9f,1.0f); 26 | 27 | mReady = false; 28 | } 29 | 30 | bool Graphics::isReady() 31 | { 32 | return mReady; 33 | } 34 | 35 | Graphics::~Graphics() 36 | { 37 | mInstance = nullptr; 38 | mReady = false; 39 | } 40 | 41 | Graphics* Graphics::Instance() 42 | { 43 | if(mInstance == nullptr) 44 | { 45 | Log::Error("Graphics::Instance: you must create object before."); 46 | return nullptr; 47 | } 48 | else 49 | return mInstance; 50 | } 51 | 52 | 53 | 54 | bool Graphics::Initialize( Window* pWindow, bool windowed,bool vsync ) 55 | { 56 | WindowHandler = pWindow->GetHWND(); 57 | Width = pWindow->GetWidth(); 58 | Height = pWindow->GetHeight(); 59 | 60 | Windowed = windowed; 61 | VSync = vsync; 62 | Depth = 32; 63 | 64 | hr = 0; 65 | FPS = 0; 66 | 67 | mReady=InitializeDirectX(); 68 | return mReady; 69 | } 70 | 71 | ID3D11Device* Graphics::GetDevice() 72 | { 73 | return Device; 74 | } 75 | 76 | ID3D11DeviceContext* Graphics::GetContext() 77 | { 78 | return Context; 79 | } 80 | 81 | int Graphics::GetWidth() 82 | { 83 | return Width; 84 | } 85 | 86 | int Graphics::GetHeight() 87 | { 88 | return Height; 89 | } 90 | 91 | void Graphics::Clear() 92 | { 93 | Context->ClearRenderTargetView( *CurrentRenderTarget, (float*)&mClearColor ); 94 | if(isDepthStencilView) 95 | Context->ClearDepthStencilView(isDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0); 96 | } 97 | 98 | void Graphics::SetClearColor( Color prColor ) 99 | { 100 | mClearColor=prColor; 101 | } 102 | 103 | void Graphics::SetRenderTarget(ID3D11RenderTargetView** pRenderTarget) 104 | { 105 | if(pRenderTarget==0) 106 | CurrentRenderTarget=&BackBuffer; 107 | else 108 | CurrentRenderTarget=pRenderTarget; 109 | 110 | Context->OMSetRenderTargets( 1, CurrentRenderTarget, isDepthStencilView); 111 | } 112 | 113 | void Graphics::SetWireframeMode(bool pEnable) 114 | { 115 | if(RasterState && WireFrameState) 116 | { 117 | if(pEnable) 118 | { 119 | Context->RSSetState( WireFrameState ); 120 | } 121 | else 122 | { 123 | Context->RSSetState( RasterState ); 124 | } 125 | } 126 | } 127 | 128 | ID3D11DepthStencilView* Graphics::GetDepthStencilView() 129 | { 130 | return DepthStencilView; 131 | } 132 | 133 | void Graphics::EnableStencilBuffer() 134 | { 135 | isDepthStencilView=DepthStencilView; 136 | Context->OMSetRenderTargets( 1, CurrentRenderTarget, isDepthStencilView ); 137 | } 138 | 139 | void Graphics::DisableStencilBuffer() 140 | { 141 | isDepthStencilView=0; 142 | Context->OMSetRenderTargets( 1, CurrentRenderTarget, isDepthStencilView ); 143 | } 144 | 145 | 146 | 147 | bool Graphics::InitializeDirectX() 148 | { 149 | CreateDeviceAndSwapChain(); 150 | CreateBackBuffer(); 151 | 152 | CreateWireframeState(); 153 | CreateBlendState(); 154 | CreateRasterState(); 155 | 156 | CreateDepthStencilBuffer(); 157 | Context->OMSetRenderTargets( 1, &BackBuffer, isDepthStencilView ); 158 | SetViewport( 0.0f, 0.0f, (float)Width, (float)Height ); 159 | 160 | return true; 161 | } 162 | 163 | void Graphics::CreateDepthStencilBuffer() 164 | { 165 | 166 | ID3D11Texture2D* DepthStencilBuffer = NULL; 167 | D3D11_TEXTURE2D_DESC descDepth; 168 | descDepth.Width = Width; 169 | descDepth.Height = Height; 170 | descDepth.MipLevels = 1; 171 | descDepth.ArraySize = 1; 172 | descDepth.Format = DXGI_FORMAT_D32_FLOAT; 173 | descDepth.SampleDesc.Count = 1; 174 | descDepth.SampleDesc.Quality = 0; 175 | descDepth.Usage = D3D11_USAGE_DEFAULT; 176 | descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL; 177 | descDepth.CPUAccessFlags = 0; 178 | descDepth.MiscFlags = 0; 179 | 180 | Device->CreateTexture2D( &descDepth, NULL, &DepthStencilBuffer ); 181 | 182 | // Create the depth stencil view 183 | D3D11_DEPTH_STENCIL_VIEW_DESC descDSV; 184 | descDSV.Format = DXGI_FORMAT_D32_FLOAT; 185 | descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; 186 | descDSV.Texture2D.MipSlice = 0; 187 | 188 | // Create the depth stencil view 189 | hr = Device->CreateDepthStencilView( DepthStencilBuffer, 0, &DepthStencilView ); 190 | if( FAILED(hr) ) Log::Error( "Graphics::CreateDepthStencilBuffer()" ); 191 | isDepthStencilView=DepthStencilView; 192 | 193 | DepthStencilBuffer->Release(); 194 | } 195 | 196 | 197 | void Graphics::CreateDeviceAndSwapChain() 198 | { 199 | DXGI_SWAP_CHAIN_DESC scd; 200 | ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) ); 201 | scd.BufferCount = 1; 202 | scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 203 | scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 204 | scd.OutputWindow = WindowHandler; 205 | scd.SampleDesc.Count = 1; 206 | scd.SampleDesc.Quality = 0; 207 | scd.Windowed = Windowed; 208 | scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; 209 | 210 | int DEVICE_CREATION_MODE; 211 | 212 | #ifdef _DEBUG 213 | DEVICE_CREATION_MODE=D3D11_CREATE_DEVICE_DEBUG; 214 | #else 215 | DEVICE_CREATION_MODE=D3D11_CREATE_DEVICE_SINGLETHREADED; 216 | #endif 217 | 218 | hr = D3D11CreateDeviceAndSwapChain( NULL, 219 | D3D_DRIVER_TYPE_HARDWARE, 220 | NULL, 221 | DEVICE_CREATION_MODE, 222 | &FeatureLevel, 223 | 1, 224 | D3D11_SDK_VERSION, 225 | &scd, 226 | &SwapChain, 227 | &Device, 228 | NULL, 229 | &Context ); 230 | 231 | 232 | 233 | if( FAILED(hr) ) Log::Error( "Graphics::CreateDeviceAndSwapChain()" ); 234 | } 235 | 236 | 237 | void Graphics::SetViewport( float pX, float pY, float pWidth, float pHeight ) 238 | { 239 | ViewPort.Width = pWidth; 240 | ViewPort.Height = pHeight; 241 | ViewPort.MinDepth = 0.0f; 242 | ViewPort.MaxDepth = 1.0f; 243 | ViewPort.TopLeftX = pX; 244 | ViewPort.TopLeftY = pY; 245 | Context->RSSetViewports( 1, &ViewPort ); 246 | } 247 | 248 | 249 | void Graphics::CreateBackBuffer() 250 | { 251 | ID3D11Texture2D *pBackBuffer; 252 | 253 | hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID * )&pBackBuffer ); 254 | if( FAILED(hr) ) Log::Error( "Graphics::CreateBackBuffer: SwapChain->GetBuffer" ); 255 | 256 | hr = Device->CreateRenderTargetView( pBackBuffer, NULL, &BackBuffer ); 257 | if( FAILED(hr) ) Log::Error( "Graphics::CreateBackBuffer: Device->CreateRenderTargetView" ); 258 | 259 | _RELEASE(pBackBuffer) 260 | 261 | CurrentRenderTarget = &BackBuffer; 262 | } 263 | 264 | 265 | void Graphics::CreateBlendState() 266 | { 267 | D3D11_BLEND_DESC blendDesc; 268 | ZeroMemory( &blendDesc, sizeof( blendDesc ) ); 269 | 270 | blendDesc.RenderTarget[0].BlendEnable = TRUE; 271 | blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; 272 | blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; 273 | blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; 274 | blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; 275 | blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; 276 | blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;//D3D11_BLEND_ZERO; 277 | blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; 278 | 279 | 280 | hr = Device->CreateBlendState( &blendDesc, &BlendState ); 281 | if( FAILED(hr) ) Log::Error( "Graphics::CreateBlendState: Device->CreateBlendState" ); 282 | 283 | float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 284 | Context->OMSetBlendState( BlendState, blendFactor, 0xFFFFFFFF ); 285 | } 286 | 287 | 288 | void Graphics::CreateWireframeState() 289 | { 290 | D3D11_RASTERIZER_DESC RasterStateDesc; 291 | ZeroMemory(&RasterStateDesc, sizeof(D3D11_RASTERIZER_DESC)); 292 | 293 | RasterStateDesc.FillMode = D3D11_FILL_WIREFRAME; 294 | RasterStateDesc.CullMode = D3D11_CULL_NONE; 295 | 296 | hr = Device->CreateRasterizerState(&RasterStateDesc, &WireFrameState); 297 | if( FAILED(hr) ) Log::Error( "Graphics::CreateWireframeState: Device->CreateRasterizerState" ); 298 | } 299 | 300 | 301 | void Graphics::CreateRasterState() 302 | { 303 | D3D11_RASTERIZER_DESC RasterStateDesc; 304 | RasterStateDesc.FillMode = D3D11_FILL_SOLID; 305 | RasterStateDesc.CullMode = D3D11_CULL_BACK; 306 | 307 | RasterStateDesc.FrontCounterClockwise = false; 308 | 309 | RasterStateDesc.DepthBias = D3D11_DEFAULT_DEPTH_BIAS; 310 | RasterStateDesc.DepthBiasClamp = D3D11_DEFAULT_DEPTH_BIAS_CLAMP; 311 | RasterStateDesc.SlopeScaledDepthBias = D3D11_DEFAULT_SLOPE_SCALED_DEPTH_BIAS; 312 | RasterStateDesc.DepthClipEnable = true; 313 | 314 | RasterStateDesc.ScissorEnable = false; 315 | RasterStateDesc.MultisampleEnable = false; //true; 316 | RasterStateDesc.AntialiasedLineEnable = false; //true; 317 | 318 | hr = Device->CreateRasterizerState( &RasterStateDesc , &RasterState ); 319 | if( FAILED(hr) ) Log::Error( "Graphics::CreateRasterState: Device->CreateRasterizerState" ); 320 | 321 | Context->RSSetState( RasterState ); 322 | } 323 | 324 | int Graphics::GetFPS() 325 | { 326 | return FPS; 327 | } 328 | 329 | 330 | void Graphics::Flip() 331 | { 332 | hr = SwapChain->Present( VSync, 0 ); 333 | CalculateFrameRate(); 334 | if( FAILED( hr ) ) Log::Error( "SwapChain->Present( 0, 0 )" ); 335 | 336 | 337 | #ifdef _DEBUG 338 | ID3D11ShaderResourceView *const pSRV[1] = {NULL}; 339 | Context->PSSetShaderResources(0, 1, pSRV); 340 | #endif 341 | 342 | } 343 | 344 | 345 | void Graphics::CalculateFrameRate() 346 | { 347 | static float framesPerSecond = 0.0f; 348 | static float lastTime = 0.0f; 349 | 350 | float currentTime = timeGetTime() * 0.001f; 351 | 352 | ++framesPerSecond; 353 | 354 | if( currentTime - lastTime > 1.0f ) 355 | { 356 | lastTime = currentTime; 357 | FPS = ( int )framesPerSecond; 358 | framesPerSecond = 0; 359 | } 360 | 361 | } 362 | 363 | 364 | void Graphics::ZeroInit() 365 | { 366 | Device = 0; 367 | Context = 0; 368 | SwapChain = 0; 369 | BackBuffer = 0; 370 | BlendState = 0; 371 | RasterState = 0; 372 | WireFrameState = 0; 373 | DepthStencilView = 0; 374 | isDepthStencilView = 0; 375 | } 376 | 377 | 378 | void Graphics::Release() 379 | { 380 | hr = SwapChain->SetFullscreenState( FALSE, NULL ); 381 | if( FAILED( hr ) ) Log::Error( "SwapChain->SetFullscreenState" ); 382 | 383 | _RELEASE(BackBuffer) 384 | _RELEASE(DepthStencilView) 385 | _RELEASE(BlendState) 386 | _RELEASE(RasterState) 387 | _RELEASE(WireFrameState) 388 | _RELEASE(SwapChain) 389 | _RELEASE(Device) 390 | _RELEASE(Context) 391 | 392 | mReady=false; 393 | } 394 | 395 | 396 | 397 | 398 | } 399 | --------------------------------------------------------------------------------