SDLPanel.d

   1 //-----------------------------------------------------------------------------
   2 // wxD/Samples - SDL.d
   3 //
   4 // A wxD version of the wx/SDL tutorial.
   5 // http://code.technoplaza.net/wx-sdl/
   6 //
   7 // wx-sdl tutorial v1.0
   8 // Copyright (C) 2005 John David Ratliff
   9 //
  10 // wx-sdl is free software; you can redistribute it and/or modify
  11 // it under the terms of the GNU General Public License as published by
  12 // the Free Software Foundation; either version 2 of the License, or
  13 // (at your option) any later version.
  14 //
  15 // wx-sdl is distributed in the hope that it will be useful,
  16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 // GNU General Public License for more details.
  19 //
  20 // You should have received a copy of the GNU General Public License
  21 // along with wx-sdl; if not, write to the Free Software Foundation, Inc.,
  22 // 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  23 //
  24 // $Id: SDLPanel.d,v 1.2 2007/04/18 19:27:17 afb Exp $
  25 //-----------------------------------------------------------------------------
  26 
  27 private import std.stdio;
  28 private import std.string;
  29 
  30 import wx.wx;
  31 
  32 import wx.MemoryDC;
  33 import wx.Image;
  34 
  35 import sdl.sdl;
  36 
  37 /*******************************************************************************
  38 // Global Declarations
  39 *******************************************************************************/
  40 
  41 enum {
  42     ID_FRAME = 10000,
  43     ID_PANEL,
  44     IDM_FILE_EXIT,
  45     IDM_HELP_ABOUT = MenuIDs.wxID_ABOUT
  46 }
  47 
  48 /*******************************************************************************
  49 // SDLPanel Class
  50 *******************************************************************************/
  51 
  52 public class SDLPanel : wxPanel
  53 {
  54 private:
  55     SDL_Surface *screen;
  56 
  57     /**
  58      * Called to paint the panel.
  59      *
  60      * @param event The triggering wxPaintEvent (unused).
  61      */
  62     void onPaint(Object sender, Event event)
  63     {
  64      // can't draw if the screen doesn't exist yet
  65     if (screen == null) {
  66         return;
  67     }
  68     
  69     // lock the surface if necessary
  70     if (SDL_MUSTLOCK(screen)) {
  71         if (SDL_LockSurface(screen) < 0) {
  72             return;
  73         }
  74     }
  75     
  76     // create a bitmap from our pixel data
  77     wxBitmap bmp = new wxBitmap(new Image(screen.w, screen.h, 
  78                     cast(ubyte *)(screen.pixels), true));
  79     
  80     // unlock the screen
  81     if (SDL_MUSTLOCK(screen)) {
  82         SDL_UnlockSurface(screen);
  83     }
  84     
  85     // paint the screen
  86     auto wxBufferedPaintDC dc = new wxBufferedPaintDC(this, bmp);
  87     }
  88     
  89     /**
  90      * Called to erase the background.
  91      *
  92      * @param event The triggering wxEraseEvent (unused).
  93      */
  94     void onEraseBackground(Object sender, Event event) {}
  95     
  96     /**
  97      * Called to update the panel.
  98      *
  99      * @param event The triggering wxIdleEvent (unused).
 100      */
 101     void onIdle(Object sender, Event event)
 102     {
 103     // create the SDL_Surface
 104     createScreen();
 105     
 106     // Lock surface if needed
 107     if (SDL_MUSTLOCK(screen)) {
 108         if (SDL_LockSurface(screen) < 0) {
 109             return;
 110         }
 111     }
 112     
 113     // Ask SDL for the time in milliseconds
 114     int tick = SDL_GetTicks();
 115     
 116     for (int y = 0; y < 480; y++) {
 117         for (int x = 0; x < 640; x++) {
 118             uint color = (y * y) + (x * x) + tick;
 119             ubyte *pixels = cast(ubyte *)(screen.pixels) + 
 120                               (y * screen.pitch) +
 121                               (x * screen.format.BytesPerPixel);
 122 
 123             version (BigEndian)
 124             {
 125                 pixels[0] = color & 0xFF;
 126                 pixels[1] = (color >> 8) & 0xFF;
 127                 pixels[2] = (color >> 16) & 0xFF;
 128             }
 129             else
 130             {
 131                 pixels[0] = (color >> 16) & 0xFF;
 132                 pixels[1] = (color >> 8) & 0xFF;
 133                 pixels[2] = color & 0xFF;
 134             }
 135         }
 136     }
 137     
 138     // Unlock if needed
 139     if (SDL_MUSTLOCK(screen)) {
 140         SDL_UnlockSurface(screen);
 141     }
 142     
 143     // refresh the panel
 144     Refresh(false);
 145     
 146     // throttle to keep from flooding the event queue
 147     wxMilliSleep(33);
 148     }
 149     
 150     /**
 151      * Creates the SDL_Surface used by this SDLPanel.
 152      */
 153     void createScreen()
 154     {
 155     if (screen == null) {
 156         Size size = size();
 157         
 158         screen = SDL_CreateRGBSurface(SDL_SWSURFACE, size.Width, size.Height, 
 159                                       24, 0, 0, 0, 0);     
 160     }
 161     }
 162    
 163 public:
 164     /**
 165      * Creates a new SDLPanel.
 166      *
 167      * @param parent The wxWindow parent.
 168      */
 169     this(wxWindow parent)
 170     {
 171     super(parent, ID_PANEL);
 172     screen = null;
 173     
 174     // ensure the size of the wxPanel
 175     wxSize size = wxSize(640, 480);
 176     
 177     MinSize = size;
 178     MaxSize = size;
 179 
 180     EVT_PAINT(&onPaint);
 181     EVT_ERASE_BACKGROUND(&onEraseBackground);
 182     EVT_IDLE(&onIdle);
 183     }
 184     
 185     /**
 186      * Destructs this SDLPanel.
 187      */
 188     ~this()
 189     {
 190     if (screen != null) {
 191         SDL_FreeSurface(screen);
 192     }
 193     }
 194 }
 195 
 196 /*******************************************************************************
 197 // SDLFrame Class
 198 *******************************************************************************/
 199 
 200 public class SDLFrame : wxFrame
 201 {
 202 private:
 203     SDLPanel panel;
 204     
 205     /**
 206      * Called when exit from the file menu is selected.
 207      *
 208      * @param event The associated wxCommandEvent (unused).
 209      */
 210     void onFileExit(Object sender, Event event) { Close(); }
 211     
 212     /**
 213      * Called when about from the help menu is selected.
 214      *
 215      * @param event The associated wxCommandEvent (unused).
 216      */
 217     void onHelpAbout(Object sender, Event event)
 218     {
 219     MessageBox("wx-sdl tutorial\nCopyright (C) 2005 John Ratliff",
 220                  "about wx-sdl tutorial", Dialog.wxOK | Dialog.wxICON_INFORMATION);
 221     }
 222     
 223 public:
 224     /**
 225      * Creates a new SDLFrame.
 226      */
 227 //    this(Point pos=wxDefaultPosition, Size size=wxDefaultSize)
 228     this()
 229     {
 230     Point pos=wxDefaultPosition;
 231     Size size=wxDefaultSize;
 232  
 233          // Create the SDLFrame
 234     Create(null, ID_FRAME, "Frame Title", pos,
 235            size, wxCAPTION | wxSYSTEM_MENU | 
 236            wxMINIMIZE_BOX | wxCLOSE_BOX, "");
 237 
 238     // create the main menubar
 239     wxMenuBar mb = new wxMenuBar;
 240     
 241     // create the file menu
 242     wxMenu fileMenu = new wxMenu;
 243     fileMenu.Append(IDM_FILE_EXIT, "E&xit");
 244     
 245     // add the file menu to the menu bar
 246     mb.Append(fileMenu, "&File");
 247     
 248     // create the help menu
 249     wxMenu helpMenu = new wxMenu;
 250     helpMenu.Append(IDM_HELP_ABOUT, "About");
 251     
 252     // add the help menu to the menu bar
 253     mb.Append(helpMenu, "&Help");
 254     
 255     // add the menu bar to the SDLFrame
 256     this.menuBar = mb;
 257     
 258     // create the SDLPanel
 259     panel = new SDLPanel(this);
 260 
 261     EVT_MENU(IDM_FILE_EXIT, &onFileExit);
 262     EVT_MENU(IDM_HELP_ABOUT, &onHelpAbout);
 263     }
 264     
 265     /**
 266      * Gets the SDLPanel within this SDLFrame.
 267      *
 268      * @return The SDLPanel.
 269      */
 270     SDLPanel getPanel() { return panel; }
 271 }
 272 
 273 /*******************************************************************************
 274 // SDLApp Class
 275 *******************************************************************************/
 276 
 277 public class SDLApp : wxApp
 278 {
 279 private:
 280     SDLFrame frame;
 281     
 282 public:
 283     /**
 284      * Called to initialize this SDLApp.
 285      *
 286      * @return true if initialization succeeded; false otherwise.
 287      */
 288     public override bool OnInit()
 289     {
 290      // create the SDLFrame
 291     frame = new SDLFrame;
 292     frame.ClientSize = Size(640, 480);
 293     frame.Centre();
 294     frame.Show();
 295     
 296     // initialization should always succeed
 297     return true;
 298     }
 299    
 300     /**
 301      * Called to run this SDLApp.
 302      *
 303      * @return The status code (0 if good, non-0 if bad).
 304      */
 305     public override int OnRun()
 306     {
 307         // initialize SDL
 308     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
 309         fwritefln(stderr, "unable to init SDL: ", std.string.toString(SDL_GetError()));
 310         
 311         return -1;
 312     }
 313     
 314 version (__WXMAC__) {} else {
 315     // Setup video mode, but don't create a window
 316     SDL_SetVideoMode(0, 0, 0, SDL_SWSURFACE);
 317 }
 318     
 319     // generate an initial idle event to start things
 320     wxIdleEvent event = new wxIdleEvent;
 321     event.EventObject = frame.getPanel();
 322     frame.getPanel().AddPendingEvent(event);
 323  
 324     // start the main loop
 325     return super.OnRun();
 326     }
 327     
 328     /**
 329      * Called when this SDLApp is ready to exit.
 330      *
 331      * @return The exit code.
 332      */
 333     public override int OnExit()
 334     {
 335     // cleanup SDL
 336     SDL_Quit();
 337     
 338     // return the standard exit code
 339     return super.OnExit();
 340     }
 341 
 342     static void Main(char[][] args)
 343     {
 344         SDLApp app = new SDLApp();
 345         app.Run(args);
 346     }
 347 
 348 }
 349 
 350 int main(char[][] args)
 351 {
 352     SDLApp.Main(args);
 353     return 0;
 354 }

SDLPanel Screenshot


Source file: SDLPanel.d (354 lines)