PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
sdl/PlatformCanvas.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/platforms/sdl/PlatformTextBox.cpp
4// Purpose: Contains SDL-specific interface for a draw canvas.
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2010-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28#include "PlatformCanvas.h"
29#include "../../objects/PCanvas.h"
30#include "SDLUtility.h"
31
32#include "../../base/PComplexData.h"
33
34
35#include "../../utility/rc_ptrs.h"
36#include "../../utility/PError.h"
37#include "../../utility/PEBLUtility.h"
38
39#include "SDL.h"
40#include <stdio.h>
41#include <string>
42
43// cout removed - use cerr for debug output
44using std::cerr;
45using std::endl;
46using std::flush;
47using std::list;
48using std::ostream;
49using std::string;
50
51
52PlatformCanvas::PlatformCanvas(int width, int height, Variant bg):
53
54 PCanvas(width, height,bg),
55 mPixels(NULL),
57
58{
59
60 mTextureWidth = width;
61 mTextureHeight = height;
62
64 InitializeProperty("BGCOLOR",bg);
65 SetColor(bg);
66
67 mSurface = NULL;
69 mTexture = NULL;
70 mNeedsTexture = true;
71 Reset();
72 Draw();
73
74}
75
76
77
78PlatformCanvas::PlatformCanvas(int width, int height):
79 mCanvasWidth(0),
80 mCanvasHeight(0),
81 mPixels(NULL),
83 PCanvas(width, height)
84{
85
86 mTextureWidth = width;
87 mTextureHeight = height;
88
89 mWidth =width;
90 mHeight = height;
91
93
94 mSurface = NULL;
95 mTexture = NULL;
97 mNeedsTexture = true;
98 Reset();
99 //Draw();
100
101}
102
103
104
105
109 PCanvas()
110{
111
114 mWidth = canvas.GetWidth();
115 mHeight = canvas.GetHeight();
118
119 mTexture = NULL;
120 mSurface = NULL;
121 mNeedsTexture = true;
122 Reset(); //reset initializes the canvas
123 //Draw();
124
125}
126
127
130{
131
132 // PlatformWidget frees mSurface,
133 // and should free children...
134
135}
136
137// Inheritable function that is called by friend method << operator of PComplexData
138ostream & PlatformCanvas::SendToStream(ostream& out) const
139{
140 out << "<SDL PlatformCanvas>" <<flush;
141 return out;
142}
143
144
145
146
150{
151
152
153
154 //create a new surface on which to render the text.
155
156 if(mSurface)
157 {
158 delete mSurface;
159 mSurface = NULL;
160 }
161
162 //Make a transparent surface of the prescribed size.
163
164#if 0
165 SDL_Surface * tmp = NULL;
166 // SDL_CreateRGBSurface(SDL_SWSURFACE,
167 // mWidth, mHeight,
168 // 32, rmask, gmask, bmask, amask);
169 if(!tmp) PError::SignalFatalError("Surface not created in Canvas::Reset.");
170
171#endif
172
173#ifdef SD2_DELETE
174
175 SDL_SetAlpha(mSurface,0,SDL_ALPHA_TRANSPARENT);
176 // SDL_SetAlpha( tmp, 0, SDL_ALPHA_OPAQUE );
177#endif
178
179
180
181 //Fill the box with the background color
182 // std::cout << mBackgroundColor << "|alpha:|" << mBackgroundColor.GetAlpha() << std::endl;
183
184
185
186 // if(mBackgroundColor.GetAlpha() <.000001)
187
188 // {
189 // std::cout << "Setting transparency\n";
190 //SDL_SetAlpha(mSurface,0,0);
191
192 // }
193 mReset = false; //Reset the reset flag.
194
195
196 //we can only render onto a texture if renderer exists.
197 //i.e., the canvas needs to be on a window.
198
199 if(mTexture)
200 {
201
202 SDL_DestroyTexture(mTexture);
203 mTexture =NULL;
204 mReset = true;
205 }
206 else
207 {
208 mReset = true;
209 }
210
211 if(mRenderer)
212 {
213
214
215 //we create texture from hidden surface to allow drawing on surface.
216 //mTexture = SDL_CreateTextureFromSurface(mRenderer,tmp);
217
218 mTexture = SDL_CreateTexture(mRenderer,
219 SDL_PIXELFORMAT_RGBA8888,
220 SDL_TEXTUREACCESS_TARGET,
221 (int)mTextureWidth,(int)mTextureHeight);
222
223 SDL_Rect screensize = {0,0,(int)(mTextureWidth),
224 (int)mTextureHeight};
225 SDL_SetRenderTarget(mRenderer,mTexture);
226 SDL_RenderClear( mRenderer );
227
228
229 SDL_SetRenderDrawColor(mRenderer, (mBackgroundColor.GetRed()),
233
234 SDL_RenderFillRect(mRenderer,&screensize);
235
236
237 SDL_SetRenderTarget(mRenderer,NULL);
238
239 //SDL_FreeSurface(tmp);
240
241 }else{
242 mReset = true;
243 }
244
245
246 if(mTexture)
247 return true;
248 else
249 {
250 mReset = true;
251 return false;
252 }
253}
254
255
256bool PlatformCanvas::SetProperty(std::string name, Variant v)
257{
258
259 //we need to handle zooming separately
260 if(name == "ZOOMX") SetZoomX((pDouble)v);
261 else if(name=="ZOOMY") SetZoomY((pDouble)v);
262 else return PCanvas::SetProperty(name,v);
263
264 return true;
265}
266
267
269{
270 //Can we only reset if something has changed on a child?
271 mReset = true;
272 if(mReset)
273 {
274 }
275 Reset();
276
277
278 bool ret = PlatformWidget::Draw();
279 mReset = false;
280
281
282 return ret;
283}
284
286{
287 mHeight = h;
288 mTextureHeight = h;
290
291}
292
294{
295
296 mWidth = w;
297 mTextureWidth = w;
299
300}
301
303{
304
305 mZoomX = x;
307 SetProperty("X",mX); //reset X as it matters for images/labels
308 mWidth = (int)(mTextureWidth*mZoomX);
310
311
312}
313
315{
316
317 mZoomY = y;
319 SetProperty("Y",mY); //reset X as it matters for images/labels
322
323
324}
325
326
327#if 0
328
329bool PlatformCanvas::GreyscaleFromMatrix()
330{
331
332
333 //Check to see if we can find the image; if not, call everything off.
334 string filename = Evaluator::gPath.FindFile(imagefilename);
335
336 if(filename == "")
337 PError::SignalFatalError(string("Unable to find image file [") + imagefilename + string("]."));
338
339
340 //This uses the SDL_image library to load a variety of
341 //image types.
342#ifdef SDL2_DELETE
343 if(mSurface)
344 {
345 SDL_FreeSurface(mSurface);
346 }
347#endif
348
349
350
351 //If the SDL_image library is being used, we can handle many different types
352 //of images. If it isn't, use SDL's built-in bmp loader.
353
354 //uSE THE FOLLOWING:
355
356 //SDL_Surface *SDL_CreateRGBSurfaceFrom(void *pixels,
357 // int width, int height, int depth, int pitch,
358 // Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
359
360 //Checking should be done here to insure the proper color depth,
361 //bpp, format, etc.
362
363
364
365 //Now, set the height and width to be the same as the
366 //initial image.
367 if( mSurface)
368 {
369
370 mWidth = mSurface->w;
371 mHeight = mSurface->h;
372 //These need to be set at the PWidget level because
373 //they are not mutable at the imagebox level.
376 return true;
377 }
378 else
379 {
380 PWidget::SetProperty("WIDTH", 0);
381 PWidget::SetProperty("HEIGHT", 0);
382
383 return false;
384 }
385}
386
387
388
389#endif
#define NULL
Definition BinReloc.cpp:317
#define pInt
Definition Defs.h:8
#define pDouble
Definition Defs.h:7
@ CDT_CANVAS
Definition PEBLObject.h:63
static PEBLPath gPath
This class is the basic generic text box.
Definition PCanvas.h:39
bool mReset
Definition PCanvas.h:67
virtual void SetColor(Variant col)
Definition PCanvas.cpp:112
virtual bool SetProperty(std::string, Variant v)
Definition PCanvas.cpp:84
bool mDrawBackground
Definition PCanvas.h:66
virtual bool GetDrawBackground() const
Definition PCanvas.h:60
int GetRed() const
Definition PColor.cpp:226
int GetAlpha() const
Definition PColor.cpp:229
int GetBlue() const
Definition PColor.cpp:228
int GetGreen() const
Definition PColor.cpp:227
virtual bool InitializeProperty(std::string name, Variant v)
virtual bool SetProperty(std::string name, Variant v)
ComplexDataType mCDT
Definition PEBLObject.h:109
std::string FindFile(const string &filename)
Definition PEBLPath.cpp:368
virtual bool SetProperty(std::string, Variant v)
Definition PImageBox.cpp:79
virtual pInt GetWidth() const
Definition PWidget.h:85
virtual pInt GetHeight() const
Definition PWidget.h:86
pDouble mZoomX
Definition PWidget.h:139
virtual bool SetProperty(std::string, Variant v)
Definition PWidget.cpp:142
pInt mWidth
Definition PWidget.h:136
PColor mBackgroundColor
The background color of the widget. if alpha = 0, will not be painted.
Definition PWidget.h:144
pInt mX
Definition PWidget.h:125
pInt mHeight
Definition PWidget.h:136
pInt mY
Definition PWidget.h:125
pDouble mZoomY
Definition PWidget.h:140
Validator platform canvas - no pixel buffer, used only for compilation.
virtual void SetZoomX(double h)
virtual bool Draw()
This method initiates everything needed to display the main window
virtual void SetWidth(pInt h)
PlatformCanvas(int width, int height, Variant bg)
virtual bool SetProperty(std::string, Variant v)
virtual void SetHeight(pInt h)
virtual void SetZoomY(double h)
virtual ~PlatformCanvas()
Standard Destructor.
virtual std::ostream & SendToStream(std::ostream &out) const
An inheritable printing class used by PEBLObjectBase::operator<<.
SDL_Surface * mSurface
SDL_Texture * mTexture
virtual bool Draw()
This method initiates everything needed to display the main window
SDL_Renderer * mRenderer
void SignalFatalError(const std::string &message)