• (2 Pages)
  • +
  • 1
  • 2

C++ Tile Engine from Scratch -- Part 2

#16 whattowrite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 13

Posted 01 March 2013 - 05:39 AM

View PostSethGandy, on 24 January 2013 - 02:32 AM, said:

Spinning off of what St0n3y provided, I made some fixes to get this code to work for me in SFML2.0
I'm using Visual Studios 2012 Express too, if that makes any difference.
The only thing that was busted after some fine tuning was the texturemanager.AddTexture(texture) call.
I had to use a pointer like texturemanager->AddTexture(texture) instead.
Hope this helps anyone who's a noob like me! :batman:/>/>/>/>/>/>

TextureManager.h
#ifndef TEXTURE_MANAGER_H
#define TEXTURE_MANAGER_H

#include <SFML/Graphics.hpp>
#include <vector>
using namespace std;

class TextureManager{
private:
vector<sf::Texture> textureList;

public:
TextureManager();
~TextureManager();

void AddTexture(sf::Texture& texture);
sf::Texture& GetTexture(int index);

};
#endif



TextureManager.cpp
#include "TextureManager.h"

TextureManager::TextureManager(){
}

TextureManager::~TextureManager(){
}

void TextureManager::AddTexture(sf::Texture& texture){
textureList.push_back(texture);
}

sf::Texture& TextureManager::GetTexture(int index){
return textureList[index];
}



Tile.h
#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

class Tile{
private:
sf::Sprite baseSprite;

public:
Tile(sf::Texture& texture);
~Tile();

void Draw(int x, int y, sf::RenderWindow* rw);
};
#endif



Tile.cpp
#include "Tile.h"
#include "SFML/Graphics.hpp"

Tile::Tile(sf::Texture& texture){
baseSprite.setTexture(texture);
}

Tile::~Tile(){
}

void Tile::Draw(int x, int y, sf::RenderWindow* rw){
baseSprite.setPosition(x, y);
rw->draw(baseSprite);
}



Engine.h
#ifndef _ENGINE_H
#define _ENGINE_H

#include <SFML\Graphics.hpp>

#include "TextureManager.h"
#include "Tile.h"

class Engine
{
private:
	//SFML Render Window
	sf::RenderWindow* window;
	TextureManager* texturemanager;

	//Initializes the engine
	bool Init();				
	//Main Game Loop
	void MainLoop();			
	//Renders one frame
	void RenderFrame();
	//Processes user input
	void ProcessInput();
	//Updates all Engine internals
	void Update();

	//Temporary
	
	void LoadTextures();
	Tile* testTile;

public:
	
	Engine();
	~Engine();
	void Go();					//Starts the engine
};

#endif



Engine.cpp
#include "Engine.h"
#include <SFML\Graphics.hpp>

Engine::Engine()
{
	
}

Engine::~Engine()
{
	delete window; 
	//this is so that the 'window = new sf::RenderWindow' is removed from memory when the engine stops)
	delete texturemanager;
	//this is so that the texturemanager is also deleted once the engine is stopped
}

bool Engine::Init()
{
	texturemanager = new TextureManager();
	window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG");
	
	if(!window)
		return false;

	LoadTextures();

	return true;
}

void Engine::LoadTextures()
{

	sf::Texture texture;
	texture.loadFromFile("sprite1.png");
	
	texturemanager->AddTexture(texture);
	testTile = new Tile(texturemanager->GetTexture(0));
}

void Engine::RenderFrame()
{
	window->clear();
	testTile->Draw(0,0, window);
	window->display();
}

void Engine::ProcessInput()
{
	sf::Event evt;
	//Loop through all window events
	while(window->pollEvent(evt))
	{
		if(evt.type == sf::Event::Closed)
			window->close();
	}
}

void Engine::Update()
{

}

void Engine::MainLoop()
{
	//Loop until our window is closed
	while(window->isOpen())
	{
		ProcessInput();
		Update();
		RenderFrame();
	}
}

void Engine::Go()
{
	if(!Init())
		throw "Could not initialize Engine";
	MainLoop();
}



Main.cpp
#include <Windows.h>

