712bd260982bd422a489747dcc0efbd9e4032017
[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         glReadPixels(0,0,w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
137
138         SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0,0,0,0);
139         if (surf == NULL)
140                 Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
141
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);
144
145         if (SDL_SaveBMP(surf, filename) != 0)
146                 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
147         
148         SDL_FreeSurface(surf);
149         delete [] pixels;
150         Debug("Succeeded!");
151 }
152
153 /**
154  * Render a BMP
155  * NOT PART OF THE DOCUMENT FORMAT
156  */
157 void Screen::RenderBMP(const char * filename) const
158 {
159         SDL_Surface * bmp = SDL_LoadBMP(filename);
160         if (bmp == NULL)
161                 Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());
162
163         int w = bmp->w;
164         int h = bmp->h;
165
166         GLenum texture_format; 
167         switch (bmp->format->BytesPerPixel)
168         {
169                 case 4: //contains alpha
170                         texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
171                         break;
172                 case 3: //does not contain alpha
173                         texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;  
174                         break;
175                 default:
176                         Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel);
177                         break;  
178         }
179
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);
181
182
183         GLuint texID;
184         glEnable(GL_TEXTURE_2D);
185         glGenTextures(1, &texID);
186         glBindTexture(GL_TEXTURE_2D, texID);
187
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);
192
193         glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels);
194
195         glMatrixMode(GL_PROJECTION);
196         glLoadIdentity();
197         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
198         glMatrixMode(GL_MODELVIEW);
199         glLoadIdentity();
200
201         glBegin(GL_QUADS);
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);
206         glEnd();
207
208         glDisable(GL_TEXTURE_2D);
209         SDL_FreeSurface(bmp);   
210 }

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