Fixed bugs and deinitialization

This commit is contained in:
Michael Chen 2018-01-15 22:01:58 +01:00
parent 0dd20c4514
commit 4ede84786e
5 changed files with 105 additions and 76 deletions

View File

@ -1,13 +1,15 @@
libs = -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lSDL2_image -lSDL2_ttf
includes = -I".\include"
compiler = gcc
warningLevel = -Wall
warningLevel = -Wall -Wno-unused-variable -Wno-unused-but-set-variable
sources = *.c
linker = -L".\lib"
dir = bin
target = Breakout.exe
target = Breakout
args = -o
all:
$(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target)
start cmd /C "cd $(dir) && $(target)"
run:
cd $(dir) && $(target)

View File

@ -27,12 +27,6 @@ bool BREAKOUT_IsInit = false;
bool BALL_IsInit = false;
bool PADDLE_IsInit = false;
bool BLOCK_IsInit = false;
Ball ball;
Paddle paddle;
Block * blocks;
int BlockCount = 60; // Move to scenery
double BALL_Speed = 15.0f;
int PADDLE_Speed = 10;
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
if (!BREAKOUT_IsInit) {
@ -43,58 +37,55 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
BALL_Initialize(renderer);
PADDLE_Initialize(renderer);
BLOCK_Initialize(renderer);
ball = BALL_CreateDefault();
paddle = PADDLE_CreateDefault();
blocks = malloc(BlockCount * sizeof(Block));
int index;
for (int y = 0; y < 6; y++) {
index = 10 * y;
for (int x = 0; x < 10; x++) {
if (x + y == 0) {
blocks[x + index] = BLOCK_CreateDefault();
blocks[x + index].TargetRect = (SDL_Rect) {.x = 1, 1, .w = 184, .h = 92 };
blocks[x + index].TextureIndex = y + x;
} else {
blocks[x + index] = BLOCK_CreateDefault();
blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 };
blocks[x + index].TextureIndex = y + x;
}
}
}
printf("Game initialized!\n");
BREAKOUT_IsInit = true;
} else printf("Game is already initialized!\n");
} /* BREAKOUT_INITIALIZE */
Scenery BREAKOUT_CreateDefault(){
Scenery scenery;
scenery.BlockCount = 60;
scenery.ball = BALL_CreateDefault();
scenery.paddle = PADDLE_CreateDefault();
scenery.blocks = malloc(scenery.BlockCount * sizeof(Block));
int index;
for (int y = 0; y < 6; y++) {
index = 10 * y;
for (int x = 0; x < 10; x++) {
scenery.blocks[x + index] = BLOCK_CreateDefault();
scenery.blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 };
scenery.blocks[x + index].TextureIndex = y + x + 2;
}
}
return scenery;
}
// This Function is obsolete! Do not use it!
void BREAKOUT_ChangeSize(int width, int height){
BREAKOUT_BoxWidth = width;
BREAKOUT_BoxHeight = height;
}
void BREAKOUT_Update(Uint8 * keystate){
PADDLE_Update(&paddle, keystate); // Update paddle before ball because paddle is not static!
BALL_Update(&ball, &paddle);
for (size_t i = 0; i < BlockCount; i++) {
BLOCK_Update(blocks + i);
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate){
PADDLE_Update(&(scenery->paddle), keystate); // Update paddle before ball because paddle is not static!
BALL_Update(&(scenery->ball), &(scenery->paddle), (scenery->blocks), (scenery->BlockCount));
for (int i = 0; i < (scenery->BlockCount); i++) {
BLOCK_Update((scenery->blocks) + i);
}
}
void BREAKOUT_Draw(SDL_Renderer * renderer){
for (int i = 0; i < BlockCount; i++) {
BLOCK_Draw(renderer, &(blocks[i]));
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer){
for (int i = 0; i < (scenery->BlockCount); i++) {
BLOCK_Draw(renderer, &((scenery->blocks)[i]));
}
BALL_Draw(renderer, &ball);
PADDLE_Draw(renderer, &paddle);
BALL_Draw(renderer, &(scenery->ball));
PADDLE_Draw(renderer, &(scenery->paddle));
}
void BREAKOUT_DEINITIALIZE(){
if (BREAKOUT_IsInit) {
printf("De-initializing Game...\n");
for (size_t i = 0; i < BlockCount; i++) {
BLOCK_DestroyObject(blocks + i);
}
free(blocks);
free(PADDLE_MoveLeftKeys);
free(PADDLE_MoveRightKeys);
free(BALL_SourceRects);
@ -108,6 +99,15 @@ void BREAKOUT_DEINITIALIZE(){
} else printf("Game is already de-initialized!\n");
}
void BREAKOUT_DestroyObject(Scenery * scenery){
for (size_t i = 0; i < (scenery->BlockCount); i++) {
BLOCK_DestroyObject((scenery->blocks) + i);
}
BALL_DestroyObject(&(scenery->ball));
PADDLE_DestroyObject(&(scenery->paddle));
free((scenery->blocks));
}
void BALL_Initialize(SDL_Renderer * renderer){
if (!BALL_IsInit) {
printf("Initializing Ball...\n");
@ -126,12 +126,13 @@ Ball BALL_CreateDefault(){
return (Ball) {
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 + 300, .y = BREAKOUT_BoxHeight / 2 },
.Momentum = (Vector) {.x = 0.0f, .y = BALL_Speed },
.Momentum = (Vector) {.x = 0.0f, .y = 15.0f },
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 },
.Size = 25.0f,
.Rotation = rotation,
.RotationValue = 2,
.TextureIndex = 0
.TextureIndex = 0,
.Speed = 15.0f
}; // Objekt für die Eigenschaften des Balls
}
@ -151,8 +152,8 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
Vector center = (Vector) {.x = ballCenter.x, .y = ballCenter.y };
Vector corner;
// Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt!
double perpendicular, oldMomentum, angle;
oldMomentum = fmod((double)(vectorRotation(obj->Momentum) + 180), 360.0f);
// double perpendicular, oldMomentum, angle;
// oldMomentum = fmod((double)(vectorRotation(obj->Momentum) + 180), 360.0f);
bool left, right, top, bottom, yMid = false, xMid = false;
left = (ballCenter.x) < (rect->x);
@ -182,9 +183,9 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
* perpendicular = vectorRotation(vectorSub(center, corner));
* angle = fabs(perpendicular - oldMomentum);
* if (oldMomentum < perpendicular)
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), BALL_Speed);
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed));
* else
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), BALL_Speed);
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed));
*/
}
return true;
@ -207,12 +208,12 @@ bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2){
}
void BALL_SteerMomentum(Ball * obj, Paddle * paddle){
int paddleHalfLen = ((paddle->TargetRect).w / 2.0f);
double paddleHalfLen = ((double)((paddle->TargetRect).w) / 2.0f);
double offset = (((obj->TargetRect).x) + (obj->Size)) - ((paddle->TargetRect).x + paddleHalfLen);
offset *= 60.0f;
offset /= (double)(paddleHalfLen);
printf("Offset = %.2f\n", offset);
offset /= paddleHalfLen;
offset *= (paddle->SteeringAngle);
DOUBLE_Constrain(&offset, -(paddle->SteeringAngle), (paddle->SteeringAngle));
(obj->Momentum) = getDirectionalUnitVector(offset);
}
@ -225,7 +226,7 @@ SDL_Point BALL_GetCenter(Ball * obj){
return (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) };
}
void BALL_Update(Ball * obj, Paddle * paddle){
void BALL_Update(Ball * obj, Paddle * paddle, Block * blocks, int BlockCount){
Vector oldMomentum = obj->Momentum;
Vector oldLocation = obj->Location;
SDL_Point ballCenter = BALL_GetCenter(obj);
@ -242,7 +243,7 @@ void BALL_Update(Ball * obj, Paddle * paddle){
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
}
(obj->Momentum) = vectorScaleTo((obj->Momentum), BALL_Speed);
(obj->Momentum) = vectorScaleTo((obj->Momentum), (obj->Speed));
}
for (size_t i = 0; i < BlockCount; i++) {
if (blocks[i].HP <= 0) continue;
@ -304,7 +305,9 @@ Paddle PADDLE_CreateDefault(){
return (Paddle) {
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 },
.TextureIndex = 0
.TextureIndex = 0,
.Speed = 10,
.SteeringAngle = 40.0f
}; // Objekt für die Eigenschaften des Balls
}
@ -315,29 +318,36 @@ void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){
// SDL_RenderDrawRect(renderer, &(obj->TargetRect));
}
bool KeyPressed(Uint8 * keystate, Uint8 * keyArray){
bool KeyPressed(const 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){
void INT_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){
void DOUBLE_Constrain(double * variable, double min, double max){
if (*variable > max)
*variable = max;
else if (*variable < min)
*variable = min;
}
void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
if (leftKeyPressed && (!rightKeyPressed)) {
((obj->TargetRect).x) -= PADDLE_Speed;
((obj->TargetRect).x) -= (obj->Speed);
} else if ((!leftKeyPressed) && rightKeyPressed) {
((obj->TargetRect).x) += PADDLE_Speed;
((obj->TargetRect).x) += (obj->Speed);
}
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
}
void PADDLE_DestroyObject(Paddle * obj){

View File

@ -15,25 +15,37 @@ typedef struct ballStruct {
SDL_Rect TargetRect;
double Size, Rotation, RotationValue;
int TextureIndex;
double Speed;
} Ball; // Objekt für die Eigenschaften des Balls
typedef struct paddleStruct {
SDL_Rect TargetRect;
int TextureIndex;
int Speed;
double SteeringAngle;
} Paddle; // Objekt für die Eigenschaften des Paddles
typedef struct blockStruct {
SDL_Rect TargetRect;
int TextureIndex, HP;
} Block; // Objekt für die Eigenschaften des Paddles
typedef struct sceneryStruct {
Ball ball;
Paddle paddle;
Block * blocks;
int BlockCount; // Move to scenery
} Scenery; // Objekt für die Objekte und Eigenschaften einer Szenerie
// End Structs
// Prototypes
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height);
Scenery BREAKOUT_CreateDefault();
void BREAKOUT_ChangeSize(int width, int height);
void BREAKOUT_Update(Uint8 * keystate);
void BREAKOUT_Draw(SDL_Renderer * renderer);
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate);
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer);
void BREAKOUT_DEINITIALIZE();
void BREAKOUT_DestroyObject(Scenery * scenery);
void BALL_Initialize(SDL_Renderer * renderer);
Ball BALL_CreateDefault();
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
@ -42,15 +54,16 @@ bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
void BALL_SteerMomentum(Ball * obj, Paddle * paddle);
void RECT_SetTargetPos(SDL_Rect * rect, Vector * Location);
SDL_Point BALL_GetCenter(Ball * obj);
void BALL_Update(Ball * obj, Paddle * paddle);
void BALL_Update(Ball * obj, Paddle * paddle, Block * blocks, int BlockCount);
void BALL_DestroyObject(Ball * obj);
void BALL_Deinitialize();
void PADDLE_Initialize(SDL_Renderer * renderer);
Paddle PADDLE_CreateDefault();
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj);
bool KeyPressed(Uint8 * keystate, Uint8 * keyArray);
void constrain(int * variable, int min, int max);
void PADDLE_Update(Paddle * obj, Uint8 * keystate);
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray);
void INT_Constrain(int * variable, int min, int max);
void DOUBLE_Constrain(double * variable, double min, double max);
void PADDLE_Update(Paddle * obj, const Uint8 * keystate);
void PADDLE_DestroyObject(Paddle * obj);
void PADDLE_Deinitialize();
void BLOCK_Initialize(SDL_Renderer * renderer);

