fork(21) download
  1. #include <iostream>
  2. using namespace std;
  3. // Một số hàm, thủ tục xây dựng sẵn để dùng cho tiện
  4. // Đi sưu tầm là chính
  5.  
  6. // Define thêm một vài phím kí tự
  7. #define KEY_A 0x41
  8. #define KEY_B 0x42
  9. #define KEY_C 0x43
  10. #define KEY_D 0x44
  11. #define KEY_E 0x45
  12. #define KEY_F 0x46
  13. #define KEY_G 0x47
  14. #define KEY_H 0x48
  15. #define KEY_I 0x49
  16. #define KEY_J 0x4A
  17. #define KEY_K 0x4B
  18. #define KEY_L 0x4C
  19. #define KEY_M 0x4D
  20. #define KEY_N 0x4E
  21. #define KEY_O 0x4F
  22. #define KEY_P 0x50
  23. #define KEY_Q 0x51
  24. #define KEY_R 0x52
  25. #define KEY_S 0x53
  26. #define KEY_T 0x54
  27. #define KEY_U 0x55
  28. #define KEY_V 0x56
  29. #define KEY_W 0x57
  30. #define KEY_X 0x58
  31. #define KEY_Y 0x59
  32. #define KEY_Z 0x5A
  33. #define KEY_LEFT VK_LEFT
  34. #define KEY_RIGHT VK_RIGHT
  35. #define KEY_UP VK_UP
  36. #define KEY_DOWN VK_DOWN
  37. #define KEY_ESC VK_ESCAPE
  38.  
  39. #include "windows.h"
  40.  
  41. double _tickCount; // Biến đếm thời gian, sử dụng cho Mainloop
  42.  
  43. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); // Lấy handle cửa sổ console hiện hành
  44.  
  45. // Đưa con trỏ đến tọa độ x:y
  46. void gotoxy(short x,short y)
  47. {
  48. HANDLE hConsoleOutput;
  49. COORD Cursor_an_Pos = { x,y};
  50. hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  51. SetConsoleCursorPosition(hConsoleOutput , Cursor_an_Pos);
  52. }
  53.  
  54. // Đặt màu cho chữ
  55. void SetColor(WORD color)
  56. {
  57. HANDLE hConsoleOutput;
  58. hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  59.  
  60. CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
  61. GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
  62.  
  63. WORD wAttributes = screen_buffer_info.wAttributes;
  64. color &= 0x000f;
  65. wAttributes &= 0xfff0;
  66. wAttributes |= color;
  67.  
  68. SetConsoleTextAttribute(hConsoleOutput, wAttributes);
  69. }
  70.  
  71. // Đặt màu nền cho chữ
  72. void SetBGColor(WORD color)
  73. {
  74. HANDLE hConsoleOutput;
  75. hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  76.  
  77. CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
  78. GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
  79.  
  80. WORD wAttributes = screen_buffer_info.wAttributes;
  81. color &= 0x000f;
  82. color <<= 4;
  83. wAttributes &= 0xff0f;
  84. wAttributes |= color;
  85.  
  86. SetConsoleTextAttribute(hConsoleOutput, wAttributes);
  87. }
  88.  
  89. // Thiết lập chế độ hiển thị, có fullscreen hay không
  90. BOOL NT_SetConsoleDisplayMode(HANDLE hOutputHandle, DWORD dwNewMode)
  91. {
  92. typedef BOOL (WINAPI *SCDMProc_t) (HANDLE, DWORD, LPDWORD);
  93. SCDMProc_t SetConsoleDisplayMode;
  94. HMODULE hKernel32;
  95. BOOL bFreeLib = FALSE, ret;
  96. const char KERNEL32_NAME[] = "kernel32.dll";
  97.  
  98. hKernel32 = GetModuleHandleA(KERNEL32_NAME);
  99. if (hKernel32 == NULL)
  100. {
  101. hKernel32 = LoadLibraryA(KERNEL32_NAME);
  102. if (hKernel32 == NULL)
  103. return FALSE;
  104.  
  105. bFreeLib = true;
  106. }
  107.  
  108. SetConsoleDisplayMode =
  109. (SCDMProc_t)GetProcAddress(hKernel32, "SetConsoleDisplayMode");
  110. if (SetConsoleDisplayMode == NULL)
  111. {
  112. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  113. ret = FALSE;
  114. }
  115. else
  116. {
  117. DWORD tmp;
  118. ret = SetConsoleDisplayMode(hOutputHandle, dwNewMode, &tmp);
  119. }
  120.  
  121. if (bFreeLib)
  122. FreeLibrary(hKernel32);
  123.  
  124. return ret;
  125. }
  126.  
  127. // Đặt chế độ FullScreen
  128. void setFullScreen()
  129. {
  130. NT_SetConsoleDisplayMode( GetStdHandle( STD_OUTPUT_HANDLE ), 1 );
  131. }
  132.  
  133. // Thoát khỏi fullscreen
  134. void exitFullScreen()
  135. {
  136. NT_SetConsoleDisplayMode( GetStdHandle( STD_OUTPUT_HANDLE ), 0 );
  137. }
  138.  
  139. // Ẩn hiện con trỏ nhấp nháy trong cửa sổ Console
  140. void ShowCur(bool CursorVisibility)
  141. {
  142. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  143. CONSOLE_CURSOR_INFO cursor = {1, CursorVisibility};
  144. SetConsoleCursorInfo(handle, &cursor);
  145. }
  146.  
  147. // Xóa toàn bộ nội dung cửa sổ console - chôm từ MSDN
  148. void cls( HANDLE hConsole )
  149. {
  150. COORD coordScreen = { 0, 0 }; // home for the cursor
  151. DWORD cCharsWritten;
  152. CONSOLE_SCREEN_BUFFER_INFO csbi;
  153. DWORD dwConSize;
  154.  
  155. // Get the number of character cells in the current buffer.
  156.  
  157. if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
  158. return;
  159. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  160.  
  161. // Fill the entire screen with blanks.
  162.  
  163. if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
  164. dwConSize, coordScreen, &cCharsWritten ))
  165. return;
  166.  
  167. // Get the current text attribute.
  168.  
  169. if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
  170. return;
  171.  
  172. // Set the buffer's attributes accordingly.
  173.  
  174. if( !FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
  175. dwConSize, coordScreen, &cCharsWritten ))
  176. return;
  177.  
  178. // Put the cursor at its home coordinates.
  179.  
  180. SetConsoleCursorPosition( hConsole, coordScreen );
  181. }
  182. // Viết lại hàm clrscr, không cần cũng được nhưng để nhìn vào code cho đẹp mắt
  183. void clrscr()
  184. {
  185. cls(hCon);
  186. }
  187. // Kiểm tra xem phím nào được nhấn, trả về true nếu phím đó đã được nhấn xuống
  188. bool checkKey(int key)
  189. {
  190. return GetAsyncKeyState(key);
  191. }
  192.  
  193. /*
  194. Khúc này mình tự sáng tác thêm để thực hiện vòng lặp chính trong game
  195. Rất tiện dụng
  196. Vì gọi mainloop bằng vòng lặp while sẽ chạy rất nhanh, khó kiểm soát được tốc độ
  197. nên giải pháp đưa ra là dùng một bộ đếm, cứ sau một quãng thời gian tăng giá trị
  198. nếu giá trị đó đạt đến một mức nhất định thì mới gọi hàm mainloop
  199. Cách này kiểm soát tốc độ game khá tốt
  200. Có thêm một cách khác là dùng Timer, nhưng ở đây mình làm vậy cho đơn giản
  201. */
  202.  
  203. // reset lại bộ đếm
  204. void resetTick()
  205. {
  206. _tickCount = 0;
  207. }
  208.  
  209. // Đếm, thực hiện việc tăng biến đếm
  210. void tickCount()
  211. {
  212. _tickCount += 0.1;
  213. }
  214.  
  215. // trả về giá trị hiện tại của biến đếm
  216. double getTickCount()
  217. {
  218. return _tickCount;
  219. }
  220.  
  221. // Thực hiện việc đếm và gọi vòng lặp chính (mainloop)
  222. // Tham số truyền vào gồm có: giá trị cực đại mà sau khi tăng đến mức đó, sẽ gọi tiếp mainloop và renderloop
  223. // gloop: game loop hay còn gọi là mainloop, thực hiện việc xử lý logic trong game
  224. // rloop: render loop, thực hiện việc in các hình ảnh, chữ,.. trong game ra màn hình
  225. void Tick(double maxValue, void (*gloop) (), void (*rloop) ())
  226. {
  227. tickCount();
  228. if (getTickCount() > maxValue){
  229. resetTick();
  230. gloop(); // Game loop
  231. }
  232. rloop(); // Render loop
  233. }
  234.  
  235. int main() {
  236. // your code goes here
  237. return 0;
  238. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:39:21: fatal error: windows.h: No such file or directory
 #include "windows.h"
                     ^
compilation terminated.
stdout
Standard output is empty