diff --git a/src/Makefile.am b/src/Makefile.am index b80ecc1f..e13f91d0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ include $(top_srcdir)/build/autotools/common.am -SUBDIRS = bullet lua data . t +SUBDIRS = bullet lua imgui data . t noinst_LIBRARIES = liblolcore.a @@ -9,7 +9,8 @@ liblolcore_a_SOURCES = \ tiler.cpp tiler.h dict.cpp dict.h \ audio.cpp audio.h scene.cpp scene.h font.cpp font.h \ entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ - tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ + textureimage.cpp textureimage.h tileset.cpp tileset.h \ + forge.cpp forge.h video.cpp video.h \ world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \ profiler.cpp profiler.h text.cpp text.h emitter.cpp emitter.h \ numeric.h utils.h messageservice.cpp messageservice.h \ @@ -25,8 +26,8 @@ liblolcore_a_SOURCES = \ $(sdl_sources) \ $(d3d9_sources) \ $(android_sources) -#liblolcore_a_CPPFLAGS = -include-pch $(srcdir)/lol/engine-internal.h.pch $(AM_CPPFLAGS) -I$(srcdir)/bullet -I$(srcdir)/lua -liblolcore_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/bullet -I$(srcdir)/lua +#liblolcore_a_CPPFLAGS = -include-pch $(srcdir)/lol/engine-internal.h.pch $(AM_CPPFLAGS) -I$(srcdir)/bullet -I$(srcdir)/lua -I$(srcdir)/imgui +liblolcore_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/bullet -I$(srcdir)/lua -I$(srcdir)/imgui EXTRA_DIST += easymesh/easymesh-scanner.l easymesh/easymesh-parser.y diff --git a/src/image/image.cpp b/src/image/image.cpp index ae59fce7..8fec290e 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -130,6 +130,15 @@ void Image::DummyFill() } } +void Image::Copy(uint8_t* pixels, ivec2 const& size, PixelFormat fmt) +{ + ASSERT(fmt != PixelFormat::Unknown); + SetFormat(fmt); + SetSize(size); + memcpy(m_data->m_pixels[(int)fmt]->data(), pixels, + size.x * size.y * BytesPerPixel(fmt)); +} + void Image::Copy(Image const &other) { ivec2 size = other.GetSize(); diff --git a/src/imgui/Makefile.am b/src/imgui/Makefile.am new file mode 100644 index 00000000..99eef13d --- /dev/null +++ b/src/imgui/Makefile.am @@ -0,0 +1,13 @@ + +include $(top_srcdir)/build/autotools/common.am + +noinst_LIBRARIES = libimgui.a + +libimgui_a_SOURCES = $(imgui_sources) +libimgui_a_CPPFLAGS = $(AM_CPPFLAGS) + +imgui_sources = \ + stb_rect_pack.h stb_textedit.h stb_truetype.h \ + imconfig.h imgui.cpp imgui.h \ + lolimgui.cpp lolimgui.h + diff --git a/src/imgui/lolimgui.cpp b/src/imgui/lolimgui.cpp new file mode 100644 index 00000000..9bbd4e61 --- /dev/null +++ b/src/imgui/lolimgui.cpp @@ -0,0 +1,299 @@ +// +// imGui integration in lolengine +// +// Copyright © 2009-2015 Benjamin "Touky" Huet +// +// This program is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include +#include "loldebug.h" + +using namespace lol; + +//----------------------------------------------------------------------------- +LolImGui::LolImGui() +{ + m_drawgroup = DRAWGROUP_HUD; +} +LolImGui::~LolImGui() +{ + ImGui::GetIO().Fonts->TexID = nullptr; + Ticker::Unref(m_font); + m_font = nullptr; +} + +//------------------------------------------------------------------------- +LolImGui* g_lolimgui = nullptr; +void LolImGui::Init() +{ + Ticker::Ref(g_lolimgui = new LolImGui()); + + ImGuiIO& io = ImGui::GetIO(); + //io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array. + //io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT; + //io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT; + //io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP; + //io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN; + //io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME; + //io.KeyMap[ImGuiKey_End] = GLFW_KEY_END; + //io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE; + //io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE; + //io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER; + //io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE; + //io.KeyMap[ImGuiKey_A] = GLFW_KEY_A; + //io.KeyMap[ImGuiKey_C] = GLFW_KEY_C; + //io.KeyMap[ImGuiKey_V] = GLFW_KEY_V; + //io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; + //io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; + //io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; + + //Func pointer + io.RenderDrawListsFn = LolImGui::RenderDrawLists; + io.SetClipboardTextFn = LolImGui::SetClipboard; + io.GetClipboardTextFn = LolImGui::GetClipboard; + + /* nope +#ifdef _MSC_VER + io.ImeWindowHandle = glfwGetWin32Window(g_Window); +#endif + */ + + /* Callback not needed but look into these to send IO stuff + if (install_callbacks) + { + glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); + glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); + glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback); + glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); + } + */ +} + +/* CALLBACKS +void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) +{ +if (action == GLFW_PRESS && button >= 0 && button < 3) +g_MousePressed[button] = true; +} + +void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) +{ +g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines. +} + +void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ +ImGuiIO& io = ImGui::GetIO(); +if (action == GLFW_PRESS) +io.KeysDown[key] = true; +if (action == GLFW_RELEASE) +io.KeysDown[key] = false; +io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0; +io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0; +} + +void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) +{ +ImGuiIO& io = ImGui::GetIO(); +if (c > 0 && c < 0x10000) +io.AddInputCharacter((unsigned short)c); +} + +*/ + +void LolImGui::Shutdown() +{ + Ticker::Unref(g_lolimgui); + + ImGui::Shutdown(); +} + +//------------------------------------------------------------------------- +static String g_clipboard; +void LolImGui::SetClipboard(const char* text) +{ + g_clipboard = text; +} +const char* LolImGui::GetClipboard() +{ + return g_clipboard.C(); +} + +//------------------------------------------------------------------------- +void LolImGui::TickGame(float seconds) +{ + super::TickGame(seconds); + + ImGuiIO& io = ImGui::GetIO(); + + //Init Texture + if (!m_font) + { + ImGuiIO& io = ImGui::GetIO(); + + // Build texture + unsigned char* pixels; + ivec2 size; + io.Fonts->GetTexDataAsAlpha8(&pixels, &size.w, &size.h); + + Image* image = new Image(); + image->Copy(pixels, size, PixelFormat::RGBA_8); + image->SetSize(size); + + Ticker::Ref(m_font = new TextureImage("", image)); + + //// Create texture + //glGenTextures(1, &g_FontTexture); + //glBindTexture(GL_TEXTURE_2D, g_FontTexture); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + //glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels); + + //// Store our identifier + //io.Fonts->TexID = (void *)(intptr_t)g_FontTexture; + + //return true; + } + //Texture has been created + if (m_font && m_font->GetTexture()) + { + io.Fonts->TexID = (void *)(intptr_t)m_font; + } + + // Setup display size (every frame to accommodate for window resizing) + vec2 video_size = vec2(0); + video_size = vec2(Video::GetSize()); + io.DisplaySize = ImVec2(video_size.w, video_size.h); + + // Setup time step + io.DeltaTime = seconds; + + // Setup inputs + // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents()) + //double mouse_x, mouse_y; + //glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); + //mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels + //mouse_y *= (float)display_h / h; + //io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) + + /* + for (int i = 0; i < 3; i++) + { + io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. + g_MousePressed[i] = false; + } + + io.MouseWheel = g_MouseWheel; + g_MouseWheel = 0.0f; + */ + + // Start the frame + ImGui::NewFrame(); + +} +void LolImGui::TickDraw(float seconds, Scene &scene) +{ + super::TickDraw(seconds, scene); + + ImGui::Render(); +} + + + + + +//// Data +//static GLFWwindow* g_Window = NULL; +//static double g_Time = 0.0f; +//static bool g_MousePressed[3] = { false, false, false }; +//static float g_MouseWheel = 0.0f; +//static GLuint g_FontTexture = 0; + +//------------------------------------------------------------------------- +// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure) +// If text or lines are blurry when integrating ImGui in your engine: +// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) +//------------------------------------------------------------------------- +void LolImGui::RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) +{ + if (cmd_lists_count == 0) + return; + + /* + // We are using the OpenGL fixed pipeline to make the example code simpler to read! + // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer. + // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers. + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glEnable(GL_SCISSOR_TEST); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + glEnable(GL_TEXTURE_2D); + + // Setup orthographic projection matrix + const float width = ImGui::GetIO().DisplaySize.x; + const float height = ImGui::GetIO().DisplaySize.y; + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + // Render command lists +#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) + for (int n = 0; n < cmd_lists_count; n++) + { + const ImDrawList* cmd_list = cmd_lists[n]; + const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front(); + glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos))); + glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv))); + glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col))); + + int vtx_offset = 0; + for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) + { + const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; + if (pcmd->user_callback) + { + pcmd->user_callback(cmd_list, pcmd); + } + else + { + glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id); + glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y)); + glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); + } + vtx_offset += pcmd->vtx_count; + } + } +#undef OFFSETOF + + // Restore modified state + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glPopAttrib(); + */ +} + diff --git a/src/imgui/lolimgui.h b/src/imgui/lolimgui.h new file mode 100644 index 00000000..d8e93759 --- /dev/null +++ b/src/imgui/lolimgui.h @@ -0,0 +1,64 @@ +// +// imGui integration in lolengine +// +// Copyright © 2009-2015 Benjamin "Touky" Huet +// +// This program is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +#include "imgui.h" + +namespace lol +{ + +//LolImGui -------------------------------------------------------------------- +class LolImGui : public Entity +{ + typedef Entity super; + + //------------------------------------------------------------------------- + LolImGui(); + ~LolImGui(); + char const *GetName() { return ""; } + + //------------------------------------------------------------------------- + static void Init(); + static void Shutdown(); + + //------------------------------------------------------------------------- + static void SetClipboard(const char* text); + static const char* GetClipboard(); + +protected: + virtual void TickGame(float seconds); + virtual void TickDraw(float seconds, Scene &scene); + + static void RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count); + + TextureImage* m_font = nullptr; +}; + +//bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks); +//void ImGui_ImplGlfw_Shutdown(); +//void ImGui_ImplGlfw_NewFrame(); +// +//// Use if you want to reset your rendering device without losing ImGui state. +//void ImGui_ImplGlfw_InvalidateDeviceObjects(); +//bool ImGui_ImplGlfw_CreateDeviceObjects(); +// +//// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization) +//// Provided here if you want to chain callbacks. +//// You can also handle inputs yourself and use those as a reference. +//void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); +//void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); +//void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); +//void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); + + +} /* namespace lol */ diff --git a/src/imgui/lolimgui.vcxproj b/src/imgui/lolimgui.vcxproj index d003a688..fd8927f4 100644 --- a/src/imgui/lolimgui.vcxproj +++ b/src/imgui/lolimgui.vcxproj @@ -62,14 +62,21 @@ + + + + + true + + diff --git a/src/lol/extras.h b/src/lol/extras.h index e59a16fd..b95fbb31 100644 --- a/src/lol/extras.h +++ b/src/lol/extras.h @@ -42,9 +42,11 @@ #include #include #include +#include #include #include #include +#include // Other objects #include diff --git a/src/lol/image/image.h b/src/lol/image/image.h index 11d8223a..3ff38724 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -72,6 +72,7 @@ public: ~Image(); void DummyFill(); + void Copy(uint8_t* pixels, ivec2 const& size, PixelFormat fmt); void Copy(Image const &other); bool Load(char const *path); bool Save(char const *path); diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index 7b5939af..7816411d 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -277,42 +277,43 @@ struct vec_t union { - struct { T x, y; }; - struct { T r, g; }; - struct { T s, t; }; - struct { T r, t; }; + struct { T x, y; }; /* axis */ + struct { T r, g; }; /* red, green */ + struct { T s, t; }; + struct { T r, t; }; /* radius, theta */ + struct { T w, h; }; /* width, height */ #if !_DOXYGEN_SKIP_ME - vec_t const xx, rr, ss/**/; - vec_t _____ xy, rg, st, rt; - vec_t _____ yx, gr, ts, tr; - vec_t const yy, gg, tt/**/; - - vec_t const xxx, rrr, sss /**/; - vec_t const xxy, rrg, sst, rrt; - vec_t const xyx, rgr, sts, rtr; - vec_t const xyy, rgg, stt, rtt; - vec_t const yxx, grr, tss, trr; - vec_t const yxy, grg, tst, trt; - vec_t const yyx, ggr, tts, ttr; - vec_t const yyy, ggg, ttt /**/; - - vec_t const xxxx, rrrr, ssss /**/; - vec_t const xxxy, rrrg, ssst, rrrt; - vec_t const xxyx, rrgr, ssts, rrtr; - vec_t const xxyy, rrgg, sstt, rrtt; - vec_t const xyxx, rgrr, stss, rtrr; - vec_t const xyxy, rgrg, stst, rtrt; - vec_t const xyyx, rggr, stts, rttr; - vec_t const xyyy, rggg, sttt, rttt; - vec_t const yxxx, grrr, tsss, trrr; - vec_t const yxxy, grrg, tsst, trrt; - vec_t const yxyx, grgr, tsts, trtr; - vec_t const yxyy, grgg, tstt, trtt; - vec_t const yyxx, ggrr, ttss, ttrr; - vec_t const yyxy, ggrg, ttst, ttrt; - vec_t const yyyx, gggr, ttts, tttr; - vec_t const yyyy, gggg, tttt /**/; + vec_t const xx, rr, ss/**/, ww; + vec_t _____ xy, rg, st, rt, wh; + vec_t _____ yx, gr, ts, tr, hw; + vec_t const yy, gg, tt/**/, hh; + + vec_t const xxx, rrr, sss /**/, www; + vec_t const xxy, rrg, sst, rrt, wwh; + vec_t const xyx, rgr, sts, rtr, whw; + vec_t const xyy, rgg, stt, rtt, whh; + vec_t const yxx, grr, tss, trr, hww; + vec_t const yxy, grg, tst, trt, hwh; + vec_t const yyx, ggr, tts, ttr, hhw; + vec_t const yyy, ggg, ttt /**/, hhh; + + vec_t const xxxx, rrrr, ssss /**/, wwww; + vec_t const xxxy, rrrg, ssst, rrrt, wwwh; + vec_t const xxyx, rrgr, ssts, rrtr, wwhw; + vec_t const xxyy, rrgg, sstt, rrtt, wwhh; + vec_t const xyxx, rgrr, stss, rtrr, whww; + vec_t const xyxy, rgrg, stst, rtrt, whwh; + vec_t const xyyx, rggr, stts, rttr, whhw; + vec_t const xyyy, rggg, sttt, rttt, whhh; + vec_t const yxxx, grrr, tsss, trrr, hwww; + vec_t const yxxy, grrg, tsst, trrt, hwwh; + vec_t const yxyx, grgr, tsts, trtr, hwhw; + vec_t const yxyy, grgg, tstt, trtt, hwhh; + vec_t const yyxx, ggrr, ttss, ttrr, hhww; + vec_t const yyxy, ggrg, ttst, ttrt, hhwh; + vec_t const yyyx, gggr, ttts, tttr, hhhw; + vec_t const yyyy, gggg, tttt /**/, hhhh; #endif T m_data[count]; @@ -434,49 +435,50 @@ struct vec_t union { - struct { T x, y, z; }; - struct { T r, g, b; }; + struct { T x, y, z; }; /* axis */ + struct { T r, g, b; }; /* red, green, blue */ struct { T s, t, p; }; - struct { T r, t, p; }; + struct { T r, t, p; }; /* radius, theta, phi */ + struct { T w, h, d; }; /* width, height, depth */ #if !_DOXYGEN_SKIP_ME - vec_t const xx, rr, ss/**/; - vec_t _____ xy, rg, st, rt; - vec_t _____ xz, rb, sp, rp; - vec_t _____ yx, gr, ts, tr; - vec_t const yy, gg, tt/**/; - vec_t _____ yz, gb, tp/**/; - vec_t _____ zx, br, ps, pr; - vec_t _____ zy, bg, pt/**/; - vec_t const zz, bb, pp/**/; - - vec_t const xxx, rrr, sss /**/; - vec_t const xxy, rrg, sst, rrt; - vec_t const xxz, rrb, ssp, rrp; - vec_t const xyx, rgr, sts, rtr; - vec_t const xyy, rgg, stt, rtt; - vec_t _____ xyz, rgb, stp, rtp; - vec_t const xzx, rbr, sps, rpr; - vec_t _____ xzy, rbg, spt, rpt; - vec_t const xzz, rbb, spp, rpp; - vec_t const yxx, grr, tss, trr; - vec_t const yxy, grg, tst, trt; - vec_t _____ yxz, grb, tsp, trp; - vec_t const yyx, ggr, tts, ttr; - vec_t const yyy, ggg, ttt /**/; - vec_t const yyz, ggb, ttp /**/; - vec_t _____ yzx, gbr, tps, tpr; - vec_t const yzy, gbg, tpt /**/; - vec_t const yzz, gbb, tpp /**/; - vec_t const zxx, brr, pss, prr; - vec_t _____ zxy, brg, pst, prt; - vec_t const zxz, brb, psp, prp; - vec_t _____ zyx, bgr, pts, ptr; - vec_t const zyy, bgg, ptt /**/; - vec_t const zyz, bgb, ptp /**/; - vec_t const zzx, bbr, pps, ppr; - vec_t const zzy, bbg, ppt /**/; - vec_t const zzz, bbb, ppp /**/; + vec_t const xx, rr, ss/**/, ww; + vec_t _____ xy, rg, st, rt, wh; + vec_t _____ xz, rb, sp, rp, wd; + vec_t _____ yx, gr, ts, tr, hw; + vec_t const yy, gg, tt/**/, hh; + vec_t _____ yz, gb, tp/**/, hd; + vec_t _____ zx, br, ps, pr, dw; + vec_t _____ zy, bg, pt/**/, dh; + vec_t const zz, bb, pp/**/, dd; + + vec_t const xxx, rrr, sss /**/, www; + vec_t const xxy, rrg, sst, rrt, wwh; + vec_t const xxz, rrb, ssp, rrp, wwd; + vec_t const xyx, rgr, sts, rtr, whw; + vec_t const xyy, rgg, stt, rtt, whh; + vec_t _____ xyz, rgb, stp, rtp, whd; + vec_t const xzx, rbr, sps, rpr, wdw; + vec_t _____ xzy, rbg, spt, rpt, wdh; + vec_t const xzz, rbb, spp, rpp, wdd; + vec_t const yxx, grr, tss, trr, hww; + vec_t const yxy, grg, tst, trt, hwh; + vec_t _____ yxz, grb, tsp, trp, hwd; + vec_t const yyx, ggr, tts, ttr, hhw; + vec_t const yyy, ggg, ttt /**/, hhh; + vec_t const yyz, ggb, ttp /**/, hhd; + vec_t _____ yzx, gbr, tps, tpr, hdw; + vec_t const yzy, gbg, tpt /**/, hdh; + vec_t const yzz, gbb, tpp /**/, hdd; + vec_t const zxx, brr, pss, prr, dww; + vec_t _____ zxy, brg, pst, prt, dwh; + vec_t const zxz, brb, psp, prp, dwd; + vec_t _____ zyx, bgr, pts, ptr, dhw; + vec_t const zyy, bgg, ptt /**/, dhh; + vec_t const zyz, bgb, ptp /**/, dhd; + vec_t const zzx, bbr, pps, ppr, ddw; + vec_t const zzy, bbg, ppt /**/, ddh; + vec_t const zzz, bbb, ppp /**/, ddd; vec_t const xxxx, rrrr, ssss; vec_t const xxxy, rrrg, ssst; @@ -654,8 +656,8 @@ struct vec_t union { - struct { T x, y, z, w; }; - struct { T r, g, b, a; }; + struct { T x, y, z, w; }; /* axis */ + struct { T r, g, b, a; }; /* red, green, blue, alpha */ struct { T s, t, p, q; }; #if !_DOXYGEN_SKIP_ME diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index f3b0960f..a19e864f 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -225,6 +225,7 @@ + @@ -381,6 +382,8 @@ + + @@ -406,7 +409,9 @@ - + + true + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 273d5f77..a5f18e60 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -91,6 +91,12 @@ {25cc0513-ad71-4fbe-bd24-acc88aa66833} + + {7fb4105d-4f17-4824-8d40-948553fdcde1} + + + {3bd5fe6f-c9cd-45f3-98a9-958d86d415e8} + @@ -210,9 +216,6 @@ ... - - ... - ... @@ -249,21 +252,12 @@ ... - - ... - - - ... - ... ... - - ... - gpu @@ -415,6 +409,21 @@ sys + + tileset + + + tileset + + + entities + + + entities + + + tileset + @@ -537,9 +546,6 @@ ... - - ... - ... @@ -588,15 +594,6 @@ ... - - ... - - - ... - - - ... - ... @@ -775,6 +772,24 @@ lol\sys + + tileset + + + tileset + + + entities + + + entities + + + tileset + + + tileset + @@ -795,9 +810,6 @@ ... - - ... - easymesh @@ -819,6 +831,9 @@ ... + + tileset + diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index 8b192231..4e51ab7e 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -322,6 +322,7 @@ protected: } }; //----------------------------------------------------------------------------- +/* TODO: FIX THAT TOUKY !! template class VarPtrLight { @@ -337,7 +338,7 @@ protected: m_value = obj ? *obj : nullptr; } }; - +*/ //----------------------------------------------------------------------------- template class Var diff --git a/src/lua/lollua.vcxproj b/src/lua/lollua.vcxproj index df9ff7bf..49f2a5d6 100644 --- a/src/lua/lollua.vcxproj +++ b/src/lua/lollua.vcxproj @@ -123,6 +123,11 @@ + + + true + + diff --git a/src/textureimage-private.h b/src/textureimage-private.h new file mode 100644 index 00000000..42179a90 --- /dev/null +++ b/src/textureimage-private.h @@ -0,0 +1,39 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The TileSet class +// ----------------- +// A TileSet is a collection of tiles stored in a texture. Texture uploading +// and freeing is done in the render tick method. When the refcount drops to +// zero, the texture is freed. +// + +namespace lol +{ + +class TextureImageData +{ + friend class TextureImage; + + //T'was protected .... should it be ? +public: + String m_name; + + /* Pixels, then texture coordinates */ + ivec2 m_image_size, m_texture_size; + + Image *m_image = nullptr; + Texture *m_texture = nullptr; +}; + +} /* namespace lol */ diff --git a/src/textureimage.cpp b/src/textureimage.cpp new file mode 100644 index 00000000..bf76cf70 --- /dev/null +++ b/src/textureimage.cpp @@ -0,0 +1,186 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2014 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +#include + +#include +#include +#include + +#if defined(_WIN32) && !defined(_XBOX) +# define WIN32_LEAN_AND_MEAN +# include +# if defined USE_D3D9 +# define FAR +# define NEAR +# include +# endif +#endif + +#include "textureimage-private.h" + +namespace lol +{ + +/* + * TileSet implementation class + */ + +TextureImageData* TextureImage::GetNewData() +{ + return new TextureImageData(); +} + +/* + * Public TextureImage class + */ + +TextureImage::TextureImage(char const *path) + : m_data(GetNewData()) +{ + Init(path); +} + +TextureImage::TextureImage(char const *path, Image* image) + : m_data(GetNewData()) +{ + Init(path, image); +} + +TextureImage::~TextureImage() +{ + delete m_data; +} + +void TextureImage::Init(char const *path) +{ + Init(path, new Image(path)); +} + +void TextureImage::Init(char const *path, Image* image) +{ + m_data->m_name = String(" ") + path; + + m_data->m_texture = nullptr; + m_data->m_image = image; + m_data->m_image_size = m_data->m_image->GetSize(); + m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x), + PotUp(m_data->m_image_size.y)); + + m_drawgroup = DRAWGROUP_BEFORE; +} + +void TextureImage::TickDraw(float seconds, Scene &scene) +{ + super::TickDraw(seconds, scene); + + if (IsDestroying()) + { + if (m_data->m_image) + { + delete m_data->m_image; + m_data->m_image = nullptr; + } + else + { + delete m_data->m_texture; + m_data->m_texture = nullptr; + } + } + else if (m_data->m_image) + { + //Update texture is needed + if (m_data->m_texture) + { + delete m_data->m_texture; + m_data->m_texture = nullptr; + } + + PixelFormat format = m_data->m_image->GetFormat(); + int planes = BytesPerPixel(format); + + int w = m_data->m_texture_size.x; + int h = m_data->m_texture_size.y; + + uint8_t *pixels = (uint8_t *)m_data->m_image->Lock(); + bool resized = false; + if (w != m_data->m_image_size.x || h != m_data->m_image_size.y) + { + uint8_t *tmp = new uint8_t[planes * w * h]; + for (int line = 0; line < m_data->m_image_size.y; line++) + memcpy(tmp + planes * w * line, + pixels + planes * m_data->m_image_size.x * line, + planes * m_data->m_image_size.x); + pixels = tmp; + resized = false; + } + + m_data->m_texture = new Texture(ivec2(w, h), format); + m_data->m_texture->SetData(pixels); + + if (resized) + delete[] pixels; + delete m_data->m_image; + m_data->m_image = nullptr; + } +} + +//----------------------------------------------------------------------------- +char const *TextureImage::GetName() +{ + return m_data->m_name.C(); +} + +void TextureImage::UpdateTexture(Image* image) +{ + m_data->m_image = image; + m_data->m_image_size = m_data->m_image->GetSize(); + m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x), + PotUp(m_data->m_image_size.y)); +} + +Texture * TextureImage::GetTexture() +{ + return m_data->m_texture; +} + +Texture const * TextureImage::GetTexture() const +{ + return m_data->m_texture; +} + +Image * TextureImage::GetImage() +{ + return m_data->m_image; +} + +Image const * TextureImage::GetImage() const +{ + return m_data->m_image; +} + +ivec2 TextureImage::GetTextureSize() const +{ + return m_data->m_texture_size; +} + +void TextureImage::Bind() +{ + if (!m_data->m_image && m_data->m_texture) + m_data->m_texture->Bind(); +} + +void TextureImage::Unbind() +{ + ; +} + +} /* namespace lol */ + diff --git a/src/textureimage.h b/src/textureimage.h new file mode 100644 index 00000000..14607f70 --- /dev/null +++ b/src/textureimage.h @@ -0,0 +1,69 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The TileSet class +// ----------------- +// A TileSet is a collection of tiles stored in a texture. Texture uploading +// and freeing is done in the render tick method. When the refcount drops to +// zero, the texture is freed. +// + +#include +#include + +#include + +#include "entity.h" + +namespace lol +{ + +class TextureImageData; + +class TextureImage : public Entity +{ + typedef Entity super; +protected: + virtual TextureImageData* GetNewData(); + +public: + TextureImage(char const *path); + TextureImage(char const *path, Image* image); + virtual ~TextureImage(); + +protected: + void Init(char const *path); + virtual void Init(char const *path, Image* image); + +protected: + virtual void TickDraw(float seconds, Scene &scene); + +public: + /* Inherited from Entity */ + virtual char const *GetName(); + + void UpdateTexture(Image* image); + Texture * GetTexture(); + Texture const * GetTexture() const; + Image * GetImage(); + Image const * GetImage() const; + ivec2 GetTextureSize() const; + void Bind(); + void Unbind(); + +protected: + TextureImageData* m_data = nullptr; +}; + +} /* namespace lol */ + diff --git a/src/tileset.cpp b/src/tileset.cpp index e1f10ef1..ad8587a2 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -24,6 +24,8 @@ # endif #endif +#include "textureimage-private.h" + namespace lol { @@ -31,30 +33,38 @@ namespace lol * TileSet implementation class */ -class TileSetData +class TileSetData : public TextureImageData { friend class TileSet; -private: - String m_name; - +protected: /* Pixels, then texture coordinates */ array m_tiles; - ivec2 m_tile_size, m_image_size, m_texture_size; - - Image *m_image; - Texture *m_texture; + ivec2 m_tile_size; }; +TextureImageData* TileSet::GetNewData() +{ + return new TileSetData(); +} + +TileSetData* TileSet::GetData() +{ + return (TileSetData*)m_data; +} + +TileSetData const* TileSet::GetData() const +{ + return (TileSetData const*)m_data; +} + /* * Public TileSet class */ TileSet::TileSet(char const *path) - : m_data(new TileSetData()) + : TextureImage(path) { - Init(path); - array tiles; if (m_data->m_image->RetrieveTiles(tiles)) for (ptrdiff_t i = 0; i < tiles.Count(); i++) @@ -62,10 +72,8 @@ TileSet::TileSet(char const *path) } TileSet::TileSet(char const *path, Image* image) - : m_data(new TileSetData()) + : TextureImage(path, image) { - Init(path, image); - array tiles; if (m_data->m_image->RetrieveTiles(tiles)) for (ptrdiff_t i = 0; i < tiles.Count(); i++) @@ -73,10 +81,8 @@ TileSet::TileSet(char const *path, Image* image) } TileSet::TileSet(char const *path, ivec2 size, ivec2 count) - : m_data(new TileSetData()) + : TileSet(path) { - Init(path); - /* If count is valid, fix size; otherwise, fix count. */ if (count.x > 0 && count.y > 0) { @@ -95,18 +101,11 @@ TileSet::TileSet(char const *path, ivec2 size, ivec2 count) AddTile(ibox2(size * ivec2(i, j), size * ivec2(i + 1, j + 1))); } - - array tiles; - if (m_data->m_image->RetrieveTiles(tiles)) - for (ptrdiff_t i = 0; i < tiles.Count(); i++) - AddTile(ibox2(tiles[i].m1, tiles[i].m1 + tiles[i].m2)); } TileSet::TileSet(char const *path, Image* image, ivec2 size, ivec2 count) - : m_data(new TileSetData()) + : TileSet(path, image) { - Init(path, image); - /* If count is valid, fix size; otherwise, fix count. */ if (count.x > 0 && count.y > 0) { @@ -125,38 +124,33 @@ TileSet::TileSet(char const *path, Image* image, ivec2 size, ivec2 count) AddTile(ibox2(size * ivec2(i, j), size * ivec2(i + 1, j + 1))); } - - array tiles; - if (m_data->m_image->RetrieveTiles(tiles)) - for (ptrdiff_t i = 0; i < tiles.Count(); i++) - AddTile(ibox2(tiles[i].m1, tiles[i].m1 + tiles[i].m2)); } -void TileSet::Init(char const *path) +TileSet::~TileSet() { - Init(path, new Image(path)); } void TileSet::Init(char const *path, Image* image) { - m_data->m_name = String(" ") + path; + super::Init(path, image); - m_palette = nullptr; - m_data->m_texture = 0; - m_data->m_image = image; - m_data->m_image_size = m_data->m_image->GetSize(); - m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x), - PotUp(m_data->m_image_size.y)); + m_data->m_name = String(" ") + path; +} - m_drawgroup = DRAWGROUP_BEFORE; +//Inherited from Entity ------------------------------------------------------- +char const *TileSet::GetName() +{ + return m_data->m_name.C(); } +//New methods ----------------------------------------------------------------- ptrdiff_t TileSet::AddTile(ibox2 rect) { - m_data->m_tiles.Push(rect, - box2((vec2)rect.A / (vec2)m_data->m_texture_size, - (vec2)rect.B / (vec2)m_data->m_texture_size)); - return m_data->m_tiles.Count() - 1; + TileSetData* data = GetData(); + data->m_tiles.Push(rect, + box2((vec2)rect.A / (vec2)m_data->m_texture_size, + (vec2)rect.B / (vec2)m_data->m_texture_size)); + return data->m_tiles.Count() - 1; } void TileSet::AddTile(ivec2 count) @@ -171,100 +165,18 @@ void TileSet::AddTile(ivec2 count) } } -TileSet::~TileSet() -{ - delete m_data; -} - -void TileSet::TickDraw(float seconds, Scene &scene) -{ - Entity::TickDraw(seconds, scene); - - if (IsDestroying()) - { - if (m_data->m_image) - { - delete m_data->m_image; - m_data->m_image = nullptr; - } - else - { - delete m_data->m_texture; - m_data->m_texture = nullptr; - } - } - else if (m_data->m_image) - { - PixelFormat format = m_data->m_image->GetFormat(); - int planes = BytesPerPixel(format); - - int w = m_data->m_texture_size.x; - int h = m_data->m_texture_size.y; - - uint8_t *pixels = (uint8_t *)m_data->m_image->Lock(); - bool resized = false; - if (w != m_data->m_image_size.x || h != m_data->m_image_size.y) - { - uint8_t *tmp = new uint8_t[planes * w * h]; - for (int line = 0; line < m_data->m_image_size.y; line++) - memcpy(tmp + planes * w * line, - pixels + planes * m_data->m_image_size.x * line, - planes * m_data->m_image_size.x); - pixels = tmp; - resized = false; - } - - m_data->m_texture = new Texture(ivec2(w, h), format); - m_data->m_texture->SetData(pixels); - - if (resized) - delete[] pixels; - delete m_data->m_image; - m_data->m_image = nullptr; - } -} - -char const *TileSet::GetName() -{ - return m_data->m_name.C(); -} - ptrdiff_t TileSet::GetTileCount() const { - return m_data->m_tiles.Count(); + return GetData()->m_tiles.Count(); } ivec2 TileSet::GetTileSize(ptrdiff_t tileid) const { - ibox2 const &box = m_data->m_tiles[tileid].m1; + ibox2 const &box = GetData()->m_tiles[tileid].m1; return box.B - box.A; } -ivec2 TileSet::GetTextureSize() const -{ - return m_data->m_texture_size; -} - -Texture * TileSet::GetTexture() -{ - return m_data->m_texture; -} - -Texture const * TileSet::GetTexture() const -{ - return m_data->m_texture; -} - -Image * TileSet::GetImage() -{ - return m_data->m_image; -} - -Image const * TileSet::GetImage() const -{ - return m_data->m_image; -} - +//Palette --------------------------------------------------------------------- void TileSet::SetPalette(TileSet* palette) { m_palette = palette; @@ -280,22 +192,12 @@ TileSet const* TileSet::GetPalette() const return m_palette; } -void TileSet::Bind() -{ - if (!m_data->m_image && m_data->m_texture) - m_data->m_texture->Bind(); -} - -void TileSet::Unbind() -{ - ; -} - void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale, float angle, vec3 *vertex, vec2 *texture) { - ibox2 pixels = m_data->m_tiles[id].m1; - box2 texels = m_data->m_tiles[id].m2; + TileSetData* data = GetData(); + ibox2 pixels = data->m_tiles[id].m1; + box2 texels = data->m_tiles[id].m2; float dtx = texels.B.x - texels.A.x; float dty = texels.B.y - texels.A.y; float tx = texels.A.x; diff --git a/src/tileset.h b/src/tileset.h index fd4314fb..27886fc6 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -18,59 +18,60 @@ // zero, the texture is freed. // +/* #include #include #include #include "entity.h" +*/ +#include "textureimage.h" namespace lol { +class TextureImageData; class TileSetData; -class TileSet : public Entity +class TileSet : public TextureImage { + typedef TextureImage super; +protected: + virtual TextureImageData* GetNewData(); + TileSetData* GetData(); + TileSetData const* GetData() const; + public: TileSet(char const *path); TileSet(char const *path, Image* image); - virtual ~TileSet(); /* Old style: path to PNG file */ TileSet(char const *path, ivec2 size, ivec2 count); TileSet(char const *path, Image* image, ivec2 size, ivec2 count); + + virtual ~TileSet(); - /* Inherited from Entity */ - virtual char const *GetName(); protected: - virtual void TickDraw(float seconds, Scene &scene); + virtual void Init(char const *path, Image* image); public: + /* Inherited from Entity */ + virtual char const *GetName(); + /* New methods */ ptrdiff_t AddTile(ibox2 rect); void AddTile(ivec2 count); ptrdiff_t GetTileCount() const; ivec2 GetTileSize(ptrdiff_t tileid) const; - Texture * GetTexture(); - Texture const * GetTexture() const; - Image * GetImage(); - Image const * GetImage() const; void SetPalette(TileSet* palette); TileSet* GetPalette(); TileSet const * GetPalette() const; - ivec2 GetTextureSize() const; - void Bind(); - void Unbind(); void BlitTile(uint32_t id, vec3 pos, int o, vec2 scale, float angle, vec3 *vertex, vec2 *texture); -private: - void Init(char const *path); - void Init(char const *path, Image* image); - - TileSetData* m_data; +protected: TileSet* m_palette; };