Backgrounds works now!

This commit is contained in:
Michael Chen 2018-01-18 17:03:37 +01:00
parent dd48975d93
commit 1aaf18b51d
4 changed files with 81 additions and 0 deletions

56
background.c Normal file
View File

@ -0,0 +1,56 @@
#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"
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");
}

22
background.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef __background_h__
#define __background_h__
#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"
// 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 870 KiB

3
main.c
View File

@ -41,6 +41,7 @@ int main(int argc, char * args[]){
switch (gameState) { switch (gameState) {
case Game: case Game:
BREAKOUT_Update(&scenery, keystate); BREAKOUT_Update(&scenery, keystate);
BACKGROUND_Draw(renderer, 0);
BREAKOUT_Draw(&scenery, renderer); BREAKOUT_Draw(&scenery, renderer);
break; break;
case MainMenu: case MainMenu:
@ -172,12 +173,14 @@ void INITIALIZE() {
scenery = BREAKOUT_CreateDefault(); scenery = BREAKOUT_CreateDefault();
Load_Textures(renderer); Load_Textures(renderer);
HIGHSCORES_Initialize(); HIGHSCORES_Initialize();
BACKGROUND_Initialize(renderer, width, height);
Settings_Initialize(renderer); Settings_Initialize(renderer);
printf("Initializing finished!\n"); printf("Initializing finished!\n");
} /* INITIALIZE */ } /* INITIALIZE */
void QUIT(){ void QUIT(){
printf("De-initializing started...\n"); printf("De-initializing started...\n");
BACKGROUND_Initialize();
Settings_Deinitialize(); Settings_Deinitialize();
HIGHSCORES_Deinitialize(); HIGHSCORES_Deinitialize();
BREAKOUT_DestroyObject(&scenery); BREAKOUT_DestroyObject(&scenery);