diff --git a/src/GameWindow.c b/src/GameWindow.c index b671aa5..d59714d 100644 --- a/src/GameWindow.c +++ b/src/GameWindow.c @@ -27,8 +27,12 @@ void GameWindow_Init(GameWindow *pGameWindow) // Load snd resources Sounds_Init(&(pGameWindow->Sounds)); - // Set port for first draw + // Initialize window buffer + WindowBuffer_Init(&(pGameWindow->WindowBuffer), pGameWindow->Window); + + // Setup graphics before first draw SetPort(pGameWindow->Window); + FillRect(&(pGameWindow->Window->portRect), WindowPattern); GameWindow_SetScene(pGameWindow, Title); } @@ -38,9 +42,10 @@ void GameWindow_Draw(const GameWindow *pGameWindow, bool fullRefresh) GrafPtr oldPort; const Rect *pContentRect = &(pGameWindow->Window->portRect); + // Save the current port GetPort(&oldPort); - SetPort(pGameWindow->Window); + WindowBuffer_StartDraw(&(pGameWindow->WindowBuffer)); if (fullRefresh) { @@ -64,6 +69,8 @@ void GameWindow_Draw(const GameWindow *pGameWindow, bool fullRefresh) break; } + WindowBuffer_EndDraw(&(pGameWindow->WindowBuffer)); + SetPort(oldPort); } diff --git a/src/GameWindow.h b/src/GameWindow.h index 367e8c2..8248fcd 100644 --- a/src/GameWindow.h +++ b/src/GameWindow.h @@ -5,6 +5,7 @@ #define GAMEWINDOW_H #include "MacCommon.h" +#include "WindowBuffer.h" #include "GameEngine.h" #include "Bitmaps.h" #include "Sounds.h" @@ -15,6 +16,7 @@ typedef struct sGameWindow { WindowPtr Window; + WindowBuffer WindowBuffer; GameEngine Engine; Bitmaps Bitmaps; Sounds Sounds; diff --git a/src/LevelEndScene.c b/src/LevelEndScene.c index c28721d..48b40f2 100644 --- a/src/LevelEndScene.c +++ b/src/LevelEndScene.c @@ -85,13 +85,12 @@ void LevelEndScene_Click(GameWindow *pGameWindow, const Point *pPosition) { if (PtInRect(*pPosition, &(pGameWindow->LevelEndScene.RetryButtonRect))) { - Sounds_PlayRetrySnd(&(pGameWindow->Sounds)); GameEngine_ResetLevel(&(pGameWindow->Engine)); GameWindow_SetScene(pGameWindow, Play); + Sounds_PlayRetrySnd(&(pGameWindow->Sounds)); } else if (PtInRect(*pPosition, &(pGameWindow->LevelEndScene.NextButtonRect))) { - Sounds_PlayClickSnd(&(pGameWindow->Sounds)); GameEngine_NextLevel(&(pGameWindow->Engine)); if (GameEngine_IsGameOver(&(pGameWindow->Engine))) diff --git a/src/MacLO.pi.bin b/src/MacLO.pi.bin index 208f15f..5b06658 100644 Binary files a/src/MacLO.pi.bin and b/src/MacLO.pi.bin differ diff --git a/src/MacLO.pi.rsrc.bin b/src/MacLO.pi.rsrc.bin index 6210454..f1e7fef 100644 Binary files a/src/MacLO.pi.rsrc.bin and b/src/MacLO.pi.rsrc.bin differ diff --git a/src/PlayScene.c b/src/PlayScene.c index ce8d252..69c42e4 100644 --- a/src/PlayScene.c +++ b/src/PlayScene.c @@ -161,9 +161,9 @@ void PlayScene_Click(GameWindow *pGameWindow, const Point *pPosition) if (PtInRect(*pPosition, &lightRect)) { - Sounds_PlayClickSnd(&(pGameWindow->Sounds)); GameEngine_ToggleLights(&(pGameWindow->Engine), c, r); GameWindow_Draw(pGameWindow, false); + Sounds_PlayClickSnd(&(pGameWindow->Sounds)); break; } } @@ -183,9 +183,9 @@ void PlayScene_Click(GameWindow *pGameWindow, const Point *pPosition) if (PtInRect(*pPosition, &(pGameWindow->PlayScene.RetryButtonRect))) { - Sounds_PlayRetrySnd(&(pGameWindow->Sounds)); GameEngine_ResetLevel(&(pGameWindow->Engine)); GameWindow_Draw(pGameWindow, false); + Sounds_PlayRetrySnd(&(pGameWindow->Sounds)); } else if (PtInRect(*pPosition, &(pGameWindow->PlayScene.SoundButtonRect))) { diff --git a/src/TitleScene.c b/src/TitleScene.c index ee3ebc9..ecc2e22 100644 --- a/src/TitleScene.c +++ b/src/TitleScene.c @@ -54,13 +54,11 @@ void TitleScene_Click(GameWindow *pGameWindow, const Point *pPosition) { if (PtInRect(*pPosition, &(pGameWindow->TitleScene.SetARect))) { - Sounds_PlayClickSnd(&(pGameWindow->Sounds)); GameEngine_NewGame(&(pGameWindow->Engine), false); GameWindow_SetScene(pGameWindow, Play); } else if (PtInRect(*pPosition, &(pGameWindow->TitleScene.SetBRect))) { - Sounds_PlayClickSnd(&(pGameWindow->Sounds)); GameEngine_NewGame(&(pGameWindow->Engine), true); GameWindow_SetScene(pGameWindow, Play); } diff --git a/src/WindowBuffer.c b/src/WindowBuffer.c new file mode 100644 index 0000000..477a3b2 --- /dev/null +++ b/src/WindowBuffer.c @@ -0,0 +1,56 @@ +// Copyright (c) Jon Thysell +// Licensed under the MIT License. + +#include "WindowBuffer.h" +#include "MacCommon.h" + +void WindowBuffer_Init(WindowBuffer *pWindowBuffer, const WindowPtr window) +{ + int16_t width, height; + BitMap newBits; + + pWindowBuffer->Window = window; + + // Create a BitMap as the backing for the buffer, + // since System 6 doesn't have fancy GWorlds + width = window->portRect.right - window->portRect.left; + height = window->portRect.bottom - window->portRect.top; + SetRect(&newBits.bounds, 0, 0, width, height); + newBits.rowBytes = ((width + 31)/32) * 4; + newBits.baseAddr = NewPtr(height * newBits.rowBytes); + + // Clear the bitmap + CopyBits(&newBits, + &newBits, + &newBits.bounds, + &newBits.bounds, + srcXor, + nil); + + // Create the buffer and get it ready + pWindowBuffer->Buffer = (GrafPtr)NewPtr(sizeof(GrafPort)); + OpenPort(pWindowBuffer->Buffer); + SetPort(pWindowBuffer->Buffer); + + pWindowBuffer->Buffer->portRect = newBits.bounds; + RectRgn(pWindowBuffer->Buffer->visRgn, &newBits.bounds); + SetPortBits(&newBits); +} + +void WindowBuffer_StartDraw(const WindowBuffer *pWindowBuffer) +{ + // Set the buffer as the port for future QuickDraw commands + SetPort(pWindowBuffer->Buffer); +} + +void WindowBuffer_EndDraw(const WindowBuffer *pWindowBuffer) +{ + SetPort(pWindowBuffer->Window); + + // Copy the buffer to the window + CopyBits(&(pWindowBuffer->Buffer->portBits), + &(pWindowBuffer->Window->portBits), + &(pWindowBuffer->Buffer->portRect), + &(pWindowBuffer->Window->portRect), + srcCopy, nil); +} diff --git a/src/WindowBuffer.h b/src/WindowBuffer.h new file mode 100644 index 0000000..7350572 --- /dev/null +++ b/src/WindowBuffer.h @@ -0,0 +1,17 @@ +// Copyright (c) Jon Thysell +// Licensed under the MIT License. + +#ifndef WINDOWBUFFER_H +#define WINDOWBUFFER_H + +typedef struct sWindowBuffer +{ + WindowPtr Window; + GrafPtr Buffer; +} WindowBuffer; + +void WindowBuffer_Init(WindowBuffer *pWindowBuffer, const WindowPtr window); +void WindowBuffer_StartDraw(const WindowBuffer *pWindowBuffer); +void WindowBuffer_EndDraw(const WindowBuffer *pWindowBuffer); + +#endif \ No newline at end of file