#include "Engine.h"

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
	Engine* engine = new Engine();

	try
	{
		engine->Go();
	}
	catch(char* e)
	{
		MessageBoxA(NULL, e, "Exception Occured", MB_OK | MB_IConerror);
	}
}





Hi

I have changed the names over but still get the same result i am not sure on how i can fix this the way you showed how to do it got rid of about 50 errors and thank you it was a lot of help. These are the only 2 problems i am having and would like to no why that is i have checked over your code to my code and there is nothing different between the 2 just this 1 name
your textureManager and my tileManager that is the only difference.

if you could point me in the right direction it would be much appreciated.


//Under Engine::~Engine()
{
delete window; this works fine

// but when i try to do
delete TileManager;
i get Error 1 error C2440: 'delete' : cannot convert from 'TileManager' to 'void *'
}



//Under init
bool Engine::Init()
{
tileManager = new TileManager();
I end up getting. Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type
'TileManager *' (or there is no acceptable conversion)
Was This Post Helpful? 0
  • +
  • -

#17 thyagobr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-March 13

Posted 08 March 2013 - 11:15 AM

@whattowrite

Quote

//Under Engine::~Engine()
{
delete window; this works fine

// but when i try to do
delete TileManager;
i get Error 1 error C2440: 'delete' : cannot convert from 'TileManager' to 'void *'
}


Is TileManager a pointer? Did you declare it like TileManager* TileManager? If it isn't, you don't have to delete it. If it is, I believe that you're trying to "delete the class"; try to use "delete tileManager", with the lower-case 't'.

Quote

//Under init
bool Engine::Init()
{
tileManager = new TileManager();
I end up getting. Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type
'TileManager *' (or there is no acceptable conversion)


Again, if tileManager isn't a pointer, you don't have to use "new" here. For example, in my own SFML 2.0 version of this code, I declare it like this:

Quote

class Engine {

private:
// Rendering Window
sf::RenderWindow* window;
// Texture Manager
TextureManager textureManager;
Tile* testTile;
...


I never use "new" for my TextureManager. I simply call the addTexture() method without explicitly initiating the object. My "loadManager()" on Engine.cpp is like this:

Quote

void Engine::loadTextures() {
sf::Texture sprite;
sprite.loadFromFile("grass.png");
textureManager.addTexture(sprite);

testTile = new Tile(textureManager.getTexture(0));
}


So, my destructor ~Engine() is like this:

Quote

Engine::~Engine() {
delete window;
delete testTile;
}


If these don't help, maybe if you paste your own code we can check these issues closer :)/>
Was This Post Helpful? 0
  • +
  • -

#18 skymonkier  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Posted 20 March 2013 - 06:05 AM

I've typed this all myself to get a feel for it, and I've gone back and made revisions according to the changes made with SFML 2.0. I'm using Xcode 4, and whenever I launch the project, I get a blank white screen and it freezes, saying that it had stopped at the textureManager->AddTexture(texture); in my Engine.cpp.

If anyone's used Xcode and gotten this same problem but managed to fix it, I'd appreciate the help. :)
Was This Post Helpful? 0
  • +
  • -

#19 TheDianamu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 13

Posted 27 September 2013 - 09:46 PM

Probablly grave digging, but I get the following error when I compile:

f:<work/dir>tile.cpp(6): error C2039: 'setImage' : is not a member of 'sf::Sprite'
          f:<work/dir>\sfml-2.1\include\sfml\graphics\sprite.hpp(47) : see declaration of 'sf::Sprite'


Any reason why? my Tile.cpp and Tile.h code are following:

//Tile.h

#ifndef _TILE_H
#define _TILE_H

#include <SFML\Graphics.hpp>

class Tile
{
private:
	sf::Sprite baseSprite;

public:
	Tile(sf::Image& image);
	~Tile();

	void Draw(int x, int y, sf::RenderWindow* rw);
};

#endif



//Tile.cpp
#include "Tile.h"
#include <SFML\Graphics.hpp>

Tile::Tile(sf::Image& image)
{
	baseSprite.setImage(image, true);
}

Tile::~Tile()
{

}

void Tile::Draw(int x, int y, sf::RenderWindow* rw)
{
	baseSprite.setPosition(x, y);
	rw->draw(baseSprite);
}



Any help is grateful.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2