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

Author Topic: Help with logic behind displaying game map?  (Read 4193 times)

0 Members and 1 Guest are viewing this topic.

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
Help with logic behind displaying game map?
« on: November 11, 2011, 06:38:53 pm »
I am very new to using the SFML library, and C++ in general. However I am trying to wrap my head around some SFML best practices in loading images and displaying sprites.

I have a txt file that contains what I want my level(s) to look like. I currently read in the lines and will iterate through each character to display tiles for my map.

My question is how would I go about loading and keeping track of my images and sprites in regards to SFML best practices?

what I am thinking is just creating an image and sprite for each tile type and then rendering it based on my map.

My other problem is actually drawing the tiles to the screen, right now I am looping though my map character by character to display each tile, it does this once each frame, which is not ideal as it lags everything out. How can I display my map and only redraw tiles that need to be redrawn? I.E a mob or player moves, only drawing the floor once etc?

Code: [Select]
Class Map
{
Public:
std::Vector<std::string> ReadMap()
{
 std::string line;
       std::vector<std::string> vec;
       std::ifstream myfile("lvl/lvl1.txt");

        if(myfile.is_open()){
            while(getline(myfile, line)){ //this will keep the loop goign and grab a new line each iteration
                 vec.push_back(line);
            }

            myfile.close();
        }
        else{
            std::cout << "Unable to open File!\n";
        }
        for(unsigned int i = 0; i < vec.size(); i++){ //iterate through vecotor and display line by line to make sure map is reading right
                    std::cout<<vec[i]<<std::endl;
                 }
    return vec;
}


void DisplayMap()
{
int x,y;
    for(unsigned int i = 0; i < levelMap.size(); i++){
        for(unsigned int j = 0; j < levelMap[i].size() + 1; j++){
        std::cout<<levelMap[i][j];
        y = i * 32;
        x = j * 32;
        window.Draw(floorSprite); //Draw floors on every space first
        if (levelMap[i][j] == '#'){
            window.Draw(wallSprite);
            }
        }
        }
}

private:
sf::Image floor;
sf::Image wall;

sf::Sprite Floor;
sf::Sprite Wall;

}

Thank you all for your input.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Help with logic behind displaying game map?
« Reply #1 on: November 12, 2011, 11:25:36 am »
Don't use more sf::Image objects than necessary. Share images among multiple sprites that use them. sf::Sprite is -- in contrary to sf::Image -- a lightweight class which can be copied fast.

And only draw the visible tiles. But redraw everything which is visible, there's mostly no gain in trying to find out what has changed and what hasn't.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
Help with logic behind displaying game map?
« Reply #2 on: November 12, 2011, 01:29:25 pm »
Quote from: "Nexus"
Don't use more sf::Image objects than necessary. Share images among multiple sprites that use them. sf::Sprite is -- in contrary to sf::Image -- a lightweight class which can be copied fast.

And only draw the visible tiles. But redraw everything which is visible, there's mostly no gain in trying to find out what has changed and what hasn't.


I had read somewhere about the image / sprite separation, so I am currently loading 1 image and 1 sprite per tile. This appears to work fine by drawing the same sprite over and over in different positions.

I am currently trying to figure out how to only draw what is visible, I am thinking that the SFML::View may be a good way to do so, but I am not sure and will be playing around with that in the coming days.

Thanks!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Help with logic behind displaying game map?
« Reply #3 on: November 12, 2011, 02:21:31 pm »
Quote from: "sec_goat"
I had read somewhere about the image / sprite separation, so I am currently loading 1 image and 1 sprite per tile. This appears to work fine by drawing the same sprite over and over in different positions.
Do you mean one image in total, or one per tile? The intention is to have as few images as possible, and to share them among multiple lightweight sprites. That is, tiles that look the same only require one common image.

