기존의 rand함수와 %연산으로 범쉬를 설정해 난수를 설정 했었으나, c++11에 관련 기능이 있으니 써보기로 했다. #include #include int Util::Random(int min, int includ_max) { random_device rn; mt19937_64 rnd( rn() ); uniform_int_distribution range(min, includ_max); return range( rnd ); } uniform_int_distribution를 통해 min,max 범위를 설정하고, 값을 리턴하면 끝이다. 간단하다. 여기서 유의해야 할 점은 min, max값이 제외가 아니라 포함이다. int r = Util::Random(-5,10) //r [-5 ~ 10] 사이의 정수 반환..
게임을 만들자/C++ 검색 결과
1. ImplementThis code use string class instead of strtok included in . void StringTokenize(const string& str, vector& tokens, const string& delimiters) { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != last..
Sometime we get error from cJSON. Window utf-8 txt file attach big-endian information [EF BB BF]. It cause error when we parse json or use setcookie in PHP. I removed this information using Notepad++.Download Link : http://notepad-plus-plus.org/ 1. Save txt file to UTF-8 File->Save to other -> Set encoding to UTF-8 2. Using Notepad ++ Open txt file in Notepad++. Select UTF-8 without BOM
1. Easing Code Many games and App use Easing Algorithm like scrolling, character moving, etc. Blow show c++ code of Easing Algorithm. start_value : 0 frame's value.change_value : End frame's value - start_value inline float easeLinear(float current_frame, float start_value, float end_frame, float change_value) { return change_value*current_frame/end_frame + start_value; } inline float easeQuadra..
1. 다수의 멤버변수 보통 UI클래스나, 많은 정보를 가진 데이터 클래스(java의 bean객체 같은)의 경우, 많은 멤버 변수를 가진다. 이런 멤버 변수들을 생성자 파라미터로 초기화하면, 코드가 많이 복잡해 보일 수 있다. 예를 들어, 아래 클래스 처럼 new를 통해 파라미터로 값을 초기화하는 경우, 가독성이 많이 떨어진다(각각의 파라미터가 어떤 의미인지 알기 어렵다). 이런 문제를 해결하기 위해 get, set함수를 만들고 사용하기도 하는데 별로 만족 스럽지 못하다. class Node { protected: vec2m_position; vec2m_draw_position; floatm_width; floatm_height; AlignTypem_child_align_type; Marginm_margi..
1. 변수 const : 변수 값을 변경하지 못하게 한다.const int n = 5;n = 10 // 에러 1-2 const int& n2 = n; : n2가 참조하는 곳의 값을 바꿀 수 없다.int n = 5;const int& n2 = n;n2 = 10; //에러n = 10 ; //가능. 2. 포인터 변수 const : 포인터가 가르키는 곳의 값을 변경하지 못하게 한다.int n = 5; const int* ap = &n; (int const* ap도 똑같다.)*ap = 10; //에러 int n2 = 10;ap = &n2; //가능함. 포인터 변경은 가능 int * ap2 = ap; //에러. 포인터가 가르키는 곳을 다른 포인터가 값을 변경할 수도 있기 때문에, 허용하지 않는다.const int ..
최근댓글