Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ elseif(MYGUI_RENDERSYSTEM EQUAL 6)
find_package(DirectX11)
macro_log_feature(DirectX_FOUND "DirectX11" "Support for the DirectX11 render system" "http://msdn.microsoft.com/en-us/directx/" TRUE "" "")
endif()
elseif(MYGUI_RENDERSYSTEM EQUAL 9)
find_package(SDL2_image)
#elseif for RENDERSYSTEM 7 is covered with RENDERSYSTEM 4
endif()

Expand Down
16 changes: 16 additions & 0 deletions CMake/Utils/MyGUIConfigTargets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ function(mygui_set_platform_name PLATFORM_ID)
set(MYGUI_PLATFORM_NAME OpenGL3 PARENT_SCOPE)
elseif(${PLATFORM_ID} EQUAL 8)
set(MYGUI_PLATFORM_NAME OpenGLES PARENT_SCOPE)
elseif(${PLATFORM_ID} EQUAL 9)
set(MYGUI_PLATFORM_NAME SDL2 PARENT_SCOPE)
endif()
endfunction(mygui_set_platform_name)

Expand Down Expand Up @@ -150,6 +152,14 @@ function(mygui_app PROJECTNAME SOLUTIONFOLDER)
${OPENGL_LIB_DIR}
${SDL2_IMAGE_LIB_DIR}
)
elseif(MYGUI_RENDERSYSTEM EQUAL 9)
add_definitions("-DMYGUI_SDL2_PLATFORM")
include_directories(SYSTEM
${SDL2_IMAGE_INCLUDE_DIRS}
)
link_directories(
${SDL2_IMAGE_LIB_DIR}
)
endif()

# setup demo target
Expand Down Expand Up @@ -192,6 +202,8 @@ function(mygui_app PROJECTNAME SOLUTIONFOLDER)
target_link_libraries(${PROJECTNAME} ${SDL2_IMAGE_LIBRARIES})
elseif(MYGUI_RENDERSYSTEM EQUAL 8)
target_link_libraries(${PROJECTNAME} ${SDL2_IMAGE_LIBRARIES})
elseif(MYGUI_RENDERSYSTEM EQUAL 9)
target_link_libraries(${PROJECTNAME} ${SDL2_IMAGE_LIBRARIES})
endif()
target_link_libraries(${PROJECTNAME}
MyGUIEngine
Expand Down Expand Up @@ -265,6 +277,8 @@ function(mygui_dll PROJECTNAME SOLUTIONFOLDER)
${OPENGL_INCLUDE_DIR}
)
link_directories(${OPENGL_LIB_DIR})
elseif(MYGUI_RENDERSYSTEM EQUAL 9)
add_definitions("-DMYGUI_SDL2_PLATFORM")
endif()


Expand Down Expand Up @@ -304,6 +318,8 @@ function(mygui_dll PROJECTNAME SOLUTIONFOLDER)
target_link_libraries(${PROJECTNAME} ${SDL2_IMAGE_LIBRARIES})
elseif(MYGUI_RENDERSYSTEM EQUAL 8)
target_link_libraries(${PROJECTNAME} ${SDL2_IMAGE_LIBRARIES})
elseif(MYGUI_RENDERSYSTEM EQUAL 9)
target_link_libraries(${PROJECTNAME} ${SDL2_IMAGE_LIBRARIES})
endif()

