4 #include "SDL_opengl.h"
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);
17 Fatal("Couldn't create window!");
20 m_gl_context = SDL_GL_CreateContext(m_window);
22 ResizeViewport(800, 600);
28 SDL_GL_DeleteContext(m_gl_context);
29 SDL_DestroyWindow(m_window);
33 void Screen::ResizeViewport(int width, int height)
35 glViewport(0, 0, width, height);
36 m_viewport_width = width;
37 m_viewport_height = height;
40 bool Screen::PumpEvents()
43 bool no_quit_requested = true;
44 while (SDL_PollEvent(&evt))
49 no_quit_requested = false;
52 switch (evt.window.event)
54 case SDL_WINDOWEVENT_RESIZED:
55 case SDL_WINDOWEVENT_SIZE_CHANGED:
56 ResizeViewport(evt.window.data1, evt.window.data2);
61 m_last_mouse_x = evt.motion.x;
62 m_last_mouse_y = evt.motion.y;
65 m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
68 case SDL_MOUSEBUTTONDOWN:
69 case SDL_MOUSEBUTTONUP:
70 m_last_mouse_x = evt.button.x;
71 m_last_mouse_y = evt.button.y;
74 m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0);
80 m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y);
85 Debug("Key %c down", (char)evt.key.keysym.sym);
86 if (isalnum((char)evt.key.keysym.sym))
88 char filename[] = "0.bmp";
89 filename[0] = (char)evt.key.keysym.sym;
97 return no_quit_requested;
100 void Screen::SetMouseCursor(Screen::MouseCursors cursor)
102 SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
105 case CursorArrow: system_cursor_id = SDL_SYSTEM_CURSOR_ARROW; break;
106 case CursorWait: system_cursor_id = SDL_SYSTEM_CURSOR_WAIT; break;
107 case CursorWaitArrow: system_cursor_id = SDL_SYSTEM_CURSOR_WAITARROW; break;
108 case CursorMove: system_cursor_id = SDL_SYSTEM_CURSOR_SIZEALL; break;
109 case CursorHand: system_cursor_id = SDL_SYSTEM_CURSOR_HAND; break;
112 SDL_Cursor *system_cursor = SDL_CreateSystemCursor(system_cursor_id);
113 SDL_SetCursor(system_cursor);
114 //TODO: Check if we need to free the system cursors.
117 void Screen::Present()
119 SDL_GL_SwapWindow(m_window);
122 void Screen::ScreenShot(const char * filename) const
124 Debug("Attempting to save BMP to file %s", filename);
126 int w = ViewportWidth();
127 int h = ViewportHeight();
128 unsigned char * pixels = new unsigned char[w*h*4];
130 Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4);
132 glReadPixels(0,0,w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
134 SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0,0,0,0);
136 Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
138 if (SDL_SaveBMP(surf, filename) != 0)
139 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
141 SDL_FreeSurface(surf);
148 * NOT PART OF THE DOCUMENT FORMAT
150 void Screen::RenderBMP(const char * filename) const
152 SDL_Surface * bmp = SDL_LoadBMP(filename);
154 Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());
159 GLenum texture_format;
160 switch (bmp->format->BytesPerPixel)
162 case 4: //contains alpha
163 texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
165 case 3: //does not contain alpha
166 texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;
169 Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel);
172 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);
177 glGenTextures(1, &texID);
178 glBindTexture(GL_TEXTURE_2D, texID);
179 glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels);
180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
183 glEnable(GL_TEXTURE_2D);
184 glBindTexture(GL_TEXTURE_2D, texID);
187 glTexCoord2i(0,0); glVertex2f(-0.5f*w, -0.5f*h);
188 glTexCoord2i(1,0); glVertex2f(0.5f*w, -0.5f*h);
189 glTexCoord2i(1,1); glVertex2f(0.5f*w, 0.5f*h);
190 glTexCoord2i(0,1); glVertex2f(-0.5*w,0.5*h);
193 glDisable(GL_TEXTURE_2D);
195 //SDL_FreeSurface(bmp);