GCC Code Coverage Report


Directory: src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 71 / 0 / 71
Functions: 100.0% 9 / 0 / 9
Branches: 100.0% 22 / 0 / 22

graphic/src/graph.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 "graph.h"
11 #include <GL/glut.h>
12 #include <time.h>
13
14 void
15 2 graphCreateImage(sprite_t *sprite)
16 {
17
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 if (!sprite)
18 {
19 1 return;
20 }
21
22 1 glGenTextures(1, &sprite->textureId);
23 1 glBindTexture(GL_TEXTURE_2D, sprite->textureId);
24 1 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sprite->width, sprite->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, sprite->image);
25 1 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
26 1 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
27 }
28
29 void
30 2 graphDestroyImage(sprite_t *sprite)
31 {
32
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 if (!sprite)
33 {
34 1 return;
35 }
36
37 1 glDeleteTextures(1, &(sprite->textureId));
38 }
39
40 sprite_t *
41 2 graphGetSprite(base_t *b)
42 {
43
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 if (!b)
44 {
45 1 return NULL;
46 }
47 1 return &b->sprite;
48 }
49
50 int
51 4 graphGetSpriteCoordinates(const sprite_t *const sprite, sprite_coordinates_t *coordinates)
52 {
53
4/4
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 2 times.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1 time.
4 if (!sprite || !coordinates)
54 {
55 3 return 1;
56 }
57
58 1 coordinates->x1 = sprite->x;
59 1 coordinates->x2 = sprite->x + sprite->width;
60 1 coordinates->y1 = sprite->y;
61 1 coordinates->y2 = sprite->y + sprite->height;
62
63 1 return 0;
64 }
65
66 void
67 3 graphNextImageToPrint(sprite_t *sprite)
68 {
69
4/4
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 6 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 1 time.
3 if (sprite && sprite->num_frames > 1)
70 {
71 1 sprite->selected_image = (sprite->selected_image + 1) % sprite->num_frames;
72 1 size_t image_offset = sprite->selected_image * sprite->height * sprite->width * NUM_RGBA_CHANNELS;
73 1 sprite->image = sprite->image_base + image_offset;
74 1 glBindTexture(GL_TEXTURE_2D, sprite->textureId);
75 1 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sprite->width, sprite->height, GL_RGBA, GL_UNSIGNED_BYTE, sprite->image);
76 }
77 3 }
78
79 void
80 2 graphUpdateTimeSprite(sprite_t *sprite)
81 {
82
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 if (!sprite)
83 {
84 1 return;
85 }
86
87 1 struct timespec current_time;
88 1 clock_gettime(CLOCK_MONOTONIC, &current_time);
89 1 sprite->last_update = current_time;
90 }
91
92 void
93 3 graphPrintImage(const sprite_t *const sprite)
94 {
95
4/4
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1 time.
3 if (!sprite || !sprite->visible)
96 {
97 2 return;
98 }
99
100 1 glBindTexture(GL_TEXTURE_2D, sprite->textureId);
101 1 glBegin(GL_QUADS);
102 1 glTexCoord2f(0.0, 1.0);
103 1 glVertex2i(sprite->x, sprite->y);
104 1 glTexCoord2f(1.0, 1.0);
105 1 glVertex2i(sprite->x + sprite->width, sprite->y);
106 1 glTexCoord2f(1.0, 0.0);
107 1 glVertex2i(sprite->x + sprite->width, sprite->y + sprite->height);
108 1 glTexCoord2f(0.0, 0.0);
109 1 glVertex2i(sprite->x, sprite->y + sprite->height);
110 1 glEnd();
111 }
112
113 void
114 2 graphUpdateSprite(const sprite_t *const sprite)
115 {
116
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 if (!sprite)
117 {
118 1 return;
119 }
120
121 1 glBindTexture(GL_TEXTURE_2D, sprite->textureId);
122 1 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sprite->width, sprite->height, GL_RGBA, GL_UNSIGNED_BYTE, sprite->image);
123 }
124
125 void
126 1 graphPrintBorder(int x, int y, int w, int h)
127 {
128 1 GLubyte color[NUM_RGBA_CHANNELS] = W;
129
130 1 glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT);
131
132 1 glDisable(GL_TEXTURE_2D);
133 1 glColor4ubv(color);
134 1 glLineWidth(2.0f);
135
136 1 glBegin(GL_LINE_LOOP);
137 1 glVertex2i(x, y);
138 1 glVertex2i(x + w, y);
139 1 glVertex2i(x + w, y + h);
140 1 glVertex2i(x, y + h);
141 1 glEnd();
142
143 1 glPopAttrib(); // restaura todo
144 1 }
145