PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PlatformEnvironment Class Reference

#include <PlatformEnvironment.h>

Inheritance diagram for PlatformEnvironment:
PEnvironment PEBLObjectBase PEnvironment PEBLObjectBase

Public Member Functions

 PlatformEnvironment (PEBLVideoMode mode, PEBLVideoDepth depth, bool windowed, bool resizeable, bool unicode)
 Standard Constructor.
 
virtual ~PlatformEnvironment ()
 Standard Destructor.
 
void Initialize ()
 This method initiates everything needed to display the main window.
 
virtual bool Draw ()
 
PEBLVideoMode GetVideoMode ()
 
PEBLVideoDepth GetVideoDepth ()
 
bool GetWindowed ()
 
Variant GetScreenModes (int screen=-1)
 
Variant GetResizeable ()
 
virtual bool SetProperty (std::string, Variant v)
 
virtual Variant GetProperty (std::string) const
 
virtual int ShowCursor (int val)
 
virtual int SetCursorPosition (int x, int y)
 
virtual Variant GetCursorPosition ()
 
virtual void SetKeyRepeat (bool onoff)
 
virtual Variant GetMouseState ()
 
virtual bool GetUnicode ()
 
virtual int GetNumJoysticks ()
 
virtual Variant GetJoystick (int index)
 
virtual std::string GetSystemLocale ()
 
virtual bool IsSystemLocaleRTL ()
 
 PlatformEnvironment (PEBLVideoMode mode, PEBLVideoDepth depth, bool windowed, bool resizeable, bool unicode)
 
virtual ~PlatformEnvironment ()
 
void Initialize ()
 
virtual bool Draw ()
 
PEBLVideoMode GetVideoMode ()
 
PEBLVideoDepth GetVideoDepth ()
 
bool GetWindowed ()
 
Variant GetScreenModes (int screen=-1)
 
Variant GetResizeable ()
 
virtual bool SetProperty (std::string, Variant v)
 
virtual Variant GetProperty (std::string) const
 
virtual int ShowCursor (int val)
 
virtual int SetCursorPosition (int x, int y)
 
virtual Variant GetCursorPosition ()
 
virtual void SetKeyRepeat (bool onoff)
 
virtual Variant GetMouseState ()
 
virtual bool GetUnicode ()
 
virtual int GetNumJoysticks ()
 
virtual Variant GetJoystick (int index)
 
virtual std::string GetSystemLocale ()
 
virtual bool IsSystemLocaleRTL ()
 
- Public Member Functions inherited from PEnvironment
 PEnvironment ()
 
virtual ~PEnvironment ()
 
virtual bool IsInitialized ()
 
virtual bool AddWindow (PWindow *window)
 
virtual PWindowGetWindow (int index=0)
 
virtual bool RemoveWindow (PWindow *window)
 
- Public Member Functions inherited from PEBLObjectBase
 PEBLObjectBase ()
 Standard Constructor.
 
 PEBLObjectBase (ComplexDataType cdt)
 Standard Constructor.
 
 PEBLObjectBase (const PEBLObjectBase &pob)
 
virtual ~PEBLObjectBase ()
 Standard Destructor.
 
virtual bool InitializeProperty (std::string name, Variant v)
 
Variant GetProperty (std::string) const
 
virtual ObjectValidationError ValidateProperty (std::string, Variant v) const
 
virtual ObjectValidationError ValidateProperty (std::string) const
 
ComplexDataType GetType ()
 
virtual std::string ObjectName () const
 
virtual std::ostream & PrintProperties (std::ostream &out)
 
virtual Variant GetPropertyList ()
 

Protected Member Functions

virtual std::ostream & SendToStream (std::ostream &out) const
 
virtual std::ostream & SendToStream (std::ostream &out) const
 

Additional Inherited Members

- Protected Attributes inherited from PEnvironment
bool mIsInitialized
 
std::list< PWindow * > mWindows
 
- Protected Attributes inherited from PEBLObjectBase
ComplexDataType mCDT
 
std::map< std::string, VariantmProperties
 

Detailed Description

This class initializes the the SDL environment, which may include the video and audio interfaces, the ttf renderer, etc.

