From 8644e03586ac9c39741f62cbcbce87b18805538c Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 14 Mar 2019 23:12:54 +0800 Subject: =?UTF-8?q?*=E5=A4=A7=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Asura.Engine/graphics/application.graphics.cpp | 19 +++ Source/Asura.Engine/graphics/binding/canvas.cpp | 0 Source/Asura.Engine/graphics/binding/color.cpp | 0 Source/Asura.Engine/graphics/binding/image.cpp | 0 .../Asura.Engine/graphics/binding/image_data.cpp | 0 Source/Asura.Engine/graphics/binding/mesh2d.cpp | 0 .../Asura.Engine/graphics/binding/mesh2d_data.cpp | 0 Source/Asura.Engine/graphics/binding/shader.cpp | 119 +++++++++++++++++++ .../Asura.Engine/graphics/binding/sprite_batch.cpp | 0 Source/Asura.Engine/graphics/binding/window.cpp | 11 ++ Source/Asura.Engine/graphics/blend_mode.h | 17 +++ Source/Asura.Engine/graphics/canvas.cpp | 41 +++++++ Source/Asura.Engine/graphics/canvas.h | 81 +++++++++++++ Source/Asura.Engine/graphics/color.cpp | 132 +++++++++++++++++++++ Source/Asura.Engine/graphics/color.h | 94 +++++++++++++++ Source/Asura.Engine/graphics/gl.cpp | 11 ++ Source/Asura.Engine/graphics/gl.h | 19 +++ Source/Asura.Engine/graphics/image.cpp | 32 +++++ Source/Asura.Engine/graphics/image.h | 78 ++++++++++++ Source/Asura.Engine/graphics/image_data.cpp | 50 ++++++++ Source/Asura.Engine/graphics/image_data.h | 62 ++++++++++ Source/Asura.Engine/graphics/image_decoder.h | 35 ++++++ Source/Asura.Engine/graphics/mesh2d.cpp | 0 Source/Asura.Engine/graphics/mesh2d.h | 29 +++++ Source/Asura.Engine/graphics/pixel_format.h | 91 ++++++++++++++ Source/Asura.Engine/graphics/png_decoder.cpp | 19 +++ Source/Asura.Engine/graphics/png_decoder.h | 27 +++++ Source/Asura.Engine/graphics/quad.cpp | 0 Source/Asura.Engine/graphics/quad.h | 1 + Source/Asura.Engine/graphics/render_state.h | 50 ++++++++ Source/Asura.Engine/graphics/render_target.cpp | 0 Source/Asura.Engine/graphics/render_target.h | 54 +++++++++ Source/Asura.Engine/graphics/shader.cpp | 81 +++++++++++++ Source/Asura.Engine/graphics/shader.h | 123 +++++++++++++++++++ Source/Asura.Engine/graphics/shape.cpp | 0 Source/Asura.Engine/graphics/shape.h | 0 Source/Asura.Engine/graphics/sprite_batch.cpp | 0 Source/Asura.Engine/graphics/sprite_batch.h | 28 +++++ Source/Asura.Engine/graphics/stb_decoder.cpp | 65 ++++++++++ Source/Asura.Engine/graphics/stb_decoder.h | 27 +++++ Source/Asura.Engine/graphics/texture.cpp | 26 ++++ Source/Asura.Engine/graphics/texture.h | 66 +++++++++++ Source/Asura.Engine/graphics/window.cpp | 56 +++++++++ Source/Asura.Engine/graphics/window.h | 85 +++++++++++++ 44 files changed, 1629 insertions(+) create mode 100644 Source/Asura.Engine/graphics/application.graphics.cpp create mode 100644 Source/Asura.Engine/graphics/binding/canvas.cpp create mode 100644 Source/Asura.Engine/graphics/binding/color.cpp create mode 100644 Source/Asura.Engine/graphics/binding/image.cpp create mode 100644 Source/Asura.Engine/graphics/binding/image_data.cpp create mode 100644 Source/Asura.Engine/graphics/binding/mesh2d.cpp create mode 100644 Source/Asura.Engine/graphics/binding/mesh2d_data.cpp create mode 100644 Source/Asura.Engine/graphics/binding/shader.cpp create mode 100644 Source/Asura.Engine/graphics/binding/sprite_batch.cpp create mode 100644 Source/Asura.Engine/graphics/binding/window.cpp create mode 100644 Source/Asura.Engine/graphics/blend_mode.h create mode 100644 Source/Asura.Engine/graphics/canvas.cpp create mode 100644 Source/Asura.Engine/graphics/canvas.h create mode 100644 Source/Asura.Engine/graphics/color.cpp create mode 100644 Source/Asura.Engine/graphics/color.h create mode 100644 Source/Asura.Engine/graphics/gl.cpp create mode 100644 Source/Asura.Engine/graphics/gl.h create mode 100644 Source/Asura.Engine/graphics/image.cpp create mode 100644 Source/Asura.Engine/graphics/image.h create mode 100644 Source/Asura.Engine/graphics/image_data.cpp create mode 100644 Source/Asura.Engine/graphics/image_data.h create mode 100644 Source/Asura.Engine/graphics/image_decoder.h create mode 100644 Source/Asura.Engine/graphics/mesh2d.cpp create mode 100644 Source/Asura.Engine/graphics/mesh2d.h create mode 100644 Source/Asura.Engine/graphics/pixel_format.h create mode 100644 Source/Asura.Engine/graphics/png_decoder.cpp create mode 100644 Source/Asura.Engine/graphics/png_decoder.h create mode 100644 Source/Asura.Engine/graphics/quad.cpp create mode 100644 Source/Asura.Engine/graphics/quad.h create mode 100644 Source/Asura.Engine/graphics/render_state.h create mode 100644 Source/Asura.Engine/graphics/render_target.cpp create mode 100644 Source/Asura.Engine/graphics/render_target.h create mode 100644 Source/Asura.Engine/graphics/shader.cpp create mode 100644 Source/Asura.Engine/graphics/shader.h create mode 100644 Source/Asura.Engine/graphics/shape.cpp create mode 100644 Source/Asura.Engine/graphics/shape.h create mode 100644 Source/Asura.Engine/graphics/sprite_batch.cpp create mode 100644 Source/Asura.Engine/graphics/sprite_batch.h create mode 100644 Source/Asura.Engine/graphics/stb_decoder.cpp create mode 100644 Source/Asura.Engine/graphics/stb_decoder.h create mode 100644 Source/Asura.Engine/graphics/texture.cpp create mode 100644 Source/Asura.Engine/graphics/texture.h create mode 100644 Source/Asura.Engine/graphics/window.cpp create mode 100644 Source/Asura.Engine/graphics/window.h (limited to 'Source/Asura.Engine/graphics') diff --git a/Source/Asura.Engine/graphics/application.graphics.cpp b/Source/Asura.Engine/graphics/application.graphics.cpp new file mode 100644 index 0000000..4d3102f --- /dev/null +++ b/Source/Asura.Engine/graphics/application.graphics.cpp @@ -0,0 +1,19 @@ +#include "Application.h" +#include "Sdl2/SDL.h" +#include "Exceptions/Exception.h" + +namespace AsuraEngine +{ + + /// + /// 初始化graphics模块 + /// + bool Application::InitGraphics() + { + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + return false; + + } + +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/binding/canvas.cpp b/Source/Asura.Engine/graphics/binding/canvas.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/color.cpp b/Source/Asura.Engine/graphics/binding/color.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/image.cpp b/Source/Asura.Engine/graphics/binding/image.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/image_data.cpp b/Source/Asura.Engine/graphics/binding/image_data.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/mesh2d.cpp b/Source/Asura.Engine/graphics/binding/mesh2d.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/mesh2d_data.cpp b/Source/Asura.Engine/graphics/binding/mesh2d_data.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/shader.cpp b/Source/Asura.Engine/graphics/binding/shader.cpp new file mode 100644 index 0000000..27ed856 --- /dev/null +++ b/Source/Asura.Engine/graphics/binding/shader.cpp @@ -0,0 +1,119 @@ +#include "../Shader.h" + +using namespace Luax; + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 将此shader设置为活动。 + /// + int Shader::l_Use(lua_State* L) + { + LUAX_STATE(L); + + } + + /// + /// 将此shader设置为非活动。 + /// + int Shader::l_Unuse(lua_State* L) + { + LUAX_STATE(L); + + } + + /// + /// 从着色器代码中构建shader程序。如果成功返回true,失败返回false。 + /// + int Shader::l_Load(lua_State* L) + { + LUAX_STATE(L); + + } + + /// + /// 判断shader是否有这个uniform,如果有返回true,否则返回false + /// + int Shader::l_HasUniform(lua_State* L) + { + LUAX_STATE(L); + + } + + /// + /// 后的uniforms的location,如果没有这个uniform,返回nil,否则返回对应的loc + /// + int Shader::l_GetUniformLocation(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetBuiltInUniforms(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetUniformFloat(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetUniformTexture(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetUniformVector2(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetUniformVector3(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetUniformVector4(lua_State* L) + { + LUAX_STATE(L); + + } + + int Shader::l_SetUniformColor(lua_State* L) + { + LUAX_STATE(L); + + } + + void Shader::RegisterLuaxClass(LuaxState& state) + { + + } + + void Shader::RegisterLuaxInterface(LuaxState& state) + { + + } + + void Shader::RegisterLuaxClass(LuaxState& state) + { + + LuaxEnum EBuiltIn[] = { + {0, 0} + }; + + state.RegisterEnum("EBuiltIn", EBuiltIn); + + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/binding/sprite_batch.cpp b/Source/Asura.Engine/graphics/binding/sprite_batch.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/binding/window.cpp b/Source/Asura.Engine/graphics/binding/window.cpp new file mode 100644 index 0000000..3befc8c --- /dev/null +++ b/Source/Asura.Engine/graphics/binding/window.cpp @@ -0,0 +1,11 @@ +#include "../Window.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + + + } +} diff --git a/Source/Asura.Engine/graphics/blend_mode.h b/Source/Asura.Engine/graphics/blend_mode.h new file mode 100644 index 0000000..775cc45 --- /dev/null +++ b/Source/Asura.Engine/graphics/blend_mode.h @@ -0,0 +1,17 @@ +#ifndef __ASURA_ENGINE_BLEND_MODE_H__ +#define __ASURA_ENGINE_BLEND_MODE_H__ + +namespace AsuraEngine +{ + namespace Graphics + { + + enum BlendMode + { + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/canvas.cpp b/Source/Asura.Engine/graphics/canvas.cpp new file mode 100644 index 0000000..61787b6 --- /dev/null +++ b/Source/Asura.Engine/graphics/canvas.cpp @@ -0,0 +1,41 @@ +#include "Canvas.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + Canvas::Canvas() + : Texture() + , mWidth(0) + , mHeight(0) + { + glGenFramebuffers(1, &mFBO); + GLint current_fbo; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo); + glBindFramebuffer(GL_FRAMEBUFFER, mFBO); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureHandle, 0); + glBindFramebuffer(GL_FRAMEBUFFER, current_fbo); + } + + void Canvas::SetSize(uint w, uint h) + { + GLint current_tex; + glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_tex); + glBindTexture(GL_TEXTURE_2D, mTextureHandle); + + glBindTexture(GL_TEXTURE_2D, current_tex); + } + + void Canvas::Bind() + { + + } + + void Canvas::Unbind() + { + + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/canvas.h b/Source/Asura.Engine/graphics/canvas.h new file mode 100644 index 0000000..c4e0f65 --- /dev/null +++ b/Source/Asura.Engine/graphics/canvas.h @@ -0,0 +1,81 @@ +#ifndef __ASURA_ENGINE_CANVAS_H__ +#define __ASURA_ENGINE_CANVAS_H__ + +#include + +#include "Math/Rect.hpp" +#include "GL.h" +#include "Texture.h" +#include "RenderTarget.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// Canvas也可以称为render texture,自身也可以作为texture渲染。 + /// + class Canvas ASURA_FINAL + : public Drawable + , public RenderTarget + , public Scripting::Portable + { + public: + + Canvas(); + + ~Canvas(); + + /// + /// 设置render texture的大小 + /// + void SetSize(uint w, uint h); + + void Clear(const Color& col = Color::Black) override; + + void Clear(const Math::Recti& quad, const Color& col = Color::Black) override; + + void Render(const RenderTarget* rt, const Math::Vector2i& pos, const Math::Vector2i& scale, const Math::Vector2i& center, float rot); + + void Render(const RenderTarget* rt, const Math::Rectf& quad, const Math::Vector2i& pos, const Math::Vector2i& scale, const Math::Vector2i& center, float rot); + + void Draw(const Drawable* texture, const RenderState& state); + + void Draw(const Drawable* texture, const Math::Recti& quad, const RenderState& state); + + private: + + /// + /// Frame buffer object id. + /// + GLuint mFBO; + + /// + /// canvas的大小 + /// + uint mWidth, mHeight; + + public: + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(SimCanvas); + + LUAX_DECL_METHOD(l_SetSize); + LUAX_DECL_METHOD(l_Bind); + LUAX_DECL_METHOD(l_Unbind); + + //---------------------------------------------------------------------------------------------------------- + + }; + + /// + /// Canvas别名为RenderTexture + /// + using RenderTexture = Canvas; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/color.cpp b/Source/Asura.Engine/graphics/color.cpp new file mode 100644 index 0000000..106493d --- /dev/null +++ b/Source/Asura.Engine/graphics/color.cpp @@ -0,0 +1,132 @@ +#include "Color.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + Color32::Color32() + { + r = g = b = a = 0; + } + + Color32::Color32(const Color32& c) + { + r = c.r; + g = c.g; + b = c.b; + a = c.a; + } + + Color32::Color32(const Color& c) + { + r = 255.f * c.r; + g = 255.f * c.g; + b = 255.f * c.b; + a = 255.f * c.a; + } + + Color32::Color32(byte r, byte g, byte b, byte a) + { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } + + int Color32::l_GetRed(lua_State* L) + { + + } + + int Color32::l_GetGreen(lua_State* L) + { + + } + + int Color32::l_GetBlue(lua_State* L) + { + + } + + int Color32::l_GetAlpha(lua_State* L) + { + + } + + //------------------------------------------------------------------------------------------------------------ + + Color::Color() + { + r = g = b = a = 0; + } + + Color::Color(const Color& c) + { + r = c.r; + g = c.g; + b = c.b; + a = c.a; + } + + Color::Color(float r, float g, float b, float a) + { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } + + Color::Color(const Color32& c) + { + r = c.r / 255.f; + g = c.g / 255.f; + b = c.b / 255.f; + a = c.a / 255.f; + } + + Color Color::operator *(const Color& c) + { + r *= c.r; + g *= c.g; + b *= c.b; + a *= c.a; + } + + int Color::l_GetRed(lua_State* L) + { + + } + + int Color::l_GetGreen(lua_State* L) + { + + } + + int Color::l_GetBlue(lua_State* L) + { + + } + + int Color::l_GetAlpha(lua_State* L) + { + + } + + int Color::l_Add(lua_State* L) + { + + } + + int Color::l_Minus(lua_State* L) + { + + } + + int Color::l_Multiply(lua_State* L) + { + + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/color.h b/Source/Asura.Engine/graphics/color.h new file mode 100644 index 0000000..0d65cb1 --- /dev/null +++ b/Source/Asura.Engine/graphics/color.h @@ -0,0 +1,94 @@ +#ifndef __ASURA_ENGINE_COLOR_H__ +#define __ASURA_ENGINE_COLOR_H__ + +#include "Scripting/Luax.hpp" +#include "Scripting/Portable.h" +#include "Config.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class Color; + + /// + /// 32bits颜色 + /// + class Color32 ASURA_FINAL: public Scripting::Portable + { + public: + + Color32(); + + ~Color32(); + + Color32(const Color32& c); + + Color32(const Color& c); + + Color32(byte r, byte g, byte b, byte a); + + byte r, g, b, a; + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(Color32); + + LUAX_DECL_METHOD(l_ToColor); + LUAX_DECL_METHOD(l_GetRed); + LUAX_DECL_METHOD(l_GetGreen); + LUAX_DECL_METHOD(l_GetBlue); + LUAX_DECL_METHOD(l_GetAlpha); + LUAX_DECL_METHOD(l_Multiply); + LUAX_DECL_METHOD(l_Index); //索引r,g,b,a + LUAX_DECL_METHOD(l_NewIndex); //修改r,g,b,a + + //---------------------------------------------------------------------------------------------------------- + + }; + + /// + /// 规范化颜色 + /// + class Color ASURA_FINAL: public Scripting::Portable + { + public: + + static Color Black; + static Color White; + static Color Red; + static Color Green; + static Color Blue; + + Color(); + + Color(const Color& c); + + Color(float r, float g, float b, float a); + + Color(const Color32& c); + + ~Color(); + + Color operator *(const Color& c); + + float r, g, b, a; + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(Color); + + LUAX_DECL_METHOD(l_ToColor32); + LUAX_DECL_METHOD(l_SetColor); + LUAX_DECL_METHOD(l_GetColor); + LUAX_DECL_METHOD(l_Multiply); // 颜色乘法 + + //---------------------------------------------------------------------------------------------------------- + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/gl.cpp b/Source/Asura.Engine/graphics/gl.cpp new file mode 100644 index 0000000..dac2ea4 --- /dev/null +++ b/Source/Asura.Engine/graphics/gl.cpp @@ -0,0 +1,11 @@ +#include "OpenGL.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/gl.h b/Source/Asura.Engine/graphics/gl.h new file mode 100644 index 0000000..0661e17 --- /dev/null +++ b/Source/Asura.Engine/graphics/gl.h @@ -0,0 +1,19 @@ +#ifndef __ASURA_ENGINE_OPENGL_H__ +#define __ASURA_ENGINE_OPENGL_H__ + +#include "glad/glad.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class OpenGL + { + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/image.cpp b/Source/Asura.Engine/graphics/image.cpp new file mode 100644 index 0000000..e704945 --- /dev/null +++ b/Source/Asura.Engine/graphics/image.cpp @@ -0,0 +1,32 @@ +#include "Config.h" +#include "Image.h" +#include "GL.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + Image::Image() + { + } + + Image::~Image() + { + } + + //创建\更新纹理像素信息 + bool Image::Load(ImageData* data) + { + ASSERT(data); + + glBindTexture(GL_TEXTURE_2D, mTextureHandle); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, data->width, data->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels); + glBindTexture(GL_TEXTURE_2D, 0); + return true; + + RRA(data, mImageData); + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/image.h b/Source/Asura.Engine/graphics/image.h new file mode 100644 index 0000000..66c7ae1 --- /dev/null +++ b/Source/Asura.Engine/graphics/image.h @@ -0,0 +1,78 @@ +#ifndef __ASURA_ENGINE_IMAGE_H__ +#define __ASURA_ENGINE_IMAGE_H__ + +#include "Math/Vector2.hpp" +#include "Scripting/Portable.h" +#include "FileSystem/Reloadable.h" +#include "StringMap.hpp" +#include "Manager.hpp" +#include "Texture.h" +#include "Color.h" +#include "ImageData.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class ImageFactory; + + /// + /// Image是图片从内存中载入后,读取进游戏后保存的结果。一个Image在内存、显存中只会保存一份,不会产生副本。需要特征 + /// 化的区别image,如锚点位置,缩放和旋转角度,使用sprite。是一个只读类。主要是考虑到editor和engine使用不同的封装。 + /// + class Image ASURA_FINAL + : public Drawable + , public Scripting::Portable + , public Filesystem::Reloadable + { + public: + + Image(); + + ~Image(); + + /// + /// 从数据buffer构建image像素信息,如果mPixels不为空,先清空数据。用来重新构建image,使用glTexImage2D重新提交image + /// 的像素数据。 + /// + bool Load(ImageData* data); + + uint GetWidth(); + uint GetHeight(); + Math::Vector2u GetSize(); + + /// + /// 获得某一个位置的像素 + /// + Color32 GetPixel(uint x, uint y); + + void Render(const RenderTarget* rt, const RenderState& state) override; + + void Render(const RenderTarget* rt, const Math::Rectf& quad, const RenderState& state) override; + + private: + + ImageData* mImageData; + + Math::Vector2u mSize; + + public: + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(SimImage); + + LUAX_DECL_METHOD(l_Load); + LUAX_DECL_METHOD(l_GetWidth); + LUAX_DECL_METHOD(l_GetHeight); + LUAX_DECL_METHOD(l_GetSize); + + //---------------------------------------------------------------------------------------------------------- + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/image_data.cpp b/Source/Asura.Engine/graphics/image_data.cpp new file mode 100644 index 0000000..68781a1 --- /dev/null +++ b/Source/Asura.Engine/graphics/image_data.cpp @@ -0,0 +1,50 @@ +#include "ImageData.h" +#include "PNGDecoder.h" +#include "STBDecoder.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + using namespace std; + + // 解析image的decoder,用来作为解析策略。 + list ImageData::ImageDecoders = { + new PNGDecoder(), // png + new STBDecoder() // jpeg, tga, bmp + }; + + ImageData::ImageData(const Filesystem::DataBuffer& buffer) + : DecodedData(buffer) + { + } + + ImageData::~ImageData() + { + if (pixels) + delete[] pixels; + } + + /// + /// 解析,如果无法成功,抛出异常 + /// + void ImageData::Decode(const Filesystem::DataBuffer& buffer) + { + for (ImageDecoder* decoder : ImageDecoders) + { + if (decoder->CanDecode(buffer)) + { + decoder->Decode(buffer, this); + return; + } + } + } + + Color ImageData::GetPixel(uint x, uint y) + { + + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/image_data.h b/Source/Asura.Engine/graphics/image_data.h new file mode 100644 index 0000000..931eaa3 --- /dev/null +++ b/Source/Asura.Engine/graphics/image_data.h @@ -0,0 +1,62 @@ +#ifndef __ASURA_ENGINE_IMAGEDATA_H__ +#define __ASURA_ENGINE_IMAGEDATA_H__ + +#include + +#include "Scripting/Luax.hpp" +#include "Filesystem/DecodedData.h" +#include "ImageDecoder.h" +#include "PixelFormat.h" +#include "Color.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class ImageData ASURA_FINAL + : public Filesystem::DecodedData + , public Scripting::Portable + { + public: + + /// + /// 解析图片数据文件,并构建像素信息,如果解析失败,抛出异常 + /// + ImageData(const Filesystem::DataBuffer& buffer); + + ~ImageData(); + + Color GetPixel(uint x, uint y); + + uint width, height; + PixelFormat format; + std::size_t size; + byte* pixels; + + private: + + void Decode(const Filesystem::DataBuffer& buffer) override; + + /// + /// 在第一次准备image data时构建所有提供的decoder。在几个decoders中间选择解析策略。 + /// + static std::list ImageDecoders; + + public: + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(ImageData); + + LUAX_DECL_METHOD(l_GetPixel); + LUAX_DECL_METHOD(l_GetSize); + + //---------------------------------------------------------------------------------------------------------- + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/image_decoder.h b/Source/Asura.Engine/graphics/image_decoder.h new file mode 100644 index 0000000..2c73fd1 --- /dev/null +++ b/Source/Asura.Engine/graphics/image_decoder.h @@ -0,0 +1,35 @@ +#ifndef __ASURA_ENGINE_IMAGE_DECODER_H__ +#define __ASURA_ENGINE_IMAGE_DECODER_H__ + +#include "FileSystem/DataBuffer.h" + +#include "ImageData.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class ImageDecoder + { + public: + + ImageDecoder(); + virtual ~ImageDecoder(); + + /// + /// 判断内存是否能用本decoder解压 + /// + virtual bool CanDecode(const Filesystem::DataBuffer& buffer) = 0; + + /// + /// 输入一个编码后的内存,输出一个解压后的Image data,如果解压失败返回nullptr + /// + virtual void Decode(const Filesystem::DataBuffer& buffer, ImageData& data) = 0; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/mesh2d.cpp b/Source/Asura.Engine/graphics/mesh2d.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/mesh2d.h b/Source/Asura.Engine/graphics/mesh2d.h new file mode 100644 index 0000000..de4a0c8 --- /dev/null +++ b/Source/Asura.Engine/graphics/mesh2d.h @@ -0,0 +1,29 @@ +#ifndef __ASURA_ENGINE_MESH2D_H__ +#define __ASURA_ENGINE_MESH2D_H__ + +#include "Scripting/Luax.hpp" + +#include "Scripting/Portable.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 2D mesh,用于做一些顶点动画。 + /// + class Mesh2D ASURA_FINAL: public Scripting::Portable + { + public: + + Mesh2D(); + + ~Mesh2D(); + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/pixel_format.h b/Source/Asura.Engine/graphics/pixel_format.h new file mode 100644 index 0000000..8df07d5 --- /dev/null +++ b/Source/Asura.Engine/graphics/pixel_format.h @@ -0,0 +1,91 @@ +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 像素格式 + /// + enum PixelFormat + { + PIXELFORMAT_UNKNOWN, + + // these are converted to an actual format by love + PIXELFORMAT_NORMAL, + PIXELFORMAT_HDR, + + // "regular" formats + PIXELFORMAT_R8, + PIXELFORMAT_RG8, + PIXELFORMAT_RGBA8, + PIXELFORMAT_sRGBA8, + PIXELFORMAT_R16, + PIXELFORMAT_RG16, + PIXELFORMAT_RGBA16, + PIXELFORMAT_R16F, + PIXELFORMAT_RG16F, + PIXELFORMAT_RGBA16F, + PIXELFORMAT_R32F, + PIXELFORMAT_RG32F, + PIXELFORMAT_RGBA32F, + + PIXELFORMAT_LA8, // Same as RG8, but accessed as (L, L, L, A) + + // packed formats + PIXELFORMAT_RGBA4, + PIXELFORMAT_RGB5A1, + PIXELFORMAT_RGB565, + PIXELFORMAT_RGB10A2, + PIXELFORMAT_RG11B10F, + + // depth/stencil formats + PIXELFORMAT_STENCIL8, + PIXELFORMAT_DEPTH16, + PIXELFORMAT_DEPTH24, + PIXELFORMAT_DEPTH32F, + PIXELFORMAT_DEPTH24_STENCIL8, + PIXELFORMAT_DEPTH32F_STENCIL8, + + // compressed formats + PIXELFORMAT_DXT1, + PIXELFORMAT_DXT3, + PIXELFORMAT_DXT5, + PIXELFORMAT_BC4, + PIXELFORMAT_BC4s, + PIXELFORMAT_BC5, + PIXELFORMAT_BC5s, + PIXELFORMAT_BC6H, + PIXELFORMAT_BC6Hs, + PIXELFORMAT_BC7, + PIXELFORMAT_PVR1_RGB2, + PIXELFORMAT_PVR1_RGB4, + PIXELFORMAT_PVR1_RGBA2, + PIXELFORMAT_PVR1_RGBA4, + PIXELFORMAT_ETC1, + PIXELFORMAT_ETC2_RGB, + PIXELFORMAT_ETC2_RGBA, + PIXELFORMAT_ETC2_RGBA1, + PIXELFORMAT_EAC_R, + PIXELFORMAT_EAC_Rs, + PIXELFORMAT_EAC_RG, + PIXELFORMAT_EAC_RGs, + PIXELFORMAT_ASTC_4x4, + PIXELFORMAT_ASTC_5x4, + PIXELFORMAT_ASTC_5x5, + PIXELFORMAT_ASTC_6x5, + PIXELFORMAT_ASTC_6x6, + PIXELFORMAT_ASTC_8x5, + PIXELFORMAT_ASTC_8x6, + PIXELFORMAT_ASTC_8x8, + PIXELFORMAT_ASTC_10x5, + PIXELFORMAT_ASTC_10x6, + PIXELFORMAT_ASTC_10x8, + PIXELFORMAT_ASTC_10x10, + PIXELFORMAT_ASTC_12x10, + PIXELFORMAT_ASTC_12x12, + + PIXELFORMAT_MAX_ENUM + }; + + } +} diff --git a/Source/Asura.Engine/graphics/png_decoder.cpp b/Source/Asura.Engine/graphics/png_decoder.cpp new file mode 100644 index 0000000..f919090 --- /dev/null +++ b/Source/Asura.Engine/graphics/png_decoder.cpp @@ -0,0 +1,19 @@ +#include "PNGDecoder.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + bool PNGDecoder::CanDecode(const Filesystem::DataBuffer& buffer) + { + return false; + } + + void PNGDecoder::Decode(const Filesystem::DataBuffer& buffer, ImageData& data) + { + + } + + } +} diff --git a/Source/Asura.Engine/graphics/png_decoder.h b/Source/Asura.Engine/graphics/png_decoder.h new file mode 100644 index 0000000..980753f --- /dev/null +++ b/Source/Asura.Engine/graphics/png_decoder.h @@ -0,0 +1,27 @@ +#ifndef __ASURA_ENGINE_PNGDECODER_H__ +#define __ASURA_ENGINE_PNGDECODER_H__ + +#include "ImageDecoder.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 使用lodepng解压png文件 + /// + class PNGDecoder ASURA_FINAL: public ImageDecoder + { + public: + + bool CanDecode(const Filesystem::DataBuffer& buffer) override; + + void Decode(const Filesystem::DataBuffer& buffer, ImageData& data) override; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/quad.cpp b/Source/Asura.Engine/graphics/quad.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/quad.h b/Source/Asura.Engine/graphics/quad.h new file mode 100644 index 0000000..b7dd3d9 --- /dev/null +++ b/Source/Asura.Engine/graphics/quad.h @@ -0,0 +1 @@ +// Quad和rect的区别在于,rect是的边偏向于轴,quad不一定。 diff --git a/Source/Asura.Engine/graphics/render_state.h b/Source/Asura.Engine/graphics/render_state.h new file mode 100644 index 0000000..f313296 --- /dev/null +++ b/Source/Asura.Engine/graphics/render_state.h @@ -0,0 +1,50 @@ +#ifndef __ASURA_ENGINE_RENDER_STATE_H__ +#define __ASURA_ENGINE_RENDER_STATE_H__ + +#include "Math/Vector2.hpp" +#include "Math/Rect.hpp" +#include "Math/Transform.h" + +#include "Shader.h" +#include "BlendMode.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 渲染当前纹理的方式 + /// + struct RenderState ASURA_FINAL + { + /// + /// 默认的render state + /// + static RenderState Default; + + RenderState(); + ~RenderState(); + + /// + /// 位置、缩放、中心位置和旋转 + /// + + Math::Transform transform; + + /// + /// 着色器 + /// + Shader* shader; + + /// + /// 混合方式 + /// + BlendMode blendMode; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/render_target.cpp b/Source/Asura.Engine/graphics/render_target.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/render_target.h b/Source/Asura.Engine/graphics/render_target.h new file mode 100644 index 0000000..d6de164 --- /dev/null +++ b/Source/Asura.Engine/graphics/render_target.h @@ -0,0 +1,54 @@ +#ifndef __ASURA_ENGINE_RENDERTARGET_H__ +#define __ASURA_ENGINE_RENDERTARGET_H__ + +#include "Math/Rect.hpp" +#include "Texture.h" +#include "Scripting/Portable.h" +#include "Color.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class Drawable; + + /// + /// 可被作为渲染目标的类,派生类有 + /// Canvas(RenderTexture) + /// Window(RenderWindow) + /// + class RenderTarget + { + public: + + RenderTarget() {}; + + virtual ~RenderTarget() {}; + + /// + /// 用颜色col清空整个RT + /// + virtual void Clear(const Color& col = Color::Black) = 0; + + /// + /// 用颜色col清空部分RT + /// + virtual void Clear(const Math::Recti& quad, const Color& col = Color::Black) = 0; + + /// + /// 绘制texture到本RT上 + /// + virtual void Draw(const Drawable* texture, const RenderState& state) = 0; + + /// + /// 绘制一部分texture到本RT上 + /// + virtual void Draw(const Drawable* texture, const Math::Recti& quad, const RenderState& state) = 0; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/shader.cpp b/Source/Asura.Engine/graphics/shader.cpp new file mode 100644 index 0000000..1a85866 --- /dev/null +++ b/Source/Asura.Engine/graphics/shader.cpp @@ -0,0 +1,81 @@ +#include "Shader.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + Shader::Shader() + { + + } + + Shader::~Shader() + { + + } + + bool Shader::Load(const std::string& vertexShader, const std::string& fragmentShader) + { + + } + + uint Shader::GetUniformLocation(const std::string& uniform) + { + + } + + GLuint Shader::GetGLProgramHandle() + { + return mProgramHandle; + } + + void Shader::Use() + { + + } + + void Shader::Unuse() + { + + } + + void Shader::SetUniformFloat(uint loc, float value) + { + + } + + void Shader::SetUniformFloat(uint loc, float value) + { + + } + + void Shader::SetUniformTexture(uint loc, const Texture& texture) + { + + } + + void Shader::SetUniformVector2(uint loc, const Math::Vector2f& vec2) + { + + } + + void Shader::SetUniformVector3(uint loc, const Math::Vector3f& vec3) + { + + } + + void Shader::SetUniformVector4(uint loc, const Math::Vector4f& vec4) + { + + } + + uint Shader::GetGLTextureUnitCount() + { + GLint maxTextureUnits = 0; + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); + return (uint)maxTextureUnits; + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/shader.h b/Source/Asura.Engine/graphics/shader.h new file mode 100644 index 0000000..103c7b7 --- /dev/null +++ b/Source/Asura.Engine/graphics/shader.h @@ -0,0 +1,123 @@ +#ifndef __ASURA_ENGINE_SHADER_H__ +#define __ASURA_ENGINE_SHADER_H__ + +#include +#include + +#include "FileSystem/Reloadable.h" +#include "Scripting/Luax.hpp" +#include "Math/Vector2.hpp" +#include "Math/Vector3.hpp" +#include "Math/Vector4.h" +#include "Math/Matrix44.h" +#include "StringMap.hpp" +#include "scripting/portable.h" +#include "Color.h" +#include "Manager.hpp" +#include "Texture.h" +#include "GL.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 一个shader是一个在材质间共享的程序。Shader本身不保存uniforms和顶点数据,只提供设置uniforms和use着色器的方法。编辑 + /// 器针对每个shader,会通过shader代码找到声明的uniforms变量,并暴露给framework的material设置。 + /// + class Shader ASURA_FINAL + : public Scripting::Portable + , public Filesystem::Reloadable + { + public: + + Shader(); + + ~Shader(); + + /// + /// 从代码编译shader,编译时会先检测是否有上次缓存的uniforms location map。使用glAttachShader重新编译生成着色器, + /// 不会重新申请着色器程序。 + /// + bool Load(const std::string& vertexShader, const std::string& fragmentShader); + + /// + /// 将当期shader设置为活动 + /// + void Use(); + + /// + /// 将当期shader设置为非活动 + /// + void Unuse(); + + /// + /// 在已经知道uniform location的情况下,设置值。 + /// + void SetUniformFloat(uint loc, float value); + void SetUniformTexture(uint loc, const Texture& texture); + void SetUniformVector2(uint loc, const Math::Vector2f& vec2); + void SetUniformVector3(uint loc, const Math::Vector3f& vec3); + void SetUniformVector4(uint loc, const Math::Vector4f& vec4); + void SetUniformColor(uint loc, const Color& color); + void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44); + + uint GetUniformLocation(const std::string& uniform); + + bool HasUniform(const std::string& uniform); + + GLuint GetGLProgramHandle(); + + /// + /// 获得texture unit数量,一般为16个 + /// + static uint GetGLTextureUnitCount(); + + private: + + /// + /// 当前活动的shader + /// + static Shader* mCurrentShader; + + /// + /// 设置内置变量: + /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 + /// vec2 Asura_RenderTargetSize RT的大小,以像素为单位 + /// Texture Asura_MainTexture 主纹理 + /// + void SetBuiltInUniforms(); + + /// + /// OpenGL shader program handle. + /// + GLuint mProgramHandle; + + public: + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(SimShader); + + LUAX_DECL_METHOD(l_Use); + LUAX_DECL_METHOD(l_Unuse); + LUAX_DECL_METHOD(l_Load); + LUAX_DECL_METHOD(l_HasUniform); + LUAX_DECL_METHOD(l_GetUniformLocation); + LUAX_DECL_METHOD(l_SetBuiltInUniforms); + LUAX_DECL_METHOD(l_SetUniformFloat); + LUAX_DECL_METHOD(l_SetUniformTexture); + LUAX_DECL_METHOD(l_SetUniformVector2); + LUAX_DECL_METHOD(l_SetUniformVector3); + LUAX_DECL_METHOD(l_SetUniformVector4); + LUAX_DECL_METHOD(l_SetUniformColor); + + //---------------------------------------------------------------------------------------------------------- + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/shape.cpp b/Source/Asura.Engine/graphics/shape.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/shape.h b/Source/Asura.Engine/graphics/shape.h new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/sprite_batch.cpp b/Source/Asura.Engine/graphics/sprite_batch.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Source/Asura.Engine/graphics/sprite_batch.h b/Source/Asura.Engine/graphics/sprite_batch.h new file mode 100644 index 0000000..7909519 --- /dev/null +++ b/Source/Asura.Engine/graphics/sprite_batch.h @@ -0,0 +1,28 @@ +#ifndef __ASURA_ENGINE_SPRITE_BATCH_H__ +#define __ASURA_ENGINE_SPRITE_BATCH_H__ + +#include "Scripting/Portable.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// Sprite batch用在批量渲染单个图片的地方,比如粒子系统。 + /// + class SpriteBatch ASURA_FINAL + : public Scripting::Portable + { + public: + + SpriteBatch(); + + ~SpriteBatch(); + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/stb_decoder.cpp b/Source/Asura.Engine/graphics/stb_decoder.cpp new file mode 100644 index 0000000..b14d0f3 --- /dev/null +++ b/Source/Asura.Engine/graphics/stb_decoder.cpp @@ -0,0 +1,65 @@ +#include "STBDecoder.h" + +#include "Exceptions/Exception.h" +#include "stb/stb_image.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + bool STBDecoder::CanDecode(const Filesystem::DataBuffer& buffer) + { + int w = 0; + int h = 0; + int comp = 0; + + int status = stbi_info_from_memory((const stbi_uc*)buffer.data, buffer.size, &w, &h, &comp); + + return status == 1 && w > 0 && h > 0; + } + + void STBDecoder::Decode(const Filesystem::DataBuffer& db, ImageData& imageData) + { + const stbi_uc *buffer = (const stbi_uc *)db.data; + int bufferlen = db.size; + int width, height; + int comp = 0; + byte* data = nullptr; + PixelFormat format = PIXELFORMAT_UNKNOWN; + std::size_t size = 0; + + if (stbi_is_hdr_from_memory(buffer, bufferlen)) + { + // 4个channel都是float + data = (byte*)stbi_loadf_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha); + format = PIXELFORMAT_RGBA32F; + size = width * height * 4 * sizeof(float); + } + else + { + data = (byte*)stbi_load_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha); + format = PIXELFORMAT_ASTC_8x5; + size = width * height * 4; + } + if (data) + { + // 保证内存被释放,做一个防护,理论上不会出现没有释放的情况,因为image data是一次性的,image才是多次的。 + if (imageData.pixels) + delete[] imageData.pixels; + imageData.pixels = (byte*)data; + imageData.format = format; + imageData.width = width; + imageData.height = height; + } + else + { + const char *err = stbi_failure_reason(); + if (err == nullptr) + err = "unknown error"; + throw Exception("Could not decode image with stb_image (%s).", err); + } + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/stb_decoder.h b/Source/Asura.Engine/graphics/stb_decoder.h new file mode 100644 index 0000000..b8fb83c --- /dev/null +++ b/Source/Asura.Engine/graphics/stb_decoder.h @@ -0,0 +1,27 @@ +#ifndef __ASURA_ENGINE_STBDECODER_H__ +#define __ASURA_ENGINE_STBDECODER_H__ + +#include "image_decoder.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 使用stb_image解压JPEG、TGA、BMP文件 + /// + class STBDecoder ASURA_FINAL: public ImageDecoder + { + public: + + bool CanDecode(const Filesystem::DataBuffer& buffer) override; + + void Decode(const Filesystem::DataBuffer& buffer, ImageData& data) override; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/texture.cpp b/Source/Asura.Engine/graphics/texture.cpp new file mode 100644 index 0000000..6cb6497 --- /dev/null +++ b/Source/Asura.Engine/graphics/texture.cpp @@ -0,0 +1,26 @@ +#include "Texture.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + Texture::Texture() + : mTextureHandle(0) + { + // 申请GL texture + glGenTextures(1, &mTextureHandle); + } + + Texture::~Texture() + { + glDeleteTextures(1, &mTextureHandle); + } + + GLuint Texture::GetGLTextureHandle() const + { + return mTextureHandle; + } + + } +} \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/texture.h b/Source/Asura.Engine/graphics/texture.h new file mode 100644 index 0000000..81aa469 --- /dev/null +++ b/Source/Asura.Engine/graphics/texture.h @@ -0,0 +1,66 @@ +#ifndef __ASURA_ENGINE_TEXTURE_H__ +#define __ASURA_ENGINE_TEXTURE_H__ + +#include "Config.h" +#include "Math/Rect.hpp" +#include "Math/Vector2.hpp" +#include "Scripting/Luax.hpp" +#include "RenderState.h" +#include "GL.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class RenderTarget; + + /// + /// 2D纹理抽象类,在2d mesh和render target中被使用。Texture的渲染原点在左上角,游戏里面的上层会以笛卡尔坐标系为标准。 + /// 在Editor里面界面和组件也是以左上角为原点,这样是为了方便。 + /// + ASURA_ABSTRACT class Texture + { + public: + + Texture(); + + virtual ~Texture(); + + GLuint GetGLTextureHandle() const; + + /// + /// 渲染整个texture到rt上,原点在左上角,向右,向下延伸 + /// + virtual void Render(const RenderTarget* rt, const RenderState& state) = 0; + + /// + /// 渲染texture的一部分到rt上,原点在左上角,向右,向下延伸。 + /// + virtual void Render(const RenderTarget* rt, const Math::Rectf& quad, const RenderState& state) = 0; + + /// + /// 设置过滤方式 + /// + void SetSmooth(bool smooth); + + /// + /// 设置重复方式 + /// + void SetRepeated(); + + protected: + + /// + /// OpenGL texture handle。 + /// + GLuint mTextureHandle; + + }; + + using Drawable = Texture; + + } +} + +#endif \ No newline at end of file diff --git a/Source/Asura.Engine/graphics/window.cpp b/Source/Asura.Engine/graphics/window.cpp new file mode 100644 index 0000000..bb941e1 --- /dev/null +++ b/Source/Asura.Engine/graphics/window.cpp @@ -0,0 +1,56 @@ +#include "Config.h" +#include "Window.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + Window::Window(WindowStyle style) + { + //mWindowHandle = SDL_CreateWindow(); + } + + Window::~Window() + { + + } + + SDL_Window* Window::GetSDLWindowHandle() + { + return mWindowHandle; + } + + void Window::SetPosition(int x, int y) + { + ASSERT(mWindowHandle); + SDL_SetWindowPosition(mWindowHandle, x, y); + } + + void Window::SetTitle(const std::string& title) + { + + } + + void Window::Show() + { + + } + + void Window::Hide() + { + + } + + void Window::SetWindowStyle(WindowStyle style) + { + + } + + void Window::SwapRenderBuffer() + { + + } + + } +} diff --git a/Source/Asura.Engine/graphics/window.h b/Source/Asura.Engine/graphics/window.h new file mode 100644 index 0000000..973fd98 --- /dev/null +++ b/Source/Asura.Engine/graphics/window.h @@ -0,0 +1,85 @@ +#ifndef __ASURA_ENGINE_WINDOW_H__ +#define __ASURA_ENGINE_WINDOW_H__ + +#include "SDL2/Sdl.h" +#include "Graphics/RenderTarget.h" +#include "Math/Vector2.hpp" +#include "Scripting/Portable.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + enum WindowStyle + { + WINDOW_STYLE_FULLSCREEN = 1 << 1, + }; + + /// + /// 窗口,支持多窗口。在编辑器下需要多个窗口支持,runner只需要一个窗口。 + /// + class Window ASURA_FINAL + : public RenderTarget + , public Scripting::Portable + { + public: + + Window(WindowStyle style); + + ~Window(); + + SDL_Window* GetSDLWindowHandle(); + + void SetSize(uint width, uint height); + + void SetPosition(int x, int y); + + void SetTitle(const std::string& title); + + void SetWindowStyle(WindowStyle style); + + void Show(); + + void Hide(); + + /// + /// 如果是开启双缓冲的窗口,需要交换缓冲区来展示到前台 + /// + void SwapRenderBuffer(); + + void Clear(const Color& col = Color::Black) override; + + void Clear(const Math::Recti& quad, const Color& col = Color::Black) override; + + void Draw(const Drawable* texture, const RenderState& state) override; + + void Draw(const Drawable* texture, const Math::Recti& quad, const RenderState& state) override; + + private: + + /// + /// SDL window handle. + /// + SDL_Window* mWindowHandle; + + Math::Vector2i mPosition; + + Math::Vector2i mSize; + + public: + + //---------------------------------------------------------------------------------------------------------- + + LUAX_DECL_FACTORY(Window); + + //---------------------------------------------------------------------------------------------------------- + + }; + + using RenderWindow = Window; + + } +} + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0