MacLO/src/MacLO.c
Jon Thysell 50a753b35e Minor cleanup of resources
Made sure all resources are no longer marked as purgeable so they stay
in memory (not that we were running out), also cleaned up some other
code issues.
2021-12-01 10:08:07 -08:00

258 lines
5.5 KiB
C

// Copyright (c) Jon Thysell <http://jonthysell.com>
// Licensed under the MIT License.
#include "GameWindow.h"
#include "MacLO.h"
#define AppleMenuResID BaseResID
#define AboutMenuItemID 1
#define GameMenuResID BaseResID+1
#define TitleMenuItemID 1
#define ClearMenuItemID 2
#define QuitMenuItemID 4
#define AboutDialogResID BaseResID
#define AboutDialogOKID 1
GameWindow gGameWindow;
Boolean gExitApp;
void MacLO_HandleUpdate(const EventRecord *pEvent);
void MacLO_HandleMouseDown(const EventRecord *pEvent);
void MacLO_HandleMouseUp(const EventRecord *pEvent);
void MacLO_HandleMenuChoice(const int32_t menuChoice);
void MacLO_HandleAppleMenuChoice(const int16_t item);
void MacLO_ShowAboutDialog();
void MacLO_LaunchAppleMenuItem(const int16_t item);
void MacLO_HandleGameMenuChoice(const int16_t item);
void MacLO_Quit();
void MacLO_ToolBoxInit()
{
MaxApplZone();
InitGraf(&thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
FlushEvents(everyEvent, 0);
InitCursor();
}
void MacLO_AppInit()
{
Handle menuBar;
MenuHandle appleMenu;
// Add the menu bar
menuBar = GetNewMBar(BaseResID);
SetMenuBar(menuBar);
// Populate the apple menu
appleMenu = GetMHandle(AppleMenuResID);
AddResMenu(appleMenu, 'DRVR');
DrawMenuBar();
// Setup the game window
GameWindow_Init(&gGameWindow);
GameWindow_Show(&gGameWindow);
}
void MacLO_MainLoop()
{
EventRecord event;
char cmdChar;
while (!gExitApp)
{
if (WaitNextEvent(everyEvent, &event, LONG_MAX, nil))
{
switch (event.what)
{
case updateEvt:
MacLO_HandleUpdate(&event);
break;
case mouseDown:
MacLO_HandleMouseDown(&event);
break;
case mouseUp:
MacLO_HandleMouseUp(&event);
break;
case keyDown:
case autoKey:
// Translate command key combos to menu items
cmdChar = event.message & charCodeMask;
if ((event.modifiers & cmdKey) != 0)
{
MacLO_HandleMenuChoice(MenuKey(cmdChar));
}
break;
}
}
}
}
void MacLO_HandleUpdate(const EventRecord *pEvent)
{
WindowPtr window;
window = (WindowPtr)pEvent->message;
BeginUpdate(window);
if (window == gGameWindow.Window)
{
GameWindow_Draw(&gGameWindow, true);
}
EndUpdate(window);
}
void MacLO_HandleMouseDown(const EventRecord *pEvent)
{
WindowPtr window;
int32_t windowPart;
int32_t menuChoice;
Point mousePosition;
windowPart = FindWindow(pEvent->where, &window);
mousePosition = pEvent->where;
switch (windowPart)
{
case inMenuBar:
menuChoice = MenuSelect(mousePosition);
MacLO_HandleMenuChoice(menuChoice);
case inSysWindow:
SystemClick(pEvent, window);
break;
case inDrag:
DragWindow(window, mousePosition, &((*GetGrayRgn())->rgnBBox));
break;
}
}
void MacLO_HandleMouseUp(const EventRecord *pEvent)
{
WindowPtr window;
int32_t windowPart;
Point mousePosition;
windowPart = FindWindow(pEvent->where, &window);
mousePosition = pEvent->where;
switch (windowPart)
{
case inContent:
GlobalToLocal(&mousePosition);
GameWindow_Click(&gGameWindow, &mousePosition);
break;
}
}
void MacLO_HandleMenuChoice(const int32_t menuChoice)
{
int16_t menu;
int16_t item;
if (menuChoice != 0)
{
menu = HiWord(menuChoice);
item = LoWord(menuChoice);
switch (menu)
{
case AppleMenuResID:
MacLO_HandleAppleMenuChoice(item);
break;
case GameMenuResID:
MacLO_HandleGameMenuChoice(item);
break;
}
HiliteMenu(0);
}
}
void MacLO_HandleAppleMenuChoice(const int16_t item)
{
MenuHandle appleMenu;
Str255 accName;
int16_t accNumber;
switch (item)
{
case AboutMenuItemID:
MacLO_ShowAboutDialog();
break;
default:
MacLO_LaunchAppleMenuItem(item);
break;
}
}
void MacLO_ShowAboutDialog()
{
GrafPtr oldPort;
DialogPtr dialog;
int32_t itemHit;
GetPort(&oldPort);
dialog = GetNewDialog(AboutDialogResID, nil, MoveToFront);
SetPort(dialog);
SetDialogDefaultItem(dialog, AboutDialogOKID);
ShowWindow(dialog);
ModalDialog(nil, &itemHit);
DisposDialog(dialog);
SetPort(oldPort);
}
void MacLO_LaunchAppleMenuItem(const int16_t item)
{
MenuHandle appleMenu;
Str255 accName;
appleMenu = GetMHandle(AppleMenuResID);
GetItem(appleMenu, item, accName);
OpenDeskAcc(accName);
}
void MacLO_HandleGameMenuChoice(const int16_t item)
{
switch (item)
{
case TitleMenuItemID:
GameWindow_SetScene(&gGameWindow, Title);
break;
case ClearMenuItemID:
GameWindow_ClearScores(&gGameWindow);
break;
case QuitMenuItemID:
MacLO_Quit();
break;
}
}
void MacLO_Quit()
{
if (ShowConfirm("\pAre you sure you want to quit MacLO?"))
{
gExitApp = true;
}
}