| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "renderSoftware.h" |
| 21 |
#include "imgui_sw.hpp" |
| 22 |
#include "../../ta-log.h" |
| 23 |
|
| 24 |
class FurnaceSoftwareTexture: public FurnaceGUITexture { |
| 25 |
public: |
| 26 |
SWTexture* tex; |
| 27 |
FurnaceGUITextureFormat format; |
| 28 |
FurnaceSoftwareTexture(): |
| 29 |
tex(NULL), |
| 30 |
format(GUI_TEXFORMAT_UNKNOWN) {} |
| 31 |
}; |
| 32 |
|
| 33 |
ImTextureID FurnaceGUIRenderSoftware::getTextureID(FurnaceGUITexture* which) { |
| 34 |
FurnaceSoftwareTexture* t=(FurnaceSoftwareTexture*)which; |
| 35 |
return (ImTextureID)t->tex; |
| 36 |
} |
| 37 |
|
| 38 |
FurnaceGUITextureFormat FurnaceGUIRenderSoftware::getTextureFormat(FurnaceGUITexture* which) { |
| 39 |
FurnaceSoftwareTexture* t=(FurnaceSoftwareTexture*)which; |
| 40 |
return t->format; |
| 41 |
} |
| 42 |
|
| 43 |
bool FurnaceGUIRenderSoftware::lockTexture(FurnaceGUITexture* which, void** data, int* pitch) { |
| 44 |
FurnaceSoftwareTexture* t=(FurnaceSoftwareTexture*)which; |
| 45 |
if (!t->tex->managed) return false; |
| 46 |
*data=t->tex->pixels; |
| 47 |
*pitch=t->tex->width*(t->tex->isAlpha?1:4); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
bool FurnaceGUIRenderSoftware::unlockTexture(FurnaceGUITexture* which) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
bool FurnaceGUIRenderSoftware::updateTexture(FurnaceGUITexture* which, void* data, int pitch) { |
| 56 |
FurnaceSoftwareTexture* t=(FurnaceSoftwareTexture*)which; |
| 57 |
if (!t->tex->managed) return false; |
| 58 |
memcpy(t->tex->pixels,data,pitch*t->tex->height); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
FurnaceGUITexture* FurnaceGUIRenderSoftware::createTexture(bool dynamic, int width, int height, bool interpolate, FurnaceGUITextureFormat format) { |
| 63 |
if (format!=GUI_TEXFORMAT_ARGB32) { |
| 64 |
logE("unsupported texture format!"); |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
FurnaceSoftwareTexture* ret=new FurnaceSoftwareTexture; |
| 68 |
ret->tex=new SWTexture(width,height); |
| 69 |
ret->format=format; |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
bool FurnaceGUIRenderSoftware::destroyTexture(FurnaceGUITexture* which) { |
| 74 |
FurnaceSoftwareTexture* t=(FurnaceSoftwareTexture*)which; |
| 75 |
|
| 76 |
delete t->tex; |
| 77 |
delete t; |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
void FurnaceGUIRenderSoftware::setTextureBlendMode(FurnaceGUITexture* which, FurnaceGUIBlendMode mode) { |
| 82 |
// TODO |
| 83 |
} |
| 84 |
|
| 85 |
void FurnaceGUIRenderSoftware::setBlendMode(FurnaceGUIBlendMode mode) { |
| 86 |
// TODO |
| 87 |
} |
| 88 |
|
| 89 |
void FurnaceGUIRenderSoftware::clear(ImVec4 color) { |
| 90 |
SDL_Surface* surf=SDL_GetWindowSurface(sdlWin); |
| 91 |
if (!surf) return; |
| 92 |
ImU32 clearToWhat=ImGui::ColorConvertFloat4ToU32(color); |
| 93 |
clearToWhat=(clearToWhat&0xff00ff00)|((clearToWhat&0xff)<<16)|((clearToWhat&0xff0000)>>16); |
| 94 |
|
| 95 |
bool mustLock=SDL_MUSTLOCK(surf); |
| 96 |
if (mustLock) { |
| 97 |
if (SDL_LockSurface(surf)!=0) return; |
| 98 |
} |
| 99 |
unsigned int* pixels=(unsigned int*)surf->pixels; |
| 100 |
for (size_t total=surf->w*surf->h; total; total--) { |
| 101 |
*(pixels++)=clearToWhat; |
| 102 |
} |
| 103 |
if (mustLock) { |
| 104 |
SDL_UnlockSurface(surf); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void FurnaceGUIRenderSoftware::newFrame() { |
| 109 |
ImGui_ImplSW_NewFrame(); |
| 110 |
} |
| 111 |
|
| 112 |
bool FurnaceGUIRenderSoftware::canVSync() { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
void FurnaceGUIRenderSoftware::renderGUI() { |
| 117 |
ImGui_ImplSW_RenderDrawData(ImGui::GetDrawData()); |
| 118 |
} |
| 119 |
|
| 120 |
void FurnaceGUIRenderSoftware::wipe(float alpha) { |
| 121 |
// TODO |
| 122 |
} |
| 123 |
|
| 124 |
void FurnaceGUIRenderSoftware::present() { |
| 125 |
SDL_UpdateWindowSurface(sdlWin); |
| 126 |
} |
| 127 |
|
| 128 |
bool FurnaceGUIRenderSoftware::getOutputSize(int& w, int& h) { |
| 129 |
SDL_Surface* surf=SDL_GetWindowSurface(sdlWin); |
| 130 |
if (surf==NULL) return false; |
| 131 |
w=surf->w; |
| 132 |
h=surf->h; |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
int FurnaceGUIRenderSoftware::getWindowFlags() { |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
int FurnaceGUIRenderSoftware::getMaxTextureWidth() { |
| 141 |
return 16384; |
| 142 |
} |
| 143 |
|
| 144 |
int FurnaceGUIRenderSoftware::getMaxTextureHeight() { |
| 145 |
return 16384; |
| 146 |
} |
| 147 |
|
| 148 |
unsigned int FurnaceGUIRenderSoftware::getTextureFormats() { |
| 149 |
return GUI_TEXFORMAT_ARGB32; |
| 150 |
} |
| 151 |
|
| 152 |
const char* FurnaceGUIRenderSoftware::getBackendName() { |
| 153 |
return "Software"; |
| 154 |
} |
| 155 |
|
| 156 |
const char* FurnaceGUIRenderSoftware::getVendorName() { |
| 157 |
return "emilk, JesusKrists and tildearrow"; |
| 158 |
} |
| 159 |
|
| 160 |
const char* FurnaceGUIRenderSoftware::getDeviceName() { |
| 161 |
return "imgui_sw Software Renderer"; |
| 162 |
} |
| 163 |
|
| 164 |
const char* FurnaceGUIRenderSoftware::getAPIVersion() { |
| 165 |
return "N/A"; |
| 166 |
} |
| 167 |
|
| 168 |
void FurnaceGUIRenderSoftware::setSwapInterval(int swapInterval) { |
| 169 |
} |
| 170 |
|
| 171 |
void FurnaceGUIRenderSoftware::preInit(const DivConfig& conf) { |
| 172 |
} |
| 173 |
|
| 174 |
bool FurnaceGUIRenderSoftware::init(SDL_Window* win, int swapInterval) { |
| 175 |
sdlWin=win; |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
void FurnaceGUIRenderSoftware::initGUI(SDL_Window* win) { |
| 180 |
// hack |
| 181 |
ImGui_ImplSDL2_InitForMetal(win); |
| 182 |
ImGui_ImplSW_Init(win); |
| 183 |
} |
| 184 |
|
| 185 |
void FurnaceGUIRenderSoftware::quitGUI() { |
| 186 |
ImGui_ImplSW_Shutdown(); |
| 187 |
} |
| 188 |
|
| 189 |
bool FurnaceGUIRenderSoftware::quit() { |
| 190 |
return true; |
| 191 |
} |