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 glClearColor(1.f,1.f,1.f,1.f);
23 glClear(GL_COLOR_BUFFER_BIT);
26 ResizeViewport(800, 600);
32 SDL_GL_DeleteContext(m_gl_context);
33 SDL_DestroyWindow(m_window);
37 void Screen::ResizeViewport(int width, int height)
39 glViewport(0, 0, width, height);
40 m_viewport_width = width;
41 m_viewport_height = height;
44 bool Screen::PumpEvents()
47 bool no_quit_requested = true;
48 while (SDL_PollEvent(&evt))
53 no_quit_requested = false;
56 switch (evt.window.event)
58 case SDL_WINDOWEVENT_RESIZED:
59 case SDL_WINDOWEVENT_SIZE_CHANGED:
60 ResizeViewport(evt.window.data1, evt.window.data2);
65 m_last_mouse_x = evt.motion.x;
66 m_last_mouse_y = evt.motion.y;
69 m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
72 case SDL_MOUSEBUTTONDOWN:
73 case SDL_MOUSEBUTTONUP:
74 m_last_mouse_x = evt.button.x;
75 m_last_mouse_y = evt.button.y;
78 m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0);
84 m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y);
89 Debug("Key %c down", (char)evt.key.keysym.sym);
90 if (isalnum((char)evt.key.keysym.sym))
92 char filename[] = "0.bmp";
93 filename[0] = (char)evt.key.keysym.sym;
101 return no_quit_requested;
104 void Screen::SetMouseCursor(Screen::MouseCursors cursor)
106 SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
109 case CursorArrow: system_cursor_id = SDL_SYSTEM_CURSOR_ARROW; break;
110 case CursorWait: system_cursor_id = SDL_SYSTEM_CURSOR_WAIT; break;
111 case CursorWaitArrow: system_cursor_id = SDL_SYSTEM_CURSOR_WAITARROW; break;
112 case CursorMove: system_cursor_id = SDL_SYSTEM_CURSOR_SIZEALL; break;
113 case CursorHand: system_cursor_id = SDL_SYSTEM_CURSOR_HAND; break;
116 SDL_Cursor *system_cursor = SDL_CreateSystemCursor(system_cursor_id);
117 SDL_SetCursor(system_cursor);
118 //TODO: Check if we need to free the system cursors.
121 void Screen::Present()
123 SDL_GL_SwapWindow(m_window);
126 void Screen::ScreenShot(const char * filename) const
128 Debug("Attempting to save BMP to file %s", filename);
130 int w = ViewportWidth();
131 int h = ViewportHeight();
132 unsigned char * pixels = new unsigned char[w*h*4];
134 Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4);
136 glReadPixels(0,0,w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
138 SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0,0,0,0);
140 Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
142 GLenum texture_format = (surf->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
143 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);
145 if (SDL_SaveBMP(surf, filename) != 0)
146 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
148 SDL_FreeSurface(surf);
155 * NOT PART OF THE DOCUMENT FORMAT
157 void Screen::RenderBMP(const char * filename) const
159 SDL_Surface * bmp = SDL_LoadBMP(filename);
161 Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());
166 GLenum texture_format;
167 switch (bmp->format->BytesPerPixel)
169 case 4: //contains alpha
170 texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
172 case 3: //does not contain alpha
173 texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;
176 Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel);
180 //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);
184 glEnable(GL_TEXTURE_2D);
185 glGenTextures(1, &texID);
186 glBindTexture(GL_TEXTURE_2D, texID);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
193 glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels);
195 glMatrixMode(GL_PROJECTION);
197 glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
198 glMatrixMode(GL_MODELVIEW);
202 glTexCoord2i(0,0); glVertex2f(0,0);
203 glTexCoord2i(1,0); glVertex2f(1,0);
204 glTexCoord2i(1,1); glVertex2f(1,1);
205 glTexCoord2i(0,1); glVertex2f(0,1);
208 glDisable(GL_TEXTURE_2D);
209 SDL_FreeSurface(bmp);