Quote from: "sec_goat"
I am currently trying to figure out how to only draw what is visible, I am thinking that the SFML::View may be a good way to do so, but I am not sure and will be playing around with that in the coming days.
You can find it out by testing whether the object coordinates are outside of the screen. Usually, this amounts to 4 inequations per object.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Help with logic behind displaying game map?
« Reply #4 on: November 12, 2011, 10:04:02 pm »
You have a good ResourceManager implementation on the Wiki that you can use for images and other resources. This way you'll load images only once.

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
Help with logic behind displaying game map?
« Reply #5 on: November 15, 2011, 06:48:10 pm »
Quote from: "Nexus"
Quote from: "sec_goat"
I had read somewhere about the image / sprite separation, so I am currently loading 1 image and 1 sprite per tile. This appears to work fine by drawing the same sprite over and over in different positions.
Do you mean one image in total, or one per tile? The intention is to have as few images as possible, and to share them among multiple lightweight sprites. That is, tiles that look the same only require one common image.

Quote from: "sec_goat"
I am currently trying to figure out how to only draw what is visible, I am thinking that the SFML::View may be a good way to do so, but I am not sure and will be playing around with that in the coming days.
You can find it out by testing whether the object coordinates are outside of the screen. Usually, this amounts to 4 inequations per object.


Thanks Nexus, I am notoriously bad at not communicating clearly. I am loading 1 image per tile type, and then rendering he 1 sprite per tiletype over and over again, seems to work well so far.  I think I understand the way to make sure I am not rendering outside the players view.

Thanks for everyone's help!

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
new Question!
« Reply #6 on: November 15, 2011, 09:39:12 pm »
Ok so I am making a little bit of progress in regards to displaying my map and handling images etc.

The new question I have that is in this same vein is, how do I create a section for specific types of rendering? I.E. I have a 1024x768 window and I would like to set aside 600x400 for rendering my map. I tried creating a new sf::RenderWindow, but that creates a whole new window.  I think I am looking for something like an area. . . Thanks again every one! :D

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Help with logic behind displaying game map?
« Reply #7 on: November 16, 2011, 03:20:32 am »

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
Help with logic behind displaying game map?
« Reply #8 on: November 16, 2011, 02:40:27 pm »
Quote from: "Tex Killer"
You are looking for the views.
http://www.sfml-dev.org/tutorials/1.6/graphics-views.php


I am missing something about the view, probably obvious and simple as I tend to over-think my problems. here is what I am doing so far ( not a the complete code sample but you should get the gist).

Code: [Select]

sf::View currentView; //create the view
currentView.SetFromRect(sf::FloatRect(0, 0, 1000, 1000));

//move the view to center on player
sf::Vector2f temp_pos = player->GetPosition();
currentView.SetCenter(temp_pos);


My problem is that when I try to feed my view different rectangles to initialize it, it always ends up filling the whole RenderWindow and not just say 800x600 or whatever I set them to.

How do I go about implementing this in a more correct manner?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Help with logic behind displaying game map?
« Reply #9 on: November 16, 2011, 02:43:11 pm »
Quote
My problem is that when I try to feed my view different rectangles to initialize it, it always ends up filling the whole RenderWindow and not just say 800x600 or whatever I set them to.

The rectangle that you pass to the view is the source rectangle, ie. the area of the scene that will be mapped to the entire render window. It defines what you see, not where you see it.
To define the destination rectangle, ie. the area of the render window where the source rectangle will be mapped, you need to play with the viewport (SFML 2 only).
Laurent Gomila - SFML developer

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
Help with logic behind displaying game map?
« Reply #10 on: November 16, 2011, 03:17:30 pm »
Quote from: "Laurent"
Quote
My problem is that when I try to feed my view different rectangles to initialize it, it always ends up filling the whole RenderWindow and not just say 800x600 or whatever I set them to.

The rectangle that you pass to the view is the source rectangle, ie. the area of the scene that will be mapped to the entire render window. It defines what you see, not where you see it.
To define the destination rectangle, ie. the area of the render window where the source rectangle will be mapped, you need to play with the viewport (SFML 2 only).


Awesome thanks! I will have to look at building SFML 2 again, I had some issues following the tutorial because I did not make my make file correctly I will bet you.

 

anything