2014년 6월 30일 월요일

[일기] SDL 초기 튜토리얼 연습 1

http://www.willusher.io/sdl2%20tutorials/2014/06/16/postscript-0-properly-finding-resource-paths/

아주 좋은 사이트가 있다.

경로를 다음과 같이 잡으라는데


Lessons/
    bin/
        executables in here
    res/
        Lesson1/
            Lesson 1's assets are stored here
    src/
        Lesson1/
            Lesson 1's source code
    include/
        Headers shared between lessons

무시하고 다음과 같이 진행했다.

 First/
        bin/
              executables
        res/
              Recored's assets
        src/
              source codes

 바로 유용한 함수 하나를 사이트에서 제공했다.

#ifndef RES_PATH_H
#define RES_PATH_H

#include <iostream>
#include <string>
#include <SDL.h>

/*
 * Get the resource path for resources located in res/subDir
 * It's assumed the project directory is structured like:
 * bin/
 *  the executable
 * res/
 *  Lesson1/
 *  Lesson2/
 *
 * Paths returned will be Lessons/res/subDir
 */
std::string getResourcePath(const std::string &subDir = ""){
//We need to choose the path separator properly based on which
//platform we're running on, since Windows uses a different
//separator than most systems
#ifdef _WIN32
const char PATH_SEP = '\\';
#else
const char PATH_SEP = '/';
#endif
//This will hold the base resource path: Lessons/res/
//We give it static lifetime so that we'll only need to call
//SDL_GetBasePath once to get the executable path
static std::string baseRes;
if (baseRes.empty()){
//SDL_GetBasePath will return NULL if something went wrong in retrieving the path
char *basePath = SDL_GetBasePath();
if (basePath){
baseRes = basePath;
SDL_free(basePath);
}
else {
std::cerr << "Error getting resource path: " << SDL_GetError() << std::endl;
return "";
}
//We replace the last bin/ with res/ to get the the resource path
size_t pos = baseRes.find_last_of("bin") - 2;
baseRes = baseRes.substr(0, pos) + "res" + PATH_SEP;
}
//If we want a specific subdirectory path in the resource directory
//append it to the base path. This would be something like Lessons/res/Lesson0
return subDir.empty() ? baseRes : baseRes + subDir + PATH_SEP;
}

#endif

- 출처 : http://www.willusher.io/sdl2%20tutorials/2014/06/16/postscript-0-properly-finding-resource-paths/



헤더 파일 생성 후 위 코드 복사 붙여넣기.


메인 함수에 코딩.


결과 확인.

결론.
  
  현재 작업경로를 얻어오는 방법을 알게 되었다...(?)

 보통은
#include <Windows.h>
 std::string MyTi_ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    std::string::size_type pos = std::string( buffer ).find_last_of( "\\/" );
    return std::string( buffer ).substr( 0, pos);
}

이렇게 얻는다.

댓글 없음:

댓글 쓰기