Headless platform environment - minimal initialization without SDL Used for validation mode where no actual display is needed

Definition at line 61 of file sdl/PlatformEnvironment.h.

Constructor & Destructor Documentation

◆ PlatformEnvironment() [1/2]

PlatformEnvironment::PlatformEnvironment ( PEBLVideoMode  mode,
PEBLVideoDepth  depth,
bool  windowed,
bool  resizeable,
bool  unicode 
)

Standard Constructor.

Definition at line 62 of file sdl/PlatformEnvironment.cpp.

63 :
65 mVideoMode(mode),
66 mVideoDepth(depth),
67 mWindowed(windowed),
68 mUnicode(unicode),
69 mResizeable(resizeable),
70 mNumJoysticks(0)
71{
72 mIsInitialized = false;
73}
@ CDT_ENVIRONMENT
Definition PEBLObject.h:50
PEBLObjectBase()
Standard Constructor.
bool mIsInitialized

References PEnvironment::mIsInitialized.

◆ ~PlatformEnvironment() [1/2]

PlatformEnvironment::~PlatformEnvironment ( )
virtual

Standard Destructor.

Definition at line 77 of file sdl/PlatformEnvironment.cpp.

78{
79 mWindows.clear();
80
81 // SDL_Quit(); //this can cause a crash; SDL_Quit needs to be called at very end.
82 // especially if there are global SDL objects like gWin remaining.
83 //probably need to explicitly delete the globalvariable map;for now SDL_quit gets
84 //called essentially in main().
85}
std::list< PWindow * > mWindows

References PEnvironment::mWindows.

◆ PlatformEnvironment() [2/2]

PlatformEnvironment::PlatformEnvironment ( PEBLVideoMode  mode,
PEBLVideoDepth  depth,
bool  windowed,
bool  resizeable,
bool  unicode 
)

◆ ~PlatformEnvironment() [2/2]

virtual PlatformEnvironment::~PlatformEnvironment ( )
virtual

Member Function Documentation

◆ Draw() [1/2]

bool PlatformEnvironment::Draw ( )
virtual

This method 'draws' the environment; it iterates through each window in the environment and calls the draw method on each.

Definition at line 199 of file sdl/PlatformEnvironment.cpp.

200{
201 //Iterate through all child windows and draw them
202 list<PWindow*>::iterator p;
203 for(p=mWindows.begin(); p!= mWindows.end(); p++)
204 {
205
206
207 if(*p)
208 (*p)->Draw();
209 }
210
211 return true;
212}

References PEnvironment::mWindows.

Referenced by PEBLObjects::Draw(), PEBLEnvironment::GetInput0(), main(), and PlatformEventQueue::Prime().

◆ Draw() [2/2]

virtual bool PlatformEnvironment::Draw ( )
virtual

◆ GetCursorPosition() [1/2]

Variant PlatformEnvironment::GetCursorPosition ( )
virtual

Definition at line 260 of file sdl/PlatformEnvironment.cpp.

261{
262 int x = 0;
263 int y = 0;
264 SDL_PumpEvents();
265 SDL_GetMouseState(&x,&y);
266
267 PList * newlist = new PList();
268 newlist->PushBack(Variant(x));
269 newlist->PushBack(Variant(y));
271 PComplexData * pcd = new PComplexData(newlist2);
272 return Variant(pcd);
273}
Definition PList.h:45
void PushBack(const Variant &v)
Definition PList.cpp:149

References PList::PushBack().

Referenced by PEBLEnvironment::GetCursorPosition().

◆ GetCursorPosition() [2/2]

virtual Variant PlatformEnvironment::GetCursorPosition ( )
virtual

◆ GetJoystick() [1/2]

Variant PlatformEnvironment::GetJoystick ( int  index)
virtual

Definition at line 329 of file sdl/PlatformEnvironment.cpp.

330{
331
332 if(index < 1 || index > SDL_NumJoysticks())
333 {
334 PError::SignalFatalError("Requesting an invalid joystick");
335 }
336 // SDL_Joystick * joystick = SDL_JoystickOpen(int index);
337 PlatformJoystick* joystick = new PlatformJoystick(index+1);
338
339
340
342
343 PComplexData * pcd = new PComplexData(tmp);
344 return Variant(pcd);
345
346}
void SignalFatalError(const std::string &message)

