Fix an intel LG warning by orphaning text memory
[ipdf/code.git] / src / screen.h
1 #ifndef _SCREEN_H
2 #define _SCREEN_H
3
4 #include <SDL.h>
5
6 #include <functional>
7
8 #include "stb_truetype.h"
9 #include "graphicsbuffer.h"
10 #include "shaderprogram.h"
11
12 namespace IPDF
13 {
14         /*
15          * The "Screen" class handles managing the OS window (using SDL2).
16          */
17         class Screen
18         {
19         public:
20                 Screen();
21                 ~Screen();
22
23                 // 'Pumps' the system event queue.
24                 // Returns 'false' if the program should quit.
25                 bool PumpEvents();
26
27                 // Clears the screen to a given colour.
28                 void Clear(float r=1.0, float g=1.0, float b=1.0, float a=1.0);
29
30                 // Finishes rendering a frame, and presents it on the screen.
31                 void Present();
32
33                 // Get the current width/height of the window's viewport.
34                 int ViewportWidth() const { return m_viewport_width; }
35                 int ViewportHeight() const { return m_viewport_height; }
36                 
37                 // Debug Font handling
38                 void DebugFontInit(const char *font_name, float font_size = 12);
39                 void DebugFontClear();
40                 void DebugFontPrint(const char *str);
41                 void DebugFontPrintF(const char *fmt, ...);
42                 
43                 // Handle mouse input.
44                 typedef std::function<void(int x, int y, int button, int wheel)> MouseHandler;
45                 void SetMouseHandler(MouseHandler handler)
46                 {
47                         m_mouse_handler = handler;
48                 }
49                 
50                 enum MouseCursors
51                 {
52                         CursorArrow,
53                         CursorWait,
54                         CursorWaitArrow,
55                         CursorMove,
56                         CursorHand
57                 };
58                 void SetMouseCursor(MouseCursors cursor);
59
60                 void ScreenShot(const char * filename) const;
61                 void RenderBMP(const char * filename) const;
62
63                 // Returns the CPU time (in seconds) it took to render the last completed frame.
64                 double GetLastFrameTimeCPU() const { return m_last_frame_time / SDL_GetPerformanceFrequency(); }
65                 // Returns the GPU time (in seconds) it took to render the last completed frame.
66                 double GetLastFrameTimeGPU() const;
67         private:
68                 void ResizeViewport(int width, int height);
69                 void DebugFontFlush();
70                 
71                 MouseHandler m_mouse_handler;
72                 int m_last_mouse_x;
73                 int m_last_mouse_y;
74
75                 double m_last_frame_time;
76                 double m_frame_begin_time;
77                 GLuint m_frame_gpu_timer;
78                 GLuint m_last_frame_gpu_timer;
79
80                 int m_viewport_width;
81                 int m_viewport_height;
82                 SDL_Window *m_window;
83                 SDL_GLContext m_gl_context;
84                 ShaderProgram m_texture_prog;
85                 GLint m_colour_uniform_location;
86                 GraphicsBuffer m_viewport_ubo;
87                 stbtt_bakedchar m_debug_font_rects[96];
88                 unsigned int m_debug_font_atlas;
89                 float m_debug_font_x;
90                 float m_debug_font_y;
91                 float m_debug_font_size;
92                 GraphicsBuffer m_debug_font_vertices;
93                 GraphicsBuffer m_debug_font_indices;
94                 int m_debug_font_vertex_head;
95                 int m_debug_font_index_head;
96         };
97
98 }
99
100 #endif // _SCREEN_H

UCC git Repository :: git.ucc.asn.au