2018-01-09 13:22:10 +01:00
|
|
|
#include <stdio.h>
|
2018-01-10 23:40:17 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2018-01-09 13:22:10 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
|
2018-01-10 15:37:55 +01:00
|
|
|
#include "breakout.h"
|
2018-01-09 13:22:10 +01:00
|
|
|
#include "vector.h"
|
|
|
|
|
|
|
|
#ifndef __nullptr__
|
|
|
|
#define Nullptr(type) (type *)0
|
|
|
|
#endif // __nullptr__
|
|
|
|
|
2018-01-10 15:37:55 +01:00
|
|
|
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
|
|
|
|
SDL_Texture * BALL_Texture;
|
2018-01-10 23:40:17 +01:00
|
|
|
SDL_Rect * BALL_SourceRects;
|
|
|
|
Ball ball;
|
2018-01-09 13:22:10 +01:00
|
|
|
|
2018-01-10 15:37:55 +01:00
|
|
|
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
|
|
|
printf("Initializing Game...\n");
|
2018-01-10 23:40:17 +01:00
|
|
|
srand(time(NULL));
|
2018-01-10 15:37:55 +01:00
|
|
|
BREAKOUT_BoxWidth = width;
|
|
|
|
BREAKOUT_BoxHeight = height;
|
|
|
|
if (!BALL_Texture) printf("Ball texture cannot be loaded!\n");
|
|
|
|
printf("Game initialized!\n");
|
|
|
|
}
|
|
|
|
|
2018-01-10 23:40:17 +01:00
|
|
|
void BREAKOUT_Update(Uint8 * keystate){
|
|
|
|
BALL_Update(&ball);
|
2018-01-10 15:37:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 23:40:17 +01:00
|
|
|
void BREAKOUT_Draw(SDL_Renderer * renderer){
|
|
|
|
BALL_Draw(renderer, &ball);
|
2018-01-10 15:37:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 23:40:17 +01:00
|
|
|
void BREAKOUT_DEINITIALIZE(){
|
2018-01-10 15:37:55 +01:00
|
|
|
printf("De-initializing Game...\n");
|
|
|
|
SDL_DestroyTexture(BALL_Texture);
|
|
|
|
printf("Game de-initialized!\n");
|
2018-01-09 13:22:10 +01:00
|
|
|
}
|
2018-01-10 23:40:17 +01:00
|
|
|
|
|
|
|
void BALL_Initialize(SDL_Renderer * renderer){
|
|
|
|
printf("Initializing Ball...\n");
|
|
|
|
BALL_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
|
|
|
|
if (!BALL_Texture) printf("Ball texture failed to load!\n");
|
|
|
|
BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
|
|
|
|
BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
|
|
|
|
ball = BALL_CreateDefault();
|
|
|
|
printf("Ball initialized!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ball BALL_CreateDefault(){
|
|
|
|
double rotation = (double)(rand() % 360);
|
|
|
|
|
|
|
|
return (Ball) {
|
|
|
|
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 },
|
|
|
|
.Momentum = getScaledDirectionalUnitVector(rotation, 5),
|
|
|
|
.TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2, .w = 50, .h = 50 },
|
|
|
|
.Size = 50.0f,
|
|
|
|
.Rotation = rotation,
|
|
|
|
.RotationValue = 2,
|
|
|
|
.TextureIndex = 0
|
|
|
|
}; // Objekt für die Eigenschaften des Balls
|
|
|
|
}
|
|
|
|
|
|
|
|
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
|
|
|
|
SDL_RenderCopyEx(renderer, BALL_Texture, BALL_SourceRects + (obj->TextureIndex), &(obj->TargetRect), obj->Rotation, NULL, SDL_FLIP_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BALL_Update(Ball * obj){
|
|
|
|
obj->Location = vectorAdd(obj->Location, obj->Momentum);
|
|
|
|
(obj->TargetRect).x = (int)round((obj->Location).x);
|
|
|
|
(obj->TargetRect).y = (int)round((obj->Location).y);
|
|
|
|
}
|
|
|
|
void BALL_DestroyObject(Ball * obj){
|
|
|
|
}
|
|
|
|
void BALL_Deinitialize(){
|
|
|
|
printf("De-initializing Ball...\n");
|
|
|
|
|
|
|
|
printf("Ball de-initialized!\n");
|
|
|
|
}
|