References PError::SignalFatalError().

◆ GetJoystick() [2/2]

virtual Variant PlatformEnvironment::GetJoystick ( int  index)
virtual

◆ GetMouseState() [1/2]

Variant PlatformEnvironment::GetMouseState ( )
virtual

Definition at line 287 of file sdl/PlatformEnvironment.cpp.

288{
289 int x = 0;
290 int y = 0;
291 SDL_PumpEvents();
292 int button = SDL_GetMouseState(&x,&y);
293 int b1 = button & SDL_BUTTON(1);
294 int b2 = button & SDL_BUTTON(2);
295 int b3 = button & SDL_BUTTON(3);
296
297 PList * newlist = new PList();
298 newlist->PushBack(Variant(x));
299 newlist->PushBack(Variant(y));
300 newlist->PushBack(Variant(b1));
301 newlist->PushBack(Variant(b2));
302 newlist->PushBack(Variant(b3));
303
305 PComplexData * pcd = new PComplexData(newlist2);
306 return Variant(pcd);
307}

References PList::PushBack().

Referenced by PEBLEnvironment::GetMouseState().

◆ GetMouseState() [2/2]

virtual Variant PlatformEnvironment::GetMouseState ( )
virtual

◆ GetNumJoysticks() [1/2]

int PlatformEnvironment::GetNumJoysticks ( )
virtual

Definition at line 312 of file sdl/PlatformEnvironment.cpp.

313{
314 //Initialize joystick subsystem if has not yet been.
315 if(SDL_InitSubSystem(SDL_INIT_JOYSTICK)<0)
316 {
317 PError::SignalWarning("No joysticks available");
318 }
319 int num = SDL_NumJoysticks();
320
321 return num;
322}
void SignalWarning(const std::string &message)
Definition PError.cpp:119

References PError::SignalWarning().

Referenced by PEBLEnvironment::GetNumJoysticks().

◆ GetNumJoysticks() [2/2]

virtual int PlatformEnvironment::GetNumJoysticks ( )
virtual

◆ GetProperty() [1/2]

Variant PlatformEnvironment::GetProperty ( std::string  name) const
virtual

Definition at line 222 of file sdl/PlatformEnvironment.cpp.

223{
224 return PEBLObjectBase::GetProperty(name);
225}
Variant GetProperty(std::string) const

References PEBLObjectBase::GetProperty().

◆ GetProperty() [2/2]

virtual Variant PlatformEnvironment::GetProperty ( std::string  ) const
virtual

◆ GetResizeable() [1/2]

Variant PlatformEnvironment::GetResizeable ( )
inline

Definition at line 80 of file sdl/PlatformEnvironment.h.

80{return mResizeable;};

Referenced by PEBLObjects::MakeWindow().

◆ GetResizeable() [2/2]

Variant PlatformEnvironment::GetResizeable ( )
inline

Definition at line 52 of file validator/PlatformEnvironment.h.

52{ return mResizeable; };

◆ GetScreenModes() [1/2]

Variant PlatformEnvironment::GetScreenModes ( int  screen = -1)

Definition at line 349 of file sdl/PlatformEnvironment.cpp.

350{
351
352
353 int low,hi;
354
355 int numdisplays = SDL_GetNumVideoDisplays();
356
357 if(screen<0 | screen >=numdisplays)
358 {
359 low=0;
360 hi = numdisplays;
361 }
362 else
363 {
364 low = screen;
365 hi = screen+1;
366 }
367
368
369
370 // Uint32 f;
371 PList * baselist = new PList();
372 PComplexData * pcd=NULL;
373
374
375 //Get a list of available resolutions and try these out.
376
377
378 int nummodes = 0;
379 int totalmodes = 0;
380 SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
381 for(int i = low; i < hi; i++) //go through each screen specified.
382 {
383 nummodes = SDL_GetNumDisplayModes(i); //We should query each display here.
384 totalmodes += nummodes;
385
386 for(int modeid = 0;modeid < nummodes; modeid++)
387 {
388
389 SDL_GetDisplayMode(i,modeid,&mode);
390
391 PList * newlist = new PList();
392
393 newlist->PushBack(Variant(mode.w));
394 newlist->PushBack(Variant(mode.h));
395 newlist->PushBack(Variant(i)); //add screen ID
396 newlist->PushBack(Variant(mode.refresh_rate));
397
399 pcd = new PComplexData(newlist2);
400 baselist->PushBack(Variant(pcd));
401
402 }
403 }
404
405
407 pcd = new PComplexData(baselist2);
408 return Variant(pcd);
409}
#define NULL
Definition BinReloc.cpp:317

