The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
DesktopSDL2.h
Go to the documentation of this file.
1
2/* *************************************************************************/
30#ifndef DESKTOPINTERFACESDL2
31#define DESKTOPINTERFACESDL2
32
33#include <thread>
34#include <atomic>
35#include <memory>
36// The following line disables the main() that SDL has
37#define SDL_MAIN_HANDLED
38#include <SDL2/SDL.h>
39#include "DesktopInterface.h"
40
41
42class DesktopSDL2; // forward declarations. This class is the main one
43class PalWinSDL2;
44
45
46/* ****************************************************************************
47
48 Singleton class: DesktopSDL2
49
50*******************************************************************************/
51
54{
55 public:
56
57 /* -------------------- Class methods ------------------------------ */
58
59 static DesktopSDL2 & theDesktop(const SoundPlayerParms & spps);
61
69 DesktopSDL2(const DesktopSDL2 &) = delete;
71 DesktopSDL2 & operator=(const DesktopSDL2 &) = delete;
77
78 virtual ~DesktopSDL2(void);
80
86 RGBImage readPNGImageFile(const std::string & filepath);
88
89 void writePNGImageFile(const RGBImage & img,
90 const std::string & filepath);
92
98 PalettizedWindow * createWindow(const std::string & title,
99 unsigned dimw, unsigned dimh,
100 unsigned scalex, unsigned scaley);
102
103 void getDesktopResolution(unsigned &w, unsigned &h);
105
112 void playSound(double secs, const SoundMonoProducer & waveprod,
113 bool async);
115
136 void pausePlaying(void);
138
142 void resumePlaying(void);
144
146 bool playingPaused(void) { return(soundpaused_); }
148
149 bool soundPlaying(void) const noexcept { return(playing_); }
151
152 void nextSoundPlayingTime(double &t) noexcept;
154
156 const SoundMonoProducer & lastSoundProducer(void) const { return(smprod_); }
158
159 double lastSoundDuration(void) const noexcept { return(sdur_); }
161
170
173
174 bool textEventsEnabled(void) const { return(txtevsen_); }
175
182 std::string copyFromClipboard(void) const;
184
186 void copyToClipboard(const std::string & txt) const;
188
193 private:
194
195 static std::unique_ptr<DesktopSDL2> pgi;
196
197 static void audio_callback(void *userdata, Uint8 *buffer, int length);
198
199 bool withsound_;
200 SDL_AudioSpec soundspecs_;
201 SoundMonoProducer smprod_;
202 double sdur_;
203 uint64_t sind_;
204 volatile std::atomic<bool> playing_;
205 bool soundpaused_;
206 bool txtevsen_;
207
208 DesktopSDL2(const SoundPlayerParms & spps);
209 // Default constructor: initiate the SDL2 video system and the IMG lib.
210
211};
212
213
214/* ****************************************************************************
215
216 Class: PalWinSDL2
217
218*******************************************************************************/
219
222{
223 friend class DesktopSDL2;
224
225 public:
226
227 /* -------------------- Constructors ------------------------------ */
228
229 virtual ~PalWinSDL2(void);
231
232 PalWinSDL2(const PalWinSDL2 &) = delete;
233 PalWinSDL2 & operator=(const PalWinSDL2 &) = delete;
234 PalWinSDL2(PalWinSDL2 &&) = delete;
236
237 /* -------------------- Methods ------------------------------ */
238
239 Cursor getCursor(unsigned x = 0, unsigned y = 0) noexcept;
241
242 CursorCheck getCursorCheck(unsigned x = 0, unsigned y = 0);
244
245 PaletteIndex getPixel(const Cursor & cursor);
247
249 void setPixel(const Cursor & cursor, PaletteIndex b);
251
253 void fillRect(const Rectangle &r, PaletteIndex c);
255
257 void refresh(const Rectangle & rect = Rectangle{});
259
268 void serveEvents(const PalettizedWindow::Event::IDSet & whichevents ={true},
269 ListOfEvents * evs = nullptr);
271
275 void setPaletteColors(const PaletteLevel * colors,
276 PaletteIndex first,
277 PaletteIndex count);
279
283 bool getPointingInWindow(unsigned &x, unsigned &y,
284 bool & leftbutton, bool & rightbutton);
286
298
305
310 private:
311
312 static std::thread::id mainthreadid_;
313
314 SDL_Window *window_;
315 SDL_Renderer *renderer_;
316 SDL_Surface *surfpal_;
317 SDL_Surface *surfaux_;
318 SDL_Texture *texture_;
319 SDL_Color colorpal_[NUMCOLSPAL], auxcolor;
320
321 // constructor is private for forcing user to create windows through
322 // the DesktopSDL2 class
323 PalWinSDL2(const std::string & title, unsigned dimw, unsigned dimh,
324 unsigned scalex, unsigned scaley);
325
326 void destroy(void) noexcept;
327};
328
329
330
331#endif
332 // DesktopSDL2
334
A 2D matrix of certain type that has contiguous storage.
Definition: ColorImages.h:434
std::function< double(double t, uint64_t i, unsigned l, unsigned pos)> SoundMonoProducer
A routine to produce a mono-channel sound wave.
uint8_t PaletteLevel
Intensity of a color component (r, g, b).
static constexpr unsigned NUMCOLSPAL
No. of colors in palette.
uint8_t PaletteIndex
Index of a color in the palette.
KeyID
IDs for the most common keys in a keyboard.
Interface with the functionality that a desktop must provide.
A map of the keyboard, as seen by the desktop.
A palettized window on the desktop.
To refer to pixels in the drawable area of the window.
Cursor to refer to pixels that includes checking code.
Main parameters of the sound player.
A rectangle on a palettized window.
void pausePlaying(void)
Pause the current playing, if any, and play just silence.
CursorCheck getCursorCheck(unsigned x=0, unsigned y=0)
Get a checking cursor that points to that pixel.
void serveEvents(const PalettizedWindow::Event::IDSet &whichevents={true}, ListOfEvents *evs=nullptr)
Serve pending events.
std::string copyFromClipboard(void) const
Return a copy of the text existing in the clipboard, if any.
void disableTextEvents(void)
Disable K_TEXT events from now on to the active window in the desktop.
DesktopSDL2 & operator=(DesktopSDL2 &&)=delete
Assignment-moves forbidden.
static DesktopSDL2 & theDesktop(const SoundPlayerParms &spps)
Get the singleton desktop interface with SPPS (created in the 1st call)
void fillRect(const Rectangle &r, PaletteIndex c)
Fill a rectangle in the window with the given color, efficiently.
void writePNGImageFile(const RGBImage &img, const std::string &filepath)
Write an RGBImage into a PNG image in the given file.
void copyToClipboard(const std::string &txt) const
Copy TXT, if not empty, to the clipboard.
void refresh(const Rectangle &rect=Rectangle{})
Render the internal displayable data onto the desktop window.
PalWinSDL2 & operator=(const PalWinSDL2 &)=delete
Assignments forb.
PalWinSDL2(const PalWinSDL2 &)=delete
Copies forbidden.
PalWinSDL2(PalWinSDL2 &&)=delete
Moves forbidden.
void setPaletteColors(const PaletteLevel *colors, PaletteIndex first, PaletteIndex count)
Change color(s) in the palette of the window.
bool soundPlaying(void) const noexcept
Return TRUE if the last commanded sound is still playing.
Definition: DesktopSDL2.h:149
bool textEventsEnabled(void) const
Enable K_TEXT events from now on to the active window in the desktop.
Definition: DesktopSDL2.h:174
void getDesktopResolution(unsigned &w, unsigned &h)
Fill W and H with the current desktop resolution in pixels.
RGBImage readPNGImageFile(const std::string &filepath)
Read a PNG image from file and return it as an RGBImage.
PaletteIndex getPixel(const Cursor &cursor)
Return the value of the pixel at CURSOR in the window, if all is valid.
virtual ~PalWinSDL2(void)
Destructor: close the window.
virtual ~DesktopSDL2(void)
Destructor: finish both libraries.
DesktopSDL2(const DesktopSDL2 &)=delete
Copies forbidden.
void resumePlaying(void)
Resume a previously paused playing.
bool getPointingInWindow(unsigned &x, unsigned &y, bool &leftbutton, bool &rightbutton)
Get the status of the mouse within the entire window.
void enableTextEvents(void)
Enable K_TEXT events from now on to the active window in the desktop.
double lastSoundDuration(void) const noexcept
Get the current sound duration.
Definition: DesktopSDL2.h:159
void playSound(double secs, const SoundMonoProducer &waveprod, bool async)
Start playing the sound produced by WAVEPROD for the next SECS seconds.
bool playingPaused(void)
Return whether there is a paused playing or not.
Definition: DesktopSDL2.h:146
PalWinSDL2 & operator=(PalWinSDL2 &&)=delete
Assignment-moves forb.
const SoundMonoProducer & lastSoundProducer(void) const
Get the current sound producer or raise an exception if none.
Definition: DesktopSDL2.h:156
bool keyPressed(DesktopInterface::KeyID k)
Check whether the key K is pressed now when the focus is on the window.
void setPixel(const Cursor &cursor, PaletteIndex b)
Set pixel at CURSOR to B, if all is valid.
void updateKeypressMap(DesktopInterface::KeypressMap &map)
Update MAP with key press information when the focus is on the window.
DesktopSDL2 & operator=(const DesktopSDL2 &)=delete
Assignments forbidden.
PalettizedWindow * createWindow(const std::string &title, unsigned dimw, unsigned dimh, unsigned scalex, unsigned scaley)
Factory method: return a new palettized window of type PalWinSDL2.
DesktopSDL2(DesktopSDL2 &&)=delete
Moves forbidden.
void nextSoundPlayingTime(double &t) noexcept
Update T with the next time where the sound callback will be called.
Cursor getCursor(unsigned x=0, unsigned y=0) noexcept
Get a non-checking cursor that points to that pixel.
The desktop interface main class, based on SDL2 and SDL2-image.
Definition: DesktopSDL2.h:54
A palettized window managed by SDL2.
Definition: DesktopSDL2.h:222