Skip to content

Commit

Permalink
Merge pull request #475 from winterheart/cfile-std-filesystem
Browse files Browse the repository at this point in the history
cfile / ddio: use most of functions to std::filesystem::path
  • Loading branch information
Lgt2x authored Jul 6, 2024
2 parents 9263055 + 2fce643 commit de75a80
Show file tree
Hide file tree
Showing 47 changed files with 922 additions and 1,130 deletions.
15 changes: 9 additions & 6 deletions Descent3/Mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,11 @@
*
* $NoKeywords: $
*/

#include <cstring>
#include <cstdlib>
#include <filesystem>

#include "Mission.h"
#include "3d.h"
#include "LoadLevel.h"
Expand Down Expand Up @@ -665,8 +670,6 @@
#include "osiris_dll.h"
#include "mission_download.h"
#include "manage.h"
#include <string.h>
#include <stdlib.h>
#include "ship.h"
#include "BOA.h"
#include "terrain.h"
Expand Down Expand Up @@ -742,10 +745,10 @@ void InitMission() {
Current_mission.mn3_handle = 0;
// create add on mission directory
ddio_MakePath(D3MissionsDir, LocalD3Dir, "missions", NULL);
if (!ddio_DirExists(D3MissionsDir)) {
if (!ddio_CreateDir(D3MissionsDir)) {
Error(TXT_MSN_FAILEDCREATEDIR);
}
std::error_code ec;
std::filesystem::create_directories(D3MissionsDir, ec);
if (ec) {
Error(TXT_MSN_FAILEDCREATEDIR);
}
atexit(ResetMission);
}
Expand Down
16 changes: 6 additions & 10 deletions Descent3/cinematics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,17 @@ void SetMovieProperties(int x, int y, int w, int h, renderer_type type) {
mve_SetRenderProperties(x, y, w, h, type, kHiColorEnabled);
}

bool PlayMovie(const char *moviename) {
bool PlayMovie(const std::filesystem::path &moviename) {
if (!Cinematic_lib_init)
return false;

// get in the right directory
char filename[_MAX_PATH];
strncpy(filename, moviename, sizeof(filename) - 1);
filename[sizeof(filename) - 1] = 0;

std::filesystem::path filename = moviename;
// check extension
const char *extension = strrchr(filename, '.');
if (extension == NULL || (stricmp(extension, ".mve") != 0 && stricmp(extension, ".mv8") != 0)) {
std::filesystem::path extension = moviename.extension();
if (stricmp(extension.u8string().c_str(), ".mve") != 0 && stricmp(extension.u8string().c_str(), ".mv8") != 0) {
// we need an extension
strncat(filename, ".mve", sizeof(filename) - strlen(filename) - 1);
filename[sizeof(filename) - 1] = 0;
filename.replace_extension(extension.u8string() + ".mve");
}

// shutdown sound.
Expand All @@ -110,7 +106,7 @@ bool PlayMovie(const char *moviename) {

SetMovieProperties(0, 0, Max_window_w, Max_window_h, Renderer_type);

int mveerr = mve_PlayMovie(filename, Descent);
int mveerr = mve_PlayMovie(filename.u8string().c_str(), Descent);

// Shutdown the subtitle system
SubtCloseSubtitles();
Expand Down
5 changes: 3 additions & 2 deletions Descent3/cinematics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Descent 3
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -55,6 +55,7 @@
#include "bitmap.h"

#include <cstdint>
#include <filesystem>

// Movie Cinematic
struct tCinematic {
Expand All @@ -65,7 +66,7 @@ struct tCinematic {

bool InitCinematics();
void SetMovieProperties(int x, int y, int w, int h, renderer_type type);
bool PlayMovie(const char *moviename);
bool PlayMovie(const std::filesystem::path &moviename);
tCinematic *StartMovie(const char *moviename, bool looping = false);
bool FrameMovie(tCinematic *mve, int x, int y, bool sequence = true);
void EndMovie(tCinematic *mve);
Expand Down
6 changes: 3 additions & 3 deletions Descent3/descent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@

#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <vector>

#include "pserror.h"
Expand Down Expand Up @@ -483,15 +484,14 @@ void Descent3() {

// Show the intro movie
if (!FindArg("-nointro")) {
char intropath[_MAX_PATH * 2];
// Intros to be played
const std::vector<const char *> intros = {
const std::vector<std::filesystem::path> intros = {
"dolby1.mv8",
"intro.mve",
};

for (auto const &intro : intros) {
ddio_MakePath(intropath, Base_directory, "movies", intro, nullptr);
std::filesystem::path intropath = std::filesystem::path(Base_directory) / "movies" / intro;
if (cfexist(intropath)) {
PlayMovie(intropath);
}
Expand Down
15 changes: 8 additions & 7 deletions Descent3/gamesave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@
* $NoKeywords: $
*/

#include <cstring>
#include <filesystem>

#include "gamesave.h"
#include "descent.h"
#include "newui.h"
Expand Down Expand Up @@ -291,7 +294,6 @@
#include "osiris_dll.h"
#include "levelgoal.h"
#include "aistruct.h"
#include <string.h>
#include "matcen.h"
#include "hud.h"
#include "marker.h"
Expand Down Expand Up @@ -395,11 +397,10 @@ void SaveGameDialog() {
// ddio_MakePath(pathname, savegame_dir, "*.sav", NULL); -unused

// create savegame directory if it didn't exist before.
if (!ddio_DirExists(savegame_dir)) {
if (!ddio_CreateDir(savegame_dir)) {
DoMessageBox(TXT_ERROR, TXT_ERRCREATEDIR, MSGBOX_OK);
return;
}
std::error_code ec;
if (!std::filesystem::create_directories(savegame_dir, ec)) {
DoMessageBox(TXT_ERROR, TXT_ERRCREATEDIR, MSGBOX_OK);
return;
}

// open window
Expand Down Expand Up @@ -589,7 +590,7 @@ bool LoadGameDialog() {
ddio_MakePath(pathname, savegame_dir, "*.sav", NULL);

// create savegame directory if it didn't exist before.
if (!ddio_DirExists(savegame_dir)) {
if (!std::filesystem::is_directory(savegame_dir)) {
DoMessageBox(TXT_ERROR, TXT_ERRNOSAVEGAMES, MSGBOX_OK);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions Descent3/hotspotmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,9 @@ int menutga_alloc_file(const char *name, char *hsmap[1], int *w, int *h) {

// TODO: MTS: only used in this file
// Given a filename and a hotspotmap structure, it saves it to disk (.HSM)
void menutga_SaveHotSpotMap(const char *filename, hotspotmap_t *hsmap, windowmap_t *wndmap) {
void menutga_SaveHotSpotMap(const std::filesystem::path &filename, hotspotmap_t *hsmap, windowmap_t *wndmap) {
CFILE *file;
mprintf(0, "Saving HotSpotMap %s ", filename);
mprintf(0, "Saving HotSpotMap %s ", filename.u8string().c_str());
file = (CFILE *)cfopen(filename, "wb");
if (!file) {
Int3(); // get jeff!
Expand Down Expand Up @@ -883,7 +883,7 @@ bool menutga_ConvertTGAtoHSM(const char *fpath) {
strcat(menu_filename, ".HSM"); // Hot Spot Map
mprintf(0, "HSM=%s\n", menu_filename);

ddio_MakePath(path, LocalManageGraphicsDir, menu_filename, NULL);
std::filesystem::path save_path = LocalManageGraphicsDir / menu_filename;

// now load up the tga and alloc it, then save it with the alpha hotspots
int bm_handle;
Expand All @@ -895,7 +895,7 @@ bool menutga_ConvertTGAtoHSM(const char *fpath) {
if (wndmap.num_of_windows == -1)
return false; // DAJ -1FIX
CreateWindowMap(map[0], width, height, &wndmap);
menutga_SaveHotSpotMap(path, &hsmap, &wndmap);
menutga_SaveHotSpotMap(save_path, &hsmap, &wndmap);
ExportHotSpot("C:\\hotspot.txt", &hsmap);
mem_free(menu_filename);
mem_free(map[0]);
Expand Down
3 changes: 2 additions & 1 deletion Descent3/hotspotmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#ifndef __HOTSPOTMAP_H_
#define __HOTSPOTMAP_H_

#include <filesystem>
#include "grdefs.h"

// for the next two defines this is how they work when converting the alpha values into hotspots/windows
Expand Down Expand Up @@ -99,7 +100,7 @@ struct windowmap_t {
// Loads a tga or ogf file into a bitmap...returns handle to bm or -1 on error, and fills in the alphamap
int menutga_alloc_file(const char *name, char *hsmap[], int *w, int *h);
// Given a filename and a HotSpotMap structure, it saves it to disk (.HSM)
void menutga_SaveHotSpotMap(const char *filename, hotspotmap_t *hsmap, windowmap_t *wndmap);
void menutga_SaveHotSpotMap(const std::filesystem::path &filename, hotspotmap_t *hsmap, windowmap_t *wndmap);
// Given a filename and a HotSpotMap structure, it loads the hotspot map (.HSM)
void menutga_LoadHotSpotMap(int back_bmp, const char *filename, hotspotmap_t *hsmap, windowmap_t *wndmap);
// This function (given a filename) loads a TGA file, extracts a hotspot map, and saves the hotspot map
Expand Down
19 changes: 11 additions & 8 deletions Descent3/newui_filedlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
*
* $NoKeywords: $
*/

#include <cstdlib>
#include <cstring>
#include <filesystem>

#include "mono.h"
#include "renderer.h"
#include "render.h"
Expand All @@ -108,8 +113,6 @@
#include "game.h"
#include "cfile.h"
#include "application.h"
#include <stdlib.h>
#include <string.h>
#include "newui.h"
#include "grtext.h"
#include "gamefont.h"
Expand Down Expand Up @@ -311,7 +314,7 @@ bool DoPathFileDialog(bool save_dialog, char *path, const char *title, const cha
if (path[0] == '\0') {
DoMessageBox(TXT_ERROR, TXT_ERRCHOOSEFILE, MSGBOX_OK);
} else {
if (ddio_DirExists(path)) {
if (std::filesystem::is_directory(path)) {
char newpath[_MAX_PATH];
char file[_MAX_PATH];
int selection = listbox->GetCurrentIndex();
Expand Down Expand Up @@ -424,7 +427,7 @@ bool DoPathFileDialog(bool save_dialog, char *path, const char *title, const cha
strcpy(c, edits[ED_FILENAME]);

if (strlen(c) > 0) {
if (!ddio_DirExists(c)) {
if (!std::filesystem::is_directory(c)) {
// the user wants a file
if (flags & PFDF_FILEMUSTEXIST) {
if (cfexist(c)) {
Expand Down Expand Up @@ -500,7 +503,7 @@ int GetFilesInPath(char **buffer, int maxcount, const char *p, const char *wildc
char tempbuffer[_MAX_PATH];

if (ddio_FindFileStart(wildcard, tempbuffer)) {
if (!ddio_DirExists(tempbuffer)) {
if (!std::filesystem::is_directory(tempbuffer)) {
buffer[count] = mem_strdup(tempbuffer);
count++;
}
Expand All @@ -509,7 +512,7 @@ int GetFilesInPath(char **buffer, int maxcount, const char *p, const char *wildc

while (!done) {
if (ddio_FindNextFile(tempbuffer)) {
if (!ddio_DirExists(tempbuffer)) {
if (!std::filesystem::is_directory(tempbuffer)) {
if (count < maxcount)
buffer[count] = mem_strdup(tempbuffer);
count++;
Expand Down Expand Up @@ -559,7 +562,7 @@ int GetDirectoriesInPath(char **buffer, int maxcount, const char *p) {
char tempbuffer[_MAX_PATH];

if (ddio_FindFileStart("*", tempbuffer)) {
if (ddio_DirExists(tempbuffer)) {
if (std::filesystem::is_directory(tempbuffer)) {
buffer[count] = mem_strdup(tempbuffer);
count++;
}
Expand All @@ -568,7 +571,7 @@ int GetDirectoriesInPath(char **buffer, int maxcount, const char *p) {

while (!done) {
if (ddio_FindNextFile(tempbuffer)) {
if (ddio_DirExists(tempbuffer)) {
if (std::filesystem::is_directory(tempbuffer)) {
if (count < maxcount)
buffer[count] = mem_strdup(tempbuffer);
count++;
Expand Down
33 changes: 6 additions & 27 deletions Descent3/subtitles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,44 +217,23 @@ void SubtResetSubTitles(void) {
}

// Initializes the subtitles for a given movie file
void SubtInitSubtitles(const char *filename) {
void SubtInitSubtitles(const std::filesystem::path &filename) {
// Load the subtitles
Num_subtitles = 0;
Movie_subtitle_init = 0;

CFILE *ifile;
char subtitle_file[_MAX_FNAME], subtitle_path[_MAX_PATH], *p;

// need a quick out here if no subtitles.
if (!FindArg("-subtitles"))
return;

// find the start of the filename
const char *pFilenameStart = strrchr(filename, '\\');
if (pFilenameStart != NULL) {
filename = pFilenameStart + 1;
}

pFilenameStart = strrchr(filename, '/');
if (pFilenameStart != NULL) {
filename = pFilenameStart + 1;
}

// strip off any extension for the movie and append the subtitle text
strcpy(subtitle_file, filename);
p = strchr(subtitle_file, '.');
if (p) {
*p = '\0';
}
strcat(subtitle_file, MOVIE_SUBTITLE_EXTENSION);

ddio_MakePath(subtitle_path, LocalD3Dir, "movies", subtitle_file, NULL);
std::filesystem::path subtitle_path = std::filesystem::path(LocalArtDir) / "movies" / filename;
subtitle_path.replace_extension(MOVIE_SUBTITLE_EXTENSION);

mprintf(0, "Looking for the subtitle file %s\n", subtitle_path);
mprintf(0, "Looking for the subtitle file %s\n", subtitle_path.u8string().c_str());

ifile = cfopen(subtitle_path, "rt");
CFILE *ifile = cfopen(subtitle_path, "rt");
if (!ifile) {
mprintf(0, "Movie: Couldn't find subtitle file %s\n", subtitle_path);
mprintf(0, "Movie: Couldn't find subtitle file %s\n", subtitle_path.u8string().c_str());
return;
}

Expand Down
4 changes: 3 additions & 1 deletion Descent3/subtitles.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
#ifndef __SUBTITLES_H_
#define __SUBTITLES_H_

#include <filesystem>

// draw the subtitles for this frame
void SubtDrawSubtitles(int frame_num);
// Shutsdown the subtitle system
void SubtCloseSubtitles();
// Initializes the subtitles for a given movie file
void SubtInitSubtitles(const char *filename);
void SubtInitSubtitles(const std::filesystem::path &filename);

#endif
Loading

0 comments on commit de75a80

Please sign in to comment.