References NULL, and PList::PushBack().

Referenced by PEBLEnvironment::GetVideoModes().

◆ GetScreenModes() [2/2]

Variant PlatformEnvironment::GetScreenModes ( int  screen = -1)

◆ GetSystemLocale() [1/2]

std::string PlatformEnvironment::GetSystemLocale ( )
virtual

Implements PEnvironment.

Definition at line 415 of file sdl/PlatformEnvironment.cpp.

415 {
416 SDL_Locale *locales = SDL_GetPreferredLocales();
417 if (!locales) {
418 return ""; // Error or not supported on this platform
419 }
420
421 // Get the first (primary) locale
422 std::string result = "";
423 if (locales[0].language) {
424 result = locales[0].language;
425
426 // Append country code if available (e.g., "en_US", "zh_CN")
427 if (locales[0].country) {
428 result += "_";
429 result += locales[0].country;
430 }
431 }
432
433 SDL_free(locales);
434 return result; // e.g., "ar", "en_US", "zh_CN", "he_IL", "ko_KR"
435}

Referenced by PEBLString::GetSystemLocale(), and IsSystemLocaleRTL().

◆ GetSystemLocale() [2/2]

virtual std::string PlatformEnvironment::GetSystemLocale ( )
inlinevirtual

Implements PEnvironment.

Definition at line 69 of file validator/PlatformEnvironment.h.

69{ return ""; }

◆ GetUnicode() [1/2]

virtual bool PlatformEnvironment::GetUnicode ( )
inlinevirtual

Definition at line 93 of file sdl/PlatformEnvironment.h.

93{return mUnicode;};

◆ GetUnicode() [2/2]

virtual bool PlatformEnvironment::GetUnicode ( )
inlinevirtual

Definition at line 63 of file validator/PlatformEnvironment.h.

63{ return mUnicode; };

◆ GetVideoDepth() [1/2]

PEBLVideoDepth PlatformEnvironment::GetVideoDepth ( )
inline

Definition at line 76 of file sdl/PlatformEnvironment.h.

76{return mVideoDepth;};

Referenced by PEBLObjects::MakeWindow().

◆ GetVideoDepth() [2/2]

PEBLVideoDepth PlatformEnvironment::GetVideoDepth ( )
inline

Definition at line 48 of file validator/PlatformEnvironment.h.

48{ return mVideoDepth; };

◆ GetVideoMode() [1/2]

PEBLVideoMode PlatformEnvironment::GetVideoMode ( )
inline

Definition at line 75 of file sdl/PlatformEnvironment.h.

75{return mVideoMode;};

Referenced by PEBLObjects::MakeWindow().

◆ GetVideoMode() [2/2]

PEBLVideoMode PlatformEnvironment::GetVideoMode ( )
inline

Definition at line 47 of file validator/PlatformEnvironment.h.

47{ return mVideoMode; };

◆ GetWindowed() [1/2]

bool PlatformEnvironment::GetWindowed ( )
inline

Definition at line 77 of file sdl/PlatformEnvironment.h.

77{return mWindowed;};

Referenced by PEBLObjects::MakeWindow().

◆ GetWindowed() [2/2]

bool PlatformEnvironment::GetWindowed ( )
inline

Definition at line 49 of file validator/PlatformEnvironment.h.

49{ return mWindowed; };

◆ Initialize() [1/2]

void PlatformEnvironment::Initialize ( )
virtual

This method initiates everything needed to display the main window.

Implements PEnvironment.

Definition at line 99 of file sdl/PlatformEnvironment.cpp.

