Map editor started

Rendering transparancy to an image working
Saving randered images working
Gui_Panel_Refactor
Joeyrp 4 years ago
parent 3f7ae8aaa1
commit ca9d25073e

@ -24,6 +24,7 @@ Core:
✔ Test rotation of images @done (11/1/2021, 2:11:13 PM) ✔ Test rotation of images @done (11/1/2021, 2:11:13 PM)
☐ Fix line rotation @low ☐ Fix line rotation @low
✔ Add Roboto-Regular.ttf as an internal font @high @done (11/3/2021, 8:35:51 PM) ✔ Add Roboto-Regular.ttf as an internal font @high @done (11/3/2021, 8:35:51 PM)
☐ Allow an image size to be passed in for rendering to an image @high
GUI: GUI:

@ -30,7 +30,7 @@ namespace lunarium
protected: // Interface methods for use only by the engine protected: // Interface methods for use only by the engine
friend Core; friend Core;
IGraphics() : mbIsInit(false) {} IGraphics() : mbIsInit(false), mpFBTexture(nullptr) {}
virtual OpRes Initialize(Window* pWindow, bool enableDebugMessages = true) = 0; virtual OpRes Initialize(Window* pWindow, bool enableDebugMessages = true) = 0;
virtual void Shutdown() = 0; virtual void Shutdown() = 0;

@ -149,15 +149,14 @@ namespace lunarium
// Buffer width and height (in pixels) is the same as the screen size // Buffer width and height (in pixels) is the same as the screen size
// Need to multiply these by the number bytes per pixel // Need to multiply these by the number bytes per pixel
//int bufferSize = mFBWidth * mFBHeight * 4; // NOTE: Assuming 4 channels for now int bufferSize = mFBWidth * mFBHeight * 4; // NOTE: Assuming 4 channels for now
//unsigned char* buffer = new unsigned char[bufferSize]; // TODO: Preallocate the buffer so we don't call new every frame!
//glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)buffer); unsigned char* buffer = new unsigned char[bufferSize];
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)buffer);
//FlipImageVertically(buffer, mFBWidth, mFBHeight, 1);
// FlipImageVertically(buffer, mFBWidth, mFBHeight, 1);
//glSetTexImage() // mpFBTexture->FreeRawData();
mpFBTexture->SetData(buffer);
mpFBTexture->FreeRawData();
mpFBTexture->SetWidth(mFBWidth); mpFBTexture->SetWidth(mFBWidth);
mpFBTexture->SetHeight(mFBHeight); mpFBTexture->SetHeight(mFBHeight);
mpFBTexture->SetFormat(ImageFormat::RGBA); mpFBTexture->SetFormat(ImageFormat::RGBA);
@ -595,7 +594,7 @@ namespace lunarium
glGenTextures(1, &id); glGenTextures(1, &id);
mpFBTexture->SetGLTextureID(id); mpFBTexture->SetGLTextureID(id);
glBindTexture(GL_TEXTURE_2D, mpFBTexture->GetGLTextureID()); glBindTexture(GL_TEXTURE_2D, mpFBTexture->GetGLTextureID());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mFBWidth, mFBHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mFBWidth, mFBHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);

