File: DebugWindow.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (93 lines) | stat: -rw-r--r-- 2,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once

#include "globalincs/pstypes.h"
#include "osapi/osapi.h"

namespace osapi {

/**
 * @brief A debug window
 *
 * A debug window is a separate os::Viewport which displays additional debug information at runtime.
 */
class DebugWindow {
	/**
	 * A single line in the debug log can have multiple properties which are stored in this structure
	 */
	struct LineInfo {
		SCP_string category;
		SCP_string text;
	};

	os::Viewport* debug_view = nullptr; //!< The viewport in which the debug output is shown
	SDL_Window* debug_sdl_window = nullptr; //!< The SDL window handle of the debug view

	SCP_vector<LineInfo> lines; //!< Finished lines

	SCP_string current_category; //!< The category of the current line
	SCP_string current_line; //!< Line that is currently being constructed, may be empty

	int max_category_width = 0; //!< The maximum width of any category string encountered thus far

	size_t log_view_offset = 0; //!< The offset into the @c lines array of the last message to display

	/**
	 * @brief Adds the specified line to the log
	 * @param line The line
	 */
	void addToLog(LineInfo&& line);

	/**
	 * @brief Prints a line of the log to the screen
	 * @param bottom_y The bottom position of where the text should be drawn
	 * @param line The line to draw
	 * @return The new bottom position for the next line
	 */
	float print_line(float bottom_y, const LineInfo& line);

	/**
	 * @brief Splits current_line into individual strings and adds them to the log
	 *
	 * Since debug output can contain multiple newlines in one string we need to split them before storing them in the vector
	 *
	 * @param category The category to use for these lines
	 */
	void split_current_and_add_to_log(const SCP_string& category);

	/**
	 * @brief Event handler for window events
	 *
	 * @details This handles the keyboard events for the debug window
	 *
	 * @param evt The keyboard event
	 * @return @c true if the event was handled, @c false otherwise
	 */
	bool debug_key_handler(const SDL_Event& evt);
 public:
	/**
	 * @brief Initializes the instance
	 */
	DebugWindow();

	~DebugWindow();

	/**
	 * @brief Renders the contents of the debug log
	 *
	 * This does all the viewport switching and page flipping automatically
	 *
	 * @param frametime The time the last frame took to process
	 */
	void doFrame(float frametime);

	/**
	 * @brief Adds a debug log line to the buffer
	 *
	 * @param category The category of the specified text
	 * @param text The text to print to the debug log
	 */
	void addDebugMessage(const char* category, const char* text);
};

}