Advertisement

Cannot define "TextureManager" class

Started by October 13, 2019 09:55 PM
2 comments, last by wintertime 5 years, 1 month ago

Hi I'm building my first game engine out of the book SDL Game Development by Shaun Mitchell, I got to the stage of adding a texture manager class tot the engine but the compiler won't letme define it. Why?

console output

error: ‘TextureManager’ does not name a type; did you mean ‘__TextureManager__’?

 TextureManager m_textureManager;
 ^~~~~~~~~~~~~~
 __TextureManager__
In file included from main.cpp:1:0:
Game.cpp: In member function ‘bool Game::init(const char*, int, int, int, int, int)’:
Game.cpp:48:3: error: ‘m_textureManager’ was not declared in this scope
   m_textureManager.load("assets/animate-alpha.png","animate",
   ^~~~~~~~~~~~~~~~
Game.cpp:48:3: note: suggested alternative: ‘__TextureManager__’
   m_textureManager.load("assets/animate-alpha.png","animate",
   ^~~~~~~~~~~~~~~~
   __TextureManager__
Game.cpp: In member function ‘void Game::render()’:
Game.cpp:80:1: error: ‘m_textureManager’ was not declared in this scope
 m_textureManager.draw("animate", 0,0,128,82, m_pRenderer);
 ^~~~~~~~~~~~~~~~
Game.cpp:80:1: note: suggested alternative: ‘__TextureManager__’
 m_textureManager.draw("animate", 0,0,128,82, m_pRenderer);
 ^~~~~~~~~~~~~~~~
 __TextureManager__
 here are my files

please help.

 

Thanks

main.cpp

 


#include "Game.cpp"
#include "TextureManager.cpp"

//our game object
Game* g_game = 0;

int main(int argc, char* argv[])
{
g_game = new Game();

g_game->init("Chapter 1",100,100,640,480,SDL_WINDOW_SHOWN);

while(g_game->running())
{
g_game->handleEvents();
g_game->update();
g_game->render();
g_game->clean();
}

return 0;
}

Game.h


#ifndef __Game__
#define __Game__

#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>

class Game
{
public:
Game() {}
~Game() {}

// init function passed to SDL_CreateWindow
bool init(const char* title, int xpos, int ypos, int width,
    int height, int flags);

void render();
void update();
void handleEvents();
void clean();

// a function to access the private running variable
bool running() { return m_bRunning; }

private:

SDL_Window* m_pWindow = 0;
SDL_Renderer* m_pRenderer = 0;

SDL_Texture* m_pTexture= 0; // the new SDL_Texture variable
//SDL_Rect m_sourceRectangle; // the first rectangle
//SDL_Rect m_destinationRectangle; // another rectangle
bool m_bRunning;

int m_currentFrame;
TextureManager m_textureManager;

};

#endif /* defined(__Game__) */

Game.cpp


#include "Game.h"
#include<iostream>

    bool Game::init(const char* title, int xpos, int ypos, int width,
    int height, int flags)
    {
		m_bRunning = true; 
		//attempt to initialize SDL
			if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
			{
			std::cout<<"SDL init sucess\n";
            //init the window
            m_pWindow = SDL_CreateWindow(title, xpos, ypos,
            width, height, flags);
				if(m_pWindow !=0) // window init success
				{
				std::cout << "window creation success\n";
				m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
    
					if(m_pRenderer !=0) // renderer init success
					{
					std::cout << "renderer creation success\n";
					SDL_SetRenderDrawColor(m_pRenderer,
					255,0,0,255);
					}
					else
					{
					std::cout<<"renderer init fail\n";
					return false; // renderer init fail
					}
				}
				else
				{
				std::cout <<"window init fail\n";
				return false; // window init fail
				}
			}
			else
			{
			std::cout<<"SDL init fail\n";
			return false; // SDL init fail
			}

		std::cout <<"init success\n";
		m_bRunning= true; // everything inited successfully,
		//start the main loop
		
		m_textureManager.load("assets/animate-alpha.png","animate",
		m_pRenderer);
		
		//SDL_Surface* pTempSurface = IMG_Load("assets/animate-alpha.png");
		
		//m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);
		
		//SDL_FreeSurface(pTempSurface);
		
		//SDL_QueryTexture(m_pTexture, NULL, NULL,
		//&m_sourceRectangle.w, &m_sourceRectangle.h);
		
		//m_destinationRectangle.x = m_sourceRectangle.x = 0;
		//m_destinationRectangle.y = m_sourceRectangle.y = 0;
		//m_destinationRectangle.w = 128;
		//m_destinationRectangle.h = 82;
		//m_sourceRectangle.w = 128;
		//m_sourceRectangle.h = 82;
		//m_sourceRectangle.x = 0;
		//m_sourceRectangle.y = 0;
		//m_destinationRectangle.x = 0;
		//m_destinationRectangle.y = 0;
		
		return true;
		
		}

void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the
// draw color

m_textureManager.draw("animate", 0,0,128,82, m_pRenderer);

m_textureManager.drawFrame("animate",100,100,128,82);

//SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,
//&m_destinationRectangle);

SDL_RenderPresent(m_pRenderer); // draw to the screen
}

void Game::update()
{
	m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
}
 
void Game::handleEvents()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
    switch (event.type)
    {
    case SDL_QUIT:
        m_bRunning = false;
    break;

    default:
    break;
}
}
}


