4 #if (DRV_OSNUM == 0x0002)
\r
6 // for Mac OSX 32-bit
\r
8 #include <mach/mach.h>
\r
9 #include <Cocoa/Cocoa.h>
\r
11 void *mallocRWE(int bytes)
\r
13 void *p = malloc(bytes);
\r
14 vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
\r
20 @interface OSECPUView : NSView
\r
22 unsigned char *_buf;
\r
25 CGContextRef _context;
\r
28 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
\r
29 - (void)drawRect:(NSRect)rect;
\r
32 @implementation OSECPUView
\r
33 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
\r
35 self = [super initWithFrame:frameRect];
\r
44 - (void)drawRect:(NSRect)rect {
\r
45 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
\r
46 _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
\r
47 CGImageRef image = CGBitmapContextCreateImage(_context);
\r
48 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
\r
49 CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
\r
51 CFRelease(colorSpace);
\r
57 @interface Main : NSObject<NSWindowDelegate>
\r
60 const unsigned char **argv;
\r
66 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;
\r
67 - (BOOL)windowShouldClose:(id)sender;
\r
68 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
\r
69 - (void)flushWin:(NSRect)rect;
\r
72 @implementation Main
\r
75 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
\r
76 HeavyOSECPUMain(argc, (char **)argv);
\r
77 [NSApp terminate:self];
\r
81 - (void)createThread : (int)_argc args:(const unsigned char **)_argv
\r
85 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
\r
89 - (BOOL)windowShouldClose:(id)sender
\r
95 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
\r
98 NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
\r
99 [window setTitle: @"osecpu"];
\r
101 [window makeKeyAndOrderFront:nil];
\r
102 [window setReleasedWhenClosed:YES];
\r
103 window.delegate = self;
\r
104 winClosed = _winClosed;
\r
106 _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
\r
107 [window.contentView addSubview:_view];
\r
110 - (void)flushWin : (NSRect)rect
\r
112 [_view drawRect:rect];
\r
119 int main(int argc, char **argv)
\r
121 objc_main = [[Main alloc] init];
\r
123 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
\r
124 app = [[NSApplication alloc] init];
\r
125 [objc_main createThread:argc args:(const unsigned char **)argv];
\r
131 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
\r
133 [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
\r
136 void drv_flshWin(int sx, int sy, int x0, int y0)
\r
138 [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
\r
141 void drv_sleep(int msec)
\r
143 [NSThread sleepForTimeInterval:0.001*msec];
\r
148 #elif (DRV_OSNUM == 0x0003)
\r
150 #error "Your OS is not supported."
\r
152 #elif (DRV_OSNUM == 0x0001)
\r
154 // for Windows 32-bit
\r
156 #include <windows.h>
\r
159 #define TIMER_INTERVAL 10
\r
165 int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
\r
170 struct BLD_WORK bld_work;
\r
173 int xsiz, ysiz, *buf;
\r
179 int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
\r
180 int tmcount, tmcount0, mod, rand_seed;
\r
182 unsigned char *ftyp;
\r
183 unsigned char **fptn;
\r
185 int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
\r
188 struct BL_WORK bl_work;
\r
190 #define BL_SIZ_KBUF 8192
\r
192 #define BL_WAITKEYF 0x00000001
\r
193 #define BL_WAITKEYNF 0x00000002
\r
194 #define BL_WAITKEY 0x00000003
\r
195 #define BL_GETKEY 0x00000004
\r
196 #define BL_CLEARREP 0x00000008
\r
197 #define BL_DELFFF 0x00000010
\r
199 #define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
\r
202 #define dw bld_work
\r
204 void bld_openWin(int x, int y, char *winClosed);
\r
205 void bld_flshWin(int sx, int sy, int x0, int y0);
\r
206 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
\r
208 int bl_iCol(int i);
\r
209 void bl_readyWin(int n);
\r
211 static HANDLE threadhandle;
\r
213 int main(int argc, char **argv)
\r
215 // Program entry point
\r
216 return HeavyOSECPUMain(argc, argv);
\r
219 void *mallocRWE(int bytes)
\r
221 void *p = malloc(bytes);
\r
223 VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
\r
227 static int winthread(void *dmy)
\r
234 x = dw.bmi.bmiHeader.biWidth;
\r
235 y = -dw.bmi.bmiHeader.biHeight;
\r
237 wc.cbSize = sizeof (WNDCLASSEX);
\r
238 wc.style = CS_HREDRAW | CS_VREDRAW;
\r
239 wc.lpfnWndProc = WndProc;
\r
242 wc.hInstance = dw.hi;
\r
243 wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
\r
244 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
\r
245 wc.hIconSm = wc.hIcon;
\r
246 wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
\r
247 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
\r
248 wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
\r
249 wc.lpszMenuName = NULL;
\r
250 wc.lpszClassName = L"WinClass";
\r
251 if (RegisterClassEx(&wc) == 0)
\r
257 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
\r
258 x = r.right - r.left;
\r
259 y = r.bottom - r.top;
\r
261 char *t = "osecpu";
\r
263 dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
\r
264 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
\r
267 ShowWindow(dw.hw, SW_SHOW);
\r
268 UpdateWindow(dw.hw);
\r
269 SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
\r
270 SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
\r
271 SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
\r
275 i = GetMessage(&msg, NULL, 0, 0);
\r
276 if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
\r
278 /* そのほかはとりあえずデフォルト処理で */
\r
279 TranslateMessage(&msg);
\r
280 DispatchMessage(&msg);
\r
282 // PostQuitMessage(0);
\r
283 dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
\r
284 if (dw.winClosed != NULL)
\r
289 void bld_openWin(int sx, int sy, char *winClosed)
\r
293 dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
\r
294 dw.bmi.bmiHeader.biWidth = sx;
\r
295 dw.bmi.bmiHeader.biHeight = -sy;
\r
296 dw.bmi.bmiHeader.biPlanes = 1;
\r
297 dw.bmi.bmiHeader.biBitCount = 32;
\r
298 dw.bmi.bmiHeader.biCompression = BI_RGB;
\r
299 dw.winClosed = winClosed;
\r
301 threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
\r
306 void drv_flshWin(int sx, int sy, int x0, int y0)
\r
308 InvalidateRect(dw.hw, NULL, FALSE);
\r
309 UpdateWindow(dw.hw);
\r
313 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
\r
316 if (msg == WM_PAINT) {
\r
318 HDC hdc = BeginPaint(dw.hw, &ps);
\r
319 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
\r
320 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
\r
321 EndPaint(dw.hw, &ps);
\r
323 if (msg == WM_DESTROY) {
\r
324 PostQuitMessage(0);
\r
327 if (msg == WM_TIMER && wp == TIMER_ID) {
\r
328 w.tmcount += TIMER_INTERVAL;
\r
331 if (msg == WM_TIMER && wp == TIMER_ID + 1) {
\r
332 dw.tmcount1 += TIMER_INTERVAL * 10;
\r
333 w.tmcount = dw.tmcount1;
\r
336 if (msg == WM_TIMER && wp == TIMER_ID + 2) {
\r
337 dw.tmcount2 += TIMER_INTERVAL * 100;
\r
338 w.tmcount = dw.tmcount1 = dw.tmcount2;
\r
341 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
\r
344 if (wp == VK_RETURN) i = KEY_ENTER;
\r
345 if (wp == VK_ESCAPE) i = KEY_ESC;
\r
346 if (wp == VK_BACK) i = KEY_BACKSPACE;
\r
347 if (wp == VK_TAB) i = KEY_TAB;
\r
348 if (wp == VK_PRIOR) i = KEY_PAGEUP;
\r
349 if (wp == VK_NEXT) i = KEY_PAGEDWN;
\r
350 if (wp == VK_END) i = KEY_END;
\r
351 if (wp == VK_HOME) i = KEY_HOME;
\r
352 if (wp == VK_LEFT) i = KEY_LEFT;
\r
353 if (wp == VK_RIGHT) i = KEY_RIGHT;
\r
354 if (wp == VK_UP) i = KEY_UP;
\r
355 if (wp == VK_DOWN) i = KEY_DOWN;
\r
356 if (wp == VK_INSERT) i = KEY_INS;
\r
357 if (wp == VK_DELETE) i = KEY_DEL;
\r
359 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
\r
360 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
\r
361 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
\r
362 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
\r
363 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
\r
364 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
\r
365 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
\r
366 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
\r
368 if ('A' <= wp && wp <= 'Z') i = wp;
\r
372 // bl_putKeyB(1, &i);
\r
376 if (msg == WM_KEYUP) {
\r
378 // bl_putKeyB(1, &i);
\r
380 if (msg == WM_CHAR) {
\r
382 if (' ' <= wp && wp <= 0x7e) {
\r
385 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
\r
386 if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
\r
387 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
\r
388 if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
\r
389 if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
\r
390 if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
\r
391 if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
\r
392 if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
\r
393 if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
\r
400 // bl_putKeyB(1, &i);
\r
404 return DefWindowProc(hw, msg, wp, lp);
\r
407 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
\r
409 w.win.buf = (int *)buf;
\r
412 bld_openWin(sx, sy, winClosed);
\r
416 void drv_sleep(int msec)
\r
419 // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
\r
420 /* 勉強不足でまだ書き方が分かりません! */
\r
426 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"
\r