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

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