100{
101
102 mIsInitialized = true;
103
104#ifdef PEBL_EMSCRIPTEN
105#define SDLINIT_FLAGS SDL_INIT_VIDEO
106#else
107#define SDLINIT_FLAGS SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE
108#endif
109 if ( SDL_Init(SDLINIT_FLAGS) < 0 )
110 {
111
112 cerr << "Unable to init SDL: " << SDL_GetError() << endl;;
113 mIsInitialized = false;
114 }
115 else{
116
117 std::cerr << "SDL INITIALIZED\n";
118 const char* audioDriver = SDL_GetCurrentAudioDriver();
119 if(audioDriver) {
120 std::cerr << "SDL Audio Driver: " << audioDriver << "\n";
121 } else {
122 std::cerr << "SDL Audio Driver: NONE\n";
123 }
124
125#ifdef PEBL_EMSCRIPTEN
126 if (TTF_Init() < 0 )
127#else
128 if (!TTF_WasInit() && TTF_Init() < 0 )
129#endif
130 {
131 /* Initialize the TTF library */
132 cerr << "Couldn't initialize TTF: " << SDL_GetError() << endl;
133 mIsInitialized = false;
134 } else
135 {
136 std::cerr << "TTF INITIALIZED\n";
137 }
138
139 // Initialize SDL_image for JPEG, PNG, etc.
140#ifdef PEBL_EMSCRIPTEN
141 // Emscripten: formats specified at compile time via SDL2_IMAGE_FORMATS
142 int imgFlags = IMG_INIT_JPG | IMG_INIT_PNG;
143#else
144 int imgFlags = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF;
145#endif
146 cerr << "Attempting to initialize SDL_image with flags: " << imgFlags << endl;
147 cerr << " IMG_INIT_JPG = " << IMG_INIT_JPG << endl;
148 cerr << " IMG_INIT_PNG = " << IMG_INIT_PNG << endl;
149
150 int imgInitResult = IMG_Init(imgFlags);
151 cerr << "IMG_Init returned: " << imgInitResult << endl;
152
153 if ((imgInitResult & imgFlags) != imgFlags)
154 {
155 cerr << "WARNING: SDL_image could not initialize all requested formats!" << endl;
156 cerr << " Requested flags: " << imgFlags << endl;
157 cerr << " Initialized flags: " << imgInitResult << endl;
158 cerr << " IMG_Error: " << IMG_GetError() << endl;
159
160 if (!(imgInitResult & IMG_INIT_JPG)) {
161 cerr << " JPEG support NOT available!" << endl;
162 }
163 if (!(imgInitResult & IMG_INIT_PNG)) {
164 cerr << " PNG support NOT available!" << endl;
165 }
166 } else
167 {
168 std::cerr << "SDL_image INITIALIZED successfully with all formats" << endl;
169 std::cerr << " JPEG support: YES" << endl;
170 std::cerr << " PNG support: YES" << endl;
171 }
172
173 // Pre-initialize keycode lookup tables to avoid timing artifacts on first keypress
174 // This is critical for psychological experiments with accurate response time measurements
176
177#define PRINTCONSOLE 0
178#if PRINTCONSOLE
179 //cout << "reopening console\n";
180 //This needs to be done after SDL_Init()
181 std::ofstream cct1("CON1");
182 std::ofstream cct2("CON2");
183
184 freopen( "CON1", "w", stdout );
185 freopen( "CON2", "w", stderr );
186#endif
187
188 }
189 //This shouldn't be neded, if platformenvironment gets cleaned up alright,
190 //but let's do it anyway.
191 //atexit(SDL_Quit);
192 // std::cerr << "SDL INITIALIZATION COMPLETE\n";
193}
void InitializeKeycodeLookups()
#define SDLINIT_FLAGS

References PEBLUtility::InitializeKeycodeLookups(), PEnvironment::mIsInitialized, and SDLINIT_FLAGS.

Referenced by main(), and PEBLObjects::MakeEnvironment().

◆ Initialize() [2/2]

void PlatformEnvironment::Initialize ( )
virtual

Implements PEnvironment.

◆ IsSystemLocaleRTL() [1/2]

bool PlatformEnvironment::IsSystemLocaleRTL ( )
virtual

Implements PEnvironment.

Definition at line 439 of file sdl/PlatformEnvironment.cpp.

