DungeonCrawl
Loading...
Searching...
No Matches
media_output.c
Go to the documentation of this file.
1
5#include "media_output.h"
6
8#include "../../io_handler.h" // Include this to access global nc and stdplane
9#include "../common/output_handler.h"// For get_screen_dimensions and render_frame
10#include "media_files.h"
12
13#include <stdbool.h>
14#include <stdlib.h>
15#include <string.h>
16
17
18/* =========================================================================
19 * FORWARD DECLARATIONS
20 * ========================================================================= */
21
22// Forward declarations for internal functions
23static bool display_image(loaded_visual_t* resource);
24
25//static bool display_animation(loaded_visual_t *resource, float fps, bool loop);
26
27/* =========================================================================
28 * PNG DISPLAY FUNCTIONS
29 * ========================================================================= */
30
31// Display a PNG file at specified coordinates with scaling
32bool display_image_at(const char* filename, int x, int y, int height, int width, scale_type_t scale_type) {
33 // Validate parameters
34 if (!filename || height < 0) {
35 // Allow height=0 for automatic scaling
36 log_msg(ERROR, "media_output", "Invalid parameters for display_png_at");
37 return false;
38 }
39 loaded_visual_t* resource = ready_media(filename, x, y, height, width, scale_type);
40 if (!resource) {
41 log_msg(ERROR, "media_output", "Failed to load image for display");
42 return false;
43 }
44 return display_image(resource);
45}
46
47// Fill the background with a PNG file scaled to terminal size
48/*bool display_image_background(const char* filename) {
49 // coming soon...
50 return false;
51}*/
52
53// Fill a single terminal cell with a PNG image
54bool display_image_cell(const char* filename, int x, int y) {
55 // Validate parameters
56 if (!filename) {
57 log_msg(ERROR, "media_output", "Invalid filename for fill_cell_with_png");
58 return false;
59 }
60 loaded_visual_t* resource = ready_media(filename, x, y, 1, 1, SCALE_CELL);
61 if (!resource) {
62 log_msg(ERROR, "media_output", "Failed to load image for cell");
63 return false;
64 }
65 return display_image(resource);
66}
67
68/* =========================================================================
69 * ANIMATION DISPLAY FUNCTIONS
70 * ========================================================================= */
71
72// Display a GIF file at specified coordinates with scaling
73/*bool display_gif_at(const char* filename, int x, int y, int height, int width, scale_type_t scale_type, float fps, bool loop) {
74 // coming soon
75 return false;
76}*/
77
78// Fill the background with a GIF file scaled to terminal size
79/*bool display_gif_background(const char* filename, float fps, bool loop) {
80 // comign soon
81 return false;
82}*/
83
84/*bool display_video_at(const char* filename, int x, int y, int width, int height, scale_type_t scale) {
85 // coming soon...
86 return false;
87}*/
88
89/*bool display_video_background(const char* filename, float fps, bool loop) {
90 // coming soon...
91 return false;
92}*/
93
94/* =========================================================================
95 * ANIMATION CONTROL FUNCTIONS
96 * ========================================================================= */
97
98/*void media_output_play(loaded_visual_t* media) {
99 //coming soon...
100}*/
101
102/*void media_output_pause(loaded_visual_t* media) {
103 //coming soon...
104}*/
105
106/*void media_output_reset(loaded_visual_t* media) {
107 //coming soon...
108}*/
109
110/* =========================================================================
111* INTERNAL DISPLAY FUNCTIONS
112* ========================================================================= */
113
120static bool display_image(loaded_visual_t* resource) {
121 // Validate parameters
122 if (!resource || !resource->visual) {
123 log_msg(ERROR, "media_output", "Invalid parameters for display_image");
124 return false;
125 }
126
127 if (resource->media_type == MEDIA_GIF) {
128 // If it's a GIF, display it as an animation with default 10 FPS
129 return false;//TODO: return display_animation(resource, 10.0f, false);
130 }
131
132 // Clean up existing plane if needed
133 if (resource->plane) {
134 ncplane_destroy(resource->plane);
135 resource->plane = NULL;
136 }
137
138 // Create a direct plane for the image
139 struct ncplane_options opts = {0};
140 opts.y = resource->options.y;
141 opts.x = resource->options.x;
142 opts.rows = resource->options.leny > 0 ? resource->options.leny : resource->og_height;
143 opts.cols = resource->options.lenx > 0 ? resource->options.lenx : resource->og_width;
144
145 resource->plane = ncplane_create(stdplane, &opts);
146 if (!resource->plane) {
147 log_msg(ERROR, "media_output", "Failed to create plane for image at (%d, %d)",
148 resource->options.x, resource->options.y);
149 return false;
150 }
151
152 // Set up visual options for direct blitting
153 struct ncvisual_options vopts = {0};
154 vopts.n = resource->plane;
155 vopts.y = 0;// Relative to the plane
156 vopts.x = 0;// Relative to the plane
157 vopts.scaling = resource->options.scaling;
158 vopts.blitter = NCBLIT_2x2;// Simple blitter that works better
159
160 // Use direct blit
161 if (!ncvisual_blit(nc, resource->visual, &vopts)) {
162 log_msg(ERROR, "media_output", "Failed to blit visual to plane");
163 ncplane_destroy(resource->plane);
164 resource->plane = NULL;
165 return false;
166 }
167
168 // Make sure the plane is visible
169 ncplane_move_top(resource->plane);
170
171 // Make sure changes are visible - force a render
172 notcurses_render(nc);// Directly call notcurses_render for maximum compatibility
173
174 return true;
175}
176
177// Helper function for displaying animations
178/*static bool display_animation(loaded_visual_t *resource, float fps, bool loop) {
179 // coming soon
180 return false;
181}*/
Exposes functions for the IO-Handler.
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246
Header file for logging functionality of the game.
Defines the path and metadata to media files.
bool display_image_cell(const char *filename, int x, int y)
Fill a single terminal cell with an image.
bool display_image_at(const char *filename, int x, int y, int height, int width, scale_type_t scale_type)
Display an image file at specified coordinates with scaling.
Exposes functions for drawing to the screen.
loaded_visual_t * ready_media(const char *filename, int x, int y, int height, int width, scale_type_t scale_type)
Prepares a media resource for display.
Exposes functions for the media output handler.
struct loaded_visual_s loaded_visual_t
Structure to represent a loaded visual.
Exposes functions for outputting to the console.