You can move around a document with Click+Drag!
[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 }
25
26 Screen::~Screen()
27 {
28         SDL_GL_DeleteContext(m_gl_context);
29         SDL_DestroyWindow(m_window);
30         SDL_Quit();
31 }
32
33 void Screen::ResizeViewport(int width, int height)
34 {
35         glViewport(0, 0, width, height);
36         m_viewport_width = width;
37         m_viewport_height = height;
38 }
39
40 bool Screen::PumpEvents()
41 {
42         SDL_Event evt;
43         bool no_quit_requested = true;
44         while (SDL_PollEvent(&evt))
45         {
46                 switch (evt.type)
47                 {
48                 case SDL_QUIT:
49                         no_quit_requested = false;
50                         break;
51                 case SDL_WINDOWEVENT:
52                         switch (evt.window.event)
53                         {
54                         case SDL_WINDOWEVENT_RESIZED:
55                         case SDL_WINDOWEVENT_SIZE_CHANGED:
56                                 ResizeViewport(evt.window.data1, evt.window.data2);
57                                 break;
58                         }
59                         break;
60                 case SDL_MOUSEMOTION:
61                         if (m_mouse_handler)
62                         {
63                                 m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
64                         }
65                         break;
66                 case SDL_MOUSEBUTTONDOWN:
67                 case SDL_MOUSEBUTTONUP:
68                         if (m_mouse_handler)
69                         {
70                                 m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0);
71                         }
72                         break;
73                 default:
74                         break;
75                 }
76         }
77         return no_quit_requested;
78 }
79
80 void Screen::SetMouseCursor(Screen::MouseCursors cursor)
81 {
82         SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
83         switch (cursor)
84         {
85         case CursorArrow: system_cursor_id = SDL_SYSTEM_CURSOR_ARROW; break;
86         case CursorWait: system_cursor_id = SDL_SYSTEM_CURSOR_WAIT; break;
87         case CursorWaitArrow: system_cursor_id = SDL_SYSTEM_CURSOR_WAITARROW; break;
88         case CursorMove: system_cursor_id = SDL_SYSTEM_CURSOR_SIZEALL; break;
89         case CursorHand: system_cursor_id = SDL_SYSTEM_CURSOR_HAND; break;
90         default: break;
91         }
92         SDL_Cursor *system_cursor = SDL_CreateSystemCursor(system_cursor_id);
93         SDL_SetCursor(system_cursor);
94         //TODO: Check if we need to free the system cursors.
95 }
96
97 void Screen::Present()
98 {
99         SDL_GL_SwapWindow(m_window);
100 }
101

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