Script Optimization and Organization

This commit is contained in:
Asrın Doğan 2019-09-26 21:49:53 +03:00
parent f1620e54a0
commit 5596b53c02
1 changed files with 78 additions and 71 deletions

115
SynGame.c
View File

@ -43,6 +43,7 @@ typedef struct
typedef struct typedef struct
{ {
Vector2D originalSize;
Vector2D size; Vector2D size;
ALLEGRO_BITMAP *bitmap; ALLEGRO_BITMAP *bitmap;
} Image; } Image;
@ -143,6 +144,7 @@ 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;
Image gameOverImage; Image gameOverImage;
ALLEGRO_SAMPLE *BGM = NULL; ALLEGRO_SAMPLE *BGM = NULL;
@ -171,22 +173,31 @@ const char *displayName = "Syn Game";
const char *savePath = "Save.syn"; const char *savePath = "Save.syn";
const char *settingsPath = "Settings.syn"; const char *settingsPath = "Settings.syn";
const char *settingsFormat = "%d\n%d\n%d"; const char *settingsFormat = "%d\n%d\n%d";
int isFullscreen = 1; int isFullscreen = 1;
int settingsWidth = 1600; int settingsWidth = 1600;
int settingsHeight = 900; int settingsHeight = 900;
const Vector2D referenceScreenDimensions = {160, 90}; const Vector2D referenceScreenDimensions = {160, 90};
Vector2D screenDimensions = {0, 0}; Vector2D screenDimensions = {0, 0};
Vector2D scorePosition = {0, 0}; Vector2D scorePosition = {0, 0};
Vector2D highScorePosition = {0, 0}; Vector2D highScorePosition = {0, 0};
Vector2D timerPosition = {0, 0}; Vector2D timerPosition = {0, 0};
float screenSizeMultiplier; float screenSizeMultiplier;
float timeSinceStart; float timeSinceStart;
const float FPS = 60; const float FPS = 60;
double deltaTime; double deltaTime;
unsigned int enemyRespawnCounter = 0; unsigned int enemyRespawnCounter = 0;
unsigned int highScore = 0; unsigned int highScore = 0;
int enemyLimiter = 12; int enemyLimiter = 12;
Vector2D input; Vector2D input;
byte isRestart = 0; byte isRestart = 0;
byte isRunning = 1; byte isRunning = 1;
byte isGameOver = 0; byte isGameOver = 0;
@ -288,6 +299,20 @@ int main(int argc, char **argv)
return 0; return 0;
} }
Image InitImage(const char *path)
{
Image result;
result.bitmap = al_load_bitmap(path);
result.originalSize.x = al_get_bitmap_width(result.bitmap);
result.originalSize.y = al_get_bitmap_height(result.bitmap);
result.size.x = result.originalSize.x * screenSizeMultiplier;
result.size.y = result.originalSize.y * screenSizeMultiplier;
return result;
}
Vector2D NormalizeVector(Vector2D vector) Vector2D NormalizeVector(Vector2D vector)
{ {
Vector2D normalizedVector; Vector2D normalizedVector;
@ -324,19 +349,13 @@ byte isVectorExceedingLimits(Vector2D vector, Vector2D limits)
/* /*
Gets the shortest dimensions of both images, sums these dimension and divides by 2 to get minimum accepted distance. Gets the shortest dimensions of both images, sums these dimension and divides by 2 to get minimum accepted distance.
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 simple and optimized way I can think of for a game like this.
*/ */
byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Image *secondMap) byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Image *secondMap)
{ {
// Vector2D firstMap -> size;
// Vector2D secondMap -> size;
byte result; byte result;
float minDistance; float minDistance;
float distance; float distance;
// firstMap -> size.x = (float)al_get_bitmap_width(firstMap);
// firstMap -> size.y = (float)al_get_bitmap_height(firstMap);
// secondMap -> size.x = (float)al_get_bitmap_width(secondMap);
// secondMap -> size.y = (float)al_get_bitmap_height(secondMap);
minDistance = firstMap -> size.x > firstMap -> size.y ? minDistance = firstMap -> size.x > firstMap -> size.y ?
firstMap -> size.y : firstMap -> size.y :
@ -346,7 +365,7 @@ byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Im
secondMap -> size.y : secondMap -> size.y :
secondMap -> size.x; secondMap -> size.x;
minDistance *= screenSizeMultiplier * 0.5f; minDistance *= 0.5f;
distance = VectorDistanceBetween(*firstPos, *secondPos); distance = VectorDistanceBetween(*firstPos, *secondPos);
@ -384,9 +403,10 @@ char InitializeGameWindow()
} }
screenDimensions = (Vector2D){x, y}; screenDimensions = (Vector2D){x, y};
scorePosition = (Vector2D){x * (float)0.05, y * (float)0.05};
highScorePosition = (Vector2D){x * (float)0.95, y * (float)0.05}; scorePosition = (Vector2D){x * (float)0.05, y * (float)0.05}; /* Upper Left Position */
timerPosition = (Vector2D){x * (float)0.5 , y * (float)0.95}; highScorePosition = (Vector2D){x * (float)0.95, y * (float)0.05}; /* Upper Right Position */
timerPosition = (Vector2D){x * (float)0.5 , y * (float)0.95}; /* Bottom Center Position */
screenSizeMultiplier = screenDimensions.x / referenceScreenDimensions.x; screenSizeMultiplier = screenDimensions.x / referenceScreenDimensions.x;
display = al_create_display(screenDimensions.x, screenDimensions.y); display = al_create_display(screenDimensions.x, screenDimensions.y);
@ -465,20 +485,13 @@ 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 = InitImage(enemyImagePath); enemyImage = InitImage(enemyImagePath);
numberTable = InitImage(numbersImagePath); numberTable = InitImage(numbersImagePath);
enemyBulletImage = InitImage(bulletImagePath); enemyBulletImage = InitImage(bulletImagePath);
gameOverImage = InitImage(gameOverImagePath); gameOverImage = InitImage(gameOverImagePath);
SpawnEnemies();
}
Image InitImage(const char *path) SpawnEnemies();
{
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()
@ -523,11 +536,10 @@ void SpawnEnemies()
void CheckBullets() void CheckBullets()
{ {
int i = 0; int i;
int j = 0; int j;
for (; i < bullets.bulletCount; i++) for (i = 0; i < bullets.bulletCount; i++)
{
if(isVectorExceedingLimits((bullets.bulletArray + i) -> position, screenDimensions)) if(isVectorExceedingLimits((bullets.bulletArray + i) -> position, screenDimensions))
{ {
for (j = i; j < bullets.bulletCount - 1; j++) for (j = i; j < bullets.bulletCount - 1; j++)
@ -537,7 +549,6 @@ void CheckBullets()
bullets.bulletArray = (Bullet *) realloc(bullets.bulletArray, sizeof(Bullet) * bullets.bulletCount); bullets.bulletArray = (Bullet *) realloc(bullets.bulletArray, sizeof(Bullet) * bullets.bulletCount);
} }
} }
}
void RemoveBulletAtIndex(int index) void RemoveBulletAtIndex(int index)
{ {
@ -563,9 +574,9 @@ void RemoveEnemyAtIndex(int index)
void CheckEnemies() void CheckEnemies()
{ {
int i = 0; int i;
for (; i < enemies.enemyCount; i++) for (i = 0; i < enemies.enemyCount; i++)
if(isVectorExceedingLimits((enemies.enemyArray + i) -> position, screenDimensions)) if(isVectorExceedingLimits((enemies.enemyArray + i) -> position, screenDimensions))
RemoveEnemyAtIndex(i); RemoveEnemyAtIndex(i);
@ -574,11 +585,11 @@ void CheckEnemies()
void MoveEnemies() void MoveEnemies()
{ {
int i = 0;
Vector2D velocity; Vector2D velocity;
float speed; float speed;
int i;
for (; i < enemies.enemyCount; i++) for (i = 0; i < enemies.enemyCount; i++)
{ {
speed = (enemies.enemyArray + i) -> moveSpeed; speed = (enemies.enemyArray + i) -> moveSpeed;
velocity = (enemies.enemyArray + i) -> velocity; velocity = (enemies.enemyArray + i) -> velocity;
@ -601,17 +612,14 @@ void LimitEnemies()
void DrawObject(Vector2D position, Image *image, int flag) void DrawObject(Vector2D position, Image *image, int flag)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize;
Vector2D originalSize;
InstantiateSize = image -> size; InstantiateSize = image -> size;
/* originalSize = image -> originalSize;
InstantiateSize.x = (float)al_get_bitmap_width(image);
InstantiateSize.y = (float)al_get_bitmap_height(image);
InstantiateSize.x = image -> size.x;
InstantiateSize.y = image -> size.y;
*/
al_draw_scaled_bitmap(image -> bitmap, al_draw_scaled_bitmap(image -> bitmap,
0, 0, InstantiateSize.x, InstantiateSize.y, 0, 0, originalSize.x, originalSize.y,
position.x - InstantiateSize.x / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier, position.x - InstantiateSize.x / 2, position.y - InstantiateSize.y / 2,
InstantiateSize.x * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, flag); InstantiateSize.x, InstantiateSize.y, flag);
} }
/* /*
@ -621,12 +629,15 @@ void DrawObject(Vector2D position, Image *image, int flag)
*/ */
void DrawNumber(Vector2D position, int number) void DrawNumber(Vector2D position, int number)
{ {
Vector2D InstantiateSize = numberTable.size; Vector2D InstantiateSize;
Vector2D originalSize;
InstantiateSize = numberTable.size;
originalSize = numberTable.originalSize;
al_draw_scaled_bitmap(numberTable.bitmap, al_draw_scaled_bitmap(numberTable.bitmap,
numberImageSize * number, 0, numberImageSize, InstantiateSize.y, numberImageSize * number, 0, numberImageSize, originalSize.y,
position.x - numberImageSize / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier, position.x - numberImageSize / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2,
numberImageSize * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, 0); numberImageSize * screenSizeMultiplier, InstantiateSize.y, 0);
} }
void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscreenSizeMultiplier) void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscreenSizeMultiplier)
@ -670,10 +681,9 @@ void DrawScore()
unsigned int processedScore = player.score; unsigned int processedScore = player.score;
char digit; char digit;
Vector2D spawnPosition; Vector2D spawnPosition;
int i = scoreDigitLimit - 1; int i;
/* while (processedScore >= 1 && i > 0) */ for(i = scoreDigitLimit - 1; i >= 0; i--)
while (i >= 0)
{ {
spawnPosition = scorePosition; spawnPosition = scorePosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
@ -681,7 +691,6 @@ void DrawScore()
digit = processedScore % 10; digit = processedScore % 10;
processedScore = (int)(processedScore / 10); processedScore = (int)(processedScore / 10);
DrawNumber(spawnPosition, digit); DrawNumber(spawnPosition, digit);
i--;
} }
} }
@ -690,10 +699,9 @@ void DrawHighScore()
unsigned int processedScore = highScore; unsigned int processedScore = highScore;
char digit; char digit;
Vector2D spawnPosition; Vector2D spawnPosition;
int i = 0; int i;
/* while (processedScore >= 1 && i > 0) */ for(i = 0; i < scoreDigitLimit; i++)
while (i < scoreDigitLimit)
{ {
spawnPosition = highScorePosition; spawnPosition = highScorePosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
@ -701,7 +709,6 @@ void DrawHighScore()
digit = processedScore % 10; digit = processedScore % 10;
processedScore = (int)(processedScore / 10); processedScore = (int)(processedScore / 10);
DrawNumber(spawnPosition, digit); DrawNumber(spawnPosition, digit);
i++;
} }
} }
@ -865,8 +872,8 @@ char DealDamage(char *health)
void BulletMovement() void BulletMovement()
{ {
Bullet *bullet; Bullet *bullet;
int i = 0; int i;
for (; i < bullets.bulletCount; i++) for (i = 0; i < bullets.bulletCount; i++)
{ {
bullet = (bullets.bulletArray + i); bullet = (bullets.bulletArray + i);
bullet -> position.x += bullet -> velocity.x; bullet -> position.x += bullet -> velocity.x;
@ -906,7 +913,7 @@ void PlayerShoot()
{ {
Vector2D shootDir; Vector2D shootDir;
Bullet *newBullet; Bullet *newBullet;
float offset = (player.image.size.x + enemyBulletImage.size.x * 2.0 * screenSizeMultiplier); float offset = (player.image.size.x + enemyBulletImage.size.x * 2.0);
if(player.lookDirection != 1) if(player.lookDirection != 1)
offset = -offset; offset = -offset;
@ -936,7 +943,7 @@ void EnemyShoot()
Enemy *enemy; Enemy *enemy;
Bullet *bullet; Bullet *bullet;
int i; int i;
float offset = (player.image.size.x + enemyBulletImage.size.x * 2.0 * screenSizeMultiplier); float offset = (player.image.size.x + enemyBulletImage.size.x * 2.0);
for (i = 0; i < enemies.enemyCount; i++) for (i = 0; i < enemies.enemyCount; i++)
{ {