diff --git a/background.c b/background.c new file mode 100644 index 0000000..cad10f4 --- /dev/null +++ b/background.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "breakout.h" +#include "vector.h" +#include "background.h" + +#define BG_Path_1 "assets/images/bg/bg1.png" + +int BACKGROUND_BoxWidth, BACKGROUND_BoxHeight; +int BACKGROUND_TextureCount; +SDL_Texture ** BACKGROUND_Textures; +bool BACKGROUND_IsInit = false; +SDL_Rect BACKGROUND_TotalRect; + +void BACKGROUND_Initialize(SDL_Renderer * renderer, int width, int height){ + if (!BACKGROUND_IsInit) { + printf("Initializing Background...\n"); + BACKGROUND_BoxWidth = width; + BACKGROUND_BoxHeight = height; + BACKGROUND_TextureCount = 1; + BACKGROUND_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 }; + BACKGROUND_Textures = malloc(BACKGROUND_TextureCount * sizeof(SDL_Texture *)); + BACKGROUND_Textures[0] = IMG_LoadTexture(renderer, BG_Path_1); + printf("Background initialized!\n"); + BACKGROUND_IsInit = true; + } else + printf("Background already initialized!\n"); +} + +void BACKGROUND_Draw(SDL_Renderer * renderer, int index){ + if (index < 0 || index >= BACKGROUND_TextureCount) { + printf("Bad Background Texture index: %d by max: %d\n", index, BACKGROUND_TextureCount); + return; + } + SDL_RenderCopy(renderer, BACKGROUND_Textures[index], &BACKGROUND_TotalRect, &BACKGROUND_TotalRect); +} + +void BACKGROUND_Deinitialize(){ + if (BACKGROUND_IsInit) { + printf("De-initializing Background...\n"); + for (int i = 0; i < BACKGROUND_TextureCount; i++) { + SDL_DestroyTexture(BACKGROUND_Textures[0]); + } + free(BACKGROUND_TextureCount); + printf("Background de-initialized!\n"); + BACKGROUND_IsInit = false; + } else + printf("Background already de-initialized!\n"); +} diff --git a/background.h b/background.h new file mode 100644 index 0000000..32e739c --- /dev/null +++ b/background.h @@ -0,0 +1,22 @@ +#ifndef __background_h__ +#define __background_h__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "breakout.h" +#include "vector.h" + +// Prototypes +void BACKGROUND_Initialize(SDL_Renderer * renderer, int width, int height); +void BACKGROUND_Draw(SDL_Renderer * renderer, int index); +void BACKGROUND_Deinitialize(); +// End Prototypes + +#endif diff --git a/bin/assets/images/bg/bg1.png b/bin/assets/images/bg/bg1.png new file mode 100644 index 0000000..32c489c Binary files /dev/null and b/bin/assets/images/bg/bg1.png differ diff --git a/main.c b/main.c index 48da5fc..2a17052 100644 --- a/main.c +++ b/main.c @@ -41,6 +41,7 @@ int main(int argc, char * args[]){ switch (gameState) { case Game: BREAKOUT_Update(&scenery, keystate); + BACKGROUND_Draw(renderer, 0); BREAKOUT_Draw(&scenery, renderer); break; case MainMenu: @@ -172,12 +173,14 @@ void INITIALIZE() { scenery = BREAKOUT_CreateDefault(); Load_Textures(renderer); HIGHSCORES_Initialize(); + BACKGROUND_Initialize(renderer, width, height); Settings_Initialize(renderer); printf("Initializing finished!\n"); } /* INITIALIZE */ void QUIT(){ printf("De-initializing started...\n"); + BACKGROUND_Initialize(); Settings_Deinitialize(); HIGHSCORES_Deinitialize(); BREAKOUT_DestroyObject(&scenery);