1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
#include "Core.h"
#if defined CC_BUILD_MACCLASSIC
#include "_WindowBase.h"
#include "String.h"
#include "Funcs.h"
#include "Bitmap.h"
#include "Options.h"
#include "Errors.h"
#undef true
#undef false
#include <Quickdraw.h>
#include <Dialogs.h>
#include <Fonts.h>
#include <Events.h>
#include <LowMem.h>
#ifndef M68K_INLINE
#include <DiskInit.h>
#include <Scrap.h>
#endif
#include <Gestalt.h>
static WindowPtr win;
static cc_bool hasColorQD, useGWorld;
/*########################################################################################################################*
*--------------------------------------------------Console log window-----------------------------------------------------*
*#########################################################################################################################*/
static int con_cellSizeX, con_cellSizeY;
static int con_rows, con_cols;
static int cursorX, cursorY;
static WindowPtr con_win;
static Rect con_bounds;
static void Console_EraseLine(int y) {
Rect r = con_bounds;
r.top += y * con_cellSizeY;
r.bottom = r.top + con_cellSizeY;
MoveTo(r.left, r.bottom - 2);
EraseRect(&r);
}
static void Console_Init(void) {
Rect r = qd.screenBits.bounds;
r.top += 40;
InsetRect(&r, 5, 5);
con_win = NewWindow(NULL, &r, "\pConsole log", true, 0, (WindowPtr)-1, true, 0);
GrafPtr savedPort;
GetPort(&savedPort);
SetPort(con_win);
con_bounds = con_win->portRect;
EraseRect(&con_bounds);
TextFont(kFontIDMonaco);
TextSize(9);
InsetRect(&con_bounds, 2, 2);
con_cellSizeX = CharWidth('M');
con_cellSizeY = 12;
con_rows = (con_bounds.bottom - con_bounds.top) / con_cellSizeY;
con_cols = (con_bounds.right - con_bounds.left) / con_cellSizeX;
Console_EraseLine(0);
cursorX = cursorY = 0;
SetPort(savedPort);
}
static void Console_NewLine(void) {
Console_EraseLine(cursorY);
cursorY++;
cursorX = 0;
if (cursorY >= con_rows) cursorY = 0;
}
void Console_Write(const char* msg, int len) {
if (!con_win) Console_Init();
GrafPtr savedPort;
GetPort(&savedPort);
SetPort(con_win);
for (int i = 0; i < len; i++)
{
DrawChar(msg[i]);
cursorX++;
if (cursorX >= con_cols) Console_NewLine();
}
Console_NewLine();
SetPort(savedPort);
}
/*########################################################################################################################*
*---------------------------------------------------Imported headers------------------------------------------------------*
*#########################################################################################################################*/
// On 68k these are implemented using direct 68k opcodes
// On PPC these are implemented using function calls
#if TARGET_CPU_68K
#define MAC_SYSAPI(_type) static _type
#define MAC_ONEWORDINLINE(w1) = w1
#define MAC_TWOWORDINLINE(w1,w2) = {w1, w2}
#define MAC_THREEWORDINLINE(w1,w2,w3) = {w1, w2, w3}
#define MAC_FOURWORDINLINE(w1,w2,w3,w4) = {w1, w2, w3, w4}
#else
#define MAC_SYSAPI(_type) extern pascal _type
#define MAC_ONEWORDINLINE(w1)
#define MAC_TWOWORDINLINE(w1,w2)
#define MAC_THREEWORDINLINE(w1,w2,w3)
#define MAC_FOURWORDINLINE(w1,w2,w3,w4)
#endif
typedef unsigned long MAC_FourCharCode;
typedef SInt16 MAC_WindowPartCode;
typedef UInt16 MAC_EventMask;
// Workaround issue in multiversal headers
#if defined M68K_INLINE && TARGET_CPU_68K
// Availability: in InterfaceLib 7.1 and later
MAC_SYSAPI(void) _SetEventMask(MAC_EventMask value) MAC_TWOWORDINLINE(0x31DF, 0x0144);
#define SetEventMask _SetEventMask
#endif
/*########################################################################################################################*
*--------------------------------------------------Public implementation--------------------------------------------------*
*#########################################################################################################################*/
static const cc_uint8 key_map[8 * 16] = {
/* 0x00 */ 'A', 'S', 'D', 'F', 'H', 'G', 'Z', 'X',
/* 0x08 */ 'C', 'V', 0, 'B', 'Q', 'W', 'E', 'R',
/* 0x10 */ 'Y', 'T', '1', '2', '3', '4', '6', '5',
/* 0x18 */ CCKEY_EQUALS, '9', '7', CCKEY_MINUS, '8', '0', CCKEY_RBRACKET, 'O',
/* 0x20 */ 'U', CCKEY_LBRACKET, 'I', 'P', CCKEY_ENTER, 'L', 'J', CCKEY_QUOTE,
/* 0x28 */ 'K', CCKEY_SEMICOLON, CCKEY_BACKSLASH, CCKEY_COMMA, CCKEY_SLASH, 'N', 'M', CCKEY_PERIOD,
/* 0x30 */ CCKEY_TAB, CCKEY_SPACE, CCKEY_TILDE, CCKEY_BACKSPACE, 0, CCKEY_ESCAPE, 0, CCKEY_LWIN,
/* 0x38 */ CCKEY_LSHIFT, CCKEY_CAPSLOCK, CCKEY_LALT, CCKEY_LCTRL, 0, 0, 0, 0,
/* 0x40 */ 0, CCKEY_KP_DECIMAL, 0, CCKEY_KP_MULTIPLY, 0, CCKEY_KP_PLUS, 0, CCKEY_NUMLOCK,
/* 0x48 */ CCKEY_VOLUME_UP, CCKEY_VOLUME_DOWN, CCKEY_VOLUME_MUTE, CCKEY_KP_DIVIDE, CCKEY_KP_ENTER, 0, CCKEY_KP_MINUS, 0,
/* 0x50 */ 0, CCKEY_KP_ENTER, CCKEY_KP0, CCKEY_KP1, CCKEY_KP2, CCKEY_KP3, CCKEY_KP4, CCKEY_KP5,
/* 0x58 */ CCKEY_KP6, CCKEY_KP7, 0, CCKEY_KP8, CCKEY_KP9, 'N', 'M', CCKEY_PERIOD,
/* 0x60 */ CCKEY_F5, CCKEY_F6, CCKEY_F7, CCKEY_F3, CCKEY_F8, CCKEY_F9, 0, CCKEY_F11,
/* 0x68 */ 0, CCKEY_F13, 0, CCKEY_F14, 0, CCKEY_F10, 0, CCKEY_F12,
/* 0x70 */ 'U', CCKEY_F15, CCKEY_INSERT, CCKEY_HOME, CCKEY_PAGEUP, CCKEY_DELETE, CCKEY_F4, CCKEY_END,
/* 0x78 */ CCKEY_F2, CCKEY_PAGEDOWN, CCKEY_F1, CCKEY_LEFT, CCKEY_RIGHT, CCKEY_DOWN, CCKEY_UP, 0,
};
static int MapNativeKey(UInt32 key) { return key < Array_Elems(key_map) ? key_map[key] : 0; }
void Window_PreInit(void) {
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
InitDialogs(NULL);
InitCursor();
EventRecord event;
for (int i = 0; i < 5; i++)
EventAvail(everyEvent, &event);
FlushEvents(everyEvent, 0);
SetEventMask(everyEvent);
long tmpLong = 0;
Gestalt(gestaltQuickdrawVersion, &tmpLong);
hasColorQD = tmpLong >= gestalt32BitQD;
}
void Window_Init(void) {
Rect r = qd.screenBits.bounds;
DisplayInfo.x = r.left;
DisplayInfo.y = r.top;
DisplayInfo.Width = r.right - r.left;
DisplayInfo.Height = r.bottom - r.top;
DisplayInfo.ScaleX = 1.0f;
DisplayInfo.ScaleY = 1.0f;
}
void Window_Free(void) { }
static void DoCreateWindow(int width, int height) {
if (Window_Main.Exists) return;
Rect r = qd.screenBits.bounds;
r.top += 40;
InsetRect(&r, 100, 100);
if (hasColorQD) {
win = NewCWindow(NULL, &r, "\pClassiCube", true, documentProc, (WindowPtr)-1, true, 0);
} else {
win = NewWindow( NULL, &r, "\pClassiCube", true, documentProc, (WindowPtr)-1, true, 0);
}
if (hasColorQD) {
Platform_LogConst("BLITTING IN FAST MODE");
} else {
Platform_LogConst("BLITTING IN SLOW MODE");
}
useGWorld = hasColorQD;
SetPort(win);
r = win->portRect;
Window_Main.Width = r.right - r.left;
Window_Main.Height = r.bottom - r.top;
Window_Main.Focused = true;
Window_Main.Exists = true;
}
void Window_Create2D(int width, int height) { DoCreateWindow(width, height); }
void Window_Create3D(int width, int height) { DoCreateWindow(width, height); }
void Window_SetTitle(const cc_string* title) {
// TODO
}
void Clipboard_GetText(cc_string* value) {
Handle tmp = NewHandle(0);
HLock(tmp);
int dataSize = GetScrap(tmp, 'TEXT', 0);
HUnlock(tmp);
String_AppendAll(value, (char*)tmp, dataSize);
DisposeHandle(tmp);
// TODO
}
void Clipboard_SetText(const cc_string* value) {
PutScrap(value->length, 'TEXT', value->buffer);
// TODO
}
int Window_GetWindowState(void) {
return WINDOW_STATE_NORMAL;
// TODO
}
cc_result Window_EnterFullscreen(void) {
// TODO
return 0;
}
cc_result Window_ExitFullscreen(void) {
// TODO
return 0;
}
int Window_IsObscured(void) { return 0; }
void Window_Show(void) {
// TODO
}
void Window_SetSize(int width, int height) {
// TODO
}
void Window_RequestClose(void) {
Event_RaiseVoid(&WindowEvents.Closing);
}
static void HandleMouseDown(EventRecord* event) {
MAC_WindowPartCode part;
WindowPtr window;
Point localPoint;
long res;
int x, y;
part = FindWindow(event->where, &window);
switch (part)
{
case inMenuBar:
HiliteMenu(0);
break;
case inSysWindow:
SystemClick(event, window);
break;
case inContent:
SetPt(&localPoint, event->where.h, event->where.v);
GlobalToLocal(&localPoint);
if (window != win) break;
x = localPoint.h;
y = localPoint.v;
Pointer_SetPosition(0, x, y);
Input_SetPressed(CCMOUSE_L);
break;
case inDrag:
DragWindow(window, event->where, &qd.screenBits.bounds);
break;
case inGoAway:
if (TrackGoAway(window, event->where)) {
Window_RequestClose();
Window_Main.Exists = false;
}
break;
case inGrow:
res = GrowWindow(window, event->where, &qd.screenBits.bounds);
x = res & 0xFFFF;
y = res >> 16;
SizeWindow(window, x, y, false);
if (window != win) break;
Window_Main.Width = x;
Window_Main.Height = y;
Event_RaiseVoid(&WindowEvents.Resized);
break;
}
}
static void HandleMouseUp(EventRecord* event) {
Input_SetReleased(CCMOUSE_L);
}
static void HandleKeyDown(EventRecord* event) {
if ((event->modifiers & cmdKey))
HiliteMenu(0);
int ch = event->message & 0xFF;
int key = (event->message >> 8) & 0xFF;
if (ch >= 32 && ch <= 127)
Event_RaiseInt(&InputEvents.Press, (cc_unichar)ch);
key = MapNativeKey(key);
if (key) Input_SetPressed(key);
}
static void HandleKeyUp(EventRecord* event) {
int key = (event->message >> 8) & 0xFF;
key = MapNativeKey(key);
if (key) Input_SetReleased(key);
}
void Window_ProcessEvents(float delta) {
EventRecord event;
SystemTask();
while (GetNextEvent(everyEvent, &event)) {
switch (event.what)
{
case mouseDown:
HandleMouseDown(&event);
break;
case mouseUp:
HandleMouseUp(&event);
break;
case keyDown:
case autoKey:
HandleKeyDown(&event);
break;
case keyUp:
HandleKeyUp(&event);
break;
case updateEvt:
BeginUpdate((WindowPtr)event.message);
EndUpdate( (WindowPtr)event.message);
Event_RaiseVoid(&WindowEvents.RedrawNeeded);
break;
case diskEvt:
if ((event.message & 0xFFFF0000) != noErr)
{
Point pt = { 100, 100 };
DILoad();
DIBadMount(pt, event.message);
DIUnload();
}
break;
case kHighLevelEvent:
AEProcessAppleEvent(&event);
break;
default:
break;
}
}
}
void Window_ProcessGamepads(float delta) {
}
static void Cursor_GetRawPos(int* x, int* y) {
Point point;
GetMouse(&point);
*x = point.h;
*y = point.v;
}
void Cursor_SetPosition(int x, int y) {
// TODO
Point where;
where.h = x;
where.v = y;
//LMSetRawMouseLocation(where);
}
static void Cursor_DoSetVisible(cc_bool visible) {
if (visible) {
ShowCursor();
} else {
HideCursor();
}
}
static void ShowDialogCore(const char* title, const char* msg) {
// TODO
for (int i = 0; i < 20; i++)
{
Platform_LogConst(title);
Platform_LogConst(msg);
}
}
cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) {
// TODO
return ERR_NOT_SUPPORTED;
}
cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) {
// TODO
return ERR_NOT_SUPPORTED;
}
static GWorldPtr fb_world;
static PixMapHandle fb_pixmap;
void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) {
if (!useGWorld) {
bmp->scan0 = (BitmapCol*)Mem_Alloc(width * height, 4, "window pixels");
bmp->width = width;
bmp->height = height;
return;
}
QDErr err = NewGWorld(&fb_world, 32, &win->portRect, 0, 0, 0);
if (err != noErr) Logger_Abort2(err, "Failed to allocate GWorld");
fb_pixmap = GetGWorldPixMap(fb_world);
if (!fb_pixmap) Logger_Abort("Failed to allocate pixmap");
LockPixels(fb_pixmap);
int stride = (*fb_pixmap)->rowBytes & 0x3FFF;
bmp->scan0 = (BitmapCol*)GetPixBaseAddr(fb_pixmap);
bmp->width = stride >> 2;
bmp->height = height;
}
static void DrawFramebufferBulk(Rect2D r, struct Bitmap* bmp) {
GrafPtr thePort = (GrafPtr)win;
BitMap* memBits = &((GrafPtr)fb_world)->portBits;
BitMap* winBits = &thePort->portBits;
Rect update;
update.left = r.x;
update.right = r.x + r.width;
update.top = r.y;
update.bottom = r.y + r.height;
CopyBits(memBits, winBits, &update, &update, srcCopy, nil);
}
static void DrawFramebufferSlow(Rect2D r, struct Bitmap* bmp) {
for (int y = r.y; y < r.y + r.height; ++y)
{
BitmapCol* row = Bitmap_GetRow(bmp, y);
for (int x = r.x; x < r.x + r.width; ++x)
{
// TODO optimise
BitmapCol col = row[x];
cc_uint8 R = BitmapCol_R(col);
cc_uint8 G = BitmapCol_G(col);
cc_uint8 B = BitmapCol_B(col);
RGBColor pixelColor;
pixelColor.red = R << 8;
pixelColor.green = G << 8;
pixelColor.blue = B << 8;
RGBForeColor(&pixelColor);
MoveTo(x, y);
Line(0, 0);
//SetCPixel(x, y, &pixelColor);
}
}
}
void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
SetPort(win);
if (useGWorld) {
DrawFramebufferBulk(r, bmp);
} else {
DrawFramebufferSlow(r, bmp);
}
}
void Window_FreeFramebuffer(struct Bitmap* bmp) {
Mem_Free(bmp->scan0);
if (!useGWorld) return;
UnlockPixels(fb_pixmap);
DisposeGWorld(fb_world);
}
void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { }
void OnscreenKeyboard_SetText(const cc_string* text) { }
void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { }
void OnscreenKeyboard_Draw3D(void) { }
void OnscreenKeyboard_Close(void) { }
void Window_EnableRawMouse(void) {
DefaultEnableRawMouse();
}
void Window_UpdateRawMouse(void) {
DefaultUpdateRawMouse();
}
void Window_DisableRawMouse(void) {
DefaultDisableRawMouse();
}
/*########################################################################################################################*
*-------------------------------------------------------WGL OpenGL--------------------------------------------------------*
*#########################################################################################################################*/
#if (CC_GFX_BACKEND & CC_GFX_BACKEND_GL_MASK) && !defined CC_BUILD_EGL
void GLContext_Create(void) {
// TODO
}
void GLContext_Update(void) { }
cc_bool GLContext_TryRestore(void) { return true; }
void GLContext_Free(void) {
// TODO
}
void* GLContext_GetAddress(const char* function) {
return NULL;
}
cc_bool GLContext_SwapBuffers(void) {
// TODO
return true;
}
void GLContext_SetFpsLimit(cc_bool vsync, float minFrameMs) {
// TODO
}
void GLContext_GetApiInfo(cc_string* info) { }
#endif
#endif
|