graphicglut/src/graphGlutCallbacks.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 "graphGlutCallbacks.h" | ||
| 11 | #include "graph.h" | ||
| 12 | #include "list.h" | ||
| 13 | #include "utils.h" | ||
| 14 | #include <GL/glut.h> | ||
| 15 | |||
| 16 | typedef struct ctx_node | ||
| 17 | { | ||
| 18 | sprite_t *sprite; | ||
| 19 | struct list_head node; | ||
| 20 | } ctx_node_t; | ||
| 21 | |||
| 22 | struct list_head render_layers[NUM_RENDER_LAYERS]; | ||
| 23 | |||
| 24 | typedef struct viewport | ||
| 25 | { | ||
| 26 | int x, y; | ||
| 27 | int w, h; | ||
| 28 | } viewport_t; | ||
| 29 | |||
| 30 | static struct | ||
| 31 | { | ||
| 32 | int width; | ||
| 33 | int height; | ||
| 34 | viewport_t game_vp; | ||
| 35 | viewport_t hud_vp; | ||
| 36 | } window; | ||
| 37 | |||
| 38 | void | ||
| 39 | 8 | graphRegisterPrintInit(void) | |
| 40 | { | ||
| 41 |
2/2✓ Branch 5 → 3 taken 32 times.
✓ Branch 5 → 6 taken 8 times.
|
40 | for (int i = 0; i < NUM_RENDER_LAYERS; i++) |
| 42 | { | ||
| 43 | 32 | init_list_head(&render_layers[i]); | |
| 44 | } | ||
| 45 | 8 | } | |
| 46 | |||
| 47 | int | ||
| 48 | 8 | graphRegisterPrint(sprite_t *sprite) | |
| 49 | { | ||
| 50 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 7 times.
|
8 | if (!sprite) |
| 51 | { | ||
| 52 | 1 | return -1; | |
| 53 | } | ||
| 54 | |||
| 55 | 7 | ctx_node_t *n = utilsCalloc(1, sizeof(*n)); | |
| 56 | 7 | n->sprite = sprite; | |
| 57 | 7 | list_add_tail(&n->node, &render_layers[sprite->layer]); | |
| 58 | 7 | graphCreateImage(sprite); | |
| 59 | |||
| 60 | 7 | return 0; | |
| 61 | } | ||
| 62 | |||
| 63 | int | ||
| 64 | 5 | graphUnregisterPrint(sprite_t *sprite) | |
| 65 | { | ||
| 66 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 4 times.
|
5 | if (!sprite) |
| 67 | { | ||
| 68 | 1 | return -1; | |
| 69 | } | ||
| 70 | |||
| 71 | 4 | int removed = 0; | |
| 72 | |||
| 73 |
2/2✓ Branch 13 → 5 taken 16 times.
✓ Branch 13 → 14 taken 4 times.
|
20 | for (int i = 0; i < NUM_RENDER_LAYERS; i++) |
| 74 | { | ||
| 75 | ctx_node_t *n, *tmp; | ||
| 76 | |||
| 77 |
2/2✓ Branch 11 → 6 taken 6 times.
✓ Branch 11 → 12 taken 16 times.
|
22 | list_for_each_entry_safe(n, tmp, &render_layers[i], node) |
| 78 | { | ||
| 79 |
2/2✓ Branch 6 → 7 taken 3 times.
✓ Branch 6 → 10 taken 3 times.
|
6 | if (n->sprite == sprite) |
| 80 | { | ||
| 81 | 3 | graphDestroyImage(n->sprite); | |
| 82 | 3 | list_del(&n->node); | |
| 83 | 3 | UTILS_FREE(n); | |
| 84 | 3 | removed++; | |
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | 4 | return removed; | |
| 90 | } | ||
| 91 | |||
| 92 | static void | ||
| 93 | 2 | drawLayers(int first, int last) | |
| 94 | { | ||
| 95 |
2/2✓ Branch 8 → 3 taken 4 times.
✓ Branch 8 → 9 taken 2 times.
|
6 | for (int i = first; i <= last; i++) |
| 96 | { | ||
| 97 | ctx_node_t *n; | ||
| 98 |
2/2✓ Branch 6 → 4 taken 2 times.
✓ Branch 6 → 7 taken 4 times.
|
6 | list_for_each_entry(n, &render_layers[i], node) |
| 99 | { | ||
| 100 | 2 | graphPrintImage(n->sprite); | |
| 101 | } | ||
| 102 | } | ||
| 103 | 2 | } | |
| 104 | |||
| 105 | static void | ||
| 106 | 1 | setupGameViewport(void) | |
| 107 | { | ||
| 108 | 1 | glViewport(window.game_vp.x, window.game_vp.y, window.game_vp.w, window.game_vp.h); | |
| 109 | |||
| 110 | 1 | glMatrixMode(GL_PROJECTION); | |
| 111 | 1 | glLoadIdentity(); | |
| 112 | 1 | gluOrtho2D(0, GAME_WIDTH, 0, GAME_HEIGHT); | |
| 113 | |||
| 114 | 1 | glMatrixMode(GL_MODELVIEW); | |
| 115 | 1 | glLoadIdentity(); | |
| 116 | 1 | } | |
| 117 | |||
| 118 | static void | ||
| 119 | 1 | setupHudViewport(void) | |
| 120 | { | ||
| 121 | 1 | glViewport(window.hud_vp.x, window.hud_vp.y, window.hud_vp.w, window.hud_vp.h); | |
| 122 | |||
| 123 | 1 | glMatrixMode(GL_PROJECTION); | |
| 124 | 1 | glLoadIdentity(); | |
| 125 | 1 | gluOrtho2D(0, GAME_WIDTH, 0, HUD_HEIGHT); | |
| 126 | |||
| 127 | 1 | glMatrixMode(GL_MODELVIEW); | |
| 128 | 1 | glLoadIdentity(); | |
| 129 | 1 | } | |
| 130 | |||
| 131 | void | ||
| 132 | 1 | graphGlutDisplay(void) | |
| 133 | { | ||
| 134 | 1 | glClear(GL_COLOR_BUFFER_BIT); | |
| 135 | |||
| 136 | 1 | setupGameViewport(); | |
| 137 | 1 | drawLayers(LAYER_BACKGROUND, LAYER_EFFECTS); | |
| 138 | 1 | graphPrintBorder(0, 0, GAME_WIDTH, GAME_HEIGHT); | |
| 139 | |||
| 140 | 1 | setupHudViewport(); | |
| 141 | 1 | drawLayers(LAYER_HUD, LAYER_HUD); | |
| 142 | 1 | graphPrintBorder(0, 0, GAME_WIDTH, HUD_HEIGHT); | |
| 143 | |||
| 144 | 1 | glutSwapBuffers(); | |
| 145 | 1 | } | |
| 146 | |||
| 147 | static void | ||
| 148 | 2 | calculateViewports(void) | |
| 149 | { | ||
| 150 | 2 | int scale_x = window.width / GAME_WIDTH; | |
| 151 | 2 | int scale_y = window.height / (GAME_HEIGHT + HUD_HEIGHT); | |
| 152 | 2 | int scale = scale_x < scale_y ? scale_x : scale_y; | |
| 153 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
|
2 | if (scale < 1) |
| 154 | { | ||
| 155 | 1 | scale = 1; | |
| 156 | } | ||
| 157 | |||
| 158 | 2 | int game_width = GAME_WIDTH * scale; | |
| 159 | 2 | int game_height = GAME_HEIGHT * scale; | |
| 160 | 2 | int hud_width = game_width; | |
| 161 | 2 | int hud_height = HUD_HEIGHT * scale; | |
| 162 | |||
| 163 | 2 | int vp_x = (window.width - game_width) / 2; | |
| 164 | 2 | int vp_y = (window.height - (game_height + hud_height)) / 2; | |
| 165 | |||
| 166 | 2 | window.game_vp.x = vp_x; | |
| 167 | 2 | window.game_vp.y = hud_height + vp_y; | |
| 168 | 2 | window.game_vp.w = game_width; | |
| 169 | 2 | window.game_vp.h = game_height; | |
| 170 | |||
| 171 | 2 | window.hud_vp.x = vp_x; | |
| 172 | 2 | window.hud_vp.y = vp_y; | |
| 173 | 2 | window.hud_vp.w = hud_width; | |
| 174 | 2 | window.hud_vp.h = hud_height; | |
| 175 | 2 | } | |
| 176 | |||
| 177 | void | ||
| 178 | 2 | graphGlutReshape(int w, int h) | |
| 179 | { | ||
| 180 | 2 | window.width = w; | |
| 181 | 2 | window.height = h; | |
| 182 | |||
| 183 | 2 | calculateViewports(); | |
| 184 | 2 | } | |
| 185 | |||
| 186 | #ifdef UNIT_TESTING | ||
| 187 | void | ||
| 188 | 16 | helperUT_graphGlutResetRegisteredSprites(void) | |
| 189 | { | ||
| 190 |
2/2✓ Branch 8 → 3 taken 64 times.
✓ Branch 8 → 9 taken 16 times.
|
80 | for (int i = 0; i < NUM_RENDER_LAYERS; i++) |
| 191 | { | ||
| 192 |
2/2✓ Branch 6 → 4 taken 6 times.
✓ Branch 6 → 7 taken 64 times.
|
70 | list_clear_with_free(&render_layers[i], ctx_node_t, node); |
| 193 | } | ||
| 194 | 16 | } | |
| 195 | |||
| 196 | bool | ||
| 197 | 14 | helperUT_graphGlutSpriteIsRegistered(sprite_t *sprite) | |
| 198 | { | ||
| 199 |
2/2✓ Branch 9 → 3 taken 26 times.
✓ Branch 9 → 10 taken 4 times.
|
30 | for (int i = 0; i < NUM_RENDER_LAYERS; i++) |
| 200 | { | ||
| 201 | ctx_node_t *n; | ||
| 202 |
2/2✓ Branch 7 → 4 taken 18 times.
✓ Branch 7 → 8 taken 16 times.
|
34 | list_for_each_entry(n, &render_layers[i], node) |
| 203 | { | ||
| 204 |
2/2✓ Branch 4 → 5 taken 10 times.
✓ Branch 4 → 6 taken 8 times.
|
18 | if (n->sprite == sprite) |
| 205 | { | ||
| 206 | 10 | return true; | |
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | 4 | return false; | |
| 212 | } | ||
| 213 | |||
| 214 | void | ||
| 215 | 2 | helperUT_graphGlutInjectSprite(sprite_t *sprite) | |
| 216 | { | ||
| 217 | 2 | ctx_node_t *n = utilsCalloc(1, sizeof(*n)); | |
| 218 | 2 | n->sprite = sprite; | |
| 219 | 2 | list_add(&n->node, &render_layers[sprite->layer]); | |
| 220 | 2 | } | |
| 221 | |||
| 222 | void | ||
| 223 | 1 | helperUT_graphGlutSetGameViewport(void *game_vp) | |
| 224 | { | ||
| 225 | 1 | window.game_vp = *(viewport_t *)game_vp; | |
| 226 | 1 | } | |
| 227 | |||
| 228 | void | ||
| 229 | 1 | helperUT_graphGlutSetHudViewport(void *hud_vp) | |
| 230 | { | ||
| 231 | 1 | window.hud_vp = *(viewport_t *)hud_vp; | |
| 232 | 1 | } | |
| 233 | |||
| 234 | void * | ||
| 235 | 2 | helperUT_graphGlutGetGameViewport(void) | |
| 236 | { | ||
| 237 | 2 | return &window.game_vp; | |
| 238 | } | ||
| 239 | |||
| 240 | void * | ||
| 241 | 2 | helperUT_graphGlutGetHudViewport(void) | |
| 242 | { | ||
| 243 | 2 | return &window.hud_vp; | |
| 244 | } | ||
| 245 | #endif | ||
| 246 |