View File

@ -10,6 +10,7 @@ User * HIGHSCORES_GetList(int * usercount){
system("bhi top output.txt");
printf("BHI interface quit!\nBHI output handling...\n");
*usercount = 0;
FILE * fp;
char * line = NULL;
size_t len = 0;
@ -20,10 +21,10 @@ User * HIGHSCORES_GetList(int * usercount){
User * ul = malloc(10 * sizeof(User));
fp = fopen("output.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
return ul;
if ((read = getline(&line, &len, fp)) != -1)
if (line[0] == 0)
return EXIT_FAILURE;
return ul;
int counter = 0;
while ((read = getline(&line, &len, fp)) != -1) {
name = malloc(read * sizeof(char));

17
main.c
View File

@ -38,15 +38,15 @@ const int height = 1080;
float XScale = 1.0f, YScale = 1.0f;
Uint8 * keystate; // TODO: export all this into scenery and enemy waves
int numKeys;
const Uint8 * keystate; // TODO: export all this into scenery and enemy waves
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Event event;
bool running = true, fullscreen = false;
TTF_Font * font = NULL;
GameState gameState = Game;
Ball ball;
Scenery scenery;
int main(int argc, char * args[]){
printf("Spielbereiche\n\t- 1: Hauptmen"ue "\n\t- 2: Spiel\n\t- 3: Level Select\n\t- 4: Settings\n\t- 5: Highscores\n");
@ -54,12 +54,11 @@ int main(int argc, char * args[]){
INITIALIZE();
while (running) { // Gameloop
HandleSDLEvents();
keystate = SDL_GetKeyboardState(NULL);
DrawBackground(renderer);
switch (gameState) {
case Game:
BREAKOUT_Update(keystate);
BREAKOUT_Draw(renderer);
BREAKOUT_Update(&scenery, keystate);
BREAKOUT_Draw(&scenery, renderer);
break;
case MainMenu:
// Startmenu_Update(keystate);
@ -176,9 +175,11 @@ void printFontStyle(TTF_Font * ffont){
}
void INITIALIZE() {
printf("Initializing started...\n");
srand(time(NULL));
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! Error: %s\n", SDL_GetError());
else printf("SDL was successfully initialized!\n");
keystate = SDL_GetKeyboardState(&numKeys);
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) printf("IMG could not initialize! Error: %s\n", IMG_GetError());
else printf("IMG was successfully initialized!\n");
if (TTF_Init() == -1) printf("TTF could not initialize! Error: %s\n", TTF_GetError());
@ -196,13 +197,15 @@ void INITIALIZE() {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
printf("Renderer was created!\n");
BREAKOUT_INITIALIZE(renderer, width, height);
scenery = BREAKOUT_CreateDefault();
Load_Textures(renderer);
printf("Initializing finished!\n");
} /* INITIALIZE */
void QUIT(){
printf("De-initializing started...\n");
BREAKOUT_DestroyObject(&scenery);
BREAKOUT_DEINITIALIZE();
free(keystate);
TTF_CloseFont(font);
font = NULL; // to be safe...
TTF_Quit();