Skip to content

Commit

Permalink
Add option 'Save camera position'
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Jan 31, 2024
1 parent c788062 commit da60860
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 19 deletions.
3 changes: 2 additions & 1 deletion resources/bspguy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ MAX_MAP_LIGHTDATA=64
MAX_TEXTURE_DIMENSION=1024
TEXTURE_STEP=16
language=EN
palette=quake_1
palette=quake_1
save_cam=0
39 changes: 37 additions & 2 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void Bsp::init_empty_bsp()
bsp_header.lump[i].nLength = 4;
}


save_cam_pos = save_cam_angles = {};
bsp_name = "empty";
bsp_path = "empty.bsp";
bsp_valid = true;
Expand Down Expand Up @@ -181,6 +183,8 @@ Bsp::Bsp(std::string fpath)
bsp_header_ex = BSPHEADER_EX();
parentMap = NULL;

save_cam_pos = save_cam_angles = {};

if (fpath.empty())
{
fpath = "newmap.bsp";
Expand Down Expand Up @@ -309,6 +313,22 @@ Bsp::Bsp(std::string fpath)
replacedLump[i] = false;
}

if (g_settings.save_cam)
{
if (ents.size())
{
if (ents[0]->hasKey("camera_pos"))
{
save_cam_pos = parseVector(ents[0]->keyvalues["camera_pos"]);
}

if (ents[0]->hasKey("camera_angles"))
{
save_cam_angles = parseVector(ents[0]->keyvalues["camera_angles"]);
}
}
}

save_undo_lightmaps();
}

Expand Down Expand Up @@ -2570,7 +2590,7 @@ bool Bsp::is_invisible_solid(Entity* ent)

void Bsp::update_ent_lump(bool stripNodes)
{
std::stringstream ent_data = std::stringstream();
std::stringstream ent_data{};

for (size_t i = 0; i < ents.size(); i++)
{
Expand Down Expand Up @@ -2677,6 +2697,21 @@ void Bsp::write(const std::string& path)
// bsp_header.nVersion = 30;
//}



if (g_settings.save_cam)
{
if (ents.size())
{
if (!save_cam_pos.IsZero())
ents[0]->setOrAddKeyvalue("camera_pos", save_cam_pos.toKeyvalueString());
if (!save_cam_angles.IsZero())
ents[0]->setOrAddKeyvalue("camera_angles", save_cam_angles.toKeyvalueString());

update_ent_lump();
}
}

update_lump_pointers();

unsigned char* nulls = new unsigned char[sizeof(BSPHEADER) + sizeof(BSPHEADER_EX)];
Expand Down Expand Up @@ -7073,7 +7108,7 @@ int Bsp::merge_two_models(int src_model, int dst_model)
vec3 bmin = models[src_model].nMins + models[src_model].vOrigin;
vec3 bmax = models[src_model].nMaxs + models[src_model].vOrigin;

BSPPLANE separate_plane = getSeparatePlane(bmin, bmax,amin, amax);
BSPPLANE separate_plane = getSeparatePlane(bmin, bmax, amin, amax);

std::vector<vec3> veclist;
veclist.push_back(amin);
Expand Down
2 changes: 2 additions & 0 deletions src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Bsp

bool replacedLump[32];

vec3 save_cam_pos, save_cam_angles;

bool bsp_valid;
bool is_bsp_model;
bool is_mdl_model;
Expand Down
14 changes: 7 additions & 7 deletions src/bsp/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Entity
renderamt = 0;
renderfx = kRenderFxNone;
rendercolor = vec3(1.0f, 1.0f, 1.0f);
origin = vec3(0.0f, 0.0f, 0.0f);
origin = vec3();
originInited = false;
targetsCached = false;
hide = false;
Expand Down Expand Up @@ -84,11 +84,11 @@ class Entity

size_t getMemoryUsage(); // aproximate

bool originInited = false;
vec3 origin = vec3(0.0f, 0.0f, 0.0f);
int rendermode = kRenderNormal;
int renderamt = 0;
int renderfx = kRenderFxNone;
vec3 rendercolor = vec3(1.0f, 1.0f, 1.0f);
bool originInited;
vec3 origin;
int rendermode;
int renderamt;
int renderfx;
vec3 rendercolor;
};

14 changes: 14 additions & 0 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ BspRenderer::BspRenderer(Bsp* _map)
}*/
}


if (g_settings.save_cam)
{
if (!map->save_cam_pos.IsZero())
{
renderCameraOrigin = map->save_cam_pos;
}

if (!map->save_cam_angles.IsZero())
{
renderCameraAngles = map->save_cam_angles;
}
}

if (g_app->getSelectedMap() == NULL || map == g_app->getSelectedMap())
{
cameraOrigin = renderCameraOrigin;
Expand Down
2 changes: 1 addition & 1 deletion src/editor/Fgd.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct FgdClass
mins = vec3(-8, -8, -8);
maxs = vec3(8, 8, 8);
color = { 220, 0, 220 };
offset = vec3(0, 0, 0);
offset = vec3();
modelSkin = modelBody = modelSequence = 0;
}

