SDL_Flip() crashing

Started by
13 comments, last by dudedbz1 18 years, 5 months ago
Did you actually try Drew's suggestion for using SDL_GetVideoSurface() instead of your m_Screen variable? SDL_Flip calls SDL_UpdateRect internally if SDL_DOUBLEBUF is not available so there really shouldn't be a difference.

What version of SDL do you have installed?

Try walking through the code with a debugger. Put a break point right before that line of code and see what the value of m_Screen is before and durring the SDL_Flip function.
Evillive2
Advertisement
Quote:Original post by dudedbz1
So, anyone have an idea why UpdateRect might work but SDL_Flip doesnt?


Well you got me, no clue why the SDL_Flip isn't working at all. If you could post all your code, that is if you wanted to, I'd not mind taking a look though it and see if it crashes on my computer.

Hey, by any chance, do you have a new NVidia card, either PCI-E or higher model past 6600 either agp? There was a recent thread with a problem with SDL and that type, so if you do have one of those, maybe it's a new problem that has not been acknowledge yet.
Quote:Original post by Drew_Benton
Quote:Original post by dudedbz1
So, anyone have an idea why UpdateRect might work but SDL_Flip doesnt?


Well you got me, no clue why the SDL_Flip isn't working at all. If you could post all your code, that is if you wanted to, I'd not mind taking a look though it and see if it crashes on my computer.

Hey, by any chance, do you have a new NVidia card, either PCI-E or higher model past 6600 either agp? There was a recent thread with a problem with SDL and that type, so if you do have one of those, maybe it's a new problem that has not been acknowledge yet.


Well, I guess I can do that. It's just that there's a lot of code that I'm not using yet, so I'll see what to post here. It's most likely not my video card, since I have used this same method before... And the interesting part is, that with this:
int main(int, char**){    cApp app(1);    app.cApp_Init(SDL_INIT_VIDEO);    app.cApp_InitWindow(500, 500, SDL_HWSURFACE | SDL_DOUBLEBUF);    app.cApp_AddImage("Paddle.bmp");    SDL_Event event;    while (true)    {        app.cApp_RefreshScreen();        if (SDL_PollEvent(&event))        {            if (event.type == SDL_QUIT)                break;        }        app.cApp_Draw(0);        app.cApp_Flip();    }    app.cApp_DeInit();    return 0;}

It works perfectly... I just dont know...

