Compare commits

..

No commits in common. "master" and "1.0" have entirely different histories.
master ... 1.0

5 changed files with 725 additions and 827 deletions

4
.gitignore vendored
View File

@ -4,13 +4,13 @@
*.a *.a
*.dll *.dll
*.syn *.syn
!allegro_audio-5.0.10-md.dll
!allegro_acodec-5.0.10-md.dll !allegro_acodec-5.0.10-md.dll
!allegro_audio-5.0.10-md.dll
!allegro_image-5.0.10-md.dll !allegro_image-5.0.10-md.dll
!allegro_primitives-5.0.10-md.dll !allegro_primitives-5.0.10-md.dll
!allegro-5.0.10-md.dll !allegro-5.0.10-md.dll
!liballegro_audio-5.0.10-md.a !liballegro_audio-5.0.10-md.a
!liballegro_acodec-5.0.10-md.a !liballegro_acodec-5.0.10-md.a
!liballegro_image-5.0.10-md.a !liballegro_image-5.0.10-md.a
!liballegro_dialog-5.0.10-md.a
!liballegro_primitives-5.0.10-md.a !liballegro_primitives-5.0.10-md.a
!liballegro-5.0.10-md.a

View File

@ -1,24 +1,26 @@
# SynGame # SynGame
Simple Shoot 'Em Up game for Windows. Simple Shoot 'Em Up game.
Keys:\ Keys:
Move Up - Up Arrow\ Move Up - Up Arrow
Move Right - Right Arrow\ Move Right - Right Arrow
Move Down - Down Arrow\ Move Down - Down Arrow
Move Left - Left Arrow\ Move Left - Left Arrow
Shoot - Space\ Shoot - Space
Restart - R\ Restart - R
Exit - Escape(ESC) Exit - Escape(ESC)
Settings.syn can be opened with a text editor.\ Settings.syn can be opened with a text editor.
Settings Format:\ Format:
First line: Screen Mode, 1 is Fullscreen, 0 is Windowed (Default Value = 1)\ First line: Screen Mode, 1 is Fullscreen, 0 is Windowed (Default Value = 1)
Second line: Screen Width, Windowed Mode Only (Default Value = 1600)\ Second line: Screen Width, Windowed Mode Only (Default Value = 1600)
Third line: Screen Height, Windowed Mode Only (Default Value = 900)\ Third line: Screen Height, Windowed Mode Only (Default Value = 900)
Fourth line: Enemy Count Limiter, there will be no more enemies than this number on the screen (Default Value = 12) Fourth line: Enemy Count Limiter, there will be no more enemies than this number on the screen (Default Value = 12)
Exe without console window\ Exe without console window
gcc SynGame.c -o SynGame.exe "allegro\lib\liballegro-5.0.10-md.a" "allegro\lib\liballegro_audio-5.0.10-md.a" "allegro\lib\liballegro_acodec-5.0.10-md.a" "allegro\lib\liballegro_image-5.0.10-md.a" "allegro\lib\liballegro_dialog-5.0.10-md.a" "allegro\lib\liballegro_primitives-5.0.10-md.a" --machine-windows gcc SynGame.c -o SynGame.exe "allegro\lib\liballegro-5.0.10-md.a" "allegro\lib\liballegro_audio-5.0.10-md.a" "allegro\lib\liballegro_acodec-5.0.10-md.a" "allegro\lib\liballegro_image-5.0.10-md.a" "allegro\lib\liballegro_dialog-5.0.10-md.a" "allegro\lib\liballegro_primitives-5.0.10-md.a" --machine-windows
Exe with console window\ Exe with console window
gcc SynGame.c -o SynGame.exe "allegro\lib\liballegro-5.0.10-md.a" "allegro\lib\liballegro_audio-5.0.10-md.a" "allegro\lib\liballegro_acodec-5.0.10-md.a" "allegro\lib\liballegro_image-5.0.10-md.a" "allegro\lib\liballegro_primitives-5.0.10-md.a" gcc SynGame.c -o SynGame.exe "allegro\lib\liballegro-5.0.10-md.a" "allegro\lib\liballegro_audio-5.0.10-md.a" "allegro\lib\liballegro_acodec-5.0.10-md.a" "allegro\lib\liballegro_image-5.0.10-md.a" "allegro\lib\liballegro_primitives-5.0.10-md.a"

Binary file not shown.

452
SynGame.c
View File

