comparison Applications/Sdl/SdlWindow.cpp @ 51:b340879da9bd

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 27 Apr 2017 14:50:20 +0200
parents Framework/Applications/Sdl/SdlWindow.cpp@28956ed68280
children ea377c770ef4
comparison
equal deleted inserted replaced
49:c45f368de5c0 51:b340879da9bd
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017 Osimis, Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "SdlWindow.h"
23
24 #if ORTHANC_ENABLE_SDL == 1
25
26 #include "../../../Resources/Orthanc/Core/Logging.h"
27 #include "../../../Resources/Orthanc/Core/OrthancException.h"
28
29 namespace OrthancStone
30 {
31 SdlWindow::SdlWindow(const char* title,
32 unsigned int width,
33 unsigned int height,
34 bool enableOpenGl) :
35 maximized_(false)
36 {
37 // TODO Understand why, with SDL_WINDOW_OPENGL + MinGW32 + Release
38 // build mode, the application crashes whenever the SDL window is
39 // resized or maximized
40
41 uint32_t windowFlags, rendererFlags;
42 if (enableOpenGl)
43 {
44 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
45 rendererFlags = SDL_RENDERER_ACCELERATED;
46 }
47 else
48 {
49 windowFlags = SDL_WINDOW_RESIZABLE;
50 rendererFlags = SDL_RENDERER_SOFTWARE;
51 }
52
53 window_ = SDL_CreateWindow(title,
54 SDL_WINDOWPOS_UNDEFINED,
55 SDL_WINDOWPOS_UNDEFINED,
56 width, height, windowFlags);
57
58 if (window_ == NULL)
59 {
60 LOG(ERROR) << "Cannot create the SDL window: " << SDL_GetError();
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
62 }
63
64 renderer_ = SDL_CreateRenderer(window_, -1, rendererFlags);
65 if (!renderer_)
66 {
67 LOG(ERROR) << "Cannot create the SDL renderer: " << SDL_GetError();
68 SDL_DestroyWindow(window_);
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
70 }
71 }
72
73
74 SdlWindow::~SdlWindow()
75 {
76 if (renderer_ != NULL)
77 {
78 SDL_DestroyRenderer(renderer_);
79 }
80
81 if (window_ != NULL)
82 {
83 SDL_DestroyWindow(window_);
84 }
85 }
86
87
88 unsigned int SdlWindow::GetWidth() const
89 {
90 int w = -1;
91 SDL_GetWindowSize(window_, &w, NULL);
92
93 if (w < 0)
94 {
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
96 }
97 else
98 {
99 return static_cast<unsigned int>(w);
100 }
101 }
102
103
104 unsigned int SdlWindow::GetHeight() const
105 {
106 int h = -1;
107 SDL_GetWindowSize(window_, NULL, &h);
108
109 if (h < 0)
110 {
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
112 }
113 else
114 {
115 return static_cast<unsigned int>(h);
116 }
117 }
118
119
120 void SdlWindow::Render(SDL_Surface* surface)
121 {
122 //SDL_RenderClear(renderer_);
123
124 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer_, surface);
125 if (texture != NULL)
126 {
127 SDL_RenderCopy(renderer_, texture, NULL, NULL);
128 SDL_DestroyTexture(texture);
129 }
130
131 SDL_RenderPresent(renderer_);
132 }
133
134
135 void SdlWindow::ToggleMaximize()
136 {
137 if (maximized_)
138 {
139 SDL_RestoreWindow(window_);
140 maximized_ = false;
141 }
142 else
143 {
144 SDL_MaximizeWindow(window_);
145 maximized_ = true;
146 }
147 }
148 }
149
150 #endif