From bc3df897c3f387ffc4693fa668fb5353def5688b Mon Sep 17 00:00:00 2001 From: spartanj Date: Mon, 2 May 2011 01:44:45 -0300 Subject: [PATCH] Added SDL 1.3 backend ( wip, working on linux, i suppose that works on the rest os, but not tested ). --- Makefile | 13 +- src/base.hpp | 2 +- src/test/eetest.cpp | 2 +- src/window/backend/SDL/cwindowsdl.cpp | 12 +- src/window/backend/SDL/cwindowsdl.hpp | 2 + src/window/backend/SDL13/base.hpp | 19 + src/window/backend/SDL13/cbackendsdl.hpp | 28 ++ src/window/backend/SDL13/cclipboardsdl.cpp | 39 ++ src/window/backend/SDL13/cclipboardsdl.hpp | 35 ++ .../backend/SDL13/ccursormanagersdl.cpp | 79 ++++ .../backend/SDL13/ccursormanagersdl.hpp | 42 ++ src/window/backend/SDL13/ccursorsdl.cpp | 74 +++ src/window/backend/SDL13/ccursorsdl.hpp | 36 ++ src/window/backend/SDL13/cinputsdl.cpp | 376 +++++++++++++++ src/window/backend/SDL13/cinputsdl.hpp | 40 ++ .../backend/SDL13/cjoystickmanagersdl.cpp | 56 +++ .../backend/SDL13/cjoystickmanagersdl.hpp | 32 ++ src/window/backend/SDL13/cjoysticksdl.cpp | 87 ++++ src/window/backend/SDL13/cjoysticksdl.hpp | 40 ++ src/window/backend/SDL13/cwindowsdl.cpp | 446 ++++++++++++++++++ src/window/backend/SDL13/cwindowsdl.hpp | 83 ++++ src/window/cengine.cpp | 13 + src/window/cinput.cpp | 3 +- src/window/platform/x11/cx11impl.cpp | 54 ++- 24 files changed, 1580 insertions(+), 33 deletions(-) create mode 100644 src/window/backend/SDL13/base.hpp create mode 100644 src/window/backend/SDL13/cbackendsdl.hpp create mode 100644 src/window/backend/SDL13/cclipboardsdl.cpp create mode 100644 src/window/backend/SDL13/cclipboardsdl.hpp create mode 100644 src/window/backend/SDL13/ccursormanagersdl.cpp create mode 100644 src/window/backend/SDL13/ccursormanagersdl.hpp create mode 100644 src/window/backend/SDL13/ccursorsdl.cpp create mode 100644 src/window/backend/SDL13/ccursorsdl.hpp create mode 100644 src/window/backend/SDL13/cinputsdl.cpp create mode 100644 src/window/backend/SDL13/cinputsdl.hpp create mode 100644 src/window/backend/SDL13/cjoystickmanagersdl.cpp create mode 100644 src/window/backend/SDL13/cjoystickmanagersdl.hpp create mode 100644 src/window/backend/SDL13/cjoysticksdl.cpp create mode 100644 src/window/backend/SDL13/cjoysticksdl.hpp create mode 100644 src/window/backend/SDL13/cwindowsdl.cpp create mode 100644 src/window/backend/SDL13/cwindowsdl.hpp diff --git a/Makefile b/Makefile index b3fc1f7c9..b4222d70a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ STRLOWERCASE = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1)))))))))))))))))))))))))) OS = $(strip $(call STRLOWERCASE, $(shell uname) ) ) +SDLVERSION = $(shell sdl-config --version) export CFLAGS = -Wall -Wno-unknown-pragmas $(FINALFLAGS) $(BUILDFLAGS) $(BACKENDFLAGS) export CFLAGSEXT = $(FINALFLAGS) $(BUILDFLAGS) @@ -65,8 +66,15 @@ ifeq ($(BACKEND_SDL), ) endif ifeq ($(BACKEND_SDL), yes) - SDL_BACKEND_LINK = -lSDL - SDL_BACKEND_SRC = $(wildcard ./src/window/backend/SDL/*.cpp) + + ifeq ($(SDLVERSION), 1.3.0) + SDL_BACKEND_LINK = libs/$(OS)/libSDL.a + SDL_BACKEND_SRC = $(wildcard ./src/window/backend/SDL13/*.cpp) + else + SDL_BACKEND_LINK = -lSDL + SDL_BACKEND_SRC = $(wildcard ./src/window/backend/SDL/*.cpp) + endif + SDL_DEFINE = -DEE_BACKEND_SDL_ACTIVE else SDL_BACKEND_LINK = @@ -243,6 +251,7 @@ dirs: @mkdir -p $(OBJDIR)/src/utils @mkdir -p $(OBJDIR)/src/window @mkdir -p $(OBJDIR)/src/window/backend/SDL + @mkdir -p $(OBJDIR)/src/window/backend/SDL13 @mkdir -p $(OBJDIR)/src/window/backend/null @mkdir -p $(OBJDIR)/src/window/backend/allegro5 @mkdir -p $(OBJDIR)/src/window/platform/x11 diff --git a/src/base.hpp b/src/base.hpp index 1e48b7ea3..bcfff9eb8 100644 --- a/src/base.hpp +++ b/src/base.hpp @@ -143,7 +143,7 @@ /// Activate at least one backend for the compilation #if !defined( EE_BACKEND_SDL_ACTIVE ) && !defined( EE_BACKEND_ALLEGRO_ACTIVE ) -#define EE_BACKEND_SDL_ACTIVE + #define EE_BACKEND_SDL_ACTIVE #endif #define eeARRAY_SIZE(__array) ( sizeof(__array) / sizeof(__array[0]) ) diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index 3407d1dc1..3bf6eff3d 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -838,7 +838,7 @@ void cEETest::LoadTextures() { cCursorManager * CurMan = mWindow->GetCursorManager(); CurMan->Visible( false ); //CurMan->Visible( true ); - //CurMan->Set( Window::Cursor::SYS_CURSOR_DEFAULT ); + //CurMan->Set( Window::Cursor::SYS_CURSOR_LINK ); //CurMan->Set( CurMan->Add( CurMan->Create( CursorP[1], eeVector2i( 2, 2 ), "cursor_special" ) ) ); CL1.AddFrame(TN[2]); diff --git a/src/window/backend/SDL/cwindowsdl.cpp b/src/window/backend/SDL/cwindowsdl.cpp index c33e578db..74f183fc6 100644 --- a/src/window/backend/SDL/cwindowsdl.cpp +++ b/src/window/backend/SDL/cwindowsdl.cpp @@ -113,7 +113,7 @@ bool cWindowSDL::Create( WindowSettings Settings, ContextSettings Context ) { mWindow.Created = true; - LogSuccessfulInit( "SDL" ); + LogSuccessfulInit( GetVersion() ); /// Init the clipboard after the window creation reinterpret_cast ( mClipboard )->Init(); @@ -125,11 +125,19 @@ bool cWindowSDL::Create( WindowSettings Settings, ContextSettings Context ) { return true; } catch (...) { - LogFailureInit( "cWindowSDL", "SDL" ); + LogFailureInit( "cWindowSDL", GetVersion() ); return false; } } +std::string cWindowSDL::GetVersion() { + SDL_version ver; + + SDL_GetVersion( &ver ); + + return StrFormated( "SDL %d.%d.%d", ver.major, ver.minor, ver.patch ); +} + void cWindowSDL::CreatePlatform() { eeSAFE_DELETE( mPlatform ); #if EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX || defined( EE_X11_PLATFORM ) diff --git a/src/window/backend/SDL/cwindowsdl.hpp b/src/window/backend/SDL/cwindowsdl.hpp index a6fa53929..370814c0a 100644 --- a/src/window/backend/SDL/cwindowsdl.hpp +++ b/src/window/backend/SDL/cwindowsdl.hpp @@ -54,6 +54,8 @@ class EE_API cWindowSDL : public cWindow { void SwapBuffers(); void SetGLConfig(); + + std::string GetVersion(); }; }}}} diff --git a/src/window/backend/SDL13/base.hpp b/src/window/backend/SDL13/base.hpp new file mode 100644 index 000000000..0b97e8eab --- /dev/null +++ b/src/window/backend/SDL13/base.hpp @@ -0,0 +1,19 @@ +#ifndef EE_WINDOWBACKEND_BASE_SDL13_HPP +#define EE_WINDOWBACKEND_BASE_SDL13_HPP + +#include "../../base.hpp" + +#ifdef EE_BACKEND_SDL_ACTIVE + +#include + +#if SDL_VERSION_ATLEAST(1,3,0) + #ifndef EE_BACKEND_SDL_1_3 + #define EE_BACKEND_SDL_1_3 + #endif +#endif + +#endif + +#endif + diff --git a/src/window/backend/SDL13/cbackendsdl.hpp b/src/window/backend/SDL13/cbackendsdl.hpp new file mode 100644 index 000000000..3f70f1633 --- /dev/null +++ b/src/window/backend/SDL13/cbackendsdl.hpp @@ -0,0 +1,28 @@ +#ifndef EE_WINDOWCBACKENDSDL13_HPP +#define EE_WINDOWCBACKENDSDL13_HPP + +#include "../../cbackend.hpp" + +#ifdef EE_BACKEND_SDL_ACTIVE + +#include "cwindowsdl.hpp" + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class EE_API cBackendSDL : public cBackend { + public: + inline cBackendSDL() : cBackend() + { + } + + inline ~cBackendSDL() + { + SDL_Quit(); + } +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/cclipboardsdl.cpp b/src/window/backend/SDL13/cclipboardsdl.cpp new file mode 100644 index 000000000..0cfe17633 --- /dev/null +++ b/src/window/backend/SDL13/cclipboardsdl.cpp @@ -0,0 +1,39 @@ +#include "cclipboardsdl.hpp" +#include "cwindowsdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cClipboardSDL::cClipboardSDL( cWindow * window ) : + cClipboard( window ) +{ +} + +cClipboardSDL::~cClipboardSDL() { +} + +void cClipboardSDL::Init() { +} + +void cClipboardSDL::SetText( const std::string& Text ) { + SDL_SetClipboardText( Text.c_str() ); +} + +std::string cClipboardSDL::GetText() { + char * text = SDL_GetClipboardText(); + std::string str( text ); + SDL_free(text); + return str; +} + +String cClipboardSDL::GetWideText() { + char * text = SDL_GetClipboardText(); + String str( String::FromUtf8( text ) ); + SDL_free(text); + return str; +} + +}}}} + +#endif diff --git a/src/window/backend/SDL13/cclipboardsdl.hpp b/src/window/backend/SDL13/cclipboardsdl.hpp new file mode 100644 index 000000000..aecabbe46 --- /dev/null +++ b/src/window/backend/SDL13/cclipboardsdl.hpp @@ -0,0 +1,35 @@ +#ifndef EE_WINDOWCCLIPBOARDSDL13_HPP +#define EE_WINDOWCCLIPBOARDSDL13_HPP + +#include "../../cbackend.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#include "../../base.hpp" +#include "../../cclipboard.hpp" + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class EE_API cClipboardSDL : public cClipboard { + public: + virtual ~cClipboardSDL(); + + std::string GetText(); + + String GetWideText(); + + void SetText( const std::string& Text ); + protected: + friend class cWindowSDL; + + cClipboardSDL( cWindow * window ); + + void Init(); +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/ccursormanagersdl.cpp b/src/window/backend/SDL13/ccursormanagersdl.cpp new file mode 100644 index 000000000..16774192d --- /dev/null +++ b/src/window/backend/SDL13/ccursormanagersdl.cpp @@ -0,0 +1,79 @@ +#include "ccursormanagersdl.hpp" +#include "ccursorsdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cCursorManagerSDL::cCursorManagerSDL( cWindow * window ) : + cCursorManager( window ) +{ +} + +cCursor * cCursorManagerSDL::Create( cTexture * tex, const eeVector2i& hotspot, const std::string& name ) { + return eeNew( cCursorSDL, ( tex, hotspot, name, mWindow ) ); +} + +cCursor * cCursorManagerSDL::Create( cImage * img, const eeVector2i& hotspot, const std::string& name ) { + return eeNew( cCursorSDL, ( img, hotspot, name, mWindow ) ); +} + +cCursor * cCursorManagerSDL::Create( const std::string& path, const eeVector2i& hotspot, const std::string& name ) { + return eeNew( cCursorSDL, ( path, hotspot, name, mWindow ) ); +} + +void cCursorManagerSDL::Set( cCursor * cursor ) { + SDL_SetCursor( reinterpret_cast( cursor )->GetCursor() ); + + mCurrrent = cursor; + mCurSysCursor = false; + mSysCursor = Cursor::SYS_CURSOR_NONE; +} + +void cCursorManagerSDL::Set( EE_SYSTEM_CURSOR syscurid ) { + mWindow->GetPlatform()->SetSystemMouseCursor( syscurid ); + + mCurrrent = NULL; + mCurSysCursor = true; + mSysCursor = syscurid; +} + +void cCursorManagerSDL::Show() { + Visible( true ); +} + +void cCursorManagerSDL::Hide() { + Visible( false ); +} + +void cCursorManagerSDL::Visible( bool visible ) { + if ( visible ) { + SDL_ShowCursor( SDL_ENABLE ); + mVisible = true; + } else { + SDL_ShowCursor( SDL_DISABLE ); + mVisible = false; + } +} + +void cCursorManagerSDL::Remove( cCursor * cursor, bool Delete ) { + cCursorManager::Remove( cursor, Delete ); +} + +void cCursorManagerSDL::Reload() { + if ( mVisible ) { + Show(); + + if ( mCurSysCursor ) { + Set( mSysCursor ); + } else { + Set( mCurrrent ); + } + } else { + Hide(); + } +} + +}}}} + +#endif diff --git a/src/window/backend/SDL13/ccursormanagersdl.hpp b/src/window/backend/SDL13/ccursormanagersdl.hpp new file mode 100644 index 000000000..769bddc0c --- /dev/null +++ b/src/window/backend/SDL13/ccursormanagersdl.hpp @@ -0,0 +1,42 @@ +#ifndef EE_WINDOWCCURSORMANAGERSDL13_HPP +#define EE_WINDOWCCURSORMANAGERSDL13_HPP + +#include "../../ccursormanager.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +using namespace EE::Window; + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class cCursorManagerSDL : public cCursorManager { + public: + cCursorManagerSDL( cWindow * window ); + + cCursor * Create( cTexture * tex, const eeVector2i& hotspot, const std::string& name ); + + cCursor * Create( cImage * img, const eeVector2i& hotspot, const std::string& name ); + + cCursor * Create( const std::string& path, const eeVector2i& hotspot, const std::string& name ); + + void Set( cCursor * cursor ); + + void Set( EE_SYSTEM_CURSOR syscurid ); + + void Show(); + + void Hide(); + + void Visible( bool visible ); + + void Remove( cCursor * cursor, bool Delete = false ); + + void Reload(); +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/ccursorsdl.cpp b/src/window/backend/SDL13/ccursorsdl.cpp new file mode 100644 index 000000000..f197b2520 --- /dev/null +++ b/src/window/backend/SDL13/ccursorsdl.cpp @@ -0,0 +1,74 @@ +#include "ccursorsdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cCursorSDL::cCursorSDL( cTexture * tex, const eeVector2i& hotspot, const std::string& name, cWindow * window ) : + cCursor( tex, hotspot, name, window ), + mCursor( NULL ) +{ + Create(); +} + +cCursorSDL::cCursorSDL( cImage * img, const eeVector2i& hotspot, const std::string& name, cWindow * window ) : + cCursor( img, hotspot, name, window ), + mCursor( NULL ) +{ + Create(); +} + +cCursorSDL::cCursorSDL( const std::string& path, const eeVector2i& hotspot, const std::string& name, cWindow * window ) : + cCursor( path, hotspot, name, window ), + mCursor( NULL ) +{ + Create(); +} + +cCursorSDL::~cCursorSDL() { + if ( NULL != mCursor ) + SDL_FreeCursor( mCursor ); +} + +void cCursorSDL::Create() { + if ( NULL == mImage ) + return; + + Uint32 rmask, gmask, bmask, amask; + #if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; + #else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; + #endif + SDL_Surface * TempGlyphSheet = SDL_CreateRGBSurface( SDL_SWSURFACE, mImage->Width(), mImage->Height(), mImage->Channels() * 8, rmask, gmask, bmask, amask ); + + SDL_LockSurface( TempGlyphSheet ); + + Uint32 ssize = TempGlyphSheet->w * TempGlyphSheet->h * mImage->Channels(); + + Uint8 * Ptr = mImage->GetPixels(); + + for ( Uint32 i=0; i < ssize; i++ ) { + ( static_cast( TempGlyphSheet->pixels ) )[i] = Ptr[i]; + } + + mCursor = SDL_CreateColorCursor( TempGlyphSheet, mHotSpot.x, mHotSpot.y ); + + SDL_UnlockSurface( TempGlyphSheet ); + + SDL_FreeSurface( TempGlyphSheet ); +} + +SDL_Cursor * cCursorSDL::GetCursor() const { + return mCursor; +} + +}}}} + +#endif diff --git a/src/window/backend/SDL13/ccursorsdl.hpp b/src/window/backend/SDL13/ccursorsdl.hpp new file mode 100644 index 000000000..649bec489 --- /dev/null +++ b/src/window/backend/SDL13/ccursorsdl.hpp @@ -0,0 +1,36 @@ +#ifndef EE_WINDOWCCURSORSDL13_HPP +#define EE_WINDOWCCURSORSDL13_HPP + +#include "../../ccursor.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +using namespace EE::Window; + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class cCursorSDL : public cCursor { + public: + SDL_Cursor * GetCursor() const; + protected: + friend class cCursorManagerSDL; + + SDL_Cursor * mCursor; + + cCursorSDL( cTexture * tex, const eeVector2i& hotspot, const std::string& name, cWindow * window ); + + cCursorSDL( cImage * img, const eeVector2i& hotspot, const std::string& name, cWindow * window ); + + cCursorSDL( const std::string& path, const eeVector2i& hotspot, const std::string& name, cWindow * window ); + + virtual ~cCursorSDL(); + + void Create(); +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/cinputsdl.cpp b/src/window/backend/SDL13/cinputsdl.cpp new file mode 100644 index 000000000..ca1e61d80 --- /dev/null +++ b/src/window/backend/SDL13/cinputsdl.cpp @@ -0,0 +1,376 @@ +#include "cinputsdl.hpp" +#include "cjoystickmanagersdl.hpp" +#include "ccursormanagersdl.hpp" +#include "cwindowsdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#define EE_APPMOUSEFOCUS 0x01 +#define EE_APPINPUTFOCUS 0x02 +#define EE_APPACTIVE 0x04 + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cInputSDL::cInputSDL( cWindow * window ) : + cInput( window, eeNew( cJoystickManagerSDL, () ) ) +{ + #if defined( EE_X11_PLATFORM ) + mMouseSpeed = 1.75f; + #endif +} + +cInputSDL::~cInputSDL() { +} + +void cInputSDL::Update() { + SDL_Event SDLEvent; + InputEvent EEEvent; + + CleanStates(); + + while ( SDL_PollEvent( &SDLEvent ) ) { + switch( SDLEvent.type ) { + case SDL_WINDOWEVENT: + { + switch ( SDLEvent.window.event ) { + case SDL_WINDOWEVENT_RESIZED: + { + if ( mWindow->Windowed() ) { + EEEvent.Type = InputEvent::VideoResize; + EEEvent.resize.w = SDLEvent.window.data1; + EEEvent.resize.h = SDLEvent.window.data2; + } else { + EEEvent.Type = InputEvent::NoEvent; + } + break; + } + case SDL_WINDOWEVENT_EXPOSED: + { + EEEvent.Type = InputEvent::VideoExpose; + EEEvent.expose.type = EEEvent.Type; + break; + } + case SDL_WINDOWEVENT_MINIMIZED: + { + EEEvent.Type = InputEvent::Active; + EEEvent.active.gain = 0; + EEEvent.active.state = EE_APPACTIVE; + break; + } + case SDL_WINDOWEVENT_RESTORED: + { + EEEvent.Type = InputEvent::Active; + EEEvent.active.gain = 1; + EEEvent.active.state = EE_APPACTIVE; + break; + } + case SDL_WINDOWEVENT_ENTER: + { + EEEvent.Type = InputEvent::Active; + EEEvent.active.gain = 1; + EEEvent.active.state = EE_APPMOUSEFOCUS; + break; + } + case SDL_WINDOWEVENT_LEAVE: + { + EEEvent.Type = InputEvent::Active; + EEEvent.active.gain = 0; + EEEvent.active.state = EE_APPMOUSEFOCUS; + break; + } + case SDL_WINDOWEVENT_FOCUS_GAINED: + { + EEEvent.Type = InputEvent::Active; + EEEvent.active.gain = 1; + EEEvent.active.state = EE_APPINPUTFOCUS; + break; + } + case SDL_WINDOWEVENT_FOCUS_LOST: + { + EEEvent.Type = InputEvent::Active; + EEEvent.active.gain = 0; + EEEvent.active.state = EE_APPINPUTFOCUS; + break; + } + } + + break; + } + case SDL_TEXTINPUT: + { + EEEvent.Type = InputEvent::KeyDown; + EEEvent.key.state = SDLEvent.key.state; + EEEvent.key.which = SDLEvent.key.windowID; + EEEvent.key.keysym.sym = 0; + EEEvent.key.keysym.mod = 0xFFFFFFFF; + EEEvent.key.keysym.unicode = String::FromUtf8( SDLEvent.text.text )[0]; + break; + } + case SDL_KEYDOWN: + { + EEEvent.Type = InputEvent::KeyDown; + EEEvent.key.state = SDLEvent.key.state; + EEEvent.key.which = SDLEvent.key.windowID; + EEEvent.key.keysym.sym = mKeyCodesTable[ SDLEvent.key.keysym.scancode ]; + EEEvent.key.keysym.mod = SDLEvent.key.keysym.mod; + EEEvent.key.keysym.unicode = 0; + break; + } + case SDL_KEYUP: + { + EEEvent.Type = InputEvent::KeyUp; + EEEvent.key.state = SDLEvent.key.state; + EEEvent.key.which = SDLEvent.key.windowID; + EEEvent.key.keysym.sym = mKeyCodesTable[ SDLEvent.key.keysym.scancode ]; + + if ( SDLEvent.key.keysym.scancode == SDL_SCANCODE_1 ) { + EEEvent.key.state = SDLEvent.key.state; + } + + EEEvent.key.keysym.mod = SDLEvent.key.keysym.mod; + EEEvent.key.keysym.unicode = 0; + break; + } + case SDL_MOUSEMOTION: + { + EEEvent.Type = InputEvent::MouseMotion; + EEEvent.motion.which = SDLEvent.motion.windowID; + EEEvent.motion.state = SDLEvent.motion.state; + EEEvent.motion.x = SDLEvent.motion.x; + EEEvent.motion.y = SDLEvent.motion.y; + EEEvent.motion.xrel = SDLEvent.motion.xrel; + EEEvent.motion.yrel = SDLEvent.motion.yrel; + break; + } + case SDL_MOUSEBUTTONDOWN: + { + EEEvent.Type = InputEvent::MouseButtonDown; + EEEvent.button.button = SDLEvent.button.button; + EEEvent.button.which = SDLEvent.button.windowID; + EEEvent.button.state = SDLEvent.button.state; + EEEvent.button.x = SDLEvent.button.x; + EEEvent.button.y = SDLEvent.button.y; + break; + } + case SDL_MOUSEBUTTONUP: + { + EEEvent.Type = InputEvent::MouseButtonUp; + EEEvent.button.button = SDLEvent.button.button; + EEEvent.button.which = SDLEvent.button.windowID; + EEEvent.button.state = SDLEvent.button.state; + EEEvent.button.x = SDLEvent.button.x; + EEEvent.button.y = SDLEvent.button.y; + break; + } + case SDL_MOUSEWHEEL: + { + Uint8 button; + int x, y; + + if ( SDLEvent.wheel.y == 0 ) { + break; + } + + SDL_GetMouseState( &x, &y ); + + if ( SDLEvent.wheel.y > 0 ) { + button = EE_BUTTON_WHEELUP; + } else { + button = EE_BUTTON_WHEELDOWN; + } + + EEEvent.button.button = button; + EEEvent.button.x = x; + EEEvent.button.y = y; + EEEvent.button.which = SDLEvent.wheel.windowID; + + EEEvent.Type = InputEvent::MouseButtonDown; + EEEvent.button.state = 1; + ProcessEvent( &EEEvent ); + + EEEvent.Type = InputEvent::MouseButtonUp; + EEEvent.button.state = 0; + break; + } + case SDL_JOYAXISMOTION: + { + EEEvent.Type = InputEvent::JoyAxisMotion; + EEEvent.jaxis.which = SDLEvent.jaxis.which; + EEEvent.jaxis.axis = SDLEvent.jaxis.axis; + EEEvent.jaxis.value = SDLEvent.jaxis.value; + break; + } + case SDL_JOYBALLMOTION: + { + EEEvent.Type = InputEvent::JoyBallMotion; + EEEvent.jball.which = SDLEvent.jball.which; + EEEvent.jball.ball = SDLEvent.jball.ball; + EEEvent.jball.xrel = SDLEvent.jball.xrel; + EEEvent.jball.yrel = SDLEvent.jball.yrel; + break; + } + case SDL_JOYHATMOTION: + { + EEEvent.Type = InputEvent::JoyHatMotion; + EEEvent.jhat.which = SDLEvent.jhat.which; + EEEvent.jhat.value = SDLEvent.jhat.value; + EEEvent.jhat.hat = SDLEvent.jhat.hat; + break; + } + case SDL_JOYBUTTONDOWN: + { + EEEvent.Type = InputEvent::JoyButtonDown; + EEEvent.jbutton.which = SDLEvent.jbutton.which; + EEEvent.jbutton.state = SDLEvent.jbutton.state; + EEEvent.jbutton.button = SDLEvent.jbutton.button; + break; + } + case SDL_JOYBUTTONUP: + { + EEEvent.Type = InputEvent::JoyButtonUp; + EEEvent.jbutton.which = SDLEvent.jbutton.which; + EEEvent.jbutton.state = SDLEvent.jbutton.state; + EEEvent.jbutton.button = SDLEvent.jbutton.button; + break; + } + case SDL_QUIT: + { + EEEvent.Type = InputEvent::Quit; + EEEvent.quit.type = EEEvent.Type; + break; + } + case SDL_SYSWMEVENT: + { + EEEvent.Type = InputEvent::SysWM; + EEEvent.syswm.msg = (InputEvent::SysWMmsg*)SDLEvent.syswm.msg; + break; + } + default: + { + if ( SDLEvent.type >= SDL_USEREVENT && SDLEvent.type < SDL_LASTEVENT ) { + EEEvent.Type = InputEvent::EventUser + SDLEvent.type - SDL_USEREVENT; + EEEvent.user.type = EEEvent.Type; + EEEvent.user.code = SDLEvent.user.code; + EEEvent.user.data1 = SDLEvent.user.data1; + EEEvent.user.data2 = SDLEvent.user.data2; + } else { + EEEvent.Type = InputEvent::NoEvent; + } + } + } + + if ( InputEvent::NoEvent != EEEvent.Type ) { + ProcessEvent( &EEEvent ); + } + } +} + +bool cInputSDL::GrabInput() { + return ( SDL_GetWindowGrab( reinterpret_cast ( mWindow )->GetSDLWindow() ) == SDL_TRUE ) ? true : false; +} + +void cInputSDL::GrabInput( const bool& Grab ) { + SDL_SetWindowGrab( reinterpret_cast ( mWindow )->GetSDLWindow(), Grab ? SDL_TRUE : SDL_FALSE ); +} + +void cInputSDL::InjectMousePos( const Uint16& x, const Uint16& y ) { + SDL_WarpMouseInWindow( reinterpret_cast( mWindow )->GetSDLWindow(), x, y ); +} + +void cInputSDL::Init() { + eeVector2if mTempMouse; + + SDL_GetMouseState( &mTempMouse.x, &mTempMouse.y ); + + mMousePos.x = (eeInt)mTempMouse.x; + mMousePos.y = (eeInt)mTempMouse.y; + + InitializeTables(); + + mJoystickManager->Open(); +} + +void cInputSDL::InitializeTables() { + Uint32 i; + + for ( i = SDL_SCANCODE_A; i <= SDL_SCANCODE_Z; i++ ) + mKeyCodesTable[ i ] = KEY_A + i - SDL_SCANCODE_A; + + mKeyCodesTable[ SDL_SCANCODE_0 ] = KEY_0; + mKeyCodesTable[ SDL_SCANCODE_1 ] = KEY_1; + mKeyCodesTable[ SDL_SCANCODE_2 ] = KEY_2; + mKeyCodesTable[ SDL_SCANCODE_3 ] = KEY_3; + mKeyCodesTable[ SDL_SCANCODE_4 ] = KEY_4; + mKeyCodesTable[ SDL_SCANCODE_5 ] = KEY_5; + mKeyCodesTable[ SDL_SCANCODE_6 ] = KEY_6; + mKeyCodesTable[ SDL_SCANCODE_7 ] = KEY_7; + mKeyCodesTable[ SDL_SCANCODE_8 ] = KEY_8; + mKeyCodesTable[ SDL_SCANCODE_9 ] = KEY_9; + mKeyCodesTable[ SDL_SCANCODE_KP_0 ] = KEY_KP0; + mKeyCodesTable[ SDL_SCANCODE_KP_1 ] = KEY_KP1; + mKeyCodesTable[ SDL_SCANCODE_KP_2 ] = KEY_KP2; + mKeyCodesTable[ SDL_SCANCODE_KP_3 ] = KEY_KP3; + mKeyCodesTable[ SDL_SCANCODE_KP_4 ] = KEY_KP4; + mKeyCodesTable[ SDL_SCANCODE_KP_5 ] = KEY_KP5; + mKeyCodesTable[ SDL_SCANCODE_KP_6 ] = KEY_KP6; + mKeyCodesTable[ SDL_SCANCODE_KP_7 ] = KEY_KP7; + mKeyCodesTable[ SDL_SCANCODE_KP_8 ] = KEY_KP8; + mKeyCodesTable[ SDL_SCANCODE_KP_9 ] = KEY_KP9; + mKeyCodesTable[ SDL_SCANCODE_F1 ] = KEY_F1; + mKeyCodesTable[ SDL_SCANCODE_F2 ] = KEY_F2; + mKeyCodesTable[ SDL_SCANCODE_F3 ] = KEY_F3; + mKeyCodesTable[ SDL_SCANCODE_F4 ] = KEY_F4; + mKeyCodesTable[ SDL_SCANCODE_F5 ] = KEY_F5; + mKeyCodesTable[ SDL_SCANCODE_F6 ] = KEY_F6; + mKeyCodesTable[ SDL_SCANCODE_F7 ] = KEY_F7; + mKeyCodesTable[ SDL_SCANCODE_F8 ] = KEY_F8; + mKeyCodesTable[ SDL_SCANCODE_F9 ] = KEY_F9; + mKeyCodesTable[ SDL_SCANCODE_F10 ] = KEY_F10; + mKeyCodesTable[ SDL_SCANCODE_F11 ] = KEY_F11; + mKeyCodesTable[ SDL_SCANCODE_F12 ] = KEY_F12; + mKeyCodesTable[ SDL_SCANCODE_ESCAPE ] = KEY_ESCAPE; + mKeyCodesTable[ SDL_SCANCODE_MINUS ] = KEY_MINUS; + mKeyCodesTable[ SDL_SCANCODE_EQUALS ] = KEY_EQUALS; + mKeyCodesTable[ SDL_SCANCODE_BACKSPACE ] = KEY_BACKSPACE; + mKeyCodesTable[ SDL_SCANCODE_TAB ] = KEY_TAB; + mKeyCodesTable[ SDL_SCANCODE_RETURN ] = KEY_RETURN; + mKeyCodesTable[ SDL_SCANCODE_SEMICOLON ] = KEY_SEMICOLON; + mKeyCodesTable[ SDL_SCANCODE_BACKSLASH ] = KEY_BACKSLASH; + mKeyCodesTable[ SDL_SCANCODE_COMMA ] = KEY_COMMA; + mKeyCodesTable[ SDL_SCANCODE_SLASH ] = KEY_SLASH; + mKeyCodesTable[ SDL_SCANCODE_KP_SPACE ] = KEY_SPACE; + mKeyCodesTable[ SDL_SCANCODE_INSERT] = KEY_INSERT; + mKeyCodesTable[ SDL_SCANCODE_DELETE ] = KEY_DELETE; + mKeyCodesTable[ SDL_SCANCODE_HOME ] = KEY_HOME; + mKeyCodesTable[ SDL_SCANCODE_END ] = KEY_END; + mKeyCodesTable[ SDL_SCANCODE_PAGEUP ] = KEY_PAGEUP; + mKeyCodesTable[ SDL_SCANCODE_PAGEDOWN ] = KEY_PAGEDOWN; + mKeyCodesTable[ SDL_SCANCODE_LEFT ] = KEY_LEFT; + mKeyCodesTable[ SDL_SCANCODE_RIGHT ] = KEY_RIGHT; + mKeyCodesTable[ SDL_SCANCODE_UP ] = KEY_UP; + mKeyCodesTable[ SDL_SCANCODE_DOWN ] = KEY_DOWN; + mKeyCodesTable[ SDL_SCANCODE_KP_DIVIDE ] = KEY_KP_DIVIDE; + mKeyCodesTable[ SDL_SCANCODE_KP_MULTIPLY ] = KEY_KP_MULTIPLY; + mKeyCodesTable[ SDL_SCANCODE_KP_MINUS ] = KEY_KP_MINUS; + mKeyCodesTable[ SDL_SCANCODE_KP_PLUS ] = KEY_KP_PLUS; + mKeyCodesTable[ SDL_SCANCODE_KP_ENTER ] = KEY_KP_ENTER; + mKeyCodesTable[ SDL_SCANCODE_PRINTSCREEN ] = KEY_PRINT; + mKeyCodesTable[ SDL_SCANCODE_PAUSE ] = KEY_PAUSE; + mKeyCodesTable[ SDL_SCANCODE_KP_EQUALS ] = KEY_KP_EQUALS; + mKeyCodesTable[ SDL_SCANCODE_LSHIFT ] = KEY_LSHIFT; + mKeyCodesTable[ SDL_SCANCODE_RSHIFT ] = KEY_RSHIFT; + mKeyCodesTable[ SDL_SCANCODE_LCTRL ] = KEY_LCTRL; + mKeyCodesTable[ SDL_SCANCODE_RCTRL ] = KEY_RCTRL; + mKeyCodesTable[ SDL_SCANCODE_LALT ] = KEY_LALT; + mKeyCodesTable[ SDL_SCANCODE_RALT ] = KEY_RALT; + mKeyCodesTable[ SDL_SCANCODE_MODE ] = KEY_MODE; + mKeyCodesTable[ SDL_SCANCODE_LGUI ] = KEY_LSUPER; + mKeyCodesTable[ SDL_SCANCODE_RGUI ] = KEY_RSUPER; + mKeyCodesTable[ SDL_SCANCODE_SCROLLLOCK ] = KEY_SCROLLOCK; + mKeyCodesTable[ SDL_SCANCODE_NUMLOCKCLEAR ] = KEY_NUMLOCK; + mKeyCodesTable[ SDL_SCANCODE_CAPSLOCK ] = KEY_CAPSLOCK; +} + +}}}} + +#endif diff --git a/src/window/backend/SDL13/cinputsdl.hpp b/src/window/backend/SDL13/cinputsdl.hpp new file mode 100644 index 000000000..3313dc190 --- /dev/null +++ b/src/window/backend/SDL13/cinputsdl.hpp @@ -0,0 +1,40 @@ +#ifndef EE_WINDOWCINPUTSDL13_HPP +#define EE_WINDOWCINPUTSDL13_HPP + +#include "../../cbackend.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#include "../../cinput.hpp" + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class EE_API cInputSDL : public cInput { + public: + virtual ~cInputSDL(); + + void Update(); + + bool GrabInput(); + + void GrabInput( const bool& Grab ); + + void InjectMousePos( const Uint16& x, const Uint16& y ); + protected: + friend class cWindowSDL; + + cInputSDL( cWindow * window ); + + virtual void Init(); + + Uint32 mKeyCodesTable[ SDL_NUM_SCANCODES ]; + + void InitializeTables(); +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/cjoystickmanagersdl.cpp b/src/window/backend/SDL13/cjoystickmanagersdl.cpp new file mode 100644 index 000000000..fd2140ab3 --- /dev/null +++ b/src/window/backend/SDL13/cjoystickmanagersdl.cpp @@ -0,0 +1,56 @@ +#include "cjoystickmanagersdl.hpp" +#include "cjoysticksdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cJoystickManagerSDL::cJoystickManagerSDL() : + cJoystickManager() +{ +} + +cJoystickManagerSDL::~cJoystickManagerSDL() { +} + +void cJoystickManagerSDL::Update() { + if ( mInit ) { + SDL_JoystickUpdate(); + + for ( eeUint i = 0; i < mCount; i++ ) + if ( NULL != mJoysticks[i] ) + mJoysticks[i]->Update(); + } +} + +void cJoystickManagerSDL::Open() { + eeInt error = SDL_InitSubSystem( SDL_INIT_JOYSTICK ); + + if ( !error ) { + mCount = SDL_NumJoysticks(); + + for ( eeUint i = 0; i < mCount; i++ ) + Create(i); + + mInit = true; + } +} + +void cJoystickManagerSDL::Close() { + if ( SDL_WasInit( SDL_INIT_JOYSTICK ) ) { + SDL_QuitSubSystem( SDL_INIT_JOYSTICK ); + + mInit = false; + } +} + +void cJoystickManagerSDL::Create( const Uint32& index ) { + if ( NULL != mJoysticks[ index ] ) + mJoysticks[ index ]->ReOpen(); + else + mJoysticks[ index ] = eeNew( cJoystickSDL, ( index ) ); +} + +}}}} + +#endif diff --git a/src/window/backend/SDL13/cjoystickmanagersdl.hpp b/src/window/backend/SDL13/cjoystickmanagersdl.hpp new file mode 100644 index 000000000..049851ded --- /dev/null +++ b/src/window/backend/SDL13/cjoystickmanagersdl.hpp @@ -0,0 +1,32 @@ +#ifndef EE_WINDOWCJOYSTICKMANAGERSDL13_HPP +#define EE_WINDOWCJOYSTICKMANAGERSDL13_HPP + +#include "../../cbackend.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#include "../../cjoystickmanager.hpp" + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class EE_API cJoystickManagerSDL : public cJoystickManager { + public: + cJoystickManagerSDL(); + + virtual ~cJoystickManagerSDL(); + + void Update(); + + void Close(); + + void Open(); + protected: + void Create( const Uint32& index ); +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/cjoysticksdl.cpp b/src/window/backend/SDL13/cjoysticksdl.cpp new file mode 100644 index 000000000..7707b6b57 --- /dev/null +++ b/src/window/backend/SDL13/cjoysticksdl.cpp @@ -0,0 +1,87 @@ +#include "cjoysticksdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cJoystickSDL::cJoystickSDL( const Uint32& index ) : + cJoystick( index ), + mJoystick( NULL ) +{ + Open(); +} + +cJoystickSDL::~cJoystickSDL() { + Close(); +} + +void cJoystickSDL::Open() { + mJoystick = SDL_JoystickOpen( mIndex ); + + if ( NULL != mJoystick ) { + mName = SDL_JoystickName( mIndex ); + mHats = SDL_JoystickNumHats( mJoystick ); + mButtons = SDL_JoystickNumButtons( mJoystick ); + mAxes = SDL_JoystickNumAxes( mJoystick ); + mBalls = SDL_JoystickNumBalls( mJoystick ); + + mButtonDown = mButtonDownLast = mButtonUp = 0; + + if ( mButtons > 32 ) + mButtons = 32; + } +} + +void cJoystickSDL::Close() { + if( SDL_JoystickOpened( mIndex ) ) + SDL_JoystickClose( mJoystick ); + + mJoystick = NULL; + mName = ""; + mHats = mButtons = mAxes = mBalls = 0; +} + +void cJoystickSDL::Update() { + if ( NULL != mJoystick ) { + ClearStates(); + + for ( Int32 i = 0; i < mButtons; i++ ) { + UpdateButton( i, 0 != SDL_JoystickGetButton( mJoystick, i ) ); + } + + } +} + +Uint8 cJoystickSDL::GetHat( const Int32& index ) { + if ( index >= 0 && index < mHats ) + return SDL_JoystickGetHat( mJoystick, index ); + + return HAT_CENTERED; +} + +eeFloat cJoystickSDL::GetAxis( const Int32& axis ) { + if ( axis >= 0 && axis < mAxes ) { + return (eeFloat)SDL_JoystickGetAxis( mJoystick, axis ) / 32768.f; + } + + return 0; +} + +eeVector2i cJoystickSDL::GetBallMotion( const Int32& ball ) { + eeVector2i v; + + if ( ball >= 0 && ball < mBalls ) + SDL_JoystickGetBall( mJoystick, ball, &v.x, &v.y ); + + return v; +} + + +bool cJoystickSDL::Plugged() const { + return NULL != mJoystick; +} + + +}}}} + +#endif diff --git a/src/window/backend/SDL13/cjoysticksdl.hpp b/src/window/backend/SDL13/cjoysticksdl.hpp new file mode 100644 index 000000000..4fedb37dc --- /dev/null +++ b/src/window/backend/SDL13/cjoysticksdl.hpp @@ -0,0 +1,40 @@ +#ifndef EE_WINDOWCJOYSTICKSDL13_HPP +#define EE_WINDOWCJOYSTICKSDL13_HPP + +#include "../../cbackend.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#include "../../cjoystick.hpp" + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class EE_API cJoystickSDL : public cJoystick { + public: + cJoystickSDL( const Uint32& index ); + + virtual ~cJoystickSDL(); + + void Close(); + + void Open(); + + void Update(); + + Uint8 GetHat( const Int32& index ); + + eeFloat GetAxis( const Int32& axis ); + + eeVector2i GetBallMotion( const Int32& ball ); + + bool Plugged() const; + protected: + SDL_Joystick * mJoystick; +}; + +}}}} + +#endif + +#endif diff --git a/src/window/backend/SDL13/cwindowsdl.cpp b/src/window/backend/SDL13/cwindowsdl.cpp new file mode 100644 index 000000000..0d1d6bb86 --- /dev/null +++ b/src/window/backend/SDL13/cwindowsdl.cpp @@ -0,0 +1,446 @@ +#include "cwindowsdl.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#include "cclipboardsdl.hpp" +#include "cinputsdl.hpp" +#include "ccursormanagersdl.hpp" + +#include "../../../graphics/cglobalbatchrenderer.hpp" +#include "../../../graphics/cshaderprogrammanager.hpp" +#include "../../../graphics/cvertexbuffermanager.hpp" +#include "../../../graphics/cframebuffermanager.hpp" +#include "../../../graphics/ctexturefactory.hpp" +#include "../../platform/platformimpl.hpp" + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +cWindowSDL::cWindowSDL( WindowSettings Settings, ContextSettings Context ) : + cWindow( Settings, Context, eeNew( cClipboardSDL, ( this ) ), eeNew( cInputSDL, ( this ) ), eeNew( cCursorManagerSDL, ( this ) ) ), + mSDLWindow( NULL ), + mGLContext( NULL ) +{ + Create( Settings, Context ); +} + +cWindowSDL::~cWindowSDL() { +} + +bool cWindowSDL::Create( WindowSettings Settings, ContextSettings Context ) { + try { + if ( mWindow.Created ) + return false; + + mWindow.WindowConfig = Settings; + mWindow.ContextConfig = Context; + + if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) { + cLog::instance()->Write( "Unable to initialize SDL: " + std::string( SDL_GetError() ) ); + return false; + } + + SDL_DisplayMode dpm; + SDL_GetDesktopDisplayMode( 0, &dpm ); + + mWindow.DesktopResolution = eeSize( dpm.w, dpm.h ); + + if ( mWindow.WindowConfig.Style & WindowStyle::UseDesktopResolution ) { + mWindow.WindowConfig.Width = mWindow.DesktopResolution.Width(); + mWindow.WindowConfig.Height = mWindow.DesktopResolution.Height(); + } + + mWindow.Flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; + + if ( mWindow.WindowConfig.Style & WindowStyle::Resize ) { + mWindow.Flags |= SDL_WINDOW_RESIZABLE; + } + + if ( mWindow.WindowConfig.Style & WindowStyle::NoBorder ) { + mWindow.Flags |= SDL_WINDOW_BORDERLESS; + } + + SetGLConfig(); + + Uint32 mTmpFlags = mWindow.Flags; + + if ( mWindow.WindowConfig.Style & WindowStyle::Fullscreen ) { + mTmpFlags |= SDL_WINDOW_FULLSCREEN; + } + + mSDLWindow = SDL_CreateWindow( mWindow.WindowConfig.Caption.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, mWindow.WindowConfig.Width, mWindow.WindowConfig.Height, mTmpFlags ); + + mWindow.WindowSize = eeSize( mWindow.WindowConfig.Width, mWindow.WindowConfig.Height ); + + if ( NULL == mSDLWindow ) { + cLog::instance()->Write( "Unable to create window: " + std::string( SDL_GetError() ) ); + return false; + } + + mGLContext = SDL_GL_CreateContext( mSDLWindow ); + + if ( NULL == mGLContext ) { + cLog::instance()->Write( "Unable to create context: " + std::string( SDL_GetError() ) ); + return false; + } + + SDL_GL_SetSwapInterval( ( mWindow.ContextConfig.VSync ? 1 : 0 ) ); // VSync + + if ( NULL == cGL::ExistsSingleton() ) { + cGL::CreateSingleton( mWindow.ContextConfig.Version ); + cGL::instance()->Init(); + } + + CreatePlatform(); + + GetMainContext(); + + Caption( mWindow.WindowConfig.Caption ); + + CreateView(); + + Setup2D(); + + mWindow.Created = true; + + if ( "" != mWindow.WindowConfig.Icon ) { + Icon( mWindow.WindowConfig.Icon ); + } + + LogSuccessfulInit( GetVersion() ); + + /// Init the clipboard after the window creation + reinterpret_cast ( mClipboard )->Init(); + + /// Init the input after the window creation + reinterpret_cast ( mInput )->Init(); + + mCursorManager->Set( Cursor::SYS_CURSOR_DEFAULT ); + + return true; + } catch (...) { + LogFailureInit( "cWindowSDL", GetVersion() ); + return false; + } +} + +std::string cWindowSDL::GetVersion() { + SDL_version ver; + + SDL_GetVersion( &ver ); + + return StrFormated( "SDL %d.%d.%d", ver.major, ver.minor, ver.patch ); +} + +void cWindowSDL::CreatePlatform() { + eeSAFE_DELETE( mPlatform ); +#if EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX || defined( EE_X11_PLATFORM ) + SDL_VERSION( &mWMinfo.version ); + SDL_GetWindowWMInfo ( mSDLWindow, &mWMinfo ); +#endif + +#if defined( EE_X11_PLATFORM ) + mPlatform = eeNew( Platform::cX11Impl, ( this, mWMinfo.info.x11.display, mWMinfo.info.x11.window, mWMinfo.info.x11.window, NULL, NULL ) ); +#elif EE_PLATFORM == EE_PLATFORM_WIN + mPlatform = eeNew( Platform::cWinImpl, ( this, GetWindowHandler() ) ); +#elif EE_PLATFORM == EE_PLATFORM_MACOSX + mPlatform = eeNew( Platform::cOSXImpl, ( this ) ); +#else + cWindow::CreatePlatform(); +#endif +} + +void cWindowSDL::SetGLConfig() { + SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE , mWindow.ContextConfig.DepthBufferSize ); // Depth + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, ( mWindow.ContextConfig.DoubleBuffering ? 1 : 0 ) ); // Double Buffering + SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, mWindow.ContextConfig.StencilBufferSize ); + + if ( mWindow.WindowConfig.BitsPerPixel == 16 ) { + SDL_GL_SetAttribute( SDL_GL_RED_SIZE , 4 ); + SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE , 4 ); + SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE , 4 ); + SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE , 4 ); + } else { + SDL_GL_SetAttribute( SDL_GL_RED_SIZE , 8); + SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE , 8 ); + SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE , 8 ); + SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE , 8 ); + } +} + +void cWindowSDL::ToggleFullscreen() { + bool WasMaximized = mWindow.Maximized; + + if ( Windowed() ) { + Size( mWindow.WindowConfig.Width, mWindow.WindowConfig.Height, !Windowed() ); + } else { + Size( mWindow.WindowSize.Width(), mWindow.WindowSize.Height(), !Windowed() ); + } + + if ( WasMaximized ) { + Maximize(); + } + + GetCursorManager()->Reload(); +} + +void cWindowSDL::Caption( const std::string& Caption ) { + mWindow.WindowConfig.Caption = Caption; + + SDL_SetWindowTitle( mSDLWindow, Caption.c_str() ); +} + +bool cWindowSDL::Active() { + Uint32 flags = 0; + + flags = SDL_GetWindowFlags( mSDLWindow ); + + return 0 != ( ( flags & SDL_WINDOW_INPUT_FOCUS ) && ( flags & SDL_WINDOW_MOUSE_FOCUS ) ); +} + +bool cWindowSDL::Visible() { + Uint32 flags = 0; + + flags = SDL_GetWindowFlags( mSDLWindow ); + + return 0 != ( ( flags & SDL_WINDOW_SHOWN ) && !( flags & SDL_WINDOW_MINIMIZED ) ); +} + +void cWindowSDL::Size( Uint32 Width, Uint32 Height, bool Windowed ) { + if ( ( !Width || !Height ) ) { + Width = mWindow.DesktopResolution.Width(); + Height = mWindow.DesktopResolution.Height(); + } + + if ( this->Windowed() == Windowed && Width == mWindow.WindowConfig.Width && Height == mWindow.WindowConfig.Height ) + return; + + try { + cLog::instance()->Writef( "Switching from %s to %s. Width: %d Height %d.", this->Windowed() ? "windowed" : "fullscreen", Windowed ? "windowed" : "fullscreen", Width, Height ); + + #if EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX + #if EE_PLATFORM == EE_PLATFORM_WIN + bool Reload = this->Windowed() != Windowed; + #else + bool Reload = true; + #endif + + if ( Reload ) + Graphics::cTextureFactory::instance()->GrabTextures(); + #endif + + Uint32 oldWidth = mWindow.WindowConfig.Width; + Uint32 oldHeight = mWindow.WindowConfig.Height; + + mWindow.WindowConfig.Width = Width; + mWindow.WindowConfig.Height = Height; + + if ( Windowed ) { + mWindow.WindowSize = eeSize( Width, Height ); + } else { + mWindow.WindowSize = eeSize( oldWidth, oldHeight ); + } + + if ( this->Windowed() && !Windowed ) { + mWinPos = Position(); + } + + SDL_SetWindowSize( mSDLWindow, Width, Height ); + + if ( this->Windowed() && !Windowed ) { + mWinPos = Position(); + + SetGLConfig(); + + SDL_SetWindowFullscreen( mSDLWindow, Windowed ? SDL_FALSE : SDL_TRUE ); + } + + #if EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX + if ( Reload ) { + cGL::instance()->Init(); + + Graphics::cTextureFactory::instance()->UngrabTextures(); // Reload all textures + Graphics::cShaderProgramManager::instance()->Reload(); // Reload all shaders + Graphics::Private::cFrameBufferManager::instance()->Reload(); // Reload all frame buffers + Graphics::Private::cVertexBufferManager::instance()->Reload(); // Reload all vertex buffers + GetMainContext(); // Recover the context + CreatePlatform(); + } + #endif + + if ( !this->Windowed() && Windowed ) { + Position( mWinPos.x, mWinPos.y ); + } + + SetFlagValue( &mWindow.WindowConfig.Style, WindowStyle::Fullscreen, !Windowed ); + + mDefaultView.SetView( 0, 0, Width, Height ); + + Setup2D(); + + SendVideoResizeCb(); + + mCursorManager->Reload(); + } catch (...) { + cLog::instance()->Write( "Unable to change resolution: " + std::string( SDL_GetError() ) ); + cLog::instance()->Save(); + mWindow.Created = false; + } +} + +void cWindowSDL::SwapBuffers() { + SDL_GL_SwapWindow( mSDLWindow ); +} + +std::vector< std::pair > cWindowSDL::GetPossibleResolutions() const { + std::vector< std::pair > result; + + for ( Int32 i = 0; i < SDL_GetNumDisplayModes(0); i++ ) { + SDL_DisplayMode mode; + SDL_GetDisplayMode( 0, i, &mode ); + + result.push_back( std::pair( mode.w, mode.h ) ); + } + + return result; +} + +void cWindowSDL::SetGamma( eeFloat Red, eeFloat Green, eeFloat Blue ) { + eeclamp( &Red , 0.1f, 10.0f ); + eeclamp( &Green , 0.1f, 10.0f ); + eeclamp( &Blue , 0.1f, 10.0f ); + + Uint16 red_ramp[256]; + Uint16 green_ramp[256]; + Uint16 blue_ramp[256]; + + SDL_CalculateGammaRamp(Red, red_ramp); + + if (Green == Red) { + SDL_memcpy(green_ramp, red_ramp, sizeof(red_ramp)); + } else { + SDL_CalculateGammaRamp(Green, green_ramp); + } + + if (Blue == Red) { + SDL_memcpy(blue_ramp, red_ramp, sizeof(red_ramp)); + } else { + SDL_CalculateGammaRamp(Blue, blue_ramp); + } + + SDL_SetWindowGammaRamp( mSDLWindow, red_ramp, green_ramp, blue_ramp ); +} + +eeWindowHandler cWindowSDL::GetWindowHandler() { +#if EE_PLATFORM == EE_PLATFORM_WIN + return mWMinfo.window; +#elif defined( EE_X11_PLATFORM ) + return mWMinfo.info.x11.display; +#elif EE_PLATFORM == EE_PLATFORM_MACOSX + return mWMinfo.cocoa.window; +#else + return 0; +#endif +} + +bool cWindowSDL::Icon( const std::string& Path ) { + int x, y, c; + + if ( !FileExists( Path ) ) { + return false; + } + + if ( !mWindow.Created ) { + if ( stbi_info( Path.c_str(), &x, &y, &c ) ) { + mWindow.WindowConfig.Icon = Path; + + return true; + } + + return false; + } + + unsigned char * Ptr = stbi_load( Path.c_str(), &x, &y, &c, 0 ); + + if ( NULL != Ptr ) { + Int32 W = x; + Int32 H = y; + + if ( ( W % 8 ) == 0 && ( H % 8 ) == 0 ) { + Uint32 rmask, gmask, bmask, amask; + #if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; + #else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; + #endif + SDL_Surface * TempGlyphSheet = SDL_CreateRGBSurface(SDL_SWSURFACE, W, H, c * 8, rmask, gmask, bmask, amask); + + SDL_LockSurface( TempGlyphSheet ); + + Uint32 ssize = TempGlyphSheet->w * TempGlyphSheet->h * c; + + for ( Uint32 i=0; i < ssize; i++ ) { + ( static_cast( TempGlyphSheet->pixels ) )[i+0] = (Ptr)[i]; + } + + SDL_UnlockSurface( TempGlyphSheet ); + + SDL_SetWindowIcon( mSDLWindow, TempGlyphSheet ); + + SDL_FreeSurface( TempGlyphSheet ); + + free( Ptr ); + + return true; + } + + free( Ptr ); + } + + return false; +} + +void cWindowSDL::Minimize() { + SDL_MinimizeWindow( mSDLWindow ); +} + +void cWindowSDL::Maximize() { + SDL_MaximizeWindow( mSDLWindow ); +} + +void cWindowSDL::Hide() { + SDL_HideWindow( mSDLWindow ); +} + +void cWindowSDL::Raise() { + SDL_RaiseWindow( mSDLWindow ); +} + +void cWindowSDL::Show() { + SDL_ShowWindow( mSDLWindow ); +} + +void cWindowSDL::Position( Int16 Left, Int16 Top ) { + SDL_SetWindowPosition( mSDLWindow, Left, Top ); +} + +eeVector2i cWindowSDL::Position() { + eeVector2i p; + + SDL_GetWindowPosition( mSDLWindow, &p.x, &p.y ); + + return p; +} + +SDL_WindowID cWindowSDL::GetSDLWindow() const { + return mSDLWindow; +} + +}}}} + +#endif diff --git a/src/window/backend/SDL13/cwindowsdl.hpp b/src/window/backend/SDL13/cwindowsdl.hpp new file mode 100644 index 000000000..ee971982f --- /dev/null +++ b/src/window/backend/SDL13/cwindowsdl.hpp @@ -0,0 +1,83 @@ +#ifndef EE_WINDOWCWINDOWSDL13_HPP +#define EE_WINDOWCWINDOWSDL13_HPP + +#include "../../cbackend.hpp" +#include "base.hpp" + +#ifdef EE_BACKEND_SDL_1_3 + +#include "../../cwindow.hpp" + +#if EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX || defined( EE_X11_PLATFORM ) +#include +#endif + +namespace EE { namespace Window { namespace Backend { namespace SDL13 { + +class EE_API cWindowSDL : public cWindow { + public: + cWindowSDL( WindowSettings Settings, ContextSettings Context ); + + virtual ~cWindowSDL(); + + bool Create( WindowSettings Settings, ContextSettings Context ); + + void ToggleFullscreen(); + + void Caption( const std::string& Caption ); + + bool Icon( const std::string& Path ); + + bool Active(); + + bool Visible(); + + void Size( Uint32 Width, Uint32 Height, bool Windowed ); + + std::vector< std::pair > GetPossibleResolutions() const; + + void SetGamma( eeFloat Red, eeFloat Green, eeFloat Blue ); + + eeWindowHandler GetWindowHandler(); + + virtual void Minimize(); + + virtual void Maximize(); + + virtual void Hide(); + + virtual void Raise(); + + virtual void Show(); + + virtual void Position( Int16 Left, Int16 Top ); + + virtual eeVector2i Position(); + + SDL_WindowID GetSDLWindow() const; + protected: + friend class cClipboardSDL; + + SDL_WindowID mSDLWindow; + SDL_GLContext mGLContext; + + #if EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX || defined( EE_X11_PLATFORM ) + SDL_SysWMinfo mWMinfo; + #endif + + eeVector2i mWinPos; + + void CreatePlatform(); + + void SwapBuffers(); + + void SetGLConfig(); + + std::string GetVersion(); +}; + +}}}} + +#endif + +#endif diff --git a/src/window/cengine.cpp b/src/window/cengine.cpp index 0302ef3f4..17510278a 100755 --- a/src/window/cengine.cpp +++ b/src/window/cengine.cpp @@ -13,6 +13,7 @@ #include "../physics/cphysicsmanager.hpp" #include "backend/SDL/cbackendsdl.hpp" +#include "backend/SDL13/cbackendsdl.hpp" #include "backend/allegro5/cbackendal.hpp" #define BACKEND_SDL 1 @@ -37,7 +38,13 @@ cEngine::cEngine() : mWindow( NULL ) { #if DEFAULT_BACKEND == BACKEND_SDL + + #if SDL_VERSION_ATLEAST(1,3,0) + mBackend = eeNew( Backend::SDL13::cBackendSDL, () ); + #else mBackend = eeNew( Backend::SDL::cBackendSDL, () ); + #endif + #elif DEFAULT_BACKEND == BACKEND_ALLEGRO mBackend = eeNew( Backend::Al::cBackendAl, () ); #endif @@ -87,7 +94,13 @@ void cEngine::Destroy() { cWindow * cEngine::CreateWindow( WindowSettings Settings, ContextSettings Context ) { #if DEFAULT_BACKEND == BACKEND_SDL + + #if SDL_VERSION_ATLEAST(1,3,0) + cWindow * window = eeNew( Backend::SDL13::cWindowSDL, ( Settings, Context ) ); + #else cWindow * window = eeNew( Backend::SDL::cWindowSDL, ( Settings, Context ) ); + #endif + #elif DEFAULT_BACKEND == BACKEND_ALLEGRO cWindow * window = eeNew( Backend::Al::cWindowAl, ( Settings, Context ) ); #endif diff --git a/src/window/cinput.cpp b/src/window/cinput.cpp index 0832da28a..c5d925df6 100644 --- a/src/window/cinput.cpp +++ b/src/window/cinput.cpp @@ -41,7 +41,8 @@ void cInput::ProcessEvent( InputEvent * Event ) { switch( Event->Type ) { case InputEvent::KeyDown: { - mInputMod = Event->key.keysym.mod; + if ( Event->key.keysym.mod != 0xFFFFFFFF ) + mInputMod = Event->key.keysym.mod; PushKey( &mKeysDown [ Event->key.keysym.sym / 8 ], Event->key.keysym.sym % 8, true ); break; diff --git a/src/window/platform/x11/cx11impl.cpp b/src/window/platform/x11/cx11impl.cpp index f20c9741f..d2cb9ac09 100644 --- a/src/window/platform/x11/cx11impl.cpp +++ b/src/window/platform/x11/cx11impl.cpp @@ -30,18 +30,18 @@ cX11Impl::~cX11Impl() { } void cX11Impl::MinimizeWindow() { - mLock(); + Lock(); XIconifyWindow( mDisplay, mX11Window, 0 ); XFlush( mDisplay ); - mUnlock(); + Unlock(); } void cX11Impl::MaximizeWindow() { // coded by Rafał Maj, idea from Måns Rullgård http://tinyurl.com/68mvk3 - mLock(); + Lock(); XEvent xev; Atom wm_state = XInternAtom( mDisplay, "_NET_WM_STATE", False); @@ -61,49 +61,49 @@ void cX11Impl::MaximizeWindow() { XFlush(mDisplay); - mUnlock(); + Unlock(); } void cX11Impl::HideWindow() { - mLock(); + Lock(); XUnmapWindow( mDisplay, mX11Window ); - mUnlock(); + Unlock(); } void cX11Impl::RaiseWindow() { - mLock(); + Lock(); XRaiseWindow( mDisplay, mX11Window ); - mUnlock(); + Unlock(); } void cX11Impl::ShowWindow() { - mLock(); + Lock(); XMapRaised( mDisplay, mX11Window ); - mUnlock(); + Unlock(); } void cX11Impl::MoveWindow( int left, int top ) { - mLock(); + Lock(); XMoveWindow( mDisplay, mX11Window, left, top ); XFlush( mDisplay ); - mUnlock(); + Unlock(); } void cX11Impl::SetContext( eeWindowContex Context ) { - mLock(); + Lock(); glXMakeCurrent( mDisplay, mX11Window, Context ); - mUnlock(); + Unlock(); } eeVector2i cX11Impl::Position() { @@ -119,20 +119,20 @@ void cX11Impl::ShowMouseCursor() { if ( !mCursorHidden ) return; - mLock(); + Lock(); XDefineCursor( mDisplay, mMainWindow, mCursorCurrent ); mCursorHidden = false; - mUnlock(); + Unlock(); } void cX11Impl::HideMouseCursor() { if ( mCursorHidden ) return; - mLock(); + Lock(); if ( mCursorInvisible == None ) { unsigned long gcmask; @@ -163,7 +163,7 @@ void cX11Impl::HideMouseCursor() { mCursorHidden = true; - mUnlock(); + Unlock(); } cCursor * cX11Impl::CreateMouseCursor( cTexture * tex, const eeVector2i& hotspot, const std::string& name ) { @@ -182,21 +182,21 @@ void cX11Impl::SetMouseCursor( cCursor * cursor ) { mCursorCurrent = reinterpret_cast( cursor )->GetCursor(); if ( !mCursorHidden ) { - mLock(); + Lock(); XDefineCursor( mDisplay, mMainWindow, mCursorCurrent ); - mUnlock(); + Unlock(); } } void cX11Impl::RestoreCursor() { if ( !mCursorHidden ) { - mLock(); + Lock(); XDefineCursor( mDisplay, mMainWindow, mCursorCurrent ); - mUnlock(); + Unlock(); } else { HideMouseCursor(); } @@ -267,7 +267,7 @@ void cX11Impl::SetSystemMouseCursor( Cursor::EE_SYSTEM_CURSOR syscursor ) { XFreeCursor( mDisplay, mCursorSystemLast ); } - mLock(); + Lock(); mCursorCurrent = XCreateFontCursor( mDisplay, cursor_shape ); mCursorSystemLast = mCursorCurrent; @@ -276,7 +276,7 @@ void cX11Impl::SetSystemMouseCursor( Cursor::EE_SYSTEM_CURSOR syscursor ) { XDefineCursor( mDisplay, mMainWindow, mCursorCurrent ); } - mUnlock(); + Unlock(); } eeWindowHandler cX11Impl::GetDisplay() const { @@ -284,11 +284,13 @@ eeWindowHandler cX11Impl::GetDisplay() const { } void cX11Impl::Lock() { - mLock(); + if ( NULL != mLock ) + mLock(); } void cX11Impl::Unlock() { - mUnlock(); + if ( NULL != mUnlock ) + mUnlock(); } }}}