Changed initialization process! Added todos and bugs to fix later :)

This commit is contained in:
Michael Chen 2017-12-27 22:01:59 +01:00
parent 1088424048
commit 39d6786dc0
3 changed files with 211 additions and 182 deletions

View File

@ -15,6 +15,8 @@
#include "vector.h"
#include "asteroids.h"
// TODO: Export all this to a scenery (For later implementation of enemy waves)
// Properties
const double BULLET_Speed_Ally = 12.0f;
const double BULLET_Speed_Enemy = 6.0f;
@ -41,15 +43,36 @@ const SDL_Scancode SHIP_ShootButton = SDL_SCANCODE_SPACE;
// Vars
Bullet NULLET;
int Asteroids_BoxWidth, Asteroids_BoxHeight, SHIP_MaxBulletCount, ENEMY_MaxBulletCount; // TODO: Explosion Texture
int GAME_BoxWidth, GAME_BoxHeight, SHIP_MaxBulletCount, ENEMY_MaxBulletCount; // TODO: Explosion Texture
SDL_Rect SHIP_SourceRect_Gliding, SHIP_SourceRect_Accelerating, * SHIP_SourceRect_Explosion, * ASTEROID_SourceRect_Size1, * ASTEROID_SourceRect_Size2, * ASTEROID_SourceRect_Size3;
SDL_Texture * SHIP_Texture_Gliding, * SHIP_Texture_Accelerating, * SHIP_Texture_Explosion, * ASTEROID_Texture, * BULLET_Texture_Ally, * BULLET_Texture_Enemy, * ENEMY_Texture;
SDL_Point SHIP_RotationCenter_Flight, SHIP_RotationCenter_Explosion;
Asteroid ASTEROIDULLET;
bool ASTEROID_IsDestroyed = false;
bool ASTEROID_IsDestroyed = false, BULLET_IsInit = false, ENEMY_IsInit = false, SHIP_IsInit = false, ASTEROID_IsInit = false; // TODO: check if init successful
// End Vars
void BULLET_Initialize(SDL_Renderer * renderer, int width, int height){
void ASTEROIDS_InitializeGame(SDL_Renderer * renderer, int width, int height){
printf("Initializing Game...\n");
GAME_BoxWidth = width;
GAME_BoxHeight = height;
SHIP_Initialize(renderer);
BULLET_Initialize(renderer); // Call may be redundant
ENEMY_Initialize(renderer);
ASTEROID_Initialize(renderer);
printf("Game initialized!\n");
}
void ASTEROIDS_DeinitializeGame(){
printf("Make sure all resources are destroyed (not fatal)!\nDe-initializing Game...\n");
SHIP_Deinitialize();
BULLET_Deinitialize(); // Call may be redundant
ENEMY_Deinitialize();
ASTEROID_Deinitialize();
printf("Game de-initialized!\n");
}
void BULLET_Initialize(SDL_Renderer * renderer){
if (BULLET_IsInit) return;
printf("Initializing Bullet...\n");
NULLET = (Bullet) {
.Rotation = 0.0f,
@ -60,106 +83,52 @@ void BULLET_Initialize(SDL_Renderer * renderer, int width, int height){
.IsDestroyed = true,
.Type = AllyBullet
}; // Default or null bullet
Asteroids_BoxWidth = width;
Asteroids_BoxHeight = height;
BULLET_Texture_Ally = IMG_LoadTexture(renderer, "assets/images/bullet.png");
if (!BULLET_Texture_Ally) printf("Ally bullet texture cannot be loaded!\n");
BULLET_Texture_Enemy = IMG_LoadTexture(renderer, "assets/images/enemyBullet.png");
if (!BULLET_Texture_Enemy) printf("Enemy bullet texture cannot be loaded!\n");
printf("Bullet initialized!\n");
BULLET_IsInit = true;
}
void BULLET_Deinitialize(){
printf("De-initializing Bullet...\n");
printf("De-initializing ally bullet texture...\n");
SDL_DestroyTexture(BULLET_Texture_Ally);
printf("De-initializing enemy bullet texture...\n");
SDL_DestroyTexture(BULLET_Texture_Enemy);
printf("De-initializing Bullet finished!\n");
}
void ENEMY_Initialize(SDL_Renderer * renderer){
if (ENEMY_IsInit) return;
printf("Initializing Enemy...\n");
BULLET_Initialize(renderer);
ENEMY_Texture = IMG_LoadTexture(renderer, "assets/images/enemy.png");
if (!ENEMY_Texture) printf("Enemy texture cannot be loaded!\n");
ENEMY_MaxBulletCount = ((BULLET_DecayTime_Ally / ENEMY_ShootIntevale) + 10);
printf("Enemy initialized!\n");
ENEMY_IsInit = true;
} // ENEMY_Initialize
Bullet BULLET_Fire(Vector location, double direction, BulletType type){
return (Bullet) {
.Rotation = direction,
.Location = location,
.Momentum = getScaledDirectionalUnitVector(direction, (type == AllyBullet ? BULLET_Speed_Ally : BULLET_Speed_Enemy)),
.TargetRect = (type == AllyBullet ? (SDL_Rect) {.x = 0, .y = 0, .w = 12, .h = 35 } : (SDL_Rect) {.x = 0, .y = 0, .w = 30, .h = 30 }),
.Lifetime = (type == AllyBullet ? BULLET_DecayTime_Ally : BULLET_DecayTime_Enemy),
.IsDestroyed = false,
.Type = type
};
}
void BULLET_UpdateAlly(Bullet * bul, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount) {
if (!(bul->IsDestroyed)) {
if ((bul->Lifetime)-- <= 0) { // Decay
(bul->IsDestroyed) = true;
return;
}
(bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move
if ((bul->Location).x < 0.0f) (bul->Location).x = Asteroids_BoxWidth; // Check Boundaries
else if ((bul->Location).x > Asteroids_BoxWidth) (bul->Location).x = 0.0f;
if ((bul->Location).y < 0.0f) (bul->Location).y = Asteroids_BoxHeight;
else if ((bul->Location).y > Asteroids_BoxHeight) (bul->Location).y = 0.0f;
(bul->TargetRect).x = (int)round((bul->Location).x); // Update render Target
(bul->TargetRect).y = (int)round((bul->Location).y);
int i;
for (i = 0; i < (*astCount); i++) { // Collide with Asteroids
if (!(asts[i].IsDestroyed)) {
if (RECT_CircleCollide(&(asts[i].TargetRect), &(bul->TargetRect))) {
printf("Bullet is destroyed by asteroid no. %d!\n", i + 1);
ASTEROID_TryDestroy(asts, astCount, i);
if ((asts[i].IsDestroyed)) printf("Bullet destroyed asteroid no. %d!\n", i + 1);
(bul->IsDestroyed) = true;
break;
}
}
}
void SHIP_Initialize(SDL_Renderer * renderer){
if (SHIP_IsInit) return;
printf("Initializing Ship...\n");
BULLET_Initialize(renderer);
SHIP_Texture_Gliding = IMG_LoadTexture(renderer, "assets/images/ship.png");
if (!SHIP_Texture_Gliding) printf("Ship texture 1 cannot be loaded!\n");
SHIP_Texture_Accelerating = IMG_LoadTexture(renderer, "assets/images/shipAcc.png");
if (!SHIP_Texture_Accelerating) printf("Ship texture 2 cannot be loaded!\n");
SHIP_Texture_Explosion = IMG_LoadTexture(renderer, "assets/images/shipExpl.png");
if (!SHIP_Texture_Explosion) printf("Ship texture explosion cannot be loaded!\n");
SHIP_SourceRect_Explosion = calloc(5, sizeof(SDL_Rect));
if (!SHIP_SourceRect_Explosion) printf("Memory for SDL_Rect (SHIP) cannot be allocated!\n");
int i;
for (i = 0; i < SHIP_DeathAnimFrameCount; i++) {
SHIP_SourceRect_Explosion[i] = (SDL_Rect) {.x = 0, .y = (i * 1632), .w = 1280, .h = 1632 };
}
} // BULLET_Update
SHIP_SourceRect_Gliding = (SDL_Rect) {.x = 0, .y = 0, .w = 320, .h = 408 }; // TODO: Sourcerects are currently redundant considering the usage of SDL_RenderCopyEx
SHIP_SourceRect_Accelerating = (SDL_Rect) {.x = 0, .y = 0, .w = 320, .h = 576 };
SHIP_RotationCenter_Flight = (SDL_Point) {.x = 20, .y = 25 };
SHIP_MaxBulletCount = ((BULLET_DecayTime_Ally / SHIP_ShootIntevale) + 10);
printf("Ship initialized!\n");
SHIP_IsInit = true;
} // SHIP_Initialize
void BULLET_UpdateEnemy(Bullet * bul, Ship * shp, int * shpCount) {
if (!(bul->IsDestroyed)) {
if ((bul->Lifetime)-- <= 0) { // Decay
(bul->IsDestroyed) = true;
return;
}
// TODO: Export triple use of boundary check to method using pointers
(bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move
if ((bul->Location).x < 0.0f) (bul->Location).x = Asteroids_BoxWidth; // Check Boundaries
else if ((bul->Location).x > Asteroids_BoxWidth) (bul->Location).x = 0.0f;
if ((bul->Location).y < 0.0f) (bul->Location).y = Asteroids_BoxHeight;
else if ((bul->Location).y > Asteroids_BoxHeight) (bul->Location).y = 0.0f;
(bul->TargetRect).x = (int)round((bul->Location).x); // Update render Target
(bul->TargetRect).y = (int)round((bul->Location).y);
int i;
for (i = 0; i < (*shpCount); i++) { // Collide with Asteroids
if (!(shp->IsDead)) {
if (RECT_CircleCollide(&(shp[i].TargetRect_Gliding), &(bul->TargetRect))) {
printf("Player %d is destroyed by bullet!\n", i + 1);
(bul->IsDestroyed) = true;
(shp[i].IsDead) = true;
break;
}
}
}
}
} // BULLET_Update
void BULLET_Draw(Bullet * bul, SDL_Renderer * renderer) {
if (!(bul->IsDestroyed)) {
if ((bul->Type) == AllyBullet) {
SDL_RenderCopyEx(renderer, BULLET_Texture_Ally, NULL, &(bul->TargetRect), (bul->Rotation), NULL, SDL_FLIP_NONE);
} else {
SDL_RenderCopyEx(renderer, BULLET_Texture_Enemy, NULL, &(bul->TargetRect), (bul->Rotation), NULL, SDL_FLIP_NONE);
}
}
}
void ASTEROID_Initialize(SDL_Renderer * renderer, int width, int height){
void ASTEROID_Initialize(SDL_Renderer * renderer){
if (ASTEROID_IsInit) return;
printf("Initializing Asteroid...\n");
Asteroids_BoxWidth = width;
Asteroids_BoxHeight = height;
ASTEROID_Texture = IMG_LoadTexture(renderer, "assets/images/asteroid.png");
ASTEROIDULLET = (Asteroid) {
.Rotation = 0.0f,
@ -186,15 +155,142 @@ void ASTEROID_Initialize(SDL_Renderer * renderer, int width, int height){
ASTEROID_SourceRect_Size3[2] = (SDL_Rect) {.x = 1400, .y = 1200, .w = 200, .h = 200 };
ASTEROID_SourceRect_Size3[3] = (SDL_Rect) {.x = 1400, .y = 1400, .w = 200, .h = 200 };
printf("Asteroid initialized!\n");
ASTEROID_IsInit = true;
} // ASTEROID_Initialize
void BULLET_Deinitialize(){
if (!BULLET_IsInit) return;
printf("De-initializing Bullet...\n");
printf("De-initializing ally bullet texture...\n");
SDL_DestroyTexture(BULLET_Texture_Ally);
printf("De-initializing enemy bullet texture...\n");
SDL_DestroyTexture(BULLET_Texture_Enemy);
printf("De-initializing Bullet finished!\n");
BULLET_IsInit = false;
}
void ENEMY_Deinitialize(){
if (!ENEMY_IsInit) return;
printf("De-initializing Enemy class...\n");
BULLET_Deinitialize();
printf("De-initializing Enemy texture 1...\n");
SDL_DestroyTexture(ENEMY_Texture);
printf("De-initializing Enemy class finished!\n");
ENEMY_IsInit = false;
}
void ASTEROID_Deinitialize(){
if (!ASTEROID_IsInit) return;
printf("De-initializing Asteroid...\n");
printf("De-initializing Asteroid texture...\n");
SDL_DestroyTexture(ASTEROID_Texture);
free(ASTEROID_SourceRect_Size1);
free(ASTEROID_SourceRect_Size2);
free(ASTEROID_SourceRect_Size3);
printf("De-initializing Asteroid finished!\n");
ASTEROID_IsInit = false;
}
void SHIP_Deinitialize(){
if (!SHIP_IsInit) return;
printf("De-initializing Ship class...\n");
BULLET_Deinitialize();
free(SHIP_SourceRect_Explosion);
printf("De-initializing Ship texture 1...\n");
SDL_DestroyTexture(SHIP_Texture_Gliding);
printf("De-initializing Ship texture 2...\n");
SDL_DestroyTexture(SHIP_Texture_Accelerating);
printf("De-initializing Ship texture 2...\n");
SDL_DestroyTexture(SHIP_Texture_Explosion);
printf("De-initializing Ship class finished!\n");
SHIP_IsInit = false;
}
Bullet BULLET_Fire(Vector location, double direction, BulletType type){
return (Bullet) {
.Rotation = direction,
.Location = location,
.Momentum = getScaledDirectionalUnitVector(direction, (type == AllyBullet ? BULLET_Speed_Ally : BULLET_Speed_Enemy)),
.TargetRect = (type == AllyBullet ? (SDL_Rect) {.x = 0, .y = 0, .w = 12, .h = 35 } : (SDL_Rect) {.x = 0, .y = 0, .w = 30, .h = 30 }),
.Lifetime = (type == AllyBullet ? BULLET_DecayTime_Ally : BULLET_DecayTime_Enemy),
.IsDestroyed = false,
.Type = type
};
}
void BULLET_UpdateAlly(Bullet * bul, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount) {
if (!(bul->IsDestroyed)) {
if ((bul->Lifetime)-- <= 0) { // Decay
(bul->IsDestroyed) = true;
return;
}
(bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move
if ((bul->Location).x < 0.0f) (bul->Location).x = GAME_BoxWidth; // Check Boundaries
else if ((bul->Location).x > GAME_BoxWidth) (bul->Location).x = 0.0f;
if ((bul->Location).y < 0.0f) (bul->Location).y = GAME_BoxHeight;
else if ((bul->Location).y > GAME_BoxHeight) (bul->Location).y = 0.0f;
(bul->TargetRect).x = (int)round((bul->Location).x); // Update render Target
(bul->TargetRect).y = (int)round((bul->Location).y);
int i;
for (i = 0; i < (*astCount); i++) { // Collide with Asteroids
if (!(asts[i].IsDestroyed)) {
if (RECT_CircleCollide(&(asts[i].TargetRect), &(bul->TargetRect))) {
printf("Bullet is destroyed by asteroid no. %d!\n", i + 1);
ASTEROID_TryDestroy(asts, astCount, i);
if ((asts[i].IsDestroyed)) printf("Bullet destroyed asteroid no. %d!\n", i + 1);
(bul->IsDestroyed) = true;
break;
}
}
}
}
} // BULLET_Update
void BULLET_UpdateEnemy(Bullet * bul, Ship * shp, int * shpCount) {
if (!(bul->IsDestroyed)) {
if ((bul->Lifetime)-- <= 0) { // Decay
(bul->IsDestroyed) = true;
return;
}
// TODO: Export triple use of boundary check to method using pointers
(bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move
if ((bul->Location).x < 0.0f) (bul->Location).x = GAME_BoxWidth; // Check Boundaries
else if ((bul->Location).x > GAME_BoxWidth) (bul->Location).x = 0.0f;
if ((bul->Location).y < 0.0f) (bul->Location).y = GAME_BoxHeight;
else if ((bul->Location).y > GAME_BoxHeight) (bul->Location).y = 0.0f;
(bul->TargetRect).x = (int)round((bul->Location).x); // Update render Target
(bul->TargetRect).y = (int)round((bul->Location).y);
int i;
for (i = 0; i < (*shpCount); i++) { // Collide with Asteroids
if (!(shp->IsDead)) {
if (RECT_CircleCollide(&(shp[i].TargetRect_Gliding), &(bul->TargetRect))) {
printf("Player %d is destroyed by bullet!\n", i + 1);
(bul->IsDestroyed) = true;
(shp[i].IsDead) = true;
break;
}
}
}
}
} // BULLET_Update
void BULLET_Draw(Bullet * bul, SDL_Renderer * renderer) {
if (!(bul->IsDestroyed)) {
if ((bul->Type) == AllyBullet) {
SDL_RenderCopyEx(renderer, BULLET_Texture_Ally, NULL, &(bul->TargetRect), (bul->Rotation), NULL, SDL_FLIP_NONE);
} else {
SDL_RenderCopyEx(renderer, BULLET_Texture_Enemy, NULL, &(bul->TargetRect), (bul->Rotation), NULL, SDL_FLIP_NONE);
}
}
}
Asteroid ASTEROID_CreateRandom(){
return (Asteroid) {
.Rotation = (double)(rand() % 360),
.Size = 200,
.RotationValue = fmod((double)(rand()), 4.0f) - 2.0f,
// TODO: Reimplement random start pos here later
// .Location = (_vector) {.x = (double)(rand() % Asteroids_BoxWidth), .y = (double)(rand() % Asteroids_BoxHeight) },
// .Location = (_vector) {.x = (double)(rand() % GAME_BoxWidth), .y = (double)(rand() % GAME_BoxHeight) },
.Location = (Vector) {.x = 0, .y = 0 },
.Momentum = getScaledDirectionalUnitVector((double)(rand() % 360), fmod((double)rand(), 2.0f) + 1.0f),
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 200, .h = 200 },
@ -259,25 +355,15 @@ double distance(SDL_Rect * r1, SDL_Rect * r2){
return round(sqrt((dx * dx ) + (dy * dy)));
}
void ASTEROID_Deinitialize(){
printf("De-initializing Asteroid...\n");
printf("De-initializing Asteroid texture...\n");
SDL_DestroyTexture(ASTEROID_Texture);
free(ASTEROID_SourceRect_Size1);
free(ASTEROID_SourceRect_Size2);
free(ASTEROID_SourceRect_Size3);
printf("De-initializing Asteroid finished!\n");
}
void ASTEROID_Update(Asteroid * ast){
if (!(ast->IsDestroyed)) {
(ast->Rotation) += (ast->RotationValue); // Rotate
(ast->Location) = vectorAdd(ast->Location, ast->Momentum); // Update Position
if ((ast->Location).x < -(ast->Size)) (ast->Location).x = Asteroids_BoxWidth; // Check Boundaries
else if ((ast->Location).x > Asteroids_BoxWidth) (ast->Location).x = -(ast->Size);
if ((ast->Location).y < -(ast->Size)) (ast->Location).y = Asteroids_BoxHeight;
else if ((ast->Location).y > Asteroids_BoxHeight) (ast->Location).y = -(ast->Size);
if ((ast->Location).x < -(ast->Size)) (ast->Location).x = GAME_BoxWidth; // Check Boundaries
else if ((ast->Location).x > GAME_BoxWidth) (ast->Location).x = -(ast->Size);
if ((ast->Location).y < -(ast->Size)) (ast->Location).y = GAME_BoxHeight;
else if ((ast->Location).y > GAME_BoxHeight) (ast->Location).y = -(ast->Size);
(ast->TargetRect).x = (int)round((ast->Location).x);
(ast->TargetRect).y = (int)round((ast->Location).y);
}
@ -289,17 +375,6 @@ void ASTEROID_Draw(Asteroid * ast, SDL_Renderer * renderer){
}
}
void ENEMY_Initialize(SDL_Renderer * renderer, int width, int height){
printf("Initializing Enemy...\n");
BULLET_Initialize(renderer, width, height);
Asteroids_BoxWidth = width;
Asteroids_BoxHeight = height;
ENEMY_Texture = IMG_LoadTexture(renderer, "assets/images/enemy.png");
if (!ENEMY_Texture) printf("Enemy texture cannot be loaded!\n");
ENEMY_MaxBulletCount = ((BULLET_DecayTime_Ally / ENEMY_ShootIntevale) + 10);
printf("Enemy initialized!\n");
} // ENEMY_Initialize
Enemy ENEMY_GetDefault(){
Enemy temp;
@ -308,7 +383,7 @@ Enemy ENEMY_GetDefault(){
for (i = 0; i < ENEMY_MaxBulletCount; i++) {
temp.Bullets[i] = NULLET; // Nullet is set only if BULLET_Initialize() was called!
}
temp.Location = (Vector) {.x = (double)(rand() % Asteroids_BoxWidth), .y = (double)(rand() % Asteroids_BoxHeight) };
temp.Location = (Vector) {.x = (double)(rand() % GAME_BoxWidth), .y = (double)(rand() % GAME_BoxHeight) };
temp.Momentum = getScaledDirectionalUnitVector((rand() % 360), fmod((double)rand(), ENEMY_MaxSpeed / 2.0f) + (ENEMY_MaxSpeed / 2.0f));
temp.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 80, .h = 40 };
temp.WeaponCooldown = ENEMY_ShootIntevale;
@ -319,14 +394,6 @@ Enemy ENEMY_GetDefault(){
return temp;
} // ENEMY_CreateDefault
void ENEMY_Deinitialize(){
printf("De-initializing Enemy class...\n");
BULLET_Deinitialize();
printf("De-initializing Enemy texture 1...\n");
SDL_DestroyTexture(ENEMY_Texture);
printf("De-initializing Enemy class finished!\n");
}
void ENEMY_DestroyObject(Enemy * enm){
printf("De-initializing Enemy...\n");
printf("De-initializing Bullet-array...\n");
@ -347,10 +414,10 @@ void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount){
}
(enm->Location) = vectorAdd((enm->Location), (enm->Momentum)); // Update Position
if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = Asteroids_BoxWidth;
else if ((enm->Location).x > Asteroids_BoxWidth + (enm->TargetRect).w) (enm->Location).x = 0.0f;
if ((enm->Location).y < -(enm->TargetRect).h) (enm->Location).y = Asteroids_BoxHeight;
else if ((enm->Location).y > Asteroids_BoxHeight + (enm->TargetRect).h) (enm->Location).y = 0.0f;
if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = GAME_BoxWidth;
else if ((enm->Location).x > GAME_BoxWidth + (enm->TargetRect).w) (enm->Location).x = 0.0f;
if ((enm->Location).y < -(enm->TargetRect).h) (enm->Location).y = GAME_BoxHeight;
else if ((enm->Location).y > GAME_BoxHeight + (enm->TargetRect).h) (enm->Location).y = 0.0f;
(enm->TargetRect).x = (int)round((enm->Location).x);
(enm->TargetRect).y = (int)round((enm->Location).y);
@ -378,30 +445,6 @@ void ENEMY_Draw(Enemy * enm, SDL_Renderer * renderer){
}
}
void SHIP_Initialize(SDL_Renderer * renderer, int width, int height){
printf("Initializing Ship...\n");
BULLET_Initialize(renderer, width, height);
Asteroids_BoxWidth = width;
Asteroids_BoxHeight = height;
SHIP_Texture_Gliding = IMG_LoadTexture(renderer, "assets/images/ship.png");
if (!SHIP_Texture_Gliding) printf("Ship texture 1 cannot be loaded!\n");
SHIP_Texture_Accelerating = IMG_LoadTexture(renderer, "assets/images/shipAcc.png");
if (!SHIP_Texture_Accelerating) printf("Ship texture 2 cannot be loaded!\n");
SHIP_Texture_Explosion = IMG_LoadTexture(renderer, "assets/images/shipExpl.png");
if (!SHIP_Texture_Explosion) printf("Ship texture explosion cannot be loaded!\n");
SHIP_SourceRect_Explosion = calloc(5, sizeof(SDL_Rect));
if (!SHIP_SourceRect_Explosion) printf("Memory for SDL_Rect (SHIP) cannot be allocated!\n");
int i;
for (i = 0; i < SHIP_DeathAnimFrameCount; i++) {
SHIP_SourceRect_Explosion[i] = (SDL_Rect) {.x = 0, .y = (i * 1632), .w = 1280, .h = 1632 };
}
SHIP_SourceRect_Gliding = (SDL_Rect) {.x = 0, .y = 0, .w = 320, .h = 408 };
SHIP_SourceRect_Accelerating = (SDL_Rect) {.x = 0, .y = 0, .w = 320, .h = 576 };
SHIP_RotationCenter_Flight = (SDL_Point) {.x = 20, .y = 25 };
SHIP_MaxBulletCount = ((BULLET_DecayTime_Ally / SHIP_ShootIntevale) + 10);
printf("Ship initialized!\n");
} // SHIP_Initialize
Ship SHIP_CreateDefault(){
Ship temp;
@ -425,19 +468,6 @@ Ship SHIP_CreateDefault(){
return temp;
} // SHIP_CreateDefault
void SHIP_Deinitialize(){
printf("De-initializing Ship class...\n");
BULLET_Deinitialize();
free(SHIP_SourceRect_Explosion);
printf("De-initializing Ship texture 1...\n");
SDL_DestroyTexture(SHIP_Texture_Gliding);
printf("De-initializing Ship texture 2...\n");
SDL_DestroyTexture(SHIP_Texture_Accelerating);
printf("De-initializing Ship texture 2...\n");
SDL_DestroyTexture(SHIP_Texture_Explosion);
printf("De-initializing Ship class finished!\n");
}
void SHIP_DestroyObject(Ship * shp){
printf("De-initializing Ship...\n");
printf("De-initializing Bullet-array...\n");
@ -474,10 +504,10 @@ void SHIP_Update(Ship * shp, Uint8 * keystate, Asteroid * asts, int * astCount,
}
(shp->Location) = vectorAdd((shp->Location), (shp->Momentum)); // Update Position
if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = Asteroids_BoxWidth;
else if ((shp->Location).x > Asteroids_BoxWidth + (shp->TargetRect_Gliding).w) (shp->Location).x = 0.0f;
if ((shp->Location).y < -(shp->TargetRect_Gliding).h) (shp->Location).y = Asteroids_BoxHeight;
else if ((shp->Location).y > Asteroids_BoxHeight + (shp->TargetRect_Gliding).h) (shp->Location).y = 0.0f;
if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = GAME_BoxWidth;
else if ((shp->Location).x > GAME_BoxWidth + (shp->TargetRect_Gliding).w) (shp->Location).x = 0.0f;
if ((shp->Location).y < -(shp->TargetRect_Gliding).h) (shp->Location).y = GAME_BoxHeight;
else if ((shp->Location).y > GAME_BoxHeight + (shp->TargetRect_Gliding).h) (shp->Location).y = 0.0f;
(shp->TargetRect_Gliding).x = (int)round((shp->Location).x);
(shp->TargetRect_Gliding).y = (int)round((shp->Location).y);
(shp->TargetRect_Accelerating).x = (shp->TargetRect_Gliding).x;

View File

@ -44,13 +44,15 @@ typedef struct asteroidStruct {
// End Structs
// Prototypes
void BULLET_Initialize(SDL_Renderer * renderer, int width, int height);
void ASTEROIDS_InitializeGame(SDL_Renderer * renderer, int width, int height);
void ASTEROIDS_DeinitializeGame();
void BULLET_Initialize(SDL_Renderer * renderer);
void BULLET_Deinitialize();
Bullet BULLET_Fire(Vector location, double direction, BulletType type);
void BULLET_UpdateAlly(Bullet * bul, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount);
void BULLET_UpdateEnemy(Bullet * bul, Ship * shp, int * shpCount);
void BULLET_Draw(Bullet * bul, SDL_Renderer * renderer);
void ASTEROID_Initialize(SDL_Renderer * renderer, int width, int height);
void ASTEROID_Initialize(SDL_Renderer * renderer);
Asteroid ASTEROID_CreateRandom();
bool ASTEROID_TryDestroy(Asteroid * asts, int * astCount, int index);
Asteroid ASTEROID_CreateFromSplit(Asteroid * creator);
@ -60,13 +62,13 @@ double distance(SDL_Rect * r1, SDL_Rect * r2);
void ASTEROID_Deinitialize();
void ASTEROID_Update(Asteroid * ast);
void ASTEROID_Draw(Asteroid * ast, SDL_Renderer * renderer);
void ENEMY_Initialize(SDL_Renderer * renderer, int width, int height);
void ENEMY_Initialize(SDL_Renderer * renderer);
Enemy ENEMY_GetDefault();
void ENEMY_Deinitialize();
void ENEMY_DestroyObject(Enemy * enm);
void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount);
void ENEMY_Draw(Enemy * enm, SDL_Renderer * renderer);
void SHIP_Initialize(SDL_Renderer * renderer, int width, int height);
void SHIP_Initialize(SDL_Renderer * renderer);
Ship SHIP_CreateDefault();
void SHIP_Deinitialize();
void SHIP_DestroyObject(Ship * shp);

9
main.c
View File

@ -130,10 +130,9 @@ void INITIALIZE() {
printf("Window was created!\n");
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
printf("Renderer was created!\n");
STARFIELD_Initialize(renderer, width, height);
SHIP_Initialize(renderer, width, height);
ENEMY_Initialize(renderer, width, height);
ASTEROID_Initialize(renderer, width, height);
ASTEROIDS_InitializeGame(renderer, width, height);
starfield = STARFIELD_GetDefault(100);
ship = SHIP_CreateDefault();
@ -159,9 +158,7 @@ void QUIT(){
free(enemies);
STARFIELD_DestroyObject(&starfield);
STARFIELD_Deinitialize();
SHIP_Deinitialize();
ENEMY_Deinitialize();
ASTEROID_Deinitialize();
ASTEROIDS_DeinitializeGame();
TTF_CloseFont(Sans);
TTF_Quit();
free(asteroids);