Commit 762dfd03 authored by Michael Chen's avatar Michael Chen
Browse files

Initial commit

parents
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "vector.h"
#include "asteroids.h"
#include "starfield.h"
#ifndef __nullptr__
#define Nullptr(type) (type *)0
#endif // __nullptr__
void DrawFrame();
void INITIALIZE();
void QUIT();
void GAMELOOP();
void mousePress(SDL_MouseButtonEvent b);
const int width = 1600;
const int height = 900;
const int totalAsteroidCount = 6;
const int totalEnemyCount = 6;
Uint8 * keystate;
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Event event;
bool running = true;
SDL_Color messageColor;
TTF_Font * Sans;
SDL_Surface * messageSurface;
SDL_Rect messageRect;
SDL_Texture * messageTexture;
Asteroid * asteroids;
Ship ship;
Enemy * enemies;
Starfield starfield;
int asteroidCount = 0, enemyCount = 0;
int main(int argc, char * args[]){
INITIALIZE();
while (running) {
GAMELOOP();
DrawFrame();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
break;
}
}
}
QUIT();
return 0;
} /* main */
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_DestroyObject(&(enemies[i]));
}
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(){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
STARFIELD_Draw(&starfield, renderer);
SHIP_Draw(&ship, renderer);
int i;
for (i = 0; i < enemyCount; i++){
ENEMY_Draw(&(enemies[i]), renderer);
}
for (i = 0; i < asteroidCount; i++) {
ASTEROID_Draw(&(asteroids[i]), renderer);
}
// SDL_RenderCopy(renderer, messageTexture, NULL, &messageRect);
SDL_RenderPresent(renderer);
}
void INITIALIZE() {
srand(time(NULL));
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
else printf("SDL was successfully initialized!\n");
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) printf("IMG could not initialize! IMG_Error: %s\n", IMG_GetError());
else printf("IMG was successfully initialized!\n");
TTF_Init();
Sans = TTF_OpenFont("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};
messageSurface = TTF_RenderText_Solid(Sans, "A", messageColor);
if (!messageSurface) printf("Text surface is null!\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");
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);
printf("Window was created!\n");
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
printf("Renderer was created!\n");
STARFIELD_Initialize(renderer, width, height);
SHIP_Initialize(renderer, width, height);
ENEMY_Initialize(renderer, width, height);
ASTEROID_Initialize(renderer, width, height);
starfield = STARFIELD_GetDefault(100);
ship = SHIP_CreateDefault();
enemies = calloc(20, sizeof(Enemy));
int i;
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);
int 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){
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);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "starfield.h"
// Properties
double STAR_Speed = 5.0f;
double STAR_MaxRotationValue = 5.0f;
// End Properties
// Vars
int STARFIELD_BoxWidth, STARFIELD_BoxHeight;
SDL_Texture * STAR_Texture;
SDL_Rect * STAR_SourceRects;
// End Vars
void STARFIELD_Initialize(SDL_Renderer * renderer, int width, int height){
printf("Initializing starfield...\n");
STARFIELD_BoxWidth = width;
STARFIELD_BoxHeight = height;
STAR_Texture = IMG_LoadTexture(renderer, "star.png");
if (!STAR_Texture)
printf("Star texture cannot be loaded!\n");
STAR_SourceRects = malloc(3 * sizeof(SDL_Rect));
int i;
for (i = 0; i < 3; i++) {
STAR_SourceRects[i] = (SDL_Rect) {.x = 0, .y = (100 * i), .w = 100, .h = 100 };
}
printf("Starfield initialized!\n");
}
void STARFIELD_Deinitialize(){
printf("De-initializing Starfield class...\n");
printf("De-initializing Source Rectangles...\n");
free(STAR_SourceRects);
printf("De-initializing Starfield textures...\n");
SDL_DestroyTexture(STAR_Texture);
printf("Starfield class de-initialized!\n");
}
Starfield STARFIELD_GetDefault(int count){
Star * strs = malloc(count * sizeof(Star));
int i;
for (i = 0; i < count; i++) {
strs[i] = STAR_GetDefault();
}
return (Starfield) {.Stars = strs, .StarCount = 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]));
}
}
void STARFIELD_DestroyObject(Starfield * strf){
printf("De-initializing Starfield...\n");
printf("De-initializing Star-array...\n");
free((strf->Stars));
printf("Starfield de-initialized!\n");
}
Star STAR_GetDefault(){
return (Star) {
.x = (rand() % STARFIELD_BoxWidth) - (STARFIELD_BoxWidth / 2),
.y = (rand() % STARFIELD_BoxHeight) - (STARFIELD_BoxHeight / 2),
.z = (rand() % STARFIELD_BoxWidth),
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 0, .h = 0 },
.SourceRect = STAR_SourceRects[(rand() % 3)],
.Rotation = (double)(rand() % 360),
.RotationValue = (fmod((double)(rand()), STAR_MaxRotationValue * 2.0f) - STAR_MaxRotationValue),
.OutOfBounds = false
};
}
void STAR_Update(Star * star){
if (!(star->OutOfBounds)) {
(star->Rotation) += (star->RotationValue);
(star->z) -= STAR_Speed; // increase speed (rather acceleration since it looks like a 3D room, but the speed is constant)
if ((star->z) < 1) { // respawns a star that has disappeared near the edge of the screen
*star = STAR_GetDefault();
}
double sx = (star->x) / (star->z) * STARFIELD_BoxWidth;// current distance using perspective
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)
(star->OutOfBounds) = true;
double size = (((double)(STARFIELD_BoxWidth - (star->z))) / ((double)(STARFIELD_BoxWidth)) * 2.4f) + 1.0f; // radius
size = 3 * size * size; // potentially grow particles
(star->TargetRect) = (SDL_Rect) {.x = (int)round(sx) + (STARFIELD_BoxWidth / 2), .y = (int)round(sy) + (STARFIELD_BoxHeight / 2), .w = size, .h = size };
} else {
*star = STAR_GetDefault();
}
}
void STAR_Draw(Star * star, SDL_Renderer * renderer){
if (!(star->OutOfBounds)) {
// printf("Drawing star at: %d | %d with Size %d...\n", (star->TargetRect).x,(star->TargetRect).y,(star->TargetRect).w );
SDL_RenderCopyEx(renderer, STAR_Texture, &(star->SourceRect), &(star->TargetRect), (star->Rotation), NULL, SDL_FLIP_NONE);
}
}
#ifndef __starfield_h__
#define __starfield_h__
#include <stdbool.h>
// Structs
typedef struct starStruct {
double x, y, z, Rotation, RotationValue;
SDL_Rect TargetRect, SourceRect;
bool OutOfBounds;
} Star;
typedef struct starfieldStruct {
Star * Stars;
int StarCount;
} Starfield;
// End Structs
// Prototypes
void STARFIELD_Initialize(SDL_Renderer * renderer, int width, int height);
void STARFIELD_Deinitialize();
Starfield STARFIELD_GetDefault(int count);
void STARFIELD_Draw(Starfield * strf, SDL_Renderer * renderer);
void STARFIELD_Update(Starfield * strf);
void STARFIELD_DestroyObject(Starfield * strf);
Star STAR_GetDefault();
void STAR_Update(Star * star);
void STAR_Draw(Star * star, SDL_Renderer * renderer);
// End Prototypes
#endif // __starfield_h__
#include <math.h>
#include "vector.h"
// Properties
const double degreeToRadians = M_PI / 180.0f;
// End Properties
Vector vectorScale(Vector v, double factor){
return (Vector)
{
.x = v.x * factor,
.y = v.y * factor
};
}
double vectorMagnitude(Vector v){
return sqrt((v.x * v.x) + (v.y * v.y));
}
double vectorRotation(Vector v){
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){
return vectorScale(v, magnitude / vectorMagnitude(v));
}
double dotProduct(Vector v1, Vector v2){
return (v1.x * v2.x ) + (v1.y * v2.y);
}
double vectorDist(Vector v1, Vector v2){
return sqrt(dotProduct(v1, v2));
}
Vector vectorAdd(Vector v1, Vector v2){
return (Vector)
{
.x = v1.x + v2.x,
.y = v1.y + v2.y
};
}
Vector vectorSub(Vector v1, Vector v2){
return (Vector)
{
.x = v1.x - v2.x,
.y = v1.y - v2.y
};
}
double degreeSin(double x){
return sin(x * degreeToRadians);
}
double degreeCos(double x){
return cos(x * degreeToRadians);
}
Vector getDirectionalUnitVector(double rotation){
return (Vector)
{
.x = degreeSin(rotation),
.y = -degreeCos(rotation)
};
}
Vector getScaledDirectionalUnitVector(double rotation, double Magnitude){
Vector v = (Vector)
{
.x = degreeSin(rotation),
.y = -degreeCos(rotation)
};
return vectorScale(v, Magnitude);
}
Vector getScaledVectorFromTo(Vector from, Vector to, double Magnitude){
return vectorScale(vectorSub(to, from), Magnitude);
}
#ifndef __vector_h__
#define __vector_h__
// Structs
typedef struct vectorStruct {
double x, y;
} Vector;
// End Structs
// Prototypes
Vector vectorScale(Vector v, double factor);
double vectorMagnitude(Vector v);
double vectorRotation(Vector v);
Vector vectorScaleTo(Vector v, double magnitude);
double dotProduct(Vector v1, Vector v2);
double vectorDist(Vector v1, Vector v2);
Vector vectorAdd(Vector v1, Vector v2);
Vector vectorSub(Vector v1, Vector v2);
double degreeSin(double x);
double degreeCos(double x);
Vector getDirectionalUnitVector(double rotation);
Vector getScaledDirectionalUnitVector(double rotation, double Magnitude);
Vector getScaledVectorFromTo(Vector from, Vector to, double Magnitude);
// End Prototypes
#endif // __vector_h__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment