breakout/breakout.c

89 lines
2.7 KiB
C
Raw Normal View History

#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"
#ifndef __nullptr__
#define Nullptr(type) (type *)0
#endif // __nullptr__
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
SDL_Texture * BALL_Texture;
SDL_Rect * BALL_SourceRects;
Ball ball;
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
printf("Initializing Game...\n");
srand(time(NULL));
BREAKOUT_BoxWidth = width;
BREAKOUT_BoxHeight = height;
2018-01-10 23:56:47 +01:00
BALL_Initialize(renderer);
printf("Game initialized!\n");
}
void BREAKOUT_Update(Uint8 * keystate){
BALL_Update(&ball);
}
void BREAKOUT_Draw(SDL_Renderer * renderer){
BALL_Draw(renderer, &ball);
}
void BREAKOUT_DEINITIALIZE(){
printf("De-initializing Game...\n");
SDL_DestroyTexture(BALL_Texture);
printf("Game de-initialized!\n");
}
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 },
2018-01-10 23:56:47 +01:00
.Momentum = (Vector) {.x = 0.0f, .y = 6.0f },
.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){
2018-01-10 23:56:47 +01:00
printf("Ball drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
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);
2018-01-10 23:56:47 +01:00
obj->Rotation += obj->RotationValue;
if ((obj->Location).y > BREAKOUT_BoxHeight)
obj->Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 };
}
void BALL_DestroyObject(Ball * obj){
}
void BALL_Deinitialize(){
printf("De-initializing Ball...\n");
printf("Ball de-initialized!\n");
}