GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Repaint_raylib/src/layerstack.cpp at main · EugeneDevastator/Repaint_raylib Expand file tree/ Copy pathlayerstack.cppMore file actions 334 lines (304 loc) · 14.2 KB / Copy pathlayerstack.cppFile metadata and controls334 lines (304 loc) · 14.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 #include "layerstack.h" #include "undo.h" #include "render_utils.h" #include "RaylibUtils.h" #include "texture_manager.h" #include "rlgl.h" #include "external/glad.h" #include <math.h> #include <string.h> // ── Internal state ──────────────────────────────────────────────────── static struct { sLayerProps* prop; RenderTexture2D* rt; TexSlotID* slotID; Quad* quads; // xform + rt as one unit (mirrors prop/rt) int count; Quad canvas; // canvas as first-class Quad (xform + rt) TexSlotID canvasSlot; float canvasView[6]; } LS; // ── Helper: unload a single slot's GPU/CPU resources (no array shift) ── static void UnloadLayerSlotResources(int idx) { if (TM_IsValid(LS.slotID[idx])) { bool shared = false; TexSlotID sid = LS.slotID[idx]; for (int j = 0; j < LS.count; j++) { if (j != idx && LS.slotID[j].bucket == sid.bucket && LS.slotID[j].slot == sid.slot) { shared = true; break; } } if (!shared) { TM_Remove(LS.slotID[idx]); } LS.slotID[idx] = TM_INVALID_SLOT; } bool shared = false; if(LS.rt[idx].id>0) { for(int j=0;j<LS.count;j++) { if(j!=idx && LS.rt[j].id==LS.rt[idx].id) { shared=true; break; } } } if(!shared) { if(LS.rt[idx].id>0) UnloadRenderTexture(LS.rt[idx]); } } // ── Helper: shift slot array entries up ── static void ShiftLayersUp(int from, int to) { for(int i=from;i>to;i--){ LS.prop[i]=LS.prop[i-1]; LS.rt[i]=LS.rt[i-1]; LS.slotID[i]=LS.slotID[i-1]; LS.quads[i]=LS.quads[i-1]; } } // ── Helper: shift slot array entries down ── static void ShiftLayersDown(int from, int to) { for(int i=from;i<to;i++){ LS.prop[i]=LS.prop[i+1]; LS.rt[i]=LS.rt[i+1]; LS.slotID[i]=LS.slotID[i+1]; LS.quads[i]=LS.quads[i+1]; } } // ── Helper: init a new slot with a blank image/RT ── static void InitLayerSlot(int idx, int w, int h) { LS.rt[idx]=Load16BitRT(w,h); SetTextureFilter(LS.rt[idx].texture, TEXTURE_FILTER_BILINEAR); BeginTextureMode(LS.rt[idx]); ClearBackground(BLANK); EndTextureMode(); LS.prop[idx]={}; LS.prop[idx].op=1; LS.prop[idx].visible=true; LS.prop[idx].blendmode=bmGamma; // Quad — xform mirrors canvas, RT is the layer texture LS.quads[idx].xform = LS.canvas.xform; if (LS.canvas.xform.ww <= 0.0f || LS.canvas.xform.wh <= 0.0f) { LS.quads[idx].xform.ww = (float)w; LS.quads[idx].xform.wh = (float)h; } LS.quads[idx].xform.mat[0]=1; LS.quads[idx].xform.mat[1]=0; LS.quads[idx].xform.mat[2]=0; LS.quads[idx].xform.mat[3]=0; LS.quads[idx].xform.mat[4]=1; LS.quads[idx].xform.mat[5]=0; LS.quads[idx].rt = LS.rt[idx]; // Backward compat: prop.xform mirrors quad.xform LS.prop[idx].xform = LS.quads[idx].xform; LS.prop[idx].threshold=0; LS.prop[idx].feather=1; char name[64]; snprintf(name, sizeof(name), "Layer %d", idx); LS.slotID[idx]=TM_Register(TM_BUCKET_LAYER, LS.rt[idx], name, false, w, h); } // ── Helper: remove a layer slot (unload resources + shift down) ── static void RemoveLayerSlot(int idx) { UnloadLayerSlotResources(idx); ShiftLayersDown(idx,LS.count-1); LS.count--; } // ── Init / shutdown ────────────────────────────────────────────────── void LayerStack_Init(void) { LS = {0}; LS.canvasView[0]=1; LS.canvasView[4]=1; } void LayerStack_Shutdown(void) { for(int i=0;i<LS.count;i++) UnloadLayerSlotResources(i); free(LS.prop); free(LS.rt); free(LS.slotID); free(LS.quads); if(LS.canvas.rt.id>0) UnloadRenderTexture(LS.canvas.rt); LS.canvas.rt={0}; LS.canvasSlot=TM_INVALID_SLOT; LS={0}; } void LayerStack_InitCanvas(int w, int h) { if(LS.canvas.rt.id>0) UnloadRenderTexture(LS.canvas.rt); LS.canvas.rt=Load16BitRT(w,h); if(LS.canvas.rt.id>0) LS.canvasSlot=TM_Register(TM_BUCKET_LAYER, LS.canvas.rt, "Canvas", true, w, h); } void LayerStack_SetCanvasXform(const RectXform* xf) { if (xf) LS.canvas.xform = *xf; } void LayerStack_ResizeCanvas(int newW, int newH) { if(LS.canvas.rt.id==0||newW<1||newH<1) return; int oldW=(int)LS.canvas.rt.texture.width, oldH=(int)LS.canvas.rt.texture.height; if(oldW==newW&&oldH==newH) return; RenderTexture2D newRT=Load16BitRT(newW,newH); if(newRT.id==0) return; BeginTextureMode(newRT); ClearBackground(BLANK); rlSetBlendMode(RL_BLEND_CUSTOM); rlSetBlendFactors(RL_ONE,RL_ZERO,RL_FUNC_ADD); DrawTexturePro(LS.canvas.rt.texture, Rectangle{0,0,(float)oldW,(float)-oldH}, Rectangle{0,0,(float)newW,(float)newH}, Vector2{0,0}, 0.0f, WHITE); rlSetBlendMode(RL_BLEND_ALPHA); EndTextureMode(); if(TM_IsValid(LS.canvasSlot)) TM_Remove(LS.canvasSlot); UnloadRenderTexture(LS.canvas.rt); LS.canvas.rt=newRT; LS.canvasSlot=TM_Register(TM_BUCKET_LAYER, LS.canvas.rt, "Canvas", true, newW, newH); } Quad* LayerStack_GetCanvasQuadPtr(void) { return &LS.canvas; } Quad* LayerStack_GetQuadPtr(int i) { return (i>=0&&i<LS.count)?&LS.quads[i]:NULL; } RenderTexture2D LayerStack_GetCanvasRT(void) { return LS.canvas.rt; } RenderTexture2D* LayerStack_GetCanvasRTPtr(void) { return &LS.canvas.rt; } int LayerStack_RenderW(void) { return LS.canvas.rt.id>0?(int)LS.canvas.rt.texture.width:0; } int LayerStack_RenderH(void) { return LS.canvas.rt.id>0?(int)LS.canvas.rt.texture.height:0; } void LayerStack_SetCanvasView(const float mat[6]) { memcpy(LS.canvasView, mat, 6*sizeof(float)); } const float* LayerStack_GetCanvasView(void) { return LS.canvasView; } // ── Accessors ───────────────────────────────────────────────────────── int LayerStack_Count(void) { return LS.count; } sLayerProps* LayerStack_GetProps(int i){ return (i>=0&&i<LS.count)?&LS.prop[i]:NULL; } RenderTexture2D LayerStack_GetRT(int i) { return (i>=0&&i<LS.count)?LS.rt[i]:RenderTexture2D{0}; } TexSlotID LayerStack_GetSlotID(int i){ return (i>=0&&i<LS.count)?LS.slotID[i]:TM_INVALID_SLOT; } int LayerStack_FindLayerBySlot(TexSlotID slot) { if (!TM_IsValid(slot)) return -1; for (int i = 0; i < LS.count; i++) if (LS.slotID[i].bucket == slot.bucket && LS.slotID[i].slot == slot.slot) return i; return -1; } // ── Internal array resize ──────────────────────────────────────────── static void ReallocArrays(int n) { LS.prop=(sLayerProps*)realloc(LS.prop,n*sizeof(sLayerProps)); LS.rt=(RenderTexture2D*)realloc(LS.rt,n*sizeof(RenderTexture2D)); LS.slotID=(TexSlotID*)realloc(LS.slotID,n*sizeof(TexSlotID)); LS.quads=(Quad*)realloc(LS.quads,n*sizeof(Quad)); if(n>LS.count){ memset(&LS.rt[LS.count],0,(n-LS.count)*sizeof(RenderTexture2D)); for(int i=LS.count;i<n;i++) LS.slotID[i]=TM_INVALID_SLOT; } } // ── Layer management ───────────────────────────────────────────────── int LayerStack_Add(int w, int h) { int idx=LS.count; ReallocArrays(idx+1); InitLayerSlot(idx,w,h); LS.count++; return idx; } int LayerStack_InsertLayer(int afterIdx) { int idx=afterIdx<0?0:afterIdx>LS.count?LS.count:afterIdx; int cw=LayerStack_RenderW()>0?LayerStack_RenderW():512; int ch=LayerStack_RenderH()>0?LayerStack_RenderH():512; ReallocArrays(LS.count+1); ShiftLayersUp(LS.count,idx); InitLayerSlot(idx,cw,ch); LS.count++; return idx; } void LayerStack_DeleteLayer(int idx) { if(idx<0||idx>=LS.count||LS.count<=1) return; TexSlotID sid = LS.slotID[idx]; RemoveLayerSlot(idx); if (g_undoManager && TM_IsValid(sid)) g_undoManager->InvalidateSlot(sid); } void LayerStack_DuplicateLayer(int idx) { if(idx<0||idx>=LS.count) return; int n=LS.count; ReallocArrays(n+1); ShiftLayersUp(n,idx+1); int di=idx+1; LS.prop[di]=LS.prop[idx]; int lw=GetLayerWpx(idx), lh=GetLayerHpx(idx); LS.rt[di]=Load16BitRT(lw,lh); LS.quads[di].xform = LS.quads[idx].xform; LS.quads[di].rt = LS.rt[di]; BeginTextureMode(LS.rt[di]); rlSetBlendMode(RL_BLEND_CUSTOM); rlSetBlendFactors(RL_ONE,RL_ZERO,RL_FUNC_ADD); ClearBackground(BLANK); DrawTextureRec(LS.rt[idx].texture,Rectangle{0,0,(float)lw,(float)-lh},Vector2{0,0},WHITE); rlSetBlendMode(RL_BLEND_ALPHA); EndTextureMode(); char name[64]; snprintf(name, sizeof(name), "Layer %d (dup)", di); LS.slotID[di]=TM_Register(TM_BUCKET_LAYER, LS.rt[di], name, false, lw, lh); LS.count++; } void LayerStack_DuplicateAsInstance(int idx) { if(idx<0||idx>=LS.count) return; int n=LS.count; ReallocArrays(n+1); ShiftLayersUp(n,idx+1); int di=idx+1; LS.prop[di]=LS.prop[idx]; LS.prop[di].instanced=true; snprintf(LS.prop[di].layerName, sizeof(LS.prop[di].layerName), "%s(INST)", LS.prop[idx].layerName); LS.rt[di]=LS.rt[idx]; LS.quads[di]=LS.quads[idx]; LS.slotID[di]=LS.slotID[idx]; TM_AddRef(LS.slotID[idx]); LS.count++; } void LayerStack_MoveLayer(int from, int to) { if(from<0||from>=LS.count||to<0||to>=LS.count||from==to) return; Quad mvQuad=LS.quads[from]; sLayerProps mvProp=LS.prop[from]; TexSlotID mvSlot=LS.slotID[from]; if(from<to) ShiftLayersDown(from,to); else ShiftLayersUp(from,to); LS.prop[to]=mvProp; LS.rt[to]=mvQuad.rt; LS.quads[to]=mvQuad; LS.slotID[to]=mvSlot; } // ── GPU ↔ CPU transfer ──────────────────────────────────────────────── Image LayerStack_ReadFromGPU(int idx) { Image cap = {0}; if(idx>=0&&idx<LS.count&&LS.rt[idx].id>0){ cap=LoadImageFromTexture(LS.rt[idx].texture); ImageFlipVertical(&cap); if(cap.format!=PIXELFORMAT_UNCOMPRESSED_R16G16B16A16){ int px=cap.width*cap.height; uint16_t*d=(uint16_t*)malloc(px*4*2); uint8_t*s=(uint8_t*)cap.data; for(int i=0;i<px;i++){ d[i*4]=s[i*4]*257; d[i*4+1]=s[i*4+1]*257; d[i*4+2]=s[i*4+2]*257; d[i*4+3]=s[i*4+3]*257; } free(cap.data); cap.data=d; cap.format=PIXELFORMAT_UNCOMPRESSED_R16G16B16A16; } } return cap; } void LayerStack_UploadToGPU(int idx, Image img) { if(idx<0||idx>=LS.count||LS.rt[idx].id==0||!img.data) return; Texture2D tmp = LoadTextureFromImage(img); BeginTextureMode(LS.rt[idx]); ClearBackground(BLANK); rlSetBlendMode(RL_BLEND_CUSTOM); rlSetBlendFactors(RL_ONE,RL_ZERO,RL_FUNC_ADD); DrawTexture(tmp, 0, 0, WHITE); rlSetBlendMode(RL_BLEND_ALPHA); EndTextureMode(); UnloadTexture(tmp); UnloadImage(img); } // ── Single-layer bake ──────────────────────────────────────────────── void LayerStack_BakeSingleLayer(int idx, RenderTexture2D dst) { if(idx<0||idx>=LS.count||LS.rt[idx].id==0||dst.id==0) return; BeginTextureMode(dst); ClearBackground(BLANK); rlSetBlendMode(RL_BLEND_CUSTOM); rlSetBlendFactors(RL_ONE,RL_ZERO,RL_FUNC_ADD); DrawTextureRec(LS.rt[idx].texture, Rectangle{0,0,(float)GetLayerWpx(idx),(float)-GetLayerHpx(idx)}, Vector2{0,0}, WHITE); rlSetBlendMode(RL_BLEND_ALPHA); EndTextureMode(); } // ── Scene bounds ───────────────────────────────────────────────────── bool LayerStack_GetSceneBounds(Rectangle* out) { if(!out) return false; bool any=false; Rectangle aabb={0}; for(int i=0;i<LS.count;i++){ if(!LS.prop[i].visible||LS.rt[i].id==0) continue; Rectangle layerBB = GetWorldAABB(&LS.prop[i].xform); if(!any){ aabb=layerBB; any=true; } else{ float l=fminf(aabb.x,layerBB.x), t=fminf(aabb.y,layerBB.y); float r=fmaxf(aabb.x+aabb.width,layerBB.x+layerBB.width); float b=fmaxf(aabb.y+aabb.height,layerBB.y+layerBB.height); aabb.x=l; aabb.y=t; aabb.width=r-l; aabb.height=b-t; } } *out=aabb; return any; } // ── Canvas-window commit ───────────────────────────────────────────── void LayerStack_BakeCanvasWindow(const Document* doc) { (void)doc; for(int i=0;i<LS.count;i++){ if(LS.rt[i].id==0) continue; float cmb[6]; Xform_Mul(cmb, LS.canvasView, LS.prop[i].xform.mat); memcpy(LS.prop[i].xform.mat, cmb, 6*sizeof(float)); LS.quads[i].xform = LS.prop[i].xform; // sync quad } Xform_Identity(LS.canvasView); } // ── Resize a layer's texture ────────────────────────────────────────── void LayerStack_ResizeLayer(int idx, int newW, int newH) { if(idx<0||idx>=LS.count||LS.rt[idx].id==0||newW<1||newH<1) return; int oldW = GetLayerWpx(idx), oldH = GetLayerHpx(idx); if(oldW==newW && oldH==newH) return; RenderTexture2D newRT = Load16BitRT(newW, newH); if(newRT.id==0) return; BeginTextureMode(newRT); ClearBackground(BLANK); rlSetBlendMode(RL_BLEND_CUSTOM); rlSetBlendFactors(RL_ONE, RL_ZERO, RL_FUNC_ADD); DrawTexturePro(LS.rt[idx].texture, Rectangle{0,0,(float)oldW,(float)-oldH}, Rectangle{0,0,(float)newW,(float)newH}, Vector2{0,0}, 0.0f, WHITE); rlSetBlendMode(RL_BLEND_ALPHA); EndTextureMode(); // Replace the slot — register new RT, unregister old { char name[64]; snprintf(name, sizeof(name), "Layer %d", idx); TexSlotID newSlot = TM_Register(TM_BUCKET_LAYER, newRT, name, false, newW, newH); if(newSlot.slot >= 0) { TM_Remove(LS.slotID[idx]); RenderTexture2D oldRT = LS.rt[idx]; LS.rt[idx] = newRT; LS.quads[idx].rt = newRT; // sync quad LS.slotID[idx] = newSlot; UnloadRenderTexture(oldRT); } else { UnloadRenderTexture(newRT); } } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |