59d4334ac89eb7ba4d1123531906b4e0e5916863
[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 }
42
43 void Screen::ResizeViewport(int width, int height)
44 {
45         glViewport(0, 0, width, height);
46         m_viewport_width = width;
47         m_viewport_height = height;
48 }
49
50 bool Screen::PumpEvents()
51 {
52         SDL_Event evt;
53         bool no_quit_requested = true;
54         while (SDL_PollEvent(&evt))
55         {
56                 switch (evt.type)
57                 {
58                 case SDL_QUIT:
59                         no_quit_requested = false;
60                         break;
61                 case SDL_WINDOWEVENT:
62                         switch (evt.window.event)
63                         {
64                         case SDL_WINDOWEVENT_RESIZED:
65                         case SDL_WINDOWEVENT_SIZE_CHANGED:
66                                 ResizeViewport(evt.window.data1, evt.window.data2);
67                                 break;
68                         }
69                         break;
70                 case SDL_MOUSEMOTION:
71                         m_last_mouse_x = evt.motion.x;
72                         m_last_mouse_y = evt.motion.y;
73                         if (m_mouse_handler)
74                         {
75                                 m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
76                         }
77                         break;
78                 case SDL_MOUSEBUTTONDOWN:
79                 case SDL_MOUSEBUTTONUP:
80                         m_last_mouse_x = evt.button.x;
81                         m_last_mouse_y = evt.button.y;
82                         if (m_mouse_handler)
83                         {
84                                 m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0);
85                         }
86                         break;
87                 case SDL_MOUSEWHEEL:
88                         if (m_mouse_handler)
89                         {
90                                 m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y);
91                         }
92                         break;
93                 case SDL_KEYDOWN:
94                 {
95                         Debug("Key %c down", (char)evt.key.keysym.sym);
96                         if (isalnum((char)evt.key.keysym.sym))
97                         {
98                                 char filename[] = "0.bmp";
99                                 filename[0] = (char)evt.key.keysym.sym;
100                                 ScreenShot(filename);
101                         }
102                 }
103                 default:
104                         break;
105                 }
106         }
107         return no_quit_requested;
108 }
109
110 void Screen::SetMouseCursor(Screen::MouseCursors cursor)
111 {
112         SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
113         switch (cursor)
114         {
115         case CursorArrow: system_cursor_id = SDL_SYSTEM_CURSOR_ARROW; break;
116         case CursorWait: system_cursor_id = SDL_SYSTEM_CURSOR_WAIT; break;
117         case CursorWaitArrow: system_cursor_id = SDL_SYSTEM_CURSOR_WAITARROW; break;
118         case CursorMove: system_cursor_id = SDL_SYSTEM_CURSOR_SIZEALL; break;
119         case CursorHand: system_cursor_id = SDL_SYSTEM_CURSOR_HAND; break;
120         default: break;
121         }
122         SDL_Cursor *system_cursor = SDL_CreateSystemCursor(system_cursor_id);
123         SDL_SetCursor(system_cursor);
124         //TODO: Check if we need to free the system cursors.
125 }
126
127 void Screen::Present()
128 {
129         SDL_GL_SwapWindow(m_window);
130 }
131
132 void Screen::ScreenShot(const char * filename) const
133 {
134         Debug("Attempting to save BMP to file %s", filename);
135
136         int w = ViewportWidth();
137         int h = ViewportHeight();
138         unsigned char * pixels = new unsigned char[w*h*4];
139         if (pixels == NULL)
140                 Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4);
141
142         for (int y = 0; y < h; ++y)
143         {
144                 glReadPixels(0,h-y-1,w, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[y*w*4]);
145         }
146
147 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
148         SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0x000000ff,0x0000ff00,0x00ff0000,0xff000000);
149 #else
150         SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);
151 #endif
152         if (surf == NULL)
153                 Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
154
155         GLenum texture_format = (surf->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
156         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);
157
158         if (SDL_SaveBMP(surf, filename) != 0)
159                 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
160         
161         SDL_FreeSurface(surf);
162         delete [] pixels;
163         Debug("Succeeded!");
164 }
165
166 /**
167  * Render a BMP
168  * NOT PART OF THE DOCUMENT FORMAT
169  */
170 void Screen::RenderBMP(const char * filename) const
171 {
172         SDL_Surface * bmp = SDL_LoadBMP(filename);
173         if (bmp == NULL)
174                 Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());
175
176         int w = bmp->w;
177         int h = bmp->h;
178
179         GLenum texture_format; 
180         switch (bmp->format->BytesPerPixel)
181         {
182                 case 4: //contains alpha
183                         texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
184                         break;
185                 case 3: //does not contain alpha
186                         texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;  
187                         break;
188                 default:
189                         Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel);
190                         break;  
191         }
192
193         //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);
194
195
196         GLuint texID;
197         glEnable(GL_TEXTURE_2D);
198         glGenTextures(1, &texID);
199         glBindTexture(GL_TEXTURE_2D, texID);
200
201         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
202         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
203         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
204         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
205
206         glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels);
207
208         glMatrixMode(GL_PROJECTION);
209         glLoadIdentity();
210         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
211         glMatrixMode(GL_MODELVIEW);
212         glLoadIdentity();
213
214         glBegin(GL_QUADS);
215                 glTexCoord2i(0,0); glVertex2f(0,0);
216                 glTexCoord2i(1,0); glVertex2f(1,0);
217                 glTexCoord2i(1,1); glVertex2f(1,1);
218                 glTexCoord2i(0,1); glVertex2f(0,1);
219         glEnd();
220
221         glDisable(GL_TEXTURE_2D);
222         SDL_FreeSurface(bmp);   
223 }

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