Code Cleaning, todos added
This commit is contained in:
parent
7cc0621aa5
commit
1b53c9f862
2
Makefile
2
Makefile
@ -1,3 +1,5 @@
|
||||
all:
|
||||
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
|
||||
|
@ -338,7 +338,7 @@ void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount){
|
||||
BULLET_UpdateEnemy(&((enm->Bullets)[i]), targets, &targetCount);
|
||||
}
|
||||
|
||||
if ((enm->TurnCooldown)-- <= 0){
|
||||
if ((enm->TurnCooldown)-- <= 0) {
|
||||
(enm->TurnCooldown) = (rand() % ENEMY_MaxTurnIntervale);
|
||||
(enm->Momentum) = getScaledDirectionalUnitVector((rand() % 360), fmod((double)rand(), ENEMY_MaxSpeed / 2.0f) + (ENEMY_MaxSpeed / 2.0f));
|
||||
}
|
||||
|
114
main.c
114
main.c
@ -21,12 +21,12 @@ void QUIT();
|
||||
void GAMELOOP();
|
||||
void mousePress(SDL_MouseButtonEvent b);
|
||||
|
||||
const int width = 1600;
|
||||
const int width = 1600; // TODO: Fullscreen
|
||||
const int height = 900;
|
||||
const int totalAsteroidCount = 6;
|
||||
const int totalEnemyCount = 6;
|
||||
|
||||
Uint8 * keystate;
|
||||
Uint8 * keystate; // TODO: export all this into scenery and enemy waves
|
||||
SDL_Window * window;
|
||||
SDL_Renderer * renderer;
|
||||
SDL_Event event;
|
||||
@ -55,6 +55,9 @@ int main(int argc, char * args[]){
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
|
||||
break;
|
||||
case SDL_MouseButtonEvent:
|
||||
mousePress(event.button);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -62,34 +65,29 @@ int main(int argc, char * args[]){
|
||||
return 0;
|
||||
} /* main */
|
||||
|
||||
void QUIT(){
|
||||
printf("De-initializing started...\n");
|
||||
free(keystate);
|
||||
SDL_FreeSurface(messageSurface);
|
||||
SDL_DestroyTexture(messageTexture);
|
||||
SHIP_DestroyObject(&ship);
|
||||
void GAMELOOP() {
|
||||
keystate = SDL_GetKeyboardState(NULL); // Get key changes
|
||||
SHIP_Update(&ship, keystate, asteroids, &asteroidCount, enemies, &enemyCount);
|
||||
int i;
|
||||
for (i = 0; i < enemyCount; i++){
|
||||
ENEMY_DestroyObject(&(enemies[i]));
|
||||
for (i = 0; i < enemyCount; 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(){
|
||||
@ -98,7 +96,7 @@ void DrawFrame(){
|
||||
STARFIELD_Draw(&starfield, renderer);
|
||||
SHIP_Draw(&ship, renderer);
|
||||
int i;
|
||||
for (i = 0; i < enemyCount; i++){
|
||||
for (i = 0; i < enemyCount; i++) {
|
||||
ENEMY_Draw(&(enemies[i]), renderer);
|
||||
}
|
||||
for (i = 0; i < asteroidCount; i++) {
|
||||
@ -119,13 +117,13 @@ void INITIALIZE() {
|
||||
Sans = TTF_OpenFont("assets/fonts/bulky.ttf", 12);
|
||||
if (!Sans) printf("Font cannot be 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);
|
||||
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);
|
||||
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) };
|
||||
|
||||
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();
|
||||
enemies = calloc(20, sizeof(Enemy));
|
||||
int i;
|
||||
enemies[enemyCount++] = ENEMY_GetDefault();
|
||||
enemies[enemyCount++] = ENEMY_GetDefault();
|
||||
asteroids = calloc(200, sizeof(Asteroid));
|
||||
for (i = 0; i < totalAsteroidCount; i++) {
|
||||
asteroids[asteroidCount++] = ASTEROID_CreateRandom();
|
||||
}
|
||||
} /* INITIALIZE */
|
||||
|
||||
void GAMELOOP() {
|
||||
keystate = SDL_GetKeyboardState(NULL); // Get key changes
|
||||
SHIP_Update(&ship, keystate, asteroids, &asteroidCount, enemies, &enemyCount);
|
||||
void QUIT(){
|
||||
printf("De-initializing started...\n");
|
||||
free(keystate);
|
||||
SDL_FreeSurface(messageSurface);
|
||||
SDL_DestroyTexture(messageTexture);
|
||||
SHIP_DestroyObject(&ship);
|
||||
int i;
|
||||
for (i = 0; i < enemyCount; i++){
|
||||
ENEMY_Update(&(enemies[i]), &ship, 1);
|
||||
for (i = 0; i < enemyCount; i++) {
|
||||
ENEMY_DestroyObject(&(enemies[i]));
|
||||
}
|
||||
STARFIELD_Update(&starfield);
|
||||
if (!(ship.IsDead)) {
|
||||
for (i = 0; i < asteroidCount; i++) {
|
||||
ASTEROID_Update(&(asteroids[i]));
|
||||
}
|
||||
}
|
||||
} /* GAMELOOP */
|
||||
|
||||
void mousePress(SDL_MouseButtonEvent b){
|
||||
return;
|
||||
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");
|
||||
} /* QUIT */
|
||||
|
@ -53,16 +53,16 @@ Starfield STARFIELD_GetDefault(int count){
|
||||
}
|
||||
|
||||
void STARFIELD_Draw(Starfield * strf, SDL_Renderer * renderer){
|
||||
printf("Drawing starfield...\n");
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (strf->StarCount); i++) {
|
||||
STAR_Draw(&((strf->Stars)[i]), renderer);
|
||||
}
|
||||
printf("Starfield drawn!\n");
|
||||
}
|
||||
|
||||
void STARFIELD_Update(Starfield * strf){
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (strf->StarCount); i++) {
|
||||
STAR_Update(&((strf->Stars)[i]));
|
||||
}
|
||||
@ -95,7 +95,7 @@ void STAR_Update(Star * star){
|
||||
*star = STAR_GetDefault();
|
||||
}
|
||||
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;
|
||||
double sy = (star->y) / (star->z) * STARFIELD_BoxHeight; // current distance using perspective
|
||||
if (sy * 2 > STARFIELD_BoxHeight || sy < -STARFIELD_BoxHeight / 2)
|
||||
|
7
vector.c
7
vector.c
@ -19,9 +19,10 @@ double vectorMagnitude(Vector v){
|
||||
}
|
||||
|
||||
double vectorRotation(Vector v){
|
||||
double da=atan2(v.x, -v.y)/degreeToRadians;
|
||||
if (da < 0.0f) return (da+360.0f);
|
||||
else return da;
|
||||
double da = atan2(v.x, -v.y) / degreeToRadians;
|
||||
|
||||
if (da < 0.0f) return (da + 360.0f);
|
||||
else return da;
|
||||
}
|
||||
|
||||
Vector vectorScaleTo(Vector v, double magnitude){
|
||||
|
Loading…
Reference in New Issue
Block a user