Expand Down
12 changes: 11 additions & 1 deletion src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6501,6 +6501,17 @@ void Gui::drawSettings()
ImGui::TextUnformatted(get_localized_string(LANG_0738).c_str());
ImGui::EndTooltip();
}


ImGui::Checkbox("Save map cam pos", &g_settings.save_cam);
if (ImGui::IsItemHovered() && g.HoveredIdTimer > g_tooltip_delay)
{
ImGui::BeginTooltip();
ImGui::TextUnformatted("Save camera position to map and load it at open.");
ImGui::EndTooltip();
}


ImGui::Separator();
ImGui::TextUnformatted("Language:");
ImGui::SameLine();
Expand Down Expand Up @@ -7524,7 +7535,6 @@ void Gui::drawImportMapWidget()
tmpEnt->setOrAddKeyvalue("gibmodel", std::string("models/") + basename(mapPath));
tmpEnt->setOrAddKeyvalue("model", std::string("models/") + basename(mapPath));
tmpEnt->setOrAddKeyvalue("spawnflags", "1");
tmpEnt->setOrAddKeyvalue("origin", cameraOrigin.toKeyvalueString());
map->ents.push_back(tmpEnt);
map->update_ent_lump();
print_log(get_localized_string(LANG_0402), std::string("models/") + basename(mapPath));
Expand Down
15 changes: 12 additions & 3 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ Renderer::~Renderer()
}

void Renderer::updateWindowTitle(double _curTime)
{
{
static double lastTitleTime = 0.0f;
if (_curTime - lastTitleTime > 0.25)
{
Expand Down Expand Up @@ -513,7 +513,7 @@ void Renderer::renderLoop()
oldControl = canControl;

canControl = /*!gui->imgui_io->WantCaptureKeyboard && */ !gui->imgui_io->WantTextInput && !gui->imgui_io->WantCaptureMouseUnlessPopupClose;

updateWindowTitle(curTime);

int modelIdx = -1;
Expand Down Expand Up @@ -2067,7 +2067,7 @@ vec3 Renderer::getMoveDir()
makeVectors(cameraAngles, forward, right, up);


vec3 wishdir(0, 0, 0);
vec3 wishdir{};
if (pressed[GLFW_KEY_A])
{
wishdir -= right;
Expand Down Expand Up @@ -2225,6 +2225,15 @@ void Renderer::setupView()
matview.rotateX(cameraAngles.x * PI / 180.0f);
matview.rotateY(cameraAngles.z * PI / 180.0f);
matview.translate(-cameraOrigin.x, -cameraOrigin.z, cameraOrigin.y);

if (g_settings.save_cam)
{
if (g_app->SelectedMap)
{
g_app->SelectedMap->save_cam_pos = cameraOrigin;
g_app->SelectedMap->save_cam_angles = cameraAngles;
}
}
}

void Renderer::reloadBspModels()
Expand Down
6 changes: 6 additions & 0 deletions src/editor/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void AppSettings::loadDefault()
start_at_entity = false;
backUpMap = true;
preserveCrc32 = false;
save_cam = false;
autoImportEnt = false;
sameDirForEnt = false;

Expand Down Expand Up @@ -472,6 +473,10 @@ void AppSettings::load()
{
g_settings.preserveCrc32 = atoi(val.c_str()) != 0;
}
else if (key == "save_cam")
{
g_settings.save_cam = atoi(val.c_str()) != 0;
}
else if (key == "auto_import_ent")
{
g_settings.autoImportEnt = atoi(val.c_str()) != 0;
Expand Down Expand Up @@ -803,6 +808,7 @@ void AppSettings::save(std::string path)
file << "undo_levels=" << g_settings.undoLevels << std::endl;
file << "savebackup=" << g_settings.backUpMap << std::endl;
file << "save_crc=" << g_settings.preserveCrc32 << std::endl;
file << "save_cam=" << g_settings.save_cam << std::endl;
file << "auto_import_ent=" << g_settings.autoImportEnt << std::endl;
file << "same_dir_for_ent=" << g_settings.sameDirForEnt << std::endl;
file << "reload_ents_list=" << g_settings.entListReload << std::endl;
Expand Down
1 change: 1 addition & 0 deletions src/editor/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct AppSettings
bool start_at_entity;
bool backUpMap;
bool preserveCrc32;
bool save_cam;
bool autoImportEnt;
bool sameDirForEnt;
bool entListReload;
Expand Down
8 changes: 4 additions & 4 deletions src/util/vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct vec3
Copy(other);
}

vec3() : x(+0.0f), y(+0.0f), z(+0.0f)
vec3() : x(+0.00f), y(+0.00f), z(+0.00f)
{

}
Expand All @@ -78,15 +78,15 @@ struct vec3
{
if (abs(x) < EPSILON2)
{
x = +0.0f;
x = +0.00f;
}
if (abs(y) < EPSILON2)
{
y = +0.0f;
y = +0.00f;
}
if (abs(z) < EPSILON2)
{
z = +0.0f;
z = +0.00f;
}
}
vec3 normalize(float length = 1.0f);
Expand Down

0 comments on commit da60860

Please sign in to comment.