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

Author Topic: SFML 2 Tilemap issue  (Read 6037 times)

0 Members and 2 Guests are viewing this topic.

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« on: August 02, 2011, 01:12:31 am »
Anyone know why this code
Code: [Select]


template<typename T, int size>
int GetArrLength(T(&)[size]){return size;};

int map[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};

    std::vector<sf::Image> images(2);
    images[0].LoadFromFile("grass.png");
    images[1].LoadFromFile("water.png");

    // Create the sprite
    std::vector<sf::Sprite> tiles(2);
    tiles[0] = sf::Sprite(images[0]); // Grass
    tiles[1] = sf::Sprite(images[1]); // Water

for(int x=0; x < GetArrLength(map); ++x)
{
for(int y=0; y < GetArrLength(map[x]); ++y)
{
int tileId = map[x][y];
     
            const sf::Image* image = tiles[tileId].GetImage();
             
            int width = image->GetWidth();
            int height = image->GetHeight();
     
tiles[tileId].SetPosition(x * width, y * height);
            window->Draw(tiles[tileId]);
}
}


 produces this output  with SFML 2 on Visual Studio 2010 express?


Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« Reply #1 on: August 02, 2011, 05:38:45 am »
anybody??

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 2 Tilemap issue
« Reply #2 on: August 02, 2011, 07:58:56 am »
You should check the return of LoadFromFile. Maybe it failed.

Quote
anybody??

Please wait more than 4 hours to bump a topic, it's a forum not a chat... People may be sleeping/working/whatever.
Laurent Gomila - SFML developer

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« Reply #3 on: August 02, 2011, 09:08:02 am »
Both images loaded fine actually

heres my map:

Code: [Select]

int map[25][25] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};


my output would be like it should be:



now why would my output sometimes have white?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 2 Tilemap issue
« Reply #4 on: August 02, 2011, 09:34:01 am »
It's hard to tell without seeing more code. In fact, a complete and minimal code that reproduces the problem would be very helpful.
Laurent Gomila - SFML developer

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« Reply #5 on: August 02, 2011, 10:23:41 am »
ok heres Engine.cpp:

Code: [Select]


#include <iostream>
#include "Engine.h"
#include "Link.h"

template<typename T, int size>
int GetArrLength(T(&)[size]){return size;};

Engine::Engine()
:fps(0)
{

}

Engine::~Engine()
{

}

bool Engine::Init()
{
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Legend Of Zelda: A Link To The Future");
window->SetFramerateLimit(30);
window->EnableVerticalSync(false);

    if(!window)
        return false;

    return true;
}

void Engine::Render()
{
    window->Clear();
Load_Level();
    window->Display();
}

void Engine::ProcessInput()
{
    sf::Event Event;

    while(window->PollEvent(Event))
    {
        if(Event.Type == sf::Event::Closed)
            window->Close();
link.KeyReleases();
    }
}

void Engine::Load_Level()
{
int map[25][25] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};

    std::vector<sf::Image> images(3);
    images[0].LoadFromFile("grass.png");
    images[1].LoadFromFile("rock.png");

    // Create the sprite
    std::vector<sf::Sprite> tiles(3);
    tiles[0] = sf::Sprite(images[0]); // Grass
    tiles[1] = sf::Sprite(images[1]); // rock

for(int x=0; x < GetArrLength(map); ++x)
{
for(int y=0; y < GetArrLength(map[x]); ++y)
{
int tileId = map[x][y];
     
            const sf::Image* image = tiles[tileId].GetImage();
             
            int width = image->GetWidth();
            int height = image->GetHeight();
     
tiles[tileId].SetPosition(x * width, y * height);
            window->Draw(tiles[tileId]);
}
}
}

void Engine::Update()
{
link.Movement();
}

void Engine::FramesPerSecond()
{
    fps++;
    if(Clock.GetElapsedTime() >= 1.f)
    {
int Framerate = (1.f / Clock.GetElapsedTime()) * 1000;
std::cout << "FPS: " << Framerate<< std::endl;
Clock.Reset();
    }
}

void Engine::MainLoop()
{
    while(window->IsOpened())
    {
        ProcessInput();
FramesPerSecond();
Update();
        Render();
    }
}

void Engine::Run()
{
    if(!Init())
        throw "Could Not Initialize!";

    MainLoop();
}



and heres Engine.h:

Code: [Select]


#ifndef ENGINE_H
#define ENGINE_H

#include <SFML/Graphics.hpp>
#include "Link.h"

class Engine
{
    public:
        Engine();
        ~Engine();

        void Run();

sf::RenderWindow* window;
sf::Event Event;
    private:
        bool Init();
        void MainLoop();
        void ProcessInput();
        void Render();
void Load_Level();
void Update();
void FramesPerSecond();

sf::Clock Clock;
int fps;

Link link;
};

#endif // ENGINE_H


heres main.cpp:

Code: [Select]

#include <SFML/Graphics.hpp>
#include "Engine.h"
#include "Link.h"

int main(int argc, char* args[])
{
Engine* engine = new Engine();
engine->Run();

delete engine;

return 0;

}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 2 Tilemap issue
« Reply #6 on: August 02, 2011, 10:41:11 am »
This may not explain your problem, but... why do you reload your entire map and its images 30 times per second? Do it once in the Init() function, store the images and tiles, and then have a loop that simply draws them in the Render() function.
Laurent Gomila - SFML developer

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« Reply #7 on: August 02, 2011, 11:41:00 am »
Thanks it worked perfectly

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« Reply #8 on: August 02, 2011, 11:15:35 pm »
One more issue. When i set the pixel perfect collisions from the collision class from the wiki, my framerate drops all the way to 1. Any ideas?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
SFML 2 Tilemap issue
« Reply #9 on: August 02, 2011, 11:44:30 pm »
Use more efficient (and probably less exact) collision detection algorithms ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2 Tilemap issue
« Reply #10 on: August 03, 2011, 12:16:46 am »
Pixel Perfect is the only one that really works with my game. Bounding box and circle test just dont work correctly. is there just a way to use Pixel Perfect with Tilemaps without incredible lag?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
SFML 2 Tilemap issue
« Reply #11 on: August 03, 2011, 09:51:08 pm »
Quote from: "Linux Vs God"
Pixel Perfect is the only one that really works with my game. Bounding box and circle test just dont work correctly. is there just a way to use Pixel Perfect with Tilemaps without incredible lag?
Why don't they work? For most games, bounding box should be fine.
I use the latest build of SFML2

Sam42

  • Newbie
  • *
  • Posts: 16
    • View Profile
SFML 2 Tilemap issue
« Reply #12 on: August 03, 2011, 11:12:46 pm »
I assume you've adapted the collision class on the wiki to sfml2 if that's what you're using? The new way rectangles are defined being the major change that will silently mess it up unless you've adjusted.

The other thing is what are you testing against what? Are you optimising the amount of collision checks you make, or are you testing everything against everything every frame?

Haikarainen

  • Guest
SFML 2 Tilemap issue
« Reply #13 on: August 04, 2011, 03:52:45 pm »
Linux Vs God: You can always do bounding box to pixel perfect collision!
Easy, fast, awesome.

If you're working on a lttp-like game just have like 4-8px height box and almost as wide as the character, and have it on the bottom of the char(like the feet) and check collisions on that.

 

anything