Parcourir la source

Resourcen Verwaltung hinzugefügt

Kolja Strohm il y a 5 ans
Parent
commit
31ce7de46c

+ 8 - 0
StickmanWorldOnline/GameObject.h

@@ -1,11 +1,18 @@
 #pragma once
 
 #include "Variablen.h"
+#include <Bild.h>
+
+
+using namespace Framework;
+
 
 class GameObject : public Variable
 {
 protected:
     float x, y, w, h;
+    Bild *zTextur;
+    bool texturScale;
 
 public:
     GameObject( VariableTyp typ, int x, int y, int width, int height );
@@ -14,6 +21,7 @@ public:
     void setWidth( float width );
     void setHeight( float height );
     bool intersectsWith( GameObject *zObj );
+    virtual void render( Bild &rObj );
     float getX() const;
     float getY() const;
     float getWidth() const;

+ 23 - 0
StickmanWorldOnline/GameObjekt.cpp

@@ -9,6 +9,8 @@ GameObject::GameObject( VariableTyp typ, int x, int y, int width, int height )
     this->y = (float)y;
     w = (float)width;
     h = (float)height;
+    zTextur = 0;
+    texturScale = 0;
 }
 
 void GameObject::setX( float x )
@@ -36,6 +38,27 @@ bool GameObject::intersectsWith( GameObject *zObj )
     return x < zObj->x + zObj->w && x + w > zObj->x && y < zObj->y + zObj->h && y + h > zObj->y;
 }
 
+void GameObject::render( Bild &rObj )
+{
+    if( zTextur )
+    {
+        if( rObj.setDrawOptions( (int)x, (int)y, (int)w, (int)h ) )
+        {
+            if( texturScale )
+                rObj.alphaBildSkall( (int)x, (int)y, (int)w, (int)h, *zTextur );
+            else
+            {
+                for( int xx = (int)x; xx < x + w; x += zTextur->getBreite() )
+                {
+                    for( int yy = (int)y; yy < y + h; y += zTextur->getHeight() )
+                        rObj.alphaBild( xx, yy, zTextur->getBreite(), zTextur->getHeight(), *zTextur );
+                }
+            }
+            rObj.releaseDrawOptions();
+        }
+    }
+}
+
 float GameObject::getX() const
 {
     return x;

+ 172 - 0
StickmanWorldOnline/Resource.cpp

@@ -0,0 +1,172 @@
+#include "Resource.h"
+#include <DateiSystem.h>
+
+
+ColorMode::ColorMode()
+{
+    ref = 1;
+}
+
+Bild *ColorMode::colorImage( Bild *zImg, int color )
+{
+    Bild *result = new Bild();
+    result->neuBild( zImg->getBreite(), zImg->getHeight(), 0 );
+    result->drawBild( 0, 0, zImg->getBreite(), zImg->getHeight(), *zImg );
+    return result;
+}
+
+ColorMode *ColorMode::getThis()
+{
+    ref++;
+    return this;
+}
+
+ColorMode *ColorMode::release()
+{
+    if( !--ref )
+        delete this;
+    return 0;
+}
+
+
+AlphaColorMode::AlphaColorMode( unsigned char alpha )
+{
+    this->alpha = alpha;
+}
+
+Bild *AlphaColorMode::colorImage( Bild *zImg, int color )
+{
+    Bild *result = ColorMode::colorImage( zImg, color );
+    result->alphaRegion( 0, 0, result->getBreite(), result->getHeight(), ( (int)alpha << 24 ) | ( color & 0xFFFFFF ) );
+    return result;
+}
+
+
+Bild *MaskColorMode::colorImage( Bild *zImg, int color )
+{
+    Bild *result = ColorMode::colorImage( zImg, color );
+    int *buffer = result->getBuffer();
+    int size = result->getBreite() * result->getHeight();
+    for( int i = 0; i < size; i++ )
+        buffer[ i ] = ( buffer[ i ] & 0xFF000000 ) | ( color & 0xFFFFFF );
+    return result;
+}
+
+
+Resource::Resource( ResourceIds id, int color )
+{
+    this->id = id;
+    this->color = color;
+    ref = 1;
+}
+
+Resource *Resource::createColoredResource( int color, ColorMode *mode ) const
+{
+    Resource *r = new Resource( id, color );
+    for( auto i = images.getIterator(); i; i++ )
+        r->images.add( mode->colorImage( i, color ) );
+    mode->release();
+    return r;
+}
+
+Iterator< Bild * > Resource::getImages() const
+{
+    return images.getIterator();
+}
+
+ResourceIds Resource::getId() const
+{
+    return id;
+}
+
+int Resource::getColor() const
+{
+    return color;
+}
+
+Resource *Resource::getThis()
+{
+    ref++;
+    return this;
+}
+
+Resource *Resource::release()
+{
+    if( !--ref )
+        delete this;
+    return 0;
+}
+
+
+ResourceRegistry::ResourceRegistry()
+{
+    ref = 1;
+}
+
+Resource *ResourceRegistry::getResource( ResourceIds id, int color, ColorMode *mode, Text path )
+{
+    Resource *r = zResource( id, color, mode, path );
+    return r ? r->getThis() : 0;
+}
+
+Resource *ResourceRegistry::zResource( ResourceIds id, int color, ColorMode *mode, Text path = "" )
+{
+    for( auto r = resources.getIterator(); r; r++ )
+    {
+        if( r->getId() == id && r->getColor() == color )
+        {
+            mode->release();
+            return r;
+        }
+    }
+    for( auto r = resources.getIterator(); r; r++ )
+    {
+        if( r->getId() == id )
+        {
+            Resource *nr = r->createColoredResource( color, mode );
+            resources.add( nr );
+            return nr;
+        }
+    }
+    if( path.hat( ".ltdb/" ) && path.positionVon( "/", path.anzahlVon( "/" ) - 1 ) == path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 )
+    {
+        LTDBDatei dat;
+        dat.setDatei( path.getTeilText( 0, path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 ) );
+        Bild *b = dat.laden( 0, path.getTeilText( path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 6 ) );
+        if( b )
+        {
+            Resource *r = new Resource( id, color );
+            resources.add( r );
+            r->images.add( mode->colorImage( b, color ) );
+            b->release();
+            mode->release();
+            return r;
+        }
+        mode->release();
+    }
+    Resource *r = new Resource( id, color );
+    resources.add( r );
+    LTDBDatei dat;
+    dat.setDatei( path.getThis() );
+    for( int i = 0; i < dat.getBildAnzahl(); i++ )
+    {
+        Bild *b = dat.laden( 0, dat.zBildListe()->get( i ) );
+        r->images.add( mode->colorImage( b, color ) );
+        b->release();
+    }
+    mode->release();
+    return r;
+}
+
+ResourceRegistry *ResourceRegistry::getThis()
+{
+    ref++;
+    return this;
+}
+
+ResourceRegistry *ResourceRegistry::release()
+{
+    if( !--ref )
+        delete this;
+    return 0;
+}

+ 109 - 0
StickmanWorldOnline/Resource.h

@@ -0,0 +1,109 @@
+#pragma once
+
+#include <Bild.h>
+
+using namespace Framework;
+
+class ColorMode
+{
+private:
+    int ref;
+
+public:
+    ColorMode();
+    virtual Bild *colorImage( Bild *zImg, int color );
+    ColorMode *getThis();
+    ColorMode *release();
+};
+
+class AlphaColorMode : public ColorMode
+{
+private:
+    unsigned char alpha;
+
+public:
+    AlphaColorMode( unsigned char alpha );
+    Bild *colorImage( Bild *zImg, int color ) override;
+};
+
+class MaskColorMode : public ColorMode
+{
+public:
+    Bild *colorImage( Bild *zImg, int color ) override;
+};
+
+enum ResourceIds
+{
+    R_BARIERE,
+    R_BASE,
+    R_PFEIL_GESCHOSS,
+    R_PFEIL,
+    R_LEBEL,
+    R_SCHILD,
+    R_SCHUH,
+    R_GEIST,
+    R_ROLLE,
+    R_ROLLE_OBEN,
+    R_ROLLE_LINKS,
+    R_ROLLE_RECHTS,
+    R_ROLLE_UNTEN,
+    R_STURM,
+    R_STURM_OBEN,
+    R_STURM_LINKS,
+    R_STURM_RECHTS,
+    R_STUMR_UNTEN,
+    R_DRACHENAUGE,
+    R_FEUERBALL,
+    R_FEUERBALL_TREFFER,
+    R_ENTERHAKEN_SEIL,
+    R_ENTERHAKEN_SPITZE,
+    R_ENTERHAKEN_ITEM,
+    R_MINE,
+    R_RWEISHEIT,
+    R_RBOSHEIT,
+    R_RLEBEN,
+    R_RTEMPO,
+    R_SCHALTER,
+    R_SCHIENE,
+    R_SPIELER_STIRBD,
+    R_SPIELER,
+    R_SPIELER_RECHTS,
+    R_SPIELER_LINKS,
+    R_TUNNEL,
+    R_UMLENKUNG
+};
+
+class ResourceRegistry;
+
+class Resource
+{
+private:
+    ResourceIds id;
+    int color;
+    int ref;
+    RCArray< Bild > images;
+
+public:
+    Resource( ResourceIds id, int color );
+    virtual Resource *createColoredResource( int color, ColorMode *mode ) const;
+    Iterator< Bild * > getImages() const;
+    ResourceIds getId() const;
+    int getColor() const;
+    Resource *getThis();
+    Resource *release();
+    friend ResourceRegistry;
+};
+
+class ResourceRegistry
+{
+private:
+    RCArray< Resource > resources;
+    int ref;
+
+public:
+    ResourceRegistry();
+    Resource *getResource( ResourceIds id, int color, ColorMode *mode, Text path = "" );
+    Resource *zResource( ResourceIds id, int color, ColorMode *mode, Text path = "" );
+    ResourceRegistry *getThis();
+    ResourceRegistry *release();
+};

+ 2 - 0
StickmanWorldOnline/StickmanWorldOnline.vcxproj

@@ -39,6 +39,7 @@
     <ClCompile Include="Leben.cpp" />
     <ClCompile Include="LebenRune.cpp" />
     <ClCompile Include="Reader.cpp" />
+    <ClCompile Include="Resource.cpp" />
     <ClCompile Include="Rolle.cpp" />
     <ClCompile Include="Schalter.cpp" />
     <ClCompile Include="Schiene.cpp" />
@@ -87,6 +88,7 @@
     <ClInclude Include="Leben.h" />
     <ClInclude Include="LebenRune.h" />
     <ClInclude Include="Reader.h" />
+    <ClInclude Include="Resource.h" />
     <ClInclude Include="RightTools.h" />
     <ClInclude Include="Rolle.h" />
     <ClInclude Include="Schalter.h" />

+ 9 - 0
StickmanWorldOnline/StickmanWorldOnline.vcxproj.filters

@@ -25,6 +25,9 @@
     <Filter Include="Spiel\Objekte">
       <UniqueIdentifier>{8fe817ac-57bb-4131-bae3-65f2d7e56d8f}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Resources">
+      <UniqueIdentifier>{c7883eee-d9b9-4f59-a0e6-d567836f0a90}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Ende.cpp">
@@ -143,6 +146,9 @@
     <ClCompile Include="Spiel.cpp">
       <Filter>Spiel</Filter>
     </ClCompile>
+    <ClCompile Include="Resource.cpp">
+      <Filter>Resources</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Aufzeichnung.h">
@@ -290,5 +296,8 @@
     <ClInclude Include="Umlenkung.h">
       <Filter>Spiel\Objekte</Filter>
     </ClInclude>
+    <ClInclude Include="Resource.h">
+      <Filter>Resources</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>