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-11 01:06:55 +01:00
|
|
|
Paddle paddle;
|
|
|
|
SDL_Texture * PADDLE_Texture;
|
|
|
|
SDL_Rect * PADDLE_SourceRects;
|
|
|
|
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
2018-01-11 14:12:30 +01:00
|
|
|
double BALL_Speed = 10.0f;
|
|
|
|
int PADDLE_Speed = 10;
|
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;
|
2018-01-10 23:56:47 +01:00
|
|
|
BALL_Initialize(renderer);
|
2018-01-11 01:06:55 +01:00
|
|
|
PADDLE_Initialize(renderer);
|
2018-01-10 15:37:55 +01:00
|
|
|
printf("Game initialized!\n");
|
|
|
|
}
|
|
|
|
|
2018-01-10 23:40:17 +01:00
|
|
|
void BREAKOUT_Update(Uint8 * keystate){
|
2018-01-11 14:12:30 +01:00
|
|
|
BALL_Update(&ball, &paddle);
|
2018-01-11 01:06:55 +01:00
|
|
|
PADDLE_Update(&paddle, keystate);
|
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-11 01:06:55 +01:00
|
|
|
PADDLE_Draw(renderer, &paddle);
|
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));
|
2018-01-11 01:06:55 +01:00
|
|
|
if (!BALL_SourceRects) printf("FATAL! Memory allocation failed!\n");
|
2018-01-10 23:40:17 +01:00
|
|
|
BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
|
|
|
|
ball = BALL_CreateDefault();
|
2018-01-11 01:06:55 +01:00
|
|
|
paddle = PADDLE_CreateDefault();
|
2018-01-10 23:40:17 +01:00
|
|
|
printf("Ball initialized!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ball BALL_CreateDefault(){
|
|
|
|
double rotation = (double)(rand() % 360);
|
|
|
|
|
|
|
|
return (Ball) {
|
|
|
|
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 },
|
2018-01-10 23:56:47 +01:00
|
|
|
.Momentum = (Vector) {.x = 0.0f, .y = 6.0f },
|
2018-01-11 01:06:55 +01:00
|
|
|
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 },
|
|
|
|
.Size = 25.0f,
|
2018-01-10 23:40:17 +01:00
|
|
|
.Rotation = rotation,
|
|
|
|
.RotationValue = 2,
|
|
|
|
.TextureIndex = 0
|
|
|
|
}; // Objekt für die Eigenschaften des Balls
|
|
|
|
}
|
|
|
|
|
|
|
|
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
|
2018-01-11 01:06:55 +01:00
|
|
|
// printf("Ball drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
|
2018-01-10 23:40:17 +01:00
|
|
|
SDL_RenderCopyEx(renderer, BALL_Texture, BALL_SourceRects + (obj->TextureIndex), &(obj->TargetRect), obj->Rotation, NULL, SDL_FLIP_NONE);
|
|
|
|
}
|
|
|
|
|
2018-01-11 14:12:30 +01:00
|
|
|
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
|
|
|
|
SDL_Point ballCenter = (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) };
|
|
|
|
|
|
|
|
// THIS IS CURRENTLY A RECTANGLE COLLIDE
|
|
|
|
if (((obj->TargetRect).x) + ((obj->TargetRect).w) < (rect->x)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (((obj->TargetRect).x) > (rect->x) + (rect->w)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (((obj->TargetRect).y) + ((obj->TargetRect).w) < (rect->y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (((obj->TargetRect).y) > (rect->y) + (rect->h)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt
|
|
|
|
if ((ballCenter.x) < (rect->x) || (ballCenter.x) > (rect->x) + (rect->w)) {
|
|
|
|
(obj->Momentum).x = -(obj->Momentum).x;
|
|
|
|
}
|
|
|
|
if ((ballCenter.y) < (rect->y) || (ballCenter.y) > (rect->y) + (rect->h)) {
|
|
|
|
(obj->Momentum).y = -(obj->Momentum).y;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} /* BALL_CollideWithRect */
|
|
|
|
|
|
|
|
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2){
|
|
|
|
if ((rect1->x) + (rect1->w) < (rect2->x)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((rect1->x) > (rect2->x) + (rect2->w)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((rect1->y) + (rect1->w) < (rect2->y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((rect1->y) > (rect2->y) + (rect2->h)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BALL_Update(Ball * obj, Paddle * paddle){
|
|
|
|
Vector lastMomentum = (obj->Momentum);
|
|
|
|
Vector lastLocation = (obj->Location);
|
|
|
|
|
2018-01-11 01:06:55 +01:00
|
|
|
(obj->Rotation) += (obj->RotationValue);
|
2018-01-11 14:12:30 +01:00
|
|
|
(obj->Location) = vectorAdd((obj->Location), lastMomentum);
|
|
|
|
|
|
|
|
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
|
|
|
|
(obj->Location) = vectorSub((obj->Location), lastMomentum); // Maybe remove this
|
2018-01-11 16:40:36 +01:00
|
|
|
BALL_SteerMomentum(obj, &(paddle->TargetRect));
|
2018-01-11 14:12:30 +01:00
|
|
|
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((obj->Location).y > BREAKOUT_BoxHeight)
|
2018-01-11 01:06:55 +01:00
|
|
|
(obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; // Dead
|
2018-01-11 14:12:30 +01:00
|
|
|
if ((obj->Location).y < 0.0f)
|
2018-01-11 01:06:55 +01:00
|
|
|
(obj->Momentum).y = -(obj->Momentum).y;
|
2018-01-11 14:12:30 +01:00
|
|
|
if ((obj->Location).x < 0.0f || (obj->Location).x > BREAKOUT_BoxWidth - (2 * (obj->Size)))
|
2018-01-11 01:06:55 +01:00
|
|
|
(obj->Momentum).x = -(obj->Momentum).x;
|
2018-01-11 14:12:30 +01:00
|
|
|
(obj->TargetRect).x = (int)round((obj->Location).x);
|
|
|
|
(obj->TargetRect).y = (int)round((obj->Location).y);
|
|
|
|
} /* BALL_Update */
|
2018-01-10 23:40:17 +01:00
|
|
|
void BALL_DestroyObject(Ball * obj){
|
|
|
|
}
|
|
|
|
void BALL_Deinitialize(){
|
|
|
|
printf("De-initializing Ball...\n");
|
|
|
|
|
|
|
|
printf("Ball de-initialized!\n");
|
|
|
|
}
|
2018-01-11 01:06:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
void PADDLE_Initialize(SDL_Renderer * renderer){
|
|
|
|
printf("Initializing Paddle...\n");
|
|
|
|
PADDLE_Texture = IMG_LoadTexture(renderer, "assets/images/paddle.png");
|
|
|
|
if (!PADDLE_Texture) printf("Paddle texture failed to load!\n");
|
|
|
|
PADDLE_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
|
|
|
|
if (!PADDLE_SourceRects) printf("FATAL! Memory allocation failed!\n");
|
|
|
|
PADDLE_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
|
|
|
|
PADDLE_MoveLeftKeys = (Uint8 *)malloc(2 * sizeof(Uint8));
|
|
|
|
if (!PADDLE_MoveLeftKeys) printf("FATAL! Memory allocation failed!\n");
|
|
|
|
PADDLE_MoveRightKeys = (Uint8 *)malloc(2 * sizeof(Uint8));
|
|
|
|
if (!PADDLE_MoveRightKeys) printf("FATAL! Memory allocation failed!\n");
|
|
|
|
PADDLE_MoveLeftKeys[0] = 2; // Erster wert gibt größe des arrays an
|
|
|
|
PADDLE_MoveLeftKeys[1] = SDL_SCANCODE_LEFT;
|
|
|
|
PADDLE_MoveLeftKeys[2] = SDL_SCANCODE_A;
|
|
|
|
PADDLE_MoveRightKeys[0] = 2;
|
|
|
|
PADDLE_MoveRightKeys[1] = SDL_SCANCODE_RIGHT;
|
|
|
|
PADDLE_MoveRightKeys[2] = SDL_SCANCODE_D;
|
|
|
|
ball = BALL_CreateDefault();
|
|
|
|
printf("Paddle initialized!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle PADDLE_CreateDefault(){
|
|
|
|
int defaultpaddlewidth = 300;
|
|
|
|
|
|
|
|
return (Paddle) {
|
|
|
|
.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 PADDLE_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));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyPressed(Uint8 * keystate, Uint8 * keyArray){
|
|
|
|
for (int i = 0; i < (*keyArray); i++) {
|
|
|
|
if (keystate[keyArray[(i + 1)]]) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void constrain(int * variable, int min, int max){
|
|
|
|
if (*variable > max)
|
|
|
|
*variable = max;
|
|
|
|
else if (*variable < min)
|
|
|
|
*variable = min;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADDLE_Update(Paddle * obj, Uint8 * keystate){
|
|
|
|
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
|
|
|
|
|
|
|
if (leftKeyPressed && (!rightKeyPressed)) {
|
2018-01-11 14:12:30 +01:00
|
|
|
((obj->TargetRect).x) -= PADDLE_Speed;
|
2018-01-11 01:06:55 +01:00
|
|
|
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
2018-01-11 14:12:30 +01:00
|
|
|
((obj->TargetRect).x) += PADDLE_Speed;
|
2018-01-11 01:06:55 +01:00
|
|
|
}
|
|
|
|
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
|
|
|
}
|
|
|
|
void PADDLE_DestroyObject(Paddle * obj){
|
|
|
|
}
|
|
|
|
void PADDLE_Deinitialize(){
|
|
|
|
printf("De-initializing Ball...\n");
|
|
|
|
|
|
|
|
printf("Ball de-initialized!\n");
|
|
|
|
}
|