@ -66,6 +66,12 @@ namespace lunarium
return *this; return *this;
} }
void Image::SetData(unsigned char* data)
{
mRawData = data;
}
unsigned int Image::GetGLTextureID() const unsigned int Image::GetGLTextureID() const
{ {
return mGLTextureID; return mGLTextureID;

@ -29,6 +29,7 @@ namespace lunarium
ImageFormat GetFormat() const; ImageFormat GetFormat() const;
const unsigned char* GetData() const; const unsigned char* GetData() const;
void SetData(unsigned char* data);
void SetGLTextureID(unsigned int id); void SetGLTextureID(unsigned int id);
void SetWidth(int width); void SetWidth(int width);
void SetHeight(int height); void SetHeight(int height);

@ -1,5 +1,19 @@
add_library(editor editor.cpp project/project.cpp panels/iPanel.cpp panels/mainPanel.cpp panels/about.cpp
panels/assetBrowser.cpp panels/worldTree.cpp panels/worldView.cpp panels/propertiesView.cpp) # Source Files
set(EDITOR_SRC
"editor.cpp"
"project/project.cpp"
"panels/iPanel.cpp"
"panels/mainPanel.cpp"
"panels/about.cpp"
"panels/assetBrowser.cpp"
"panels/worldTree.cpp"
"panels/worldView.cpp"
"panels/propertiesView.cpp"
"tools/map_editor/map_editor.cpp"
)
add_library(editor ${EDITOR_SRC})
target_include_directories(editor target_include_directories(editor
PUBLIC "${PROJECT_BINARY_DIR}" PUBLIC "${PROJECT_BINARY_DIR}"

@ -21,6 +21,9 @@
#include "panels/worldView.h" #include "panels/worldView.h"
#include "panels/propertiesView.h" #include "panels/propertiesView.h"
// Tools
#include "tools/map_editor/map_editor.h"
namespace lunarium namespace lunarium
{ {
namespace editor namespace editor
@ -28,7 +31,7 @@ namespace editor
Editor::Editor() Editor::Editor()
: mLogCat(-1), mpMainPanel(nullptr), mpFileBrowser(nullptr), mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false), : mLogCat(-1), mpMainPanel(nullptr), mpFileBrowser(nullptr), mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false),
mDoSaveProject(false), mDoSaveAs(false) mDoSaveProject(false), mDoSaveAs(false), mpMapEditor(nullptr)
{ {
} }
@ -67,7 +70,7 @@ namespace editor
HandleMenuEvents(); HandleMenuEvents();
} }
void Editor::OnRender(IGraphics* g) void Editor::OnRender(lunarium::IGraphics* g)
{ {
mpMainPanel->DoFrame(); mpMainPanel->DoFrame();
@ -94,6 +97,24 @@ namespace editor
return mLogCat; return mLogCat;
} }
bool Editor::IsToolOpen(ToolType type) const
{
switch (type)
{
case ToolType::TT_MAP_EDITOR:
{
if (!mpMapEditor)
return false;
return mpMapEditor->IsOpen();
}
}
Logger::Log(mLogCat, LogLevel::WARNING, "And unknown ToolType was passed into Editor::IsToolOpen: %n", type);
return false;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// HELPER METHODS // HELPER METHODS
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -110,6 +131,18 @@ namespace editor
{ {
delete mPanels[PanelType::PT_ABOUT]; delete mPanels[PanelType::PT_ABOUT];
delete mPanels[PanelType::PT_ASSET_BROWSER]; delete mPanels[PanelType::PT_ASSET_BROWSER];
delete mPanels[PanelType::PT_WORLD_TREE];
delete mPanels[PanelType::PT_WORLD_VIEW];
delete mPanels[PanelType::PT_PROPERTIES_VIEW];
mPanels.clear();
}
void Editor::DestroyTools()
{
mpMapEditor->Shutdown();
delete mpMapEditor;
mpMapEditor = nullptr;
} }
void Editor::HandleMenuEvents() void Editor::HandleMenuEvents()
@ -256,5 +289,17 @@ namespace editor
mPanels[PanelType::PT_ABOUT]->SetOpen(true); mPanels[PanelType::PT_ABOUT]->SetOpen(true);
} }
void Editor::OpenMapEditor()
{
if (!mpMapEditor)
{
mpMapEditor = new MapEditor;
mpMapEditor->Initialize(this);
}
mpMapEditor->Open();
}
} }
} }

