Added Vector library and some file structure

This commit is contained in:
Michael Chen 2018-01-09 13:22:10 +01:00
parent 4efb09070f
commit c7bf766898
6 changed files with 163 additions and 1 deletions

BIN
assets/images/ball.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

16
breakout.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "vector.h"
#ifndef __nullptr__
#define Nullptr(type) (type *)0
#endif // __nullptr__
void BREAKOUT_INITIALIZE(){
}

32
breakout.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef __breakout_h__
#define __breakout_h__
#include "vector.h"
// Structs
typedef struct ballStruct {
Vector Location, Momentum;
SDL_Rect TargetRect;
} Ball;
typedef struct paddleStruct {
double XLocation;
SDL_Rect TargetRect;
} Paddle;
typedef struct blockStruct {
Vector Location;
SDL_Rect TargetRect;
} Block;
typedef struct powerupStruct { // Maybe implement later
Vector Location;
SDL_Rect TargetRect;
} Powerup;
// End Structs
// Prototypes
// End Prototypes
#endif // __breakout_h__

6
main.c
View File

@ -1,9 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "vector.h"
#ifndef __nullptr__
#define Nullptr(type) (type *)0

84
vector.c Normal file
View File

@ -0,0 +1,84 @@
#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);
}

26
vector.h Normal file
View File

@ -0,0 +1,26 @@
#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__