سؤال

Sorry I'm a bit new with SDL and C++ development. Right now I've created a tile mapper that reads from my map.txt file. So far it works, but I want to add editing the map now.

SDL_Texture *texture; 
texture= IMG_LoadTexture(G_Renderer,"assets/tile_1.png"); 
SDL_RenderCopy(G_Renderer, texture, NULL, &destination); 
SDL_RenderPresent(G_Renderer); 

The above is the basic way I'm showing my tiles, but if I want to go in and change the texture in real time it's kind of buggy and doesn't work well. Is there a method that is best for editing a texture? Thanks for the help I appreciate everything.

هل كانت مفيدة؟

المحلول

The most basic way is to set up a storage container with some textures which you will use repeatedly; for example a vector or dictionary/map. Using the map approach for example you could do something like:

// remember to #include <map>
map<string, SDL_Texture> myTextures;
// assign using array-like notation:
myTextures["texture1"] = IMG_LoadTexture(G_Renderer,"assets/tile_1.png");
myTextures["texture2"] = IMG_LoadTexture(G_Renderer,"assets/tile_2.png");
myTextures["texture3"] = IMG_LoadTexture(G_Renderer,"assets/tile_3.png");
myTextures["texture4"] = IMG_LoadTexture(G_Renderer,"assets/tile_4.png");

then to utilise a different texture, all you have to do is use something along the lines of:

SDL_RenderCopy(G_Renderer, myTextures["texture1"], NULL, &destination); 
SDL_RenderPresent(G_Renderer);

which can be further controlled by changing the first line to

SDL_RenderCopy(G_Renderer, myTextures[textureName], NULL, &destination); 

where textureName is a string variable which you can alter in code in realtime.

This approach means you can load all the textures you will need before-hand and simply utilise them as needed later, meaning there's no loading from file system whilst rendering:)

There is a nice explanation of map here.

Hopefully this gives you a nudge in the right direction. Let me know if you need more info:)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top