Restructured and allowed added more tars to be created
This commit is contained in:
69
src/main.c
69
src/main.c
@@ -15,9 +15,10 @@
|
|||||||
#define BAR_LEN 150
|
#define BAR_LEN 150
|
||||||
#define BAR_THICK 10
|
#define BAR_THICK 10
|
||||||
#define FPS 60
|
#define FPS 60
|
||||||
#define TAR_WIDTH BAR_LEN
|
#define TAR_WIDTH BAR_THICK*2
|
||||||
#define TAR_HEIGHT BAR_THICK
|
#define TAR_LEN (BAR_LEN-50)
|
||||||
#define TAR_CAP 12
|
#define TAR_CAP 7
|
||||||
|
#define TAR_OFF TAR_LEN*0.1
|
||||||
|
|
||||||
|
|
||||||
float clamp(float value, float min, float max) {
|
float clamp(float value, float min, float max) {
|
||||||
@@ -30,15 +31,14 @@ typedef struct
|
|||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
bool dead;
|
}Position;
|
||||||
}Tar;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int x;
|
Position post;
|
||||||
int y;
|
bool dead;
|
||||||
}Position;
|
}Tar;
|
||||||
|
|
||||||
|
|
||||||
static bool running = true;
|
static bool running = true;
|
||||||
static SDL_Event event;
|
static SDL_Event event;
|
||||||
@@ -79,8 +79,8 @@ SDL_Rect bar_fn() {
|
|||||||
return bar;
|
return bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Rect tar_fn(Position mytar) {
|
SDL_Rect tar_fn(Position *tarpos) {
|
||||||
const SDL_Rect tar = make_rect(mytar.x, mytar.y, TAR_HEIGHT, TAR_WIDTH);
|
const SDL_Rect tar = make_rect(tarpos->x, tarpos->y, TAR_LEN, TAR_WIDTH);
|
||||||
return tar;
|
return tar;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,11 +101,20 @@ static bool process_events(SDL_Event *myevents) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_obj(float dt, SDL_Rect* upd_bar){
|
void update_obj(float dt, SDL_Rect *upd_bar){
|
||||||
|
keyhandle = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
if (!pausi) {
|
if (!pausi) {
|
||||||
|
if (keyhandle[SDL_SCANCODE_D]) {
|
||||||
|
bar_pos.x += 11;
|
||||||
|
}
|
||||||
|
if (keyhandle[SDL_SCANCODE_A]) {
|
||||||
|
bar_pos.x -= 11;
|
||||||
|
}
|
||||||
// Calculate our rectable motion BALL_SPEED and direction
|
// Calculate our rectable motion BALL_SPEED and direction
|
||||||
float mx_x = ball_pos.x + ball_vel.x*BALL_SPEED*dt;
|
float mx_x = ball_pos.x + ball_vel.x*BALL_SPEED*dt;
|
||||||
SDL_Rect upd_ball_x = ball_fn(mx_x , ball_pos.y);
|
SDL_Rect upd_ball_x = ball_fn(mx_x , ball_pos.y);
|
||||||
|
|
||||||
if (mx_x < 0 || mx_x + BALL_SIZE > WINDOW_WIDTH || SDL_HasIntersection(&upd_ball_x, upd_bar)) {
|
if (mx_x < 0 || mx_x + BALL_SIZE > WINDOW_WIDTH || SDL_HasIntersection(&upd_ball_x, upd_bar)) {
|
||||||
ball_vel.x *= -1;
|
ball_vel.x *= -1;
|
||||||
mx_x = ball_pos.x + ball_vel.x*BALL_SPEED*dt;
|
mx_x = ball_pos.x + ball_vel.x*BALL_SPEED*dt;
|
||||||
@@ -125,30 +134,29 @@ void update_obj(float dt, SDL_Rect* upd_bar){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void render_fn(SDL_Renderer* myrender, SDL_Rect* rend_bar) {
|
void render_fn(SDL_Renderer *myrender, SDL_Rect *rend_bar, SDL_Rect *rend_ball) {
|
||||||
// Add our Ball
|
// Add our Ball
|
||||||
SDL_SetRenderDrawColor(myrender, hexcolour(0xff0000fc));
|
SDL_SetRenderDrawColor(myrender, hexcolour(0xff0000fc));
|
||||||
//Update the render for ball
|
//Update the render for ball
|
||||||
SDL_Rect rend_ball = ball_fn(ball_pos.x , ball_pos.y);
|
SDL_RenderFillRect(myrender, rend_ball);
|
||||||
SDL_RenderFillRect(myrender, &rend_ball);
|
|
||||||
|
|
||||||
// Add our bar
|
// Add our bar
|
||||||
SDL_SetRenderDrawColor(myrender, hexcolour(0xfffffffc));
|
SDL_SetRenderDrawColor(myrender, hexcolour(0xfffffffc));
|
||||||
//Update the render for bar
|
//Update the render for bar
|
||||||
SDL_RenderFillRect(myrender, rend_bar);
|
SDL_RenderFillRect(myrender, rend_bar);
|
||||||
|
|
||||||
//Add our tars
|
//Add our tars colours
|
||||||
SDL_SetRenderDrawColor(myrender, hexcolour(0x640335fc));
|
SDL_SetRenderDrawColor(myrender, hexcolour(0xcc5d968a));
|
||||||
//Add all our tars
|
//Add all our tars
|
||||||
for (int i = 0; i < TAR_CAP; i++) {
|
for (int i = 0; i < TAR_CAP; i++) {
|
||||||
if (!tar_pool[i].dead) {
|
if (!tar_pool[i].dead) {
|
||||||
Position tar_pos = {.x = tar_pool[i].x, .y = tar_pool[i].y};
|
SDL_Rect rend_tar = tar_fn(&tar_pool[i].post);
|
||||||
SDL_Rect tar_rect = tar_fn(tar_pos);
|
SDL_RenderFillRect(myrender, &rend_tar);
|
||||||
SDL_RenderFillRect(myrender, &tar_rect);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
// Initialise SDL
|
// Initialise SDL
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
@@ -166,7 +174,6 @@ int main(int argc, char* argv[]) {
|
|||||||
SDL_WINDOW_SHOWN
|
SDL_WINDOW_SHOWN
|
||||||
);
|
);
|
||||||
|
|
||||||
keyhandle = SDL_GetKeyboardState(NULL);
|
|
||||||
|
|
||||||
if (window == NULL) {
|
if (window == NULL) {
|
||||||
fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
||||||
@@ -182,8 +189,12 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Initialise our tars
|
||||||
|
int tar_offset = 0;
|
||||||
for (int t = 0; t < TAR_CAP; t++) {
|
for (int t = 0; t < TAR_CAP; t++) {
|
||||||
tar_pool[t] = (Tar){.x = 30, .y = 30, .dead = false};
|
tar_pool[t] = (Tar){.post.x = tar_offset+30, .post.y = 30, .dead = false};
|
||||||
|
tar_offset = tar_offset + TAR_LEN + TAR_OFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main event loop
|
// Main event loop
|
||||||
@@ -192,22 +203,16 @@ int main(int argc, char* argv[]) {
|
|||||||
process_events(&event);
|
process_events(&event);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keyhandle[SDL_SCANCODE_D]) {
|
SDL_Rect main_ball = ball_fn(ball_pos.x , ball_pos.y);
|
||||||
bar_pos.x += 11;
|
SDL_Rect main_bar = bar_fn();
|
||||||
}
|
|
||||||
if (keyhandle[SDL_SCANCODE_A]) {
|
|
||||||
bar_pos.x -= 11;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SDL_Rect tmp_ball = ball_fn(ball_pos.x, ball_pos.y);
|
update_obj(delta_time, &main_bar);
|
||||||
const SDL_Rect tmp_bar = bar_fn();
|
|
||||||
update_obj(delta_time, &tmp_bar);
|
|
||||||
// Clear screen
|
// Clear screen
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(render, hexcolour(0x3bbeca88));
|
SDL_SetRenderDrawColor(render, hexcolour(0x3bbeca88));
|
||||||
SDL_RenderClear(render);
|
SDL_RenderClear(render);
|
||||||
|
|
||||||
render_fn(render, &tmp_bar);
|
render_fn(render, &main_bar, &main_ball);
|
||||||
|
|
||||||
// Show what we have on the render
|
// Show what we have on the render
|
||||||
SDL_RenderPresent(render);
|
SDL_RenderPresent(render);
|
||||||
|
|||||||
Reference in New Issue
Block a user