Hello, I have a problem with getting my image on the screen. Since I only started learning SFML today, I probably just did some dumb mistake due to my lack of knowledge and understanding, but I can't figure out what it is and I'm getting sleepy... If you have some time, please take a look and point it out if you can find it. Thanks in advance!
#Edit: Since I didn't put the more detailed explanation of my problem, I'm doing it now: Ok so, I wanted to put my test image (the one from Engine::LoadImages() - it is in the right folder) on the screen. What I get is some strange assertion error (thus my assumption that I made a mistake in the code) and after I skip the error, I get a black screen (probably meaning, that it's doing the "window clear" stage) That's all I can tell with my tired mind - I hope it helps!
(If this subforum isn't for such requests, then I'm sorry and simply ignore this thread)
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);
}
}
Engine.h
#ifndef _ENGINE_H
#define _ENGINE_H
#include <SFML\Graphics.hpp>
#include "ImageManager.h"
#include "Tile.h"
class Engine
{
private:
//Image'n'tile stuff
ImageManager imageManager;
void LoadImages();
Tile* testTile;
//Render that window
sf::RenderWindow *window;
//Starto!
bool Init();
//Main Game loop
void MainLoop();
//Render one frame
void RenderFrame();
//Processes user input
void ProcessInput();
//Update engine intervals
void Update();
public:
Engine();
~Engine();
//start that engine
void Go();
};
#endif
Engine.cpp
#include "Engine.h"
#include <SFML\Graphics.hpp>
Engine::Engine()
{
}
Engine::~Engine()
{
}
//image stuff
void Engine::LoadImages()
{
sf::Image image;
image.loadFromFile("test.png");
imageManager.AddImage(image);
testTile = new Tile(imageManager.GetImage(0));
}
void Engine::RenderFrame()
{
window->clear();
testTile->Draw(0,0, window);
window->display();
}
//startup stuff \/
bool Engine::Init()
{
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "IncuEngine");
if(!window)
return false;
LoadImages();
return true;
}
//basic user input process
void Engine::ProcessInput()
{
sf::Event event;
while (window->pollEvent(event))
{
// Request for closing the window
if (event.type == sf::Event::Closed)
window->close();
}
}
void Engine::Update()
{
}
void Engine::MainLoop()
{
while(window->isOpen())
{
ProcessInput();
Update();
RenderFrame();
}
}
void Engine::Go()
{
if(!Init())
throw "Engine initialization failed.";
MainLoop();
}
Tile.h
#ifndef _TILE_H
#define _TILE_H
#include <SFML\Graphics.hpp>
class Tile
{
private:
sf::Texture baseTexture;
sf::Sprite baseSprite;
public:
Tile(sf::Image& image);
~Tile(void);
void Draw(int x, int y, sf::RenderWindow* rw);
};
#endif
Tile.cpp
#include "Tile.h"
Tile::Tile(sf::Image& image)
{
baseTexture.create(200,200);
baseTexture.update(image);
}
Tile::~Tile(void)
{
}
void Tile::Draw(int x, int y, sf::RenderWindow* rw)
{
baseSprite.setTexture(baseTexture);
baseSprite.setPosition(x, y);
rw->draw(baseSprite);
}
ImageManager.h
#ifndef _IMAGEMANAGER_H
#define _IMAGEMANAGER_H
#include <vector>
#include <SFML\Graphics.hpp>
class ImageManager
{
private:
std::vector <sf::Image> imageList;
public:
ImageManager(void);
~ImageManager(void);
void AddImage(sf::Image& image);
sf::Image& GetImage(int index);
};
#endif
Image.cpp
#include "ImageManager.h"
ImageManager::ImageManager(void)
{
}
ImageManager::~ImageManager(void)
{
}
void ImageManager::AddImage(sf::Image& image)
{
imageList.push_back(image);
}
sf::Image& ImageManager::GetImage(int index)
{
return imageList[index];
}