Jump to content

  • Log In with Google      Sign In   
  • Create Account

doyleman77

Member Since 14 Apr 2010
Offline Last Active Oct 30 2014 09:20 AM

#5165029 STL Map and SDL2 Texture segfault

Posted by doyleman77 on 06 July 2014 - 06:35 AM

I've been to figure out why this keeps breaking for the last 3 days. I'm using a std::map to store all textures in, hopefully, and then just have my link to the images from that map. This works fine on one image; I can load it, store it, and retrieve it to have an entity use it and finally, it gets drawn properly. The minute I add in another texture, however, my game crashes on trying to create an entity that refers to any/either of the textures - GDB has it crashing on a segfault inside the std::map header file, presumably when I'm trying to retrieve the texture.

 

Here's the code in question:

 

Game.cpp

Game::Game()
{
  time(&gameTime);
  timeInfo = localtime(&gameTime);

  gameWindow = SDL_CreateWindow(globals::SCREENTITLE.c_str(),
              SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
              globals::SCREENWIDTH, globals::SCREENHEIGHT, SDL_WINDOW_SHOWN);

  gameRenderer = SDL_CreateRenderer(gameWindow, 0, SDL_RENDERER_ACCELERATED);

  loadTexture("texture.png");
  loadTexture("raindrop.png");
  //gameVector.push_back(&(textureLibrary["raindrop.png"]));

  Entity newEntity(textureLibrary["raindrop.png"]);
  //Entity anotherEntity(&(textureLibrary["raindrop.png"]));
  gameVec.push_back(&(newEntity));
  //gameVec.push_back(&(anotherEntity));

  running = true;
  while(running)
  {
    handleInput(gameInput);
    update();
    draw();
    SDL_Delay(16);
  }
};

Game.cpp

void Game::loadTexture(std::string filename)
{
  SDL_Texture* newTexture = NULL;

  SDL_Surface* loadedSurface = IMG_Load(filename.c_str());
  if(loadedSurface == NULL)
    SDL_Quit();
  /// Set the image invisibility color
  SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0xFF));

  newTexture = SDL_CreateTextureFromSurface(gameRenderer, loadedSurface);

  SDL_FreeSurface(loadedSurface);
  textureLibrary[filename.c_str()] = newTexture;
  return;
};

And finally, my entity constructor - very basic.

Entity::Entity(SDL_Texture* itsTexture)
{
    texture = itsTexture;
    SDL_QueryTexture(itsTexture, NULL, NULL, &texRect->w, &texRect->h);
};

texture is the member of Entity, and is a simple SDL_Texture*.

 

Again; this system worked fine when I had only one image loaded - it wasnt until I had both ["raindrop.png"] and ["texture.png"] that it started segfaulting on the

Entity newEntity(textureLibrary["raindrop.png"]);

bit.

 

Any clues? Don't mind the terrible coding. This is me trying to get a game worked out in a 8wk challenge; and I'm sure it may be sloppy. :(




#5132615 I don't get c++11.

Posted by doyleman77 on 19 February 2014 - 07:08 AM

I thank everyone who had explanations on all topics. I tried a few sample lambdas to try to get a feel; and it's beginning to make sense. I'll be honest in that I never really used a callback; let alone know what one is. From my understanding, it's passing a function into another, and having it perform at a later point within that first function.

Move constructors and assignments are entirely cleared up; that actually seems like a brilliant move; and left me wondering why that wasn't part of the standard earlier.

 

decltype has also been cleared up; and I can see the uses for that definitely. I bookmarked the GoingNative page; and will check that out later today when I finish classes; thanks for the link!

 

I really do appreciate all of your guy's explanation, and time taken to give them. The depth of some of them were quite long, but served to help more than what the book had shown; so thanks to all on their contributions!




#5132539 I don't get c++11.

Posted by doyleman77 on 18 February 2014 - 09:43 PM

Pretty comfortable with my c++ code now; I get classes, I get using templates and STL, smart pointers, and to an extent, exceptions. What I don't get is most of the new 2011 standard. Lambdas confuse me, in both their syntax and their purpose.

 

I've looked up articles trying to show practical uses for them, but they seem to go even further above my head than just the word 'lambda'. Rvalue / moving seems confusing just as well; decltype... gah. None of the new stuff makes much of any sense. The only thing I've understood so far is auto, and to a half-way extent: the new for loop style.

 

I've read through a third of C++ Primer 5th, and so far it's not really shed any new light to me. The frustration of these new features and my inability to grasp them has me concerned... How is it some clicked onto the new concepts rather fast? And where can I go to get to that point?




#5082880 Used old C++, Sudden feeling overwhelmed.

Posted by doyleman77 on 03 August 2013 - 08:19 PM

Back in 06 I got into programming from a buddy of mine, and asked for a book on C++ for christmas. I was 17 at the time. Anyway, unimportant. The book was Teach yourself c++ in 21 days; the only one in our local bookstore. I picked it up, and have gone over it a few times. Practiced several things, got into programming light games with SDL, Went to university, and my first day there they jumped me several classes into computer science. They moved me past about 5 computer science classes, all the way up to Intermediate C++. I'm not trying to show off, or brag, just emphasize that I seemed to think I was going places.

 

I passed the course with a B, so I thought I knew what I was doing just fine, but... Now I'm browsing the net, and seeing that what I've been doing is what everyone calls 'C with Classes'. I have barely touched on templates, exceptioins, or STL. In fact, I am embarrased to say I've never once used STL. at all. my course in college had us make our own vector, using templates (my only foot in that part of c++) to build the class.

 

Lately, my head feels... overwhelmed. I want to learn the right way to do C++; to make clean and efficient code. I feel like I almost wasted my last 6 years programming. I learned a few things, sure, but I already knew quite a bit coming from another toolkit I used for awhile; RPG maker 2 for the PS2. Sure, no hard code, but basic scripting that introduced me to control / loops / etc. But most of the C++ I have been writing hasn't really been C++. It's been classes, and C code. And browsing over what C++ 11 looks like just makes me dizzy.

 

I don't know how to word the question I have, but it's basically along the lines of... What do I do about this? Pick up C++ Primer 5ed and try to relearn? I am not sure. Thanks for any advice.




#5002807 open gl game

Posted by doyleman77 on 20 November 2012 - 06:29 PM

I am also thinking about quitting but I am going to try one more time. I am also going to try and solve things on my own. By the way this stuff is far from easy.


A prize easily obtained is a prize not worth having. - some proverb, but it applies here.


#4998693 Is SFML a better choice over SDL?

Posted by doyleman77 on 07 November 2012 - 07:58 PM

SFML is under active development. SDL went stagnant a few years back, though it appears it may be getting reactivated again with a new release... but that's not a guarantee.


according to the mailing list; it's for the most part finished, and Sam has stated he could ship it; but has been to busy with working with Valve for Steam on Linux at the moment; and that once he's more settled in it should be much closer to release.


#4963076 C++ and SFML Good Game Tutorials?

Posted by doyleman77 on 25 July 2012 - 03:55 PM

SFML_Coder and CodingMadeEasy on youtube has been my reference. ymmv.


PARTNERS