Well, here is the code:
//main.cpp#include <SDL.h>#include "cApp.h"const int NUM_IMAGES = 3;cApp app(NUM_IMAGES);const int SCREEN_H = 500;const int SCREEN_W = 500;const int FAIL = 1;const int SUCCESS = 0;enum {P1, P2, BALL};bool Init();void DeInit();bool Run();void Draw();int MiddleWidth(int width1, int width2){	return ((width1 / 2) - (width2 / 2));}int MiddleHeight(int height1, int height2){	return ((height1 / 2) - (height2 / 2));}int main(int argc, char *argv[]){	if (Init() == false)        return FAIL;    while (Run())        Draw();    DeInit();    return SUCCESS;}bool Init(){	bool ret = (app.cApp_Init(SDL_INIT_VIDEO) &&                app.cApp_InitWindow(SCREEN_H, SCREEN_W, SDL_HWSURFACE | SDL_DOUBLEBUF) &&                app.cApp_AddImage("Paddle.bmp") &&                app.cApp_AddImage("Paddle.bmp") &&                app.cApp_AddImage("Ball.bmp"));    app.cApp_GetImage(BALL)->SetColorKey();    app.cApp_GetImage(P1)->Setx(MiddleWidth(SCREEN_W, app.cApp_GetImage(P1)->Getw()));    return ret; // by accident I have true}void DeInit(){	app.cApp_DeInit();}bool Run(){	SDL_Event event;	while (SDL_PollEvent(&event))	{		if (event.type == SDL_QUIT)            return false;        if (event.type == SDL_KEYDOWN)        {        	if (event.key.keysym.sym == SDLK_ESCAPE)                return false;        }	}}void Draw(){	app.cApp_RefreshScreen();	app.cApp_Draw(P1);	app.cApp_Draw(P2);	app.cApp_Draw(BALL);	app.cApp_Flip();}//cApp.h#ifndef SDL_ENGINEMAIN_H#define SDL_ENGINEMAIN_H#include <SDL.h>#include "cApp_Image.h"#include "cApp_Text.h"#include <vector>#include <string>using std::vector;class cApp{	public:        cApp(int numToReserve = 0, int numTextToReserve = 0);        ~cApp();        bool cApp_InitWindow(int h, int w, unsigned int flags);        bool cApp_Init(unsigned int flags, bool text = false);        void cApp_DeInit();        void cApp_IncreaseGraphics(int numToReserve = 0);        void cApp_IncreaseText(int numToReserve = 0);        void cApp_AddText();        bool cApp_AddImage(const char *fileName);        Image *cApp_GetImage(int numOfImage);        Text *cApp_GetText(int i);        void cApp_RemoveLastText();        void cApp_RemoveText(int i);        void cApp_RemoveLastImage();        void cApp_RemoveImage(int imageToRemove);        void cApp_Draw(int i);        void cApp_DrawText(int i);        void cApp_Flip()//;        {        	SDL_Flip(m_Screen);//          SDL_UpdateRect(m_Screen, 0, 0, 0, 0);        }        void cApp_RefreshScreen() {SDL_FillRect(m_Screen, NULL, 0xffffcc);}        bool cApp_Collide(int i, int j);        bool cApp_Collide(char direction, int i, int j);        void cApp_SetTitle(const char *title);    private:        vector<Image*> m_Graphics;        vector<Text*> m_Text;        bool _TextOn;        SDL_Surface *m_Screen;};#endif /*SDL_ENGINEMAIN_H*///cApp.cpp#include <SDL.h>#include <vector>#include "cApp.h"using std::vector;cApp::cApp(int numToReserve, int numTextToReserve){	m_Graphics.reserve(numToReserve);	m_Text.reserve(numTextToReserve);}cApp::~cApp(){	for (int i = 0; i < m_Graphics.size(); i++)        delete m_Graphics;    for (int j = 0; j < m_Text.size(); j++)        delete m_Text[j];}bool cApp::cApp_InitWindow(int h, int w, unsigned int flags){	m_Screen = SDL_SetVideoMode(w, h, 0, flags);	if (m_Screen == NULL)        return false;    return true;}bool cApp::cApp_Init(unsigned int flags, bool text){	if (SDL_Init(flags) == -1)        return false;    if (text)    {    	if (TTF_Init() == -1)            return false;        _TextOn = true;    }    else        _TextOn = false;    return true;}void cApp::cApp_DeInit(){	if (_TextOn)	{		TTF_Quit();	}    SDL_Quit();}void cApp::cApp_IncreaseGraphics(int numToReserve){	m_Graphics.reserve(numToReserve);}void cApp::cApp_IncreaseText(int numToReserve){	m_Text.reserve(numToReserve);}void cApp::cApp_AddText(){	m_Text.push_back(new Text);}bool cApp::cApp_AddImage(const char *fileName){	Image *imageToAdd = new Image;	imageToAdd->SetSprite(IMG_Load(fileName));	if (imageToAdd->GetSprite() == NULL)	{        delete imageToAdd;        return false;	}    m_Graphics.push_back(imageToAdd);    return true;}Text *cApp::cApp_GetText(int i){    if (i < m_Text.size())        return m_Text;    return NULL;}Image *cApp::cApp_GetImage(int numOfImage){	if (numOfImage < m_Graphics.size())        return m_Graphics[numOfImage];    return NULL;}void cApp::cApp_RemoveLastText(){	int textToRemove = m_Text.size() - 1;	delete m_Text[textToRemove];	m_Text.pop_back();}void cApp::cApp_RemoveText(int i){	delete m_Text;	m_Text = m_Text;<br><br>	<span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> iter = i + <span class="cpp-number">1</span>; iter &lt; m_Graphics.size(); iter++)<br>	{<br>        <span class="cpp-keyword">if</span> (m_Text[iter + <span class="cpp-number">1</span>] != NULL)<br>            m_Text[iter] = m_Text[iter + <span class="cpp-number">1</span>];<br>	}<br><br>	m_Text.pop_back();<br>}<br><br><span class="cpp-keyword">void</span> cApp::cApp_RemoveLastImage()<br>{<br>	<span class="cpp-keyword">int</span> imageToRemove = m_Graphics.size() - <span class="cpp-number">1</span>;<br><br>	<span class="cpp-keyword">delete</span> m_Graphics[imageToRemove];<br><br>    m_Graphics.pop_back();<br>}<br><br><span class="cpp-keyword">void</span> cApp::cApp_RemoveImage(<span class="cpp-keyword">int</span> imageToRemove)<br>{<br>	<span class="cpp-keyword">delete</span> m_Graphics[imageToRemove];<br><br>	m_Graphics[imageToRemove] = m_Graphics[imageToRemove + <span class="cpp-number">1</span>];<br><br>	<span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i = imageToRemove + <span class="cpp-number">1</span>; i &lt; m_Graphics.size(); i++)<br>	{<br>        <span class="cpp-keyword">if</span> (m_Graphics != NULL)<br>            m_Graphics<span style="font-weight:bold;"> = m_Graphics;<br>	}<br><br>	m_Graphics.pop_back();<br>}<br><br><span class="cpp-keyword">void</span> cApp::cApp_DrawText(<span class="cpp-keyword">int</span> i)<br>{<br>	<span class="cpp-keyword">if</span> (i &lt; m_Text.size())<br>	{<br>		<span class="cpp-keyword">if</span> (m_Text<span style="font-weight:bold;">-&gt;GetTextSurface() != NULL)<br>		{<br>			SDL_Rect src, dst;<br><br>            dst.x = m_Text<span style="font-weight:bold;">-&gt;Getx();<br>            dst.y = m_Text<span style="font-weight:bold;">-&gt;Gety();<br>            dst.w = m_Text<span style="font-weight:bold;">-&gt;Getw();<br>            dst.h = m_Text<span style="font-weight:bold;">-&gt;Geth();<br><br>            src.x = <span class="cpp-number">0</span>;<br>            src.y = <span class="cpp-number">0</span>;<br>            src.w = dst.w;<br>            src.h = dst.h;<br><br>            SDL_BlitSurface(m_Text<span style="font-weight:bold;">-&gt;GetTextSurface(), &amp;src, m_Screen, &amp;dst);<br>		}<br>	}<br>}<br><br><span class="cpp-keyword">void</span> cApp::cApp_Draw(<span class="cpp-keyword">int</span> i)<br>{<br>	<span class="cpp-keyword">if</span> (i &lt; m_Graphics.size())<br>	{<br>		<span class="cpp-keyword">if</span> (m_Graphics<span style="font-weight:bold;">-&gt;GetSprite() != NULL)<br>		{<br>            SDL_Rect src, dst;<br><br>            dst.x = m_Graphics<span style="font-weight:bold;">-&gt;Getx();<br>            dst.y = m_Graphics<span style="font-weight:bold;">-&gt;Gety();<br>            dst.w = m_Graphics<span style="font-weight:bold;">-&gt;Getw();<br>            dst.h = m_Graphics<span style="font-weight:bold;">-&gt;Geth();<br><br>            src.x = <span class="cpp-number">0</span>;<br>            src.y = <span class="cpp-number">0</span>;<br>            src.w = dst.w;<br>            src.h = dst.h;<br><br>            SDL_BlitSurface(m_Graphics<span style="font-weight:bold;">-&gt;GetSprite(), &amp;src, m_Screen, &amp;dst);<br>		}<br>	}<br>}<br><br><span class="cpp-keyword">bool</span> cApp::cApp_Collide(<span class="cpp-keyword">int</span> i, <span class="cpp-keyword">int</span> j)<br>{<br>	<span class="cpp-keyword">if</span> (i &lt; m_Graphics.size() &amp;&amp; j &lt; m_Graphics.size())<br>	{<br>        SDL_Rect image1, image2;<br><br>        image1.x = m_Graphics<span style="font-weight:bold;">-&gt;Getx();<br>        image1.y = m_Graphics<span style="font-weight:bold;">-&gt;Gety();<br>        image1.w = m_Graphics<span style="font-weight:bold;">-&gt;Getw();<br>        image1.h = m_Graphics<span style="font-weight:bold;">-&gt;Geth();<br><br>        image2.x = m_Graphics[j]-&gt;Getx();<br>        image2.y = m_Graphics[j]-&gt;Gety();<br>        image2.w = m_Graphics[j]-&gt;Getw();<br>        image2.h = m_Graphics[j]-&gt;Geth();<br><br>        <span class="cpp-keyword">if</span> (image1.x &gt;= (image2.x + image2.w) || image2.x &gt;= (image1.x + image1.w))<br>            <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>; <span class="cpp-comment">// They are not in the same horizontal space</span><br><br>        <span class="cpp-keyword">if</span> (image1.y &gt;= (image2.y + image2.h) || image2.y &gt;= (image1.y + image1.h))<br>            <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>; <span class="cpp-comment">// They are not in the same vertical space</span><br><br>        <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>	}<br><br>	<span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>; <span class="cpp-comment">// Huh?  i || j are not in m_Images!</span><br>}<br><br><span class="cpp-keyword">bool</span> cApp::cApp_Collide(<span class="cpp-keyword">char</span> direction, <span class="cpp-keyword">int</span> i, <span class="cpp-keyword">int</span> j)<br>{<br>	<span class="cpp-keyword">if</span> (i &lt; m_Graphics.size() &amp;&amp; j &lt; m_Graphics.size())<br>	{<br>        SDL_Rect image1, image2;<br><br>        image1.x = m_Graphics<span style="font-weight:bold;">-&gt;Getx();<br>        image1.y = m_Graphics<span style="font-weight:bold;">-&gt;Gety();<br>        image1.w = m_Graphics<span style="font-weight:bold;">-&gt;Getw();<br>        image1.h = m_Graphics<span style="font-weight:bold;">-&gt;Geth();<br><br>        image2.x = m_Graphics[j]-&gt;Getx();<br>        image2.y = m_Graphics[j]-&gt;Gety();<br>        image2.w = m_Graphics[j]-&gt;Getw();<br>        image2.h = m_Graphics[j]-&gt;Geth();<br><br>        <span class="cpp-keyword">if</span> (direction == 'x')<br>            <span class="cpp-keyword">if</span> (image1.x &gt;= (image2.x + image2.w) || image2.x &gt;= (image1.x + image1.w))<br>                <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br><br>        <span class="cpp-keyword">if</span> (direction == 'y')<br>            <span class="cpp-keyword">if</span> (image1.y &gt;= (image2.y + image2.h) || image2.y &gt;= (image1.y + image1.h))<br>                <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br><br>        <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>	}<br><br>	<span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>; <span class="cpp-comment">// Huh?  i || j are not in m_Images!</span><br>}<br><br><span class="cpp-keyword">void</span> cApp::cApp_SetTitle(<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> *title)<br>{<br>	SDL_WM_SetCaption(title, NULL);<br>}<br><br><br><br><br></pre></div><!–ENDSCRIPT–><br>…  <br><br>Anyways, notice my header file checks…  SDL_ENGINEMAIN_H was the previous name, but now that I'm not gonna use it JUST for SDL and I'm gonna expand it I thought I might change it a little.<br><br>As I said, the little &#111;ne works, but the big &#111;ne doesnt…  Maybe its something from the way I have my functions set up?<br><br>//Edit: Oh, you want to see if it'll crash &#111;n you pc too?  Then I must have the other two files..  lol<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-comment">//cApp_Image.h</span><br><br><span class="cpp-directive">#ifndef</span> IMAGE_H<br><span class="cpp-directive">#define</span> IMAGE_H<br><br><span class="cpp-directive">#include</span> &lt;SDL.h&gt;<br><br><span class="cpp-directive">#include</span> &lt;SDL_image.h&gt;<br><br><span class="cpp-keyword">class</span> Image<br>{<br>	<span class="cpp-keyword">public</span>:<br>        Image(<span class="cpp-keyword">int</span> x = <span class="cpp-number">0</span>, <span class="cpp-keyword">int</span> y = <span class="cpp-number">0</span>): m_x(x), m_y(y), m_Sprite(NULL)<br>        {<br>        }<br>        ~Image()<br>        {<br>        	<span class="cpp-keyword">if</span> (m_Sprite != NULL)<br>        		SDL_FreeSurface(m_Sprite);<br>        }<br>        <br>        <span class="cpp-keyword">int</span> Getx() {<span class="cpp-keyword">return</span> m_x;}<br>        <span class="cpp-keyword">void</span> Setx(<span class="cpp-keyword">int</span> x) {m_x = x;}<br>        <br>        <span class="cpp-keyword">int</span> Gety() {<span class="cpp-keyword">return</span> m_y;}<br>        <span class="cpp-keyword">void</span> Sety(<span class="cpp-keyword">int</span> y) {m_y = y;}<br>        <br>        <span class="cpp-keyword">int</span> Getw() {<span class="cpp-keyword">return</span> m_Sprite-&gt;w;}<br>        <span class="cpp-keyword">int</span> Geth() {<span class="cpp-keyword">return</span> m_Sprite-&gt;h;}<br>        <br>        <span class="cpp-keyword">void</span> SetSprite(SDL_Surface *sprite)<br>        {<br>        	m_Sprite = sprite;<br>        }<br>        SDL_Surface *GetSprite() {<span class="cpp-keyword">return</span> m_Sprite;}<br>        <br>        <span class="cpp-keyword">void</span> SetColorKey()<br>        {<br>        	SDL_SetColorKey(m_Sprite, SDL_SRCCOLORKEY, 0xffffff);<br>        }<br>        <br>    <span class="cpp-keyword">private</span>:<br>        <span class="cpp-keyword">int</span> m_x;<br>        <span class="cpp-keyword">int</span> m_y;<br>        <br>        SDL_Surface *m_Sprite;<br>};<br><br><span class="cpp-directive">#endif</span> <span class="cpp-comment">/*IMAGE_H*/</span><br><br><span class="cpp-comment">//cApp_Text.h</span><br><br><span class="cpp-directive">#ifndef</span> TEXT_H<br><span class="cpp-directive">#define</span> TEXT_H<br><br><span class="cpp-directive">#include</span> &lt;SDL.h&gt;<br><br><span class="cpp-directive">#include</span> &lt;SDL_ttf.h&gt;<br><br><span class="cpp-keyword">class</span> Text<br>{<br>	<span class="cpp-keyword">public</span>:<br>        Text(<span class="cpp-keyword">int</span> x = <span class="cpp-number">0</span>, <span class="cpp-keyword">int</span> y = <span class="cpp-number">0</span>): m_x(x), m_y(y), m_TextSurface(NULL)<br>        {<br>        }<br>        ~Text()<br>        {<br>        	<span class="cpp-keyword">if</span> (m_TextSurface != NULL)<br>                SDL_FreeSurface(m_TextSurface);<br>        }<br>        <br>        <span class="cpp-keyword">int</span> Getx() {<span class="cpp-keyword">return</span> m_x;}<br>        <span class="cpp-keyword">void</span> Setx(<span class="cpp-keyword">int</span> x) {m_x = x;}<br>        <br>        <span class="cpp-keyword">int</span> Gety() {<span class="cpp-keyword">return</span> m_y;}<br>        <span class="cpp-keyword">void</span> Sety(<span class="cpp-keyword">int</span> y) {m_y = y;}<br>        <br>        <span class="cpp-keyword">int</span> Getw() {<span class="cpp-keyword">return</span> m_TextSurface-&gt;w;}<br>        <span class="cpp-keyword">int</span> Geth() {<span class="cpp-keyword">return</span> m_TextSurface-&gt;h;}<br>        <br>        SDL_Surface *GetTextSurface() {<span class="cpp-keyword">return</span> m_TextSurface;}<br>        <br>        <span class="cpp-keyword">void</span> SetTextColor(<span class="cpp-keyword">int</span> r, <span class="cpp-keyword">int</span> g, <span class="cpp-keyword">int</span> b)<br>        {<br>        	m_TextColor.r = r;<br>        	m_TextColor.g = g;<br>        	m_TextColor.b = b;<br>        }<br>        SDL_Color GetTextColor() {<span class="cpp-keyword">return</span> m_TextColor;}<br>        <br>        <span class="cpp-keyword">void</span> SetSize(<span class="cpp-keyword">int</span> Size) {m_Size = Size;}<br>        <span class="cpp-keyword">int</span> GetSize() {<span class="cpp-keyword">return</span> m_Size;}<br>        <br>        <span class="cpp-keyword">void</span> SetFont(<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> *Font)<br>        {<br>        	m_Font = TTF_OpenFont(Font, m_Size);<br>        }<br>        <br>        <span class="cpp-keyword">void</span> SetText(<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> *Text)<br>        {<br>        	m_TextSurface = TTF_RenderText_Solid(m_Font, Text, m_TextColor);<br>        }<br>        <br>    <span class="cpp-keyword">private</span>:<br>        <span class="cpp-keyword">int</span> m_x;<br>        <span class="cpp-keyword">int</span> m_y;<br>        <br>        <span class="cpp-keyword">int</span> m_Size;<br>        <br>        TTF_Font *m_Font;<br>        <br>        SDL_Color m_TextColor;<br>        <br>        SDL_Surface *m_TextSurface;<br>};<br><br><span class="cpp-directive">#endif</span><br><br><br><br><br><br></pre></div><!–ENDSCRIPT–><br>I hope we can solve this mystery, lol.<br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - dudedbz1 on November 6, 2005 8:35:17 AM]<!–EDIT–></span><!–/EDIT–>
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------
change your Run function to return true if nothing unusual happens

with that your code it works for me.

for some reason if you change SDL_Flip to SDL_UpdateRect it works aswell thats puzzeling...

see if that helps...
Quote:Original post by rip-off
change your Run function to return true if nothing unusual happens

with that your code it works for me.

for some reason if you change SDL_Flip to SDL_UpdateRect it works aswell thats puzzeling...

see if that helps...


Yes! That works! I cant beleive I forgot something sooo simple... Aww well. Not to be any more annoying, but if the while loop exited with the Flip(), then why not exit with the Update?

Thanks to Drew_Benton and rip-off!
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------

This topic is closed to new replies.

Advertisement