[Plib-cvs] plib/examples/src/pui/sdl_example Demo.cxx,NONE,1.1 Demo.h,NONE,1.1 EventLoopSDL.cxx,NONE
Brought to you by:
sjbaker
From: Steve B. <sj...@us...> - 2004-02-24 02:34:24
|
Update of /cvsroot/plib/plib/examples/src/pui/sdl_example In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23399/plib/examples/src/pui/sdl_example Added Files: Demo.cxx Demo.h EventLoopSDL.cxx EventLoopSDL.h Makefile mainSDL.cxx Log Message: Added an example program for using PLIB in SDL. --- NEW FILE: Demo.cxx --- #include <stdio.h> #include <GL/gl.h> #include <plib/pu.h> #include "Demo.h" static puInput* input = NULL; static void on_quit(puObject*) { exit(0); } static void on_say_hello(puObject*) { printf("Hello World!\n"); } static void on_print_text(puObject*) { printf("Input text: %s\n", input->getStringValue()); } void initPUI() { puInit(); puSetDefaultStyle(PUSTYLE_SMALL_BEVELLED); puSetDefaultColourScheme(0.75f, 0.75f, 0.75f, 1.0f); } void createInterface() { // menu bar puMenuBar* menu = new puMenuBar(); char* menu_text[] = { "Quit", "----", "Say Hello", "Print text", NULL }; puCallback menu_cb[] = { on_quit, NULL, on_say_hello, on_print_text, NULL }; menu->add_submenu("Demo", menu_text, menu_cb); menu->close(); // input and buttons puButton* button; button = new puOneShot(100, 200, 220, 225); button->setLegend("Quit"); button->setCallback(on_quit); button = new puOneShot(100, 250, 220, 275); button->setLegend("Say Hello"); button->setCallback(on_say_hello); button = new puOneShot(100, 300, 220, 325); button->setLegend("Print text"); button->setCallback(on_print_text); input = new puInput(250, 300, 450, 325); } --- NEW FILE: Demo.h --- #ifndef DEMO_H #define DEMO_H void initPUI(); void createInterface(); #endif --- NEW FILE: EventLoopSDL.cxx --- #include <GL/gl.h> #include <SDL/SDL_keysym.h> #include <plib/pu.h> #include "EventLoopSDL.h" static bool stop_requested; void EventLoopSDL::run() { SDL_Event event; stop_requested = false; while (!stop_requested) { // process events [...141 lines suppressed...] int EventLoopSDL::onMouseDown(int btn, int x, int y) { return puMouse(translateMouse(btn), PU_DOWN, x, y); } int EventLoopSDL::onMouseUp(int btn, int x, int y) { return puMouse(translateMouse(btn), PU_UP, x, y); } int EventLoopSDL::onKeyDown(const SDL_keysym& keysym) { return puKeyboard(translateKey(keysym), PU_DOWN); } int EventLoopSDL::onKeyUp(const SDL_keysym& keysym) { return puKeyboard(translateKey(keysym), PU_UP); } --- NEW FILE: EventLoopSDL.h --- #ifndef EVENTLOOPSDL_H #define EVENTLOOPSDL_H class EventLoopSDL { public: // starts the main loop static void run(); // requests main loop termination static void stop(); // draw one frame static void draw(); // translates an SDL keysym to the PUI equivalent static int translateKey(const SDL_keysym& keysym); // translates an SDL mouse button to the PUI equivalent static int translateMouse(int btn); // event handlers static int onMouseMove(char state, int x, int y); static int onMouseDown(int btn, int x, int y); static int onMouseUp(int btn, int x, int y); static int onKeyDown(const SDL_keysym& keysym); static int onKeyUp(const SDL_keysym& keysym); }; #endif --- NEW FILE: Makefile --- SDL_LDFLAGS := $(shell sdl-config --libs) OGL_LDFLAGS := -lGL -lGLU PUI_LDFLAGS := -lplibpu -lplibfnt -lplibsg -lplibul LDFLAGS := $(OGL_LDFLAGS) $(SDL_LDFLAGS) $(PUI_LDFLAGS) -lm SDL_CFLAGS := $(shell sdl-config --cflags) PUI_CFLAGS := -DPU_USE_SDL CXXFLAGS := -Wall -g $(SDL_CFLAGS) $(PUI_CFLAGS) CXX := g++ OBJECT := mainSDL.o EventLoopSDL.o Demo.o %.o : %.cxx $(CXX) $(CXXFLAGS) -c $< sdl_example: $(OBJECT) $(CXX) -o $@ $+ $(LDFLAGS) .PHONY: clean depend clean: rm -f *.o sdl_example depend: makedepend -Y *.cxx >/dev/null 2>&1 # Do 'make depend' to regenerate dependencies. # DO NOT DELETE Demo.o: Demo.h EventLoopSDL.o: EventLoopSDL.h mainSDL.o: Demo.h EventLoopSDL.h --- NEW FILE: mainSDL.cxx --- #include <stdlib.h> #include <plib/pu.h> #include "SDL.h" #include "Demo.h" #include "EventLoopSDL.h" // default window size static const int WIDTH = 640; static const int HEIGHT = 480; static void initSDL(int w, int h, const char *title) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_ANYFORMAT/* | SDL_FULLSCREEN*/) == NULL) { fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError()); exit(1); } if (title) SDL_WM_SetCaption(title, NULL); SDL_EnableKeyRepeat(150, 75); SDL_EnableUNICODE(true); } void initOGL(int w, int h) { glViewport(0, 0, w, h); glClearColor(0.1f, 0.4f, 0.1f, 1.0f); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glAlphaFunc(GL_GREATER, 0.1f); } int main(int argc, char *argv[]) { initSDL(WIDTH, HEIGHT, "PUI with SDL sample"); initOGL(WIDTH, HEIGHT); initPUI(); createInterface(); EventLoopSDL::run(); return 0; } |