My eyes, they burn! Also runs faster, slightly less buggy.
[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         class View;
15         /*
16          * The "Screen" class handles managing the OS window (using SDL2).
17          */
18         class Screen
19         {
20         public:
21                 Screen(bool visible = true);
22                 ~Screen();
23
24                 // 'Pumps' the system event queue.
25                 // Returns 'false' if the program should quit.
26                 bool PumpEvents();
27
28                 // Clears the screen to a given colour.
29                 void Clear(float r=1.0, float g=1.0, float b=1.0, float a=1.0);
30
31                 // Finishes rendering a frame, and presents it on the screen.
32                 void Present();
33
34                 // Get the current width/height of the window's viewport.
35                 int ViewportWidth() const { return m_viewport_width; }
36                 int ViewportHeight() const { return m_viewport_height; }
37                 
38                 // Debug Font handling
39                 void DebugFontInit(const char *font_name, float font_size = 12);
40                 void DebugFontClear();
41                 void DebugFontPrint(const char *str);
42                 void DebugFontPrintF(const char *fmt, ...);
43                 
44                 // Handle mouse input.
45                 typedef void(*MouseHandler)(int x, int y, int button, int wheel, Screen * scr, View * view);
46                 void SetMouseHandler(MouseHandler handler)
47                 {
48                         m_mouse_handler = handler;
49                 }
50                 
51                 enum MouseCursors
52                 {
53                         CursorArrow,
54                         CursorWait,
55                         CursorWaitArrow,
56                         CursorMove,
57                         CursorHand
58                 };
59                 void SetMouseCursor(MouseCursors cursor);
60
61                 void ScreenShot(const char * filename) const;
62                 void RenderBMP(const char * filename) const;
63                 void RenderPixels(int x, int y, int w, int h, uint8_t * pixels) const;
64
65
66                 void SetView(View * new_view) {m_view = new_view;}
67
68                 // Returns the CPU time (in seconds) it took to render the last completed frame.
69                 double GetLastFrameTimeCPU() const { return m_last_frame_time / SDL_GetPerformanceFrequency(); }
70                 // Returns the GPU time (in seconds) it took to render the last completed frame.
71                 double GetLastFrameTimeGPU() const;
72                 
73                 void RequestQuit() {m_no_quit_requested = false;}
74                 bool QuitRequested() const {return !m_no_quit_requested;}
75                 
76                 void ShowDebugFont(bool show = true) {m_show_debug_font = show;}
77                 bool DebugFontShown() const {return m_show_debug_font;}
78
79                 bool Valid() const {return m_window != NULL;}
80         private:
81                 void ResizeViewport(int width, int height);
82                 void DebugFontFlush();
83                 
84                 MouseHandler m_mouse_handler;
85                 int m_last_mouse_x;
86                 int m_last_mouse_y;
87
88                 double m_last_frame_time;
89                 double m_frame_begin_time;
90                 GLuint m_frame_gpu_timer;
91                 GLuint m_last_frame_gpu_timer;
92
93                 int m_viewport_width;
94                 int m_viewport_height;
95                 SDL_Window *m_window;
96                 SDL_GLContext m_gl_context;
97                 ShaderProgram m_texture_prog;
98                 ShaderProgram m_font_prog;
99                 GLint m_colour_uniform_location;
100                 GraphicsBuffer m_viewport_ubo;
101                 stbtt_bakedchar m_debug_font_rects[96];
102                 unsigned int m_debug_font_atlas;
103                 float m_debug_font_x;
104                 float m_debug_font_y;
105                 float m_debug_font_size;
106                 GraphicsBuffer m_debug_font_vertices;
107                 GraphicsBuffer m_debug_font_indices;
108                 int m_debug_font_vertex_head;
109                 int m_debug_font_index_head;
110                 View * m_view;
111                 bool m_no_quit_requested;
112                 bool m_show_debug_font;
113         };
114
115 }
116
117 #endif // _SCREEN_H

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