Jump to content

[SFML / C++] Game from scratch guide

as96

Can you get static linking to work?

I just simply cannot.

When I add all the static libraries and external dependencies, it still gives me undefined references to '_imp__" something (which are errors that appear when using dynamic linking ) , even though I wrote "SFML_STATIC" in the compiler settings/defines. Using Codeblocks.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Can you get static linking to work?

I just simply cannot.

When I add all the static libraries and external dependencies, it still gives me undefined references to '_imp__" something (which are errors that appear when using dynamic linking ) , even though I wrote "SFML_STATIC" in the compiler settings/defines. Using Codeblocks.

Never actually used static linking since I've never released any of my projects, but if you want I can add a static linking part to the guide.

Link to comment
Share on other sites

Link to post
Share on other sites

Graphics (part 3 of ???)
 
First of all sorry for the delay on this part but I haven't had enough free time this week, anyway in this part I'm going to do two things, the first is something that I like to make in my SFML projects and that is a container for my loaded resources (textures, sounds, fonts, etc. …), this isn't anything new, I've also seen other people do this since as you can guess it keeps everything clean and organized and access to resources will be tons of times easier especially if you are making an ECS (Entity Component System) and it will obviously allow you to avoid loading the same resource over and over consuming memory and CPU, I've seen that some users make global variables but that's gonna get messy and it's not something that I would use in my projects.
 
So, let's jump into Visual Studio (or whatever you are using) and create two new files: “ResourceContainers.h” and “ResourceContainers.cpp”, if you prefer using another name or extension feel free to do it but if you follow mines you'll probably have an easier time later since you don't have to worry about including the correct headers.
 
For now we just need the textures to be stored so there aren't many functions right now:
#include <string>#include <map>#include <SFML/Graphics/Texture.hpp>class ResourceContainer{public:	static bool LoadTexture(std::string filePath, std::string textureID);	static sf::Texture* GetTexture(std::string textureID);	static void DeleteTexture(std::string textureID);private:	static std::map<std::string, sf::Texture*> mTextures;};
Usually I separate textures and other resources and put them in a “Resource” namespace but for this guide I'm keeping things simple and doing a single completely static class, and if you want you can even use templates since most of the SFML's resource classes have the same functions for loading, for example if something can be loaded from file is going to have a “loadFromFile()” method for sure.
 
Now as you can see in my code I'm gonna store the textures (and in future the other resources too) in a map to keep things nice and organized and make substitutions easy.
 
While in the “.cpp” file the code is going to be like this:
#include "ResourceContainers.h"std::map<std::string, sf::Texture*> ResourceContainer::mTextures;bool ResourceContainer::LoadTexture(std::string filePath, std::string textureID){	if (mTextures[textureID] == nullptr)		mTextures[textureID] = new sf::Texture;	if (!mTextures[textureID]->loadFromFile(filePath))	{		delete mTextures[textureID];		return false;	}	return true;}sf::Texture* ResourceContainer::GetTexture(std::string textureID){	return mTextures[textureID];}void ResourceContainer::DeleteTexture(std::string textureID){	if (mTextures[textureID] != nullptr)		delete mTextures[textureID];}

The code is pretty basic and it would usually require a bit of cleanup/optimization but since this guide is just to show you SFML I'm leaving it like (it works don't worry :P) and let's continue by analyzing each function.

bool ResourceContainer::LoadTexture(std::string filePath, std::string textureID){	if (mTextures[textureID] == nullptr)		mTextures[textureID] = new sf::Texture;	if (!mTextures[textureID]->loadFromFile(filePath))	{		delete mTextures[textureID];		return false;	}	return true;}

This is our load method and what it does is checking if there is a texture that's already loaded with that id, if there isn't it allocates a new space for it in memory and then continue by loading the texture, that's done inside and if so if it fails to load it's gonna delete the var and then return a good old false, if it doesn't spit out any error is returning a true.

sf::Texture* ResourceContainer::GetTexture(std::string textureID){	return mTextures[textureID];}

This doesn't really need any explaination since it's just a single line and it's something that you should know by now.

void ResourceContainer::DeleteTexture(std::string textureID){	if (mTextures[textureID] != nullptr)		delete mTextures[textureID];}

And finally this is our delete method which is also extremely basic, what it does is simple, it checks if there is a texture with that id and if there is it deletes it.

 

You can now go ahead and include this header in my case is:

#include "ResourceContainers.h"

but if you picked another name you will have to change it, now in our main we had some code already so maybe comment that out just for now so you can test if the container is loading everything correctly and put this one instead:

	ResourceContainer::LoadTexture("Resources/LinusSprites/linus1.bmp", "linus");	sf::Sprite lSp;	lSp.setTexture(*ResourceContainer::GetTexture("linus"));	while (window.isOpen())	{		sf::Event event;		while (window.pollEvent(event))		{			if (event.type == sf::Event::Closed)				window.close();		}		window.clear();		window.draw(lSp);		window.display();	}

Everything should be working correctly but if you have any error in your code just let me know, and if you decided to go with another approach (maybe templates or separate classes as I usually do) feel free to share it with everyone, I will add it to the index (if you agree of course).

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×