@ -24,7 +24,12 @@ namespace lunarium
namespace editor namespace editor
{ {
enum ToolType
{
TT_MAP_EDITOR,
};
class MapEditor;
class MainPanel; class MainPanel;
class Editor : public iRunMode class Editor : public iRunMode
{ {
@ -33,10 +38,11 @@ namespace lunarium
OpRes Initialize(); OpRes Initialize();
void Shutdown(); void Shutdown();
void OnTick(double delta); void OnTick(double delta);
void OnRender(IGraphics* g); void OnRender(lunarium::IGraphics* g);
uint32_t GetLogCat() const; uint32_t GetLogCat() const;
bool IsToolOpen(ToolType type) const;
// Menu Bar Events // Menu Bar Events
void NewProject(); void NewProject();
@ -44,6 +50,7 @@ namespace lunarium
void SaveProject(); void SaveProject();
void SaveAs(); void SaveAs();
void Exit(); void Exit();
void OpenMapEditor();
void ShowAboutPanel(); void ShowAboutPanel();
@ -58,6 +65,9 @@ namespace lunarium
Project mProject; Project mProject;
// Tools
MapEditor* mpMapEditor;
FileBrowser* mpFileBrowser; FileBrowser* mpFileBrowser;
const std::filesystem::path* mpPath; const std::filesystem::path* mpPath;
@ -72,6 +82,7 @@ namespace lunarium
private: // HELPERS private: // HELPERS
void CreatePanels(); void CreatePanels();
void DestoryPanels(); void DestoryPanels();
void DestroyTools();
void HandleMenuEvents(); void HandleMenuEvents();
}; };

@ -138,10 +138,42 @@ namespace editor
if (ImGui::BeginMenu("Windows")) if (ImGui::BeginMenu("Windows"))
{ {
ImGui::EndMenu(); ImGui::EndMenu();
} }
// Tools
if (ImGui::BeginMenu("Tools"))
{
if (ImGui::MenuItem("Map Editor"))
{
mpEditor->OpenMapEditor();
}
ImGui::EndMenu();
}
// TODO: If map editor is open show it's menu
if (mpEditor->IsToolOpen(ToolType::TT_MAP_EDITOR))
{
if (ImGui::BeginMenu("Map Editor"))
{
if (ImGui::BeginMenu("Windows"))
{
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Settings"))
{
ImGui::EndMenu();
}
ImGui::EndMenu();
}
}
if (ImGui::BeginMenu("Help")) if (ImGui::BeginMenu("Help"))
{ {
if (ImGui::MenuItem("About")) if (ImGui::MenuItem("About"))

@ -0,0 +1,127 @@
/******************************************************************************
* File - map_editor.h
* Author - Joey Pollack
* Date - 2022/01/31 (y/m/d)
* Mod Date - 2022/01/31 (y/m/d)
* Description - The tile map editor tool. Controls subwindows/panels.
******************************************************************************/
#include "map_editor.h"
#include <editor/editor.h>
#include <editor/panels/iPanel.h>
#include <dearimgui/imgui.h>
namespace lunarium { namespace editor
{
MapEditor::MapEditor()
: mpEditor(nullptr), mIsOpen(false), mMapLoaded(false)
{
}
OpRes MapEditor::Initialize(Editor* editor)
{
if (mpEditor)
{
return OpRes::Fail("MapEditor is already initialized");
}
mpEditor = editor;
Open();
return OpRes::OK();
}
void MapEditor::Shutdown()
{
mpEditor = nullptr;
}
void MapEditor::OnTick(double delta)
{
for (int i = 0; i < mPanels.size(); i++)
{
mPanels[i]->Update(delta);
}
}
bool MapEditor::OnRender(IGraphics* g)
{
if (!mIsOpen)
{
Close();
return false;
}
ImGui::SetNextWindowSize(ImVec2(800, 500), ImGuiCond_FirstUseEver);
if (!ImGui::Begin("Map Editor", &mIsOpen, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus))
{
Close();
ImGui::End();
return false;
}
for (int i = 0; i < mPanels.size(); i++)
{
mPanels[i]->DoFrame();
}
ImGui::End();
return true;
}
void MapEditor::Open()
{
if (mIsOpen)
{
return;
}
for (int i = 0; i < mPanels.size(); i++)
{
mPanels[i]->SetOpen(true);
}
mIsOpen = true;
}
void MapEditor::Close()
{
for (int i = 0; i < mPanels.size(); i++)
{
mPanels[i]->SetOpen(false);
}
mIsOpen = false;
}
bool MapEditor::IsOpen() const
{
return mIsOpen;
}
void MapEditor::NewMap()
{
}
OpRes MapEditor::LoadMap(std::string map)
{
return OpRes::Fail("MapEditor::LoadMap not implemented");
}
void MapEditor::SaveMap()
{
}
bool MapEditor::IsMapLoaded() const
{
return mMapLoaded;
}
}}

@ -0,0 +1,57 @@
/******************************************************************************
* File - map_editor.h
* Author - Joey Pollack
* Date - 2022/01/31 (y/m/d)
* Mod Date - 2022/01/31 (y/m/d)
* Description - The tile map editor tool. Controls subwindows/panels.
******************************************************************************/
#ifndef MAP_EDITOR_H_
#define MAP_EDITOR_H_
#include <vector>
#include <string>
#include <utils/opRes.h>
namespace lunarium { namespace editor
{
class Editor;
class IGraphics;
class Panel;
class MapEditor
{
public:
MapEditor();
OpRes Initialize(Editor* editor);
void Shutdown();
void OnTick(double delta);
bool OnRender(IGraphics* g);
void Open();
void Close();
bool IsOpen() const;
void NewMap();
OpRes LoadMap(std::string map);
void SaveMap();
bool IsMapLoaded() const;
private:
bool mIsOpen;
bool mMapLoaded;
bool mIsDirty;
Editor* mpEditor;
std::vector<Panel*> mPanels;
enum PanelTypes
{
MPPT_MAP_VIEW,
MPPT_TILE_PALLET,
MPPT_PROPERTIES
};
};
}}
#endif // MAP_EDITOR_H_

@ -13,6 +13,7 @@
#include <graphics/igraphics.h> #include <graphics/igraphics.h>
#include <input/inputManager.h> #include <input/inputManager.h>
#include <assets/types/image.h> #include <assets/types/image.h>
#include <utils/stb/std_image_write.h>
namespace lunarium namespace lunarium
{ {
@ -87,6 +88,33 @@ namespace lunarium
angle -= 0.1f; angle -= 0.1f;
} }
if (Core::Input().IsKeyPressed(KeyCode::Q))
{
// Test writing out a rendered image with transparency
IGraphics& g = Core::Graphics();
Color prev = g.GetClearColor();
g.SetClearColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
OpRes result = Core::GetInstance().BeginRenderToTexture();
if (Failed(result))
{
Logger::Log(mLogCat, LogLevel::WARNING, "Unable to render to texture: %s", result.Description.c_str());
return;
}
Logger::Log(mLogCat, LogLevel::INFO, "Running transparent image test");
g.DrawFilledBox(Rectangle(500, 400, 300, 300), Color(0.5f, 0.0f, 0.75f, 1.0f));
g.DrawString("This is a test of rendering an image with transparency",
Rectangle::MakeFromTopLeft(50, 50, 600, 200), Color(0.0f, 1.0f, 0.2f, 1.0f), 0.5f);
mpRenderedImage = Core::GetInstance().EndRenderToTexture();
g.SetClearColor(prev);
stbi_flip_vertically_on_write(1);
stbi_write_png("lunarium_test_image.png", mpRenderedImage->GetWidth(), mpRenderedImage->GetHeight(), 4,
mpRenderedImage->GetData(), mpRenderedImage->GetWidth() * 4);
}
mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280); mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280);
mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720); mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720);
@ -106,6 +134,8 @@ namespace lunarium
mpRenderedImage = Core::GetInstance().EndRenderToTexture(); mpRenderedImage = Core::GetInstance().EndRenderToTexture();
box_angle += 0.01f; box_angle += 0.01f;
} }
@ -114,10 +144,10 @@ namespace lunarium
g->DrawImage(*mpRenderedImage, Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()), g->DrawImage(*mpRenderedImage, Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()),
Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f), angle); Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f), angle);
g->DrawBox(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(0.0f, 0.0f, 0.0f, 1.0f), 1.0f, angle); // g->DrawBox(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(0.0f, 0.0f, 0.0f, 1.0f), 1.0f, angle);
g->DrawBox(Rectangle(400, 400, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f), 2.0f, box_angle); // g->DrawBox(Rectangle(400, 400, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f), 2.0f, box_angle);
g->DrawString((*mGrid[{-2, 0,}])->msg.c_str(), Rectangle::MakeFromTopLeft(200.0f, 500.0f, 500.0f, 200.0f), Color(0.5f, 0.0f, 0.75f, 1.0f)); // g->DrawString((*mGrid[{-2, 0,}])->msg.c_str(), Rectangle::MakeFromTopLeft(200.0f, 500.0f, 500.0f, 200.0f), Color(0.5f, 0.0f, 0.75f, 1.0f));
} }
} }

@ -1,6 +1,6 @@
<State> <State>
<DataDirectory>data/</DataDirectory> <DataDirectory>data/</DataDirectory>
<Mode Type="editor" /> <Mode Type="test" />
<Display Renderer="opengl" IsFullScreen="false" VSyncEnabled="true"> <Display Renderer="opengl" IsFullScreen="false" VSyncEnabled="true">
<FullScreenResolution Width="1920" Height="1080" /> <FullScreenResolution Width="1920" Height="1080" />
<WindowedSize Width="1280" Height="720" /> <WindowedSize Width="1280" Height="720" />

Loading…
Cancel
Save