4 #include "SDL_opengl.h"
5 #include <fcntl.h> // for access(2)
6 #include <unistd.h> // for access(2)
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);
19 Fatal("Couldn't create window!");
22 m_gl_context = SDL_GL_CreateContext(m_window);
24 m_debug_font_atlas = 0;
26 ResizeViewport(800, 600);
36 SDL_GL_DeleteContext(m_gl_context);
37 SDL_DestroyWindow(m_window);
41 void Screen::Clear(float r, float g, float b, float a)
43 glClearColor(r,g,b,a);
44 glClear(GL_COLOR_BUFFER_BIT);
48 void Screen::ResizeViewport(int width, int height)
50 glViewport(0, 0, width, height);
51 m_viewport_width = width;
52 m_viewport_height = height;
55 bool Screen::PumpEvents()
58 bool no_quit_requested = true;
59 while (SDL_PollEvent(&evt))
64 no_quit_requested = false;
67 switch (evt.window.event)
69 case SDL_WINDOWEVENT_RESIZED:
70 case SDL_WINDOWEVENT_SIZE_CHANGED:
71 ResizeViewport(evt.window.data1, evt.window.data2);
76 m_last_mouse_x = evt.motion.x;
77 m_last_mouse_y = evt.motion.y;
80 m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
83 case SDL_MOUSEBUTTONDOWN:
84 case SDL_MOUSEBUTTONUP:
85 m_last_mouse_x = evt.button.x;
86 m_last_mouse_y = evt.button.y;
89 m_mouse_handler(evt.button.x, evt.button.y, evt.button.state?evt.button.button:0, 0);
95 m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y);
100 Debug("Key %c down", (char)evt.key.keysym.sym);
101 if (isalnum((char)evt.key.keysym.sym))
103 char filename[] = "0.bmp";
104 filename[0] = (char)evt.key.keysym.sym;
105 ScreenShot(filename);
113 return no_quit_requested;
116 void Screen::SetMouseCursor(Screen::MouseCursors cursor)
118 SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
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;
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.
133 void Screen::Present()
135 if (m_debug_font_atlas)
137 SDL_GL_SwapWindow(m_window);
140 void Screen::ScreenShot(const char * filename) const
142 Debug("Attempting to save BMP to file %s", filename);
144 int w = ViewportWidth();
145 int h = ViewportHeight();
146 unsigned char * pixels = new unsigned char[w*h*4];
148 Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4);
150 for (int y = 0; y < h; ++y)
152 glReadPixels(0,h-y-1,w, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[y*w*4]);
155 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
156 SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0x000000ff,0x0000ff00,0x00ff0000,0xff000000);
158 SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);
161 Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
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);
166 if (SDL_SaveBMP(surf, filename) != 0)
167 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
169 SDL_FreeSurface(surf);
176 * NOT PART OF THE DOCUMENT FORMAT
178 void Screen::RenderBMP(const char * filename) const
180 if (access(filename, R_OK) == -1)
182 Error("No such file \"%s\" - Nothing to render - You might have done this deliberately?", filename);
185 SDL_Surface * bmp = SDL_LoadBMP(filename);
187 Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());
192 GLenum texture_format;
193 switch (bmp->format->BytesPerPixel)
195 case 4: //contains alpha
196 texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
198 case 3: //does not contain alpha
199 texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;
202 Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel);
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);
210 glEnable(GL_TEXTURE_2D);
211 glGenTextures(1, &texID);
212 glBindTexture(GL_TEXTURE_2D, texID);
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);
219 glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels);
221 glMatrixMode(GL_PROJECTION);
223 glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
224 glMatrixMode(GL_MODELVIEW);
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);
234 glDisable(GL_TEXTURE_2D);
235 SDL_FreeSurface(bmp);
238 void Screen::DebugFontInit(const char *name, float font_size)
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);
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;
256 m_debug_font_vertices.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
257 m_debug_font_vertices.SetType(GraphicsBuffer::BufferTypeVertex);
258 m_debug_font_vertices.Upload(8192, nullptr);
259 m_debug_font_vertex_head = 0;
262 void Screen::DebugFontClear()
264 m_debug_font_x = m_debug_font_y = 0;
265 if (!m_debug_font_atlas) return;
266 DebugFontPrint("\n");
269 void Screen::DebugFontFlush()
271 glBindFramebuffer(GL_FRAMEBUFFER, 0);
272 glMatrixMode(GL_PROJECTION);
275 glOrtho(0,ViewportWidth(), ViewportHeight(), 0, -1, 1);
276 glMatrixMode(GL_MODELVIEW);
283 glEnable(GL_TEXTURE_2D);
285 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
286 glBindTexture(GL_TEXTURE_2D, m_debug_font_atlas);
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 glDrawArrays(GL_QUADS, 0, m_debug_font_vertex_head/4);
294 glDisableClientState(GL_VERTEX_ARRAY);
295 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
298 glDisable(GL_TEXTURE_2D);
300 glMatrixMode(GL_PROJECTION);
303 m_debug_font_vertex_head = 0;
306 void Screen::DebugFontPrint(const char* str)
308 if (!m_debug_font_atlas) return;
310 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 if (m_debug_font_vertex_head*sizeof(float) + 16*sizeof(float) >= m_debug_font_vertices.GetSize() )
314 m_debug_font_vertices.UnMap();
319 if (*str >= 32 && *str < 128) {
320 stbtt_aligned_quad q;
321 stbtt_GetBakedQuad(m_debug_font_rects, 1024,1024, *str-32, &m_debug_font_x,&m_debug_font_y,&q,1);
322 *vertexData = q.s0; vertexData++;
323 *vertexData = q.t0; vertexData++;
324 *vertexData = q.x0; vertexData++;
325 *vertexData = q.y0; vertexData++;
326 *vertexData = q.s1; vertexData++;
327 *vertexData = q.t0; vertexData++;
328 *vertexData = q.x1; vertexData++;
329 *vertexData = q.y0; vertexData++;
330 *vertexData = q.s1; vertexData++;
331 *vertexData = q.t1; vertexData++;
332 *vertexData = q.x1; vertexData++;
333 *vertexData = q.y1; vertexData++;
334 *vertexData = q.s0; vertexData++;
335 *vertexData = q.t1; vertexData++;
336 *vertexData = q.x0; vertexData++;
337 *vertexData = q.y1; vertexData++;
339 m_debug_font_vertex_head += 16;
342 else if (*str == '\n')
345 m_debug_font_y += m_debug_font_size;
349 m_debug_font_vertices.UnMap();
353 void Screen::DebugFontPrintF(const char *fmt, ...)
358 vsnprintf(buffer, BUFSIZ, fmt,va);
360 DebugFontPrint(buffer);