From fb8552d1429f36d07fa1456190eeaa91dc258c2b Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 26 Feb 2013 01:34:40 -0300 Subject: [PATCH] Very basic rendering with SDL1.2 + OGL --- src/framework/CMakeLists.txt | 54 +++++-- src/framework/platform/crashhandler.h | 2 - src/framework/platform/platformwindow.cpp | 5 + src/framework/platform/sdlwindow.cpp | 160 +++++++++++++++++++ src/framework/platform/sdlwindow.h | 44 +++++ src/framework/platform/unixcrashhandler.cpp | 4 - src/framework/platform/unixplatform.cpp | 5 - src/framework/platform/win32crashhandler.cpp | 4 - src/framework/platform/win32platform.cpp | 4 - src/framework/platform/win32window.cpp | 4 - src/framework/platform/x11window.cpp | 4 - 11 files changed, 254 insertions(+), 36 deletions(-) diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 01d5859a..29ede3b8 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -123,15 +123,20 @@ set(framework_SOURCES ${framework_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/otml/otmlparser.h # crash handler - ${CMAKE_CURRENT_LIST_DIR}/platform/crashhandler.h - ${CMAKE_CURRENT_LIST_DIR}/platform/unixcrashhandler.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform/win32crashhandler.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform/win32platform.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform/unixplatform.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/platform.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/platform.h ) +if(WIN32) + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/platform/win32platform.cpp + ) +else() + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/platform/unixplatform.cpp + ) +endif() + set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/luafunctions.cpp PROPERTIES LANGUAGE CXX COMPILE_FLAGS "-g0 -Os") @@ -258,6 +263,15 @@ if(CRASH_HANDLER) message(STATUS "Crash handler: ON") if(WIN32) set(framework_LIBRARIES ${framework_LIBRARIES} imagehlp) + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/platform/crashhandler.h + ${CMAKE_CURRENT_LIST_DIR}/platform/win32crashhandler.cpp + ) + else() + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/platform/crashhandler.h + ${CMAKE_CURRENT_LIST_DIR}/platform/unixcrashhandler.cpp + ) endif() else() message(STATUS "Crash handler: OFF") @@ -343,6 +357,32 @@ if(FRAMEWORK_GRAPHICS) set(framework_LIBRARIES ${framework_LIBRARIES} X11) endif() + option(SDL "Use SDL support" OFF) + if(SDL) + find_package(SDL REQUIRED) + set(framework_DEFINITIONS ${framework_DEFINITIONS} -DSDL) + set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${SDL_INCLUDE_DIR}) + set(framework_LIBRARIES ${framework_LIBRARIES} ${SDL_LIBRARY}) + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/graphics/sdl/paintersdl.cpp + ${CMAKE_CURRENT_LIST_DIR}/graphics/sdl/paintersdl.h + ${CMAKE_CURRENT_LIST_DIR}/platform/sdlwindow.cpp + ${CMAKE_CURRENT_LIST_DIR}/platform/sdlwindow.h + ) + else() + if(WIN32) + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.cpp + ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.h + ) + else() + set(framework_SOURCES ${framework_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.cpp + ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.h + ) + endif() + endif() + set(framework_SOURCES ${framework_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/graphics/animatedtexture.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/animatedtexture.h @@ -435,10 +475,6 @@ if(FRAMEWORK_GRAPHICS) # platform window ${CMAKE_CURRENT_LIST_DIR}/platform/platformwindow.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/platformwindow.h - ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.h - ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.h # window input ${CMAKE_CURRENT_LIST_DIR}/input/mouse.cpp diff --git a/src/framework/platform/crashhandler.h b/src/framework/platform/crashhandler.h index 6a9e4fce..4c786a0f 100644 --- a/src/framework/platform/crashhandler.h +++ b/src/framework/platform/crashhandler.h @@ -23,8 +23,6 @@ #ifndef CRASHHANDLER_H #define CRASHHANDLER_H -#ifdef CRASH_HANDLER void installCrashHandler(); -#endif #endif diff --git a/src/framework/platform/platformwindow.cpp b/src/framework/platform/platformwindow.cpp index b51eff23..c0c3119d 100644 --- a/src/framework/platform/platformwindow.cpp +++ b/src/framework/platform/platformwindow.cpp @@ -22,6 +22,10 @@ #include "platformwindow.h" +#ifdef SDL +#include "sdlwindow.h" +SDLWindow window; +#else #ifdef WIN32 #include "win32window.h" WIN32Window window; @@ -30,6 +34,7 @@ WIN32Window window; #include X11Window window; #endif +#endif #include #include diff --git a/src/framework/platform/sdlwindow.cpp b/src/framework/platform/sdlwindow.cpp index f4894e48..28e1c538 100644 --- a/src/framework/platform/sdlwindow.cpp +++ b/src/framework/platform/sdlwindow.cpp @@ -19,3 +19,163 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + +#include "sdlwindow.h" + +SDLWindow::SDLWindow() +{ + m_visual = 0; + m_display = 0; +} + +void SDLWindow::init() +{ + if(SDL_Init(SDL_INIT_VIDEO) < 0) + g_logger.fatal(stdext::format("SDL video initialization failed: %s", SDL_GetError())); + + m_visual = SDL_GetVideoInfo(); + if(!m_visual) + g_logger.fatal(stdext::format("SDL video query failed: %s", SDL_GetError())); + + int width = 640; + int height = 480; + int bpp = m_visual->vfmt->BitsPerPixel; + int flags = SDL_OPENGL | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE; + + 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); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + m_display = SDL_SetVideoMode(width, height, bpp, flags); + if(!m_display) + g_logger.fatal(stdext::format("SDL video mode set failed: %s", SDL_GetError())); + + m_size = Size(width,height); + m_visible = true; +} + +void SDLWindow::terminate() +{ + SDL_Quit(); +} + +void SDLWindow::move(const Point& pos) +{ + +} + +void SDLWindow::resize(const Size& size) +{ + +} + +void SDLWindow::show() +{ + +} + +void SDLWindow::hide() +{ + +} + +void SDLWindow::maximize() +{ + +} + +void SDLWindow::poll() +{ + SDL_Event event; + while(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_KEYDOWN: + break; + case SDL_MOUSEMOTION: + break; + case SDL_VIDEORESIZE: + break; + case SDL_QUIT: + if(m_onClose) + m_onClose(); + default: + //printf("I don't know what this event is!\n"); + break; + } + } +} + +void SDLWindow::swapBuffers() +{ + SDL_GL_SwapBuffers(); +} + +void SDLWindow::showMouse() +{ + +} + +void SDLWindow::hideMouse() +{ + +} + +void SDLWindow::setMouseCursor(int cursorId) +{ + +} +void SDLWindow::restoreMouseCursor() +{ + +} + +void SDLWindow::setTitle(const std::string& title) +{ + SDL_WM_SetCaption(title.c_str(), title.c_str()); +} + +void SDLWindow::setMinimumSize(const Size& minimumSize) +{ + +} + +void SDLWindow::setFullscreen(bool fullscreen) +{ + +} + +void SDLWindow::setVerticalSync(bool enable) +{ + +} +void SDLWindow::setIcon(const std::string& file) +{ + +} +void SDLWindow::setClipboardText(const std::string& text) +{ + +} + +Size SDLWindow::getDisplaySize() +{ + return m_size; +} + +std::string SDLWindow::getClipboardText() +{ + return std::string(); +} + +std::string SDLWindow::getPlatformType() +{ + return "SDL"; +} + +int SDLWindow::internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) +{ + return 0; +} diff --git a/src/framework/platform/sdlwindow.h b/src/framework/platform/sdlwindow.h index 6a651755..9608f532 100644 --- a/src/framework/platform/sdlwindow.h +++ b/src/framework/platform/sdlwindow.h @@ -23,4 +23,48 @@ #ifndef SDLWINDOW_H #define SDLWINDOW_H +#include "platformwindow.h" +#include +#include + +class SDLWindow : public PlatformWindow +{ +public: + SDLWindow(); + + void init(); + void terminate(); + + void move(const Point& pos); + void resize(const Size& size); + void show(); + void hide(); + void maximize(); + void poll(); + void swapBuffers(); + void showMouse(); + void hideMouse(); + + void setMouseCursor(int cursorId); + void restoreMouseCursor(); + + void setTitle(const std::string& title); + void setMinimumSize(const Size& minimumSize); + void setFullscreen(bool fullscreen); + void setVerticalSync(bool enable); + void setIcon(const std::string& file); + void setClipboardText(const std::string& text); + + Size getDisplaySize(); + std::string getClipboardText(); + std::string getPlatformType(); + +protected: + int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot); + +private: + const SDL_VideoInfo *m_visual; + SDL_Surface *m_display; +}; + #endif diff --git a/src/framework/platform/unixcrashhandler.cpp b/src/framework/platform/unixcrashhandler.cpp index aeae5f72..9a97b062 100644 --- a/src/framework/platform/unixcrashhandler.cpp +++ b/src/framework/platform/unixcrashhandler.cpp @@ -20,8 +20,6 @@ * THE SOFTWARE. */ -#if !defined(WIN32) && defined(CRASH_HANDLER) - #include "crashhandler.h" #include #include @@ -133,5 +131,3 @@ void installCrashHandler() sigaction(SIGFPE, &sa, NULL); // floating-point exception sigaction(SIGABRT, &sa, NULL); // process aborted (asserts) } - -#endif diff --git a/src/framework/platform/unixplatform.cpp b/src/framework/platform/unixplatform.cpp index 29fc9565..613ce4ec 100644 --- a/src/framework/platform/unixplatform.cpp +++ b/src/framework/platform/unixplatform.cpp @@ -20,8 +20,6 @@ * THE SOFTWARE. */ -#ifndef WIN32 - #include "platform.h" #include #include @@ -167,6 +165,3 @@ std::string Platform::getOSName() } return std::string(); } - - -#endif diff --git a/src/framework/platform/win32crashhandler.cpp b/src/framework/platform/win32crashhandler.cpp index 59544a21..02992a75 100644 --- a/src/framework/platform/win32crashhandler.cpp +++ b/src/framework/platform/win32crashhandler.cpp @@ -20,8 +20,6 @@ * THE SOFTWARE. */ -#if defined(WIN32) && defined(CRASH_HANDLER) - #include "crashhandler.h" #include #include @@ -158,5 +156,3 @@ void installCrashHandler() { SetUnhandledExceptionFilter(ExceptionHandler); } - -#endif diff --git a/src/framework/platform/win32platform.cpp b/src/framework/platform/win32platform.cpp index 724a2568..e114f89f 100644 --- a/src/framework/platform/win32platform.cpp +++ b/src/framework/platform/win32platform.cpp @@ -20,8 +20,6 @@ * THE SOFTWARE. */ -#ifdef WIN32 - #include "platform.h" #include #include @@ -413,5 +411,3 @@ std::string Platform::getOSName() } return ret; } - -#endif diff --git a/src/framework/platform/win32window.cpp b/src/framework/platform/win32window.cpp index edc7d803..c8a2a2da 100644 --- a/src/framework/platform/win32window.cpp +++ b/src/framework/platform/win32window.cpp @@ -20,8 +20,6 @@ * THE SOFTWARE. */ -#ifdef WIN32 - #include "win32window.h" #include #include @@ -1019,5 +1017,3 @@ Rect WIN32Window::adjustWindowRect(const Rect& clientRect) } return rect; } - -#endif diff --git a/src/framework/platform/x11window.cpp b/src/framework/platform/x11window.cpp index 7d32563e..de07f3d6 100644 --- a/src/framework/platform/x11window.cpp +++ b/src/framework/platform/x11window.cpp @@ -20,8 +20,6 @@ * THE SOFTWARE. */ -#ifndef WIN32 - #include "x11window.h" #include #include @@ -1075,5 +1073,3 @@ std::string X11Window::getPlatformType() return "X11-EGL"; #endif } - -#endif