win32text.c 6.3 KB

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