summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2026-01-02 14:59:29 +0100
committerCarlos Maiolino <[email protected]>2026-01-02 14:59:29 +0100
commit0ef17f4387bc95fad289a7d29c28703743072214 (patch)
tree0f66e4199637ae0f6ae092efb0931df809cba89c
parentb44a1bf90f4336833f8ade39d053b9d917ede848 (diff)
Redraw ship using transformationsHEADmaster
Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--Makefile7
-rw-r--r--src/include/rb.h5
-rw-r--r--src/rb.c65
-rw-r--r--src/spaceship.c71
4 files changed, 88 insertions, 60 deletions
diff --git a/Makefile b/Makefile
index ee2e518..49a6f81 100644
--- a/Makefile
+++ b/Makefile
@@ -11,11 +11,14 @@ INCLUDES= -I$(RB_LIBS_DIR)
OBJS= ./build/*.o
all: $(BUILD_DIR)/rb.o
- gcc -Wall $(INCLUDES) $(OBJS) $(ALLEGRO_LIBS) -o $(BIN_DIR)/rocksblaster
+ gcc -Wall $(INCLUDES) -lm $(OBJS) $(ALLEGRO_LIBS) -o $(BIN_DIR)/rocksblaster
-$(BUILD_DIR)/rb.o: src/rb.c
+$(BUILD_DIR)/rb.o: $(BUILD_DIR)/spaceship.o src/rb.c
gcc -Wall $(INCLUDES) -c src/rb.c -o $(BUILD_DIR)/rb.o
+$(BUILD_DIR)/spaceship.o: src/spaceship.c
+ gcc -Wall $(INCLUDES) -c src/spaceship.c -o $(BUILD_DIR)/spaceship.o
+
clean:
rm -f $(BUILD_DIR)/*.o
rm -f $(BIN_DIR)/rocksblaster
diff --git a/src/include/rb.h b/src/include/rb.h
index 0752bc2..22f2981 100644
--- a/src/include/rb.h
+++ b/src/include/rb.h
@@ -2,9 +2,10 @@
#define RB_H
#include <allegro5/allegro5.h>
+#include <allegro5/allegro_font.h>
-#define RB_WIDTH 1600
-#define RB_HEIGHT 900
+#define RB_WIDTH 1792
+#define RB_HEIGHT 1344
#define RB_X_CENTER (RB_WIDTH / 2)
#define RB_Y_CENTER (RB_HEIGHT / 2)
#define RB_FPS (1.0 / 80.0) /* 1 sec divided by 30 frames */
diff --git a/src/rb.c b/src/rb.c
index a478671..fdef5f0 100644
--- a/src/rb.c
+++ b/src/rb.c
@@ -3,7 +3,12 @@
#include <allegro5/allegro_primitives.h>
#include <stdlib.h>
#include <stdio.h>
+
#include <rb.h>
+#include <spaceship.h>
+
+long frames;
+long score;
ALLEGRO_DISPLAY *
init_allegro_display(struct rb *rb)
@@ -112,6 +117,21 @@ register_events(struct rb *rb)
}
void
+rb_draw_status(struct rb *rb, struct spaceship *sp,
+ float x, float y, float dx, float dy)
+{
+ ALLEGRO_TRANSFORM transform;
+ al_identity_transform(&transform);
+ al_rotate_transform(&transform, 0);
+ al_translate_transform(&transform, 0, 0);
+ al_use_transform(&transform);
+ al_draw_textf(rb->font, al_map_rgb(255, 255, 255),
+ 0, 0, 0,
+ "X: %.1f Y: %.1f DX: %f DY: %f HDG: %f",
+ sp->x, sp->y, dx, dy, sp->heading);
+}
+
+void
start_game(struct rb *rb)
{
#define KEY_SEEN 1
@@ -123,6 +143,10 @@ start_game(struct rb *rb)
float dx, dy; /* mouse velocity - change in coordinate since prev event*/
ALLEGRO_EVENT event;
unsigned char key[ALLEGRO_KEY_MAX];
+ struct spaceship sp = {0};
+
+ rb_ship_init(&sp);
+
memset(key, 0, sizeof(key));
@@ -140,25 +164,13 @@ start_game(struct rb *rb)
x += dx;
y += dy;
- if (x < 0) {
- x *= -1;
- dx *= -1;
- }
-
- if (x > rb->width) {
- x -= (x - rb->width) * 2;
- dx *= -1;
- }
+ if(key[ALLEGRO_KEY_UP])
+ rb_ship_accelerate(&sp);
- if (y < 0) {
- y *= -1;
- dy *= -1;
- }
-
- if (y > rb->height) {
- y -= (y - rb->height) * 2;
- dy *= -1;
- }
+ if(key[ALLEGRO_KEY_LEFT])
+ rb_ship_rotate_left(&sp);
+ if(key[ALLEGRO_KEY_RIGHT])
+ rb_ship_rotate_right(&sp);
if (key[ALLEGRO_KEY_ESCAPE])
done = true;
@@ -175,8 +187,6 @@ start_game(struct rb *rb)
case ALLEGRO_EVENT_MOUSE_AXES:
dx += event.mouse.dx * 0.1;
dy += event.mouse.dy * 0.1;
- al_set_mouse_xy(rb->display, RB_X_CENTER, RB_Y_CENTER);
- break;
case ALLEGRO_EVENT_KEY_DOWN:
key[event.keyboard.keycode] = KEY_SEEN | KEY_DOWN;
@@ -185,20 +195,18 @@ start_game(struct rb *rb)
key[event.keyboard.keycode] &= ~KEY_DOWN;
break;
-#if 0
case ALLEGRO_EVENT_KEY_CHAR:
if (event.keyboard.keycode == ALLEGRO_KEY_UP)
- y--;
+ rb_ship_accelerate(&sp);
if (event.keyboard.keycode == ALLEGRO_KEY_DOWN)
y++;
if (event.keyboard.keycode == ALLEGRO_KEY_LEFT)
- x--;
+ rb_ship_rotate_left(&sp);
if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT)
- x++;
+ rb_ship_rotate_right(&sp);
if (event.keyboard.keycode != ALLEGRO_KEY_ESCAPE)
break;
-#endif
case ALLEGRO_EVENT_DISPLAY_CLOSE:
done = true;
@@ -210,11 +218,8 @@ start_game(struct rb *rb)
if (redraw && al_is_event_queue_empty(rb->event_queue)) {
al_clear_to_color(al_map_rgb(0, 0, 0));
- al_draw_textf(rb->font, al_map_rgb(255, 255, 255),
- 0, 0, 0,
- "X: %.1f Y: %.1f DX: %f DY: %f", x, y, dx, dy);
- al_draw_filled_rectangle(x, y, x+40, y+120,
- al_map_rgb(255, 0, 0));
+ rb_draw_status(rb, &sp, x, y, dx, dy);
+ rb_draw_ship(&sp);
al_flip_display();
redraw = false;
diff --git a/src/spaceship.c b/src/spaceship.c
index e92c4bb..2abf321 100644
--- a/src/spaceship.c
+++ b/src/spaceship.c
@@ -1,6 +1,7 @@
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <stdio.h>
+#include <math.h>
#include <rb.h>
#include <spaceship.h>
@@ -10,44 +11,62 @@ rb_ship_init(struct spaceship *sp)
{
sp->x = RB_X_CENTER;
sp->y = RB_Y_CENTER;
- sp->heading = 24;
- sp->speed = 0;
- sp->rot_velocity = 0;
+ sp->heading = 0;
+ sp->speed = 3;
+ sp->rot_velocity = 1;
sp->scale = 0;
sp->alive = 1;
sp->color = al_map_rgb(255, 255, 255);
printf("ship initialized\n");
}
-#if 0
void
-rb_ship_accelerate(struct spaceship *sp,
- float x, float, y)
+rb_ship_accelerate(struct spaceship *sp)
{
+ sp->x += sin(sp->heading * ALLEGRO_PI / 180) * sp->speed;
+ if (sp->x >= RB_WIDTH)
+ sp->x = 0;
+ if (sp->x < 0)
+ sp->x = RB_WIDTH;
+ sp->y += -cos(sp->heading * ALLEGRO_PI / 180) * sp->speed;
+ if (sp->y >= RB_HEIGHT)
+ sp->y = 0;
+ if (sp->y < 0)
+ sp->y = RB_HEIGHT;
+}
+
+void
+rb_ship_rotate_left(struct spaceship *sp)
+{
+ if (sp->heading == 0)
+ sp->heading = 360;
+
+ sp->heading -= sp->rot_velocity;
+
+}
+
+void
+rb_ship_rotate_right(struct spaceship *sp)
+{
+ if (sp->heading >= 360)
+ sp->heading = 0;
+
+ sp->heading += sp->rot_velocity;
+
}
-#endif
void
rb_draw_ship(struct spaceship *sp)
{
- ALLEGRO_TRANSFORM transform;
- al_identity_transform(&transform);
- al_rotate_transform(&transform, (ALLEGRO_PI / 180) * sp->heading);
- al_translate_transform(&transform, sp->x, sp->y);
- al_use_transform(&transform);
-
- al_draw_line(RB_X_CENTER - 8, RB_Y_CENTER + 9,
- RB_X_CENTER, RB_Y_CENTER - 11,
- sp->color, 3.0f);
-
- al_draw_line(RB_X_CENTER, RB_Y_CENTER - 11,
- RB_X_CENTER + 8, RB_Y_CENTER + 9,
- sp->color, 3.0f);
- al_draw_line(RB_X_CENTER - 6, RB_Y_CENTER + 4,
- RB_X_CENTER - 1, RB_Y_CENTER + 4,
- sp->color, 3.0f);
- al_draw_line(RB_X_CENTER + 6, RB_Y_CENTER + 4,
- RB_X_CENTER + 1, RB_Y_CENTER + 4,
- sp->color, 3.0f);
+ float scale = 1.9;
+ ALLEGRO_TRANSFORM t;
+ al_build_transform(&t,sp->x, sp->y, scale, scale,
+ (ALLEGRO_PI) / 180 * sp->heading);
+
+ al_use_transform(&t);
+ al_draw_line(-8, 9, 0, -11, sp->color, 3.0f);
+ al_draw_line(0, -11, 8, 9, sp->color, 3.0f);
+ al_draw_line(-6, 4, -1, 4, sp->color, 3.0f);
+ al_draw_line(6, 4, 1, 4, sp->color, 3.0f);
}