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

Author Topic: Can't get MapRenderer class to work  (Read 2421 times)

0 Members and 1 Guest are viewing this topic.

ScorpionWasp

  • Newbie
  • *
  • Posts: 6
    • View Profile
Can't get MapRenderer class to work
« on: November 06, 2015, 12:02:01 am »
Ok, I'm trying to use the MapRenderer class provided here (http://pastebin.com/SrBE69EC), and it works fine with the built in example, that draws only colored tiles with no texture, wrapping around. The moment I try to get a spritesheet into the mix, it stops working, and I get a black screen with nothing. Here's the relevant code:

#include <SFML/Graphics.hpp>
#include <TGUI/TGUI.hpp>

#include "World.h"
#include "MapRenderer.h"
using namespace sf;

#define screenWidth  1024
#define screenHeight 768
#define PI 3.14
#define fps 30
#define tileSize 32

void provider(int x, int y, int layer, Color& color, IntRect &src)
{
        //color = Color::Yellow;
        src = IntRect(10, 42, 32, 32);
}

void main()
{
        sf::Clock clock; // starts the clock
        sf::Time elapsed = elapsed.Zero;
        sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "SFML works!");

        View view = window.getDefaultView();
        Texture texture;
        texture.loadFromFile("World_A2.png");
        MapRenderer map(&texture, provider, 32, 1);
        Vector2f prev;

        tgui::Gui gui(window);
        gui.setGlobalFont("TGUI/fonts/DejaVuSans.ttf");
        World *world=new World;
        world->generate(1000,1000);
        int screenX=500, screenY=500;

        //Main Loop
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        Vector2f ms, delta;
                        switch (event.type)
                        {
                        case Event::Closed:
                                window.close();
                                break;
                        case Event::Resized:
                                view.setSize(event.size.width, event.size.height);
                                view.setCenter(view.getSize() * 0.5f);
                                break;
                        case Event::MouseMoved:
                                ms = Vector2f(event.mouseMove.x, event.mouseMove.y);
                                delta = ms - prev;
                                if (Mouse::isButtonPressed(Mouse::Button::Left))
                                        view.setCenter(view.getCenter() - delta);
                                prev = ms;
                                break;
                        case Event::KeyPressed:
                                map.refresh();
                                break;
                        }

                        gui.handleEvent(event);
                }
                elapsed += clock.restart();
                if (elapsed.asMilliseconds() > 1000 / fps) // is it time to loop again?
                {
                        elapsed -= sf::milliseconds(1000 / fps);
                        window.clear();
                        window.setView(view);

                        map.update(window);
                        window.draw(map);
//                      Sprite test;
//                      test.setTexture(texture);
//                      window.draw(test);   <-This was to test if the texture was actually loading correctly, it does
// draw the spritesheet on screen when uncommented
//GUI

                        gui.draw();
                        window.display();
                }
                else sf::sleep(sf::milliseconds(10));
        }

}


Any help?  :-[
« Last Edit: November 06, 2015, 12:18:38 am by eXpl0it3r »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't get MapRenderer class to work
« Reply #1 on: November 06, 2015, 03:50:33 am »
The default constructor of sf::Color creates black. A sprite with a black color will be black.
Try setting your color to white in the provider.
« Last Edit: November 06, 2015, 03:52:11 am by G. »

ScorpionWasp

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can't get MapRenderer class to work
« Reply #2 on: November 06, 2015, 04:57:50 am »
Tried that, started getting an opaque white instead. So I tried this:

void provider(int x, int y, int layer, Color& color, IntRect &src)
{
   color.a = 0;
   color.b = 100;
   src = IntRect(10, 42, 32, 32);
}

And that gave me the same empty blackness as before...  :(

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Can't get MapRenderer class to work
« Reply #3 on: November 06, 2015, 05:35:01 am »
Maybe because you set the alpha to 0? Did you try 255?

ScorpionWasp

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can't get MapRenderer class to work
« Reply #4 on: November 06, 2015, 06:56:37 am »
Tried several combinations of alpha and rgb. Doesn't seem that's it...

Anyone got example code of this class being used with an actual SpriteSheet?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't get MapRenderer class to work
« Reply #5 on: November 06, 2015, 09:13:55 am »
Quote
void MapRenderer::draw(sf::RenderTarget &rt, sf::RenderStates states) const
{
        states.texture = texture;
        rt.draw(&vertices[0], vertices.size(), sf::PrimitiveType::Quads);
}
You set the texture to states, but you don't use it.

rt.draw(&vertices[0], vertices.size(), sf::PrimitiveType::Quads, states);

ScorpionWasp

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can't get MapRenderer class to work
« Reply #6 on: November 10, 2015, 06:11:00 am »
That did the trick. Thank you!  :)