Skip to content

Commit 776601d

Browse files
added incomplete bullet code
1 parent 2b97b0c commit 776601d

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/bullet.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "bullet.h"
2+
#include "vector.h"
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include <SDL2/SDL.h>
7+
8+
#define MAX_BULLETS 50
9+
#define BULLET_SPEED 10
10+
#define TRAIL_LENGTH 20
11+
12+
bullet_t* bullets[MAX_BULLETS];
13+
14+
void bullet_init(vec2f* pos, float angle)
15+
{
16+
bullet_t* b = NULL;
17+
for(int i=0; i < MAX_BULLETS; i++)
18+
{
19+
if(bullets[i] == NULL)
20+
{
21+
bullets[i] = malloc(sizeof(bullet_t));
22+
b = bullets[i];
23+
break;
24+
}
25+
}
26+
if(b == NULL)
27+
{
28+
fprintf(stderr, "too many bullets added to static list (anna is lazy)\n");
29+
return;
30+
}
31+
memcpy(&b->position, pos, sizeof(vec2f));
32+
memcpy(&b->initial_position, pos, sizeof(vec2f));
33+
34+
printf("bullet added at %.2f %.2f\n", b->initial_position.x, b->initial_position.y);
35+
36+
vec2fNormalisedVectorFromAngleDegrees(&b->velocity, angle);
37+
vec2fScalarProduct(&b->velocity, &b->velocity, BULLET_SPEED);
38+
}
39+
40+
void bullet_update(void)
41+
{
42+
for (int i=0; i < MAX_BULLETS; i++)
43+
{
44+
if (bullets[i] != NULL)
45+
{
46+
vec2fAdd(&bullets[i]->position, &bullets[i]->position, &bullets[i]->velocity);
47+
}
48+
}
49+
}
50+
51+
void bullet_draw(SDL_Renderer* r)
52+
{
53+
for(int i=0; i < MAX_BULLETS; i++)
54+
{
55+
if(bullets[i] != NULL)
56+
{
57+
vec2f head;
58+
vec2f tail;
59+
head.x = bullets[i]->position.x;
60+
head.y = bullets[i]->position.y;
61+
tail.x = bullets[i]->initial_position.x;
62+
tail.y = bullets[i]->initial_position.y;
63+
64+
if(vec2fDist(&head, &tail) < TRAIL_LENGTH)
65+
{
66+
SDL_RenderDrawLine(r, head.x, head.y, tail.x, tail.y);
67+
printf("1: %.2f %.2f %.2f %.2f\n", head.x, head.y, tail.x, tail.y);
68+
}
69+
else
70+
{
71+
vec2f newtail;
72+
vec2fDiff(&newtail, &head, &tail);
73+
vec2fClampMagnitude(&newtail, &newtail, TRAIL_LENGTH);
74+
SDL_RenderDrawLine(r, head.x, head.y, newtail.x, newtail.y);
75+
printf("1: %.2f %.2f %.2f %.2f\n", head.x, head.y, newtail.x, newtail.y);
76+
}
77+
}
78+
}
79+
}

src/bullet.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef BULLET_H
2+
#define BULLET_H
3+
4+
#include "vector.h"
5+
#include <SDL2/SDL.h>
6+
7+
typedef struct{
8+
vec2f position;
9+
vec2f velocity;
10+
vec2f initial_position;
11+
}bullet_t;
12+
13+
14+
void bullet_init(vec2f* pos, float angle);
15+
void bullet_draw(SDL_Renderer* r);
16+
void bullet_update(void);
17+
18+
#endif

src/gamescreen.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <time.h>
1515
#include <SDL2/SDL_ttf.h>
1616
#include <SDL2/SDL_mixer.h>
17+
#include "bullet.h"
1718

1819
#define BG_STAR_AMOUNT 30
1920
#define BG_STAR_SIZE 2
@@ -101,6 +102,8 @@ char game_update(void)
101102
case SDL_SCANCODE_LEFT:
102103
turn = TURN_LEFT;
103104
break;
105+
/* case SDL_SCANCODE_SPACE: */
106+
/* bullet_init(&player.position, player.rotation); */
104107
default:
105108
break;
106109
}
@@ -136,6 +139,7 @@ char game_update(void)
136139
player_update(&player, accelerate, turn);
137140
asteroid_all_update(&player);
138141
scrapyards_update(&player);
142+
/* bullet_update(); */
139143

140144
if(player.health <= 0)
141145
return 0;
@@ -159,6 +163,10 @@ void game_draw(void)
159163

160164
scrapyards_draw(r);
161165

166+
/* SDL_SetRenderDrawColor(r, 255, 255, 255, 255); */
167+
/* bullet_draw(r); */
168+
/* SDL_SetRenderDrawColor(r, 0, 0, 0, 255); */
169+
162170
// copy texture to back buffer
163171
player_draw(&player, r);
164172

0 commit comments

Comments
 (0)