@ -1,6 +1,8 @@
/* /*
Author: Syntriax <Syntriax@gmail.com> Author: Asrın "Syntriax" Doğan
Start: 05.09.2019 Date: 10.09.2019
Mail: asrindogan99@gmail.com
Simple Shoot 'Em Up game. Simple Shoot 'Em Up game.
Keys: Keys:
@ -21,7 +23,6 @@
#include "allegro\include\allegro5\allegro_acodec.h" #include "allegro\include\allegro5\allegro_acodec.h"
#include "allegro\include\allegro5\allegro_image.h" #include "allegro\include\allegro5\allegro_image.h"
#include "allegro\include\allegro5\allegro_primitives.h" #include "allegro\include\allegro5\allegro_primitives.h"
#define playerSpeed 0.75 #define playerSpeed 0.75
#define enemySpeed 0.1 #define enemySpeed 0.1
#define initialPlayerHealth 4 #define initialPlayerHealth 4
@ -40,13 +41,6 @@ typedef struct
float y; float y;
} Vector2D; } Vector2D;
typedef struct
{
Vector2D originalSize;
Vector2D size;
ALLEGRO_BITMAP *bitmap;
} Image;
typedef struct typedef struct
{ {
Vector2D position; Vector2D position;
@ -81,33 +75,25 @@ struct
Vector2D position; Vector2D position;
char health; char health;
float moveSpeed; float moveSpeed;
char lookDirection; byte lookDirection;
int shootPerSecond; int shootPerSecond;
float shootCooldown; float shootCooldown;
unsigned int killedEnemyCount; unsigned int killedEnemyCount;
unsigned int score; unsigned int score;
Image image; ALLEGRO_BITMAP *playerImage;
} player; } player;
char InitializeGameWindow();
char InitializeGame();
char InitializeEnemies();
char DealDamage(char *health);
char isVectorExceedingLimits(Vector2D vector, Vector2D limits);
char CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Image *secondMap);
void Update();
void SpawnEnemies(); void SpawnEnemies();
void CheckBullets(); void CheckBullets();
void RemoveBulletAtIndex(int index); void RemoveBulletAtIndex(int index);
void RemoveEnemyAtIndex(int index); void RemoveEnemyAtIndex(int index);
void CheckEnemies(); void CheckEnemies();
void MoveEnemies(); void MoveEnemies();
void LimitEnemies(); void InitializeEnemies();
void DrawObject(Vector2D position, Image *image, int flag); void DestroyGame();
void DrawNumber(Vector2D position, int number); void DrawObject(Vector2D position, ALLEGRO_BITMAP *image, int flag);
void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscreenSizeMultiplier); void DrawSizedObject(Vector2D position, ALLEGRO_BITMAP *image, int flag, float objectscreenSizeMultiplier);
void DrawScreen(); void DrawScreen();
void DrawScore(); void DrawScore();
void DrawHighScore(); void DrawHighScore();
@ -115,92 +101,70 @@ void DrawTimer();
void CheckHighScore(); void CheckHighScore();
void GetHighScore(); void GetHighScore();
void GetSettings(); void GetSettings();
void CalculateScore(); void DrawNumber(Vector2D position, int number);
void Inputs(); void Inputs();
void PlayerMovement(); void PlayerMovement();
void ClampPlayerPositionToScreenDimensions(); void ClampPlayerPositionToScreenDimensions();
void BulletMovement();
void ShootSoundEffect(); void ShootSoundEffect();
void DieSoundEffect(); void DieSoundEffect();
void PlayerShootCheck();
void PlayerShoot(); void PlayerShoot();
void EnemyShoot(); void EnemyShoot();
void BulletMovement();
void BulletCollisions(); void BulletCollisions();
void DestroyGame(); void Update();
void DestroyGameWindow(); void DestroyGameWindow();
float VectorMagnitude(Vector2D vector); float VectorMagnitude(Vector2D vector);
float VectorDistanceBetween(Vector2D vectorFirst, Vector2D vectorSecond); float VectorDistanceBetween(Vector2D vectorFirst, Vector2D vectorSecond);
byte isVectorExceedingLimits(Vector2D vector, Vector2D limits);
byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, ALLEGRO_BITMAP *firstMap, ALLEGRO_BITMAP *secondMap);
char InitializeGameWindow();
char InitializeGame();
char DealDamage(char *health);
Vector2D NormalizeVector(Vector2D vector); Vector2D NormalizeVector(Vector2D vector);
Image InitImage(const char *path);
ALLEGRO_KEYBOARD_STATE keyboardState; ALLEGRO_KEYBOARD_STATE keyboardState;
ALLEGRO_DISPLAY_MODE disp_data; ALLEGRO_DISPLAY_MODE disp_data;
ALLEGRO_COLOR backgroundColor; 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;
Image enemyImage; ALLEGRO_BITMAP *enemyImage = NULL;
Image enemyBulletImage; ALLEGRO_BITMAP *enemyBulletImage = NULL;
ALLEGRO_SAMPLE *enemyDieSound = NULL; ALLEGRO_SAMPLE *enemyDieSound = NULL;
ALLEGRO_SAMPLE_ID enemyDieSoundID; ALLEGRO_SAMPLE_ID enemyDieSoundID;
Image numberTable; ALLEGRO_BITMAP *numberTable = NULL;
const char *playerImagePath = "Images/Player.png";
const char *enemyImagePath = "Images/Enemy.png";
const char *bulletImagePath = "Images/Bullet.png";
const char *gameOverImagePath = "Images/GameOver.png";
const char *numbersImagePath = "Images/Numbers.png";
const char *dieSoundPath = "Sounds/Die.wav";
const char *bgmSoundPath = "Sounds/Background.wav";
const char *shootSoundPath = "Sounds/Shoot.wav";
const char *displayName = "Syn Game"; 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;
char isRestart = 0; byte isRunning = 1;
char isRunning = 1; byte isGameOver = 0;
char isGameOver = 0;
void Update() void Update()
{ {
@ -225,16 +189,28 @@ void Update()
printf("BulletCollisions();\n"); printf("BulletCollisions();\n");
BulletCollisions(); BulletCollisions();
printf("PlayerShootCheck();\n"); if(player.shootCooldown < 0.0f)
PlayerShootCheck(); {
if(al_key_down(&keyboardState, ALLEGRO_KEY_SPACE))
printf("LimitEnemies();\n"); {
LimitEnemies(); printf("PlayerShoot();\n");
PlayerShoot();
}
}
else
player.shootCooldown -= deltaTime;
timeSinceStart += deltaTime; timeSinceStart += deltaTime;
player.score = (int)(timeSinceStart * timeSinceStart) * (player.killedEnemyCount + 1);
printf("CalculateScore();\n"); /* To limit the enemies on the screen */
CalculateScore(); if(enemies.enemyLimit != enemyLimiter)
{
enemies.enemyLimit = initialEnemyLimit + (int)(timeSinceStart / 10);
if(enemies.enemyCount > enemyLimiter)
enemies.enemyLimit = enemyLimiter;
}
} }
else if(al_key_down(&keyboardState, ALLEGRO_KEY_R)) else if(al_key_down(&keyboardState, ALLEGRO_KEY_R))
isRestart = 1; isRestart = 1;
@ -255,6 +231,7 @@ void Update()
printf("DrawScreen();\n"); printf("DrawScreen();\n");
DrawScreen(); DrawScreen();
al_flip_display(); al_flip_display();
} }
@ -290,8 +267,7 @@ int main(int argc, char **argv)
} }
DestroyGame(); DestroyGame();
} } while (isRestart);
while (isRestart);
DestroyGameWindow(); DestroyGameWindow();
@ -299,26 +275,10 @@ 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;
float magnitude; float magnitude = sqrt(vector.x * vector.x + vector.y * vector.y);
magnitude = sqrt(vector.x * vector.x + vector.y * vector.y);
if(vector.x == 0.0 && vector.y == 0.0) if(vector.x == 0.0 && vector.y == 0.0)
return vector; return vector;
@ -342,32 +302,33 @@ float VectorDistanceBetween(Vector2D vectorFirst, Vector2D vectorSecond)
return VectorMagnitude(difference); return VectorMagnitude(difference);
} }
char isVectorExceedingLimits(Vector2D vector, Vector2D limits) byte isVectorExceedingLimits(Vector2D vector, Vector2D limits)
{ {
char result = vector.x > limits.x || vector.x < 0 || vector.y > limits.y || vector.y < 0; byte result = vector.x > limits.x || vector.x < 0 || vector.y > limits.y || vector.y < 0;
return result; return result;
} }
/* /*
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 simple and 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.
*/ */
char CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Image *secondMap) byte CheckCollision(Vector2D *firstPos, Vector2D *secondPos, ALLEGRO_BITMAP *firstMap, ALLEGRO_BITMAP *secondMap)
{ {
char result; Vector2D firstImageSize;
Vector2D secondImageSize;
byte result;
float minDistance; float minDistance;
float distance; float distance;
firstImageSize.x = (float)al_get_bitmap_width(firstMap);
firstImageSize.y = (float)al_get_bitmap_height(firstMap);
secondImageSize.x = (float)al_get_bitmap_width(secondMap);
secondImageSize.y = (float)al_get_bitmap_height(secondMap);
minDistance = firstMap -> size.x > firstMap -> size.y ? minDistance = firstImageSize.x > firstImageSize.y ? firstImageSize.y : firstImageSize.x;
firstMap -> size.y : minDistance += secondImageSize.x > secondImageSize.y ? secondImageSize.y : secondImageSize.x;
firstMap -> size.x;
minDistance += secondMap -> size.x > secondMap -> size.y ? minDistance *= screenSizeMultiplier * 0.5f;
secondMap -> size.y :
secondMap -> size.x;
minDistance *= 0.5f;
distance = VectorDistanceBetween(*firstPos, *secondPos); distance = VectorDistanceBetween(*firstPos, *secondPos);
@ -377,10 +338,8 @@ char CheckCollision(Vector2D *firstPos, Vector2D *secondPos, Image *firstMap, Im
char InitializeGameWindow() char InitializeGameWindow()
{ {
float x; float x = 0.0f;
float y; float y = 0.0f;
x = 0.0f;
y = 0.0f;
if(!al_init() || if(!al_init() ||
!al_init_primitives_addon() || !al_init_primitives_addon() ||
@ -407,10 +366,9 @@ char InitializeGameWindow()
} }
screenDimensions = (Vector2D){x, y}; screenDimensions = (Vector2D){x, y};
scorePosition = (Vector2D){x * (float)0.05, y * (float)0.05};
scorePosition = (Vector2D){x * (float)0.05, y * (float)0.05}; /* Upper Left Position */ highScorePosition = (Vector2D){x * (float)0.95, y * (float)0.05};
highScorePosition = (Vector2D){x * (float)0.95, y * (float)0.05}; /* Upper Right Position */ timerPosition = (Vector2D){x * (float)0.5 , y * (float)0.95};
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);
@ -441,30 +399,21 @@ char InitializeGameWindow()
al_set_window_title(display, displayName); al_set_window_title(display, displayName);
backgroundColor.a = 1; backgroundColor.a = 1;
backgroundColor.r = .1; backgroundColor.a = 0;
backgroundColor.g = .1; backgroundColor.a = 0;
backgroundColor.b = .1; backgroundColor.a = 0;
/* BGM is an exception since I don't want to it to restart itself every restart */ /* BGM is an exception since I don't want to it to restart itself every restart */
BGM = al_load_sample(bgmSoundPath); BGM = al_load_sample("Sounds/Background.wav");
if(BGM == NULL)
return 0;
al_play_sample(BGM, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL); al_play_sample(BGM, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
return 1;
} }
char InitializeGame() char InitializeGame()
{ {
shootSound = al_load_sample(shootSoundPath); shootSound = al_load_sample("Sounds/Shoot.wav");
enemyDieSound = al_load_sample(dieSoundPath); enemyDieSound = al_load_sample("Sounds/Die.wav");
if(shootSound == NULL || enemyDieSound == NULL) InitializeEnemies();
return 0;
if(!InitializeEnemies())
return 0;
GetHighScore(); GetHighScore();
/* Player Initialization */ /* Player Initialization */
@ -473,9 +422,9 @@ char InitializeGame()
player.moveSpeed = playerSpeed * screenSizeMultiplier; player.moveSpeed = playerSpeed * screenSizeMultiplier;
player.shootPerSecond = 10; player.shootPerSecond = 10;
player.health = initialPlayerHealth; player.health = initialPlayerHealth;
player.image = InitImage(playerImagePath); player.playerImage = al_load_bitmap("Images/Player.png");
if(player.image.bitmap == NULL || if(player.playerImage == NULL ||
shootSound == NULL || shootSound == NULL ||
enemyDieSound == NULL) enemyDieSound == NULL)
return 0; return 0;
@ -493,30 +442,16 @@ char InitializeGame()
return 1; return 1;
} }
char InitializeEnemies() 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("Images/Enemy.png");
enemyImage = InitImage(enemyImagePath); numberTable = al_load_bitmap("Images/Numbers.png");
numberTable = InitImage(numbersImagePath); enemyBulletImage = al_load_bitmap("Images/Bullet.png");
enemyBulletImage = InitImage(bulletImagePath); gameOverImage = al_load_bitmap("Images/GameOver.png");
gameOverImage = InitImage(gameOverImagePath);
if( enemyImage.bitmap == NULL ||
numberTable.bitmap == NULL ||
enemyBulletImage.bitmap == NULL ||
gameOverImage.bitmap == NULL )
return 0;
SpawnEnemies(); SpawnEnemies();
return 1;
}
char DealDamage(char *health)
{
return --*health <= 0;
} }
void SpawnEnemies() void SpawnEnemies()
@ -534,7 +469,7 @@ void SpawnEnemies()
{ {
/* enemyRespawnCounter is just for making the value of rand() more randomized */ /* enemyRespawnCounter is just for making the value of rand() more randomized */
srand(time(0) + enemyRespawnCounter); srand(time(0) + enemyRespawnCounter);
randomNumber = rand(); randomNumber = rand() * enemies.enemyCount;
enemies.enemyCount++; enemies.enemyCount++;
enemies.enemyArray = (Enemy *) realloc(enemies.enemyArray, sizeof(Enemy) * enemies.enemyCount); enemies.enemyArray = (Enemy *) realloc(enemies.enemyArray, sizeof(Enemy) * enemies.enemyCount);
@ -553,7 +488,7 @@ void SpawnEnemies()
enemySpawnVector.y = enemyVelocity.y > 0 ? 0 : screenDimensions.y; enemySpawnVector.y = enemyVelocity.y > 0 ? 0 : screenDimensions.y;
enemy -> position = enemySpawnVector; enemy -> position = enemySpawnVector;
enemy -> fireCooldown = 1.0 / (rand() % 5) + 2.0; enemy -> fireCooldown = 01.0 / (rand() % 5) + 2.0;
enemyRespawnCounter++; enemyRespawnCounter++;
} }
@ -561,10 +496,11 @@ void SpawnEnemies()
void CheckBullets() void CheckBullets()
{ {
int i; int i = 0;
int j; int j = 0;
for (i = 0; i < bullets.bulletCount; i++) for (; 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++)
@ -574,6 +510,7 @@ 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)
{ {
@ -599,9 +536,9 @@ void RemoveEnemyAtIndex(int index)
void CheckEnemies() void CheckEnemies()
{ {
int i; int i = 0;
for (i = 0; i < enemies.enemyCount; i++) for (; i < enemies.enemyCount; i++)
if(isVectorExceedingLimits((enemies.enemyArray + i) -> position, screenDimensions)) if(isVectorExceedingLimits((enemies.enemyArray + i) -> position, screenDimensions))
RemoveEnemyAtIndex(i); RemoveEnemyAtIndex(i);
@ -610,11 +547,11 @@ void CheckEnemies()
void MoveEnemies() void MoveEnemies()
{ {
int i = 0;
Vector2D velocity; Vector2D velocity;
float speed; float speed;
int i;
for (i = 0; i < enemies.enemyCount; i++) for (; i < enemies.enemyCount; i++)
{ {
speed = (enemies.enemyArray + i) -> moveSpeed; speed = (enemies.enemyArray + i) -> moveSpeed;
velocity = (enemies.enemyArray + i) -> velocity; velocity = (enemies.enemyArray + i) -> velocity;
@ -622,60 +559,38 @@ void MoveEnemies()
(enemies.enemyArray + i) -> position.y += velocity.y * speed; (enemies.enemyArray + i) -> position.y += velocity.y * speed;
} }
} }
void DrawObject(Vector2D position, ALLEGRO_BITMAP *image, int flag)
void LimitEnemies()
{
if(enemies.enemyLimit != enemyLimiter)
{
enemies.enemyLimit = initialEnemyLimit + (int)(timeSinceStart / 10);
if(enemies.enemyCount > enemyLimiter)
enemies.enemyLimit = enemyLimiter;
}
}
void DrawObject(Vector2D position, Image *image, int flag)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize;
Vector2D originalSize; InstantiateSize.x = (float)al_get_bitmap_width(image);
InstantiateSize.y = (float)al_get_bitmap_height(image);
InstantiateSize = image -> size; al_draw_scaled_bitmap(image,
originalSize = image -> originalSize; 0, 0, InstantiateSize.x, InstantiateSize.y,
position.x - InstantiateSize.x / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier,
al_draw_scaled_bitmap(image -> bitmap, InstantiateSize.x * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, flag);
0, 0, originalSize.x, originalSize.y,
position.x - InstantiateSize.x / 2, position.y - InstantiateSize.y / 2,
InstantiateSize.x, InstantiateSize.y, flag);
} }
/*
Allegra Fonts is not working so I use this for displaying numbers
Special Characters:
10 = :
*/
void DrawNumber(Vector2D position, int number) void DrawNumber(Vector2D position, int number)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize;
Vector2D originalSize; InstantiateSize.x = (float)al_get_bitmap_width(numberTable);
InstantiateSize.y = (float)al_get_bitmap_height(numberTable);
InstantiateSize = numberTable.size; al_draw_scaled_bitmap(numberTable,
originalSize = numberTable.originalSize; numberImageSize * number, 0, numberImageSize, InstantiateSize.y,
position.x - numberImageSize / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2 * screenSizeMultiplier,
al_draw_scaled_bitmap(numberTable.bitmap, numberImageSize * screenSizeMultiplier, InstantiateSize.y * screenSizeMultiplier, 0);
numberImageSize * number, 0, numberImageSize, originalSize.y,
position.x - numberImageSize / 2 * screenSizeMultiplier, position.y - InstantiateSize.y / 2,
numberImageSize * screenSizeMultiplier, InstantiateSize.y, 0);
} }
void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscreenSizeMultiplier) void DrawSizedObject(Vector2D position, ALLEGRO_BITMAP *image, int flag, float objectscreenSizeMultiplier)
{ {
Vector2D InstantiateSize; Vector2D InstantiateSize;
float sizeFactor; float sizeFactor = screenSizeMultiplier * objectscreenSizeMultiplier;
InstantiateSize.x = (float)al_get_bitmap_width(image);
InstantiateSize.y = (float)al_get_bitmap_height(image);
sizeFactor = screenSizeMultiplier * objectscreenSizeMultiplier; al_draw_scaled_bitmap(image,
InstantiateSize = image -> size;
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);
@ -683,23 +598,22 @@ void DrawSizedObject(Vector2D position, Image *image, int flag, float objectscre
void DrawScreen() void DrawScreen()
{ {
int i; int i = 0;
Vector2D halfScreen; Vector2D halfScreen = {screenDimensions.x / 2, screenDimensions.y / 2};
halfScreen = (Vector2D){screenDimensions.x / 2, screenDimensions.y / 2};
/* 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.image, player.lookDirection == 1 ? ALLEGRO_FLIP_HORIZONTAL : 0); DrawObject(player.position, player.playerImage, player.lookDirection == 1 ? ALLEGRO_FLIP_HORIZONTAL : 0);
else else
DrawObject(halfScreen, &gameOverImage, 0); DrawObject(halfScreen, gameOverImage, 0);
DrawScore(); DrawScore();
DrawHighScore(); DrawHighScore();
@ -708,14 +622,13 @@ void DrawScreen()
void DrawScore() void DrawScore()
{ {
unsigned int processedScore; unsigned int processedScore = player.score;
char digit; char digit;
Vector2D spawnPosition; Vector2D spawnPosition;
int i; int i = scoreDigitLimit - 1;
processedScore = player.score; /* while (processedScore >= 1 && i > 0) */
while (i >= 0)
for(i = scoreDigitLimit - 1; i >= 0; i--)
{ {
spawnPosition = scorePosition; spawnPosition = scorePosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
@ -723,19 +636,19 @@ void DrawScore()
digit = processedScore % 10; digit = processedScore % 10;
processedScore = (int)(processedScore / 10); processedScore = (int)(processedScore / 10);
DrawNumber(spawnPosition, digit); DrawNumber(spawnPosition, digit);
i--;
} }
} }
void DrawHighScore() void DrawHighScore()
{ {
unsigned int processedScore; unsigned int processedScore = highScore;
char digit; char digit;
Vector2D spawnPosition; Vector2D spawnPosition;
int i; int i = 0;
processedScore = highScore; /* while (processedScore >= 1 && i > 0) */
while (i < scoreDigitLimit)
for(i = 0; i < scoreDigitLimit; i++)
{ {
spawnPosition = highScorePosition; spawnPosition = highScorePosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
@ -743,26 +656,25 @@ void DrawHighScore()
digit = processedScore % 10; digit = processedScore % 10;
processedScore = (int)(processedScore / 10); processedScore = (int)(processedScore / 10);
DrawNumber(spawnPosition, digit); DrawNumber(spawnPosition, digit);
i++;
} }
} }
void DrawTimer() void DrawTimer()
{ {
int seconds; int seconds = (int)timeSinceStart % 60;
int minutes; int minutes = (timeSinceStart - seconds) / 60;
char digit; char digit;
Vector2D spawnPosition; Vector2D spawnPosition;
int i; int i = -timerDigitLimit / 2;
seconds = (int)timeSinceStart % 60;
minutes = (timeSinceStart - seconds) / 60;
spawnPosition = timerPosition;
i = -timerDigitLimit / 2;
/* while (processedScore >= 1 && i > 0) */
while (i < 0) while (i < 0)
{ {
spawnPosition = timerPosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
spawnPosition.x = timerPosition.x - screenSizeMultiplier * (numberImageSize + 1) * i; spawnPosition.x -= screenSizeMultiplier * (numberImageSize + 1) * i;
digit = seconds % 10; digit = seconds % 10;
seconds = (int)(seconds / 10); seconds = (int)(seconds / 10);
@ -770,15 +682,18 @@ void DrawTimer()
i++; i++;
} }
spawnPosition = timerPosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
spawnPosition.x = timerPosition.x - screenSizeMultiplier * (numberImageSize + 1) * i; spawnPosition.x -= screenSizeMultiplier * (numberImageSize + 1) * i;
DrawNumber(spawnPosition, colon); DrawNumber(spawnPosition, colon);
i++; i++;
while (i < (timerDigitLimit / 2) + 1) while (i < (timerDigitLimit / 2) + 1)
{ {
spawnPosition = timerPosition;
/* numberImageSize + 1 is because 1 pixel space between digits */ /* numberImageSize + 1 is because 1 pixel space between digits */
spawnPosition.x = timerPosition.x - screenSizeMultiplier * (numberImageSize + 1) * i; spawnPosition.x -= screenSizeMultiplier * (numberImageSize + 1) * i;
if(i == 0) if(i == 0)
{ {
@ -816,8 +731,8 @@ void CheckHighScore()
void GetHighScore() void GetHighScore()
{ {
FILE *saveFile = fopen(savePath, "rb");
printf("Getting Highscore\n"); printf("Getting Highscore\n");
FILE *saveFile = fopen(savePath, "rb");
if(saveFile == NULL) if(saveFile == NULL)
{ {
printf("!!!!Error Reading Highscore!!!!\n"); printf("!!!!Error Reading Highscore!!!!\n");
@ -831,8 +746,8 @@ void GetHighScore()
void GetSettings() void GetSettings()
{ {
FILE *settingsFile = fopen(settingsPath, "r");
printf("Getting Settings\n"); printf("Getting Settings\n");
FILE *settingsFile = fopen(settingsPath, "r");
if(settingsFile == NULL) if(settingsFile == NULL)
{ {
printf("!!!!Error Reading Settings!!!!\n"); printf("!!!!Error Reading Settings!!!!\n");
@ -851,11 +766,6 @@ void GetSettings()
fclose(settingsFile); fclose(settingsFile);
} }
void CalculateScore()
{
player.score = (int)(timeSinceStart * timeSinceStart) * (player.killedEnemyCount + 1);
}
void Inputs() void Inputs()
{ {
input.x = 0; input.x = 0;
@ -877,6 +787,7 @@ void Inputs()
player.lookDirection = input.x; player.lookDirection = input.x;
} }
input = NormalizeVector(input); input = NormalizeVector(input);
} }
@ -901,6 +812,23 @@ void ClampPlayerPositionToScreenDimensions()
player.position.y = screenDimensions.y; player.position.y = screenDimensions.y;
} }
char DealDamage(char *health)
{
return --*health <= 0;
}
void BulletMovement()
{
Bullet *bullet;
int i = 0;
for (; i < bullets.bulletCount; i++)
{
bullet = (bullets.bulletArray + i);
bullet -> position.x += bullet -> velocity.x;
bullet -> position.y += bullet -> velocity.y;
}
}
void ShootSoundEffect() void ShootSoundEffect()
{ {
printf("ShootSoundEffect();\n"); printf("ShootSoundEffect();\n");
@ -915,27 +843,11 @@ void DieSoundEffect()
al_play_sample(enemyDieSound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, &enemyDieSoundID); al_play_sample(enemyDieSound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, &enemyDieSoundID);
} }
void PlayerShootCheck()
{
if(player.shootCooldown < 0.0f)
{
if(al_key_down(&keyboardState, ALLEGRO_KEY_SPACE))
{
printf("PlayerShoot();\n");
PlayerShoot();
}
}
else
player.shootCooldown -= deltaTime;
}
void PlayerShoot() void PlayerShoot()
{ {
Vector2D shootDir; Vector2D shootDir;
Bullet *newBullet; Bullet *newBullet;
float offset; float offset = (al_get_bitmap_width(player.playerImage) + al_get_bitmap_width(enemyBulletImage) * 2.0 * screenSizeMultiplier);
offset = (player.image.size.x + enemyBulletImage.size.x * 2.0);
if(player.lookDirection != 1) if(player.lookDirection != 1)
offset = -offset; offset = -offset;
@ -945,10 +857,8 @@ void PlayerShoot()
shootDir.y = 0; shootDir.y = 0;
player.shootCooldown = 1 / (float)player.shootPerSecond; player.shootCooldown = 1 / (float)player.shootPerSecond;
bullets.bulletCount++; bullets.bulletCount++;
bullets.bulletArray = (Bullet *) realloc(bullets.bulletArray, sizeof(Bullet) * bullets.bulletCount); bullets.bulletArray = (Bullet *) realloc(bullets.bulletArray, sizeof(Bullet) * bullets.bulletCount);
newBullet = (bullets.bulletArray + bullets.bulletCount - 1); newBullet = (bullets.bulletArray + bullets.bulletCount - 1);
newBullet -> position = player.position; newBullet -> position = player.position;
newBullet -> position.x += offset; newBullet -> position.x += offset;
@ -965,15 +875,12 @@ void EnemyShoot()
Enemy *enemy; Enemy *enemy;
Bullet *bullet; Bullet *bullet;
int i; int i;
float offset; float offset = (al_get_bitmap_width(player.playerImage) + al_get_bitmap_width(enemyBulletImage) * 2.0 * screenSizeMultiplier);
offset = (player.image.size.x + enemyBulletImage.size.x * 2.0);
for (i = 0; i < enemies.enemyCount; i++) for (i = 0; i < enemies.enemyCount; i++)
{ {
srand(time(0) + enemyRespawnCounter++); srand(time(0) + enemyRespawnCounter++);
enemy = (enemies.enemyArray + i); enemy = (enemies.enemyArray + i);
if(enemy -> fireCooldown > 0.0) if(enemy -> fireCooldown > 0.0)
enemy -> fireCooldown -= deltaTime; enemy -> fireCooldown -= deltaTime;
else else
@ -996,21 +903,21 @@ void EnemyShoot()
ShootSoundEffect(); ShootSoundEffect();
} }
} }
} }
void BulletCollisions() void BulletCollisions()
{ {
Bullet *bullet; Bullet *bullet;
Enemy *enemy; Enemy *enemy;
int bulletCounter; int bulletCounter = 0;
int enemyCounter; int enemyCounter = 0;
int i = 0;
printf("Enemy-Bullet\n"); printf("Enemy-Bullet\n");
for (enemyCounter = 0; enemyCounter < enemies.enemyCount; enemyCounter++) for (enemyCounter = 0; enemyCounter < enemies.enemyCount; enemyCounter++)
{ {
printf("Enemy-Bullet|enemyCounter\n"); printf("Enemy-Bullet|enemyCounter\n");
enemy = (enemies.enemyArray + enemyCounter); enemy = (enemies.enemyArray + enemyCounter);
for (bulletCounter = 0; bulletCounter < bullets.bulletCount; bulletCounter++) for (bulletCounter = 0; bulletCounter < bullets.bulletCount; bulletCounter++)
{ {
bullet = (bullets.bulletArray + bulletCounter); bullet = (bullets.bulletArray + bulletCounter);
@ -1018,8 +925,8 @@ void BulletCollisions()
if(bullet -> isEnemyBullet == 1 && CheckCollision( if(bullet -> isEnemyBullet == 1 && CheckCollision(
&bullet -> position, &bullet -> position,
&player.position, &player.position,
&enemyBulletImage, enemyBulletImage,
&player.image player.playerImage
)) ))
{ {
RemoveBulletAtIndex(bulletCounter); RemoveBulletAtIndex(bulletCounter);
@ -1033,8 +940,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");
@ -1052,8 +959,8 @@ void BulletCollisions()
if(CheckCollision( if(CheckCollision(
&enemy -> position, &enemy -> position,
&player.position, &player.position,
&enemyImage, enemyImage,
&player.image player.playerImage
)) ))
{ {
RemoveEnemyAtIndex(enemyCounter); RemoveEnemyAtIndex(enemyCounter);
@ -1063,30 +970,19 @@ void BulletCollisions()
isGameOver = 1; isGameOver = 1;
} }
} }
}
void BulletMovement()
{
Bullet *bullet;
int i;
for (i = 0; i < bullets.bulletCount; i++)
{
bullet = (bullets.bulletArray + i);
bullet -> position.x += bullet -> velocity.x;
bullet -> position.y += bullet -> velocity.y;
}
} }
void DestroyGame() void DestroyGame()
{ {
CheckHighScore(); CheckHighScore();
al_destroy_bitmap(enemyImage.bitmap); al_destroy_bitmap(enemyImage);
al_destroy_bitmap(enemyBulletImage.bitmap); al_destroy_bitmap(enemyBulletImage);
al_destroy_bitmap(gameOverImage.bitmap); al_destroy_bitmap(gameOverImage);
al_destroy_bitmap(numberTable.bitmap); al_destroy_bitmap(numberTable);
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);
} }

Binary file not shown.