Jump to content
  • Advertisement
Sign in to follow this  
AneuryPerez

problem with my engine (sdl2)

This topic is 1090 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greeting first of all !! 

 

 

Im trying to write a engine simple to run all of the game I  trying to writte , im using SDL2  as a base , I can create the window , catch SDL_KEYDOWN and  handler it but everytime I tried to render a surface or a texture the program crash 

below im going to leave the code if somebody could advise me please i would be very pleasure... 

#include"include.hpp"
#include"define.hpp"

int main(int argc, char *argv[])
{

  GameEngine 	engine("title", 500, 400, false);
  InputStream 	is(engine);
    engine.Init();
    //engine.InitWithRenderContext();
    while(is.KeyListener())
  {
	  starCounter = SDL_GetTicks();
	  cout <<"\npolling event.\nFPS : "<<wait <<"and the key pressed respond at the next event : "<<is.getKeyEventEngine()<<endl;
	  SDL_UpdateWindowSurface(engine.getWindow());
	  wait = 1000/FPS - ( starCounter - SDL_GetTicks() );
	  SDL_Delay(wait);
  }
  SDL_Quit();
  cin.get();

  return 0;
}
    InputStream::InputStream(GameEngine ge):Run(true), EVENT_HANDLER_NO(0),g(ge){}


		     bool InputStream::KeyListener()
		    	   {
		    	             SDL_Surface *im = SDL_LoadBMP("sprite1.bmp");
		    	             SDL_Rect    rect ={10,10,0,0};
		    	             SDL_Rect    clip ={0,0,100,100};
		    					while(SDL_PollEvent(&this->event))
		    					   {
		    						   if(this->event.type == SDL_QUIT)
		    							   this->Run = false;
		    						   if(this->event.type == SDL_KEYDOWN)
		    						   {
		    							   switch(this->event.key.keysym.sym)
		    							   {
		    								   case SDLK_q:
		    									   this->EVENT_HANDLER_NO = QUIT;
		    									   this->Run = false;
		    									   break;
		    								   case SDLK_UP:
		    									   this->EVENT_HANDLER_NO = UP;
		    									  
		    									   break;
		    								   case SDLK_DOWN:
		    									   this->EVENT_HANDLER_NO =DOWN;
		    									 
		    										break;
		    								   case SDLK_LEFT:
		    									   this->EVENT_HANDLER_NO = LEFT;
		    									 
		    									   rect.x -=10;
		    									   SDL_BlitSurface(im, &clip, g.getScreen(), &rect);
		    									   SDL_UpdateWindowSurface(g.getWindow());
		    									   break;
		    								   case SDLK_RIGHT:
		    									   rect.x +=10;
		    						    		   SDL_BlitSurface(im, &clip, g.getScreen(), &rect);
		    									   SDL_UpdateWindowSurface(g.getWindow());
		    									   this->EVENT_HANDLER_NO =RIGHT;
		    									 
		    									   break;
		    								   case SDLK_f:
		    									   this->EVENT_HANDLER_NO = FIRE;
		    									   
		    									   break;
		    								   case SDLK_p:
		    								   case SDLK_KP_ENTER:
		    									   this->EVENT_HANDLER_NO = PAUSE;
		    									   break;
		    								   default:
		    									   this->EVENT_HANDLER_NO = NONE;
		    									   break;
		    							   }
		    						   }
		    					   	 }
		    		   return this->Run;
		    	   }


		    EVENT_BUTTON InputStream::getKeyEventEngine()
		    {
		       return this->EVENT_HANDLER_NO;
		    }

that 's the code thar result in crashing I know there are more ways but what is wrong with this .? 

 

thank you ...

Share this post


Link to post
Share on other sites
Advertisement


bool InputStream::KeyListener()
{
    SDL_Surface *im = SDL_LoadBMP("sprite1.bmp");

 

You're dong that in frame loop, loading bmp each time, never releasing it.

Depending on bmp size that code will exhaust your memory pretty quickly.

At some point sdl will fail to load sprite image, and you will blit that surface using zero pointer, which will give you crash for sure.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!