void Game::clean()
{
std::cout<<"cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

TextureManager.h

 


#ifndef __TextureManager__
#define __TextureManager__

#include<map>
#include<string>

class TextureManager
{
	public:
bool load(std::string filename, std::string id,
SDL_Renderer* pRenderer);

//draw
void draw(std::string id, int x, int y, int width, int
height, SDL_Renderer* pRenderer, SDL_RendererFlip flip =
SDL_FLIP_NONE);

//draw function

void drawFrame(std::string id, int x, int y, int width, int
height, int currentRow, int currentFrame, SDL_Renderer*
pRenderer, SDL_RendererFlip flip= SDL_FLIP_NONE);

std::map<std::string, SDL_Texture*> m_textureMap;
	
	private:
};

#endif /* defined(__TextureManager__) */

TextureManager.cpp

 


#include "TextureManager.h"

bool TextureManager::load(std::string fileName, std::string
id, SDL_Renderer* pRenderer)
{
	SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
	
	if(pTempSurface == 0)
	{	
	return false;
	}
	
	SDL_Texture* pTexture =
	SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
	
	SDL_FreeSurface(pTempSurface);
	
	// everything went ok, add the texture to our list
	if(pTexture != 0)
	{
	m_textureMap[id] = pTexture;
	return true;
	}

	// reaching here means something went wrong

	return false;

	}	

void TextureManager::draw(std::string id, int x, int y,
int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
{
	SDL_Rect srcRect;
	SDL_Rect destRect;
	
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = destRect.w = width;
	srcRect.h = destRect.h = height;
	destRect.x = x;
	destRect.y = y;
	
	SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect,
	&destRect, 0, 0, flip);
}

void TextureManager::drawFrame(std::string id, int x, int y,
int width, int height, int currentRow, int currentFrame,
SDL_Renderer *pRenderer, SDL_RendererFlip flip)
{
	SDL_Rect srcRect;
	SDL_Rect destRect;
	srcRect.x = width * currentFrame;
	srcRect.y = height * currentRow -1;
	srcRect.w = destRect.w = width;
	srcRect.h = destRect.h = height;
	destRect.x = x;
	destRect.y = y;
	
	SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect,
	&destRect, 0, 0, flip);
} 

 

In main.cpp, you're #include'ing Game.cpp and TextureManager.cpp, instead of Game.h and TextureManager.cpp.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Advertisement

game.h needs an


#include "TextureManager.h"

Additionally, its weird to include .cpp files.

This topic is closed to new replies.

Advertisement