target_link_libraries(${PROJECTNAME}
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ set(MYGUI_RENDERSYSTEM 3 CACHE STRING
5 - Direct3D 9
6 - Direct3D 11
7 - OpenGL 3.x
8 - OpenGL ES 2.0 (Emscripten)"
8 - OpenGL ES 2.0 (Emscripten)
9 - SDL2 (>=2.0.18)
"
)

if(MYGUI_RENDERSYSTEM EQUAL 4 OR MYGUI_RENDERSYSTEM EQUAL 7)
Expand Down
14 changes: 11 additions & 3 deletions Common/Base/PlatformBaseManager/SdlBaseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace base

void SdlBaseManager::_windowResized(int w, int h)
{
resizeRender(w, h);
int fbWidth, fbHeight;
SDL_GL_GetDrawableSize(mSdlWindow, &fbWidth, &fbHeight);
resizeRender(fbWidth, fbHeight);

if (mPlatformReady)
MyGUI::RenderManager::getInstance().setViewSize(w, h);
Expand Down Expand Up @@ -43,13 +45,16 @@ namespace base
int left = (currDisp.w - width) / 2;
int top = (currDisp.h - height) / 2;

mSdlWindow = SDL_CreateWindow("MyGUI Render Window", left, top, width, height, (mIsOpenGlWindow ? SDL_WINDOW_OPENGL : 0) | SDL_WINDOW_RESIZABLE);
mSdlWindow = SDL_CreateWindow("MyGUI Render Window", left, top, width, height, (mIsOpenGlWindow ? SDL_WINDOW_OPENGL : 0) | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if (mSdlWindow == nullptr)
{
std::cerr << "Failed to create SDL window.";
exit(1);
}
mWindowOn = true;
int realW, realH;
SDL_GL_GetDrawableSize(mSdlWindow, &realW, &realH);
mScale = (double)realW / (double)width;

#if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
// set icon
Expand All @@ -73,7 +78,7 @@ namespace base
::SendMessageA((HWND)handle, WM_SETICON, 1, (LPARAM)hIconBig);
#endif

if (!createRender(width, height, windowed))
if (!createRender(realW, realH, windowed))
{
return false;
}
Expand Down Expand Up @@ -107,6 +112,9 @@ namespace base
{
switch (mEvent.type)
{
case SDL_QUIT:
mExit = true;
break;
// keyboard events
case SDL_KEYDOWN:
mKeyCode = mEvent.key.keysym.sym;
Expand Down
2 changes: 2 additions & 0 deletions Common/Base/PlatformBaseManager/SdlBaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ namespace base
bool mWindowOn = false;
SDL_Keycode mKeyCode;
int mFpsCounter = 0;

double mScale = 0;
};

}
109 changes: 109 additions & 0 deletions Common/Base/SDL2/BaseManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "Precompiled.h"
#include "BaseManager.h"

#include <SDL_image.h>

namespace base
{
bool BaseManager::createRender(int _width, int _height, bool _windowed)
{
mRenderer = SDL_CreateRenderer(mSdlWindow, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC);

if (mRenderer == nullptr) {
std::cerr << "Failed to initialize SDL_Renderer: " << SDL_GetError();
exit(1);
}

// TODO: Why do we need this? We need to understand why SubSkin::doRender() multiplies the size of the quad by 2
// SDL_RenderSetScale(mRenderer, 0.5, 0.5);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main issue with the platform. I wrote this many months ago, so I don't remember the details, but basically, without scaling the SDL2 output, it appears wrong (too big). I've removed this in the meantime because by enabling HiDPI support in SdlManager.cpp and passing the real framebuffer size this gets mitigated somehow.

However, there are 2 issues with this:

  • I'm almost sure this will look wrong on a non HiDPI system (although I don't actually have any to compare, sad but true :D).
  • There is actually no HiDPI rendering, it looks exactly like a non-HiDPI one but it requires 2x the pixels to look right, which obviously means there is an issue somewhere.

To see the problem, just compile this branch without the changes in SdlBaseManager.cpp.


if (IMG_Init(~0) == 0)
{
std::cerr << "Failed to initialize SDL_image: " << IMG_GetError();
exit(1);
}
return true;
}

void BaseManager::destroyRender()
{
SDL_DestroyRenderer(mRenderer);
IMG_Quit();
}

void BaseManager::createGuiPlatform()
{
mPlatform = new MyGUI::SDL2Platform();
setupResources();
mPlatform->initialise(this, mRenderer);
}

void BaseManager::destroyGuiPlatform()
{
if (mPlatform)
{
mPlatform->shutdown();
delete mPlatform;
mPlatform = nullptr;
}
}

void BaseManager::drawOneFrame()
{
SDL_SetRenderDrawColor(mRenderer, 10, 20, 30, 0xFF);
SDL_RenderClear(mRenderer);

if (mPlatform)
mPlatform->getRenderManagerPtr()->drawOneFrame();

SDL_RenderPresent(mRenderer);
}

void BaseManager::resizeRender(int _width, int _height)
{
// TODO: Maybe we need to resize the renderer?
printf("Resized\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I actually need to verify if SDL2 takes care of this automatically or we actually need to manually do it

}

void BaseManager::addResourceLocation(const std::string& _name, bool _recursive)
{
mPlatform->getDataManagerPtr()->addResourceLocation(_name, _recursive);
}


SDL_Texture* BaseManager::loadTexture(const std::string& _filename)
{
std::string fullname = MyGUI::SDL2DataManager::getInstance().getDataPath(_filename);

SDL_Texture *texture = NULL;
SDL_Surface *surface = IMG_Load(fullname.c_str());

MYGUI_ASSERT(surface != nullptr, "Failed to load image: " + fullname);

auto bpp = surface->format->BytesPerPixel;
auto newFormat = SDL_PIXELFORMAT_UNKNOWN;
switch (bpp) {
case 4:
newFormat = SDL_PIXELFORMAT_RGBA32;
break;
case 3:
newFormat = SDL_PIXELFORMAT_RGB24;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Texture loading is done in RGB but manual texture creation is done in BGR (it seems MyGUI uses BGR for color processing?) so this probably needs changing to adapt to that as well.

break;
}
surface = SDL_ConvertSurfaceFormat(surface, newFormat, 0);

MYGUI_ASSERT(surface != nullptr, "Failed to convert image: " + fullname);

texture = SDL_CreateTextureFromSurface(mRenderer, surface);
SDL_FreeSurface(surface);

return texture;
}

void BaseManager::saveImage(int _width, int _height, MyGUI::PixelFormat _format, void* _texture, const std::string& _filename)
{
SDL_Surface* surface = SDL_CreateRGBSurface(0, _width, _height, _format.getBytesPerPixel() * 8, 0, 0, 0, 0);
std::memcpy(surface->pixels, _texture, _width * _height * _format.getBytesPerPixel());
IMG_SavePNG(surface, _filename.c_str());
}
}
34 changes: 34 additions & 0 deletions Common/Base/SDL2/BaseManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "Base/PlatformBaseManager/SdlBaseManager.h"

#include <MyGUI_SDL2Platform.h>
#include <MyGUI_SDL2ImageLoader.h>

namespace base
{

class BaseManager :
public SdlBaseManager,
public MyGUI::SDL2ImageLoader
{
public:
BaseManager() : SdlBaseManager(false) { }
bool createRender(int _width, int _height, bool _windowed) override;
void destroyRender() override;
void drawOneFrame() override;
void resizeRender(int _width, int _height) override;
void addResourceLocation(const std::string& _name, bool _recursive = false) override;
void createGuiPlatform() override;
void destroyGuiPlatform() override;

// /*internal:*/
SDL_Texture* loadTexture(const std::string& _filename) override;
void saveImage(int _width, int _height, MyGUI::PixelFormat _format, void* _texture, const std::string& _filename) override;

private:
MyGUI::SDL2Platform* mPlatform = nullptr;
SDL_Renderer *mRenderer = nullptr;
};

}
6 changes: 6 additions & 0 deletions Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ elseif(MYGUI_RENDERSYSTEM EQUAL 8)
)
link_directories(${OPENGL_LIB_DIR})
link_directories(${SDL2_IMAGE_LIB_DIR})
elseif(MYGUI_RENDERSYSTEM EQUAL 9)
add_definitions("-DMYGUI_SDL2_PLATFORM")
include_directories(SYSTEM
${SDL2_IMAGE_INCLUDE_DIRS}
)
link_directories(${SDL2_IMAGE_LIB_DIR})
endif()

include_directories(Input/SDL)
Expand Down
22 changes: 22 additions & 0 deletions Platforms/SDL2/SDL2Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set(PROJECTNAME MyGUI.SDL2Platform)

include_directories(
include
${MYGUI_SOURCE_DIR}/MyGUIEngine/include
${MYGUI_SOURCE_DIR}/Common
${SDL2_INCLUDE_DIRS}
)

include(${PROJECTNAME}.list)

add_library(${PROJECTNAME} ${HEADER_FILES} ${SOURCE_FILES})

add_dependencies(${PROJECTNAME} MyGUIEngine)

target_link_libraries(${PROJECTNAME} MyGUIEngine ${SDL2_LIBRARIES})

# installation rules
install(FILES ${HEADER_FILES}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/MYGUI"
)
mygui_install_target(${PROJECTNAME} "")
28 changes: 28 additions & 0 deletions Platforms/SDL2/SDL2Platform/MyGUI.SDL2Platform.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set (HEADER_FILES
include/MyGUI_SDL2DataManager.h
include/MyGUI_SDL2Diagnostic.h
include/MyGUI_SDL2Platform.h
include/MyGUI_SDL2RenderManager.h
include/MyGUI_SDL2Texture.h
include/MyGUI_SDL2VertexBuffer.h
)
set (SOURCE_FILES
src/MyGUI_SDL2DataManager.cpp
src/MyGUI_SDL2RenderManager.cpp
src/MyGUI_SDL2Texture.cpp
src/MyGUI_SDL2VertexBuffer.cpp
)
SOURCE_GROUP("Header Files" FILES
include/MyGUI_SDL2DataManager.h
include/MyGUI_SDL2Diagnostic.h
include/MyGUI_SDL2Platform.h
include/MyGUI_SDL2RenderManager.h
include/MyGUI_SDL2Texture.h
include/MyGUI_SDL2VertexBuffer.h
)
SOURCE_GROUP("Source Files" FILES
src/MyGUI_SDL2DataManager.cpp
src/MyGUI_SDL2RenderManager.cpp
src/MyGUI_SDL2Texture.cpp
src/MyGUI_SDL2VertexBuffer.cpp
)
Loading