Added a new Struct for Images

This commit is contained in:
Asrın Doğan 2019-09-26 21:10:50 +03:00
parent 049612b1c8
commit f1620e54a0
1 changed files with 76 additions and 53 deletions

125
SynGame.c
View File

@ -41,6 +41,12 @@ typedef struct
float y; float y;
} Vector2D; } Vector2D;
typedef struct
{
Vector2D size;
ALLEGRO_BITMAP *bitmap;
} Image;
typedef struct typedef struct
{ {
Vector2D position; Vector2D position;
@ -81,7 +87,7 @@ struct
unsigned int killedEnemyCount; unsigned int killedEnemyCount;
unsigned int score; unsigned int score;
ALLEGRO_BITMAP *playerImage; Image image;
} player; } player;
void SpawnEnemies(); void SpawnEnemies();
@ -93,8 +99,8 @@ void CheckEnemies();
void MoveEnemies(); void MoveEnemies();
void LimitEnemies(); void LimitEnemies();
void DestroyGame(); void DestroyGame();
void DrawObject(Vector2D position, ALLEGRO_BITMAP *image, int flag); void DrawObject(Vector2D position, Image *image, int flag);
void DrawSizedObject(Vector2D position, ALLEGRO_BITMAP *image, int flag, float objectscreenSizeMultiplier); void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscreenSizeMultiplier);
void DrawScreen(); void DrawScreen();
void DrawScore(); void DrawScore();
void DrawHighScore(); void DrawHighScore();
@ -121,12 +127,14 @@ float VectorMagnitude(Vector2D vector);
float VectorDistanceBetween(Vector2D vectorFirst, Vector2D vectorSecond); float VectorDistanceBetween(Vector2D vectorFirst, Vector2D vectorSecond);
byte isVectorExceedingLimits(Vector2D vector, Vector2D limits); byte isVectorExceedingLimits(Vector2D vector, Vector2D limits);
byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, ALLEGRO_BITMAP *firstMap, ALLEGRO_BITMAP *secondMap); byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Image *secondMap);
char InitializeGameWindow(); char InitializeGameWindow();
char InitializeGame(); char InitializeGame();
char DealDamage(char *health); char DealDamage(char *health);
Image InitImage(const char *path);
Vector2D NormalizeVector(Vector2D vector); Vector2D NormalizeVector(Vector2D vector);
ALLEGRO_KEYBOARD_STATE keyboardState; ALLEGRO_KEYBOARD_STATE keyboardState;
@ -135,19 +143,19 @@ ALLEGRO_COLOR backgroundColor;
ALLEGRO_DISPLAY *display = NULL; ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL; ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *gameOverImage = NULL; Image gameOverImage;
ALLEGRO_SAMPLE *BGM = NULL; ALLEGRO_SAMPLE *BGM = NULL;
ALLEGRO_SAMPLE *shootSound = NULL; ALLEGRO_SAMPLE *shootSound = NULL;
ALLEGRO_SAMPLE_ID shootSoundID; ALLEGRO_SAMPLE_ID shootSoundID;
ALLEGRO_BITMAP *enemyImage = NULL; Image enemyImage;
ALLEGRO_BITMAP *enemyBulletImage = NULL; Image enemyBulletImage;
ALLEGRO_SAMPLE *enemyDieSound = NULL; ALLEGRO_SAMPLE *enemyDieSound = NULL;
ALLEGRO_SAMPLE_ID enemyDieSoundID; ALLEGRO_SAMPLE_ID enemyDieSoundID;
ALLEGRO_BITMAP *numberTable = NULL; Image numberTable;
const char *playerImagePath = "Images/Player.png"; const char *playerImagePath = "Images/Player.png";
const char *enemyImagePath = "Images/Enemy.png"; const char *enemyImagePath = "Images/Enemy.png";
@ -318,20 +326,25 @@ byte isVectorExceedingLimits(Vector2D vector, Vector2D limits)
And compares the distance between those objects to the minimum distancce to check if they're colliding. And compares the distance between those objects to the minimum distancce to check if they're colliding.
It's the most optimized way I can think of for a game like this. It's the most optimized way I can think of for a game like this.
*/ */
byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, ALLEGRO_BITMAP *firstMap, ALLEGRO_BITMAP *secondMap) byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Image *secondMap)
{ {
Vector2D firstImageSize; // Vector2D firstMap -> size;
Vector2D secondImageSize; // Vector2D secondMap -> size;
byte result; byte result;
float minDistance; float minDistance;
float distance; float distance;
firstImageSize.x = (float)al_get_bitmap_width(firstMap); // firstMap -> size.x = (float)al_get_bitmap_width(firstMap);
firstImageSize.y = (float)al_get_bitmap_height(firstMap); // firstMap -> size.y = (float)al_get_bitmap_height(firstMap);
secondImageSize.x = (float)al_get_bitmap_width(secondMap); // secondMap -> size.x = (float)al_get_bitmap_width(secondMap);
secondImageSize.y = (float)al_get_bitmap_height(secondMap); // secondMap -> size.y = (float)al_get_bitmap_height(secondMap);
minDistance = firstImageSize.x > firstImageSize.y ? firstImageSize.y : firstImageSize.x; minDistance = firstMap -> size.x > firstMap -> size.y ?
minDistance += secondImageSize.x > secondImageSize.y ? secondImageSize.y : secondImageSize.x; firstMap -> size.y :
firstMap -> size.x;
minDistance += secondMap -> size.x > secondMap -> size.y ?
secondMap -> size.y :
secondMap -> size.x;
minDistance *= screenSizeMultiplier * 0.5f; minDistance *= screenSizeMultiplier * 0.5f;
@ -427,9 +440,9 @@ char InitializeGame()
player.moveSpeed = playerSpeed * screenSizeMultiplier; player.moveSpeed = playerSpeed * screenSizeMultiplier;
player.shootPerSecond = 10; player.shootPerSecond = 10;
player.health = initialPlayerHealth; player.health = initialPlayerHealth;
player.playerImage = al_load_bitmap(playerImagePath); player.image = InitImage(playerImagePath);
if(player.playerImage == NULL || if(player.image.bitmap == NULL ||
shootSound == NULL || shootSound == NULL ||
enemyDieSound == NULL) enemyDieSound == NULL)
return 0; return 0;
@ -452,13 +465,22 @@ void InitializeEnemies()
enemies.enemyLimit = initialEnemyLimit; enemies.enemyLimit = initialEnemyLimit;
enemies.enemyCount = 0; enemies.enemyCount = 0;
enemies.enemyArray = (Enemy *) malloc(sizeof(Enemy) * enemies.enemyCount); enemies.enemyArray = (Enemy *) malloc(sizeof(Enemy) * enemies.enemyCount);
enemyImage = al_load_bitmap(enemyImagePath); enemyImage = InitImage(enemyImagePath);
numberTable = al_load_bitmap(numbersImagePath); numberTable = InitImage(numbersImagePath);
enemyBulletImage = al_load_bitmap(bulletImagePath); enemyBulletImage = InitImage(bulletImagePath);
gameOverImage = al_load_bitmap(gameOverImagePath); gameOverImage = InitImage(gameOverImagePath);
SpawnEnemies(); SpawnEnemies();
} }
Image InitImage(const char *path)
{
Image result;
result.bitmap = al_load_bitmap(path);
result.size.x = al_get_bitmap_width(result.bitmap);
result.size.y = al_get_bitmap_height(result.bitmap);
return result;
}
void SpawnEnemies() void SpawnEnemies()
{ {
Vector2D enemySpawnVector; Vector2D enemySpawnVector;
@ -576,13 +598,17 @@ void LimitEnemies()
} }
} }
void DrawObject(Vector2D position, ALLEGRO_BITMAP *image, int flag) void DrawObject(Vector2D position, Image *image, int flag)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize;
InstantiateSize = image -> size;
/*
InstantiateSize.x = (float)al_get_bitmap_width(image); InstantiateSize.x = (float)al_get_bitmap_width(image);
InstantiateSize.y = (float)al_get_bitmap_height(image); InstantiateSize.y = (float)al_get_bitmap_height(image);
InstantiateSize.x = image -> size.x;
al_draw_scaled_bitmap(image, InstantiateSize.y = image -> size.y;
*/
al_draw_scaled_bitmap(image -> bitmap,
0, 0, InstantiateSize.x, InstantiateSize.y, 0, 0, InstantiateSize.x, InstantiateSize.y,
position.x - InstantiateSize.x / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier, position.x - InstantiateSize.x / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier,
InstantiateSize.x * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, flag); InstantiateSize.x * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, flag);
@ -595,24 +621,21 @@ void DrawObject(Vector2D position, ALLEGRO_BITMAP *image, int flag)
*/ */
void DrawNumber(Vector2D position, int number) void DrawNumber(Vector2D position, int number)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize = numberTable.size;
InstantiateSize.x = (float)al_get_bitmap_width(numberTable);
InstantiateSize.y = (float)al_get_bitmap_height(numberTable);
al_draw_scaled_bitmap(numberTable, al_draw_scaled_bitmap(numberTable.bitmap,
numberImageSize * number, 0, numberImageSize, InstantiateSize.y, numberImageSize * number, 0, numberImageSize, InstantiateSize.y,
position.x - numberImageSize / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier, position.x - numberImageSize / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier,
numberImageSize * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, 0); numberImageSize * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, 0);
} }
void DrawSizedObject(Vector2D position, ALLEGRO_BITMAP *image, int flag, float objectscreenSizeMultiplier) void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscreenSizeMultiplier)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize;
float sizeFactor = screenSizeMultiplier * objectscreenSizeMultiplier; float sizeFactor = screenSizeMultiplier * objectscreenSizeMultiplier;
InstantiateSize.x = (float)al_get_bitmap_width(image); InstantiateSize = image -> size;
InstantiateSize.y = (float)al_get_bitmap_height(image);
al_draw_scaled_bitmap(image, al_draw_scaled_bitmap(image -> bitmap,
0, 0, InstantiateSize.x, InstantiateSize.y, 0, 0, InstantiateSize.x, InstantiateSize.y,
position.x - InstantiateSize.x / 2 * sizeFactor, position.y - InstantiateSize.y / 2 * sizeFactor, position.x - InstantiateSize.x / 2 * sizeFactor, position.y - InstantiateSize.y / 2 * sizeFactor,
InstantiateSize.x * sizeFactor, InstantiateSize.y * sizeFactor, flag); InstantiateSize.x * sizeFactor, InstantiateSize.y * sizeFactor, flag);
@ -625,17 +648,17 @@ void DrawScreen()
/* Enemy Draw */ /* Enemy Draw */
for (i = 0; i < enemies.enemyCount; i++) for (i = 0; i < enemies.enemyCount; i++)
DrawObject((enemies.enemyArray + i) -> position, enemyImage, (enemies.enemyArray + i) -> velocity.x > 0 ? ALLEGRO_FLIP_HORIZONTAL : 0 ); DrawObject((enemies.enemyArray + i) -> position, &enemyImage, (enemies.enemyArray + i) -> velocity.x > 0 ? ALLEGRO_FLIP_HORIZONTAL : 0 );
/* Bullet Draw */ /* Bullet Draw */
for (i = 0; i < bullets.bulletCount; i++) for (i = 0; i < bullets.bulletCount; i++)
DrawObject((bullets.bulletArray + i) -> position, enemyBulletImage, 0); DrawObject((bullets.bulletArray + i) -> position, &enemyBulletImage, 0);
/* Player Draw */ /* Player Draw */
if(!isGameOver) if(!isGameOver)
DrawObject(player.position, player.playerImage, player.lookDirection == 1 ? ALLEGRO_FLIP_HORIZONTAL : 0); DrawObject(player.position, &player.image, player.lookDirection == 1 ? ALLEGRO_FLIP_HORIZONTAL : 0);
else else
DrawObject(halfScreen, gameOverImage, 0); DrawObject(halfScreen, &gameOverImage, 0);
DrawScore(); DrawScore();
DrawHighScore(); DrawHighScore();
@ -883,7 +906,7 @@ void PlayerShoot()
{ {
Vector2D shootDir; Vector2D shootDir;
Bullet *newBullet; Bullet *newBullet;
float offset = (al_get_bitmap_width(player.playerImage) + al_get_bitmap_width(enemyBulletImage) * 2.0 * screenSizeMultiplier); float offset = (player.image.size.x + enemyBulletImage.size.x * 2.0 * screenSizeMultiplier);
if(player.lookDirection != 1) if(player.lookDirection != 1)
offset = -offset; offset = -offset;
@ -913,7 +936,7 @@ void EnemyShoot()
Enemy *enemy; Enemy *enemy;
Bullet *bullet; Bullet *bullet;
int i; int i;
float offset = (al_get_bitmap_width(player.playerImage) + al_get_bitmap_width(enemyBulletImage) * 2.0 * screenSizeMultiplier); float offset = (player.image.size.x + enemyBulletImage.size.x * 2.0 * screenSizeMultiplier);
for (i = 0; i < enemies.enemyCount; i++) for (i = 0; i < enemies.enemyCount; i++)
{ {
@ -964,8 +987,8 @@ void BulletCollisions()
if(bullet -> isEnemyBullet == 1 && CheckCollision( if(bullet -> isEnemyBullet == 1 && CheckCollision(
&bullet -> position, &bullet -> position,
&player.position, &player.position,
enemyBulletImage, &enemyBulletImage,
player.playerImage &player.image
)) ))
{ {
RemoveBulletAtIndex(bulletCounter); RemoveBulletAtIndex(bulletCounter);
@ -979,8 +1002,8 @@ void BulletCollisions()
if(bullet -> isEnemyBullet == 0 && CheckCollision( if(bullet -> isEnemyBullet == 0 && CheckCollision(
&bullet -> position, &bullet -> position,
&enemy -> position, &enemy -> position,
enemyBulletImage, &enemyBulletImage,
enemyImage &enemyImage
)) ))
{ {
printf("Enemy-Bullet|EnemyRemove\n"); printf("Enemy-Bullet|EnemyRemove\n");
@ -998,8 +1021,8 @@ void BulletCollisions()
if(CheckCollision( if(CheckCollision(
&enemy -> position, &enemy -> position,
&player.position, &player.position,
enemyImage, &enemyImage,
player.playerImage &player.image
)) ))
{ {
RemoveEnemyAtIndex(enemyCounter); RemoveEnemyAtIndex(enemyCounter);
@ -1015,13 +1038,13 @@ void BulletCollisions()
void DestroyGame() void DestroyGame()
{ {
CheckHighScore(); CheckHighScore();
al_destroy_bitmap(enemyImage); al_destroy_bitmap(enemyImage.bitmap);
al_destroy_bitmap(enemyBulletImage); al_destroy_bitmap(enemyBulletImage.bitmap);
al_destroy_bitmap(gameOverImage); al_destroy_bitmap(gameOverImage.bitmap);
al_destroy_bitmap(numberTable); al_destroy_bitmap(numberTable.bitmap);
al_destroy_bitmap(player.image.bitmap);
al_destroy_sample(shootSound); al_destroy_sample(shootSound);
al_destroy_sample(enemyDieSound); al_destroy_sample(enemyDieSound);
al_destroy_bitmap(player.playerImage);
free(enemies.enemyArray); free(enemies.enemyArray);
} }