Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 14 Apr 2010
Offline Last Active Dec 11 2015 12:17 PM

Posts I've Made

In Topic: SDL_BlendMode when sharing textures?

21 July 2014 - 03:21 PM



So far, I've just skipped doing alpha & blends on textures; but I am curious more if there's a better way than the above proposed?

In Topic: STL Map and SDL2 Texture segfault

08 July 2014 - 10:25 AM

The pointers are (I believe) the only local objects now, and are then passed to the map/vector which covers the entire app/game's scope, anyway. The code hasn't changed much, other than declaring new on entities. I also removed the string as a parameter, because somewhere along the line I would have to make a string object to pass upon it.


Also, my bad on calling it the stl. I suppose I assumed because it's standard, templated, and a library, that was it's appropriate name. I'm guessing the STL is more of unofficial libraries, where the C++ Libraries are official, and required for most, if not all, C++ compilers...?



Also, when this segfault happens, Code::Blocks auto opens stl_map.h file - also leading me to believe this is the STL?


Anyway, yeah - the code hasn't changed much. I know, I should have all of this loading / game loop outside of the constructor. I'll move it - I've just been occupied with this crashing, so far.

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

  gameRenderer = SDL_CreateRenderer(gameWindow, 0, SDL_RENDERER_ACCELERATED);


  /// this is me, making sure that the map pulls texture.png. this works.
  SDL_Texture* myTex = textureLibrary["texture.png"];

  Entity* newEntity = new Entity(textureLibrary["raindrop.png"]);
  Entity* anotherEntity = new Entity(textureLibrary["texture.png"]);

  running = true;

void Game::loadTexture(const char filename[])
  //SDL_Texture** newTexture = new SDL_Texture*;

  SDL_Surface* loadedSurface = IMG_Load(filename);

  /// Set the image invisibility color
  SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0xFF));

  //*newTexture = SDL_CreateTextureFromSurface(gameRenderer, loadedSurface);

  textureLibrary[filename] = SDL_CreateTextureFromSurface(gameRenderer, loadedSurface);
Entity::Entity(SDL_Texture* itsTexture)
    texture = itsTexture;
    SDL_QueryTexture(itsTexture, NULL, NULL, &texRect->w, &texRect->h);

Here, I initialize SDL, load in some textures (which are then placed into the map/cache), and then I test out by pulling into myTex. Originally, just below that, I did a quick RenderClear, RenderCopy, and RenderPresent to show myTex, and it appeared - in all it's glory.


Where as before, when newEntity and anotherEntity were on the stack, rather than the heap, I could _at least_ get newEntity to load it's texture, and display properly, and the anotherEntity would crash; the segfault now happens on newEntity.


Following the call stack, the only place other than stl_map.h that maybe is a problem area is the constructor of Game, or maybe of Entity (despite it looking like Entity's constructor isn't on the stack at all, yet).


Thanks for the replies.




I've just given up on this...

In Topic: STL Map and SDL2 Texture segfault

08 July 2014 - 07:58 AM

Run it through a debugger and observe the lines it is crashing on and check the state of everything when that happens. If you don't know how your debugger works yet, now is a perfect time to learn. A simple, reliably to reproduce crash is the textbook example for it.

That said, have you dealt with what rip-off said in post #4?

I have. It crashes at line 472 of stl_map.h:

	iterator __i = lower_bound(__k);
	// __i->first is greater than or equivalent to __k.
	if (__i == end() || key_comp()(__k, (*__i).first))
          __i = insert(__i, std::make_pair(std::move(__k), mapped_type()));
	return (*__i).second;

the call stack shows it crashes at the [] operator call of map<>:



#0 6C78619F    SDL_LogCritical() (C:\Users\name\Dropbox\CardGame\bin\Debug\SDL2.dll:??)
#1 004658B5    std::map<std::string, SDL_Texture*, std::less<std::string>, std::allocator<std::pair<std::string const, SDL_Texture*> > >::operator[](std::string&&) (this=0x4013cd <Entity::Entity(SDL_Texture*&) (c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_map.h:472)
#2 00401690    Game::Game(this=0x28fdb0) (C:\Users\name\Dropbox\CardGame\game.cpp:38)
#3 00401F1B    SDL_main(argc=argc@entry=1, args=args@entry=0x340008) (C:\Users\name\Dropbox\CardGame\main.cpp:8)
#4 004027EC    console_main(argc=argc@entry=1, argv=argv@entry=0x340008) (../src/main/windows/SDL_windows_main.c:140)
#5 004029AD    WinMain@16(hInst=0x400000, hPrev=0x0, szCmdLine=0x7b3bf8 "", sw=10) (../src/main/windows/SDL_windows_main.c:177)
#6 0046E23B    main () (??:??)


I have made my Entities on the heap, and then pushing them onto the Vector<Entity*> GameVec vector - and the segfault occurs there, too - but the app doesn't crash at that point, it just doesn' display the images (where as before, I could do one entity and display it, it'd just crash on 2.)


I guess I don't know how to pry open the map<> and test the addresses of what it's pointing too, but for what its worth: it does work if I do a direct SDL_RenderCopy and using the map, rather than the Entity, or if I make a local Texture and assign it a texture from the map, too.

In Topic: STL Map and SDL2 Texture segfault

07 July 2014 - 07:28 PM

I've read the wiki article before, and while minimal, the code listings arent helping me understand what is going on. Lambdas are by far the worst for me to understand. :-/


On a seperate note, regarding my textureLibrary map: would it be that I need to have a getTexture() function that iterates the map, and returns it? Am I not able to simply use textureLibrary["texture.png"] to bring up the appropriate texture? The only problem I could see that causing, at the moment, is that if I use the wrong key, it'd grab a newly made, blank texture. But I don't see why that'd still segfault.


I've cleaned up the local variables, and the string bits - and I'm still getting crashes. It's odd, because it seems to work fine on one entity, but not the 2nd. I can directly display the texture using the same key, it only seems to crash when I try to instantiate an Entity with that texture...


I apologize if my wording is confusing.

In Topic: STL Map and SDL2 Texture segfault

07 July 2014 - 11:28 AM

I haven't wrapped my mind around lambda's yet, unfortunately. :(


Most of c++11 still seems mystified to me; and I can't find a place that breaks down the new features well enough for me to understand.


I appreciate and thank you for your explanations, though!