From d3b5a77eaad738fb11523f428640d750e3c7d826 Mon Sep 17 00:00:00 2001 From: Jon Thysell Date: Sun, 7 Nov 2021 15:35:44 -0800 Subject: [PATCH] Updated TitleScene and PlayScene with new graphic chars TitleScene now shows A and B "buttons" to start a game. PlayScene now shows just the level name in the HUD. Lots of new helpers added to scale and align rects where I want them. --- src/Bitmaps.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++-- src/Bitmaps.h | 11 +++++- src/MacCommon.c | 27 +++++++++++--- src/MacCommon.h | 6 ++- src/PlayScene.c | 63 +++++++++++++------------------ src/Scenes.h | 5 ++- src/TitleScene.c | 46 +++++++++++++---------- 7 files changed, 185 insertions(+), 70 deletions(-) diff --git a/src/Bitmaps.c b/src/Bitmaps.c index 64308bb..c1f8256 100644 --- a/src/Bitmaps.c +++ b/src/Bitmaps.c @@ -3,14 +3,14 @@ #include "Bitmaps.h" -#define NumChars 10 - #define TitlePictResID BaseResID #define NumCharPictBaseResID (TitlePictResID + 1) #define ACharPictResID (NumCharPictBaseResID + NumChars) #define BCharPictResID (ACharPictResID + 1) #define SlashCharPictResID (BCharPictResID + 1) +void Bitmaps_DrawScaledPic(const PicHandle pic, const uint8_t scale); + void Bitmaps_Init(Bitmaps *pBitmaps) { int16_t i; @@ -52,4 +52,95 @@ void Bitmaps_Init(Bitmaps *pBitmaps) { ShowError("\pSlash char PICT resource missing!", true); } -} \ No newline at end of file +} + +void Bitmaps_DrawScaledPic(const PicHandle pic, const uint8_t scale) +{ + Point penPosition; + Rect destRect; + + GetPen(&penPosition); + GetScaledPicFrame(pic, scale, &destRect); + + OffsetRect(&destRect, penPosition.h, penPosition.v); + + DrawPicture(pic, &destRect); + MoveTo(destRect.right, destRect.top); +} + +void Bitmaps_GetNumberRect(const Bitmaps *pBitmaps, const uint32_t number, const uint8_t scale, Rect *pDestRect) +{ + bool started; + uint32_t k, digit, remainder; + Rect digitRect; + + pDestRect->top = 0; + pDestRect->left = 0; + pDestRect->bottom = 0; + pDestRect->right = 0; + + if (number == 0) + { + GetScaledPicFrame(pBitmaps->NumCharPicts[0], scale, &digitRect); + ConcatenateRect(pDestRect, &digitRect, pDestRect); + return; + } + + started = false; + remainder = number; + + for (k = 1000000000UL; k > 0; k = k / 10) + { + digit = remainder / k; + remainder = remainder % k; + + if (started || (digit > 0 && digit < 10)) + { + GetScaledPicFrame(pBitmaps->NumCharPicts[digit], scale, &digitRect); + ConcatenateRect(pDestRect, &digitRect, pDestRect); + started = true; + } + } +} + +void Bitmaps_DrawNumber(const Bitmaps *pBitmaps, const uint32_t number, const uint8_t scale) +{ + bool started; + uint32_t k, digit, remainder; + + if (number == 0) + { + Bitmaps_DrawScaledPic(pBitmaps->NumCharPicts[0], scale); + return; + } + + started = false; + remainder = number; + + for (k = 1000000000UL; k > 0; k = k / 10) + { + digit = remainder / k; + remainder = remainder % k; + + if (started || (digit > 0 && digit < 10)) + { + Bitmaps_DrawScaledPic(pBitmaps->NumCharPicts[digit], scale); + started = true; + } + } +} + +void Bitmaps_DrawAChar(const Bitmaps *pBitmaps, const uint8_t scale) +{ + Bitmaps_DrawScaledPic(pBitmaps->ACharPict, scale); +} + +void Bitmaps_DrawBChar(const Bitmaps *pBitmaps, const uint8_t scale) +{ + Bitmaps_DrawScaledPic(pBitmaps->BCharPict, scale); +} + +void Bitmaps_DrawSlashChar(const Bitmaps *pBitmaps, const uint8_t scale) +{ + Bitmaps_DrawScaledPic(pBitmaps->SlashCharPict, scale); +} diff --git a/src/Bitmaps.h b/src/Bitmaps.h index 1d8b2e6..bb8e0e5 100644 --- a/src/Bitmaps.h +++ b/src/Bitmaps.h @@ -6,10 +6,12 @@ #include "MacCommon.h" +#define NumChars 10 + typedef struct sBitmaps { PicHandle TitlePict; - PicHandle NumCharPicts[10]; + PicHandle NumCharPicts[NumChars]; PicHandle ACharPict; PicHandle BCharPict; PicHandle SlashCharPict; @@ -17,4 +19,11 @@ typedef struct sBitmaps void Bitmaps_Init(Bitmaps *pBitmaps); +void Bitmaps_GetNumberRect(const Bitmaps *pBitmaps, const uint32_t number, const uint8_t scale, Rect *pDestRect); +void Bitmaps_DrawNumber(const Bitmaps *pBitmaps, const uint32_t number, const uint8_t scale); + +void Bitmaps_DrawAChar(const Bitmaps *pBitmaps, const uint8_t scale); +void Bitmaps_DrawBChar(const Bitmaps *pBitmaps, const uint8_t scale); +void Bitmaps_DrawSlashChar(const Bitmaps *pBitmaps, const uint8_t scale); + #endif diff --git a/src/MacCommon.c b/src/MacCommon.c index 892da58..c0f7c95 100644 --- a/src/MacCommon.c +++ b/src/MacCommon.c @@ -3,11 +3,6 @@ #include "MacCommon.h" -int32_t GetThemeID() -{ - return 0; -} - void ShowError(Str255 message, bool isFatal) { ParamText(message, EmptyString, EmptyString, EmptyString); @@ -37,6 +32,18 @@ void CenterRectV(const Rect *pOuterRect, Rect *pInnerRect) OffsetRect(pInnerRect, 0, (pOuterRect->bottom - pInnerRect->bottom) / 2); } +void ConcatenateRect(const Rect *pLeftRect, const Rect *pRightRect, Rect *pDestRect) +{ + Rect newRightRect; + + newRightRect.top = pLeftRect->top; + newRightRect.left = pLeftRect->right; + newRightRect.bottom = newRightRect.top + (pRightRect->bottom - pRightRect->top); + newRightRect.right = newRightRect.left + (pRightRect->right - pRightRect->left); + + UnionRect(pLeftRect, &newRightRect, pDestRect); +} + void GetBoxRect(const Rect *pOuterRect, const BoxAlignment boxAlignment, Rect *pBoxRect) { int32_t boxWidth, boxHeight; @@ -101,4 +108,12 @@ void GetBoxRect(const Rect *pOuterRect, const BoxAlignment boxAlignment, Rect *p pBoxRect->right = pOuterRect->right - boxWidth; break; } -} \ No newline at end of file +} + +void GetScaledPicFrame(const PicHandle picHandle, const uint8_t scale, Rect *pDestRect) +{ + *pDestRect = (**(picHandle)).picFrame; + + pDestRect->right = pDestRect->left + ((pDestRect->right - pDestRect->left) * max(scale, 1)); + pDestRect->bottom = pDestRect->top + ((pDestRect->bottom - pDestRect->top) * max(scale, 1)); +} diff --git a/src/MacCommon.h b/src/MacCommon.h index 861d7b9..58c5a4c 100644 --- a/src/MacCommon.h +++ b/src/MacCommon.h @@ -29,14 +29,16 @@ typedef enum eBoxAlignment pascal OSErr SetDialogDefaultItem(DialogPtr theDialog, int16_t newItem) = { 0x303C, 0x0304, 0xAA68 }; -int32_t GetThemeID(); - void ShowError(Str255 message, bool isFatal); void CenterRect(const Rect *pOuterRect, Rect *pInnerRect); void CenterRectH(const Rect *pOuterRect, Rect *pInnerRect); void CenterRectV(const Rect *pOuterRect, Rect *pInnerRect); +void ConcatenateRect(const Rect *pLeftRect, const Rect *pRightRect, Rect *pDestRect); + void GetBoxRect(const Rect *pOuterRect, const BoxAlignment boxAlignment, Rect *pBoxRect); +void GetScaledPicFrame(const PicHandle picHandle, const uint8_t scale, Rect *pDestRect); + #endif diff --git a/src/PlayScene.c b/src/PlayScene.c index d7d2d22..679db09 100644 --- a/src/PlayScene.c +++ b/src/PlayScene.c @@ -15,18 +15,32 @@ #define HUDCornerSize PlayfieldCornerSize #define HUDPattern PlayfieldPattern +#define LevelTextScale 2 + void PlayScene_Init(GameWindow *pGameWindow) { - // Setup rects + Rect r1, r2; + + // Setup Playfield pGameWindow->PlayScene.PlayfieldRect.top = PlayfieldMargin; pGameWindow->PlayScene.PlayfieldRect.bottom = pGameWindow->Window->portRect.bottom - PlayfieldMargin; pGameWindow->PlayScene.PlayfieldRect.left = pGameWindow->PlayScene.PlayfieldRect.top; pGameWindow->PlayScene.PlayfieldRect.right = pGameWindow->PlayScene.PlayfieldRect.bottom; + // Setup HUD pGameWindow->PlayScene.HUDRect.top = HUDMargin; pGameWindow->PlayScene.HUDRect.bottom = pGameWindow->Window->portRect.bottom - HUDMargin; pGameWindow->PlayScene.HUDRect.left = pGameWindow->PlayScene.PlayfieldRect.right + HUDMargin; pGameWindow->PlayScene.HUDRect.right = pGameWindow->Window->portRect.right - HUDMargin; + + // Setup Level + GetScaledPicFrame(pGameWindow->Engine.SetB ? pGameWindow->Bitmaps.BCharPict : pGameWindow->Bitmaps.ACharPict, LevelTextScale, &r1); + Bitmaps_GetNumberRect(&(pGameWindow->Bitmaps), 1 + pGameWindow->Engine.Level, LevelTextScale, &r2); + ConcatenateRect(&r1, &r2, &(pGameWindow->PlayScene.LevelRect)); + + GetBoxRect(&(pGameWindow->PlayScene.HUDRect), Top, &r1); + GetBoxRect(&r1, Bottom, &r2); + CenterRect(&r2, &(pGameWindow->PlayScene.LevelRect)); } void PlayScene_SetLightRect(const GameWindow *pGameWindow, Rect *pRect, const int8_t c, const int8_t r) @@ -41,13 +55,12 @@ void PlayScene_Draw(const GameWindow *pGameWindow, bool fullRefresh) { int8_t r, c; Rect lightRect; - Str255 levelStr, parStr, movesStr, halfStarsStr, scoreStr; if (fullRefresh) { // Fill backgrounds FillRoundRect(&(pGameWindow->PlayScene.PlayfieldRect), PlayfieldCornerSize, PlayfieldCornerSize, PlayfieldPattern); - FillRoundRect(&(pGameWindow->PlayScene.HUDRect), HUDCornerSize, HUDCornerSize, HUDPattern); + //FillRoundRect(&(pGameWindow->PlayScene.HUDRect), HUDCornerSize, HUDCornerSize, HUDPattern); } // Draw Playfield @@ -74,43 +87,19 @@ void PlayScene_Draw(const GameWindow *pGameWindow, bool fullRefresh) // Draw HUD - ForeColor(blackColor); - TextFace(bold + outline); - // Draw Level - MoveTo(pGameWindow->PlayScene.HUDRect.left + 10, pGameWindow->PlayScene.HUDRect.top + 20); - DrawString("\pLevel: "); - NumToString(1L + pGameWindow->Engine.Level, &levelStr); - DrawString(levelStr); - DrawString("\p/50"); - - // Draw Par - MoveTo(pGameWindow->PlayScene.HUDRect.left + 10, pGameWindow->PlayScene.HUDRect.top + 40); - DrawString("\pPar: "); - NumToString((int32_t)(pGameWindow->Engine.Par), &parStr); - DrawString(parStr); - - // Draw Moves - MoveTo(pGameWindow->PlayScene.HUDRect.left + 10, pGameWindow->PlayScene.HUDRect.top + 60); - DrawString("\pMoves: "); - NumToString((int32_t)(pGameWindow->Engine.Moves), &movesStr); - DrawString(movesStr); - DrawString("\p/"); - DrawString(parStr); + MoveTo(pGameWindow->PlayScene.LevelRect.left, pGameWindow->PlayScene.LevelRect.top); + if (pGameWindow->Engine.SetB) + { + Bitmaps_DrawBChar(&(pGameWindow->Bitmaps), LevelTextScale); + } + else + { + Bitmaps_DrawAChar(&(pGameWindow->Bitmaps), LevelTextScale); + } + Bitmaps_DrawNumber(&(pGameWindow->Bitmaps), 1 + pGameWindow->Engine.Level, LevelTextScale); // Draw Stars - MoveTo(pGameWindow->PlayScene.HUDRect.left + 10, pGameWindow->PlayScene.HUDRect.top + 80); - DrawString("\pStars: "); - NumToString((int32_t)GameEngine_GetHalfStars(&(pGameWindow->Engine)), &halfStarsStr); - DrawString(halfStarsStr); - DrawString("\p/6"); - - // Draw Score - MoveTo(pGameWindow->PlayScene.HUDRect.left + 10, pGameWindow->PlayScene.HUDRect.top + 100); - DrawString("\pScore: "); - NumToString((int32_t)(pGameWindow->Engine.Score), &scoreStr); - DrawString(scoreStr); - DrawString("\p/300"); } void PlayScene_Click(GameWindow *pGameWindow, const Point *pPosition) diff --git a/src/Scenes.h b/src/Scenes.h index 2293bff..9d05482 100644 --- a/src/Scenes.h +++ b/src/Scenes.h @@ -16,13 +16,16 @@ typedef enum eSceneId typedef struct sTitleScene { - Rect TitleRect; + Rect TitleRect; + Rect SetARect; + Rect SetBRect; } TitleScene; typedef struct sPlayScene { Rect PlayfieldRect; Rect HUDRect; + Rect LevelRect; } PlayScene; typedef struct sLevelEndScene diff --git a/src/TitleScene.c b/src/TitleScene.c index e0a50aa..8437d2b 100644 --- a/src/TitleScene.c +++ b/src/TitleScene.c @@ -4,13 +4,25 @@ #include "TitleScene.h" #include "Bitmaps.h" +#define TitleTextScale 2 + void TitleScene_Init(GameWindow *pGameWindow) { + Rect r; + const Rect *pContentRect = &(pGameWindow->Window->portRect); // Setup rects - pGameWindow->TitleScene.TitleRect = (**(pGameWindow->Bitmaps.TitlePict)).picFrame; + GetScaledPicFrame(pGameWindow->Bitmaps.TitlePict, 1, &(pGameWindow->TitleScene.TitleRect)); CenterRect(pContentRect, &(pGameWindow->TitleScene.TitleRect)); + + GetBoxRect(pContentRect, BottomLeft, &r); + GetScaledPicFrame(pGameWindow->Bitmaps.ACharPict, TitleTextScale, &(pGameWindow->TitleScene.SetARect)); + CenterRect(&r, &(pGameWindow->TitleScene.SetARect)); + + GetBoxRect(pContentRect, BottomRight, &r); + GetScaledPicFrame(pGameWindow->Bitmaps.BCharPict, TitleTextScale, &(pGameWindow->TitleScene.SetBRect)); + CenterRect(&r, &(pGameWindow->TitleScene.SetBRect)); } void TitleScene_Draw(const GameWindow *pGameWindow, bool fullRefresh) @@ -20,34 +32,28 @@ void TitleScene_Draw(const GameWindow *pGameWindow, bool fullRefresh) { } - // Draw Title PICT + // Draw Title DrawPicture(pGameWindow->Bitmaps.TitlePict, &(pGameWindow->TitleScene.TitleRect)); - TextFace(bold + outline); + // Draw Set A + MoveTo(pGameWindow->TitleScene.SetARect.left, pGameWindow->TitleScene.SetARect.top); + Bitmaps_DrawAChar(&(pGameWindow->Bitmaps), TitleTextScale); - MoveTo(100, pGameWindow->TitleScene.TitleRect.bottom + 30); - DrawString("\pSet A"); - - MoveTo(350, pGameWindow->TitleScene.TitleRect.bottom + 30); - DrawString("\pSet B"); + // Draw Set B + MoveTo(pGameWindow->TitleScene.SetBRect.left, pGameWindow->TitleScene.SetBRect.top); + Bitmaps_DrawBChar(&(pGameWindow->Bitmaps), TitleTextScale); } void TitleScene_Click(GameWindow *pGameWindow, const Point *pPosition) { - bool setB; - - // TODO: Proper click handling - - if (pPosition->h < ((pGameWindow->Window->portRect.right - pGameWindow->Window->portRect.left) / 2)) + if (PtInRect(*pPosition, &(pGameWindow->TitleScene.SetARect))) { - setB = false; + GameEngine_NewGame(&(pGameWindow->Engine), false); + GameWindow_SetScene(pGameWindow, Play); } - else + else if (PtInRect(*pPosition, &(pGameWindow->TitleScene.SetBRect))) { - setB = true; + GameEngine_NewGame(&(pGameWindow->Engine), true); + GameWindow_SetScene(pGameWindow, Play); } - - GameEngine_NewGame(&(pGameWindow->Engine), setB); - - GameWindow_SetScene(pGameWindow, Play); }