OSDN Git Service

描画関連の変数を構造体にまとめた。
[heavyosecpu/HeavyOSECPU.git] / dpndenv.c
1 #include "osecpu.h"
2
3
4 #if (DRV_OSNUM == 0x0002)
5 //
6 // for Mac OSX 32-bit
7 //
8
9 // by Liva, 2013.05.29-
10
11 #include <mach/mach.h>
12 #include <Cocoa/Cocoa.h>
13
14 void *mallocRWE(int bytes)
15 {
16         void *p = malloc(bytes);
17         vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
18         return p;
19 }
20
21 NSApplication* app;
22
23 @interface OSECPUView : NSView
24 {
25     unsigned char *_buf;
26     int _sx;
27     int _sy;
28         CGContextRef _context;
29 }
30
31 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
32 - (void)drawRect:(NSRect)rect;
33 @end
34
35 @implementation OSECPUView
36 - (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
37 {
38     self = [super initWithFrame:frameRect];
39     if (self) {
40         _buf = buf;
41         _sx = sx;
42         _sy = sy;
43     }
44     return self;
45 }
46
47 - (void)drawRect:(NSRect)rect {
48     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
49     _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
50     CGImageRef image = CGBitmapContextCreateImage(_context);
51     CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
52     CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
53     
54     CFRelease(colorSpace);
55     CFRelease(image);
56 }
57
58 @end
59
60 @interface Main : NSObject<NSWindowDelegate>
61 {
62     int argc;
63     const unsigned char **argv;
64     char *winClosed;
65     OSECPUView *_view;
66 }
67
68 - (void)runApp;
69 - (void)createThread:(int)_argc args:(const unsigned char **)_argv;
70 - (BOOL)windowShouldClose:(id)sender;
71 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
72 - (void)flushWin:(NSRect)rect;
73 @end
74
75 @implementation Main
76 - (void)runApp
77 {
78     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
79     HeavyOSECPUMain(argc, (char **)argv);
80     [NSApp terminate:self];
81         [pool release];
82 }
83
84 - (void)createThread : (int)_argc args:(const unsigned char **)_argv
85 {
86     argc = _argc;
87     argv = _argv;
88     NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
89     [thread start];
90 }
91
92 - (BOOL)windowShouldClose:(id)sender
93 {
94     *winClosed = 1;
95     return YES;
96 }
97
98 - (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
99 {
100     
101         NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
102         [window setTitle: @"osecpu"];
103         [window center];
104         [window makeKeyAndOrderFront:nil];
105         [window setReleasedWhenClosed:YES];
106         window.delegate = self;
107         winClosed = _winClosed;
108     
109         _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
110         [window.contentView addSubview:_view];
111 }
112
113 - (void)flushWin : (NSRect)rect
114 {
115     [_view drawRect:rect];
116 }
117
118 @end
119
120 id objc_main;
121
122 int main(int argc, char **argv)
123 {
124     objc_main = [[Main alloc] init];
125     
126     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
127     app = [[NSApplication alloc] init];
128     [objc_main createThread:argc args:(const unsigned char **)argv];
129     [app run];
130         [pool release];
131     return 0;
132 }
133
134 void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
135 {
136     [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
137 }
138
139 void drv_flshWin(int sx, int sy, int x0, int y0)
140 {
141     [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
142 }
143
144 void drv_sleep(int msec)
145 {
146     [NSThread sleepForTimeInterval:0.001*msec];
147     return;
148 }
149
150
151 #elif (DRV_OSNUM == 0x0003)
152
153 #error "Your OS is not supported."
154
155 #elif (DRV_OSNUM == 0x0001)
156 //
157 // for Windows 32-bit
158 //
159 #include <windows.h>
160
161 #define TIMER_ID                         1
162 #define TIMER_INTERVAL          10
163
164 struct BLD_WORK {
165         HINSTANCE hi;
166         HWND hw;
167         BITMAPINFO bmi;
168         int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
169         HANDLE mtx;
170         char *winClosed;
171 };
172
173 struct BLD_WORK bld_work;
174
175 struct BL_WIN {
176         int xsiz, ysiz, *buf;
177 };
178
179 struct BL_WORK {
180         struct BL_WIN win;
181         jmp_buf jb;
182         int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
183         int tmcount, tmcount0, mod, rand_seed;
184         int *cbuf;
185         unsigned char *ftyp;
186         unsigned char **fptn;
187         int *ccol, *cbak;
188         int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
189 };
190
191 struct BL_WORK bl_work;
192
193 #define BL_SIZ_KBUF                     8192
194
195 #define BL_WAITKEYF             0x00000001
196 #define BL_WAITKEYNF    0x00000002
197 #define BL_WAITKEY              0x00000003
198 #define BL_GETKEY               0x00000004
199 #define BL_CLEARREP             0x00000008
200 #define BL_DELFFF               0x00000010
201
202 #define BL_KEYMODE              0x00000000      // 作りかけ, make/remake/breakが見えるかどうか
203
204 #define w       bl_work
205 #define dw      bld_work
206
207 void bld_openWin(int x, int y, char *winClosed);
208 void bld_flshWin(int sx, int sy, int x0, int y0);
209 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
210 void bl_cls();
211 int bl_iCol(int i);
212 void bl_readyWin(int n);
213
214 static HANDLE threadhandle;
215
216 int main(int argc, char **argv)
217 {
218         // Program entry point
219         return HeavyOSECPUMain(argc, argv);
220 }
221
222 void *mallocRWE(int bytes)
223 {
224         void *p = malloc(bytes);
225         DWORD dmy;
226         VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
227         return p;
228 }
229
230 static int winthread(void *dmy)
231 {
232         WNDCLASSEX wc;
233         RECT r;
234         int i, x, y;
235         MSG msg;
236
237         x = dw.bmi.bmiHeader.biWidth;
238         y = -dw.bmi.bmiHeader.biHeight;
239
240         wc.cbSize = sizeof (WNDCLASSEX);
241         wc.style = CS_HREDRAW | CS_VREDRAW;
242         wc.lpfnWndProc = WndProc;
243         wc.cbClsExtra = 0;
244         wc.cbWndExtra = 0;
245         wc.hInstance = dw.hi;
246         wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
247                 IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
248         wc.hIconSm = wc.hIcon;
249         wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
250                 IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
251         wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
252         wc.lpszMenuName = NULL;
253         wc.lpszClassName = L"WinClass";
254         if (RegisterClassEx(&wc) == 0)
255                 return 1;
256         r.left = 0;
257         r.top = 0;
258         r.right = x;
259         r.bottom = y;
260         AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
261         x = r.right - r.left;
262         y = r.bottom - r.top;
263
264         char *t = "osecpu";
265
266         dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
267                 CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
268         if (dw.hw == NULL)
269                 return 1;
270         ShowWindow(dw.hw, SW_SHOW);
271         UpdateWindow(dw.hw);
272         SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
273         SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
274         SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
275         dw.flags |= 2 | 4;
276
277         for (;;) {
278                 i = GetMessage(&msg, NULL, 0, 0);
279                 if (i == 0 || i == -1)  /* エラーもしくは終了メッセージ */
280                         break;
281                 /* そのほかはとりあえずデフォルト処理で */
282                 TranslateMessage(&msg);
283                 DispatchMessage(&msg);
284         }
285         //      PostQuitMessage(0);
286         dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
287         if (dw.winClosed != NULL)
288                 *dw.winClosed = 1;
289         return 0;
290 }
291
292 void bld_openWin(int sx, int sy, char *winClosed)
293 {
294         static int i;
295
296         dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
297         dw.bmi.bmiHeader.biWidth = sx;
298         dw.bmi.bmiHeader.biHeight = -sy;
299         dw.bmi.bmiHeader.biPlanes = 1;
300         dw.bmi.bmiHeader.biBitCount = 32;
301         dw.bmi.bmiHeader.biCompression = BI_RGB;
302         dw.winClosed = winClosed;
303
304         threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
305
306         return;
307 }
308
309 void drv_flshWin(int sx, int sy, int x0, int y0)
310 {
311         InvalidateRect(dw.hw, NULL, FALSE);
312         UpdateWindow(dw.hw);
313         return;
314 }
315
316 LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
317 {
318         int i, j;
319         if (msg == WM_PAINT) {
320                 PAINTSTRUCT ps;
321                 HDC hdc = BeginPaint(dw.hw, &ps);
322                 SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
323                         0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
324                 EndPaint(dw.hw, &ps);
325         }
326         if (msg == WM_DESTROY) {
327                 PostQuitMessage(0);
328                 return 0;
329         }
330         if (msg == WM_TIMER && wp == TIMER_ID) {
331                 w.tmcount += TIMER_INTERVAL;
332                 return 0;
333         }
334         if (msg == WM_TIMER && wp == TIMER_ID + 1) {
335                 dw.tmcount1 += TIMER_INTERVAL * 10;
336                 w.tmcount = dw.tmcount1;
337                 return 0;
338         }
339         if (msg == WM_TIMER && wp == TIMER_ID + 2) {
340                 dw.tmcount2 += TIMER_INTERVAL * 100;
341                 w.tmcount = dw.tmcount1 = dw.tmcount2;
342                 return 0;
343         }
344         if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
345                 i = -1;
346
347                 if (wp == VK_RETURN)    i = KEY_ENTER;
348                 if (wp == VK_ESCAPE)    i = KEY_ESC;
349                 if (wp == VK_BACK)              i = KEY_BACKSPACE;
350                 if (wp == VK_TAB)               i = KEY_TAB;
351                 if (wp == VK_PRIOR)             i = KEY_PAGEUP;
352                 if (wp == VK_NEXT)              i = KEY_PAGEDWN;
353                 if (wp == VK_END)               i = KEY_END;
354                 if (wp == VK_HOME)              i = KEY_HOME;
355                 if (wp == VK_LEFT)              i = KEY_LEFT;
356                 if (wp == VK_RIGHT)             i = KEY_RIGHT;
357                 if (wp == VK_UP)                i = KEY_UP;
358                 if (wp == VK_DOWN)              i = KEY_DOWN;
359                 if (wp == VK_INSERT)    i = KEY_INS;
360                 if (wp == VK_DELETE)    i = KEY_DEL;
361                 j &= 0;
362                 if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
363                 if ((GetKeyState(VK_LMENU)    & (1 << 15)) != 0) j |= 1 << 18;
364                 if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
365                 if ((GetKeyState(VK_RMENU)    & (1 << 15)) != 0) j |= 1 << 26;
366                 if ((GetKeyState(VK_RSHIFT)   & (1 << 15)) != 0) i |= 1 << 24;
367                 if ((GetKeyState(VK_LSHIFT)   & (1 << 15)) != 0) i |= 1 << 16;
368                 if ((GetKeyState(VK_NUMLOCK)  & (1 << 0)) != 0) i |= 1 << 22;
369                 if ((GetKeyState(VK_CAPITAL)  & (1 << 0)) != 0) i |= 1 << 23;
370                 if (j != 0) {
371                         if ('A' <= wp && wp <= 'Z') i = wp;
372                 }
373                 if (i != -1) {
374                         putKeybuf(i | j);
375                         //                      bl_putKeyB(1, &i);
376                         return 0;
377                 }
378         }
379         if (msg == WM_KEYUP) {
380                 i = 0xfff;
381                 //              bl_putKeyB(1, &i);
382         }
383         if (msg == WM_CHAR) {
384                 i = 0;
385                 if (' ' <= wp && wp <= 0x7e) {
386                         i = wp;
387                         j &= 0;
388                         if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
389                         if ((GetKeyState(VK_LMENU)    & (1 << 15)) != 0) j |= 1 << 18;
390                         if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
391                         if ((GetKeyState(VK_RMENU)    & (1 << 15)) != 0) j |= 1 << 26;
392                         if ((GetKeyState(VK_RSHIFT)   & (1 << 15)) != 0) i |= 1 << 24;
393                         if ((GetKeyState(VK_LSHIFT)   & (1 << 15)) != 0) i |= 1 << 16;
394                         if ((GetKeyState(VK_NUMLOCK)  & (1 << 0)) != 0) i |= 1 << 22;
395                         if ((GetKeyState(VK_CAPITAL)  & (1 << 0)) != 0) i |= 1 << 23;
396                         if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
397                                 if (j != 0) {
398                                         i |= j;
399                                         i &= ~0x20;
400                                 }
401                         }
402                         putKeybuf(i);
403                         //                      bl_putKeyB(1, &i);
404                         return 0;
405                 }
406         }
407         return DefWindowProc(hw, msg, wp, lp);
408 }
409
410 void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
411 {
412         w.win.buf = (int *)buf;
413         w.win.xsiz = sx;
414         w.win.ysiz = sy;
415         bld_openWin(sx, sy, winClosed);
416         return;
417 }
418
419 void drv_sleep(int msec)
420 {
421         Sleep(msec);
422         //      MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
423         /* 勉強不足でまだ書き方が分かりません! */
424         return;
425 }
426
427 #else
428
429 #error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"
430
431 #endif