From 0371f99359d1f58dbec6353234c2b1ebd86a7585 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 25 Jul 2018 20:27:55 +0800 Subject: *image->texture --- src/libjin/audio/audio.h | 3 +- src/libjin/audio/sdl/audio.h | 2 +- src/libjin/audio/source.cpp | 2 +- src/libjin/common/data.h | 6 +-- src/libjin/common/modules.h | 14 ++++++ src/libjin/common/subsystem.h | 6 +-- src/libjin/render/graphics.h | 2 +- src/libjin/render/image.cpp | 95 ----------------------------------- src/libjin/render/image.h | 34 ------------- src/libjin/render/jsl.cpp | 4 +- src/libjin/render/jsl.h | 4 +- src/libjin/render/render.h | 2 +- src/libjin/render/texture.cpp | 95 +++++++++++++++++++++++++++++++++++ src/libjin/render/texture.h | 34 +++++++++++++ src/libjin/render/window.cpp | 12 +---- src/libjin/render/window.h | 6 --- src/lua/graphics/luaopen_Image.cpp | 16 +++--- src/lua/graphics/luaopen_JSL.cpp | 4 +- src/lua/graphics/luaopen_graphics.cpp | 6 +-- 19 files changed, 174 insertions(+), 173 deletions(-) create mode 100644 src/libjin/common/modules.h delete mode 100644 src/libjin/render/image.cpp delete mode 100644 src/libjin/render/image.h create mode 100644 src/libjin/render/texture.cpp create mode 100644 src/libjin/render/texture.h (limited to 'src') diff --git a/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index 7b27e1f..239b718 100644 --- a/src/libjin/audio/audio.h +++ b/src/libjin/audio/audio.h @@ -12,7 +12,8 @@ namespace audio { class Source; - class AudioSystem : public Subsystem + template + class AudioSystem : public Subsystem { public: diff --git a/src/libjin/audio/sdl/audio.h b/src/libjin/audio/sdl/audio.h index 861acb3..83ad255 100644 --- a/src/libjin/audio/sdl/audio.h +++ b/src/libjin/audio/sdl/audio.h @@ -13,7 +13,7 @@ namespace audio #define SDLAUDIO_BYTEDEPTH (SDLAUDIO_BITDEPTH >> 3) #define SDLAUDIO_CHANNELS 2 - class SDLAudio : public AudioSystem + class SDLAudio : public AudioSystem { public: diff --git a/src/libjin/audio/source.cpp b/src/libjin/audio/source.cpp index a09bbab..f6da44b 100644 --- a/src/libjin/audio/source.cpp +++ b/src/libjin/audio/source.cpp @@ -21,4 +21,4 @@ namespace audio } } -} \ No newline at end of file +} diff --git a/src/libjin/common/data.h b/src/libjin/common/data.h index 51a3252..6f1c504 100644 --- a/src/libjin/common/data.h +++ b/src/libjin/common/data.h @@ -4,10 +4,10 @@ namespace jin { - struct Data + struct Buffer { - void* data; - int len; + unsigned int len; + char data[0]; }; } diff --git a/src/libjin/common/modules.h b/src/libjin/common/modules.h new file mode 100644 index 0000000..52b0160 --- /dev/null +++ b/src/libjin/common/modules.h @@ -0,0 +1,14 @@ +#ifndef __JIN_COMMON_MODULES_H +#define __JIN_COMMON_MODULES_H + +#define JIN_MODULES_AUDIO 1 +#define JIN_MODULES_RENDER 1 +#define JIN_MODULES_DEBUG 1 +#define JIN_MODULES_FILESYSTEM 1 +#define JIN_MODULES_INPUT 1 +#define JIN_MODULES_MATH 1 +#define JIN_MODULES_NET 1 +#define JIN_MODULES_PHYSICS 1 +#define JIN_MODULES_TILEMAP 1 + +#endif \ No newline at end of file diff --git a/src/libjin/common/subsystem.h b/src/libjin/common/subsystem.h index 90e8570..35563da 100644 --- a/src/libjin/common/subsystem.h +++ b/src/libjin/common/subsystem.h @@ -21,7 +21,7 @@ namespace jin void quit() { - quitSystem(); + CALLONCE(quitSystem()); destroy(); } @@ -32,8 +32,8 @@ namespace jin SINGLETON(System); - virtual bool initSystem(const Setting* setting) = 0; - virtual void quitSystem() = 0; + virtual onlyonce bool initSystem(const Setting* setting) = 0; + virtual onlyonce void quitSystem() = 0; }; diff --git a/src/libjin/render/graphics.h b/src/libjin/render/graphics.h index a3125c5..579158d 100644 --- a/src/libjin/render/graphics.h +++ b/src/libjin/render/graphics.h @@ -3,7 +3,7 @@ #include "color.h" #include "canvas.h" -#include "image.h" +#include "texture.h" namespace jin { diff --git a/src/libjin/render/image.cpp b/src/libjin/render/image.cpp deleted file mode 100644 index 564b54e..0000000 --- a/src/libjin/render/image.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include "image.h" -#include "3rdparty/stb/stb_image.h" -#include "../utils/utils.h" -#include "../math/math.h" - -namespace jin -{ -namespace render -{ - - Image* Image::createImage(const char* file) - { - std::ifstream fs; - fs.open(file, std::ios::binary); - Image* img = nullptr; - if (fs.is_open()) - { - fs.seekg(0, std::ios::end); - int size = fs.tellg(); - fs.seekg(0, std::ios::beg); - char* buffer = (char*)malloc(size); - memset(buffer, 0, size); - fs.read(buffer, size); - img = createImage(buffer, size); - free(buffer); - } - fs.close(); - return img; - } - - Image* Image::createImage(const void* mem, size_t size) - { - Image* img = new Image(); - if(!img->loadb(mem, size)) - { - delete img; - img = nullptr; - } - return img; - } - - Image::Image() - : Drawable(), pixels(0) - { - } - - Image::~Image() - { - stbi_image_free(pixels); - } - - color Image::getPixel(int x, int y) - { - if (without(x, 0, width) || without(y, 0, height)) - { - return { 0 }; - } - return pixels[x + y * width]; - } - - bool Image::loadb(const void* b, size_t size) - { - // 使用stbi_load_from_memory替代 - unsigned char* imageData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha); - if (imageData == 0) return false; - pixels = (color*)imageData; - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, - height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); - - // set render vertices - Drawable::setVertices( - new float [DRAWABLE_V_SIZE] { - 0, 0, - 0, (float)height, - (float)width, (float)height, - (float)width, 0, - }, - new float [DRAWABLE_V_SIZE] { - 0, 0, - 0, 1, - 1, 1, - 1, 0 - } - ); - - return true; - } -} -} \ No newline at end of file diff --git a/src/libjin/render/image.h b/src/libjin/render/image.h deleted file mode 100644 index 95b4e4f..0000000 --- a/src/libjin/render/image.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __JIN_IMAGE_H -#define __JIN_IMAGE_H -#include "3rdparty/GLee/GLee.h" -#include "color.h" -#include "drawable.h" -namespace jin -{ -namespace render -{ - class Image: public Drawable - { - - public: - - static Image* createImage(const char* file); - static Image* createImage(const void* mem, size_t size); - - ~Image(); - - color getPixel(int x, int y); - - private: - - Image(); - - bool loadb(const void* buffer, size_t size); - - color* pixels; - - }; -} -} - -#endif \ No newline at end of file diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp index 003f93a..ee3c824 100644 --- a/src/libjin/render/jsl.cpp +++ b/src/libjin/render/jsl.cpp @@ -85,7 +85,7 @@ namespace render glUniform1f(loc, number); } - void JSLProgram::sendImage(const char* variable, const Image* image) + void JSLProgram::sendTexture(const char* variable, const Texture* tex) { checkJSL(); @@ -95,7 +95,7 @@ namespace render GLint texture_unit = getTextureUnit(variable); glUniform1i(location, texture_unit); glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(GL_TEXTURE_2D, image->getTexture()); + glBindTexture(GL_TEXTURE_2D, tex->getTexture()); glActiveTexture(GL_TEXTURE0); } diff --git a/src/libjin/render/jsl.h b/src/libjin/render/jsl.h index 520a6eb..5943fbe 100644 --- a/src/libjin/render/jsl.h +++ b/src/libjin/render/jsl.h @@ -4,7 +4,7 @@ #include #include #include "color.h" -#include "image.h" +#include "texture.h" #include "canvas.h" #include "3rdparty/GLee/GLee.h" @@ -35,7 +35,7 @@ namespace render } void sendFloat(const char* name, float number); - void sendImage(const char* name, const Image* image); + void sendTexture(const char* name, const Texture* image); void sendVec2(const char* name, float x, float y); void sendVec3(const char* name, float x, float y, float z); void sendVec4(const char* name, float x, float y, float z, float w); diff --git a/src/libjin/render/render.h b/src/libjin/render/render.h index 6c0d6bf..1bf9082 100644 --- a/src/libjin/render/render.h +++ b/src/libjin/render/render.h @@ -5,7 +5,7 @@ #include "color.h" #include "font.h" #include "graphics.h" -#include "image.h" +#include "texture.h" #include "jsl.h" #include "window.h" diff --git a/src/libjin/render/texture.cpp b/src/libjin/render/texture.cpp new file mode 100644 index 0000000..d5cd695 --- /dev/null +++ b/src/libjin/render/texture.cpp @@ -0,0 +1,95 @@ +#include +#include "texture.h" +#include "3rdparty/stb/stb_image.h" +#include "../utils/utils.h" +#include "../math/math.h" + +namespace jin +{ +namespace render +{ + + Texture* Texture::createTexture(const char* file) + { + std::ifstream fs; + fs.open(file, std::ios::binary); + Texture* tex = nullptr; + if (fs.is_open()) + { + fs.seekg(0, std::ios::end); + int size = fs.tellg(); + fs.seekg(0, std::ios::beg); + char* buffer = (char*)malloc(size); + memset(buffer, 0, size); + fs.read(buffer, size); + tex = createTexture(buffer, size); + free(buffer); + } + fs.close(); + return tex; + } + + Texture* Texture::createTexture(const void* mem, size_t size) + { + Texture* tex = new Texture(); + if(!tex->loadb(mem, size)) + { + delete tex; + tex = nullptr; + } + return tex; + } + + Texture::Texture() + : Drawable(), pixels(0) + { + } + + Texture::~Texture() + { + stbi_image_free(pixels); + } + + color Texture::getPixel(int x, int y) + { + if (without(x, 0, width) || without(y, 0, height)) + { + return { 0 }; + } + return pixels[x + y * width]; + } + + bool Texture::loadb(const void* b, size_t size) + { + // 使用stbi_load_from_memory替代 + unsigned char* textureData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha); + if (textureData == 0) return false; + pixels = (color*)textureData; + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, + height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); + + // set render vertices + Drawable::setVertices( + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, (float)height, + (float)width, (float)height, + (float)width, 0, + }, + new float [DRAWABLE_V_SIZE] { + 0, 0, + 0, 1, + 1, 1, + 1, 0 + } + ); + + return true; + } +} +} \ No newline at end of file diff --git a/src/libjin/render/texture.h b/src/libjin/render/texture.h new file mode 100644 index 0000000..6b0c699 --- /dev/null +++ b/src/libjin/render/texture.h @@ -0,0 +1,34 @@ +#ifndef __JIN_IMAGE_H +#define __JIN_IMAGE_H +#include "3rdparty/GLee/GLee.h" +#include "color.h" +#include "drawable.h" +namespace jin +{ +namespace render +{ + class Texture: public Drawable + { + + public: + + static Texture* createTexture(const char* file); + static Texture* createTexture(const void* mem, size_t size); + + ~Texture(); + + color getPixel(int x, int y); + + private: + + Texture(); + + bool loadb(const void* buffer, size_t size); + + color* pixels; + + }; +} +} + +#endif \ No newline at end of file diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 3357f07..1107a53 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -32,6 +32,8 @@ namespace render SDL_FlushEvent(SDL_WINDOWEVENT); } + SDL_GLContext ctx = NULL; + if (ctx) { SDL_GL_DeleteContext(ctx); @@ -74,16 +76,6 @@ namespace render SDL_DestroyWindow(wnd); } - SDL_Window* WindowSystem::getWnd() - { - return wnd; - } - - SDL_GLContext WindowSystem::getCtx() - { - return ctx; - } - inline void WindowSystem::swapBuffers() { if (wnd) diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h index c7e2651..48ead67 100644 --- a/src/libjin/render/window.h +++ b/src/libjin/render/window.h @@ -21,10 +21,6 @@ namespace render const char* title; // 标题 }; - SDL_Window* getWnd(); - - SDL_GLContext getCtx(); - inline int getW() { return width; @@ -46,8 +42,6 @@ namespace render SDL_Window* wnd; - SDL_GLContext ctx; - int width, height; onlyonce bool initSystem(const SettingBase* setting) override; diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp index c5648a5..d651e20 100644 --- a/src/lua/graphics/luaopen_Image.cpp +++ b/src/lua/graphics/luaopen_Image.cpp @@ -9,31 +9,31 @@ namespace jin namespace lua { - static inline Image* checkImage(lua_State* L) + static inline Texture* checkTexture(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); if (proxy != 0 && proxy != nullptr) - return (Image*)proxy->object; + return (Texture*)proxy->object; return nullptr; } static int l_getWidth(lua_State* L) { - Image* i = checkImage(L); + Texture* i = checkTexture(L); luax_pushnumber(L, i->getWidth()); return 1; } static int l_getHeight(lua_State *L) { - Image* i = checkImage(L); + Texture* i = checkTexture(L); luax_pushnumber(L, i->getHeight()); return 1; } static int l_getPixel(lua_State* L) { - Image* i = checkImage(L); + Texture* i = checkTexture(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); color c = i->getPixel(x, y); @@ -46,7 +46,7 @@ namespace lua static int l_setAnchor(lua_State* L) { - Image* i = checkImage(L); + Texture* i = checkTexture(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); i->setAnchor(x, y); @@ -55,7 +55,7 @@ namespace lua static int l_getSize(lua_State* L) { - Image* i = checkImage(L); + Texture* i = checkTexture(L); luax_pushnumber(L, i->getWidth()); luax_pushnumber(L, i->getHeight()); return 2; @@ -64,7 +64,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); - Image* img = (Image*)proxy->object; + Texture* img = (Texture*)proxy->object; delete img; return 0; } diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp index 236d8a6..5289a43 100644 --- a/src/lua/graphics/luaopen_JSL.cpp +++ b/src/lua/graphics/luaopen_JSL.cpp @@ -67,8 +67,8 @@ namespace lua case IMAGE: { Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); - Image* img = (Image*)proxy->object; - jsl->sendImage(variable, img); + Texture* tex = (Texture*)proxy->object; + jsl->sendTexture(variable, tex); break; } case CANVAS: diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index 44b0a2c..be40247 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -72,7 +72,7 @@ namespace lua fs->read(f, &b); Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy)); - Image* img = Image::createImage(b.data, b.size); + Texture* img = Texture::createTexture(b.data, b.size); proxy->bind(img); return 1; } @@ -142,8 +142,8 @@ namespace lua if (luax_istype(L, 1, TYPE_IMAGE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Image* img = (Image*)proxy->object; - img->draw(x, y, sx, sy, r); + Texture* tex = (Texture*)proxy->object; + tex->draw(x, y, sx, sy, r); } else if (luax_istype(L, 1, TYPE_CANVAS)) { -- cgit v1.1-26-g67d0