Block Textures

This commit is contained in:
Michael Chen 2018-01-12 09:47:32 +01:00
parent 47e2c125f5
commit ab98ff12c5
4 changed files with 59 additions and 8 deletions

BIN
bin/assets/images/Texts.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 658 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 KiB

View File

@ -16,17 +16,20 @@
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight; int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
SDL_Texture * BALL_Texture; SDL_Texture * BALL_Texture;
SDL_Texture * PADDLE_Texture;
SDL_Texture * BLOCK_Texture;
SDL_Rect * BALL_SourceRects; SDL_Rect * BALL_SourceRects;
SDL_Rect * PADDLE_SourceRects;
SDL_Rect * BLOCK_SourceRects;
Ball ball; Ball ball;
Paddle paddle; Paddle paddle;
SDL_Texture * PADDLE_Texture;
SDL_Rect * PADDLE_SourceRects;
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys; Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
double BALL_Speed = 15.0f; double BALL_Speed = 15.0f;
int PADDLE_Speed = 10; int PADDLE_Speed = 10;
bool BREAKOUT_IsInit = false; bool BREAKOUT_IsInit = false;
bool BALL_IsInit = false; bool BALL_IsInit = false;
bool PADDLE_IsInit = false; bool PADDLE_IsInit = false;
bool BLOCK_IsInit = false;
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
if (!BREAKOUT_IsInit) { if (!BREAKOUT_IsInit) {
@ -36,6 +39,8 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
BREAKOUT_BoxHeight = height; BREAKOUT_BoxHeight = height;
BALL_Initialize(renderer); BALL_Initialize(renderer);
PADDLE_Initialize(renderer); PADDLE_Initialize(renderer);
ball = BALL_CreateDefault();
paddle = PADDLE_CreateDefault();
printf("Game initialized!\n"); printf("Game initialized!\n");
BREAKOUT_IsInit = true; BREAKOUT_IsInit = true;
} else printf("Game is already initialized!\n"); } else printf("Game is already initialized!\n");
@ -74,8 +79,6 @@ void BALL_Initialize(SDL_Renderer * renderer){
BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect)); BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
if (!BALL_SourceRects) printf("FATAL! Memory allocation failed!\n"); if (!BALL_SourceRects) printf("FATAL! Memory allocation failed!\n");
BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 }; BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
ball = BALL_CreateDefault();
paddle = PADDLE_CreateDefault();
printf("Ball initialized!\n"); printf("Ball initialized!\n");
BALL_IsInit = true; BALL_IsInit = true;
} else printf("Ball is already initialized!\n"); } else printf("Ball is already initialized!\n");
@ -184,7 +187,6 @@ void BALL_Deinitialize(){
} else printf("Ball is already de-initialized!\n"); } else printf("Ball is already de-initialized!\n");
} }
void PADDLE_Initialize(SDL_Renderer * renderer){ void PADDLE_Initialize(SDL_Renderer * renderer){
if (!PADDLE_IsInit) { if (!PADDLE_IsInit) {
printf("Initializing Paddle...\n"); printf("Initializing Paddle...\n");
@ -203,7 +205,6 @@ void PADDLE_Initialize(SDL_Renderer * renderer){
PADDLE_MoveRightKeys[0] = 2; PADDLE_MoveRightKeys[0] = 2;
PADDLE_MoveRightKeys[1] = SDL_SCANCODE_RIGHT; PADDLE_MoveRightKeys[1] = SDL_SCANCODE_RIGHT;
PADDLE_MoveRightKeys[2] = SDL_SCANCODE_D; PADDLE_MoveRightKeys[2] = SDL_SCANCODE_D;
ball = BALL_CreateDefault();
printf("Paddle initialized!\n"); printf("Paddle initialized!\n");
PADDLE_IsInit = true; PADDLE_IsInit = true;
} else printf("Paddle is already initialized!\n"); } else printf("Paddle is already initialized!\n");
@ -252,6 +253,57 @@ void PADDLE_Update(Paddle * obj, Uint8 * keystate){
void PADDLE_DestroyObject(Paddle * obj){ void PADDLE_DestroyObject(Paddle * obj){
} }
void PADDLE_Deinitialize(){ void PADDLE_Deinitialize(){
if (PADDLE_IsInit) {
printf("De-initializing Paddle...\n");
printf("Paddle de-initialized!\n");
PADDLE_IsInit = false;
} else printf("Paddle is already de-initialized!\n");
}
void BLOCK_Initialize(SDL_Renderer * renderer){
if (!BLOCK_IsInit) {
printf("Initializing Block...\n");
BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks.png");
if (!BLOCK_Texture) printf("Blocktexture failed to load!\n");
BLOCK_SourceRects = (SDL_Rect *)malloc(24 * sizeof(SDL_Rect));
if (!BLOCK_SourceRects) printf("FATAL! Memory allocation failed!\n");
for (int i = 0; i < 24; i++) {
BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i, .w = 1000, .h = 500 };
}
printf("Block initialized!\n");
BLOCK_IsInit = true;
} else printf("Block is already initialized!\n");
} /* PADDLE_Initialize */
Block BLOCK_CreateDefault(){
int defaultpaddlewidth = 300;
return (Block) {
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 },
.TextureIndex = 0
}; // Objekt für die Eigenschaften des Balls
}
void BLOCK_Draw(SDL_Renderer * renderer, Paddle * obj){
// printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
// SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect));
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, &(obj->TargetRect));
}
void BLOCK_Update(Paddle * obj, Uint8 * keystate){
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
if (leftKeyPressed && (!rightKeyPressed)) {
((obj->TargetRect).x) -= PADDLE_Speed;
} else if ((!leftKeyPressed) && rightKeyPressed) {
((obj->TargetRect).x) += PADDLE_Speed;
}
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
}
void BLOCK_DestroyObject(Paddle * obj){
}
void BLOCK_Deinitialize(){
if (PADDLE_IsInit) { if (PADDLE_IsInit) {
printf("De-initializing Paddle...\n"); printf("De-initializing Paddle...\n");

View File

@ -23,9 +23,8 @@ typedef struct paddleStruct {
} Paddle; // Objekt für die Eigenschaften des Paddles } Paddle; // Objekt für die Eigenschaften des Paddles
typedef struct blockStruct { typedef struct blockStruct {
Vector Location;
SDL_Rect TargetRect; SDL_Rect TargetRect;
int XSize, YSize, TextureIndex; int TextureIndex;
} Block; // Objekt für die Eigenschaften des Paddles } Block; // Objekt für die Eigenschaften des Paddles
// End Structs // End Structs