breakout/background.c

63 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "breakout.h"
#include "vector.h"
#include "background.h"
#define BG_Path_1 "assets/images/bg/bg1.png"
#define BG_Path_2 "assets/images/bg/bg2.png"
#define BG_Path_3 "assets/images/bg/bg3.png"
#define BG_Path_4 "assets/images/bg/bg4.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 = 4;
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);
BACKGROUND_Textures[1] = IMG_LoadTexture(renderer, BG_Path_2);
BACKGROUND_Textures[2] = IMG_LoadTexture(renderer, BG_Path_3);
BACKGROUND_Textures[3] = IMG_LoadTexture(renderer, BG_Path_4);
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_Textures);
printf("Background de-initialized!\n");
BACKGROUND_IsInit = false;
} else
printf("Background already de-initialized!\n");
}