Code Cleaning, todos added

This commit is contained in:
Michael Chen 2017-12-27 00:42:06 +01:00
parent 7cc0621aa5
commit 1b53c9f862
5 changed files with 68 additions and 63 deletions

View File

@ -1,3 +1,5 @@
all: all:
gcc -Wall -IC:\SDL2\x32\include *.c -LC:\SDL2\x32\lib -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lSDL2_image -lSDL2_ttf -o Asteroids.exe gcc -Wall -IC:\SDL2\x32\include *.c -LC:\SDL2\x32\lib -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lSDL2_image -lSDL2_ttf -o Asteroids.exe
run:
Asteroids.exe Asteroids.exe

View File

@ -338,7 +338,7 @@ void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount){
BULLET_UpdateEnemy(&((enm->Bullets)[i]), targets, &targetCount); BULLET_UpdateEnemy(&((enm->Bullets)[i]), targets, &targetCount);
} }
if ((enm->TurnCooldown)-- <= 0){ if ((enm->TurnCooldown)-- <= 0) {
(enm->TurnCooldown) = (rand() % ENEMY_MaxTurnIntervale); (enm->TurnCooldown) = (rand() % ENEMY_MaxTurnIntervale);
(enm->Momentum) = getScaledDirectionalUnitVector((rand() % 360), fmod((double)rand(), ENEMY_MaxSpeed / 2.0f) + (ENEMY_MaxSpeed / 2.0f)); (enm->Momentum) = getScaledDirectionalUnitVector((rand() % 360), fmod((double)rand(), ENEMY_MaxSpeed / 2.0f) + (ENEMY_MaxSpeed / 2.0f));
} }

114
main.c
View File

