You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
2.9 KiB
155 lines
2.9 KiB
#include <SDL2/SDL.h> |
|
#include <SDL2/SDL_image.h> |
|
#include <stdbool.h> |
|
#include <stdint.h> |
|
#include <math.h> |
|
#include "vect.h" |
|
|
|
#ifndef _DEFGAME |
|
#define _DEFGAME |
|
|
|
#include "garbo.h" |
|
#include "draw.h" |
|
|
|
enum motors { |
|
M_GRAVITY = 0, |
|
M_FRICTION = 1, |
|
M_PLAYER_WALK = 2 |
|
}; |
|
|
|
enum world_thing_kind { |
|
PLAYER_W, |
|
STATIC_WALL_W, |
|
FLOOR |
|
}; |
|
|
|
// used to exert a force on an object |
|
typedef struct motorstruct { |
|
double x; // positive is right |
|
double y; // positive is down |
|
double torque; // positive is anticlockwise |
|
double max_velocity; // max motor output velocity |
|
// does not apply force if the velocity in the |
|
// direction of the motor vector is greater than |
|
// max_velocity |
|
uint32_t timeout;// absolute time (in ms, from when the program starts) |
|
// for when the motor stops. |
|
// set to -1 for infinity |
|
void (*update_motor)(struct motorstruct *motor); // function pointer for generating |
|
// the motor's output curve |
|
} Motor; |
|
|
|
typedef struct { |
|
int x; |
|
int y; |
|
} Point; |
|
|
|
typedef struct BodyStruct{ |
|
|
|
// turn on dynamic physics |
|
// For moving objects. |
|
// eg. on for payer, off for walls |
|
bool dynamics; |
|
|
|
// unique identifier |
|
int uid; |
|
|
|
bool colliding; |
|
|
|
// position in viewport (pixels) |
|
SDL_Point screen_pos; |
|
|
|
// SI Unit kinematics |
|
/*------------------*/ |
|
Vect position; |
|
Vect vel; |
|
Vect acc; |
|
|
|
// properties |
|
double obj_mass; // kgs |
|
double obj_elasticity; // rho |
|
|
|
//float x_vel; |
|
//float y_vel; |
|
|
|
//float x_acc; |
|
//float y_acc; |
|
/*------------------*/ |
|
|
|
|
|
// constants |
|
double elasticity; |
|
|
|
// collisions |
|
Vect *collision_poly; |
|
Vect *collision_shape; |
|
int collision_poly_size; |
|
void (*updateCollisionPoly)(struct BodyStruct *); |
|
|
|
|
|
// fields |
|
double glob_friction; |
|
bool glob_gravity; // t/f |
|
|
|
uint32_t last_advance_time; |
|
|
|
// applying forces |
|
int num_motors; |
|
int max_motors; |
|
Motor *motors; |
|
} Body; |
|
|
|
typedef struct { |
|
bool has_physics; |
|
Body *physics; |
|
int max_walking_speed; |
|
int colliding; |
|
} player_st; |
|
|
|
typedef struct { |
|
int numNodes; |
|
SDL_Point *nodes; |
|
Body *physics; |
|
} Wall; |
|
|
|
typedef struct { |
|
Vect left; |
|
Vect right; |
|
Body *physics; |
|
} FloorPoly; |
|
|
|
typedef struct { |
|
FloorPoly *polys; |
|
int numPolys; |
|
} Floor; |
|
|
|
typedef struct { |
|
enum world_thing_kind kind; |
|
int nid; |
|
bool collisions; |
|
bool physics; |
|
union { |
|
player_st *player; |
|
Wall *wall; |
|
Floor *floor; |
|
}; |
|
} world_thing; |
|
|
|
|
|
/* array of all the things in the world and their kinds */ |
|
world_thing *world; |
|
|
|
int things_in_world; |
|
int world_size; |
|
|
|
void startgame(SDL_Renderer * ren) ; |
|
|
|
void process_keydown(SDL_Keysym key); |
|
void process_keyup(SDL_Keysym key); |
|
|
|
void step(int interval); |
|
|
|
player_st player; |
|
|
|
void add_motor(Body *thing, double x, double y); |
|
#endif
|
|
|