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

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