Horrible debug font bufferification. (Sorry)
[ipdf/code.git] / src / screen.cpp
1 #include "common.h"
2 #include "screen.h"
3
4 #include "SDL_opengl.h"
5 #include <fcntl.h> // for access(2)
6 #include <unistd.h> // for access(2)
7
8 using namespace IPDF;
9 using namespace std;
10
11 Screen::Screen()
12 {
13         SDL_Init(SDL_INIT_VIDEO);
14         m_window = SDL_CreateWindow("IPDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
15                         800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
16
17         if (!m_window)
18         {
19                 Fatal("Couldn't create window!");
20         }
21
22         m_gl_context = SDL_GL_CreateContext(m_window);
23
24         m_debug_font_atlas = 0;
25
26         ResizeViewport(800, 600);
27
28         Clear();
29         Present();
30         
31
32 }
33
34 Screen::~Screen()
35 {
36         SDL_GL_DeleteContext(m_gl_context);
37         SDL_DestroyWindow(m_window);
38         SDL_Quit();
39 }
40
41 void Screen::Clear(float r, float g, float b, float a)
42 {
43         glClearColor(r,g,b,a);
44         glClear(GL_COLOR_BUFFER_BIT);
45         DebugFontClear();
46 }
47
48 void Screen::ResizeViewport(int width, int height)
49 {
50         glViewport(0, 0, width, height);
51         m_viewport_width = width;
52         m_viewport_height = height;
53 }
54
55 bool Screen::PumpEvents()
56 {
57         SDL_Event evt;
58         bool no_quit_requested = true;
59         while (SDL_PollEvent(&evt))
60         {
61                 switch (evt.type)
62                 {
63                 case SDL_QUIT:
64                         no_quit_requested = false;
65                         break;
66                 case SDL_WINDOWEVENT:
67                         switch (evt.window.event)
68                         {
69                         case SDL_WINDOWEVENT_RESIZED:
70                         case SDL_WINDOWEVENT_SIZE_CHANGED:
71                                 ResizeViewport(evt.window.data1, evt.window.data2);
72                                 break;
73                         }
74                         break;
75                 case SDL_MOUSEMOTION:
76                         m_last_mouse_x = evt.motion.x;
77                         m_last_mouse_y = evt.motion.y;
78                         if (m_mouse_handler)
79                         {
80                                 m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
81                         }
82                         break;
83                 case SDL_MOUSEBUTTONDOWN:
84                 case SDL_MOUSEBUTTONUP:
85                         m_last_mouse_x = evt.button.x;
86                         m_last_mouse_y = evt.button.y;
87                         if (m_mouse_handler)
88                         {
89                                 m_mouse_handler(evt.button.x, evt.button.y, evt.button.state?evt.button.button:0, 0);
90                         }
91                         break;
92                 case SDL_MOUSEWHEEL:
93                         if (m_mouse_handler)
94                         {
95                                 m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y);
96                         }
97                         break;
98                 case SDL_KEYDOWN:
99                 {
100                         Debug("Key %c down", (char)evt.key.keysym.sym);
101                         if (isalnum((char)evt.key.keysym.sym))
102                         {
103                                 char filename[] = "0.bmp";
104                                 filename[0] = (char)evt.key.keysym.sym;
105                                 ScreenShot(filename);
106                         }
107                         break;
108                 }
109                 default:
110                         break;
111                 }
112         }
113         return no_quit_requested;
114 }
115
116 void Screen::SetMouseCursor(Screen::MouseCursors cursor)
117 {
118         SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
119         switch (cursor)
120         {
121         case CursorArrow: system_cursor_id = SDL_SYSTEM_CURSOR_ARROW; break;
122         case CursorWait: system_cursor_id = SDL_SYSTEM_CURSOR_WAIT; break;
123         case CursorWaitArrow: system_cursor_id = SDL_SYSTEM_CURSOR_WAITARROW; break;
124         case CursorMove: system_cursor_id = SDL_SYSTEM_CURSOR_SIZEALL; break;
125         case CursorHand: system_cursor_id = SDL_SYSTEM_CURSOR_HAND; break;
126         default: break;
127         }
128         SDL_Cursor *system_cursor = SDL_CreateSystemCursor(system_cursor_id);
129         SDL_SetCursor(system_cursor);
130         //TODO: Check if we need to free the system cursors.
131 }
132
133 void Screen::Present()
134 {
135         if (m_debug_font_atlas)
136                 DebugFontFlush();
137         SDL_GL_SwapWindow(m_window);
138 }
139
140 void Screen::ScreenShot(const char * filename) const
141 {
142         Debug("Attempting to save BMP to file %s", filename);
143
144         int w = ViewportWidth();
145         int h = ViewportHeight();
146         unsigned char * pixels = new unsigned char[w*h*4];
147         if (pixels == NULL)
148                 Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4);
149
150         for (int y = 0; y < h; ++y)
151         {
152                 glReadPixels(0,h-y-1,w, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[y*w*4]);
153         }
154
155 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
156         SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0x000000ff,0x0000ff00,0x00ff0000,0xff000000);
157 #else
158         SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);
159 #endif
160         if (surf == NULL)
161                 Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
162
163         GLenum texture_format = (surf->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
164         Debug("SDL_Surface %d BytesPerPixel, format %d (RGB = %d, BGR = %d, RGBA = %d, BGRA = %d)", surf->format->BytesPerPixel, texture_format, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA);
165
166         if (SDL_SaveBMP(surf, filename) != 0)
167                 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
168         
169         SDL_FreeSurface(surf);
170         delete [] pixels;
171         Debug("Succeeded!");
172 }
173
174 /**
175  * Render a BMP
176  * NOT PART OF THE DOCUMENT FORMAT
177  */
178 void Screen::RenderBMP(const char * filename) const
179 {
180         if (access(filename, R_OK) == -1)
181         {
182                 Error("No such file \"%s\" - Nothing to render - You might have done this deliberately?", filename);
183                 return;
184         }
185         SDL_Surface * bmp = SDL_LoadBMP(filename);
186         if (bmp == NULL)
187                 Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());
188
189         int w = bmp->w;
190         int h = bmp->h;
191
192         GLenum texture_format; 
193         switch (bmp->format->BytesPerPixel)
194         {
195                 case 4: //contains alpha
196                         texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
197                         break;
198                 case 3: //does not contain alpha
199                         texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;  
200                         break;
201                 default:
202                         Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel);
203                         break;  
204         }
205
206         //Debug("SDL_Surface %d BytesPerPixel, format %d (RGB = %d, BGR = %d, RGBA = %d, BGRA = %d)", bmp->format->BytesPerPixel, texture_format, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA);
207
208
209         GLuint texID;
210         glEnable(GL_TEXTURE_2D);
211         glGenTextures(1, &texID);
212         glBindTexture(GL_TEXTURE_2D, texID);
213
214         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
215         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
216         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
217         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
218
219         glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels);
220
221         glMatrixMode(GL_PROJECTION);
222         glLoadIdentity();
223         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
224         glMatrixMode(GL_MODELVIEW);
225         glLoadIdentity();
226
227         glBegin(GL_QUADS);
228                 glTexCoord2i(0,0); glVertex2f(0,0);
229                 glTexCoord2i(1,0); glVertex2f(1,0);
230                 glTexCoord2i(1,1); glVertex2f(1,1);
231                 glTexCoord2i(0,1); glVertex2f(0,1);
232         glEnd();
233
234         glDisable(GL_TEXTURE_2D);
235         SDL_FreeSurface(bmp);   
236 }
237
238 void Screen::DebugFontInit(const char *name, float font_size)
239 {
240         unsigned char font_atlas_data[1024*1024];
241         FILE *font_file = fopen(name, "rb");
242         fseek(font_file, 0, SEEK_END);
243         size_t font_file_size = ftell(font_file);
244         fseek(font_file, 0, SEEK_SET);
245         unsigned char *font_file_data = (unsigned char*)malloc(font_file_size);
246         fread(font_file_data, 1, font_file_size, font_file);
247         fclose(font_file);
248         stbtt_BakeFontBitmap(font_file_data,0, font_size, font_atlas_data,1024,1024, 32,96, m_debug_font_rects);
249         free(font_file_data);
250         glGenTextures(1, &m_debug_font_atlas);
251         glBindTexture(GL_TEXTURE_2D, m_debug_font_atlas);
252         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024,1024, 0, GL_ALPHA, GL_UNSIGNED_BYTE, font_atlas_data);
253         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
254         m_debug_font_size = font_size;
255
256         m_debug_font_vertices.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
257         m_debug_font_vertices.SetType(GraphicsBuffer::BufferTypeVertex);
258         m_debug_font_vertices.Upload(512, nullptr);
259         m_debug_font_vertex_head = 0;
260 }
261
262 void Screen::DebugFontClear()
263 {
264         m_debug_font_x = m_debug_font_y = 0;
265         if (!m_debug_font_atlas) return;
266         DebugFontPrint("\n");
267 }
268
269 void Screen::DebugFontFlush()
270 {
271         glBindFramebuffer(GL_FRAMEBUFFER, 0);
272         glMatrixMode(GL_PROJECTION);
273         glPushMatrix();
274         glLoadIdentity();
275         glOrtho(0,ViewportWidth(), ViewportHeight(), 0, -1, 1);
276         glMatrixMode(GL_MODELVIEW);
277         glPushMatrix();
278         glLoadIdentity();
279         
280         glColor4f(0,0,0,1);
281         
282                 
283         glEnable(GL_TEXTURE_2D);
284         glEnable(GL_BLEND);
285         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
286         glBindTexture(GL_TEXTURE_2D, m_debug_font_atlas);
287
288         m_debug_font_vertices.Bind();
289         glEnableClientState(GL_VERTEX_ARRAY);
290         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
291         glVertexPointer(2, GL_FLOAT, 4*sizeof(float), (void*)(2*sizeof(float)));
292         glTexCoordPointer(2, GL_FLOAT, 4*sizeof(float), 0);
293         Debug("Flushing Debug Font arrays, %d verts (%d floats)", m_debug_font_vertex_head/4, m_debug_font_vertex_head);
294         glDrawArrays(GL_QUADS, 0, m_debug_font_vertex_head/4);
295         glDisableClientState(GL_VERTEX_ARRAY);
296         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
297
298         glDisable(GL_BLEND);
299         glDisable(GL_TEXTURE_2D);
300         glPopMatrix();
301         glMatrixMode(GL_PROJECTION);
302         glPopMatrix();
303
304         m_debug_font_vertex_head = 0;
305 }
306
307 void Screen::DebugFontPrint(const char* str)
308 {
309         if (!m_debug_font_atlas) return;
310
311         float *vertexData = (float*)m_debug_font_vertices.MapRange(m_debug_font_vertex_head*sizeof(float), m_debug_font_vertices.GetSize() - m_debug_font_vertex_head*sizeof(float), false, true, true);
312         while (*str) {
313                 if (m_debug_font_vertex_head*sizeof(float) + 16*sizeof(float) >= m_debug_font_vertices.GetSize() )
314                 {
315                         m_debug_font_vertices.UnMap();
316                         DebugFontFlush();
317                         DebugFontPrint(str);
318                         return;
319                 }
320                 if (*str >= 32 && *str < 128) {
321                         stbtt_aligned_quad q;
322                         stbtt_GetBakedQuad(m_debug_font_rects, 1024,1024, *str-32, &m_debug_font_x,&m_debug_font_y,&q,1);
323                         *vertexData = q.s0; vertexData++;
324                         *vertexData = q.t0; vertexData++;
325                         *vertexData = q.x0; vertexData++;
326                         *vertexData = q.y0; vertexData++;
327                         *vertexData = q.s1; vertexData++;
328                         *vertexData = q.t0; vertexData++;
329                         *vertexData = q.x1; vertexData++;
330                         *vertexData = q.y0; vertexData++;
331                         *vertexData = q.s1; vertexData++;
332                         *vertexData = q.t1; vertexData++;
333                         *vertexData = q.x1; vertexData++;
334                         *vertexData = q.y1; vertexData++;
335                         *vertexData = q.s0; vertexData++;
336                         *vertexData = q.t1; vertexData++;
337                         *vertexData = q.x0; vertexData++;
338                         *vertexData = q.y1; vertexData++;
339
340                         m_debug_font_vertex_head += 16;
341
342                 }
343                 else if (*str == '\n')
344                 {
345                         m_debug_font_x = 0;
346                         m_debug_font_y += m_debug_font_size;
347                 }
348                 ++str;
349         }
350         m_debug_font_vertices.UnMap();
351         //DebugFontFlush();
352 }
353
354 void Screen::DebugFontPrintF(const char *fmt, ...)
355 {
356         char buffer[BUFSIZ];
357         va_list va;
358         va_start(va, fmt);
359         vsnprintf(buffer, BUFSIZ, fmt,va);
360         va_end(va);
361         DebugFontPrint(buffer);
362 }

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