PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
sdl/PlatformImageBox.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/PlatformImageBox.cpp
4// Purpose: Contains SDL-specific interface for images
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-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
15// by the Free Software Foundation; either version 2 of the License,
16// or (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#include "PlatformImageBox.h"
28#include "../../utility/PEBLPath.h"
29#include "../../utility/PError.h"
30
31#include "../../base/PEBLObject.h"
32
33#ifdef PEBL_EMSCRIPTEN
34#include "../../base/Evaluator-es.h"
35#else
36#include "../../base/Evaluator.h"
37#endif
38
39#include "SDL.h"
40#include "SDL_image.h"
41
42#include <string>
43#include <stdio.h>
44
45#include <assert.h>
46//#include <emscripten.h>
47
48
49// cout removed - use cerr for debug output
50using std::cerr;
51using std::endl;
52using std::flush;
53using std::list;
54using std::ostream;
55using std::string;
56
57#define SDL_IMAGE
58
59
72
73
74
80
81
82
83// Inheritable function that is called by friend method << operator of PComplexData
84
85ostream & PlatformImageBox::SendToStream(ostream& out) const
86{
87
88 out << "<SDL PlatformImageBox>" << flush;
89
90 return out;
91}
92
93
94
95
96
97bool PlatformImageBox::LoadImage(const std::string & imagefilename)
98{
99
100
101 //Check to see if we can find the image; if not, call everything off.
102 string filename = Evaluator::gPath.FindFile(imagefilename);
103
104 if(filename == "")
105 PError::SignalFatalError(string("Unable to find image file [") +
106 imagefilename + string("]."));
107
108
109
110 //This uses the SDL_image library to load a variety of
111 //image types.
112
113 //If the SDL_image library is being used, we can handle many different types
114 //of images. If it isn't, use SDL's built-in bmp loader.
115#ifdef PEBL_EMSCRIPTENX
116
117 mSurface = SDL_LoadBMP(filename.c_str());
118#else
119
120#ifdef SDL_IMAGE
121 mSurface = IMG_Load(filename.c_str());
122#else
123 mSurface = SDL_LoadBMP(filename.c_str());
124#endif
125
126#endif
127
128 //Now, set the height and width to be the same as the
129 //initial image.
130
131 if(mSurface)
132 {
133
134 mWidth = mSurface->w;
135 mHeight = mSurface->h;
138
139 //These need to be set at the PWidget level because
140 //they are not mutable at the imagebox level.
143 return true;
144 }
145 else
146 {
147
148 PWidget::SetProperty("WIDTH", 0);
149 PWidget::SetProperty("HEIGHT", 0);
150
151
152 PError::SignalFatalError(string("Image not created.[") +
153 imagefilename + string("].") +
154 string(IMG_GetError()));
155 return false;
156 }
157}
158
159
160
162{
163
164 if(mRenderer)
165 {
166
167 if(!mTexture)
168 {
169
170 mTexture = SDL_CreateTextureFromSurface(mRenderer, mSurface);
171 mTextureCreated = true;
172
173 }
174 else
175 {
176 //In this case, a texture already exists...
177
178 }
179 } else {
180
181 }
182 int out = PlatformWidget::Draw();
183 return out==0;
184}
185
186//
187
189{
190
191 mZoomX = x;
193 SetProperty("X",mX); //reset X as it matters for images/labels
194 SetProperty("WIDTH",(int)(mTextureWidth*mZoomX));
195}
196
199{
200
201 mZoomY = y;
203 SetProperty("Y",mY);//reset y as it matters for images/labels
204 SetProperty("HEIGHT",(int)(mTextureHeight*mZoomY));
205}
206
207
208
#define NULL
Definition BinReloc.cpp:317
#define pDouble
Definition Defs.h:7
@ CDT_IMAGEBOX
Definition PEBLObject.h:53
static PEBLPath gPath
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
pDouble mZoomX
Definition PWidget.h:139
virtual bool SetProperty(std::string, Variant v)
Definition PWidget.cpp:142
pInt mWidth
Definition PWidget.h:136
pInt mX
Definition PWidget.h:125
pInt mHeight
Definition PWidget.h:136
pInt mY
Definition PWidget.h:125
pDouble mZoomY
Definition PWidget.h:140
virtual ~PlatformImageBox()
Standard Destructor.
virtual std::ostream & SendToStream(std::ostream &out) const
An inheritable printing class used by PEBLObjectBase::operator<<.
virtual void SetZoomX(pDouble x)
This sets the widget's position on its parent widget.
virtual void SetZoomY(pDouble x)
This sets the widget's position on its parent widget.
virtual bool SetProperty(std::string name, Variant v)
PlatformImageBox()
Standard Constructor.
virtual bool LoadImage(const std::string &imagefilename)
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)