win32text.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // wonbe Win32 text emulation
  2. // First Created: Nov.4,2000 by Nashiko
  3. // Copyright 2000 (c) by Pie Dey Co.,Ltd.
  4. #include <windows.h>
  5. #include "win32text.h"
  6. #define SCREEN_SIZE_X (224/8)
  7. #define SCREEN_SIZE_Y (144/8)
  8. static WORD screen[SCREEN_SIZE_X][SCREEN_SIZE_Y];
  9. static WORD lastChar;
  10. static HWND hwnd;
  11. static void wmPaint( HWND hWnd, HDC hdc )
  12. {
  13. int x,y;
  14. int xArea, yArea;
  15. RECT rect, rectDraw;
  16. TEXTMETRIC tm;
  17. GetClientRect( hWnd, &rect );
  18. GetTextMetrics( hdc, &tm );
  19. xArea = SCREEN_SIZE_X * tm.tmMaxCharWidth;
  20. yArea = SCREEN_SIZE_Y * tm.tmHeight;
  21. rectDraw.left = (rect.right-rect.left)/2-xArea/2;
  22. rectDraw.top = (rect.bottom-rect.top)/2-yArea/2;
  23. rectDraw.right = (rect.right-rect.left)/2+xArea/2;
  24. rectDraw.bottom = (rect.bottom-rect.top)/2+yArea/2;
  25. FillRect( hdc, &rect, GetStockObject(GRAY_BRUSH) );
  26. FillRect( hdc, &rectDraw, GetStockObject(WHITE_BRUSH) );
  27. for( y=0; y<SCREEN_SIZE_Y; y++ ) {
  28. for( x=0; x<SCREEN_SIZE_X; x++ ) {
  29. WORD val;
  30. BYTE str[2];
  31. val = screen[x][y];
  32. if( val >= 0x100 ) {
  33. str[0] = (val >> 8);
  34. str[1] = (val & 0xff);
  35. TextOut( hdc,
  36. rectDraw.left+x*tm.tmMaxCharWidth,
  37. rectDraw.top+y*tm.tmHeight,
  38. str, 2 );
  39. } else {
  40. str[0] = (BYTE)val;
  41. TextOut( hdc,
  42. rectDraw.left+x*tm.tmMaxCharWidth,
  43. rectDraw.top+y*tm.tmHeight,
  44. str, 1 );
  45. }
  46. }
  47. }
  48. }
  49. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  50. {
  51. PAINTSTRUCT ps;
  52. HDC hdc;
  53. switch (message) {
  54. case WM_PAINT:
  55. hdc = BeginPaint(hWnd, &ps);
  56. wmPaint(hWnd,hdc);
  57. EndPaint(hWnd, &ps);
  58. break;
  59. case WM_CHAR:
  60. lastChar = (WORD)wParam;
  61. break;
  62. case WM_CLOSE:
  63. return 0; // 閉じちゃ駄目
  64. case WM_DESTROY:
  65. hwnd = NULL;
  66. PostQuitMessage(0);
  67. break;
  68. default:
  69. return (DefWindowProc(hWnd, message, wParam, lParam));
  70. }
  71. return (0);
  72. }
  73. static int createTextInThread()
  74. {
  75. WNDCLASS wc;
  76. wc.style = CS_HREDRAW | CS_VREDRAW;
  77. wc.lpfnWndProc = (WNDPROC)WndProc;
  78. wc.cbClsExtra = 0;
  79. wc.cbWndExtra = 0;
  80. wc.hInstance = GetModuleHandle(NULL);
  81. wc.hIcon = NULL;
  82. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  83. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  84. wc.lpszMenuName = NULL;
  85. wc.lpszClassName = "wonbe_win32text";
  86. RegisterClass(&wc);
  87. hwnd = CreateWindow(wc.lpszClassName, "WONBE TEXT", WS_OVERLAPPEDWINDOW,
  88. CW_USEDEFAULT, 0, SCREEN_SIZE_X*16+64, SCREEN_SIZE_Y*20+64,
  89. NULL, NULL, wc.hInstance, NULL);
  90. if( !hwnd ) return FALSE;
  91. clearText();
  92. ShowWindow( hwnd, SW_SHOW );
  93. UpdateWindow( hwnd );
  94. return TRUE;
  95. }
  96. void clearText()
  97. {
  98. int x,y;
  99. if( hwnd == NULL ) return;
  100. for( y=0; y<SCREEN_SIZE_Y; y++ ) {
  101. for( x=0; x<SCREEN_SIZE_X; x++ ) {
  102. screen[x][y] = ' ';
  103. }
  104. }
  105. InvalidateRect( hwnd, NULL, FALSE );
  106. }
  107. void drawChar( int x, int y, unsigned short ch )
  108. {
  109. if( x < 0 ) return;
  110. if( y < 0 ) return;
  111. if( x >= SCREEN_SIZE_X ) return;
  112. if( y >= SCREEN_SIZE_Y ) return;
  113. screen[x][y] = ch;
  114. InvalidateRect( hwnd, NULL, FALSE );
  115. }
  116. void updateText()
  117. {
  118. UpdateWindow( hwnd );
  119. }
  120. void deleteText()
  121. {
  122. if( hwnd == NULL ) return;
  123. DestroyWindow( hwnd );
  124. hwnd = NULL;
  125. }
  126. DWORD WINAPI ThreadFunc( LPVOID lpParam )
  127. {
  128. MSG msg;
  129. createTextInThread();
  130. while (GetMessage(&msg, NULL, 0, 0)) {
  131. TranslateMessage(&msg);
  132. DispatchMessage(&msg);
  133. }
  134. return 0;
  135. }
  136. int createText()
  137. {
  138. DWORD dwThreadId, dwThrdParam = 1;
  139. HANDLE hThread;
  140. lastChar = 0;
  141. hThread = CreateThread(
  142. NULL, // no security attributes
  143. 0, // use default stack size
  144. ThreadFunc, // thread function
  145. &dwThrdParam, // argument to thread function
  146. 0, // use default creation flags
  147. &dwThreadId); // returns the thread identifier
  148. return TRUE;
  149. }
  150. unsigned short win32_wait()
  151. {
  152. WORD t;
  153. while( TRUE ) {
  154. // 以下の2行は本当はマルチスレッド的にNGなんだけど
  155. // テスト用環境だから大目に見る
  156. t = lastChar;
  157. lastChar = 0;
  158. switch( t ) {
  159. case 'p':
  160. case 'P':
  161. return 0x02; // START BUTTON
  162. case ' ':
  163. return 0x04; //KEYWORD_SCAN_A;
  164. case 0x1b:
  165. return 0x08; //KEYWORD_SCAN_B;
  166. case 'e':
  167. case 'E':
  168. return 0x10; //KEYWORD_SCAN_X1;
  169. case 'd':
  170. case 'D':
  171. return 0x20; //KEYWORD_SCAN_X2;
  172. case 'x':
  173. case 'X':
  174. return 0x40; //KEYWORD_SCAN_X3;
  175. case 's':
  176. case 'S':
  177. return 0x80; //KEYWORD_SCAN_X4;
  178. case 't':
  179. case 'T':
  180. return 0x100; //KEYWORD_SCAN_Y1;
  181. case 'g':
  182. case 'G':
  183. return 0x200; //KEYWORD_SCAN_Y2;
  184. case 'v':
  185. case 'V':
  186. return 0x400; //KEYWORD_SCAN_Y3;
  187. case 'f':
  188. case 'F':
  189. return 0x800; //KEYWORD_SCAN_Y4;
  190. }
  191. Sleep( 100 );
  192. }
  193. }
  194. unsigned short win32_scan()
  195. {
  196. WORD t;
  197. t = 0;
  198. if( GetAsyncKeyState('P') & 0x8000 ) {
  199. t |= 0x02; // START
  200. }
  201. if( GetAsyncKeyState(VK_SPACE) & 0x8000 ) {
  202. t |= 0x04; // A
  203. }
  204. if( GetAsyncKeyState(VK_ESCAPE) & 0x8000 ) {
  205. t |= 0x08; // B
  206. }
  207. if( GetAsyncKeyState('E') & 0x8000 ) {
  208. t |= 0x10; // X1
  209. }
  210. if( GetAsyncKeyState('D') & 0x8000 ) {
  211. t |= 0x20; // X2
  212. }
  213. if( GetAsyncKeyState('X') & 0x8000 ) {
  214. t |= 0x40; // X3
  215. }
  216. if( GetAsyncKeyState('S') & 0x8000 ) {
  217. t |= 0x80; // X4
  218. }
  219. if( GetAsyncKeyState('T') & 0x8000 ) {
  220. t |= 0x100; // Y1
  221. }
  222. if( GetAsyncKeyState('G') & 0x8000 ) {
  223. t |= 0x200; // Y2
  224. }
  225. if( GetAsyncKeyState('V') & 0x8000 ) {
  226. t |= 0x400; // Y3
  227. }
  228. if( GetAsyncKeyState('F') & 0x8000 ) {
  229. t |= 0x800; // Y4
  230. }
  231. return t;
  232. }
  233. unsigned int win32_get_tick_count()
  234. {
  235. return GetTickCount()/10;
  236. }
  237. void win32_sys_wait( unsigned int val )
  238. {
  239. Sleep( val*10 );
  240. }
  241. // end of win32text.c