Accueil > > > UTILISER WXWIDGETS ET LA SDL EN MÊME TEMPS
UTILISER WXWIDGETS ET LA SDL EN MÊME TEMPS
Information sur la source
Description
Ce code montre comment utiliser la SDL dans une fênetre WxWidgets
Source
- #include <iostream>
-
- #include <wx/wxprec.h>
-
- #ifdef __BORLANDC__
- #pragma hdrstop
- #endif
-
- #ifndef WX_PRECOMP
- #include <wx/wx.h>
- #endif
-
-
- #include <wx/dcbuffer.h>
- #include <wx/image.h>
-
- #include <SDL/SDL.h>
-
- enum {
- ID_FRAME = 10000,
- ID_PANEL,
- IDM_FILE_EXIT,
- IDM_HELP_ABOUT
- };
-
- //le panel qui contiendra le contexte SDL
- class SDLPanel : public wxPanel {
- DECLARE_CLASS(SDLPanel)
- DECLARE_EVENT_TABLE()
-
- private:
- SDL_Surface *screen;
-
- void onPaint(wxPaintEvent &event);
- void onEraseBackground(wxEraseEvent &event){}
- void onIdle(wxIdleEvent &event);
- void createScreen();
-
- public:
- SDLPanel(wxWindow *parent);
- ~SDLPanel();
- };
-
- //la frame
- class SDLFrame : public wxFrame {
- DECLARE_CLASS(SDLFrame)
- DECLARE_EVENT_TABLE()
-
- private:
- SDLPanel *panel;
-
- void onFileExit(wxCommandEvent &event){ Close(); }
- void onHelpAbout(wxCommandEvent &event);
-
- public:
- SDLFrame();
- SDLPanel &getPanel(){ return *panel; }
- };
-
- //L'application
- class SDLApp : public wxApp {
- DECLARE_CLASS(SDLApp)
-
- private:
- SDLFrame *frame;
-
- public:
- bool OnInit();
- int OnRun();
- int OnExit();
- };
-
- IMPLEMENT_APP(SDLApp)
-
- IMPLEMENT_CLASS(SDLApp, wxApp)
- //===========================================
- IMPLEMENT_CLASS(SDLFrame, wxFrame)
-
- BEGIN_EVENT_TABLE(SDLFrame, wxFrame)
- EVT_MENU(IDM_FILE_EXIT, SDLFrame::onFileExit)
- EVT_MENU(IDM_HELP_ABOUT, SDLFrame::onHelpAbout)
- END_EVENT_TABLE()
- //===========================================
- IMPLEMENT_CLASS(SDLPanel, wxPanel)
-
- BEGIN_EVENT_TABLE(SDLPanel, wxPanel)
- EVT_PAINT(SDLPanel::onPaint)
- EVT_ERASE_BACKGROUND(SDLPanel::onEraseBackground)
- EVT_IDLE(SDLPanel::onIdle)
- END_EVENT_TABLE()
- //===========================================
- bool SDLApp::OnInit() {
- // crée la SDLFrame
- frame = new SDLFrame;
- frame->SetClientSize(640, 480);
- frame->Centre();
- frame->Show();
-
- // Notre frame au premier plan
- SetTopWindow(frame);
-
- //l'initialisation devrais toujours marché.
- return true;
- }
-
- int SDLApp::OnRun() {
- //initialise SDL
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- std::cerr << "Impossible d'initialiser SDL: " << SDL_GetError() << '\n';
-
- return -1;
- }
-
- // Met ne place le mode vidéo sans crée de fenetre
- SDL_SetVideoMode(0, 0, 0, SDL_SWSURFACE);
-
- // démare la boucle principale
- return wxApp::OnRun();
- }
-
- int SDLApp::OnExit() {
- // quitte la SDL
- SDL_Quit();
-
- //retourne le code de sortie standard
- return wxApp::OnExit();
- }
-
- SDLFrame::SDLFrame() {
- // Crée la frame SDL
- Create(NULL, ID_FRAME, wxT("Frame Title"), wxDefaultPosition,
- wxDefaultSize, wxCAPTION | wxSYSTEM_MENU |
- wxMINIMIZE_BOX | wxCLOSE_BOX);
-
- // crée la bare de mnu
- wxMenuBar *mb = new wxMenuBar;
-
- // crée le menu file
- wxMenu *fileMenu = new wxMenu;
- fileMenu->Append(IDM_FILE_EXIT, wxT("E&xit"));
-
- // add the file menu to the menu bar
- mb->Append(fileMenu, wxT("&File"));
-
- // crée le menu aide
- wxMenu *helpMenu = new wxMenu;
- helpMenu->Append(IDM_HELP_ABOUT, wxT("About"));
-
- // ajoute le menu help a la bare de menus
- mb->Append(helpMenu, wxT("&Help"));
-
- //ajoute la bare de menu la frame
- SetMenuBar(mb);
-
- //crée le panel SDL.
- panel = new SDLPanel(this);
- }
-
- void SDLFrame::onHelpAbout(wxCommandEvent &) {
- wxMessageBox(wxT("wx-sdl snippet"),
- wxT("A propos du snippet WxSDL"), wxOK | wxICON_INFORMATION);
- }
-
- SDLPanel::SDLPanel(wxWindow *parent) : wxPanel(parent, ID_PANEL), screen(NULL) {
- //definit la taille du panel
- wxSize size(640, 480);
-
- SetMinSize(size);
- SetMaxSize(size);
- SetSize(size);
-
- //génère une évenement IDLE pour "démarer" la fenêtre.
- wxIdleEvent event;
- event.SetEventObject(this);
- this->AddPendingEvent(event);
- }
-
- SDLPanel::~SDLPanel() {
- if (screen != NULL) {
- SDL_FreeSurface(screen);
- }
- }
-
- void SDLPanel::onPaint(wxPaintEvent &) {
- // on ne peut dessiner si l'écran n'existe pas
- if (screen == NULL) {
- return;
- }
-
- //vérouille l'écran si besoin est.
- if (SDL_MUSTLOCK(screen)) {
- if (SDL_LockSurface(screen) < 0) {
- return;
- }
- }
-
- // ccrée un bitmap a partir de l'écran
- wxBitmap bmp(wxImage(screen->w, screen->h,
- static_cast<unsigned char *>(screen->pixels), true));
-
- //devérouille l'écran
- if (SDL_MUSTLOCK(screen)) {
- SDL_UnlockSurface(screen);
- }
-
- //paint l'écran
- wxBufferedPaintDC dc(this, bmp);
- }
-
- void SDLPanel::onIdle(wxIdleEvent &) {
- // crée screen
- createScreen();
-
- //vérouille si besoin est
- if (SDL_MUSTLOCK(screen)) {
- if (SDL_LockSurface(screen) < 0) {
- return;
- }
- }
-
- //ne cherchez pas a comprendre , contentez vous de regarder.
- int tick = SDL_GetTicks();
-
- for (int y = 0; y < 480; y++) {
- for (int x = 0; x < 640; x++) {
- wxUint32 color = (y * y) + (x * x) + tick;
- wxUint8 *pixels = static_cast<wxUint8 *>(screen->pixels) +
- (y * screen->pitch) +
- (x * screen->format->BytesPerPixel);
-
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- pixels[0] = color & 0xFF;
- pixels[1] = (color >> 8) & 0xFF;
- pixels[2] = (color >> 16) & 0xFF;
- #else
- pixels[0] = (color >> 16) & 0xFF;
- pixels[1] = (color >> 8) & 0xFF;
- pixels[2] = color & 0xFF;
- #endif
- }
- }
-
- //dévérouille si besoin est
- if (SDL_MUSTLOCK(screen)) {
- SDL_UnlockSurface(screen);
- }
-
- //rafraichie le panel
- Refresh(false);
-
- //endort l'application
- wxMilliSleep(33);
- }
-
- void SDLPanel::createScreen() {
- if (screen == NULL) {
- int width, height;
- GetSize(&width, &height);
-
- screen = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
- 24, 0, 0, 0, 0);
- }
- }
#include <iostream>
#include <wx/wxprec.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/dcbuffer.h>
#include <wx/image.h>
#include <SDL/SDL.h>
enum {
ID_FRAME = 10000,
ID_PANEL,
IDM_FILE_EXIT,
IDM_HELP_ABOUT
};
//le panel qui contiendra le contexte SDL
class SDLPanel : public wxPanel {
DECLARE_CLASS(SDLPanel)
DECLARE_EVENT_TABLE()
private:
SDL_Surface *screen;
void onPaint(wxPaintEvent &event);
void onEraseBackground(wxEraseEvent &event){}
void onIdle(wxIdleEvent &event);
void createScreen();
public:
SDLPanel(wxWindow *parent);
~SDLPanel();
};
//la frame
class SDLFrame : public wxFrame {
DECLARE_CLASS(SDLFrame)
DECLARE_EVENT_TABLE()
private:
SDLPanel *panel;
void onFileExit(wxCommandEvent &event){ Close(); }
void onHelpAbout(wxCommandEvent &event);
public:
SDLFrame();
SDLPanel &getPanel(){ return *panel; }
};
//L'application
class SDLApp : public wxApp {
DECLARE_CLASS(SDLApp)
private:
SDLFrame *frame;
public:
bool OnInit();
int OnRun();
int OnExit();
};
IMPLEMENT_APP(SDLApp)
IMPLEMENT_CLASS(SDLApp, wxApp)
//===========================================
IMPLEMENT_CLASS(SDLFrame, wxFrame)
BEGIN_EVENT_TABLE(SDLFrame, wxFrame)
EVT_MENU(IDM_FILE_EXIT, SDLFrame::onFileExit)
EVT_MENU(IDM_HELP_ABOUT, SDLFrame::onHelpAbout)
END_EVENT_TABLE()
//===========================================
IMPLEMENT_CLASS(SDLPanel, wxPanel)
BEGIN_EVENT_TABLE(SDLPanel, wxPanel)
EVT_PAINT(SDLPanel::onPaint)
EVT_ERASE_BACKGROUND(SDLPanel::onEraseBackground)
EVT_IDLE(SDLPanel::onIdle)
END_EVENT_TABLE()
//===========================================
bool SDLApp::OnInit() {
// crée la SDLFrame
frame = new SDLFrame;
frame->SetClientSize(640, 480);
frame->Centre();
frame->Show();
// Notre frame au premier plan
SetTopWindow(frame);
//l'initialisation devrais toujours marché.
return true;
}
int SDLApp::OnRun() {
//initialise SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "Impossible d'initialiser SDL: " << SDL_GetError() << '\n';
return -1;
}
// Met ne place le mode vidéo sans crée de fenetre
SDL_SetVideoMode(0, 0, 0, SDL_SWSURFACE);
// démare la boucle principale
return wxApp::OnRun();
}
int SDLApp::OnExit() {
// quitte la SDL
SDL_Quit();
//retourne le code de sortie standard
return wxApp::OnExit();
}
SDLFrame::SDLFrame() {
// Crée la frame SDL
Create(NULL, ID_FRAME, wxT("Frame Title"), wxDefaultPosition,
wxDefaultSize, wxCAPTION | wxSYSTEM_MENU |
wxMINIMIZE_BOX | wxCLOSE_BOX);
// crée la bare de mnu
wxMenuBar *mb = new wxMenuBar;
// crée le menu file
wxMenu *fileMenu = new wxMenu;
fileMenu->Append(IDM_FILE_EXIT, wxT("E&xit"));
// add the file menu to the menu bar
mb->Append(fileMenu, wxT("&File"));
// crée le menu aide
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(IDM_HELP_ABOUT, wxT("About"));
// ajoute le menu help a la bare de menus
mb->Append(helpMenu, wxT("&Help"));
//ajoute la bare de menu la frame
SetMenuBar(mb);
//crée le panel SDL.
panel = new SDLPanel(this);
}
void SDLFrame::onHelpAbout(wxCommandEvent &) {
wxMessageBox(wxT("wx-sdl snippet"),
wxT("A propos du snippet WxSDL"), wxOK | wxICON_INFORMATION);
}
SDLPanel::SDLPanel(wxWindow *parent) : wxPanel(parent, ID_PANEL), screen(NULL) {
//definit la taille du panel
wxSize size(640, 480);
SetMinSize(size);
SetMaxSize(size);
SetSize(size);
//génère une évenement IDLE pour "démarer" la fenêtre.
wxIdleEvent event;
event.SetEventObject(this);
this->AddPendingEvent(event);
}
SDLPanel::~SDLPanel() {
if (screen != NULL) {
SDL_FreeSurface(screen);
}
}
void SDLPanel::onPaint(wxPaintEvent &) {
// on ne peut dessiner si l'écran n'existe pas
if (screen == NULL) {
return;
}
//vérouille l'écran si besoin est.
if (SDL_MUSTLOCK(screen)) {
if (SDL_LockSurface(screen) < 0) {
return;
}
}
// ccrée un bitmap a partir de l'écran
wxBitmap bmp(wxImage(screen->w, screen->h,
static_cast<unsigned char *>(screen->pixels), true));
//devérouille l'écran
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
//paint l'écran
wxBufferedPaintDC dc(this, bmp);
}
void SDLPanel::onIdle(wxIdleEvent &) {
// crée screen
createScreen();
//vérouille si besoin est
if (SDL_MUSTLOCK(screen)) {
if (SDL_LockSurface(screen) < 0) {
return;
}
}
//ne cherchez pas a comprendre , contentez vous de regarder.
int tick = SDL_GetTicks();
for (int y = 0; y < 480; y++) {
for (int x = 0; x < 640; x++) {
wxUint32 color = (y * y) + (x * x) + tick;
wxUint8 *pixels = static_cast<wxUint8 *>(screen->pixels) +
(y * screen->pitch) +
(x * screen->format->BytesPerPixel);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
pixels[0] = color & 0xFF;
pixels[1] = (color >> 8) & 0xFF;
pixels[2] = (color >> 16) & 0xFF;
#else
pixels[0] = (color >> 16) & 0xFF;
pixels[1] = (color >> 8) & 0xFF;
pixels[2] = color & 0xFF;
#endif
}
}
//dévérouille si besoin est
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
//rafraichie le panel
Refresh(false);
//endort l'application
wxMilliSleep(33);
}
void SDLPanel::createScreen() {
if (screen == NULL) {
int width, height;
GetSize(&width, &height);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
24, 0, 0, 0, 0);
}
}
Conclusion
Ce code a été testé sous WinXP et sous Linux(distribution Fedora Core 5). Compilé avec WxWidgets v 2.6.3 et SDL version 1.2.9
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
[SDL/WxWidgets] Events [ par djardjar ]
Bonjour ! Comment on gere les évenements de la SDL dans une fenêtre WxWidgets ?En fait, je voudrais placer ceci dans une fenêtre WxWid
[SDL] Obtenir la couleur d'un pixel dans une SDL_Surface [ par Rouliann ]
Bonjour tout le monde!Voilà je cherche une fonction capable de lire la couleur d'un pixel dans une SDL_Surface, j'ai cherché dans SDL_video.
SDL en statique [ par shenron666 ]
Bonjour, je ne suis pas particulièrement fan des librairies "externes" (.dll) mais SDL étant une bonne librairie en open source j'aimerai sa
Devpack wxWidgets 2.5.5 [ par fredcl ]
Bonjour à tous, Pour ceux que celà interresse, vous trouverez sur mon site http://cfred.free.fr les DevPack de wxWidgets 2.5.5 pour Dev-Cp
SDL : linker error [ par bigben89 ]
Ca fais à peine quelques mois que j'fais de la programmation (en C) et j'aimerais faire un peu de 2d et de 3d avec SDL. Je regarde des tutoriaux
SDL _ttf [ par RLBDC ]
Bonjour à tous !Voilà , je débute en c++ .Je souhaite créer un "menu" de jeu , en mode grafique , à l'aide de la SDL .Donc j'
WxWidgets : wxSplitterWindow et Sizers [ par nacedo23 ]
Salut! Je développe avec wxDevcpp.Voila mon probleme : Je veux couper ma fenetre en 2 avec un splitterwindow pour pouvoir redimensionner. A gauc
Devpack wxWidgets 2.6.0 pour Dev-Cpp 4.9.9.2 [ par fredcl ]
Bonjour à tous, Pour ceux que celà interresse, WxWidgets vient de sortir (21/04/2005) en version stable 2.6.0. J'ai donc mis à votr
Lecture de vidéos avec SDL [ par licorna ]
Bonjour à tous, voilà plusieurs semaines que je tente de décoder des vidéos afin de les afficher à l
mysql devcpp 4.9.9.2 wxwidgets [ par nomad56 ]
nomad56 ,salut je dois faire une gestion en c++ avec mysql devcpp 4.9.9.2 wxwidgets je suis tout juste débutant en c++ (en fait je connais un peu
|
Derniers Blogs
TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVéTECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVé par ROMELARD Fabrice
Speaker : Guillaume Rochette Cette session est dédiée à fournir le retour sur la mise en place d'un cloud privé (IaaS) par Osiatis pour son compte ou celui de ses clients. Ce projet s'est déroulé sur 4 mois et a permis de faire évoluer...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYSTECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYS par ROMELARD Fabrice
Speakers : Lionel Limozin et Alain Marty La session commence par une découverte de SharePoint à travers la mise en place d'un environnement SharePoint pour la gestion des Sessions animées par BeWise. Le besoin est très ba...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
AUMLAUML par sassion
Cliquez pour lire la suite par sassion
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|