-
Notifications
You must be signed in to change notification settings - Fork 223
SDL2 platform #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
SDL2 platform #239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,8 @@ namespace base | |
| bool mWindowOn = false; | ||
| SDL_Keycode mKeyCode; | ||
| int mFpsCounter = 0; | ||
|
|
||
| double mScale = 0; | ||
| }; | ||
|
|
||
| } | ||
| 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); | ||
|
|
||
| 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"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| 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; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
| 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; | ||
| }; | ||
|
|
||
| } |
| 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} "") |
| 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 | ||
| ) |
There was a problem hiding this comment.
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:
To see the problem, just compile this branch without the changes in
SdlBaseManager.cpp.