Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Placing image on the screen - problem.  (Read 2494 times)

0 Members and 1 Guest are viewing this topic.

Kyubey

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Placing image on the screen - problem.
« on: October 09, 2013, 12:48:40 am »
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];
}
 
« Last Edit: October 09, 2013, 01:06:54 am by Kyubey »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Placing image on the screen - problem.
« Reply #1 on: October 09, 2013, 12:58:35 am »
You need to tell us what actually happens and what you expected to happen, so we can at least begin to guess where the problem is.  For all we know, the loadFromFile call is failing because the image file isn't in the working directory.  If you use a debugger you should be able to rule out things like that and at least determine if it's going through all the initialization routines you think it is.

I don't see anything obviously wrong with the code, though I'm terrible at understanding Texture scope so there might be an ordinary "white square problem" in there.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Placing image on the screen - problem.
« Reply #2 on: October 09, 2013, 01:01:31 am »
Please read this thread.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Kyubey

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Placing image on the screen - problem.
« Reply #3 on: October 09, 2013, 01:05:58 am »
Ok, I'm still up so I'll add what exactly is my problem. As I said I'm getting really sleepy so I didn't think the post through... :s 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!

@Up
Edited 1st post with details
« Last Edit: October 09, 2013, 01:07:30 am by Kyubey »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Placing image on the screen - problem.
« Reply #4 on: October 09, 2013, 01:07:58 am »
You really should tell us what the assertion error was.  And when you're more awake, use a debugger on this.

Incidentally I tested the code on my system with a small image and it displays just fine.

Kyubey

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Placing image on the screen - problem.
« Reply #5 on: October 09, 2013, 01:10:30 am »
Ok, I'll post the assertion error tomorrow then. Thanks for testing the code tho, now I won't spend all the time thinking what I did wrong :)

Kyubey

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Placing image on the screen - problem.
« Reply #6 on: October 09, 2013, 07:57:36 am »
SOLVED! Turns out it really was just a dumb mistake. My picture was bigger than the size I put in my code