439 {
440 std::string locale = GetSystemLocale();
441 if (locale.empty()) {
442 return false; // Default to LTR if we can't detect
443 }
444
445 // Extract language code (first 2 characters)
446 std::string langCode = locale.substr(0, 2);
447 std::transform(langCode.begin(), langCode.end(), langCode.begin(), ::tolower);
448
449 // Check if it's Arabic or Hebrew
450 return (langCode == "ar" || langCode == "he" || langCode == "iw");
451}
virtual std::string GetSystemLocale()

References GetSystemLocale().

Referenced by PEBLString::IsSystemLocaleRTL().

◆ IsSystemLocaleRTL() [2/2]

virtual bool PlatformEnvironment::IsSystemLocaleRTL ( )
inlinevirtual

Implements PEnvironment.

Definition at line 70 of file validator/PlatformEnvironment.h.

70{ return false; }

◆ SendToStream() [1/2]

std::ostream & PlatformEnvironment::SendToStream ( std::ostream &  out) const
protectedvirtual

Reimplemented from PEnvironment.

Definition at line 123 of file validator/PlatformEnvironment.cpp.

123 {
124 out << "PlatformEnvironment (validator)";
125 return out;
126}

◆ SendToStream() [2/2]

virtual std::ostream & PlatformEnvironment::SendToStream ( std::ostream &  out) const
protectedvirtual

Reimplemented from PEnvironment.

◆ SetCursorPosition() [1/2]

int PlatformEnvironment::SetCursorPosition ( int  x,
int  y 
)
virtual

Definition at line 244 of file sdl/PlatformEnvironment.cpp.

245{
246 //this will move the mouse in the first window.
247 //another method is necessary to move it within an arbitrary window.
248 PlatformWindow* pw =dynamic_cast<PlatformWindow*>( mWindows.front());
249
250 SDL_SetRelativeMouseMode(SDL_FALSE);
251 //cout << "Warping mouse:" <<x << "|" << y<< std::endl;
252 SDL_Window *win = pw->PlatformWindow::GetSDLWindow();
253 SDL_WarpMouseInWindow(win,x,y);
254 //printf("SDL_warp error?: %s\n", SDL_GetError());
255
256 return 1;
257}

References PEnvironment::mWindows.

Referenced by PEBLEnvironment::SetCursorPosition().

◆ SetCursorPosition() [2/2]

virtual int PlatformEnvironment::SetCursorPosition ( int  x,
int  y 
)
virtual

◆ SetKeyRepeat() [1/2]

void PlatformEnvironment::SetKeyRepeat ( bool  onoff)
virtual

Definition at line 276 of file sdl/PlatformEnvironment.cpp.

277{
278 //This is no longer available in SDL2
279#ifdef SDL2_DELETE
280 if(onoff)
281 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
282 else
283 SDL_EnableKeyRepeat(0,0);
284#endif
285}

Referenced by PEBLEnvironment::GetInput0().

◆ SetKeyRepeat() [2/2]

virtual void PlatformEnvironment::SetKeyRepeat ( bool  onoff)
virtual

◆ SetProperty() [1/2]

bool PlatformEnvironment::SetProperty ( std::string  name,
Variant  v 
)
virtual

Reimplemented from PEBLObjectBase.

Definition at line 216 of file sdl/PlatformEnvironment.cpp.

217{
218 //this class has no settable properties.
219 return false;
220}

◆ SetProperty() [2/2]

virtual bool PlatformEnvironment::SetProperty ( std::string  ,
Variant  v 
)
virtual

Reimplemented from PEBLObjectBase.

◆ ShowCursor() [1/2]

int PlatformEnvironment::ShowCursor ( int  val)
virtual

Definition at line 230 of file sdl/PlatformEnvironment.cpp.

231{
232
233 if(val)
234 {
235 return SDL_ShowCursor(SDL_ENABLE);
236 }
237 else
238 {
239 return SDL_ShowCursor(SDL_DISABLE);
240 }
241}

Referenced by PEBLEnvironment::ShowCursor().

◆ ShowCursor() [2/2]

virtual int PlatformEnvironment::ShowCursor ( int  val)
virtual

The documentation for this class was generated from the following files: