<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Unvorhersehbare Fehler]]></title><description><![CDATA[<p>Hallo liebe &quot;Cplusplusler&quot;!</p>
<p>Ich hab angefangen unter Linux Code zum Darstellen von Sprites über einen von SDL bereitgestellten Framebuffer zu schreiben, da mir die Möglichkeiten die SDL selbst bietet nicht ausreichen und da ich auf diese Weise Erfahrung sammeln möchte.</p>
<p>Jetzt habe ich scheinbar Fehler mit der Speicherverwaltung und weiß nicht mehr weiter. Die Methode, die für das Übertragen von Bilddaten zuständig sein soll scheint zwar zunächst zu funktionieren, jedoch lässt sich eine Verschiebung erkennen, die sich durch das gesamte Bild zieht und am oberem Teil lassen sich wenige scheinbar willkürlich gesetzte Pixel erkennen. Da ich keinen Fehler im Algorithmus finden konnte, habe ich zum testen Code zum Prüfen des Indexes des Array hinzugefügt (und inzwischen wieder entfernt). Alle Werte lagen im richtigem Bereich. Die Frage ist also, was meinen Pointer verschoben haben kann. Wenn ich ihn mit Pointerarithmetik verschiebe, kommt tatsächlich das gewünschte Resultat zustande. Dabei möchte ich es aber nicht belassen.</p>
<p>Ich habe festgestellt, dass durch eine Veränderung im Code auch das Resultat beeinflusst werden kann. Ich habe eine Zeile hinzugefügt, die dies bewirkt.</p>
<p>Den Source habe ich hier hochgeladen, denn ich fand es wäre zu viel für das Forum:<br />
<a href="http://dl.dropbox.com/u/1155350/Problem-Source.zip" rel="nofollow">http://dl.dropbox.com/u/1155350/Problem-Source.zip</a></p>
<p>Die betroffene Zeile findet sich in main.cpp und ist unübersehbar gekennzeichnet. Zum kompilieren braucht es g++ und SDL. Für Linux habe ich das Script &quot;build&quot; erstellt.</p>
<p>Vielen Dank im Voraus!</p>
<p>Für alle die einen schnellen Blick drauf werfen wollen hier das wichtigste des Source:</p>
<pre><code class="language-cpp">#include &lt;stdint.h&gt;
#include &lt;iostream&gt;
#include &lt;SDL/SDL.h&gt;
#include &quot;Texture.cpp&quot;

#ifndef NULL
#define NULL 0
#endif

class TexExerpt
{
public:
	TexExerpt(Texture* Tex);
	TexExerpt(int W, int H);
	TexExerpt();
	Texture* tex;
	int x;
	int y;
	int w;
	int h;
	void Draw(TexExerpt* ex);
}; 

TexExerpt::TexExerpt(Texture* Tex)
{
	tex = Tex;
	x = 0; y = 0; w = tex-&gt;w; h = tex-&gt;h;
}

TexExerpt::TexExerpt(int W, int H)
{
	tex = new Texture(W, H);
	x = 0; y = 0; w = W; h = H;
}

TexExerpt::TexExerpt()
{
}

void TexExerpt::Draw(TexExerpt* ex)
{
	int startX = x;
	int startY = y;
	int endX = x+w;
	int endY = y+h;
	if (ex-&gt;x &lt; 0) startX += -ex-&gt;x;
	if (ex-&gt;y &lt; 0) startY += -ex-&gt;y;
	if (ex-&gt;x+ex-&gt;w &gt; ex-&gt;tex-&gt;w) endX -= ex-&gt;x+ex-&gt;w - ex-&gt;tex-&gt;w;
	if (ex-&gt;y+ex-&gt;h &gt; ex-&gt;tex-&gt;h) endY -= ex-&gt;y+ex-&gt;h - ex-&gt;tex-&gt;h;
	if (startX &lt; 0) startX = 0;
	if (startY &lt; 0) startY = 0;
	if (endX &gt; tex-&gt;w) endX = tex-&gt;w;
	if (endY &gt; tex-&gt;h) endY = tex-&gt;h;
	///////////////////////////////////////////////////////////////////////////////////////
	//std::cout &lt;&lt; &quot;test&quot; &lt;&lt; ex-&gt;tex-&gt;pixbuf[0] &lt;&lt; std::endl; ////Hier Ein-/Auskommentieren
	///////////////////////////////////////////////////////////////////////////////////////
	uint8_t* in = ex-&gt;tex-&gt;pixbuf;
	uint8_t* out = tex-&gt;pixbuf;
	for (int Y = startY; Y &lt; endY; Y++)
	{
		int exIndexBase = (Y-y) * ex-&gt;h;
		exIndexBase /= h;
		exIndexBase *= 4 * ex-&gt;tex-&gt;w;
		int index = (Y*tex-&gt;w+startX)*4;
		for (int X = startX; X &lt; endX; X++)
		{
			int tmp = (X-x)*ex-&gt;w; tmp /= w; tmp *= 4;
			int exIndex = exIndexBase + tmp;
			uint8_t op 	= in[exIndex+3];
			uint8_t aop	= 255 - op;
			out[index+2] = (out[index+2]*aop + in[exIndex+2]*op) / 255;
			out[index+1] = (out[index+1]*aop + in[exIndex+1]*op) / 255;
			out[index+0] = (out[index+0]*aop + in[exIndex+0]*op) / 255;
			index += 4;
		}
	}
}

class VideoOut : public Texture
{
public:
	SDL_Surface* win;
	VideoOut();
	VideoOut(int W, int H);
	~VideoOut();
	void SwapBuffers();
};

VideoOut::VideoOut() : Texture()
{
	win = SDL_SetVideoMode(0, 0, 32, SDL_SWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN);
	w = win-&gt;w;
	h = win-&gt;h;
	pixbuf = new uint8_t [w*h*4];
	deletePixbuf = true;
}

VideoOut::VideoOut(int W, int H) : Texture(W, H)
{
	win = SDL_SetVideoMode(W, H, 32, SDL_SWSURFACE | SDL_RESIZABLE);
}

VideoOut::~VideoOut()
{
	SDL_FreeSurface(win);
}

void VideoOut::SwapBuffers()
{
	int size = w*h;
	uint32_t* srf = (uint32_t*) (win-&gt;pixels);
	uint8_t* buf = pixbuf;
	int index;
	for (int n = 0; n &lt; size; n++)
	{
		srf[n] = SDL_MapRGB(win-&gt;format, buf[index+2], buf[index+1], buf[index+0]);
		index += 4;
	}
	SDL_UpdateRect(win, 0, 0, win-&gt;w, win-&gt;h);
}

#include &quot;Tiles.cpp&quot;

int main()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	VideoOut out (img_Tiles.w*4, img_Tiles.h);
	TexExerpt Tiles(&amp;img_Tiles);
	TexExerpt o(&amp;out);
	o.Draw(&amp;Tiles);
	out.SwapBuffers();
	SDL_Delay(2000);
}
</code></pre>
<p>Und hier noch Texture.cpp:</p>
<pre><code class="language-cpp">#ifndef Texture_cpp
#define Texture_cpp

class Texture
{
public:
	Texture();
	Texture(int W, int H);
	Texture(int W, int H, uint8_t* Pixbuf);
	~Texture();
	uint8_t* pixbuf;
	bool deletePixbuf;
	int w;
	int h;
};

Texture::Texture()
{
	pixbuf = NULL;
	deletePixbuf = false;
}

Texture::Texture(int W, int H)
{
	w = W;
	h = H;
	pixbuf = new uint8_t [w*h*4];
	deletePixbuf = true;
}

Texture::Texture(int W, int H, uint8_t* Pixbuf)
{
	w = W;
	h = H;
	pixbuf = Pixbuf;
	deletePixbuf = false;
}

Texture::~Texture()
{
	if (deletePixbuf) delete [] pixbuf;
}

#endif
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/306668/unvorhersehbare-fehler</link><generator>RSS for Node</generator><lastBuildDate>Fri, 07 Aug 2026 08:53:16 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306668.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Aug 2012 23:17:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unvorhersehbare Fehler on Fri, 03 Aug 2012 23:17:43 GMT]]></title><description><![CDATA[<p>Hallo liebe &quot;Cplusplusler&quot;!</p>
<p>Ich hab angefangen unter Linux Code zum Darstellen von Sprites über einen von SDL bereitgestellten Framebuffer zu schreiben, da mir die Möglichkeiten die SDL selbst bietet nicht ausreichen und da ich auf diese Weise Erfahrung sammeln möchte.</p>
<p>Jetzt habe ich scheinbar Fehler mit der Speicherverwaltung und weiß nicht mehr weiter. Die Methode, die für das Übertragen von Bilddaten zuständig sein soll scheint zwar zunächst zu funktionieren, jedoch lässt sich eine Verschiebung erkennen, die sich durch das gesamte Bild zieht und am oberem Teil lassen sich wenige scheinbar willkürlich gesetzte Pixel erkennen. Da ich keinen Fehler im Algorithmus finden konnte, habe ich zum testen Code zum Prüfen des Indexes des Array hinzugefügt (und inzwischen wieder entfernt). Alle Werte lagen im richtigem Bereich. Die Frage ist also, was meinen Pointer verschoben haben kann. Wenn ich ihn mit Pointerarithmetik verschiebe, kommt tatsächlich das gewünschte Resultat zustande. Dabei möchte ich es aber nicht belassen.</p>
<p>Ich habe festgestellt, dass durch eine Veränderung im Code auch das Resultat beeinflusst werden kann. Ich habe eine Zeile hinzugefügt, die dies bewirkt.</p>
<p>Den Source habe ich hier hochgeladen, denn ich fand es wäre zu viel für das Forum:<br />
<a href="http://dl.dropbox.com/u/1155350/Problem-Source.zip" rel="nofollow">http://dl.dropbox.com/u/1155350/Problem-Source.zip</a></p>
<p>Die betroffene Zeile findet sich in main.cpp und ist unübersehbar gekennzeichnet. Zum kompilieren braucht es g++ und SDL. Für Linux habe ich das Script &quot;build&quot; erstellt.</p>
<p>Vielen Dank im Voraus!</p>
<p>Für alle die einen schnellen Blick drauf werfen wollen hier das wichtigste des Source:</p>
<pre><code class="language-cpp">#include &lt;stdint.h&gt;
#include &lt;iostream&gt;
#include &lt;SDL/SDL.h&gt;
#include &quot;Texture.cpp&quot;

#ifndef NULL
#define NULL 0
#endif

class TexExerpt
{
public:
	TexExerpt(Texture* Tex);
	TexExerpt(int W, int H);
	TexExerpt();
	Texture* tex;
	int x;
	int y;
	int w;
	int h;
	void Draw(TexExerpt* ex);
}; 

TexExerpt::TexExerpt(Texture* Tex)
{
	tex = Tex;
	x = 0; y = 0; w = tex-&gt;w; h = tex-&gt;h;
}

TexExerpt::TexExerpt(int W, int H)
{
	tex = new Texture(W, H);
	x = 0; y = 0; w = W; h = H;
}

TexExerpt::TexExerpt()
{
}

void TexExerpt::Draw(TexExerpt* ex)
{
	int startX = x;
	int startY = y;
	int endX = x+w;
	int endY = y+h;
	if (ex-&gt;x &lt; 0) startX += -ex-&gt;x;
	if (ex-&gt;y &lt; 0) startY += -ex-&gt;y;
	if (ex-&gt;x+ex-&gt;w &gt; ex-&gt;tex-&gt;w) endX -= ex-&gt;x+ex-&gt;w - ex-&gt;tex-&gt;w;
	if (ex-&gt;y+ex-&gt;h &gt; ex-&gt;tex-&gt;h) endY -= ex-&gt;y+ex-&gt;h - ex-&gt;tex-&gt;h;
	if (startX &lt; 0) startX = 0;
	if (startY &lt; 0) startY = 0;
	if (endX &gt; tex-&gt;w) endX = tex-&gt;w;
	if (endY &gt; tex-&gt;h) endY = tex-&gt;h;
	///////////////////////////////////////////////////////////////////////////////////////
	//std::cout &lt;&lt; &quot;test&quot; &lt;&lt; ex-&gt;tex-&gt;pixbuf[0] &lt;&lt; std::endl; ////Hier Ein-/Auskommentieren
	///////////////////////////////////////////////////////////////////////////////////////
	uint8_t* in = ex-&gt;tex-&gt;pixbuf;
	uint8_t* out = tex-&gt;pixbuf;
	for (int Y = startY; Y &lt; endY; Y++)
	{
		int exIndexBase = (Y-y) * ex-&gt;h;
		exIndexBase /= h;
		exIndexBase *= 4 * ex-&gt;tex-&gt;w;
		int index = (Y*tex-&gt;w+startX)*4;
		for (int X = startX; X &lt; endX; X++)
		{
			int tmp = (X-x)*ex-&gt;w; tmp /= w; tmp *= 4;
			int exIndex = exIndexBase + tmp;
			uint8_t op 	= in[exIndex+3];
			uint8_t aop	= 255 - op;
			out[index+2] = (out[index+2]*aop + in[exIndex+2]*op) / 255;
			out[index+1] = (out[index+1]*aop + in[exIndex+1]*op) / 255;
			out[index+0] = (out[index+0]*aop + in[exIndex+0]*op) / 255;
			index += 4;
		}
	}
}

class VideoOut : public Texture
{
public:
	SDL_Surface* win;
	VideoOut();
	VideoOut(int W, int H);
	~VideoOut();
	void SwapBuffers();
};

VideoOut::VideoOut() : Texture()
{
	win = SDL_SetVideoMode(0, 0, 32, SDL_SWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN);
	w = win-&gt;w;
	h = win-&gt;h;
	pixbuf = new uint8_t [w*h*4];
	deletePixbuf = true;
}

VideoOut::VideoOut(int W, int H) : Texture(W, H)
{
	win = SDL_SetVideoMode(W, H, 32, SDL_SWSURFACE | SDL_RESIZABLE);
}

VideoOut::~VideoOut()
{
	SDL_FreeSurface(win);
}

void VideoOut::SwapBuffers()
{
	int size = w*h;
	uint32_t* srf = (uint32_t*) (win-&gt;pixels);
	uint8_t* buf = pixbuf;
	int index;
	for (int n = 0; n &lt; size; n++)
	{
		srf[n] = SDL_MapRGB(win-&gt;format, buf[index+2], buf[index+1], buf[index+0]);
		index += 4;
	}
	SDL_UpdateRect(win, 0, 0, win-&gt;w, win-&gt;h);
}

#include &quot;Tiles.cpp&quot;

int main()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	VideoOut out (img_Tiles.w*4, img_Tiles.h);
	TexExerpt Tiles(&amp;img_Tiles);
	TexExerpt o(&amp;out);
	o.Draw(&amp;Tiles);
	out.SwapBuffers();
	SDL_Delay(2000);
}
</code></pre>
<p>Und hier noch Texture.cpp:</p>
<pre><code class="language-cpp">#ifndef Texture_cpp
#define Texture_cpp

class Texture
{
public:
	Texture();
	Texture(int W, int H);
	Texture(int W, int H, uint8_t* Pixbuf);
	~Texture();
	uint8_t* pixbuf;
	bool deletePixbuf;
	int w;
	int h;
};

Texture::Texture()
{
	pixbuf = NULL;
	deletePixbuf = false;
}

Texture::Texture(int W, int H)
{
	w = W;
	h = H;
	pixbuf = new uint8_t [w*h*4];
	deletePixbuf = true;
}

Texture::Texture(int W, int H, uint8_t* Pixbuf)
{
	w = W;
	h = H;
	pixbuf = Pixbuf;
	deletePixbuf = false;
}

Texture::~Texture()
{
	if (deletePixbuf) delete [] pixbuf;
}

#endif
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2238540</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2238540</guid><dc:creator><![CDATA[Encypruon]]></dc:creator><pubDate>Fri, 03 Aug 2012 23:17:43 GMT</pubDate></item><item><title><![CDATA[Reply to Unvorhersehbare Fehler on Sat, 04 Aug 2012 00:11:33 GMT]]></title><description><![CDATA[<p>-O1 und -Wall benutzen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2238550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2238550</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 04 Aug 2012 00:11:33 GMT</pubDate></item><item><title><![CDATA[Reply to Unvorhersehbare Fehler on Sat, 04 Aug 2012 00:43:02 GMT]]></title><description><![CDATA[<p>Danke! Index war nicht initialisiert. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /> Jetzt muss ich nur noch herausfinden, was mit der obersten Zeile der Grafik nicht stimmt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2238553</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2238553</guid><dc:creator><![CDATA[Encypruon]]></dc:creator><pubDate>Sat, 04 Aug 2012 00:43:02 GMT</pubDate></item><item><title><![CDATA[Reply to Unvorhersehbare Fehler on Sat, 04 Aug 2012 00:47:02 GMT]]></title><description><![CDATA[<p>Gelöst. War ein ähnliches Problem. Ich habe mich irgendwie darauf verlassen, dass frisch allozierter Speicher immer leer ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2238554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2238554</guid><dc:creator><![CDATA[Encypruon]]></dc:creator><pubDate>Sat, 04 Aug 2012 00:47:02 GMT</pubDate></item><item><title><![CDATA[Reply to Unvorhersehbare Fehler on Mon, 06 Aug 2012 12:14:39 GMT]]></title><description><![CDATA[<p>Du solltest im Standardkonstruktor auf jeden Fall die Member initialisieren. Statt Zuweisungen im Funktionsrumpf solltest du auch Initialisierungslisten benutzen, z.B. :</p>
<pre><code class="language-cpp">TexExcerpt::TexExcerpt() :
   tex( 0 ), x( 0 ), y( 0 ), w( 0 ), h( 0 )
{
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2239286</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2239286</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Mon, 06 Aug 2012 12:14:39 GMT</pubDate></item></channel></rss>