Skip to content

Commit

Permalink
preventing undersired resources unload
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpages2020 committed Jan 10, 2021
1 parent 26d6004 commit 24c0ce5
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 48 deletions.
38 changes: 19 additions & 19 deletions GenesisEngine/Engine/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Collapsed=0
DockId=0x00000007,0

[Window][Inspector]
Pos=1509,19
Size=411,741
Pos=805,19
Size=219,556
Collapsed=0
DockId=0x0000000D,0

[Window][Console]
Pos=0,762
Size=1920,255
Pos=0,577
Size=1024,191
Collapsed=0
DockId=0x00000007,1

Expand All @@ -44,14 +44,14 @@ Size=479,450
Collapsed=0

[Window][Scene]
Pos=196,19
Size=1311,741
Pos=123,19
Size=680,556
Collapsed=0
DockId=0x0000000E,0

[Window][Configuration]
Pos=1509,19
Size=411,741
Pos=805,19
Size=219,556
Collapsed=0
DockId=0x0000000D,1

Expand All @@ -69,28 +69,28 @@ DockId=0x00000005,0

[Window][DockSpace]
Pos=0,19
Size=1920,998
Size=1024,749
Collapsed=0

[Window][Hierarchy]
Pos=0,19
Size=194,741
Size=121,556
Collapsed=0
DockId=0x00000008,0

[Window][Load File]
Pos=511,135
Pos=120,136
Size=901,375
Collapsed=0

[Window][Game Buttons]
Pos=555,8
Pos=582,11
Size=130,40
Collapsed=0

[Window][Assets]
Pos=0,762
Size=1920,255
Pos=0,577
Size=1024,191
Collapsed=0
DockId=0x00000007,0

Expand All @@ -100,7 +100,7 @@ Size=524,483
Collapsed=0

[Window][Save File]
Pos=559,271
Pos=565,271
Size=382,359
Collapsed=0

Expand All @@ -111,17 +111,17 @@ Collapsed=0
DockId=0x0000000B,0

[Docking][Data]
DockSpace ID=0x09EF459F Window=0x9A404470 Pos=0,19 Size=1920,998 Split=Y Selected=0x18B8C0DE
DockSpace ID=0x09EF459F Window=0x9A404470 Pos=0,19 Size=1024,749 Split=Y Selected=0x18B8C0DE
DockNode ID=0x0000000C Parent=0x09EF459F SizeRef=1600,654 Split=X
DockNode ID=0x0000000A Parent=0x0000000C SizeRef=1507,881 Split=X
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=194,862 Selected=0x788BAA0D
DockNode ID=0x00000009 Parent=0x0000000A SizeRef=1311,862 Split=X
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=227,862 Selected=0x788BAA0D
DockNode ID=0x00000009 Parent=0x0000000A SizeRef=1278,862 Split=X
DockNode ID=0x00000005 Parent=0x00000009 SizeRef=257,968 Selected=0x17A9EF69
DockNode ID=0x00000006 Parent=0x00000009 SizeRef=1039,968 Split=X Selected=0x18B8C0DE
DockNode ID=0x0000000B Parent=0x00000006 SizeRef=594,638 Selected=0x919FD48C
DockNode ID=0x0000000E Parent=0x00000006 SizeRef=681,638 CentralNode=1 Selected=0x18B8C0DE
DockNode ID=0x0000000D Parent=0x0000000C SizeRef=411,881 Selected=0x169CEE22
DockNode ID=0x00000007 Parent=0x09EF459F SizeRef=1600,225 Selected=0x7E7D78B8
DockNode ID=0x00000007 Parent=0x09EF459F SizeRef=1600,225 Selected=0xF9BEF62A
DockSpace ID=0x3BC79352 Pos=0,19 Size=1600,881 Split=Y
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=1280,729 Split=X
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1032,749 CentralNode=1 Selected=0x18B8C0DE
Expand Down
70 changes: 47 additions & 23 deletions GenesisEngine/Source/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@ Material::~Material()
{
if (_resource != nullptr)
{
if (shader != nullptr)
{
App->resources->ReleaseResource(shader->GetUID());
shader = nullptr;
}

if (_resource->diffuseMap != nullptr)
{
App->resources->ReleaseResource(_resource->diffuseMap->GetUID());
_resource->diffuseMap = nullptr;
//_resource->diffuseMap = nullptr;
}

if (_resource->normalMap != nullptr)
{
App->resources->ReleaseResource(_resource->normalMap->GetUID());
_resource->normalMap = nullptr;
}

if (shader != nullptr)
{
App->resources->ReleaseResource(shader->GetUID());
shader = nullptr;
//_resource->normalMap = nullptr;
}

App->resources->ReleaseResource(_resourceUID);
App->resources->ReleaseResource(_resource->GetUID());
_resource = nullptr;
}

Expand All @@ -93,6 +93,12 @@ void Material::SetResourceUID(uint UID)
//Assign new material
_resourceUID = UID;
_resource = dynamic_cast<ResourceMaterial*>(App->resources->RequestResource(UID));

if (_resource != nullptr)
{
SetTexture(dynamic_cast<ResourceTexture*>(App->resources->RequestResource(_resource->diffuseMapID)),DIFFUSE_MAP);
SetTexture(dynamic_cast<ResourceTexture*>(App->resources->RequestResource(_resource->normalMapID)), NORMAL_MAP);
}
}

