X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2F3dengfx%2Fsrc%2Ffxwt%2Ffxwt_sdl.cpp;fp=src%2F3dengfx%2Fsrc%2Ffxwt%2Ffxwt_sdl.cpp;h=e0cd95e7fc46e0d03b3a15ca2ccce04cb2b8f48d;hb=6e23259dbabaeb1711a2a5ca25b9cb421f693759;hp=0000000000000000000000000000000000000000;hpb=fe068fa879814784c45e0cb2e65dac489e8f5594;p=summerhack diff --git a/src/3dengfx/src/fxwt/fxwt_sdl.cpp b/src/3dengfx/src/fxwt/fxwt_sdl.cpp new file mode 100644 index 0000000..e0cd95e --- /dev/null +++ b/src/3dengfx/src/fxwt/fxwt_sdl.cpp @@ -0,0 +1,146 @@ +/* +This file is part of fxwt, the window system toolkit of 3dengfx. + +Copyright (C) 2004, 2005 John Tsiombikas + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* main fxwt event handling and window system abstraction through SDL. + * + * Author: John Tsiombikas 2004 + */ + +#include "3dengfx_config.h" + +#if GFX_LIBRARY == SDL + +#include "gfx_library.h" +#include "fxwt.hpp" +#include "3dengfx/3denginefx.hpp" +#include "common/err_msg.h" + +#ifdef __unix__ +#include +#ifdef _POSIX_PRIORITY_SCHEDULING +#include +#endif // _POSIX_PRIORITY_SCHEDULING +#endif // __unix__ + +#ifdef WIN32 +#include +#endif // WIN32 + +using std::list; +using namespace fxwt; + +static void handle_event(const SDL_Event &event); + +Vector2 fxwt::get_mouse_pos_normalized() { + int x, y; + SDL_GetMouseState(&x, &y); + + return Vector2((scalar_t)x / (scalar_t)screenx, (scalar_t)y / (scalar_t)screeny); +} + +void fxwt::set_window_title(const char *title) { + SDL_WM_SetCaption(title, 0); +} + +void fxwt::swap_buffers() { + SDL_GL_SwapBuffers(); +#if defined(_POSIX_PRIORITY_SCHEDULING) + sched_yield(); +#elif defined(WIN32) + Sleep(0); +#endif +} + +int fxwt::main_loop() { + set_verbosity(3); + + SDL_EnableKeyRepeat(300, 20); + + while(1) { + SDL_Event event; + + if(!idle_handlers.empty()) { + while(SDL_PollEvent(&event)) { + handle_event(event); + } + + list::iterator iter = idle_handlers.begin(); + while(iter != idle_handlers.end()) { + (*iter++)(); + } + } else { + SDL_WaitEvent(&event); + handle_event(event); + } + } + + return 0; +} + +static void handle_event(const SDL_Event &event) { + switch(event.type) { + case SDL_KEYDOWN: + { + list::iterator iter = keyb_handlers.begin(); + while(iter != keyb_handlers.end()) { + (*iter++)(event.key.keysym.sym); + } + } + break; + + case SDL_VIDEOEXPOSE: + { + list::iterator iter = disp_handlers.begin(); + while(iter != disp_handlers.end()) { + (*iter++)(); + } + } + break; + + case SDL_MOUSEMOTION: + { + list::iterator iter = motion_handlers.begin(); + while(iter != motion_handlers.end()) { + (*iter++)(event.motion.x, event.motion.y); + } + } + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + { + button_state[event.button.button] = event.button.state; + list::iterator iter = button_handlers.begin(); + while(iter != button_handlers.end()) { + (*iter++)(event.button.button, event.button.state == SDL_PRESSED, event.button.x, event.button.y); + } + } + break; + + case SDL_QUIT: + exit(0); + + default: + break; + } +} + + +#endif // GFX_LIBRARY == SDL