spaceship/src/spaceship.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * SPDX-License-Identifier: MIT | ||
| 3 | * | ||
| 4 | * Copyright (c) 2025 Manuel Hernández Méndez | ||
| 5 | * | ||
| 6 | * Authors: | ||
| 7 | * Manuel Hernández Méndez <maherme.dev@gmail.com> | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include "spaceship.h" | ||
| 11 | #include "graph.h" | ||
| 12 | #include "graphGlutCallbacks.h" | ||
| 13 | #include "utils.h" | ||
| 14 | #include <stdbool.h> | ||
| 15 | |||
| 16 | #define SPACESHIP_WIDTH 13 | ||
| 17 | #define SPACESHIP_HEIGHT 8 | ||
| 18 | |||
| 19 | struct spaceship_instance_t | ||
| 20 | { | ||
| 21 | sprite_t sprite; | ||
| 22 | bool alive; | ||
| 23 | }; | ||
| 24 | |||
| 25 | static struct spaceship_instance_t spaceship; | ||
| 26 | |||
| 27 | static const char spaceshipImage[SPACESHIP_HEIGHT][SPACESHIP_WIDTH][NUM_RGBA_CHANNELS] = { | ||
| 28 | {B, B, B, B, B, B, G, B, B, B, B, B, B}, | ||
| 29 | {B, B, B, B, B, G, G, G, B, B, B, B, B}, | ||
| 30 | {B, B, B, B, B, G, G, G, B, B, B, B, B}, | ||
| 31 | {B, G, G, G, G, G, G, G, G, G, G, G, B}, | ||
| 32 | {G, G, G, G, G, G, G, G, G, G, G, G, G}, | ||
| 33 | {G, G, G, G, G, G, G, G, G, G, G, G, G}, | ||
| 34 | {B, B, G, G, B, B, B, B, B, G, G, B, B}, | ||
| 35 | {B, G, G, G, G, B, B, B, G, G, G, G, B}, | ||
| 36 | }; | ||
| 37 | |||
| 38 | spaceship_t | ||
| 39 | 1 | spaceshipCreate(int x, int y) | |
| 40 | { | ||
| 41 | 1 | spaceship_t inst = &spaceship; | |
| 42 | 1 | inst->sprite.x = x; | |
| 43 | 1 | inst->sprite.y = y; | |
| 44 | 1 | inst->sprite.width = SPACESHIP_WIDTH; | |
| 45 | 1 | inst->sprite.height = SPACESHIP_HEIGHT; | |
| 46 | 1 | inst->sprite.image = (const char *)spaceshipImage; | |
| 47 | 1 | inst->sprite.pixels_to_move = 1; | |
| 48 | 1 | inst->sprite.time_to_move = 1000 / 60 * NS_PER_MS; | |
| 49 | 1 | inst->sprite.max_movement.right = GAME_WIDTH; | |
| 50 | 1 | inst->sprite.max_movement.left = 0; | |
| 51 | 1 | inst->sprite.layer = LAYER_GAME_OBJECTS; | |
| 52 | 1 | inst->sprite.visible = true; | |
| 53 | 1 | graphRegisterPrint(&inst->sprite); | |
| 54 | 1 | inst->alive = true; | |
| 55 | |||
| 56 | 1 | return inst; | |
| 57 | } | ||
| 58 | |||
| 59 | void | ||
| 60 | 2 | spaceshipDestroy(spaceship_t spaceship) | |
| 61 | { | ||
| 62 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
|
2 | if (!spaceship) |
| 63 | { | ||
| 64 | 1 | return; | |
| 65 | } | ||
| 66 | |||
| 67 | 1 | sprite_t *spaceship_sprite = graphGetSprite((base_t *)spaceship); | |
| 68 | 1 | graphUnregisterPrint(spaceship_sprite); | |
| 69 | 1 | spaceship->alive = false; | |
| 70 | } | ||
| 71 | |||
| 72 | bool | ||
| 73 | 5 | spaceshipAlive(spaceship_t spaceship) | |
| 74 | { | ||
| 75 |
4/4✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 2 times.
|
5 | return (spaceship && spaceship->alive); |
| 76 | } | ||
| 77 | |||
| 78 | #ifdef UNIT_TESTING | ||
| 79 | #include <string.h> | ||
| 80 | |||
| 81 | spaceship_t | ||
| 82 | 3 | helperUT_spaceshipGetInstance(void) | |
| 83 | { | ||
| 84 | 3 | spaceship_t inst = &spaceship; | |
| 85 | 3 | memset(inst, 0, sizeof(*inst)); | |
| 86 | |||
| 87 | 3 | inst->alive = true; | |
| 88 | |||
| 89 | 3 | return inst; | |
| 90 | } | ||
| 91 | |||
| 92 | void | ||
| 93 | 1 | helperUT_spaceshipSetAlive(spaceship_t spaceship, bool alive) | |
| 94 | { | ||
| 95 | 1 | spaceship->alive = alive; | |
| 96 | 1 | } | |
| 97 | #endif /* UNIT_TESTING */ | ||
| 98 |