Don't write past the end of a GPU mem buffer.
[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();
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         private:
73                 void ResizeViewport(int width, int height);
74                 void DebugFontFlush();
75                 
76                 MouseHandler m_mouse_handler;
77                 int m_last_mouse_x;
78                 int m_last_mouse_y;
79
80                 double m_last_frame_time;
81                 double m_frame_begin_time;
82                 GLuint m_frame_gpu_timer;
83                 GLuint m_last_frame_gpu_timer;
84
85                 int m_viewport_width;
86                 int m_viewport_height;
87                 SDL_Window *m_window;
88                 SDL_GLContext m_gl_context;
89                 ShaderProgram m_texture_prog;
90                 ShaderProgram m_font_prog;
91                 GLint m_colour_uniform_location;
92                 GraphicsBuffer m_viewport_ubo;
93                 stbtt_bakedchar m_debug_font_rects[96];
94                 unsigned int m_debug_font_atlas;
95                 float m_debug_font_x;
96                 float m_debug_font_y;
97                 float m_debug_font_size;
98                 GraphicsBuffer m_debug_font_vertices;
99                 GraphicsBuffer m_debug_font_indices;
100                 int m_debug_font_vertex_head;
101                 int m_debug_font_index_head;
102                 View * m_view;
103         };
104
105 }
106
107 #endif // _SCREEN_H

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