@ -21,12 +21,12 @@ void QUIT();
void GAMELOOP(); void GAMELOOP();
void mousePress(SDL_MouseButtonEvent b); void mousePress(SDL_MouseButtonEvent b);
const int width = 1600; const int width = 1600; // TODO: Fullscreen
const int height = 900; const int height = 900;
const int totalAsteroidCount = 6; const int totalAsteroidCount = 6;
const int totalEnemyCount = 6; const int totalEnemyCount = 6;
Uint8 * keystate; Uint8 * keystate; // TODO: export all this into scenery and enemy waves
SDL_Window * window; SDL_Window * window;
SDL_Renderer * renderer; SDL_Renderer * renderer;
SDL_Event event; SDL_Event event;
@ -55,6 +55,9 @@ int main(int argc, char * args[]){
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false; if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
break; break;
case SDL_MouseButtonEvent:
mousePress(event.button);
break;
} }
} }
} }
@ -62,34 +65,29 @@ int main(int argc, char * args[]){
return 0; return 0;
} /* main */ } /* main */
void QUIT(){ void GAMELOOP() {
printf("De-initializing started...\n"); keystate = SDL_GetKeyboardState(NULL); // Get key changes
free(keystate); SHIP_Update(&ship, keystate, asteroids, &asteroidCount, enemies, &enemyCount);
SDL_FreeSurface(messageSurface);
SDL_DestroyTexture(messageTexture);
SHIP_DestroyObject(&ship);
int i; int i;
for (i = 0; i < enemyCount; i++){ for (i = 0; i < enemyCount; i++) {
ENEMY_DestroyObject(&(enemies[i])); ENEMY_Update(&(enemies[i]), &ship, 1);
}
STARFIELD_Update(&starfield);
if (!(ship.IsDead)) {
for (i = 0; i < asteroidCount; i++) {
ASTEROID_Update(&(asteroids[i]));
}
}
} /* GAMELOOP */
void mousePress(SDL_MouseButtonEvent b){
if (b.button == SDL_BUTTON_LEFT) {
printf("Left mouse pressed...\n");
} else if (b.button == SDL_BUTTON_RIGHT) {
printf("Right mouse pressed...\n");
} else {
printf("Unknown mouse button pressed: %d\n", b.button);
} }
free(enemies);
STARFIELD_DestroyObject(&starfield);
STARFIELD_Deinitialize();
SHIP_Deinitialize();
ENEMY_Deinitialize();
ASTEROID_Deinitialize();
TTF_CloseFont(Sans);
TTF_Quit();
free(asteroids);
IMG_Quit();
printf("Quitting SDL_IMG finished!\n");
SDL_DestroyRenderer(renderer);
printf("De-initializing renderer finished!\n");
SDL_DestroyWindow(window);
printf("De-initializing window finished!\n");
SDL_Quit();
printf("Quitting SDL finished!\n");
printf("De-initializing finished!\n");
} }
void DrawFrame(){ void DrawFrame(){
@ -98,7 +96,7 @@ void DrawFrame(){
STARFIELD_Draw(&starfield, renderer); STARFIELD_Draw(&starfield, renderer);
SHIP_Draw(&ship, renderer); SHIP_Draw(&ship, renderer);
int i; int i;
for (i = 0; i < enemyCount; i++){ for (i = 0; i < enemyCount; i++) {
ENEMY_Draw(&(enemies[i]), renderer); ENEMY_Draw(&(enemies[i]), renderer);
} }
for (i = 0; i < asteroidCount; i++) { for (i = 0; i < asteroidCount; i++) {
@ -119,13 +117,13 @@ void INITIALIZE() {
Sans = TTF_OpenFont("assets/fonts/bulky.ttf", 12); Sans = TTF_OpenFont("assets/fonts/bulky.ttf", 12);
if (!Sans) printf("Font cannot be initialized!\n"); if (!Sans) printf("Font cannot be initialized!\n");
else printf("Font was successfully initialized!\n"); else printf("Font was successfully initialized!\n");
messageColor = (SDL_Color) {.r = 125,.g=255,.b = 125,.a = 255}; messageColor = (SDL_Color) {.r = 125, .g = 255, .b = 125, .a = 255 };
messageSurface = TTF_RenderText_Solid(Sans, "A", messageColor); messageSurface = TTF_RenderText_Solid(Sans, "A", messageColor);
if (!messageSurface) printf("Text surface is null!\n"); if (!messageSurface) printf("Text surface is null!\n");
else printf("Text surface was created!\n"); else printf("Text surface was created!\n");
messageTexture = SDL_CreateTextureFromSurface(renderer, messageSurface); messageTexture = SDL_CreateTextureFromSurface(renderer, messageSurface);
if (!messageTexture) printf("Text texture is null!\n"); if (!messageTexture) printf("Text texture is null!\n");
else printf("Text texture was created!\n"); else printf("Text texture was created!\n");
messageRect = (SDL_Rect) {.x = 100, .y = 100, .w = (messageSurface->w), .h = (messageSurface->h) }; messageRect = (SDL_Rect) {.x = 100, .y = 100, .w = (messageSurface->w), .h = (messageSurface->h) };
window = SDL_CreateWindow("Asteroids Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL); window = SDL_CreateWindow("Asteroids Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
@ -141,35 +139,39 @@ void INITIALIZE() {
ship = SHIP_CreateDefault(); ship = SHIP_CreateDefault();
enemies = calloc(20, sizeof(Enemy)); enemies = calloc(20, sizeof(Enemy));
int i; int i;
enemies[enemyCount++] = ENEMY_GetDefault(); enemies[enemyCount++] = ENEMY_GetDefault();
asteroids = calloc(200, sizeof(Asteroid)); asteroids = calloc(200, sizeof(Asteroid));
for (i = 0; i < totalAsteroidCount; i++) { for (i = 0; i < totalAsteroidCount; i++) {
asteroids[asteroidCount++] = ASTEROID_CreateRandom(); asteroids[asteroidCount++] = ASTEROID_CreateRandom();
} }
} /* INITIALIZE */ } /* INITIALIZE */
void GAMELOOP() { void QUIT(){
keystate = SDL_GetKeyboardState(NULL); // Get key changes printf("De-initializing started...\n");
SHIP_Update(&ship, keystate, asteroids, &asteroidCount, enemies, &enemyCount); free(keystate);
SDL_FreeSurface(messageSurface);
SDL_DestroyTexture(messageTexture);
SHIP_DestroyObject(&ship);
int i; int i;
for (i = 0; i < enemyCount; i++){ for (i = 0; i < enemyCount; i++) {
ENEMY_Update(&(enemies[i]), &ship, 1); ENEMY_DestroyObject(&(enemies[i]));
} }
STARFIELD_Update(&starfield); free(enemies);
if (!(ship.IsDead)) { STARFIELD_DestroyObject(&starfield);
for (i = 0; i < asteroidCount; i++) { STARFIELD_Deinitialize();
ASTEROID_Update(&(asteroids[i])); SHIP_Deinitialize();
} ENEMY_Deinitialize();
} ASTEROID_Deinitialize();
} /* GAMELOOP */ TTF_CloseFont(Sans);
TTF_Quit();
void mousePress(SDL_MouseButtonEvent b){ free(asteroids);
return; IMG_Quit();
if (b.button == SDL_BUTTON_LEFT) { printf("Quitting SDL_IMG finished!\n");
printf("Left mouse pressed...\n"); SDL_DestroyRenderer(renderer);
} else if (b.button == SDL_BUTTON_RIGHT) { printf("De-initializing renderer finished!\n");
printf("Right mouse pressed...\n"); SDL_DestroyWindow(window);
} else { printf("De-initializing window finished!\n");
printf("Unknown mouse button pressed: %d\n", b.button); SDL_Quit();
} printf("Quitting SDL finished!\n");
} printf("De-initializing finished!\n");
} /* QUIT */

View File

@ -53,16 +53,16 @@ Starfield STARFIELD_GetDefault(int count){
} }
void STARFIELD_Draw(Starfield * strf, SDL_Renderer * renderer){ void STARFIELD_Draw(Starfield * strf, SDL_Renderer * renderer){
printf("Drawing starfield...\n");
int i; int i;
for (i = 0; i < (strf->StarCount); i++) { for (i = 0; i < (strf->StarCount); i++) {
STAR_Draw(&((strf->Stars)[i]), renderer); STAR_Draw(&((strf->Stars)[i]), renderer);
} }
printf("Starfield drawn!\n");
} }
void STARFIELD_Update(Starfield * strf){ void STARFIELD_Update(Starfield * strf){
int i; int i;
for (i = 0; i < (strf->StarCount); i++) { for (i = 0; i < (strf->StarCount); i++) {
STAR_Update(&((strf->Stars)[i])); STAR_Update(&((strf->Stars)[i]));
} }
@ -95,7 +95,7 @@ void STAR_Update(Star * star){
*star = STAR_GetDefault(); *star = STAR_GetDefault();
} }
double sx = (star->x) / (star->z) * STARFIELD_BoxWidth;// current distance using perspective double sx = (star->x) / (star->z) * STARFIELD_BoxWidth;// current distance using perspective
if (sx > STARFIELD_BoxWidth / 2|| sx < -STARFIELD_BoxWidth / 2) if (sx > STARFIELD_BoxWidth / 2 || sx < -STARFIELD_BoxWidth / 2)
(star->OutOfBounds) = true; (star->OutOfBounds) = true;
double sy = (star->y) / (star->z) * STARFIELD_BoxHeight; // current distance using perspective double sy = (star->y) / (star->z) * STARFIELD_BoxHeight; // current distance using perspective
if (sy * 2 > STARFIELD_BoxHeight || sy < -STARFIELD_BoxHeight / 2) if (sy * 2 > STARFIELD_BoxHeight || sy < -STARFIELD_BoxHeight / 2)

View File

@ -19,9 +19,10 @@ double vectorMagnitude(Vector v){
} }
double vectorRotation(Vector v){ double vectorRotation(Vector v){
double da=atan2(v.x, -v.y)/degreeToRadians; double da = atan2(v.x, -v.y) / degreeToRadians;
if (da < 0.0f) return (da+360.0f);
else return da; if (da < 0.0f) return (da + 360.0f);
else return da;
} }
Vector vectorScaleTo(Vector v, double magnitude){ Vector vectorScaleTo(Vector v, double magnitude){