void Material::SetShader(ResourceShader* new_shader)
Expand Down Expand Up @@ -142,11 +148,19 @@ void Material::UseShader()
{
if (_resource->diffuseMap != nullptr)
{
shader->SetInt("diffuseMap", 0);
glActiveTexture(GL_TEXTURE0);
BindTexture(_resource->diffuseMap);
shader->SetVec2("diffuseMapTiling", _resource->tiling[DIFFUSE_MAP][0], _resource->tiling[DIFFUSE_MAP][1]);
shader->SetBool("hasDiffuseMap", true);
if (!App->resources->Exists(_resource->diffuseMapID))
{
_resource->diffuseMap = nullptr;
_resource->diffuseMapID = 0;
}
else
{
shader->SetInt("diffuseMap", 0);
glActiveTexture(GL_TEXTURE0);
BindTexture(_resource->diffuseMap);
shader->SetVec2("diffuseMapTiling", _resource->tiling[DIFFUSE_MAP][0], _resource->tiling[DIFFUSE_MAP][1]);
shader->SetBool("hasDiffuseMap", true);
}
}
else {
shader->SetBool("hasDiffuseMap", false);}
Expand All @@ -159,11 +173,20 @@ void Material::UseShader()
//normal map
if (_resource->normalMap != nullptr)
{
shader->SetInt("normalMap", 1);
glActiveTexture(GL_TEXTURE1);
BindTexture(_resource->normalMap);
shader->SetVec2("normalMapTiling", _resource->tiling[NORMAL_MAP][0], _resource->tiling[NORMAL_MAP][1]);
shader->SetBool("hasNormalMap", true);
if (!App->resources->Exists(_resource->normalMapID))
{
_resource->normalMap = nullptr;
_resource->normalMapID = 0;
}
else
{
shader->SetInt("normalMap", 1);
glActiveTexture(GL_TEXTURE1);
BindTexture(_resource->normalMap);
shader->SetVec2("normalMapTiling", _resource->tiling[NORMAL_MAP][0], _resource->tiling[NORMAL_MAP][1]);
shader->SetBool("hasNormalMap", true);
}

}
else {
shader->SetBool("hasNormalMap", false);}
Expand All @@ -175,17 +198,15 @@ void Material::Save(GnJSONArray& save_array)
GnJSONObj save_object;

save_object.AddInt("Type", type);
save_object.AddInt("MaterialID", _resource->GetUID());

if (_resource != nullptr)
{
App->resources->SaveResource(_resource);
save_object.AddInt("MaterialID", _resource->GetUID());
}

if (shader != nullptr)
{
save_object.AddInt("ShaderID", shader->GetUID());
App->resources->SaveResource(shader);
}

save_array.AddObject(save_object);
Expand All @@ -194,7 +215,7 @@ void Material::Save(GnJSONArray& save_array)
void Material::Load(GnJSONObj& load_object)
{
_resourceUID = load_object.GetInt("MaterialID", 0);
_resource = (ResourceMaterial*)App->resources->RequestResource(_resourceUID);
SetResourceUID(_resourceUID);
uint shaderUID = load_object.GetInt("ShaderID", 0);
SetShader((ResourceShader*)App->resources->RequestResource(shaderUID));
}
Expand Down Expand Up @@ -359,8 +380,11 @@ ResourceTexture* Material::DrawTextureInformation(ResourceTexture* texture, uint

void Material::SetTexture(ResourceTexture* texture, TextureType type)
{

if (texture != nullptr)
{
texture->type = type;

switch (type)
{
case TextureType::DIFFUSE_MAP:
Expand Down
4 changes: 2 additions & 2 deletions GenesisEngine/Source/MaterialImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ bool MaterialImporter::Load(const char* fileBuffer, ResourceMaterial* material,

if (diffuseMapID != 0)
{
material->diffuseMap = dynamic_cast<ResourceTexture*>(App->resources->RequestResource(diffuseMapID));
material->diffuseMapID = diffuseMapID;
material->diffuseMap = dynamic_cast<ResourceTexture*>(App->resources->RequestResource(diffuseMapID));
float2 diffuseMapTiling = material_data.GetFloat2("diffuseMapTiling");
material->tiling[DIFFUSE_MAP][0] = diffuseMapTiling.x;
material->tiling[DIFFUSE_MAP][1] = diffuseMapTiling.y;
Expand All @@ -92,8 +92,8 @@ bool MaterialImporter::Load(const char* fileBuffer, ResourceMaterial* material,

if (normalMapID != 0)
{
material->normalMap = dynamic_cast<ResourceTexture*>(App->resources->RequestResource(normalMapID));
material->normalMapID = normalMapID;
material->normalMap = dynamic_cast<ResourceTexture*>(App->resources->RequestResource(normalMapID));
float2 normalMapTiling = material_data.GetFloat2("normalMapTiling");
material->tiling[NORMAL_MAP][0] = normalMapTiling.x;
material->tiling[NORMAL_MAP][1] = normalMapTiling.y;
Expand Down
6 changes: 3 additions & 3 deletions GenesisEngine/Source/ModuleCamera3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ void ModuleCamera3D::OnResize(int width, int height)
// -----------------------------------------------------------------
update_status ModuleCamera3D::Update(float dt)
{
if (!App->editor->IsWindowFocused(WindowType::WINDOW_SCENE))
return UPDATE_CONTINUE;
//if (!App->editor->IsWindowFocused(WindowType::WINDOW_SCENE))
//return UPDATE_CONTINUE;

float3 newPos = float3::zero;
int speed_multiplier = 1;
Expand Down Expand Up @@ -126,7 +126,7 @@ update_status ModuleCamera3D::Update(float dt)
((App->input->GetKey(SDL_SCANCODE_LALT) == KEY_REPEAT) && (App->input->GetMouseButton(SDL_BUTTON_LEFT) == KEY_REPEAT)))
Orbit(dt);

if (App->input->GetMouseButton(SDL_BUTTON_LEFT) == KEY_DOWN && !ImGuizmo::IsOver() && App->editor->MouseOnScene())
if (App->input->GetMouseButton(SDL_BUTTON_LEFT) == KEY_DOWN && !ImGuizmo::IsUsing() && App->editor->MouseOnScene())
{
GameObject* pickedGameObject = PickGameObject();
App->scene->selectedGameObject = pickedGameObject;
Expand Down
22 changes: 22 additions & 0 deletions GenesisEngine/Source/ModuleResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ void ModuleResources::OnEditor()
}
}

void ModuleResources::Save()
{
std::vector<Resource*> resources_to_save;

std::map<uint, Resource*>::iterator it = resources.begin();
for (it; it != resources.end(); it++)
{
ResourceType type = it->second->GetType();
if (type == ResourceType::RESOURCE_MATERIAL || type == ResourceType::RESOURCE_SHADER)
{
resources_to_save.push_back(it->second);
}
}

for (size_t i = 0; i < resources_to_save.size(); i++)
{
SaveResource(resources_to_save[i]);
}

}

void ModuleResources::LoadEngineAssets(AssetsIcons& icons)
{
icons.folder = dynamic_cast<ResourceTexture*>(RequestResource(Find("Assets/EngineAssets/folder.png")));
Expand Down Expand Up @@ -840,6 +861,7 @@ GameObject* ModuleResources::RequestGameObject(const char* assets_file)
void ModuleResources::ReleaseResource(uint UID)
{
std::map<uint, Resource*>::iterator it = resources.find(UID);

if (it != resources.end())
{
it->second->referenceCount--;
Expand Down
1 change: 1 addition & 0 deletions GenesisEngine/Source/ModuleResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ModuleResources : public Module
bool Start() override;
bool CleanUp() override;
void OnEditor();
void Save();
void LoadEngineAssets(AssetsIcons& icons);
void OnFrameEnd() override;

Expand Down
2 changes: 2 additions & 0 deletions GenesisEngine/Source/ModuleScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ bool ModuleScene::Save(const char* file_path)

FileSystem::DuplicateFile(file_path, assets_path.c_str());

App->resources->Save();

save_file.Release();
RELEASE_ARRAY(buffer);

Expand Down
4 changes: 3 additions & 1 deletion GenesisEngine/Source/ResourceMaterial.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ResourceMaterial.h"
#include "Application.h"

ResourceMaterial::ResourceMaterial(uint UID) : Resource(UID, ResourceType::RESOURCE_MATERIAL), diffuseMapID(0), normalMapID(0), diffuseMap(nullptr), normalMap(nullptr)
{
Expand All @@ -7,5 +8,6 @@ ResourceMaterial::ResourceMaterial(uint UID) : Resource(UID, ResourceType::RESOU
tiling[TextureType::NORMAL_MAP][0] = tiling[TextureType::NORMAL_MAP][1] = 1.0f;
}

ResourceMaterial::~ResourceMaterial() {}
ResourceMaterial::~ResourceMaterial()
{}

0 comments on commit 24c0ce5

Please sign in to comment.