본문 바로가기

Win32 api

Win32 api 강의 55 - 56화.

- 파일입출력 보완.

// CScene_Tool.h
...    
    void SaveTileData();
    void LoadTileData();
...

 

툴씬에서 함수 2개를 추가해주고...

이전에 구현한 저장, 로드 함수를 이 함수에서 대신 호출해준다.

윈도우 운영체제에서 파일 저장 / 열기 할 때 뜨는 창을 사용해줄 거임.

 

// CScene_Tool.cpp
...
void CScene_Tool::SaveTileData()
{
    wchar_t szFileName[MAX_PATH] = L"";
    OPENFILENAME ofn = {};
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = CCore::GetInstance()->GetMainHwnd();
    ofn.lpstrFilter = L"Tile Map Files (*.tilemap)\0*.tilemap\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrTitle = L"Save Tile Map";
    ofn.lpstrInitialDir = CPathMgr::GetInstance()->GetContentPath();
    ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR 
        | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
    // 기본 확장자 설정.
    ofn.lpstrDefExt = L"tilemap";

    // Modal 방식으로 파일 저장 대화상자 표시.
    if (GetSaveFileName(&ofn) == FALSE)
        return; // 사용자가 저장 대화상자를 취소한 경우.

    // 전체 경로에서 콘텐츠 폴더 이후의 상대 경로 추출.
    const wstring relativePath = CPathMgr::GetInstance()->GetRelativePathFromFullPath(szFileName);
    if (!relativePath.empty())
        SaveTileMap(relativePath);
}

 

OPENFILENAME 이라는 구조체를 설정해줘야 함. 

여기에 설정된 값을 기준으로 윈도우에서 창을 띄워주는 듯.

 

GetSaveFileName() 함수가 실행되면 윈도우 창이 뜨고, 취소 버튼을 누르면 FALSE 가 반환됨. 저장 버튼을 누르면 

미리 만들어둔 버퍼인 szFileName 에 사용자가 입력한 절대경로 값이 들어온다.

 

여기서 우리 프로그램의 콘텐츠 폴더 상대 경로를 필터링해서 이전에 구현했던 SaveTileMap 함수에 넘겨줌.

// CPathMgr.cpp
...
const wstring CPathMgr::GetRelativePathFromFullPath(const wchar_t* fullPath) const
{
    if(fullPath == nullptr)
        return wstring(); // 빈 wstring 반환

    // 전체 경로에서 콘텐츠 폴더 이후의 상대 경로 추출.
    wstring fullPathStr = fullPath;
    wstring contentPath = m_szContentPath;
    size_t pos = fullPathStr.find(contentPath);
    // contentPath가 fullPath에 포함되어 있지 않은 경우 오류 처리.
    if (pos == wstring::npos)
    {
        MessageBox(CCore::GetInstance()->GetMainHwnd(), L"Please select a path within the content folder.", L"Error", MB_OK);
        return wstring(); // 빈 wstring 반환
    }
    const wstring relativePath = fullPathStr.substr(pos + contentPath.length());
    return relativePath;
}

 

필터링 함수는 find, substr 을 사용해서 상대 경로를 확인함. 없으면 에러 메시지를 뽑도록 했다.

 

// CScene_Tool.cpp
...
void CScene_Tool::LoadTileData()
{
    wchar_t szFileName[MAX_PATH] = L"";
    OPENFILENAME ofn = {};
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = CCore::GetInstance()->GetMainHwnd();
    ofn.lpstrFilter = L"Tile Map Files (*.tilemap)\0*.tilemap\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrTitle = L"Load Tile Map";
    ofn.lpstrInitialDir = CPathMgr::GetInstance()->GetContentPath();
    // OFN_NOCHANGEDIR: 대화상자를 닫은 후 현재 디렉터리를 변경하지 않음.
    // OFN_FILEMUSTEXIST: 사용자가 지정한 파일이 반드시 존재해야 함.
    // OFN_PATHMUSTEXIST: 사용자가 지정한 경로가 반드시 존재해야 함.
    ofn.Flags = OFN_NOCHANGEDIR 
        | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
    // 기본 확장자 설정.
    ofn.lpstrDefExt = L"tilemap";
    // Modal 방식으로 파일 열기 대화상자 표시.
    if (GetOpenFileName(&ofn) == FALSE)
        return; // 사용자가 열기 대화상자를 취소한 경우.
    // 전체 경로에서 콘텐츠 폴더 이후의 상대 경로 추출.
    const wstring relativePath = CPathMgr::GetInstance()->GetRelativePathFromFullPath(szFileName);
    if (!relativePath.empty())
        LoadTileMap(relativePath);
}

 

로드 부분도 크게 다르지 않다. 

'Win32 api' 카테고리의 다른 글

Win32 api 강의 59 - 61화.  (0) 2025.09.30
Win32 api 강의 57 - 58화.  (0) 2025.09.29
Win32 api 강의 54화.  (0) 2025.09.27
Win32 api 강의 53화.  (0) 2025.09.26
Win32 api 강의 52화.  (0) 2025.09.26